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 }
lldb_private::Breakpoint::HasResolvedLocations
bool HasResolvedLocations() const
Return whether this breakpoint has any resolved locations.
Definition: Breakpoint.cpp:835
lldb_private::ThreadPlanRunToAddress::SetStopOthers
void SetStopOthers(bool new_value) override
Definition: ThreadPlanRunToAddress.cpp:163
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:41
lldb_private::Stoppoint::GetID
lldb::break_id_t GetID() const
Definition: Stoppoint.cpp:22
lldb_private::Event
Definition: Event.h:180
lldb_private::ThreadPlanRunToAddress::ThreadPlanRunToAddress
ThreadPlanRunToAddress(Thread &thread, Address &address, bool stop_others)
Definition: ThreadPlanRunToAddress.cpp:22
lldb_private::Breakpoint::SetBreakpointKind
void SetBreakpointKind(const char *kind)
Set the "kind" description for a breakpoint.
Definition: Breakpoint.h:442
lldb_private::ThreadPlanRunToAddress::AtOurAddress
bool AtOurAddress()
Definition: ThreadPlanRunToAddress.cpp:191
LLDB_LOGF
#define LLDB_LOGF(log,...)
Definition: Log.h:249
lldb_private::ThreadPlanRunToAddress::ValidatePlan
bool ValidatePlan(Stream *error) override
Returns whether this plan could be successfully created.
Definition: ThreadPlanRunToAddress.cpp:130
lldb_private::ThreadPlanRunToAddress::StopOthers
bool StopOthers() override
Definition: ThreadPlanRunToAddress.cpp:161
lldb_private::Thread::CalculateTarget
lldb::TargetSP CalculateTarget() override
Definition: Thread.cpp:1390
lldb_private::ThreadPlanRunToAddress::GetPlanRunState
lldb::StateType GetPlanRunState() override
Definition: ThreadPlanRunToAddress.cpp:167
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:331
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:157
lldb_private::Target
Definition: Target.h:454
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:367
lldb_private::ThreadPlanRunToAddress::DoPlanExplainsStop
bool DoPlanExplainsStop(Event *event_ptr) override
Definition: ThreadPlanRunToAddress.cpp:153
Process.h
Target.h
lldb_private::Thread::GetProcess
lldb::ProcessSP GetProcess() const
Definition: Thread.h:154
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::ThreadPlan::GetTarget
Target & GetTarget()
Definition: ThreadPlan.cpp:37
lldb_private::ThreadPlan
Definition: ThreadPlan.h:282
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:71
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:62
lldb_private::GetLogIfAllCategoriesSet
Log * GetLogIfAllCategoriesSet(uint32_t mask)
Definition: Logging.cpp:58
lldb_private::ThreadPlanRunToAddress::~ThreadPlanRunToAddress
~ThreadPlanRunToAddress() override
Definition: ThreadPlanRunToAddress.cpp:77
lldb_private::ThreadPlanRunToAddress::SetInitialBreakpoints
void SetInitialBreakpoints()
Definition: ThreadPlanRunToAddress.cpp:59
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:2527
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:340
LLDB_INVALID_BREAK_ID
#define LLDB_INVALID_BREAK_ID
Definition: lldb-defines.h:49
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:85
lldb_private::ThreadPlanRunToAddress::WillStop
bool WillStop() override
Definition: ThreadPlanRunToAddress.cpp:169
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:528
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:827
lldb_private::Target::RemoveBreakpointByID
bool RemoveBreakpointByID(lldb::break_id_t break_id)
Definition: Target.cpp:934
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:171
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:510
LIBLLDB_LOG_STEP
#define LIBLLDB_LOG_STEP
Definition: Logging.h:21
lldb_private::ThreadPlan::m_tid
lldb::tid_t m_tid
Definition: ThreadPlan.h:524
lldb::StateType
StateType
Process and Thread States.
Definition: lldb-enumerations.h:73
Stream.h
lldb_private::Log
Definition: Log.h:49
lldb_private::Target::GetBreakpointByID
lldb::BreakpointSP GetBreakpointByID(lldb::break_id_t break_id)
Definition: Target.cpp:303
lldb_private::Thread::GetRegisterContext
virtual lldb::RegisterContextSP GetRegisterContext()=0
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
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:79