110 lines · cpp
1//===-- StackID.cpp -------------------------------------------------------===//2//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-exception6//7//===----------------------------------------------------------------------===//8 9#include "lldb/Target/StackID.h"10#include "lldb/Symbol/Block.h"11#include "lldb/Symbol/Symbol.h"12#include "lldb/Symbol/SymbolContext.h"13#include "lldb/Target/Process.h"14#include "lldb/Utility/Stream.h"15 16using namespace lldb_private;17 18StackID::StackID(lldb::addr_t pc, lldb::addr_t cfa,19 SymbolContextScope *symbol_scope, Process *process)20 : m_pc(pc), m_cfa(cfa), m_cfa_with_metadata(cfa),21 m_symbol_scope(symbol_scope) {22 if (process) {23 m_pc = process->FixCodeAddress(m_pc);24 m_cfa = process->FixDataAddress(m_cfa);25 }26}27 28void StackID::SetPC(lldb::addr_t pc, Process *process) {29 m_pc = process ? process->FixCodeAddress(pc) : pc;30}31 32void StackID::SetCFA(lldb::addr_t cfa, Process *process) {33 m_cfa_with_metadata = cfa;34 m_cfa = process ? process->FixDataAddress(cfa) : cfa;35}36 37void StackID::Dump(Stream *s) {38 s->Printf("StackID (pc = 0x%16.16" PRIx64 ", cfa = 0x%16.16" PRIx6439 ", symbol_scope = %p",40 m_pc, m_cfa, static_cast<void *>(m_symbol_scope));41 if (m_symbol_scope) {42 SymbolContext sc;43 44 m_symbol_scope->CalculateSymbolContext(&sc);45 if (sc.block)46 s->Printf(" (Block {0x%8.8" PRIx64 "})", sc.block->GetID());47 else if (sc.symbol)48 s->Printf(" (Symbol{0x%8.8x})", sc.symbol->GetID());49 }50 s->PutCString(") ");51}52 53bool lldb_private::operator==(const StackID &lhs, const StackID &rhs) {54 if (lhs.GetCallFrameAddressWithoutMetadata() !=55 rhs.GetCallFrameAddressWithoutMetadata())56 return false;57 58 SymbolContextScope *lhs_scope = lhs.GetSymbolContextScope();59 SymbolContextScope *rhs_scope = rhs.GetSymbolContextScope();60 61 // Only compare the PC values if both symbol context scopes are nullptr62 if (lhs_scope == nullptr && rhs_scope == nullptr)63 return lhs.GetPC() == rhs.GetPC();64 65 return lhs_scope == rhs_scope;66}67 68bool lldb_private::operator!=(const StackID &lhs, const StackID &rhs) {69 return !(lhs == rhs);70}71 72bool lldb_private::operator<(const StackID &lhs, const StackID &rhs) {73 const lldb::addr_t lhs_cfa = lhs.GetCallFrameAddressWithoutMetadata();74 const lldb::addr_t rhs_cfa = rhs.GetCallFrameAddressWithoutMetadata();75 76 // FIXME: We are assuming that the stacks grow downward in memory. That's not77 // necessary, but true on78 // all the machines we care about at present. If this changes, we'll have to79 // deal with that. The ABI is the agent who knows this ordering, but the80 // StackID has no access to the ABI. The most straightforward way to handle81 // this is to add a "m_grows_downward" bool to the StackID, and set it in the82 // constructor. But I'm not going to waste a bool per StackID on this till we83 // need it.84 85 if (lhs_cfa != rhs_cfa)86 return lhs_cfa < rhs_cfa;87 88 SymbolContextScope *lhs_scope = lhs.GetSymbolContextScope();89 SymbolContextScope *rhs_scope = rhs.GetSymbolContextScope();90 91 if (lhs_scope != nullptr && rhs_scope != nullptr) {92 // Same exact scope, lhs is not less than (younger than rhs)93 if (lhs_scope == rhs_scope)94 return false;95 96 SymbolContext lhs_sc;97 SymbolContext rhs_sc;98 lhs_scope->CalculateSymbolContext(&lhs_sc);99 rhs_scope->CalculateSymbolContext(&rhs_sc);100 101 // Items with the same function can only be compared102 if (lhs_sc.function == rhs_sc.function && lhs_sc.function != nullptr &&103 lhs_sc.block != nullptr && rhs_sc.function != nullptr &&104 rhs_sc.block != nullptr) {105 return rhs_sc.block->Contains(lhs_sc.block);106 }107 }108 return false;109}110