LLDB  mainline
ThreadPlanRunToAddress.cpp
Go to the documentation of this file.
1 //===-- ThreadPlanRunToAddress.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-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 #include "lldb/Target/Process.h"
12 #include "lldb/Target/Target.h"
13 #include "lldb/Target/Thread.h"
14 #include "lldb/Utility/LLDBLog.h"
15 #include "lldb/Utility/Log.h"
16 #include "lldb/Utility/Stream.h"
17 
18 using namespace lldb;
19 using namespace lldb_private;
20 
21 // ThreadPlanRunToAddress: Continue plan
22 
23 ThreadPlanRunToAddress::ThreadPlanRunToAddress(Thread &thread, Address &address,
24  bool stop_others)
25  : ThreadPlan(ThreadPlan::eKindRunToAddress, "Run to address plan", thread,
27  m_stop_others(stop_others), m_addresses(), m_break_ids() {
28  m_addresses.push_back(
29  address.GetOpcodeLoadAddress(thread.CalculateTarget().get()));
31 }
32 
34  lldb::addr_t address,
35  bool stop_others)
36  : ThreadPlan(ThreadPlan::eKindRunToAddress, "Run to address plan", thread,
38  m_stop_others(stop_others), m_addresses(), m_break_ids() {
39  m_addresses.push_back(
40  thread.CalculateTarget()->GetOpcodeLoadAddress(address));
42 }
43 
45  Thread &thread, const std::vector<lldb::addr_t> &addresses,
46  bool stop_others)
47  : ThreadPlan(ThreadPlan::eKindRunToAddress, "Run to address plan", thread,
49  m_stop_others(stop_others), m_addresses(addresses), m_break_ids() {
50  // Convert all addresses into opcode addresses to make sure we set
51  // breakpoints at the correct address.
52  Target &target = thread.GetProcess()->GetTarget();
53  std::vector<lldb::addr_t>::iterator pos, end = m_addresses.end();
54  for (pos = m_addresses.begin(); pos != end; ++pos)
55  *pos = target.GetOpcodeLoadAddress(*pos);
56 
58 }
59 
61  size_t num_addresses = m_addresses.size();
62  m_break_ids.resize(num_addresses);
63 
64  for (size_t i = 0; i < num_addresses; i++) {
65  Breakpoint *breakpoint;
66  breakpoint =
67  GetTarget().CreateBreakpoint(m_addresses[i], true, false).get();
68  if (breakpoint != nullptr) {
69  if (breakpoint->IsHardware() && !breakpoint->HasResolvedLocations())
71  m_break_ids[i] = breakpoint->GetID();
72  breakpoint->SetThreadID(m_tid);
73  breakpoint->SetBreakpointKind("run-to-address");
74  }
75  }
76 }
77 
79  size_t num_break_ids = m_break_ids.size();
80  for (size_t i = 0; i < num_break_ids; i++) {
82  }
84 }
85 
87  lldb::DescriptionLevel level) {
88  size_t num_addresses = m_addresses.size();
89 
90  if (level == lldb::eDescriptionLevelBrief) {
91  if (num_addresses == 0) {
92  s->Printf("run to address with no addresses given.");
93  return;
94  } else if (num_addresses == 1)
95  s->Printf("run to address: ");
96  else
97  s->Printf("run to addresses: ");
98 
99  for (size_t i = 0; i < num_addresses; i++) {
100  DumpAddress(s->AsRawOstream(), m_addresses[i], sizeof(addr_t));
101  s->Printf(" ");
102  }
103  } else {
104  if (num_addresses == 0) {
105  s->Printf("run to address with no addresses given.");
106  return;
107  } else if (num_addresses == 1)
108  s->Printf("Run to address: ");
109  else {
110  s->Printf("Run to addresses: ");
111  }
112 
113  for (size_t i = 0; i < num_addresses; i++) {
114  if (num_addresses > 1) {
115  s->Printf("\n");
116  s->Indent();
117  }
118 
119  DumpAddress(s->AsRawOstream(), m_addresses[i], sizeof(addr_t));
120  s->Printf(" using breakpoint: %d - ", m_break_ids[i]);
121  Breakpoint *breakpoint =
123  if (breakpoint)
124  breakpoint->Dump(s);
125  else
126  s->Printf("but the breakpoint has been deleted.");
127  }
128  }
129 }
130 
133  if (error)
134  error->Printf("Could not set hardware breakpoint(s)");
135  return false;
136  }
137 
138  // If we couldn't set the breakpoint for some reason, then this won't work.
139  bool all_bps_good = true;
140  size_t num_break_ids = m_break_ids.size();
141  for (size_t i = 0; i < num_break_ids; i++) {
143  all_bps_good = false;
144  if (error) {
145  error->Printf("Could not set breakpoint for address: ");
146  DumpAddress(error->AsRawOstream(), m_addresses[i], sizeof(addr_t));
147  error->Printf("\n");
148  }
149  }
150  }
151  return all_bps_good;
152 }
153 
155  return AtOurAddress();
156 }
157 
159  return AtOurAddress();
160 }
161 
163 
165  m_stop_others = new_value;
166 }
167 
169 
170 bool ThreadPlanRunToAddress::WillStop() { return true; }
171 
173  Log *log = GetLog(LLDBLog::Step);
174 
175  if (AtOurAddress()) {
176  // Remove the breakpoint
177  size_t num_break_ids = m_break_ids.size();
178 
179  for (size_t i = 0; i < num_break_ids; i++) {
183  }
184  }
185  LLDB_LOGF(log, "Completed run to address plan.");
187  return true;
188  } else
189  return false;
190 }
191 
193  lldb::addr_t current_address = GetThread().GetRegisterContext()->GetPC();
194  bool found_it = false;
195  size_t num_addresses = m_addresses.size();
196  for (size_t i = 0; i < num_addresses; i++) {
197  if (m_addresses[i] == current_address) {
198  found_it = true;
199  break;
200  }
201  }
202  return found_it;
203 }
lldb_private::Breakpoint::HasResolvedLocations
bool HasResolvedLocations() const
Return whether this breakpoint has any resolved locations.
Definition: Breakpoint.cpp:838
lldb_private::ThreadPlanRunToAddress::SetStopOthers
void SetStopOthers(bool new_value) override
Definition: ThreadPlanRunToAddress.cpp:164
lldb_private::ThreadPlanRunToAddress::m_stop_others
bool m_stop_others
Definition: ThreadPlanRunToAddress.h:55
lldb_private::ThreadPlan::GetThread
Thread & GetThread()
Returns the Thread that is using this thread plan.
Definition: ThreadPlan.cpp:42
lldb_private::Stoppoint::GetID
lldb::break_id_t GetID() const
Definition: Stoppoint.cpp:22
lldb_private::Event
Definition: Event.h:182
lldb_private::ThreadPlanRunToAddress::ThreadPlanRunToAddress
ThreadPlanRunToAddress(Thread &thread, Address &address, bool stop_others)
Definition: ThreadPlanRunToAddress.cpp:23
lldb_private::Breakpoint::SetBreakpointKind
void SetBreakpointKind(const char *kind)
Set the "kind" description for a breakpoint.
Definition: Breakpoint.h:447
lldb_private::ThreadPlanRunToAddress::AtOurAddress
bool AtOurAddress()
Definition: ThreadPlanRunToAddress.cpp:192
LLDB_LOGF
#define LLDB_LOGF(log,...)
Definition: Log.h:343
lldb_private::ThreadPlanRunToAddress::ValidatePlan
bool ValidatePlan(Stream *error) override
Returns whether this plan could be successfully created.
Definition: ThreadPlanRunToAddress.cpp:131
lldb_private::ThreadPlanRunToAddress::StopOthers
bool StopOthers() override
Definition: ThreadPlanRunToAddress.cpp:162
lldb_private::Thread::CalculateTarget
lldb::TargetSP CalculateTarget() override
Definition: Thread.cpp:1383
lldb_private::ThreadPlanRunToAddress::GetPlanRunState
lldb::StateType GetPlanRunState() override
Definition: ThreadPlanRunToAddress.cpp:168
lldb_private::eVoteNoOpinion
@ eVoteNoOpinion
Definition: lldb-private-enumerations.h:59
lldb_private::Stream
Definition: Stream.h:28
lldb_private::Target::CreateBreakpoint
lldb::BreakpointSP CreateBreakpoint(const FileSpecList *containingModules, const FileSpec &file, uint32_t line_no, uint32_t column, lldb::addr_t offset, LazyBool check_inlines, LazyBool skip_prologue, bool internal, bool request_hardware, LazyBool move_to_nearest_code)
Definition: Target.cpp:350
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
lldb_private::ThreadPlanRunToAddress::ShouldStop
bool ShouldStop(Event *event_ptr) override
Definition: ThreadPlanRunToAddress.cpp:158
lldb_private::Target
Definition: Target.h:473
lldb_private::Address::GetOpcodeLoadAddress
lldb::addr_t GetOpcodeLoadAddress(Target *target, AddressClass addr_class=AddressClass::eInvalid) const
Get the load address as an opcode load address.
Definition: Address.cpp:368
lldb_private::ThreadPlanRunToAddress::DoPlanExplainsStop
bool DoPlanExplainsStop(Event *event_ptr) override
Definition: ThreadPlanRunToAddress.cpp:154
Process.h
Target.h
lldb_private::Thread::GetProcess
lldb::ProcessSP GetProcess() const
Definition: Thread.h:153
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::ThreadPlan::GetTarget
Target & GetTarget()
Definition: ThreadPlan.cpp:38
lldb_private::ThreadPlan
Definition: ThreadPlan.h:287
lldb_private::Stream::Indent
size_t Indent(llvm::StringRef s="")
Indent the current line in the stream.
Definition: Stream.cpp:130
Log.h
lldb_private::ThreadPlan::MischiefManaged
virtual bool MischiefManaged()
Definition: ThreadPlan.cpp:72
lldb_private::DumpAddress
void DumpAddress(llvm::raw_ostream &s, uint64_t addr, uint32_t addr_size, const char *prefix=nullptr, const char *suffix=nullptr)
Output an address value to this stream.
Definition: Stream.cpp:81
lldb_private::Thread
Definition: Thread.h:61
lldb_private::ThreadPlanRunToAddress::~ThreadPlanRunToAddress
~ThreadPlanRunToAddress() override
Definition: ThreadPlanRunToAddress.cpp:78
lldb_private::ThreadPlanRunToAddress::SetInitialBreakpoints
void SetInitialBreakpoints()
Definition: ThreadPlanRunToAddress.cpp:60
ThreadPlanRunToAddress.h
Thread.h
lldb_private::Target::GetOpcodeLoadAddress
lldb::addr_t GetOpcodeLoadAddress(lldb::addr_t load_addr, AddressClass addr_class=AddressClass::eInvalid) const
Get load_addr as an opcode for this target.
Definition: Target.cpp:2606
lldb_private::LLDBLog::Step
@ Step
lldb_private::ThreadPlanRunToAddress::m_break_ids
std::vector< lldb::break_id_t > m_break_ids
Definition: ThreadPlanRunToAddress.h:59
lldb_private::Breakpoint::SetThreadID
void SetThreadID(lldb::tid_t thread_id)
Set the valid thread to be checked when the breakpoint is hit.
Definition: Breakpoint.cpp:341
LLDB_INVALID_BREAK_ID
#define LLDB_INVALID_BREAK_ID
Definition: lldb-defines.h:37
lldb_private::ThreadPlanRunToAddress::m_addresses
std::vector< lldb::addr_t > m_addresses
Definition: ThreadPlanRunToAddress.h:57
lldb_private::ThreadPlanRunToAddress::GetDescription
void GetDescription(Stream *s, lldb::DescriptionLevel level) override
Print a description of this thread to the stream s.
Definition: ThreadPlanRunToAddress.cpp:86
lldb_private::ThreadPlanRunToAddress::WillStop
bool WillStop() override
Definition: ThreadPlanRunToAddress.cpp:170
lldb_private::Address
Definition: Address.h:59
lldb_private::ThreadPlan::m_could_not_resolve_hw_bp
bool m_could_not_resolve_hw_bp
Definition: ThreadPlan.h:533
lldb_private::Stream::AsRawOstream
llvm::raw_ostream & AsRawOstream()
Returns a raw_ostream that forwards the data to this Stream object.
Definition: Stream.h:357
lldb_private::Breakpoint::Dump
void Dump(Stream *s) override
Standard "Dump" method. At present it does nothing.
Definition: Breakpoint.cpp:830
lldb_private::Target::RemoveBreakpointByID
bool RemoveBreakpointByID(lldb::break_id_t break_id)
Definition: Target.cpp:948
lldb_private::Stream::Printf
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
lldb_private::ThreadPlanRunToAddress::MischiefManaged
bool MischiefManaged() override
Definition: ThreadPlanRunToAddress.cpp:172
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::Breakpoint::IsHardware
bool IsHardware() const
Definition: Breakpoint.h:515
lldb_private::ThreadPlan::m_tid
lldb::tid_t m_tid
Definition: ThreadPlan.h:529
lldb::StateType
StateType
Process and Thread States.
Definition: lldb-enumerations.h:73
Stream.h
lldb_private::Log
Definition: Log.h:115
lldb_private::Target::GetBreakpointByID
lldb::BreakpointSP GetBreakpointByID(lldb::break_id_t break_id)
Definition: Target.cpp:322
lldb_private::Thread::GetRegisterContext
virtual lldb::RegisterContextSP GetRegisterContext()=0
lldb_private::GetLog
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition: Log.h:308
lldb
Definition: SBAddress.h:15
lldb::eStateRunning
@ eStateRunning
Process or thread is running and can't be examined.
Definition: lldb-enumerations.h:85
RegisterContext.h
LLDBLog.h
lldb::eDescriptionLevelBrief
@ eDescriptionLevelBrief
Definition: lldb-enumerations.h:208
lldb::DescriptionLevel
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
Definition: lldb-enumerations.h:207
lldb_private::Breakpoint
General Outline: A breakpoint has four main parts, a filter, a resolver, the list of breakpoint locat...
Definition: Breakpoint.h:80