blob: 222b2d3f2aa841d370a2e699fee99fd4f9be35bb [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- 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
Chris Lattner30fdc8d2010-06-08 16:52:24 +000010// C Includes
11// C++ Includes
12// Other libraries and framework includes
13// Project includes
Eugene Zelenkod70a6e72016-02-18 18:52:47 +000014#include "lldb/Target/StackID.h"
Greg Clayton59e8fc1c2010-08-30 18:11:35 +000015#include "lldb/Core/Stream.h"
16#include "lldb/Symbol/Block.h"
17#include "lldb/Symbol/Symbol.h"
18#include "lldb/Symbol/SymbolContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019
20using namespace lldb_private;
21
Greg Clayton59e8fc1c2010-08-30 18:11:35 +000022void
23StackID::Dump (Stream *s)
24{
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000025 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));
Greg Clayton59e8fc1c2010-08-30 18:11:35 +000027 if (m_symbol_scope)
28 {
29 SymbolContext sc;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000030
Greg Clayton59e8fc1c2010-08-30 18:11:35 +000031 m_symbol_scope->CalculateSymbolContext (&sc);
32 if (sc.block)
Daniel Malead01b2952012-11-29 21:49:15 +000033 s->Printf(" (Block {0x%8.8" PRIx64 "})", sc.block->GetID());
Greg Clayton59e8fc1c2010-08-30 18:11:35 +000034 else if (sc.symbol)
35 s->Printf(" (Symbol{0x%8.8x})", sc.symbol->GetID());
36 }
37 s->PutCString(") ");
38}
39
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040bool
41lldb_private::operator== (const StackID& lhs, const StackID& rhs)
42{
Greg Clayton6dadd502010-09-02 21:44:10 +000043 if (lhs.GetCallFrameAddress() != rhs.GetCallFrameAddress())
44 return false;
45
46 SymbolContextScope *lhs_scope = lhs.GetSymbolContextScope();
47 SymbolContextScope *rhs_scope = rhs.GetSymbolContextScope();
48
Eugene Zelenkod70a6e72016-02-18 18:52:47 +000049 // Only compare the PC values if both symbol context scopes are nullptr
50 if (lhs_scope == nullptr && rhs_scope == nullptr)
Greg Clayton6dadd502010-09-02 21:44:10 +000051 return lhs.GetPC() == rhs.GetPC();
52
53 return lhs_scope == rhs_scope;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000054}
55
56bool
57lldb_private::operator!= (const StackID& lhs, const StackID& rhs)
58{
Greg Clayton6dadd502010-09-02 21:44:10 +000059 if (lhs.GetCallFrameAddress() != rhs.GetCallFrameAddress())
60 return true;
61
62 SymbolContextScope *lhs_scope = lhs.GetSymbolContextScope();
63 SymbolContextScope *rhs_scope = rhs.GetSymbolContextScope();
64
Eugene Zelenkod70a6e72016-02-18 18:52:47 +000065 if (lhs_scope == nullptr && rhs_scope == nullptr)
Greg Clayton6dadd502010-09-02 21:44:10 +000066 return lhs.GetPC() != rhs.GetPC();
67
68 return lhs_scope != rhs_scope;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000069}
70
71bool
72lldb_private::operator< (const StackID& lhs, const StackID& rhs)
73{
Greg Clayton6dadd502010-09-02 21:44:10 +000074 const lldb::addr_t lhs_cfa = lhs.GetCallFrameAddress();
75 const lldb::addr_t rhs_cfa = rhs.GetCallFrameAddress();
76
Jim Inghamb5c0d1c2012-03-01 00:50:50 +000077 // 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
Greg Clayton6dadd502010-09-02 21:44:10 +000083 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
Eugene Zelenkod70a6e72016-02-18 18:52:47 +000089 if (lhs_scope != nullptr && rhs_scope != nullptr)
Greg Clayton6dadd502010-09-02 21:44:10 +000090 {
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 &&
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000102 lhs_sc.function != nullptr && lhs_sc.block != nullptr &&
103 rhs_sc.function != nullptr && rhs_sc.block != nullptr)
Greg Clayton6dadd502010-09-02 21:44:10 +0000104 {
105 return rhs_sc.block->Contains (lhs_sc.block);
106 }
107 }
108 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000109}