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