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 // C Includes
11 // C++ Includes
12 // Other libraries and framework includes
13 // Project includes
14 #include "lldb/Target/StackID.h"
15 #include "lldb/Core/Stream.h"
16 #include "lldb/Symbol/Block.h"
17 #include "lldb/Symbol/Symbol.h"
18 #include "lldb/Symbol/SymbolContext.h"
19 
20 using namespace lldb_private;
21 
22 void
23 StackID::Dump (Stream *s)
24 {
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));
27     if (m_symbol_scope)
28     {
29         SymbolContext sc;
30 
31         m_symbol_scope->CalculateSymbolContext (&sc);
32         if (sc.block)
33             s->Printf(" (Block {0x%8.8" PRIx64 "})", sc.block->GetID());
34         else if (sc.symbol)
35             s->Printf(" (Symbol{0x%8.8x})", sc.symbol->GetID());
36     }
37     s->PutCString(") ");
38 }
39 
40 bool
41 lldb_private::operator== (const StackID& lhs, const StackID& rhs)
42 {
43     if (lhs.GetCallFrameAddress() != rhs.GetCallFrameAddress())
44         return false;
45 
46     SymbolContextScope *lhs_scope = lhs.GetSymbolContextScope();
47     SymbolContextScope *rhs_scope = rhs.GetSymbolContextScope();
48 
49     // Only compare the PC values if both symbol context scopes are nullptr
50     if (lhs_scope == nullptr && rhs_scope == nullptr)
51         return lhs.GetPC() == rhs.GetPC();
52 
53     return lhs_scope == rhs_scope;
54 }
55 
56 bool
57 lldb_private::operator!= (const StackID& lhs, const StackID& rhs)
58 {
59     if (lhs.GetCallFrameAddress() != rhs.GetCallFrameAddress())
60         return true;
61 
62     SymbolContextScope *lhs_scope = lhs.GetSymbolContextScope();
63     SymbolContextScope *rhs_scope = rhs.GetSymbolContextScope();
64 
65     if (lhs_scope == nullptr && rhs_scope == nullptr)
66         return lhs.GetPC() != rhs.GetPC();
67 
68     return lhs_scope != rhs_scope;
69 }
70 
71 bool
72 lldb_private::operator< (const StackID& lhs, const StackID& rhs)
73 {
74     const lldb::addr_t lhs_cfa = lhs.GetCallFrameAddress();
75     const lldb::addr_t rhs_cfa = rhs.GetCallFrameAddress();
76 
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 
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 
89     if (lhs_scope != nullptr && rhs_scope != nullptr)
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 &&
102             lhs_sc.function != nullptr && lhs_sc.block != nullptr &&
103             rhs_sc.function != nullptr && rhs_sc.block != nullptr)
104         {
105             return rhs_sc.block->Contains (lhs_sc.block);
106         }
107     }
108     return false;
109 }
110