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 | |
Greg Clayton | 59e8fc1c | 2010-08-30 18:11:35 +0000 | [diff] [blame] | 22 | void |
| 23 | StackID::Dump (Stream *s) |
| 24 | { |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 25 | s->Printf("StackID (pc = 0x%16.16" PRIx64 ", cfa = 0x%16.16" PRIx64 ", symbol_scope = %p", |
| 26 | m_pc, m_cfa, static_cast<void*>(m_symbol_scope)); |
Greg Clayton | 59e8fc1c | 2010-08-30 18:11:35 +0000 | [diff] [blame] | 27 | if (m_symbol_scope) |
| 28 | { |
| 29 | SymbolContext sc; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 30 | |
Greg Clayton | 59e8fc1c | 2010-08-30 18:11:35 +0000 | [diff] [blame] | 31 | m_symbol_scope->CalculateSymbolContext (&sc); |
| 32 | if (sc.block) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 33 | s->Printf(" (Block {0x%8.8" PRIx64 "})", sc.block->GetID()); |
Greg Clayton | 59e8fc1c | 2010-08-30 18:11:35 +0000 | [diff] [blame] | 34 | else if (sc.symbol) |
| 35 | s->Printf(" (Symbol{0x%8.8x})", sc.symbol->GetID()); |
| 36 | } |
| 37 | s->PutCString(") "); |
| 38 | } |
| 39 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 40 | bool |
| 41 | lldb_private::operator== (const StackID& lhs, const StackID& rhs) |
| 42 | { |
Greg Clayton | 6dadd50 | 2010-09-02 21:44:10 +0000 | [diff] [blame] | 43 | if (lhs.GetCallFrameAddress() != rhs.GetCallFrameAddress()) |
| 44 | return false; |
| 45 | |
| 46 | SymbolContextScope *lhs_scope = lhs.GetSymbolContextScope(); |
| 47 | SymbolContextScope *rhs_scope = rhs.GetSymbolContextScope(); |
| 48 | |
Eugene Zelenko | d70a6e7 | 2016-02-18 18:52:47 +0000 | [diff] [blame] | 49 | // Only compare the PC values if both symbol context scopes are nullptr |
| 50 | if (lhs_scope == nullptr && rhs_scope == nullptr) |
Greg Clayton | 6dadd50 | 2010-09-02 21:44:10 +0000 | [diff] [blame] | 51 | return lhs.GetPC() == rhs.GetPC(); |
| 52 | |
| 53 | return lhs_scope == rhs_scope; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | bool |
| 57 | lldb_private::operator!= (const StackID& lhs, const StackID& rhs) |
| 58 | { |
Greg Clayton | 6dadd50 | 2010-09-02 21:44:10 +0000 | [diff] [blame] | 59 | if (lhs.GetCallFrameAddress() != rhs.GetCallFrameAddress()) |
| 60 | return true; |
| 61 | |
| 62 | SymbolContextScope *lhs_scope = lhs.GetSymbolContextScope(); |
| 63 | SymbolContextScope *rhs_scope = rhs.GetSymbolContextScope(); |
| 64 | |
Eugene Zelenko | d70a6e7 | 2016-02-18 18:52:47 +0000 | [diff] [blame] | 65 | if (lhs_scope == nullptr && rhs_scope == nullptr) |
Greg Clayton | 6dadd50 | 2010-09-02 21:44:10 +0000 | [diff] [blame] | 66 | return lhs.GetPC() != rhs.GetPC(); |
| 67 | |
| 68 | return lhs_scope != rhs_scope; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | bool |
| 72 | lldb_private::operator< (const StackID& lhs, const StackID& rhs) |
| 73 | { |
Greg Clayton | 6dadd50 | 2010-09-02 21:44:10 +0000 | [diff] [blame] | 74 | const lldb::addr_t lhs_cfa = lhs.GetCallFrameAddress(); |
| 75 | const lldb::addr_t rhs_cfa = rhs.GetCallFrameAddress(); |
| 76 | |
Jim Ingham | b5c0d1c | 2012-03-01 00:50:50 +0000 | [diff] [blame] | 77 | // FIXME: We are assuming that the stacks grow downward in memory. That's not necessary, but true on |
| 78 | // all the machines we care about at present. If this changes, we'll have to deal with that. The ABI is the |
| 79 | // agent who knows this ordering, but the StackID has no access to the ABI. The most straightforward way |
| 80 | // to handle this is to add a "m_grows_downward" bool to the StackID, and set it in the constructor. |
| 81 | // But I'm not going to waste a bool per StackID on this till we need it. |
| 82 | |
Greg Clayton | 6dadd50 | 2010-09-02 21:44:10 +0000 | [diff] [blame] | 83 | if (lhs_cfa != rhs_cfa) |
| 84 | return lhs_cfa < rhs_cfa; |
| 85 | |
| 86 | SymbolContextScope *lhs_scope = lhs.GetSymbolContextScope(); |
| 87 | SymbolContextScope *rhs_scope = rhs.GetSymbolContextScope(); |
| 88 | |
Eugene Zelenko | d70a6e7 | 2016-02-18 18:52:47 +0000 | [diff] [blame] | 89 | if (lhs_scope != nullptr && rhs_scope != nullptr) |
Greg Clayton | 6dadd50 | 2010-09-02 21:44:10 +0000 | [diff] [blame] | 90 | { |
| 91 | // Same exact scope, lhs is not less than (younger than rhs) |
| 92 | if (lhs_scope == rhs_scope) |
| 93 | return false; |
| 94 | |
| 95 | SymbolContext lhs_sc; |
| 96 | SymbolContext rhs_sc; |
| 97 | lhs_scope->CalculateSymbolContext (&lhs_sc); |
| 98 | rhs_scope->CalculateSymbolContext (&rhs_sc); |
| 99 | |
| 100 | // Items with the same function can only be compared |
| 101 | if (lhs_sc.function == rhs_sc.function && |
Eugene Zelenko | d70a6e7 | 2016-02-18 18:52:47 +0000 | [diff] [blame] | 102 | lhs_sc.function != nullptr && lhs_sc.block != nullptr && |
| 103 | rhs_sc.function != nullptr && rhs_sc.block != nullptr) |
Greg Clayton | 6dadd50 | 2010-09-02 21:44:10 +0000 | [diff] [blame] | 104 | { |
| 105 | return rhs_sc.block->Contains (lhs_sc.block); |
| 106 | } |
| 107 | } |
| 108 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 109 | } |