Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- StackID.cpp ---------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 10 | // C Includes |
| 11 | // C++ Includes |
| 12 | // Other libraries and framework includes |
| 13 | // Project includes |
Eugene Zelenko | d70a6e7 | 2016-02-18 18:52:47 +0000 | [diff] [blame] | 14 | #include "lldb/Target/StackID.h" |
Greg Clayton | 59e8fc1c | 2010-08-30 18:11:35 +0000 | [diff] [blame] | 15 | #include "lldb/Core/Stream.h" |
| 16 | #include "lldb/Symbol/Block.h" |
| 17 | #include "lldb/Symbol/Symbol.h" |
| 18 | #include "lldb/Symbol/SymbolContext.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace lldb_private; |
| 21 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 22 | void StackID::Dump(Stream *s) { |
| 23 | s->Printf("StackID (pc = 0x%16.16" PRIx64 ", cfa = 0x%16.16" PRIx64 |
| 24 | ", symbol_scope = %p", |
| 25 | m_pc, m_cfa, static_cast<void *>(m_symbol_scope)); |
| 26 | if (m_symbol_scope) { |
| 27 | SymbolContext sc; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 28 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 29 | m_symbol_scope->CalculateSymbolContext(&sc); |
| 30 | if (sc.block) |
| 31 | s->Printf(" (Block {0x%8.8" PRIx64 "})", sc.block->GetID()); |
| 32 | else if (sc.symbol) |
| 33 | s->Printf(" (Symbol{0x%8.8x})", sc.symbol->GetID()); |
| 34 | } |
| 35 | s->PutCString(") "); |
Greg Clayton | 59e8fc1c | 2010-08-30 18:11:35 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 38 | bool lldb_private::operator==(const StackID &lhs, const StackID &rhs) { |
| 39 | if (lhs.GetCallFrameAddress() != rhs.GetCallFrameAddress()) |
Greg Clayton | 6dadd50 | 2010-09-02 21:44:10 +0000 | [diff] [blame] | 40 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 41 | |
| 42 | SymbolContextScope *lhs_scope = lhs.GetSymbolContextScope(); |
| 43 | SymbolContextScope *rhs_scope = rhs.GetSymbolContextScope(); |
| 44 | |
| 45 | // Only compare the PC values if both symbol context scopes are nullptr |
| 46 | if (lhs_scope == nullptr && rhs_scope == nullptr) |
| 47 | return lhs.GetPC() == rhs.GetPC(); |
| 48 | |
| 49 | return lhs_scope == rhs_scope; |
| 50 | } |
| 51 | |
| 52 | bool lldb_private::operator!=(const StackID &lhs, const StackID &rhs) { |
| 53 | if (lhs.GetCallFrameAddress() != rhs.GetCallFrameAddress()) |
| 54 | return true; |
| 55 | |
| 56 | SymbolContextScope *lhs_scope = lhs.GetSymbolContextScope(); |
| 57 | SymbolContextScope *rhs_scope = rhs.GetSymbolContextScope(); |
| 58 | |
| 59 | if (lhs_scope == nullptr && rhs_scope == nullptr) |
| 60 | return lhs.GetPC() != rhs.GetPC(); |
| 61 | |
| 62 | return lhs_scope != rhs_scope; |
| 63 | } |
| 64 | |
| 65 | bool lldb_private::operator<(const StackID &lhs, const StackID &rhs) { |
| 66 | const lldb::addr_t lhs_cfa = lhs.GetCallFrameAddress(); |
| 67 | const lldb::addr_t rhs_cfa = rhs.GetCallFrameAddress(); |
| 68 | |
| 69 | // FIXME: We are assuming that the stacks grow downward in memory. That's not |
| 70 | // necessary, but true on |
| 71 | // all the machines we care about at present. If this changes, we'll have to |
| 72 | // deal with that. The ABI is the |
| 73 | // agent who knows this ordering, but the StackID has no access to the ABI. |
| 74 | // The most straightforward way |
| 75 | // to handle this is to add a "m_grows_downward" bool to the StackID, and set |
| 76 | // it in the constructor. |
| 77 | // But I'm not going to waste a bool per StackID on this till we need it. |
| 78 | |
| 79 | if (lhs_cfa != rhs_cfa) |
| 80 | return lhs_cfa < rhs_cfa; |
| 81 | |
| 82 | SymbolContextScope *lhs_scope = lhs.GetSymbolContextScope(); |
| 83 | SymbolContextScope *rhs_scope = rhs.GetSymbolContextScope(); |
| 84 | |
| 85 | if (lhs_scope != nullptr && rhs_scope != nullptr) { |
| 86 | // Same exact scope, lhs is not less than (younger than rhs) |
| 87 | if (lhs_scope == rhs_scope) |
| 88 | return false; |
| 89 | |
| 90 | SymbolContext lhs_sc; |
| 91 | SymbolContext rhs_sc; |
| 92 | lhs_scope->CalculateSymbolContext(&lhs_sc); |
| 93 | rhs_scope->CalculateSymbolContext(&rhs_sc); |
| 94 | |
| 95 | // Items with the same function can only be compared |
| 96 | if (lhs_sc.function == rhs_sc.function && lhs_sc.function != nullptr && |
| 97 | lhs_sc.block != nullptr && rhs_sc.function != nullptr && |
| 98 | rhs_sc.block != nullptr) { |
| 99 | return rhs_sc.block->Contains(lhs_sc.block); |
| 100 | } |
| 101 | } |
| 102 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 103 | } |