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 10 #include "lldb/Target/StackID.h" 11 #include "lldb/Symbol/Block.h" 12 #include "lldb/Symbol/Symbol.h" 13 #include "lldb/Symbol/SymbolContext.h" 14 #include "lldb/Utility/Stream.h" 15 16 using namespace lldb_private; 17 18 void StackID::Dump(Stream *s) { 19 s->Printf("StackID (pc = 0x%16.16" PRIx64 ", cfa = 0x%16.16" PRIx64 20 ", symbol_scope = %p", 21 m_pc, m_cfa, static_cast<void *>(m_symbol_scope)); 22 if (m_symbol_scope) { 23 SymbolContext sc; 24 25 m_symbol_scope->CalculateSymbolContext(&sc); 26 if (sc.block) 27 s->Printf(" (Block {0x%8.8" PRIx64 "})", sc.block->GetID()); 28 else if (sc.symbol) 29 s->Printf(" (Symbol{0x%8.8x})", sc.symbol->GetID()); 30 } 31 s->PutCString(") "); 32 } 33 34 bool lldb_private::operator==(const StackID &lhs, const StackID &rhs) { 35 if (lhs.GetCallFrameAddress() != rhs.GetCallFrameAddress()) 36 return false; 37 38 SymbolContextScope *lhs_scope = lhs.GetSymbolContextScope(); 39 SymbolContextScope *rhs_scope = rhs.GetSymbolContextScope(); 40 41 // Only compare the PC values if both symbol context scopes are nullptr 42 if (lhs_scope == nullptr && rhs_scope == nullptr) 43 return lhs.GetPC() == rhs.GetPC(); 44 45 return lhs_scope == rhs_scope; 46 } 47 48 bool lldb_private::operator!=(const StackID &lhs, const StackID &rhs) { 49 if (lhs.GetCallFrameAddress() != rhs.GetCallFrameAddress()) 50 return true; 51 52 SymbolContextScope *lhs_scope = lhs.GetSymbolContextScope(); 53 SymbolContextScope *rhs_scope = rhs.GetSymbolContextScope(); 54 55 if (lhs_scope == nullptr && rhs_scope == nullptr) 56 return lhs.GetPC() != rhs.GetPC(); 57 58 return lhs_scope != rhs_scope; 59 } 60 61 bool lldb_private::operator<(const StackID &lhs, const StackID &rhs) { 62 const lldb::addr_t lhs_cfa = lhs.GetCallFrameAddress(); 63 const lldb::addr_t rhs_cfa = rhs.GetCallFrameAddress(); 64 65 // FIXME: We are assuming that the stacks grow downward in memory. That's not 66 // necessary, but true on 67 // all the machines we care about at present. If this changes, we'll have to 68 // deal with that. The ABI is the agent who knows this ordering, but the 69 // StackID has no access to the ABI. The most straightforward way to handle 70 // this is to add a "m_grows_downward" bool to the StackID, and set it in the 71 // constructor. But I'm not going to waste a bool per StackID on this till we 72 // need it. 73 74 if (lhs_cfa != rhs_cfa) 75 return lhs_cfa < rhs_cfa; 76 77 SymbolContextScope *lhs_scope = lhs.GetSymbolContextScope(); 78 SymbolContextScope *rhs_scope = rhs.GetSymbolContextScope(); 79 80 if (lhs_scope != nullptr && rhs_scope != nullptr) { 81 // Same exact scope, lhs is not less than (younger than rhs) 82 if (lhs_scope == rhs_scope) 83 return false; 84 85 SymbolContext lhs_sc; 86 SymbolContext rhs_sc; 87 lhs_scope->CalculateSymbolContext(&lhs_sc); 88 rhs_scope->CalculateSymbolContext(&rhs_sc); 89 90 // Items with the same function can only be compared 91 if (lhs_sc.function == rhs_sc.function && lhs_sc.function != nullptr && 92 lhs_sc.block != nullptr && rhs_sc.function != nullptr && 93 rhs_sc.block != nullptr) { 94 return rhs_sc.block->Contains(lhs_sc.block); 95 } 96 } 97 return false; 98 } 99