xref: /src/contrib/llvm-project/lldb/source/Target/StackID.cpp (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
1cfca06d7SDimitry Andric //===-- StackID.cpp -------------------------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste 
9f3fbd1c0SDimitry Andric #include "lldb/Target/StackID.h"
10f034231aSEd Maste #include "lldb/Symbol/Block.h"
11f034231aSEd Maste #include "lldb/Symbol/Symbol.h"
12f034231aSEd Maste #include "lldb/Symbol/SymbolContext.h"
1374a628f7SDimitry Andric #include "lldb/Utility/Stream.h"
14f034231aSEd Maste 
15f034231aSEd Maste using namespace lldb_private;
16f034231aSEd Maste 
Dump(Stream * s)1714f1b3e8SDimitry Andric void StackID::Dump(Stream *s) {
1814f1b3e8SDimitry Andric   s->Printf("StackID (pc = 0x%16.16" PRIx64 ", cfa = 0x%16.16" PRIx64
1914f1b3e8SDimitry Andric             ", symbol_scope = %p",
200cac4ca3SEd Maste             m_pc, m_cfa, static_cast<void *>(m_symbol_scope));
2114f1b3e8SDimitry Andric   if (m_symbol_scope) {
22f034231aSEd Maste     SymbolContext sc;
23f034231aSEd Maste 
24f034231aSEd Maste     m_symbol_scope->CalculateSymbolContext(&sc);
25f034231aSEd Maste     if (sc.block)
26f034231aSEd Maste       s->Printf(" (Block {0x%8.8" PRIx64 "})", sc.block->GetID());
27f034231aSEd Maste     else if (sc.symbol)
28f034231aSEd Maste       s->Printf(" (Symbol{0x%8.8x})", sc.symbol->GetID());
29f034231aSEd Maste   }
30f034231aSEd Maste   s->PutCString(") ");
31f034231aSEd Maste }
32f034231aSEd Maste 
operator ==(const StackID & lhs,const StackID & rhs)3314f1b3e8SDimitry Andric bool lldb_private::operator==(const StackID &lhs, const StackID &rhs) {
34f034231aSEd Maste   if (lhs.GetCallFrameAddress() != rhs.GetCallFrameAddress())
35f034231aSEd Maste     return false;
36f034231aSEd Maste 
37f034231aSEd Maste   SymbolContextScope *lhs_scope = lhs.GetSymbolContextScope();
38f034231aSEd Maste   SymbolContextScope *rhs_scope = rhs.GetSymbolContextScope();
39f034231aSEd Maste 
40f3fbd1c0SDimitry Andric   // Only compare the PC values if both symbol context scopes are nullptr
41f3fbd1c0SDimitry Andric   if (lhs_scope == nullptr && rhs_scope == nullptr)
42f034231aSEd Maste     return lhs.GetPC() == rhs.GetPC();
43f034231aSEd Maste 
44f034231aSEd Maste   return lhs_scope == rhs_scope;
45f034231aSEd Maste }
46f034231aSEd Maste 
operator !=(const StackID & lhs,const StackID & rhs)4714f1b3e8SDimitry Andric bool lldb_private::operator!=(const StackID &lhs, const StackID &rhs) {
48f034231aSEd Maste   if (lhs.GetCallFrameAddress() != rhs.GetCallFrameAddress())
49f034231aSEd Maste     return true;
50f034231aSEd Maste 
51f034231aSEd Maste   SymbolContextScope *lhs_scope = lhs.GetSymbolContextScope();
52f034231aSEd Maste   SymbolContextScope *rhs_scope = rhs.GetSymbolContextScope();
53f034231aSEd Maste 
54f3fbd1c0SDimitry Andric   if (lhs_scope == nullptr && rhs_scope == nullptr)
55f034231aSEd Maste     return lhs.GetPC() != rhs.GetPC();
56f034231aSEd Maste 
57f034231aSEd Maste   return lhs_scope != rhs_scope;
58f034231aSEd Maste }
59f034231aSEd Maste 
operator <(const StackID & lhs,const StackID & rhs)6014f1b3e8SDimitry Andric bool lldb_private::operator<(const StackID &lhs, const StackID &rhs) {
61f034231aSEd Maste   const lldb::addr_t lhs_cfa = lhs.GetCallFrameAddress();
62f034231aSEd Maste   const lldb::addr_t rhs_cfa = rhs.GetCallFrameAddress();
63f034231aSEd Maste 
6414f1b3e8SDimitry Andric   // FIXME: We are assuming that the stacks grow downward in memory.  That's not
6514f1b3e8SDimitry Andric   // necessary, but true on
6614f1b3e8SDimitry Andric   // all the machines we care about at present.  If this changes, we'll have to
67f73363f1SDimitry Andric   // deal with that.  The ABI is the agent who knows this ordering, but the
68f73363f1SDimitry Andric   // StackID has no access to the ABI. The most straightforward way to handle
69f73363f1SDimitry Andric   // this is to add a "m_grows_downward" bool to the StackID, and set it in the
70f73363f1SDimitry Andric   // constructor. But I'm not going to waste a bool per StackID on this till we
71f73363f1SDimitry Andric   // need it.
72f034231aSEd Maste 
73f034231aSEd Maste   if (lhs_cfa != rhs_cfa)
74f034231aSEd Maste     return lhs_cfa < rhs_cfa;
75f034231aSEd Maste 
76f034231aSEd Maste   SymbolContextScope *lhs_scope = lhs.GetSymbolContextScope();
77f034231aSEd Maste   SymbolContextScope *rhs_scope = rhs.GetSymbolContextScope();
78f034231aSEd Maste 
7914f1b3e8SDimitry Andric   if (lhs_scope != nullptr && rhs_scope != nullptr) {
80f034231aSEd Maste     // Same exact scope, lhs is not less than (younger than rhs)
81f034231aSEd Maste     if (lhs_scope == rhs_scope)
82f034231aSEd Maste       return false;
83f034231aSEd Maste 
84f034231aSEd Maste     SymbolContext lhs_sc;
85f034231aSEd Maste     SymbolContext rhs_sc;
86f034231aSEd Maste     lhs_scope->CalculateSymbolContext(&lhs_sc);
87f034231aSEd Maste     rhs_scope->CalculateSymbolContext(&rhs_sc);
88f034231aSEd Maste 
89f034231aSEd Maste     // Items with the same function can only be compared
9014f1b3e8SDimitry Andric     if (lhs_sc.function == rhs_sc.function && lhs_sc.function != nullptr &&
9114f1b3e8SDimitry Andric         lhs_sc.block != nullptr && rhs_sc.function != nullptr &&
9214f1b3e8SDimitry Andric         rhs_sc.block != nullptr) {
93f034231aSEd Maste       return rhs_sc.block->Contains(lhs_sc.block);
94f034231aSEd Maste     }
95f034231aSEd Maste   }
96f034231aSEd Maste   return false;
97f034231aSEd Maste }
98