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