blob: f8b17dc10eca5e451e2aaeda86e4b02df9b50fc8 [file] [log] [blame]
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001//===-- StopInfo.cpp --------------------------------------------*- C++ -*-===//
Greg Claytonf4b47e12010-08-04 01:40:35 +00002//
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
Greg Claytonf4b47e12010-08-04 01:40:35 +000010// C Includes
11// C++ Includes
12#include <string>
13
14// Other libraries and framework includes
15// Project includes
Greg Clayton4e78f602010-11-18 18:52:36 +000016#include "lldb/Breakpoint/Breakpoint.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000017#include "lldb/Breakpoint/BreakpointLocation.h"
18#include "lldb/Breakpoint/StoppointCallbackContext.h"
Johnny Chen01a67862011-10-14 00:42:25 +000019#include "lldb/Breakpoint/Watchpoint.h"
Jim Ingham4b536182011-08-09 02:12:22 +000020#include "lldb/Core/Debugger.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000021#include "lldb/Core/ValueObject.h"
Jim Ingham151c0322015-09-15 21:13:50 +000022#include "lldb/Expression/UserExpression.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000023#include "lldb/Target/Process.h"
24#include "lldb/Target/StopInfo.h"
Jim Ingham4b536182011-08-09 02:12:22 +000025#include "lldb/Target/Target.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000026#include "lldb/Target/Thread.h"
27#include "lldb/Target/ThreadPlan.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000028#include "lldb/Target/UnixSignals.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000029#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000030#include "lldb/Utility/StreamString.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000031
32using namespace lldb;
33using namespace lldb_private;
34
Kate Stoneb9c1b512016-09-06 20:57:50 +000035StopInfo::StopInfo(Thread &thread, uint64_t value)
36 : m_thread_wp(thread.shared_from_this()),
37 m_stop_id(thread.GetProcess()->GetStopID()),
38 m_resume_id(thread.GetProcess()->GetResumeID()), m_value(value),
39 m_description(), m_override_should_notify(eLazyBoolCalculate),
40 m_override_should_stop(eLazyBoolCalculate), m_extended_info() {}
41
42bool StopInfo::IsValid() const {
43 ThreadSP thread_sp(m_thread_wp.lock());
44 if (thread_sp)
45 return thread_sp->GetProcess()->GetStopID() == m_stop_id;
46 return false;
Greg Claytonf4b47e12010-08-04 01:40:35 +000047}
48
Kate Stoneb9c1b512016-09-06 20:57:50 +000049void StopInfo::MakeStopInfoValid() {
50 ThreadSP thread_sp(m_thread_wp.lock());
51 if (thread_sp) {
52 m_stop_id = thread_sp->GetProcess()->GetStopID();
53 m_resume_id = thread_sp->GetProcess()->GetResumeID();
54 }
Greg Claytonf4b47e12010-08-04 01:40:35 +000055}
56
Kate Stoneb9c1b512016-09-06 20:57:50 +000057bool StopInfo::HasTargetRunSinceMe() {
58 ThreadSP thread_sp(m_thread_wp.lock());
59
60 if (thread_sp) {
61 lldb::StateType ret_type = thread_sp->GetProcess()->GetPrivateState();
62 if (ret_type == eStateRunning) {
63 return true;
64 } else if (ret_type == eStateStopped) {
65 // This is a little tricky. We want to count "run and stopped again
Adrian Prantl05097242018-04-30 16:49:04 +000066 // before you could ask this question as a "TRUE" answer to
67 // HasTargetRunSinceMe. But we don't want to include any running of the
68 // target done for expressions. So we track both resumes, and resumes
69 // caused by expressions, and check if there are any resumes
Kate Stoneb9c1b512016-09-06 20:57:50 +000070 // NOT caused
71 // by expressions.
72
73 uint32_t curr_resume_id = thread_sp->GetProcess()->GetResumeID();
74 uint32_t last_user_expression_id =
75 thread_sp->GetProcess()->GetLastUserExpressionResumeID();
76 if (curr_resume_id == m_resume_id) {
77 return false;
78 } else if (curr_resume_id > last_user_expression_id) {
79 return true;
80 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +000081 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000082 }
83 return false;
Jim Ingham4b536182011-08-09 02:12:22 +000084}
85
Greg Claytonf4b47e12010-08-04 01:40:35 +000086//----------------------------------------------------------------------
87// StopInfoBreakpoint
88//----------------------------------------------------------------------
89
Kate Stoneb9c1b512016-09-06 20:57:50 +000090namespace lldb_private {
91class StopInfoBreakpoint : public StopInfo {
Greg Claytonf4b47e12010-08-04 01:40:35 +000092public:
Kate Stoneb9c1b512016-09-06 20:57:50 +000093 StopInfoBreakpoint(Thread &thread, break_id_t break_id)
94 : StopInfo(thread, break_id), m_should_stop(false),
95 m_should_stop_is_valid(false), m_should_perform_action(true),
96 m_address(LLDB_INVALID_ADDRESS), m_break_id(LLDB_INVALID_BREAK_ID),
97 m_was_one_shot(false) {
98 StoreBPInfo();
99 }
100
101 StopInfoBreakpoint(Thread &thread, break_id_t break_id, bool should_stop)
102 : StopInfo(thread, break_id), m_should_stop(should_stop),
103 m_should_stop_is_valid(true), m_should_perform_action(true),
104 m_address(LLDB_INVALID_ADDRESS), m_break_id(LLDB_INVALID_BREAK_ID),
105 m_was_one_shot(false) {
106 StoreBPInfo();
107 }
108
109 ~StopInfoBreakpoint() override = default;
110
111 void StoreBPInfo() {
112 ThreadSP thread_sp(m_thread_wp.lock());
113 if (thread_sp) {
114 BreakpointSiteSP bp_site_sp(
115 thread_sp->GetProcess()->GetBreakpointSiteList().FindByID(m_value));
116 if (bp_site_sp) {
117 if (bp_site_sp->GetNumberOfOwners() == 1) {
118 BreakpointLocationSP bp_loc_sp = bp_site_sp->GetOwnerAtIndex(0);
119 if (bp_loc_sp) {
120 m_break_id = bp_loc_sp->GetBreakpoint().GetID();
121 m_was_one_shot = bp_loc_sp->GetBreakpoint().IsOneShot();
122 }
123 }
124 m_address = bp_site_sp->GetLoadAddress();
125 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000126 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000128
Kate Stoneb9c1b512016-09-06 20:57:50 +0000129 bool IsValidForOperatingSystemThread(Thread &thread) override {
130 ProcessSP process_sp(thread.GetProcess());
131 if (process_sp) {
132 BreakpointSiteSP bp_site_sp(
133 process_sp->GetBreakpointSiteList().FindByID(m_value));
134 if (bp_site_sp)
135 return bp_site_sp->ValidForThisThread(&thread);
Jim Inghamca36cd12012-10-05 19:16:31 +0000136 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000137 return false;
138 }
Jim Inghamca36cd12012-10-05 19:16:31 +0000139
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140 StopReason GetStopReason() const override { return eStopReasonBreakpoint; }
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000141
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142 bool ShouldStopSynchronous(Event *event_ptr) override {
143 ThreadSP thread_sp(m_thread_wp.lock());
144 if (thread_sp) {
145 if (!m_should_stop_is_valid) {
146 // Only check once if we should stop at a breakpoint
147 BreakpointSiteSP bp_site_sp(
148 thread_sp->GetProcess()->GetBreakpointSiteList().FindByID(m_value));
149 if (bp_site_sp) {
150 ExecutionContext exe_ctx(thread_sp->GetStackFrameAtIndex(0));
151 StoppointCallbackContext context(event_ptr, exe_ctx, true);
152 bp_site_sp->BumpHitCounts();
153 m_should_stop = bp_site_sp->ShouldStop(&context);
154 } else {
155 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
156
157 if (log)
158 log->Printf(
159 "Process::%s could not find breakpoint site id: %" PRId64 "...",
160 __FUNCTION__, m_value);
161
162 m_should_stop = true;
163 }
164 m_should_stop_is_valid = true;
165 }
166 return m_should_stop;
167 }
168 return false;
169 }
170
171 bool DoShouldNotify(Event *event_ptr) override {
172 ThreadSP thread_sp(m_thread_wp.lock());
173 if (thread_sp) {
174 BreakpointSiteSP bp_site_sp(
175 thread_sp->GetProcess()->GetBreakpointSiteList().FindByID(m_value));
176 if (bp_site_sp) {
177 bool all_internal = true;
178
179 for (uint32_t i = 0; i < bp_site_sp->GetNumberOfOwners(); i++) {
180 if (!bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint().IsInternal()) {
181 all_internal = false;
182 break;
183 }
184 }
185 return !all_internal;
186 }
187 }
188 return true;
189 }
190
191 const char *GetDescription() override {
192 if (m_description.empty()) {
193 ThreadSP thread_sp(m_thread_wp.lock());
194 if (thread_sp) {
195 BreakpointSiteSP bp_site_sp(
196 thread_sp->GetProcess()->GetBreakpointSiteList().FindByID(m_value));
197 if (bp_site_sp) {
198 StreamString strm;
199 // If we have just hit an internal breakpoint, and it has a kind
Adrian Prantl05097242018-04-30 16:49:04 +0000200 // description, print that instead of the full breakpoint printing:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000201 if (bp_site_sp->IsInternal()) {
202 size_t num_owners = bp_site_sp->GetNumberOfOwners();
203 for (size_t idx = 0; idx < num_owners; idx++) {
204 const char *kind = bp_site_sp->GetOwnerAtIndex(idx)
205 ->GetBreakpoint()
206 .GetBreakpointKind();
207 if (kind != nullptr) {
208 m_description.assign(kind);
209 return kind;
210 }
Jim Inghamca36cd12012-10-05 19:16:31 +0000211 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000212 }
Jim Ingham36f3b362010-10-14 23:45:03 +0000213
Kate Stoneb9c1b512016-09-06 20:57:50 +0000214 strm.Printf("breakpoint ");
215 bp_site_sp->GetDescription(&strm, eDescriptionLevelBrief);
Zachary Turnerc1564272016-11-16 21:15:24 +0000216 m_description = strm.GetString();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000217 } else {
218 StreamString strm;
219 if (m_break_id != LLDB_INVALID_BREAK_ID) {
220 BreakpointSP break_sp =
221 thread_sp->GetProcess()->GetTarget().GetBreakpointByID(
222 m_break_id);
223 if (break_sp) {
224 if (break_sp->IsInternal()) {
225 const char *kind = break_sp->GetBreakpointKind();
226 if (kind)
227 strm.Printf("internal %s breakpoint(%d).", kind, m_break_id);
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000228 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000229 strm.Printf("internal breakpoint(%d).", m_break_id);
230 } else {
231 strm.Printf("breakpoint %d.", m_break_id);
232 }
233 } else {
234 if (m_was_one_shot)
235 strm.Printf("one-shot breakpoint %d", m_break_id);
236 else
237 strm.Printf("breakpoint %d which has been deleted.",
238 m_break_id);
Greg Claytonf4b47e12010-08-04 01:40:35 +0000239 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000240 } else if (m_address == LLDB_INVALID_ADDRESS)
241 strm.Printf("breakpoint site %" PRIi64
242 " which has been deleted - unknown address",
243 m_value);
244 else
245 strm.Printf("breakpoint site %" PRIi64
246 " which has been deleted - was at 0x%" PRIx64,
247 m_value, m_address);
248
Zachary Turnerc1564272016-11-16 21:15:24 +0000249 m_description = strm.GetString();
Greg Claytonf4b47e12010-08-04 01:40:35 +0000250 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000251 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000252 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000253 return m_description.c_str();
254 }
Jim Inghamf57b4352012-11-10 02:08:14 +0000255
256protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000257 bool ShouldStop(Event *event_ptr) override {
Adrian Prantl05097242018-04-30 16:49:04 +0000258 // This just reports the work done by PerformAction or the synchronous
259 // stop. It should only ever get called after they have had a chance to
260 // run.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000261 assert(m_should_stop_is_valid);
262 return m_should_stop;
263 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000264
Kate Stoneb9c1b512016-09-06 20:57:50 +0000265 void PerformAction(Event *event_ptr) override {
266 if (!m_should_perform_action)
267 return;
268 m_should_perform_action = false;
Boris Ulasevich86aaa8a2017-02-15 11:42:47 +0000269 bool internal_breakpoint = true;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000270
Kate Stoneb9c1b512016-09-06 20:57:50 +0000271 ThreadSP thread_sp(m_thread_wp.lock());
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000272
Kate Stoneb9c1b512016-09-06 20:57:50 +0000273 if (thread_sp) {
274 Log *log = lldb_private::GetLogIfAnyCategoriesSet(
275 LIBLLDB_LOG_BREAKPOINTS | LIBLLDB_LOG_STEP);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000276
Kate Stoneb9c1b512016-09-06 20:57:50 +0000277 if (!thread_sp->IsValid()) {
278 // This shouldn't ever happen, but just in case, don't do more harm.
279 if (log) {
280 log->Printf("PerformAction got called with an invalid thread.");
281 }
282 m_should_stop = true;
283 m_should_stop_is_valid = true;
284 return;
285 }
286
287 BreakpointSiteSP bp_site_sp(
288 thread_sp->GetProcess()->GetBreakpointSiteList().FindByID(m_value));
289 std::unordered_set<break_id_t> precondition_breakpoints;
290
291 if (bp_site_sp) {
292 // Let's copy the owners list out of the site and store them in a local
Adrian Prantl05097242018-04-30 16:49:04 +0000293 // list. That way if one of the breakpoint actions changes the site,
294 // then we won't be operating on a bad list.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000295 BreakpointLocationCollection site_locations;
296 size_t num_owners = bp_site_sp->CopyOwnersList(site_locations);
297
298 if (num_owners == 0) {
299 m_should_stop = true;
300 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000301 // We go through each location, and test first its precondition -
302 // this overrides everything. Note, we only do this once per
303 // breakpoint - not once per location... Then check the condition.
304 // If the condition says to stop, then we run the callback for that
305 // location. If that callback says to stop as well, then we set
306 // m_should_stop to true; we are going to stop. But we still want to
307 // give all the breakpoints whose conditions say we are going to stop
308 // a chance to run their callbacks. Of course if any callback
309 // restarts the target by putting "continue" in the callback, then
Kate Stoneb9c1b512016-09-06 20:57:50 +0000310 // we're going to restart, without running the rest of the callbacks.
Adrian Prantl05097242018-04-30 16:49:04 +0000311 // And in this case we will end up not stopping even if another
312 // location said we should stop. But that's better than not running
313 // all the callbacks.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000314
315 m_should_stop = false;
316
317 // We don't select threads as we go through them testing breakpoint
Adrian Prantl05097242018-04-30 16:49:04 +0000318 // conditions and running commands. So we need to set the thread for
319 // expression evaluation here:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000320 ThreadList::ExpressionExecutionThreadPusher thread_pusher(thread_sp);
321
322 ExecutionContext exe_ctx(thread_sp->GetStackFrameAtIndex(0));
323 Process *process = exe_ctx.GetProcessPtr();
324 if (process->GetModIDRef().IsLastResumeForUserExpression()) {
325 // If we are in the middle of evaluating an expression, don't run
Adrian Prantl05097242018-04-30 16:49:04 +0000326 // asynchronous breakpoint commands or expressions. That could
327 // lead to infinite recursion if the command or condition re-calls
328 // the function with this breakpoint.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000329 // TODO: We can keep a list of the breakpoints we've seen while
330 // running expressions in the nested
331 // PerformAction calls that can arise when the action runs a
Adrian Prantl05097242018-04-30 16:49:04 +0000332 // function that hits another breakpoint, and only stop running
333 // commands when we see the same breakpoint hit a second time.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000334
335 m_should_stop_is_valid = true;
336 if (log)
337 log->Printf("StopInfoBreakpoint::PerformAction - Hit a "
338 "breakpoint while running an expression,"
339 " not running commands to avoid recursion.");
340 bool ignoring_breakpoints =
341 process->GetIgnoreBreakpointsInExpressions();
342 if (ignoring_breakpoints) {
343 m_should_stop = false;
344 // Internal breakpoints will always stop.
345 for (size_t j = 0; j < num_owners; j++) {
346 lldb::BreakpointLocationSP bp_loc_sp =
347 bp_site_sp->GetOwnerAtIndex(j);
348 if (bp_loc_sp->GetBreakpoint().IsInternal()) {
349 m_should_stop = true;
350 break;
Jason Molenda3f032ff2013-08-06 23:08:59 +0000351 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000352 }
353 } else {
354 m_should_stop = true;
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000355 }
356 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000357 log->Printf("StopInfoBreakpoint::PerformAction - in expression, "
358 "continuing: %s.",
359 m_should_stop ? "true" : "false");
360 process->GetTarget().GetDebugger().GetAsyncOutputStream()->Printf(
361 "Warning: hit breakpoint while "
362 "running function, skipping commands and conditions to prevent "
363 "recursion.");
364 return;
365 }
366
367 StoppointCallbackContext context(event_ptr, exe_ctx, false);
368
369 // For safety's sake let's also grab an extra reference to the
Adrian Prantl05097242018-04-30 16:49:04 +0000370 // breakpoint owners of the locations we're going to examine, since
371 // the locations are going to have to get back to their breakpoints,
372 // and the locations don't keep their owners alive. I'm just
373 // sticking the BreakpointSP's in a vector since I'm only using it to
374 // locally increment their retain counts.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000375
376 std::vector<lldb::BreakpointSP> location_owners;
377
378 for (size_t j = 0; j < num_owners; j++) {
379 BreakpointLocationSP loc(site_locations.GetByIndex(j));
380 location_owners.push_back(loc->GetBreakpoint().shared_from_this());
381 }
382
383 for (size_t j = 0; j < num_owners; j++) {
384 lldb::BreakpointLocationSP bp_loc_sp = site_locations.GetByIndex(j);
Jim Inghamf08f5c92017-08-03 18:13:24 +0000385 StreamString loc_desc;
386 if (log) {
387 bp_loc_sp->GetDescription(&loc_desc, eDescriptionLevelBrief);
388 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000389 // If another action disabled this breakpoint or its location, then
390 // don't run the actions.
391 if (!bp_loc_sp->IsEnabled() ||
392 !bp_loc_sp->GetBreakpoint().IsEnabled())
393 continue;
394
395 // The breakpoint site may have many locations associated with it,
Adrian Prantl05097242018-04-30 16:49:04 +0000396 // not all of them valid for this thread. Skip the ones that
397 // aren't:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000398 if (!bp_loc_sp->ValidForThisThread(thread_sp.get())) {
399 if (log) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000400 log->Printf("Breakpoint %s hit on thread 0x%llx but it was not "
401 "for this thread, continuing.",
Jim Inghamf08f5c92017-08-03 18:13:24 +0000402 loc_desc.GetData(), static_cast<unsigned long long>(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000403 thread_sp->GetID()));
404 }
405 continue;
406 }
407
Jim Inghamf08f5c92017-08-03 18:13:24 +0000408 internal_breakpoint = bp_loc_sp->GetBreakpoint().IsInternal();
409
Kate Stoneb9c1b512016-09-06 20:57:50 +0000410 // First run the precondition, but since the precondition is per
Adrian Prantl05097242018-04-30 16:49:04 +0000411 // breakpoint, only run it once per breakpoint.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000412 std::pair<std::unordered_set<break_id_t>::iterator, bool> result =
413 precondition_breakpoints.insert(
414 bp_loc_sp->GetBreakpoint().GetID());
415 if (!result.second)
416 continue;
417
418 bool precondition_result =
419 bp_loc_sp->GetBreakpoint().EvaluatePrecondition(context);
420 if (!precondition_result)
421 continue;
422
423 // Next run the condition for the breakpoint. If that says we
Adrian Prantl05097242018-04-30 16:49:04 +0000424 // should stop, then we'll run the callback for the breakpoint. If
425 // the callback says we shouldn't stop that will win.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000426
427 if (bp_loc_sp->GetConditionText() != nullptr) {
Zachary Turner97206d52017-05-12 04:51:55 +0000428 Status condition_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000429 bool condition_says_stop =
430 bp_loc_sp->ConditionSaysStop(exe_ctx, condition_error);
431
432 if (!condition_error.Success()) {
433 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
434 StreamSP error_sp = debugger.GetAsyncErrorStream();
435 error_sp->Printf("Stopped due to an error evaluating condition "
436 "of breakpoint ");
437 bp_loc_sp->GetDescription(error_sp.get(),
438 eDescriptionLevelBrief);
439 error_sp->Printf(": \"%s\"", bp_loc_sp->GetConditionText());
440 error_sp->EOL();
441 const char *err_str =
442 condition_error.AsCString("<Unknown Error>");
443 if (log)
444 log->Printf("Error evaluating condition: \"%s\"\n", err_str);
445
446 error_sp->PutCString(err_str);
447 error_sp->EOL();
448 error_sp->Flush();
449 } else {
450 if (log) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000451 log->Printf("Condition evaluated for breakpoint %s on thread "
452 "0x%llx conditon_says_stop: %i.",
Jim Inghamf08f5c92017-08-03 18:13:24 +0000453 loc_desc.GetData(),
454 static_cast<unsigned long long>(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000455 thread_sp->GetID()),
456 condition_says_stop);
457 }
458 if (!condition_says_stop) {
459 // We don't want to increment the hit count of breakpoints if
Adrian Prantl05097242018-04-30 16:49:04 +0000460 // the condition fails. We've already bumped it by the time
461 // we get here, so undo the bump:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000462 bp_loc_sp->UndoBumpHitCount();
463 continue;
464 }
465 }
466 }
467
Jim Inghamf08f5c92017-08-03 18:13:24 +0000468 // Check the auto-continue bit on the location, do this before the
469 // callback since it may change this, but that would be for the
470 // NEXT hit. Note, you might think you could check auto-continue
471 // before the condition, and not evaluate the condition if it says
472 // to continue. But failing the condition means the breakpoint was
473 // effectively NOT HIT. So these two states are different.
474 bool auto_continue_says_stop = true;
475 if (bp_loc_sp->IsAutoContinue())
476 {
477 if (log)
478 log->Printf("Continuing breakpoint %s as AutoContinue was set.",
479 loc_desc.GetData());
480 // We want this stop reported, so you will know we auto-continued
481 // but only for external breakpoints:
482 if (!internal_breakpoint)
483 thread_sp->SetShouldReportStop(eVoteYes);
484 auto_continue_says_stop = false;
485 }
486
487 bool callback_says_stop = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000488
489 // FIXME: For now the callbacks have to run in async mode - the
490 // first time we restart we need
491 // to get out of there. So set it here.
492 // When we figure out how to nest breakpoint hits then this will
493 // change.
494
495 Debugger &debugger = thread_sp->CalculateTarget()->GetDebugger();
496 bool old_async = debugger.GetAsyncExecution();
497 debugger.SetAsyncExecution(true);
498
499 callback_says_stop = bp_loc_sp->InvokeCallback(&context);
500
501 debugger.SetAsyncExecution(old_async);
502
Jim Inghamf08f5c92017-08-03 18:13:24 +0000503 if (callback_says_stop && auto_continue_says_stop)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000504 m_should_stop = true;
Boris Ulasevich86aaa8a2017-02-15 11:42:47 +0000505
Kate Stoneb9c1b512016-09-06 20:57:50 +0000506 // If we are going to stop for this breakpoint, then remove the
507 // breakpoint.
508 if (callback_says_stop && bp_loc_sp &&
509 bp_loc_sp->GetBreakpoint().IsOneShot()) {
510 thread_sp->GetProcess()->GetTarget().RemoveBreakpointByID(
511 bp_loc_sp->GetBreakpoint().GetID());
512 }
Adrian Prantl05097242018-04-30 16:49:04 +0000513 // Also make sure that the callback hasn't continued the target. If
514 // it did, when we'll set m_should_start to false and get out of
Kate Stoneb9c1b512016-09-06 20:57:50 +0000515 // here.
516 if (HasTargetRunSinceMe()) {
517 m_should_stop = false;
518 break;
519 }
520 }
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000521 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000522 // We've figured out what this stop wants to do, so mark it as valid so
523 // we don't compute it again.
524 m_should_stop_is_valid = true;
525 } else {
526 m_should_stop = true;
527 m_should_stop_is_valid = true;
528 Log *log_process(
529 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
530
531 if (log_process)
532 log_process->Printf(
533 "Process::%s could not find breakpoint site id: %" PRId64 "...",
534 __FUNCTION__, m_value);
535 }
Boris Ulasevich86aaa8a2017-02-15 11:42:47 +0000536
537 if ((m_should_stop == false || internal_breakpoint)
538 && thread_sp->CompletedPlanOverridesBreakpoint()) {
539
Adrian Prantl05097242018-04-30 16:49:04 +0000540 // Override should_stop decision when we have completed step plan
541 // additionally to the breakpoint
Boris Ulasevich86aaa8a2017-02-15 11:42:47 +0000542 m_should_stop = true;
543
Adrian Prantl05097242018-04-30 16:49:04 +0000544 // Here we clean the preset stop info so the next GetStopInfo call will
545 // find the appropriate stop info, which should be the stop info
546 // related to the completed plan
Boris Ulasevich86aaa8a2017-02-15 11:42:47 +0000547 thread_sp->ResetStopInfo();
548 }
549
Kate Stoneb9c1b512016-09-06 20:57:50 +0000550 if (log)
551 log->Printf("Process::%s returning from action with m_should_stop: %d.",
552 __FUNCTION__, m_should_stop);
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000553 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000554 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000555
556private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000557 bool m_should_stop;
558 bool m_should_stop_is_valid;
559 bool m_should_perform_action; // Since we are trying to preserve the "state"
560 // of the system even if we run functions
561 // etc. behind the users backs, we need to make sure we only REALLY perform
562 // the action once.
563 lldb::addr_t m_address; // We use this to capture the breakpoint site address
564 // when we create the StopInfo,
565 // in case somebody deletes it between the time the StopInfo is made and the
566 // description is asked for.
567 lldb::break_id_t m_break_id;
568 bool m_was_one_shot;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000569};
570
Greg Claytonf4b47e12010-08-04 01:40:35 +0000571//----------------------------------------------------------------------
572// StopInfoWatchpoint
573//----------------------------------------------------------------------
574
Kate Stoneb9c1b512016-09-06 20:57:50 +0000575class StopInfoWatchpoint : public StopInfo {
Greg Claytonf4b47e12010-08-04 01:40:35 +0000576public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000577 // Make sure watchpoint is properly disabled and subsequently enabled while
578 // performing watchpoint actions.
579 class WatchpointSentry {
580 public:
Jim Ingham209a77d2016-10-25 20:34:32 +0000581 WatchpointSentry(ProcessSP p_sp, WatchpointSP w_sp) : process_sp(p_sp),
582 watchpoint_sp(w_sp) {
583 if (process_sp && watchpoint_sp) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000584 const bool notify = false;
Jim Ingham209a77d2016-10-25 20:34:32 +0000585 watchpoint_sp->TurnOnEphemeralMode();
586 process_sp->DisableWatchpoint(watchpoint_sp.get(), notify);
587 process_sp->AddPreResumeAction(SentryPreResumeAction, this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000588 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000589 }
Jim Ingham209a77d2016-10-25 20:34:32 +0000590
591 void DoReenable() {
592 if (process_sp && watchpoint_sp) {
593 bool was_disabled = watchpoint_sp->IsDisabledDuringEphemeralMode();
594 watchpoint_sp->TurnOffEphemeralMode();
595 const bool notify = false;
596 if (was_disabled) {
597 process_sp->DisableWatchpoint(watchpoint_sp.get(), notify);
598 } else {
599 process_sp->EnableWatchpoint(watchpoint_sp.get(), notify);
Pavel Labath2e8fe802016-10-21 10:52:11 +0000600 }
Pavel Labath2e8fe802016-10-21 10:52:11 +0000601 }
Jim Ingham94bd5752016-10-21 00:06:38 +0000602 }
Jim Ingham209a77d2016-10-25 20:34:32 +0000603
604 ~WatchpointSentry() {
605 DoReenable();
606 if (process_sp)
607 process_sp->ClearPreResumeAction(SentryPreResumeAction, this);
608 }
609
610 static bool SentryPreResumeAction(void *sentry_void) {
611 WatchpointSentry *sentry = (WatchpointSentry *) sentry_void;
612 sentry->DoReenable();
613 return true;
614 }
Jim Inghamf57b4352012-11-10 02:08:14 +0000615
Kate Stoneb9c1b512016-09-06 20:57:50 +0000616 private:
Jim Ingham209a77d2016-10-25 20:34:32 +0000617 ProcessSP process_sp;
618 WatchpointSP watchpoint_sp;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000619 };
620
621 StopInfoWatchpoint(Thread &thread, break_id_t watch_id,
622 lldb::addr_t watch_hit_addr)
623 : StopInfo(thread, watch_id), m_should_stop(false),
624 m_should_stop_is_valid(false), m_watch_hit_addr(watch_hit_addr) {}
625
626 ~StopInfoWatchpoint() override = default;
627
628 StopReason GetStopReason() const override { return eStopReasonWatchpoint; }
629
630 const char *GetDescription() override {
631 if (m_description.empty()) {
632 StreamString strm;
633 strm.Printf("watchpoint %" PRIi64, m_value);
Zachary Turnerc1564272016-11-16 21:15:24 +0000634 m_description = strm.GetString();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000635 }
636 return m_description.c_str();
637 }
638
Jim Inghamf57b4352012-11-10 02:08:14 +0000639protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000640 bool ShouldStopSynchronous(Event *event_ptr) override {
Adrian Prantl05097242018-04-30 16:49:04 +0000641 // ShouldStop() method is idempotent and should not affect hit count. See
642 // Process::RunPrivateStateThread()->Process()->HandlePrivateEvent()
Kate Stoneb9c1b512016-09-06 20:57:50 +0000643 // -->Process()::ShouldBroadcastEvent()->ThreadList::ShouldStop()->
644 // Thread::ShouldStop()->ThreadPlanBase::ShouldStop()->
645 // StopInfoWatchpoint::ShouldStop() and
646 // Event::DoOnRemoval()->Process::ProcessEventData::DoOnRemoval()->
647 // StopInfoWatchpoint::PerformAction().
648 if (m_should_stop_is_valid)
649 return m_should_stop;
Johnny Chenfd158f42011-09-21 22:47:15 +0000650
Kate Stoneb9c1b512016-09-06 20:57:50 +0000651 ThreadSP thread_sp(m_thread_wp.lock());
652 if (thread_sp) {
653 WatchpointSP wp_sp(
654 thread_sp->CalculateTarget()->GetWatchpointList().FindByID(
655 GetValue()));
656 if (wp_sp) {
657 // Check if we should stop at a watchpoint.
658 ExecutionContext exe_ctx(thread_sp->GetStackFrameAtIndex(0));
659 StoppointCallbackContext context(event_ptr, exe_ctx, true);
660 m_should_stop = wp_sp->ShouldStop(&context);
661 } else {
662 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
Johnny Chenfd158f42011-09-21 22:47:15 +0000663
Kate Stoneb9c1b512016-09-06 20:57:50 +0000664 if (log)
665 log->Printf(
666 "Process::%s could not find watchpoint location id: %" PRId64
667 "...",
668 __FUNCTION__, GetValue());
Johnny Chenfd158f42011-09-21 22:47:15 +0000669
Johnny Chen16dcf712011-10-17 18:58:00 +0000670 m_should_stop = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000671 }
Johnny Chen16dcf712011-10-17 18:58:00 +0000672 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000673 m_should_stop_is_valid = true;
674 return m_should_stop;
675 }
676
677 bool ShouldStop(Event *event_ptr) override {
Adrian Prantl05097242018-04-30 16:49:04 +0000678 // This just reports the work done by PerformAction or the synchronous
679 // stop. It should only ever get called after they have had a chance to
680 // run.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000681 assert(m_should_stop_is_valid);
682 return m_should_stop;
683 }
684
685 void PerformAction(Event *event_ptr) override {
686 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS);
687 // We're going to calculate if we should stop or not in some way during the
Adrian Prantl05097242018-04-30 16:49:04 +0000688 // course of this code. Also by default we're going to stop, so set that
689 // here.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000690 m_should_stop = true;
Jim Ingham209a77d2016-10-25 20:34:32 +0000691
Kate Stoneb9c1b512016-09-06 20:57:50 +0000692
693 ThreadSP thread_sp(m_thread_wp.lock());
694 if (thread_sp) {
695
696 WatchpointSP wp_sp(
697 thread_sp->CalculateTarget()->GetWatchpointList().FindByID(
Jim Ingham209a77d2016-10-25 20:34:32 +0000698 GetValue()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000699 if (wp_sp) {
700 ExecutionContext exe_ctx(thread_sp->GetStackFrameAtIndex(0));
Jim Ingham209a77d2016-10-25 20:34:32 +0000701 ProcessSP process_sp = exe_ctx.GetProcessSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000702
703 {
704 // check if this process is running on an architecture where
Adrian Prantl05097242018-04-30 16:49:04 +0000705 // watchpoints trigger before the associated instruction runs. if so,
706 // disable the WP, single-step and then re-enable the watchpoint
Jim Ingham209a77d2016-10-25 20:34:32 +0000707 if (process_sp) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000708 uint32_t num;
709 bool wp_triggers_after;
Jim Ingham209a77d2016-10-25 20:34:32 +0000710
711 if (process_sp->GetWatchpointSupportInfo(num, wp_triggers_after)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000712 .Success()) {
713 if (!wp_triggers_after) {
Adrian Prantl05097242018-04-30 16:49:04 +0000714 // We need to preserve the watch_index before watchpoint is
715 // disable. Since Watchpoint::SetEnabled will clear the watch
716 // index. This will fix TestWatchpointIter failure
Nitesh Jain0546e842016-12-09 13:54:47 +0000717 Watchpoint *wp = wp_sp.get();
718 uint32_t watch_index = wp->GetHardwareIndex();
719 process_sp->DisableWatchpoint(wp, false);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000720 StopInfoSP stored_stop_info_sp = thread_sp->GetStopInfo();
721 assert(stored_stop_info_sp.get() == this);
722
723 ThreadPlanSP new_plan_sp(
724 thread_sp->QueueThreadPlanForStepSingleInstruction(
725 false, // step-over
726 false, // abort_other_plans
727 true)); // stop_other_threads
728 new_plan_sp->SetIsMasterPlan(true);
729 new_plan_sp->SetOkayToDiscard(false);
730 new_plan_sp->SetPrivate(true);
Jim Ingham209a77d2016-10-25 20:34:32 +0000731 process_sp->GetThreadList().SetSelectedThreadByID(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000732 thread_sp->GetID());
Jim Ingham209a77d2016-10-25 20:34:32 +0000733 process_sp->ResumeSynchronous(nullptr);
734 process_sp->GetThreadList().SetSelectedThreadByID(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000735 thread_sp->GetID());
736 thread_sp->SetStopInfo(stored_stop_info_sp);
Nitesh Jain0546e842016-12-09 13:54:47 +0000737 process_sp->EnableWatchpoint(wp, false);
738 wp->SetHardwareIndex(watch_index);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000739 }
740 }
741 }
742 }
743
Jim Ingham209a77d2016-10-25 20:34:32 +0000744 // This sentry object makes sure the current watchpoint is disabled
Adrian Prantl05097242018-04-30 16:49:04 +0000745 // while performing watchpoint actions, and it is then enabled after we
746 // are finished.
Jim Ingham209a77d2016-10-25 20:34:32 +0000747 WatchpointSentry sentry(process_sp, wp_sp);
748
Kate Stoneb9c1b512016-09-06 20:57:50 +0000749 /*
750 * MIPS: Last 3bits of the watchpoint address are masked by the kernel.
751 * For example:
752 * 'n' is at 0x120010d00 and 'm' is 0x120010d04. When a watchpoint is
753 * set at 'm', then
754 * watch exception is generated even when 'n' is read/written. To handle
755 * this case,
756 * server emulates the instruction at PC and finds the base address of
757 * the load/store
758 * instruction and appends it in the description of the stop-info
759 * packet. If watchpoint
760 * is not set on this address by user then this do not stop.
761 */
762 if (m_watch_hit_addr != LLDB_INVALID_ADDRESS) {
763 WatchpointSP wp_hit_sp =
764 thread_sp->CalculateTarget()->GetWatchpointList().FindByAddress(
765 m_watch_hit_addr);
766 if (!wp_hit_sp) {
767 m_should_stop = false;
768 wp_sp->IncrementFalseAlarmsAndReviseHitCount();
769 }
770 }
771
772 // TODO: This condition should be checked in the synchronous part of the
773 // watchpoint code
774 // (Watchpoint::ShouldStop), so that we avoid pulling an event even if
Adrian Prantl05097242018-04-30 16:49:04 +0000775 // the watchpoint fails the ignore count condition. It is moved here
776 // temporarily, because for archs with
777 // watchpoint_exceptions_received=before, the code in the previous
778 // lines takes care of moving the inferior to next PC. We have to check
779 // the ignore count condition after this is done, otherwise we will hit
780 // same watchpoint multiple times until we pass ignore condition, but
781 // we won't actually be ignoring them.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000782 if (wp_sp->GetHitCount() <= wp_sp->GetIgnoreCount())
783 m_should_stop = false;
784
Jim Ingham209a77d2016-10-25 20:34:32 +0000785 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
786
Kate Stoneb9c1b512016-09-06 20:57:50 +0000787 if (m_should_stop && wp_sp->GetConditionText() != nullptr) {
788 // We need to make sure the user sees any parse errors in their
Zachary Turnerc5d7df92016-11-08 04:52:16 +0000789 // condition, so we'll hook the constructor errors up to the
790 // debugger's Async I/O.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000791 ExpressionResults result_code;
792 EvaluateExpressionOptions expr_options;
793 expr_options.SetUnwindOnError(true);
794 expr_options.SetIgnoreBreakpoints(true);
795 ValueObjectSP result_value_sp;
Zachary Turner97206d52017-05-12 04:51:55 +0000796 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000797 result_code = UserExpression::Evaluate(
Zachary Turnerc5d7df92016-11-08 04:52:16 +0000798 exe_ctx, expr_options, wp_sp->GetConditionText(),
799 llvm::StringRef(), result_value_sp, error);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000800
801 if (result_code == eExpressionCompleted) {
802 if (result_value_sp) {
803 Scalar scalar_value;
804 if (result_value_sp->ResolveValue(scalar_value)) {
805 if (scalar_value.ULongLong(1) == 0) {
806 // We have been vetoed. This takes precedence over querying
Adrian Prantl05097242018-04-30 16:49:04 +0000807 // the watchpoint whether it should stop (aka ignore count
808 // and friends). See also StopInfoWatchpoint::ShouldStop()
809 // as well as Process::ProcessEventData::DoOnRemoval().
Kate Stoneb9c1b512016-09-06 20:57:50 +0000810 m_should_stop = false;
811 } else
812 m_should_stop = true;
813 if (log)
814 log->Printf(
815 "Condition successfully evaluated, result is %s.\n",
816 m_should_stop ? "true" : "false");
817 } else {
818 m_should_stop = true;
819 if (log)
820 log->Printf(
821 "Failed to get an integer result from the expression.");
822 }
823 }
824 } else {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000825 StreamSP error_sp = debugger.GetAsyncErrorStream();
826 error_sp->Printf(
827 "Stopped due to an error evaluating condition of watchpoint ");
828 wp_sp->GetDescription(error_sp.get(), eDescriptionLevelBrief);
829 error_sp->Printf(": \"%s\"", wp_sp->GetConditionText());
830 error_sp->EOL();
831 const char *err_str = error.AsCString("<Unknown Error>");
832 if (log)
833 log->Printf("Error evaluating condition: \"%s\"\n", err_str);
834
835 error_sp->PutCString(err_str);
836 error_sp->EOL();
837 error_sp->Flush();
838 // If the condition fails to be parsed or run, we should stop.
839 m_should_stop = true;
840 }
841 }
842
843 // If the condition says to stop, we run the callback to further decide
844 // whether to stop.
845 if (m_should_stop) {
Jim Ingham209a77d2016-10-25 20:34:32 +0000846 // FIXME: For now the callbacks have to run in async mode - the
847 // first time we restart we need
848 // to get out of there. So set it here.
849 // When we figure out how to nest watchpoint hits then this will
850 // change.
851
852 bool old_async = debugger.GetAsyncExecution();
853 debugger.SetAsyncExecution(true);
854
Kate Stoneb9c1b512016-09-06 20:57:50 +0000855 StoppointCallbackContext context(event_ptr, exe_ctx, false);
856 bool stop_requested = wp_sp->InvokeCallback(&context);
Jim Ingham209a77d2016-10-25 20:34:32 +0000857
858 debugger.SetAsyncExecution(old_async);
859
Adrian Prantl05097242018-04-30 16:49:04 +0000860 // Also make sure that the callback hasn't continued the target. If
861 // it did, when we'll set m_should_stop to false and get out of here.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000862 if (HasTargetRunSinceMe())
863 m_should_stop = false;
864
865 if (m_should_stop && !stop_requested) {
866 // We have been vetoed by the callback mechanism.
867 m_should_stop = false;
868 }
869 }
870 // Finally, if we are going to stop, print out the new & old values:
871 if (m_should_stop) {
872 wp_sp->CaptureWatchedValue(exe_ctx);
873
874 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
875 StreamSP output_sp = debugger.GetAsyncOutputStream();
876 wp_sp->DumpSnapshots(output_sp.get());
877 output_sp->EOL();
878 output_sp->Flush();
879 }
880
881 } else {
882 Log *log_process(
883 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
884
885 if (log_process)
886 log_process->Printf(
887 "Process::%s could not find watchpoint id: %" PRId64 "...",
888 __FUNCTION__, m_value);
889 }
890 if (log)
891 log->Printf("Process::%s returning from action with m_should_stop: %d.",
892 __FUNCTION__, m_should_stop);
893
894 m_should_stop_is_valid = true;
895 }
896 }
897
Greg Claytonf4b47e12010-08-04 01:40:35 +0000898private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000899 bool m_should_stop;
900 bool m_should_stop_is_valid;
901 lldb::addr_t m_watch_hit_addr;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000902};
903
Greg Claytonf4b47e12010-08-04 01:40:35 +0000904//----------------------------------------------------------------------
905// StopInfoUnixSignal
906//----------------------------------------------------------------------
907
Kate Stoneb9c1b512016-09-06 20:57:50 +0000908class StopInfoUnixSignal : public StopInfo {
Greg Claytonf4b47e12010-08-04 01:40:35 +0000909public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000910 StopInfoUnixSignal(Thread &thread, int signo, const char *description)
911 : StopInfo(thread, signo) {
912 SetDescription(description);
913 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000914
Kate Stoneb9c1b512016-09-06 20:57:50 +0000915 ~StopInfoUnixSignal() override = default;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000916
Kate Stoneb9c1b512016-09-06 20:57:50 +0000917 StopReason GetStopReason() const override { return eStopReasonSignal; }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000918
Kate Stoneb9c1b512016-09-06 20:57:50 +0000919 bool ShouldStopSynchronous(Event *event_ptr) override {
920 ThreadSP thread_sp(m_thread_wp.lock());
921 if (thread_sp)
922 return thread_sp->GetProcess()->GetUnixSignals()->GetShouldStop(m_value);
923 return false;
924 }
Jim Ingham0161b492013-02-09 01:29:05 +0000925
Kate Stoneb9c1b512016-09-06 20:57:50 +0000926 bool ShouldStop(Event *event_ptr) override {
927 ThreadSP thread_sp(m_thread_wp.lock());
928 if (thread_sp)
929 return thread_sp->GetProcess()->GetUnixSignals()->GetShouldStop(m_value);
930 return false;
931 }
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000932
Kate Stoneb9c1b512016-09-06 20:57:50 +0000933 // If should stop returns false, check if we should notify of this event
934 bool DoShouldNotify(Event *event_ptr) override {
935 ThreadSP thread_sp(m_thread_wp.lock());
936 if (thread_sp) {
937 bool should_notify =
938 thread_sp->GetProcess()->GetUnixSignals()->GetShouldNotify(m_value);
939 if (should_notify) {
940 StreamString strm;
941 strm.Printf(
942 "thread %d received signal: %s", thread_sp->GetIndexID(),
943 thread_sp->GetProcess()->GetUnixSignals()->GetSignalAsCString(
944 m_value));
945 Process::ProcessEventData::AddRestartedReason(event_ptr,
946 strm.GetData());
947 }
948 return should_notify;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000949 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000950 return true;
951 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000952
Kate Stoneb9c1b512016-09-06 20:57:50 +0000953 void WillResume(lldb::StateType resume_state) override {
954 ThreadSP thread_sp(m_thread_wp.lock());
955 if (thread_sp) {
956 if (!thread_sp->GetProcess()->GetUnixSignals()->GetShouldSuppress(
957 m_value))
958 thread_sp->SetResumeSignal(m_value);
Greg Claytonf4b47e12010-08-04 01:40:35 +0000959 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000960 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000961
Kate Stoneb9c1b512016-09-06 20:57:50 +0000962 const char *GetDescription() override {
963 if (m_description.empty()) {
964 ThreadSP thread_sp(m_thread_wp.lock());
965 if (thread_sp) {
966 StreamString strm;
967 const char *signal_name =
968 thread_sp->GetProcess()->GetUnixSignals()->GetSignalAsCString(
969 m_value);
970 if (signal_name)
971 strm.Printf("signal %s", signal_name);
972 else
973 strm.Printf("signal %" PRIi64, m_value);
Zachary Turnerc1564272016-11-16 21:15:24 +0000974 m_description = strm.GetString();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000975 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000976 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000977 return m_description.c_str();
978 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000979};
980
981//----------------------------------------------------------------------
982// StopInfoTrace
983//----------------------------------------------------------------------
984
Kate Stoneb9c1b512016-09-06 20:57:50 +0000985class StopInfoTrace : public StopInfo {
Greg Claytonf4b47e12010-08-04 01:40:35 +0000986public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000987 StopInfoTrace(Thread &thread) : StopInfo(thread, LLDB_INVALID_UID) {}
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000988
Kate Stoneb9c1b512016-09-06 20:57:50 +0000989 ~StopInfoTrace() override = default;
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000990
Kate Stoneb9c1b512016-09-06 20:57:50 +0000991 StopReason GetStopReason() const override { return eStopReasonTrace; }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000992
Kate Stoneb9c1b512016-09-06 20:57:50 +0000993 const char *GetDescription() override {
994 if (m_description.empty())
995 return "trace";
996 else
997 return m_description.c_str();
998 }
Greg Claytona658fd22011-06-04 01:26:29 +0000999};
1000
Greg Claytona658fd22011-06-04 01:26:29 +00001001//----------------------------------------------------------------------
1002// StopInfoException
1003//----------------------------------------------------------------------
1004
Kate Stoneb9c1b512016-09-06 20:57:50 +00001005class StopInfoException : public StopInfo {
Greg Claytona658fd22011-06-04 01:26:29 +00001006public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001007 StopInfoException(Thread &thread, const char *description)
1008 : StopInfo(thread, LLDB_INVALID_UID) {
1009 if (description)
1010 SetDescription(description);
1011 }
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001012
Kate Stoneb9c1b512016-09-06 20:57:50 +00001013 ~StopInfoException() override = default;
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001014
Kate Stoneb9c1b512016-09-06 20:57:50 +00001015 StopReason GetStopReason() const override { return eStopReasonException; }
1016
1017 const char *GetDescription() override {
1018 if (m_description.empty())
1019 return "exception";
1020 else
1021 return m_description.c_str();
1022 }
Greg Claytonf4b47e12010-08-04 01:40:35 +00001023};
1024
Greg Claytonf4b47e12010-08-04 01:40:35 +00001025//----------------------------------------------------------------------
1026// StopInfoThreadPlan
1027//----------------------------------------------------------------------
1028
Kate Stoneb9c1b512016-09-06 20:57:50 +00001029class StopInfoThreadPlan : public StopInfo {
Greg Claytonf4b47e12010-08-04 01:40:35 +00001030public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001031 StopInfoThreadPlan(ThreadPlanSP &plan_sp, ValueObjectSP &return_valobj_sp,
1032 ExpressionVariableSP &expression_variable_sp)
1033 : StopInfo(plan_sp->GetThread(), LLDB_INVALID_UID), m_plan_sp(plan_sp),
1034 m_return_valobj_sp(return_valobj_sp),
1035 m_expression_variable_sp(expression_variable_sp) {}
Greg Claytonf4b47e12010-08-04 01:40:35 +00001036
Kate Stoneb9c1b512016-09-06 20:57:50 +00001037 ~StopInfoThreadPlan() override = default;
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001038
Kate Stoneb9c1b512016-09-06 20:57:50 +00001039 StopReason GetStopReason() const override { return eStopReasonPlanComplete; }
Greg Claytonf4b47e12010-08-04 01:40:35 +00001040
Kate Stoneb9c1b512016-09-06 20:57:50 +00001041 const char *GetDescription() override {
1042 if (m_description.empty()) {
1043 StreamString strm;
1044 m_plan_sp->GetDescription(&strm, eDescriptionLevelBrief);
Zachary Turnerc1564272016-11-16 21:15:24 +00001045 m_description = strm.GetString();
Greg Claytonf4b47e12010-08-04 01:40:35 +00001046 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001047 return m_description.c_str();
1048 }
1049
1050 ValueObjectSP GetReturnValueObject() { return m_return_valobj_sp; }
1051
1052 ExpressionVariableSP GetExpressionVariable() {
1053 return m_expression_variable_sp;
1054 }
1055
Jim Ingham0161b492013-02-09 01:29:05 +00001056protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001057 bool ShouldStop(Event *event_ptr) override {
1058 if (m_plan_sp)
1059 return m_plan_sp->ShouldStop(event_ptr);
1060 else
1061 return StopInfo::ShouldStop(event_ptr);
1062 }
Greg Claytonf4b47e12010-08-04 01:40:35 +00001063
1064private:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001065 ThreadPlanSP m_plan_sp;
1066 ValueObjectSP m_return_valobj_sp;
1067 ExpressionVariableSP m_expression_variable_sp;
Greg Claytonf4b47e12010-08-04 01:40:35 +00001068};
Kate Stoneb9c1b512016-09-06 20:57:50 +00001069
Jim Inghamba205c12017-12-05 02:50:45 +00001070//----------------------------------------------------------------------
1071// StopInfoExec
1072//----------------------------------------------------------------------
1073
Kate Stoneb9c1b512016-09-06 20:57:50 +00001074class StopInfoExec : public StopInfo {
Greg Clayton90ba8112012-12-05 00:16:59 +00001075public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001076 StopInfoExec(Thread &thread)
1077 : StopInfo(thread, LLDB_INVALID_UID), m_performed_action(false) {}
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001078
Kate Stoneb9c1b512016-09-06 20:57:50 +00001079 ~StopInfoExec() override = default;
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001080
Jim Inghamba205c12017-12-05 02:50:45 +00001081 bool ShouldStop(Event *event_ptr) override {
1082 ThreadSP thread_sp(m_thread_wp.lock());
1083 if (thread_sp)
1084 return thread_sp->GetProcess()->GetStopOnExec();
1085 return false;
1086 }
1087
Kate Stoneb9c1b512016-09-06 20:57:50 +00001088 StopReason GetStopReason() const override { return eStopReasonExec; }
1089
1090 const char *GetDescription() override { return "exec"; }
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001091
Greg Clayton90ba8112012-12-05 00:16:59 +00001092protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001093 void PerformAction(Event *event_ptr) override {
1094 // Only perform the action once
1095 if (m_performed_action)
1096 return;
1097 m_performed_action = true;
1098 ThreadSP thread_sp(m_thread_wp.lock());
1099 if (thread_sp)
1100 thread_sp->GetProcess()->DidExec();
1101 }
1102
1103 bool m_performed_action;
Greg Clayton90ba8112012-12-05 00:16:59 +00001104};
1105
Jim Ingham4b536182011-08-09 02:12:22 +00001106} // namespace lldb_private
Greg Claytonf4b47e12010-08-04 01:40:35 +00001107
Kate Stoneb9c1b512016-09-06 20:57:50 +00001108StopInfoSP StopInfo::CreateStopReasonWithBreakpointSiteID(Thread &thread,
1109 break_id_t break_id) {
1110 return StopInfoSP(new StopInfoBreakpoint(thread, break_id));
1111}
1112
1113StopInfoSP StopInfo::CreateStopReasonWithBreakpointSiteID(Thread &thread,
1114 break_id_t break_id,
1115 bool should_stop) {
1116 return StopInfoSP(new StopInfoBreakpoint(thread, break_id, should_stop));
Greg Claytonf4b47e12010-08-04 01:40:35 +00001117}
1118
1119StopInfoSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001120StopInfo::CreateStopReasonWithWatchpointID(Thread &thread, break_id_t watch_id,
1121 lldb::addr_t watch_hit_addr) {
1122 return StopInfoSP(new StopInfoWatchpoint(thread, watch_id, watch_hit_addr));
Jim Ingham36f3b362010-10-14 23:45:03 +00001123}
1124
Kate Stoneb9c1b512016-09-06 20:57:50 +00001125StopInfoSP StopInfo::CreateStopReasonWithSignal(Thread &thread, int signo,
1126 const char *description) {
1127 return StopInfoSP(new StopInfoUnixSignal(thread, signo, description));
Greg Claytonf4b47e12010-08-04 01:40:35 +00001128}
1129
Kate Stoneb9c1b512016-09-06 20:57:50 +00001130StopInfoSP StopInfo::CreateStopReasonToTrace(Thread &thread) {
1131 return StopInfoSP(new StopInfoTrace(thread));
Greg Claytonf4b47e12010-08-04 01:40:35 +00001132}
1133
Kate Stoneb9c1b512016-09-06 20:57:50 +00001134StopInfoSP StopInfo::CreateStopReasonWithPlan(
1135 ThreadPlanSP &plan_sp, ValueObjectSP return_valobj_sp,
1136 ExpressionVariableSP expression_variable_sp) {
1137 return StopInfoSP(new StopInfoThreadPlan(plan_sp, return_valobj_sp,
1138 expression_variable_sp));
Greg Claytonf4b47e12010-08-04 01:40:35 +00001139}
1140
Kate Stoneb9c1b512016-09-06 20:57:50 +00001141StopInfoSP StopInfo::CreateStopReasonWithException(Thread &thread,
1142 const char *description) {
1143 return StopInfoSP(new StopInfoException(thread, description));
Greg Claytonf4b47e12010-08-04 01:40:35 +00001144}
Greg Claytona658fd22011-06-04 01:26:29 +00001145
Kate Stoneb9c1b512016-09-06 20:57:50 +00001146StopInfoSP StopInfo::CreateStopReasonWithExec(Thread &thread) {
1147 return StopInfoSP(new StopInfoExec(thread));
Greg Claytona658fd22011-06-04 01:26:29 +00001148}
Jim Ingham73ca05a2011-12-17 01:35:57 +00001149
Kate Stoneb9c1b512016-09-06 20:57:50 +00001150ValueObjectSP StopInfo::GetReturnValueObject(StopInfoSP &stop_info_sp) {
1151 if (stop_info_sp &&
1152 stop_info_sp->GetStopReason() == eStopReasonPlanComplete) {
1153 StopInfoThreadPlan *plan_stop_info =
1154 static_cast<StopInfoThreadPlan *>(stop_info_sp.get());
1155 return plan_stop_info->GetReturnValueObject();
1156 } else
1157 return ValueObjectSP();
Greg Clayton90ba8112012-12-05 00:16:59 +00001158}
1159
Kate Stoneb9c1b512016-09-06 20:57:50 +00001160ExpressionVariableSP StopInfo::GetExpressionVariable(StopInfoSP &stop_info_sp) {
1161 if (stop_info_sp &&
1162 stop_info_sp->GetStopReason() == eStopReasonPlanComplete) {
1163 StopInfoThreadPlan *plan_stop_info =
1164 static_cast<StopInfoThreadPlan *>(stop_info_sp.get());
1165 return plan_stop_info->GetExpressionVariable();
1166 } else
1167 return ExpressionVariableSP();
Jim Ingham30fadaf2014-07-08 01:07:32 +00001168}
Sean Callanan4740a732016-09-06 04:48:36 +00001169
1170lldb::ValueObjectSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001171StopInfo::GetCrashingDereference(StopInfoSP &stop_info_sp,
1172 lldb::addr_t *crashing_address) {
1173 if (!stop_info_sp) {
1174 return ValueObjectSP();
1175 }
Sean Callanan4740a732016-09-06 04:48:36 +00001176
Kate Stoneb9c1b512016-09-06 20:57:50 +00001177 const char *description = stop_info_sp->GetDescription();
1178 if (!description) {
1179 return ValueObjectSP();
1180 }
1181
1182 ThreadSP thread_sp = stop_info_sp->GetThread();
1183 if (!thread_sp) {
1184 return ValueObjectSP();
1185 }
1186
1187 StackFrameSP frame_sp = thread_sp->GetSelectedFrame();
1188
1189 if (!frame_sp) {
1190 return ValueObjectSP();
1191 }
1192
1193 const char address_string[] = "address=";
1194
1195 const char *address_loc = strstr(description, address_string);
1196 if (!address_loc) {
1197 return ValueObjectSP();
1198 }
1199
1200 address_loc += (sizeof(address_string) - 1);
1201
1202 uint64_t address = strtoull(address_loc, 0, 0);
1203 if (crashing_address) {
1204 *crashing_address = address;
1205 }
1206
1207 return frame_sp->GuessValueForAddress(address);
Sean Callanan4740a732016-09-06 04:48:36 +00001208}