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/Log.h"
15 #include "lldb/Utility/Stream.h"
16 
17 using namespace lldb;
18 using namespace lldb_private;
19 
20 // ThreadPlanRunToAddress: Continue plan
21 
22 ThreadPlanRunToAddress::ThreadPlanRunToAddress(Thread &thread, Address &address,
23  bool stop_others)
24  : ThreadPlan(ThreadPlan::eKindRunToAddress, "Run to address plan", thread,
26  m_stop_others(stop_others), m_addresses(), m_break_ids() {
27  m_addresses.push_back(
28  address.GetOpcodeLoadAddress(thread.CalculateTarget().get()));
30 }
31 
33  lldb::addr_t address,
34  bool stop_others)
35  : ThreadPlan(ThreadPlan::eKindRunToAddress, "Run to address plan", thread,
37  m_stop_others(stop_others), m_addresses(), m_break_ids() {
38  m_addresses.push_back(
39  thread.CalculateTarget()->GetOpcodeLoadAddress(address));
41 }
42 
44  Thread &thread, const std::vector<lldb::addr_t> &addresses,
45  bool stop_others)
46  : ThreadPlan(ThreadPlan::eKindRunToAddress, "Run to address plan", thread,
48  m_stop_others(stop_others), m_addresses(addresses), m_break_ids() {
49  // Convert all addresses into opcode addresses to make sure we set
50  // breakpoints at the correct address.
51  Target &target = thread.GetProcess()->GetTarget();
52  std::vector<lldb::addr_t>::iterator pos, end = m_addresses.end();
53  for (pos = m_addresses.begin(); pos != end; ++pos)
54  *pos = target.GetOpcodeLoadAddress(*pos);
55 
57 }
58 
60  size_t num_addresses = m_addresses.size();
61  m_break_ids.resize(num_addresses);
62 
63  for (size_t i = 0; i < num_addresses; i++) {
64  Breakpoint *breakpoint;
65  breakpoint =
66  GetTarget().CreateBreakpoint(m_addresses[i], true, false).get();
67  if (breakpoint != nullptr) {
68  if (breakpoint->IsHardware() && !breakpoint->HasResolvedLocations())
70  m_break_ids[i] = breakpoint->GetID();
71  breakpoint->SetThreadID(m_tid);
72  breakpoint->SetBreakpointKind("run-to-address");
73  }
74  }
75 }
76 
78  size_t num_break_ids = m_break_ids.size();
79  for (size_t i = 0; i < num_break_ids; i++) {
81  }
83 }
84 
86  lldb::DescriptionLevel level) {
87  size_t num_addresses = m_addresses.size();
88 
89  if (level == lldb::eDescriptionLevelBrief) {
90  if (num_addresses == 0) {
91  s->Printf("run to address with no addresses given.");
92  return;
93  } else if (num_addresses == 1)
94  s->Printf("run to address: ");
95  else
96  s->Printf("run to addresses: ");
97 
98  for (size_t i = 0; i < num_addresses; i++) {
99  DumpAddress(s->AsRawOstream(), m_addresses[i], sizeof(addr_t));
100  s->Printf(" ");
101  }
102  } else {
103  if (num_addresses == 0) {
104  s->Printf("run to address with no addresses given.");
105  return;
106  } else if (num_addresses == 1)
107  s->Printf("Run to address: ");
108  else {
109  s->Printf("Run to addresses: ");
110  }
111 
112  for (size_t i = 0; i < num_addresses; i++) {
113  if (num_addresses > 1) {
114  s->Printf("\n");
115  s->Indent();
116  }
117 
118  DumpAddress(s->AsRawOstream(), m_addresses[i], sizeof(addr_t));
119  s->Printf(" using breakpoint: %d - ", m_break_ids[i]);
120  Breakpoint *breakpoint =
122  if (breakpoint)
123  breakpoint->Dump(s);
124  else
125  s->Printf("but the breakpoint has been deleted.");
126  }
127  }
128 }
129 
132  if (error)
133  error->Printf("Could not set hardware breakpoint(s)");
134  return false;
135  }
136 
137  // If we couldn't set the breakpoint for some reason, then this won't work.
138  bool all_bps_good = true;
139  size_t num_break_ids = m_break_ids.size();
140  for (size_t i = 0; i < num_break_ids; i++) {
142  all_bps_good = false;
143  if (error) {
144  error->Printf("Could not set breakpoint for address: ");
145  DumpAddress(error->AsRawOstream(), m_addresses[i], sizeof(addr_t));
146  error->Printf("\n");
147  }
148  }
149  }
150  return all_bps_good;
151 }
152 
154  return AtOurAddress();
155 }
156 
158  return AtOurAddress();
159 }
160 
162 
164  m_stop_others = new_value;
165 }
166 
168 
169 bool ThreadPlanRunToAddress::WillStop() { return true; }
170 
173 
174  if (AtOurAddress()) {
175  // Remove the breakpoint
176  size_t num_break_ids = m_break_ids.size();
177 
178  for (size_t i = 0; i < num_break_ids; i++) {
182  }
183  }
184  LLDB_LOGF(log, "Completed run to address plan.");
186  return true;
187  } else
188  return false;
189 }
190 
192  lldb::addr_t current_address = GetThread().GetRegisterContext()->GetPC();
193  bool found_it = false;
194  size_t num_addresses = m_addresses.size();
195  for (size_t i = 0; i < num_addresses; i++) {
196  if (m_addresses[i] == current_address) {
197  found_it = true;
198  break;
199  }
200  }
201  return found_it;
202 }
llvm::raw_ostream & AsRawOstream()
Returns a raw_ostream that forwards the data to this Stream object.
Definition: Stream.h:356
bool RemoveBreakpointByID(lldb::break_id_t break_id)
Definition: Target.cpp:916
A class that represents a running process on the host machine.
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
General Outline: A breakpoint has four main parts, a filter, a resolver, the list of breakpoint locat...
Definition: Breakpoint.h:78
void SetStopOthers(bool new_value) override
void SetBreakpointKind(const char *kind)
Set the "kind" description for a breakpoint.
Definition: Breakpoint.h:440
bool ValidatePlan(Stream *error) override
Returns whether this plan could be successfully created.
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
Thread & GetThread()
Returns the Thread that is using this thread plan.
Definition: ThreadPlan.cpp:41
std::vector< lldb::addr_t > m_addresses
lldb::BreakpointSP GetBreakpointByID(lldb::break_id_t break_id)
Definition: Target.cpp:295
size_t Indent(llvm::StringRef s="")
Indent the current line in the stream.
Definition: Stream.cpp:129
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:80
bool DoPlanExplainsStop(Event *event_ptr) override
Log * GetLogIfAllCategoriesSet(uint32_t mask)
Definition: Logging.cpp:58
lldb::TargetSP CalculateTarget() override
Definition: Thread.cpp:1390
StateType
Process and Thread States.
lldb::break_id_t GetID() const
Definition: Stoppoint.cpp:22
virtual lldb::RegisterContextSP GetRegisterContext()=0
lldb::addr_t GetOpcodeLoadAddress(Target *target, AddressClass addr_class=AddressClass::eInvalid) const
Get the load address as an opcode load address.
Definition: Address.cpp:367
virtual bool MischiefManaged()
Definition: ThreadPlan.cpp:71
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:2447
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:106
bool IsHardware() const
Definition: Breakpoint.h:508
A section + offset based address class.
Definition: Address.h:59
lldb::ProcessSP GetProcess() const
Definition: Thread.h:152
bool HasResolvedLocations() const
Return whether this breakpoint has any resolved locations.
Definition: Breakpoint.cpp:861
bool ShouldStop(Event *event_ptr) override
#define LLDB_LOGF(log,...)
Definition: Log.h:249
uint64_t addr_t
Definition: lldb-types.h:83
Definition: SBAddress.h:15
void Dump(Stream *s) override
Standard "Dump" method. At present it does nothing.
Definition: Breakpoint.cpp:853
void SetThreadID(lldb::tid_t thread_id)
Set the valid thread to be checked when the breakpoint is hit.
Definition: Breakpoint.cpp:361
#define LLDB_INVALID_BREAK_ID
Definition: lldb-defines.h:49
void GetDescription(Stream *s, lldb::DescriptionLevel level) override
Print a description of this thread to the stream s.
ThreadPlanRunToAddress(Thread &thread, Address &address, bool stop_others)
#define LIBLLDB_LOG_STEP
Definition: Logging.h:21
std::vector< lldb::break_id_t > m_break_ids
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:323
Process or thread is running and can&#39;t be examined.