1*dda28197Spatrick //===-- StackID.cpp -------------------------------------------------------===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick
9061da546Spatrick #include "lldb/Target/StackID.h"
10061da546Spatrick #include "lldb/Symbol/Block.h"
11061da546Spatrick #include "lldb/Symbol/Symbol.h"
12061da546Spatrick #include "lldb/Symbol/SymbolContext.h"
13061da546Spatrick #include "lldb/Utility/Stream.h"
14061da546Spatrick
15061da546Spatrick using namespace lldb_private;
16061da546Spatrick
Dump(Stream * s)17061da546Spatrick void StackID::Dump(Stream *s) {
18061da546Spatrick s->Printf("StackID (pc = 0x%16.16" PRIx64 ", cfa = 0x%16.16" PRIx64
19061da546Spatrick ", symbol_scope = %p",
20061da546Spatrick m_pc, m_cfa, static_cast<void *>(m_symbol_scope));
21061da546Spatrick if (m_symbol_scope) {
22061da546Spatrick SymbolContext sc;
23061da546Spatrick
24061da546Spatrick m_symbol_scope->CalculateSymbolContext(&sc);
25061da546Spatrick if (sc.block)
26061da546Spatrick s->Printf(" (Block {0x%8.8" PRIx64 "})", sc.block->GetID());
27061da546Spatrick else if (sc.symbol)
28061da546Spatrick s->Printf(" (Symbol{0x%8.8x})", sc.symbol->GetID());
29061da546Spatrick }
30061da546Spatrick s->PutCString(") ");
31061da546Spatrick }
32061da546Spatrick
operator ==(const StackID & lhs,const StackID & rhs)33061da546Spatrick bool lldb_private::operator==(const StackID &lhs, const StackID &rhs) {
34061da546Spatrick if (lhs.GetCallFrameAddress() != rhs.GetCallFrameAddress())
35061da546Spatrick return false;
36061da546Spatrick
37061da546Spatrick SymbolContextScope *lhs_scope = lhs.GetSymbolContextScope();
38061da546Spatrick SymbolContextScope *rhs_scope = rhs.GetSymbolContextScope();
39061da546Spatrick
40061da546Spatrick // Only compare the PC values if both symbol context scopes are nullptr
41061da546Spatrick if (lhs_scope == nullptr && rhs_scope == nullptr)
42061da546Spatrick return lhs.GetPC() == rhs.GetPC();
43061da546Spatrick
44061da546Spatrick return lhs_scope == rhs_scope;
45061da546Spatrick }
46061da546Spatrick
operator !=(const StackID & lhs,const StackID & rhs)47061da546Spatrick bool lldb_private::operator!=(const StackID &lhs, const StackID &rhs) {
48061da546Spatrick if (lhs.GetCallFrameAddress() != rhs.GetCallFrameAddress())
49061da546Spatrick return true;
50061da546Spatrick
51061da546Spatrick SymbolContextScope *lhs_scope = lhs.GetSymbolContextScope();
52061da546Spatrick SymbolContextScope *rhs_scope = rhs.GetSymbolContextScope();
53061da546Spatrick
54061da546Spatrick if (lhs_scope == nullptr && rhs_scope == nullptr)
55061da546Spatrick return lhs.GetPC() != rhs.GetPC();
56061da546Spatrick
57061da546Spatrick return lhs_scope != rhs_scope;
58061da546Spatrick }
59061da546Spatrick
operator <(const StackID & lhs,const StackID & rhs)60061da546Spatrick bool lldb_private::operator<(const StackID &lhs, const StackID &rhs) {
61061da546Spatrick const lldb::addr_t lhs_cfa = lhs.GetCallFrameAddress();
62061da546Spatrick const lldb::addr_t rhs_cfa = rhs.GetCallFrameAddress();
63061da546Spatrick
64061da546Spatrick // FIXME: We are assuming that the stacks grow downward in memory. That's not
65061da546Spatrick // necessary, but true on
66061da546Spatrick // all the machines we care about at present. If this changes, we'll have to
67061da546Spatrick // deal with that. The ABI is the agent who knows this ordering, but the
68061da546Spatrick // StackID has no access to the ABI. The most straightforward way to handle
69061da546Spatrick // this is to add a "m_grows_downward" bool to the StackID, and set it in the
70061da546Spatrick // constructor. But I'm not going to waste a bool per StackID on this till we
71061da546Spatrick // need it.
72061da546Spatrick
73061da546Spatrick if (lhs_cfa != rhs_cfa)
74061da546Spatrick return lhs_cfa < rhs_cfa;
75061da546Spatrick
76061da546Spatrick SymbolContextScope *lhs_scope = lhs.GetSymbolContextScope();
77061da546Spatrick SymbolContextScope *rhs_scope = rhs.GetSymbolContextScope();
78061da546Spatrick
79061da546Spatrick if (lhs_scope != nullptr && rhs_scope != nullptr) {
80061da546Spatrick // Same exact scope, lhs is not less than (younger than rhs)
81061da546Spatrick if (lhs_scope == rhs_scope)
82061da546Spatrick return false;
83061da546Spatrick
84061da546Spatrick SymbolContext lhs_sc;
85061da546Spatrick SymbolContext rhs_sc;
86061da546Spatrick lhs_scope->CalculateSymbolContext(&lhs_sc);
87061da546Spatrick rhs_scope->CalculateSymbolContext(&rhs_sc);
88061da546Spatrick
89061da546Spatrick // Items with the same function can only be compared
90061da546Spatrick if (lhs_sc.function == rhs_sc.function && lhs_sc.function != nullptr &&
91061da546Spatrick lhs_sc.block != nullptr && rhs_sc.function != nullptr &&
92061da546Spatrick rhs_sc.block != nullptr) {
93061da546Spatrick return rhs_sc.block->Contains(lhs_sc.block);
94061da546Spatrick }
95061da546Spatrick }
96061da546Spatrick return false;
97061da546Spatrick }
98