blob: 652ad8a054457f0226695a0b038ed1f72d5ea615 [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
66 // before you could
67 // ask this question as a "TRUE" answer to HasTargetRunSinceMe. But we
68 // don't want to
69 // include any running of the target done for expressions. So we track
70 // both resumes,
71 // and resumes caused by expressions, and check if there are any resumes
72 // NOT caused
73 // by expressions.
74
75 uint32_t curr_resume_id = thread_sp->GetProcess()->GetResumeID();
76 uint32_t last_user_expression_id =
77 thread_sp->GetProcess()->GetLastUserExpressionResumeID();
78 if (curr_resume_id == m_resume_id) {
79 return false;
80 } else if (curr_resume_id > last_user_expression_id) {
81 return true;
82 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +000083 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000084 }
85 return false;
Jim Ingham4b536182011-08-09 02:12:22 +000086}
87
Greg Claytonf4b47e12010-08-04 01:40:35 +000088//----------------------------------------------------------------------
89// StopInfoBreakpoint
90//----------------------------------------------------------------------
91
Kate Stoneb9c1b512016-09-06 20:57:50 +000092namespace lldb_private {
93class StopInfoBreakpoint : public StopInfo {
Greg Claytonf4b47e12010-08-04 01:40:35 +000094public:
Kate Stoneb9c1b512016-09-06 20:57:50 +000095 StopInfoBreakpoint(Thread &thread, break_id_t break_id)
96 : StopInfo(thread, break_id), m_should_stop(false),
97 m_should_stop_is_valid(false), m_should_perform_action(true),
98 m_address(LLDB_INVALID_ADDRESS), m_break_id(LLDB_INVALID_BREAK_ID),
99 m_was_one_shot(false) {
100 StoreBPInfo();
101 }
102
103 StopInfoBreakpoint(Thread &thread, break_id_t break_id, bool should_stop)
104 : StopInfo(thread, break_id), m_should_stop(should_stop),
105 m_should_stop_is_valid(true), m_should_perform_action(true),
106 m_address(LLDB_INVALID_ADDRESS), m_break_id(LLDB_INVALID_BREAK_ID),
107 m_was_one_shot(false) {
108 StoreBPInfo();
109 }
110
111 ~StopInfoBreakpoint() override = default;
112
113 void StoreBPInfo() {
114 ThreadSP thread_sp(m_thread_wp.lock());
115 if (thread_sp) {
116 BreakpointSiteSP bp_site_sp(
117 thread_sp->GetProcess()->GetBreakpointSiteList().FindByID(m_value));
118 if (bp_site_sp) {
119 if (bp_site_sp->GetNumberOfOwners() == 1) {
120 BreakpointLocationSP bp_loc_sp = bp_site_sp->GetOwnerAtIndex(0);
121 if (bp_loc_sp) {
122 m_break_id = bp_loc_sp->GetBreakpoint().GetID();
123 m_was_one_shot = bp_loc_sp->GetBreakpoint().IsOneShot();
124 }
125 }
126 m_address = bp_site_sp->GetLoadAddress();
127 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000128 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000129 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000130
Kate Stoneb9c1b512016-09-06 20:57:50 +0000131 bool IsValidForOperatingSystemThread(Thread &thread) override {
132 ProcessSP process_sp(thread.GetProcess());
133 if (process_sp) {
134 BreakpointSiteSP bp_site_sp(
135 process_sp->GetBreakpointSiteList().FindByID(m_value));
136 if (bp_site_sp)
137 return bp_site_sp->ValidForThisThread(&thread);
Jim Inghamca36cd12012-10-05 19:16:31 +0000138 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000139 return false;
140 }
Jim Inghamca36cd12012-10-05 19:16:31 +0000141
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142 StopReason GetStopReason() const override { return eStopReasonBreakpoint; }
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000143
Kate Stoneb9c1b512016-09-06 20:57:50 +0000144 bool ShouldStopSynchronous(Event *event_ptr) override {
145 ThreadSP thread_sp(m_thread_wp.lock());
146 if (thread_sp) {
147 if (!m_should_stop_is_valid) {
148 // Only check once if we should stop at a breakpoint
149 BreakpointSiteSP bp_site_sp(
150 thread_sp->GetProcess()->GetBreakpointSiteList().FindByID(m_value));
151 if (bp_site_sp) {
152 ExecutionContext exe_ctx(thread_sp->GetStackFrameAtIndex(0));
153 StoppointCallbackContext context(event_ptr, exe_ctx, true);
154 bp_site_sp->BumpHitCounts();
155 m_should_stop = bp_site_sp->ShouldStop(&context);
156 } else {
157 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
158
159 if (log)
160 log->Printf(
161 "Process::%s could not find breakpoint site id: %" PRId64 "...",
162 __FUNCTION__, m_value);
163
164 m_should_stop = true;
165 }
166 m_should_stop_is_valid = true;
167 }
168 return m_should_stop;
169 }
170 return false;
171 }
172
173 bool DoShouldNotify(Event *event_ptr) override {
174 ThreadSP thread_sp(m_thread_wp.lock());
175 if (thread_sp) {
176 BreakpointSiteSP bp_site_sp(
177 thread_sp->GetProcess()->GetBreakpointSiteList().FindByID(m_value));
178 if (bp_site_sp) {
179 bool all_internal = true;
180
181 for (uint32_t i = 0; i < bp_site_sp->GetNumberOfOwners(); i++) {
182 if (!bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint().IsInternal()) {
183 all_internal = false;
184 break;
185 }
186 }
187 return !all_internal;
188 }
189 }
190 return true;
191 }
192
193 const char *GetDescription() override {
194 if (m_description.empty()) {
195 ThreadSP thread_sp(m_thread_wp.lock());
196 if (thread_sp) {
197 BreakpointSiteSP bp_site_sp(
198 thread_sp->GetProcess()->GetBreakpointSiteList().FindByID(m_value));
199 if (bp_site_sp) {
200 StreamString strm;
201 // If we have just hit an internal breakpoint, and it has a kind
202 // description, print that instead of the
203 // full breakpoint printing:
204 if (bp_site_sp->IsInternal()) {
205 size_t num_owners = bp_site_sp->GetNumberOfOwners();
206 for (size_t idx = 0; idx < num_owners; idx++) {
207 const char *kind = bp_site_sp->GetOwnerAtIndex(idx)
208 ->GetBreakpoint()
209 .GetBreakpointKind();
210 if (kind != nullptr) {
211 m_description.assign(kind);
212 return kind;
213 }
Jim Inghamca36cd12012-10-05 19:16:31 +0000214 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000215 }
Jim Ingham36f3b362010-10-14 23:45:03 +0000216
Kate Stoneb9c1b512016-09-06 20:57:50 +0000217 strm.Printf("breakpoint ");
218 bp_site_sp->GetDescription(&strm, eDescriptionLevelBrief);
Zachary Turnerc1564272016-11-16 21:15:24 +0000219 m_description = strm.GetString();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000220 } else {
221 StreamString strm;
222 if (m_break_id != LLDB_INVALID_BREAK_ID) {
223 BreakpointSP break_sp =
224 thread_sp->GetProcess()->GetTarget().GetBreakpointByID(
225 m_break_id);
226 if (break_sp) {
227 if (break_sp->IsInternal()) {
228 const char *kind = break_sp->GetBreakpointKind();
229 if (kind)
230 strm.Printf("internal %s breakpoint(%d).", kind, m_break_id);
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000231 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000232 strm.Printf("internal breakpoint(%d).", m_break_id);
233 } else {
234 strm.Printf("breakpoint %d.", m_break_id);
235 }
236 } else {
237 if (m_was_one_shot)
238 strm.Printf("one-shot breakpoint %d", m_break_id);
239 else
240 strm.Printf("breakpoint %d which has been deleted.",
241 m_break_id);
Greg Claytonf4b47e12010-08-04 01:40:35 +0000242 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000243 } else if (m_address == LLDB_INVALID_ADDRESS)
244 strm.Printf("breakpoint site %" PRIi64
245 " which has been deleted - unknown address",
246 m_value);
247 else
248 strm.Printf("breakpoint site %" PRIi64
249 " which has been deleted - was at 0x%" PRIx64,
250 m_value, m_address);
251
Zachary Turnerc1564272016-11-16 21:15:24 +0000252 m_description = strm.GetString();
Greg Claytonf4b47e12010-08-04 01:40:35 +0000253 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000254 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000255 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000256 return m_description.c_str();
257 }
Jim Inghamf57b4352012-11-10 02:08:14 +0000258
259protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000260 bool ShouldStop(Event *event_ptr) override {
261 // This just reports the work done by PerformAction or the synchronous stop.
262 // It should
263 // only ever get called after they have had a chance to run.
264 assert(m_should_stop_is_valid);
265 return m_should_stop;
266 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000267
Kate Stoneb9c1b512016-09-06 20:57:50 +0000268 void PerformAction(Event *event_ptr) override {
269 if (!m_should_perform_action)
270 return;
271 m_should_perform_action = false;
Boris Ulasevich86aaa8a2017-02-15 11:42:47 +0000272 bool internal_breakpoint = true;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000273
Kate Stoneb9c1b512016-09-06 20:57:50 +0000274 ThreadSP thread_sp(m_thread_wp.lock());
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000275
Kate Stoneb9c1b512016-09-06 20:57:50 +0000276 if (thread_sp) {
277 Log *log = lldb_private::GetLogIfAnyCategoriesSet(
278 LIBLLDB_LOG_BREAKPOINTS | LIBLLDB_LOG_STEP);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000279
Kate Stoneb9c1b512016-09-06 20:57:50 +0000280 if (!thread_sp->IsValid()) {
281 // This shouldn't ever happen, but just in case, don't do more harm.
282 if (log) {
283 log->Printf("PerformAction got called with an invalid thread.");
284 }
285 m_should_stop = true;
286 m_should_stop_is_valid = true;
287 return;
288 }
289
290 BreakpointSiteSP bp_site_sp(
291 thread_sp->GetProcess()->GetBreakpointSiteList().FindByID(m_value));
292 std::unordered_set<break_id_t> precondition_breakpoints;
293
294 if (bp_site_sp) {
295 // Let's copy the owners list out of the site and store them in a local
296 // list. That way if
297 // one of the breakpoint actions changes the site, then we won't be
298 // operating on a bad list.
299 BreakpointLocationCollection site_locations;
300 size_t num_owners = bp_site_sp->CopyOwnersList(site_locations);
301
302 if (num_owners == 0) {
303 m_should_stop = true;
304 } else {
305 // We go through each location, and test first its precondition - this
306 // overrides everything. Note,
307 // we only do this once per breakpoint - not once per location...
308 // Then check the condition. If the condition says to stop,
309 // then we run the callback for that location. If that callback says
310 // to stop as well, then
311 // we set m_should_stop to true; we are going to stop.
312 // But we still want to give all the breakpoints whose conditions say
313 // we are going to stop a
314 // chance to run their callbacks.
315 // Of course if any callback restarts the target by putting "continue"
316 // in the callback, then
317 // we're going to restart, without running the rest of the callbacks.
318 // And in this case we will
319 // end up not stopping even if another location said we should stop.
320 // But that's better than not
321 // running all the callbacks.
322
323 m_should_stop = false;
324
325 // We don't select threads as we go through them testing breakpoint
326 // conditions and running commands.
327 // So we need to set the thread for expression evaluation here:
328 ThreadList::ExpressionExecutionThreadPusher thread_pusher(thread_sp);
329
330 ExecutionContext exe_ctx(thread_sp->GetStackFrameAtIndex(0));
331 Process *process = exe_ctx.GetProcessPtr();
332 if (process->GetModIDRef().IsLastResumeForUserExpression()) {
333 // If we are in the middle of evaluating an expression, don't run
334 // asynchronous breakpoint commands or
335 // expressions. That could lead to infinite recursion if the
336 // command or condition re-calls the function
337 // with this breakpoint.
338 // TODO: We can keep a list of the breakpoints we've seen while
339 // running expressions in the nested
340 // PerformAction calls that can arise when the action runs a
341 // function that hits another breakpoint,
342 // and only stop running commands when we see the same breakpoint
343 // hit a second time.
344
345 m_should_stop_is_valid = true;
346 if (log)
347 log->Printf("StopInfoBreakpoint::PerformAction - Hit a "
348 "breakpoint while running an expression,"
349 " not running commands to avoid recursion.");
350 bool ignoring_breakpoints =
351 process->GetIgnoreBreakpointsInExpressions();
352 if (ignoring_breakpoints) {
353 m_should_stop = false;
354 // Internal breakpoints will always stop.
355 for (size_t j = 0; j < num_owners; j++) {
356 lldb::BreakpointLocationSP bp_loc_sp =
357 bp_site_sp->GetOwnerAtIndex(j);
358 if (bp_loc_sp->GetBreakpoint().IsInternal()) {
359 m_should_stop = true;
360 break;
Jason Molenda3f032ff2013-08-06 23:08:59 +0000361 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000362 }
363 } else {
364 m_should_stop = true;
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000365 }
366 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000367 log->Printf("StopInfoBreakpoint::PerformAction - in expression, "
368 "continuing: %s.",
369 m_should_stop ? "true" : "false");
370 process->GetTarget().GetDebugger().GetAsyncOutputStream()->Printf(
371 "Warning: hit breakpoint while "
372 "running function, skipping commands and conditions to prevent "
373 "recursion.");
374 return;
375 }
376
377 StoppointCallbackContext context(event_ptr, exe_ctx, false);
378
379 // For safety's sake let's also grab an extra reference to the
380 // breakpoint owners of the locations we're
381 // going to examine, since the locations are going to have to get back
382 // to their breakpoints, and the
383 // locations don't keep their owners alive. I'm just sticking the
384 // BreakpointSP's in a vector since
385 // I'm only using it to locally increment their retain counts.
386
387 std::vector<lldb::BreakpointSP> location_owners;
388
389 for (size_t j = 0; j < num_owners; j++) {
390 BreakpointLocationSP loc(site_locations.GetByIndex(j));
391 location_owners.push_back(loc->GetBreakpoint().shared_from_this());
392 }
393
394 for (size_t j = 0; j < num_owners; j++) {
395 lldb::BreakpointLocationSP bp_loc_sp = site_locations.GetByIndex(j);
Jim Inghamf08f5c92017-08-03 18:13:24 +0000396 StreamString loc_desc;
397 if (log) {
398 bp_loc_sp->GetDescription(&loc_desc, eDescriptionLevelBrief);
399 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000400 // If another action disabled this breakpoint or its location, then
401 // don't run the actions.
402 if (!bp_loc_sp->IsEnabled() ||
403 !bp_loc_sp->GetBreakpoint().IsEnabled())
404 continue;
405
406 // The breakpoint site may have many locations associated with it,
407 // not all of them valid for
408 // this thread. Skip the ones that aren't:
409 if (!bp_loc_sp->ValidForThisThread(thread_sp.get())) {
410 if (log) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000411 log->Printf("Breakpoint %s hit on thread 0x%llx but it was not "
412 "for this thread, continuing.",
Jim Inghamf08f5c92017-08-03 18:13:24 +0000413 loc_desc.GetData(), static_cast<unsigned long long>(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000414 thread_sp->GetID()));
415 }
416 continue;
417 }
418
Jim Inghamf08f5c92017-08-03 18:13:24 +0000419 internal_breakpoint = bp_loc_sp->GetBreakpoint().IsInternal();
420
Kate Stoneb9c1b512016-09-06 20:57:50 +0000421 // First run the precondition, but since the precondition is per
422 // breakpoint, only run it once
423 // per breakpoint.
424 std::pair<std::unordered_set<break_id_t>::iterator, bool> result =
425 precondition_breakpoints.insert(
426 bp_loc_sp->GetBreakpoint().GetID());
427 if (!result.second)
428 continue;
429
430 bool precondition_result =
431 bp_loc_sp->GetBreakpoint().EvaluatePrecondition(context);
432 if (!precondition_result)
433 continue;
434
435 // Next run the condition for the breakpoint. If that says we
436 // should stop, then we'll run
437 // the callback for the breakpoint. If the callback says we
438 // shouldn't stop that will win.
439
440 if (bp_loc_sp->GetConditionText() != nullptr) {
Zachary Turner97206d52017-05-12 04:51:55 +0000441 Status condition_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000442 bool condition_says_stop =
443 bp_loc_sp->ConditionSaysStop(exe_ctx, condition_error);
444
445 if (!condition_error.Success()) {
446 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
447 StreamSP error_sp = debugger.GetAsyncErrorStream();
448 error_sp->Printf("Stopped due to an error evaluating condition "
449 "of breakpoint ");
450 bp_loc_sp->GetDescription(error_sp.get(),
451 eDescriptionLevelBrief);
452 error_sp->Printf(": \"%s\"", bp_loc_sp->GetConditionText());
453 error_sp->EOL();
454 const char *err_str =
455 condition_error.AsCString("<Unknown Error>");
456 if (log)
457 log->Printf("Error evaluating condition: \"%s\"\n", err_str);
458
459 error_sp->PutCString(err_str);
460 error_sp->EOL();
461 error_sp->Flush();
462 } else {
463 if (log) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000464 log->Printf("Condition evaluated for breakpoint %s on thread "
465 "0x%llx conditon_says_stop: %i.",
Jim Inghamf08f5c92017-08-03 18:13:24 +0000466 loc_desc.GetData(),
467 static_cast<unsigned long long>(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000468 thread_sp->GetID()),
469 condition_says_stop);
470 }
471 if (!condition_says_stop) {
472 // We don't want to increment the hit count of breakpoints if
473 // the condition fails.
474 // We've already bumped it by the time we get here, so undo
475 // the bump:
476 bp_loc_sp->UndoBumpHitCount();
477 continue;
478 }
479 }
480 }
481
Jim Inghamf08f5c92017-08-03 18:13:24 +0000482 // Check the auto-continue bit on the location, do this before the
483 // callback since it may change this, but that would be for the
484 // NEXT hit. Note, you might think you could check auto-continue
485 // before the condition, and not evaluate the condition if it says
486 // to continue. But failing the condition means the breakpoint was
487 // effectively NOT HIT. So these two states are different.
488 bool auto_continue_says_stop = true;
489 if (bp_loc_sp->IsAutoContinue())
490 {
491 if (log)
492 log->Printf("Continuing breakpoint %s as AutoContinue was set.",
493 loc_desc.GetData());
494 // We want this stop reported, so you will know we auto-continued
495 // but only for external breakpoints:
496 if (!internal_breakpoint)
497 thread_sp->SetShouldReportStop(eVoteYes);
498 auto_continue_says_stop = false;
499 }
500
501 bool callback_says_stop = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000502
503 // FIXME: For now the callbacks have to run in async mode - the
504 // first time we restart we need
505 // to get out of there. So set it here.
506 // When we figure out how to nest breakpoint hits then this will
507 // change.
508
509 Debugger &debugger = thread_sp->CalculateTarget()->GetDebugger();
510 bool old_async = debugger.GetAsyncExecution();
511 debugger.SetAsyncExecution(true);
512
513 callback_says_stop = bp_loc_sp->InvokeCallback(&context);
514
515 debugger.SetAsyncExecution(old_async);
516
Jim Inghamf08f5c92017-08-03 18:13:24 +0000517 if (callback_says_stop && auto_continue_says_stop)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000518 m_should_stop = true;
Boris Ulasevich86aaa8a2017-02-15 11:42:47 +0000519
Kate Stoneb9c1b512016-09-06 20:57:50 +0000520 // If we are going to stop for this breakpoint, then remove the
521 // breakpoint.
522 if (callback_says_stop && bp_loc_sp &&
523 bp_loc_sp->GetBreakpoint().IsOneShot()) {
524 thread_sp->GetProcess()->GetTarget().RemoveBreakpointByID(
525 bp_loc_sp->GetBreakpoint().GetID());
526 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000527 // Also make sure that the callback hasn't continued the target.
528 // If it did, when we'll set m_should_start to false and get out of
529 // here.
530 if (HasTargetRunSinceMe()) {
531 m_should_stop = false;
532 break;
533 }
534 }
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000535 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000536 // We've figured out what this stop wants to do, so mark it as valid so
537 // we don't compute it again.
538 m_should_stop_is_valid = true;
539 } else {
540 m_should_stop = true;
541 m_should_stop_is_valid = true;
542 Log *log_process(
543 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
544
545 if (log_process)
546 log_process->Printf(
547 "Process::%s could not find breakpoint site id: %" PRId64 "...",
548 __FUNCTION__, m_value);
549 }
Boris Ulasevich86aaa8a2017-02-15 11:42:47 +0000550
551 if ((m_should_stop == false || internal_breakpoint)
552 && thread_sp->CompletedPlanOverridesBreakpoint()) {
553
554 // Override should_stop decision when we have
555 // completed step plan additionally to the breakpoint
556 m_should_stop = true;
557
558 // Here we clean the preset stop info so the next
559 // GetStopInfo call will find the appropriate stop info,
560 // which should be the stop info related to the completed plan
561 thread_sp->ResetStopInfo();
562 }
563
Kate Stoneb9c1b512016-09-06 20:57:50 +0000564 if (log)
565 log->Printf("Process::%s returning from action with m_should_stop: %d.",
566 __FUNCTION__, m_should_stop);
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000567 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000568 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000569
570private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000571 bool m_should_stop;
572 bool m_should_stop_is_valid;
573 bool m_should_perform_action; // Since we are trying to preserve the "state"
574 // of the system even if we run functions
575 // etc. behind the users backs, we need to make sure we only REALLY perform
576 // the action once.
577 lldb::addr_t m_address; // We use this to capture the breakpoint site address
578 // when we create the StopInfo,
579 // in case somebody deletes it between the time the StopInfo is made and the
580 // description is asked for.
581 lldb::break_id_t m_break_id;
582 bool m_was_one_shot;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000583};
584
Greg Claytonf4b47e12010-08-04 01:40:35 +0000585//----------------------------------------------------------------------
586// StopInfoWatchpoint
587//----------------------------------------------------------------------
588
Kate Stoneb9c1b512016-09-06 20:57:50 +0000589class StopInfoWatchpoint : public StopInfo {
Greg Claytonf4b47e12010-08-04 01:40:35 +0000590public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000591 // Make sure watchpoint is properly disabled and subsequently enabled while
592 // performing watchpoint actions.
593 class WatchpointSentry {
594 public:
Jim Ingham209a77d2016-10-25 20:34:32 +0000595 WatchpointSentry(ProcessSP p_sp, WatchpointSP w_sp) : process_sp(p_sp),
596 watchpoint_sp(w_sp) {
597 if (process_sp && watchpoint_sp) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000598 const bool notify = false;
Jim Ingham209a77d2016-10-25 20:34:32 +0000599 watchpoint_sp->TurnOnEphemeralMode();
600 process_sp->DisableWatchpoint(watchpoint_sp.get(), notify);
601 process_sp->AddPreResumeAction(SentryPreResumeAction, this);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000602 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000603 }
Jim Ingham209a77d2016-10-25 20:34:32 +0000604
605 void DoReenable() {
606 if (process_sp && watchpoint_sp) {
607 bool was_disabled = watchpoint_sp->IsDisabledDuringEphemeralMode();
608 watchpoint_sp->TurnOffEphemeralMode();
609 const bool notify = false;
610 if (was_disabled) {
611 process_sp->DisableWatchpoint(watchpoint_sp.get(), notify);
612 } else {
613 process_sp->EnableWatchpoint(watchpoint_sp.get(), notify);
Pavel Labath2e8fe802016-10-21 10:52:11 +0000614 }
Pavel Labath2e8fe802016-10-21 10:52:11 +0000615 }
Jim Ingham94bd5752016-10-21 00:06:38 +0000616 }
Jim Ingham209a77d2016-10-25 20:34:32 +0000617
618 ~WatchpointSentry() {
619 DoReenable();
620 if (process_sp)
621 process_sp->ClearPreResumeAction(SentryPreResumeAction, this);
622 }
623
624 static bool SentryPreResumeAction(void *sentry_void) {
625 WatchpointSentry *sentry = (WatchpointSentry *) sentry_void;
626 sentry->DoReenable();
627 return true;
628 }
Jim Inghamf57b4352012-11-10 02:08:14 +0000629
Kate Stoneb9c1b512016-09-06 20:57:50 +0000630 private:
Jim Ingham209a77d2016-10-25 20:34:32 +0000631 ProcessSP process_sp;
632 WatchpointSP watchpoint_sp;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000633 };
634
635 StopInfoWatchpoint(Thread &thread, break_id_t watch_id,
636 lldb::addr_t watch_hit_addr)
637 : StopInfo(thread, watch_id), m_should_stop(false),
638 m_should_stop_is_valid(false), m_watch_hit_addr(watch_hit_addr) {}
639
640 ~StopInfoWatchpoint() override = default;
641
642 StopReason GetStopReason() const override { return eStopReasonWatchpoint; }
643
644 const char *GetDescription() override {
645 if (m_description.empty()) {
646 StreamString strm;
647 strm.Printf("watchpoint %" PRIi64, m_value);
Zachary Turnerc1564272016-11-16 21:15:24 +0000648 m_description = strm.GetString();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000649 }
650 return m_description.c_str();
651 }
652
Jim Inghamf57b4352012-11-10 02:08:14 +0000653protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000654 bool ShouldStopSynchronous(Event *event_ptr) override {
655 // ShouldStop() method is idempotent and should not affect hit count.
656 // See Process::RunPrivateStateThread()->Process()->HandlePrivateEvent()
657 // -->Process()::ShouldBroadcastEvent()->ThreadList::ShouldStop()->
658 // Thread::ShouldStop()->ThreadPlanBase::ShouldStop()->
659 // StopInfoWatchpoint::ShouldStop() and
660 // Event::DoOnRemoval()->Process::ProcessEventData::DoOnRemoval()->
661 // StopInfoWatchpoint::PerformAction().
662 if (m_should_stop_is_valid)
663 return m_should_stop;
Johnny Chenfd158f42011-09-21 22:47:15 +0000664
Kate Stoneb9c1b512016-09-06 20:57:50 +0000665 ThreadSP thread_sp(m_thread_wp.lock());
666 if (thread_sp) {
667 WatchpointSP wp_sp(
668 thread_sp->CalculateTarget()->GetWatchpointList().FindByID(
669 GetValue()));
670 if (wp_sp) {
671 // Check if we should stop at a watchpoint.
672 ExecutionContext exe_ctx(thread_sp->GetStackFrameAtIndex(0));
673 StoppointCallbackContext context(event_ptr, exe_ctx, true);
674 m_should_stop = wp_sp->ShouldStop(&context);
675 } else {
676 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
Johnny Chenfd158f42011-09-21 22:47:15 +0000677
Kate Stoneb9c1b512016-09-06 20:57:50 +0000678 if (log)
679 log->Printf(
680 "Process::%s could not find watchpoint location id: %" PRId64
681 "...",
682 __FUNCTION__, GetValue());
Johnny Chenfd158f42011-09-21 22:47:15 +0000683
Johnny Chen16dcf712011-10-17 18:58:00 +0000684 m_should_stop = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000685 }
Johnny Chen16dcf712011-10-17 18:58:00 +0000686 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000687 m_should_stop_is_valid = true;
688 return m_should_stop;
689 }
690
691 bool ShouldStop(Event *event_ptr) override {
692 // This just reports the work done by PerformAction or the synchronous stop.
693 // It should
694 // only ever get called after they have had a chance to run.
695 assert(m_should_stop_is_valid);
696 return m_should_stop;
697 }
698
699 void PerformAction(Event *event_ptr) override {
700 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS);
701 // We're going to calculate if we should stop or not in some way during the
702 // course of
703 // this code. Also by default we're going to stop, so set that here.
704 m_should_stop = true;
Jim Ingham209a77d2016-10-25 20:34:32 +0000705
Kate Stoneb9c1b512016-09-06 20:57:50 +0000706
707 ThreadSP thread_sp(m_thread_wp.lock());
708 if (thread_sp) {
709
710 WatchpointSP wp_sp(
711 thread_sp->CalculateTarget()->GetWatchpointList().FindByID(
Jim Ingham209a77d2016-10-25 20:34:32 +0000712 GetValue()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000713 if (wp_sp) {
714 ExecutionContext exe_ctx(thread_sp->GetStackFrameAtIndex(0));
Jim Ingham209a77d2016-10-25 20:34:32 +0000715 ProcessSP process_sp = exe_ctx.GetProcessSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000716
717 {
718 // check if this process is running on an architecture where
719 // watchpoints trigger
720 // before the associated instruction runs. if so, disable the WP,
721 // single-step and then
722 // re-enable the watchpoint
Jim Ingham209a77d2016-10-25 20:34:32 +0000723 if (process_sp) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000724 uint32_t num;
725 bool wp_triggers_after;
Jim Ingham209a77d2016-10-25 20:34:32 +0000726
727 if (process_sp->GetWatchpointSupportInfo(num, wp_triggers_after)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000728 .Success()) {
729 if (!wp_triggers_after) {
Nitesh Jain0546e842016-12-09 13:54:47 +0000730 // We need to preserve the watch_index before watchpoint
731 // is disable. Since Watchpoint::SetEnabled will clear the
732 // watch index.
733 // This will fix TestWatchpointIter failure
734 Watchpoint *wp = wp_sp.get();
735 uint32_t watch_index = wp->GetHardwareIndex();
736 process_sp->DisableWatchpoint(wp, false);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000737 StopInfoSP stored_stop_info_sp = thread_sp->GetStopInfo();
738 assert(stored_stop_info_sp.get() == this);
739
740 ThreadPlanSP new_plan_sp(
741 thread_sp->QueueThreadPlanForStepSingleInstruction(
742 false, // step-over
743 false, // abort_other_plans
744 true)); // stop_other_threads
745 new_plan_sp->SetIsMasterPlan(true);
746 new_plan_sp->SetOkayToDiscard(false);
747 new_plan_sp->SetPrivate(true);
Jim Ingham209a77d2016-10-25 20:34:32 +0000748 process_sp->GetThreadList().SetSelectedThreadByID(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000749 thread_sp->GetID());
Jim Ingham209a77d2016-10-25 20:34:32 +0000750 process_sp->ResumeSynchronous(nullptr);
751 process_sp->GetThreadList().SetSelectedThreadByID(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000752 thread_sp->GetID());
753 thread_sp->SetStopInfo(stored_stop_info_sp);
Nitesh Jain0546e842016-12-09 13:54:47 +0000754 process_sp->EnableWatchpoint(wp, false);
755 wp->SetHardwareIndex(watch_index);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000756 }
757 }
758 }
759 }
760
Jim Ingham209a77d2016-10-25 20:34:32 +0000761 // This sentry object makes sure the current watchpoint is disabled
762 // while performing watchpoint actions,
763 // and it is then enabled after we are finished.
764 WatchpointSentry sentry(process_sp, wp_sp);
765
Kate Stoneb9c1b512016-09-06 20:57:50 +0000766 /*
767 * MIPS: Last 3bits of the watchpoint address are masked by the kernel.
768 * For example:
769 * 'n' is at 0x120010d00 and 'm' is 0x120010d04. When a watchpoint is
770 * set at 'm', then
771 * watch exception is generated even when 'n' is read/written. To handle
772 * this case,
773 * server emulates the instruction at PC and finds the base address of
774 * the load/store
775 * instruction and appends it in the description of the stop-info
776 * packet. If watchpoint
777 * is not set on this address by user then this do not stop.
778 */
779 if (m_watch_hit_addr != LLDB_INVALID_ADDRESS) {
780 WatchpointSP wp_hit_sp =
781 thread_sp->CalculateTarget()->GetWatchpointList().FindByAddress(
782 m_watch_hit_addr);
783 if (!wp_hit_sp) {
784 m_should_stop = false;
785 wp_sp->IncrementFalseAlarmsAndReviseHitCount();
786 }
787 }
788
789 // TODO: This condition should be checked in the synchronous part of the
790 // watchpoint code
791 // (Watchpoint::ShouldStop), so that we avoid pulling an event even if
792 // the watchpoint fails
793 // the ignore count condition. It is moved here temporarily, because for
794 // archs with
795 // watchpoint_exceptions_received=before, the code in the previous lines
796 // takes care of moving
797 // the inferior to next PC. We have to check the ignore count condition
798 // after this is done,
799 // otherwise we will hit same watchpoint multiple times until we pass
800 // ignore condition, but we
801 // won't actually be ignoring them.
802 if (wp_sp->GetHitCount() <= wp_sp->GetIgnoreCount())
803 m_should_stop = false;
804
Jim Ingham209a77d2016-10-25 20:34:32 +0000805 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
806
Kate Stoneb9c1b512016-09-06 20:57:50 +0000807 if (m_should_stop && wp_sp->GetConditionText() != nullptr) {
808 // We need to make sure the user sees any parse errors in their
Zachary Turnerc5d7df92016-11-08 04:52:16 +0000809 // condition, so we'll hook the constructor errors up to the
810 // debugger's Async I/O.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000811 ExpressionResults result_code;
812 EvaluateExpressionOptions expr_options;
813 expr_options.SetUnwindOnError(true);
814 expr_options.SetIgnoreBreakpoints(true);
815 ValueObjectSP result_value_sp;
Zachary Turner97206d52017-05-12 04:51:55 +0000816 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000817 result_code = UserExpression::Evaluate(
Zachary Turnerc5d7df92016-11-08 04:52:16 +0000818 exe_ctx, expr_options, wp_sp->GetConditionText(),
819 llvm::StringRef(), result_value_sp, error);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000820
821 if (result_code == eExpressionCompleted) {
822 if (result_value_sp) {
823 Scalar scalar_value;
824 if (result_value_sp->ResolveValue(scalar_value)) {
825 if (scalar_value.ULongLong(1) == 0) {
826 // We have been vetoed. This takes precedence over querying
827 // the watchpoint whether it should stop (aka ignore count and
828 // friends). See also StopInfoWatchpoint::ShouldStop() as
Zachary Turnerc5d7df92016-11-08 04:52:16 +0000829 // well as Process::ProcessEventData::DoOnRemoval().
Kate Stoneb9c1b512016-09-06 20:57:50 +0000830 m_should_stop = false;
831 } else
832 m_should_stop = true;
833 if (log)
834 log->Printf(
835 "Condition successfully evaluated, result is %s.\n",
836 m_should_stop ? "true" : "false");
837 } else {
838 m_should_stop = true;
839 if (log)
840 log->Printf(
841 "Failed to get an integer result from the expression.");
842 }
843 }
844 } else {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000845 StreamSP error_sp = debugger.GetAsyncErrorStream();
846 error_sp->Printf(
847 "Stopped due to an error evaluating condition of watchpoint ");
848 wp_sp->GetDescription(error_sp.get(), eDescriptionLevelBrief);
849 error_sp->Printf(": \"%s\"", wp_sp->GetConditionText());
850 error_sp->EOL();
851 const char *err_str = error.AsCString("<Unknown Error>");
852 if (log)
853 log->Printf("Error evaluating condition: \"%s\"\n", err_str);
854
855 error_sp->PutCString(err_str);
856 error_sp->EOL();
857 error_sp->Flush();
858 // If the condition fails to be parsed or run, we should stop.
859 m_should_stop = true;
860 }
861 }
862
863 // If the condition says to stop, we run the callback to further decide
864 // whether to stop.
865 if (m_should_stop) {
Jim Ingham209a77d2016-10-25 20:34:32 +0000866 // FIXME: For now the callbacks have to run in async mode - the
867 // first time we restart we need
868 // to get out of there. So set it here.
869 // When we figure out how to nest watchpoint hits then this will
870 // change.
871
872 bool old_async = debugger.GetAsyncExecution();
873 debugger.SetAsyncExecution(true);
874
Kate Stoneb9c1b512016-09-06 20:57:50 +0000875 StoppointCallbackContext context(event_ptr, exe_ctx, false);
876 bool stop_requested = wp_sp->InvokeCallback(&context);
Jim Ingham209a77d2016-10-25 20:34:32 +0000877
878 debugger.SetAsyncExecution(old_async);
879
Kate Stoneb9c1b512016-09-06 20:57:50 +0000880 // Also make sure that the callback hasn't continued the target.
881 // If it did, when we'll set m_should_stop to false and get out of
882 // here.
883 if (HasTargetRunSinceMe())
884 m_should_stop = false;
885
886 if (m_should_stop && !stop_requested) {
887 // We have been vetoed by the callback mechanism.
888 m_should_stop = false;
889 }
890 }
891 // Finally, if we are going to stop, print out the new & old values:
892 if (m_should_stop) {
893 wp_sp->CaptureWatchedValue(exe_ctx);
894
895 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
896 StreamSP output_sp = debugger.GetAsyncOutputStream();
897 wp_sp->DumpSnapshots(output_sp.get());
898 output_sp->EOL();
899 output_sp->Flush();
900 }
901
902 } else {
903 Log *log_process(
904 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
905
906 if (log_process)
907 log_process->Printf(
908 "Process::%s could not find watchpoint id: %" PRId64 "...",
909 __FUNCTION__, m_value);
910 }
911 if (log)
912 log->Printf("Process::%s returning from action with m_should_stop: %d.",
913 __FUNCTION__, m_should_stop);
914
915 m_should_stop_is_valid = true;
916 }
917 }
918
Greg Claytonf4b47e12010-08-04 01:40:35 +0000919private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000920 bool m_should_stop;
921 bool m_should_stop_is_valid;
922 lldb::addr_t m_watch_hit_addr;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000923};
924
Greg Claytonf4b47e12010-08-04 01:40:35 +0000925//----------------------------------------------------------------------
926// StopInfoUnixSignal
927//----------------------------------------------------------------------
928
Kate Stoneb9c1b512016-09-06 20:57:50 +0000929class StopInfoUnixSignal : public StopInfo {
Greg Claytonf4b47e12010-08-04 01:40:35 +0000930public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000931 StopInfoUnixSignal(Thread &thread, int signo, const char *description)
932 : StopInfo(thread, signo) {
933 SetDescription(description);
934 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000935
Kate Stoneb9c1b512016-09-06 20:57:50 +0000936 ~StopInfoUnixSignal() override = default;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000937
Kate Stoneb9c1b512016-09-06 20:57:50 +0000938 StopReason GetStopReason() const override { return eStopReasonSignal; }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000939
Kate Stoneb9c1b512016-09-06 20:57:50 +0000940 bool ShouldStopSynchronous(Event *event_ptr) override {
941 ThreadSP thread_sp(m_thread_wp.lock());
942 if (thread_sp)
943 return thread_sp->GetProcess()->GetUnixSignals()->GetShouldStop(m_value);
944 return false;
945 }
Jim Ingham0161b492013-02-09 01:29:05 +0000946
Kate Stoneb9c1b512016-09-06 20:57:50 +0000947 bool ShouldStop(Event *event_ptr) override {
948 ThreadSP thread_sp(m_thread_wp.lock());
949 if (thread_sp)
950 return thread_sp->GetProcess()->GetUnixSignals()->GetShouldStop(m_value);
951 return false;
952 }
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000953
Kate Stoneb9c1b512016-09-06 20:57:50 +0000954 // If should stop returns false, check if we should notify of this event
955 bool DoShouldNotify(Event *event_ptr) override {
956 ThreadSP thread_sp(m_thread_wp.lock());
957 if (thread_sp) {
958 bool should_notify =
959 thread_sp->GetProcess()->GetUnixSignals()->GetShouldNotify(m_value);
960 if (should_notify) {
961 StreamString strm;
962 strm.Printf(
963 "thread %d received signal: %s", thread_sp->GetIndexID(),
964 thread_sp->GetProcess()->GetUnixSignals()->GetSignalAsCString(
965 m_value));
966 Process::ProcessEventData::AddRestartedReason(event_ptr,
967 strm.GetData());
968 }
969 return should_notify;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000970 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000971 return true;
972 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000973
Kate Stoneb9c1b512016-09-06 20:57:50 +0000974 void WillResume(lldb::StateType resume_state) override {
975 ThreadSP thread_sp(m_thread_wp.lock());
976 if (thread_sp) {
977 if (!thread_sp->GetProcess()->GetUnixSignals()->GetShouldSuppress(
978 m_value))
979 thread_sp->SetResumeSignal(m_value);
Greg Claytonf4b47e12010-08-04 01:40:35 +0000980 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000981 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000982
Kate Stoneb9c1b512016-09-06 20:57:50 +0000983 const char *GetDescription() override {
984 if (m_description.empty()) {
985 ThreadSP thread_sp(m_thread_wp.lock());
986 if (thread_sp) {
987 StreamString strm;
988 const char *signal_name =
989 thread_sp->GetProcess()->GetUnixSignals()->GetSignalAsCString(
990 m_value);
991 if (signal_name)
992 strm.Printf("signal %s", signal_name);
993 else
994 strm.Printf("signal %" PRIi64, m_value);
Zachary Turnerc1564272016-11-16 21:15:24 +0000995 m_description = strm.GetString();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000996 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000997 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000998 return m_description.c_str();
999 }
Greg Claytonf4b47e12010-08-04 01:40:35 +00001000};
1001
1002//----------------------------------------------------------------------
1003// StopInfoTrace
1004//----------------------------------------------------------------------
1005
Kate Stoneb9c1b512016-09-06 20:57:50 +00001006class StopInfoTrace : public StopInfo {
Greg Claytonf4b47e12010-08-04 01:40:35 +00001007public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001008 StopInfoTrace(Thread &thread) : StopInfo(thread, LLDB_INVALID_UID) {}
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001009
Kate Stoneb9c1b512016-09-06 20:57:50 +00001010 ~StopInfoTrace() override = default;
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001011
Kate Stoneb9c1b512016-09-06 20:57:50 +00001012 StopReason GetStopReason() const override { return eStopReasonTrace; }
Greg Claytonf4b47e12010-08-04 01:40:35 +00001013
Kate Stoneb9c1b512016-09-06 20:57:50 +00001014 const char *GetDescription() override {
1015 if (m_description.empty())
1016 return "trace";
1017 else
1018 return m_description.c_str();
1019 }
Greg Claytona658fd22011-06-04 01:26:29 +00001020};
1021
Greg Claytona658fd22011-06-04 01:26:29 +00001022//----------------------------------------------------------------------
1023// StopInfoException
1024//----------------------------------------------------------------------
1025
Kate Stoneb9c1b512016-09-06 20:57:50 +00001026class StopInfoException : public StopInfo {
Greg Claytona658fd22011-06-04 01:26:29 +00001027public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001028 StopInfoException(Thread &thread, const char *description)
1029 : StopInfo(thread, LLDB_INVALID_UID) {
1030 if (description)
1031 SetDescription(description);
1032 }
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001033
Kate Stoneb9c1b512016-09-06 20:57:50 +00001034 ~StopInfoException() override = default;
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001035
Kate Stoneb9c1b512016-09-06 20:57:50 +00001036 StopReason GetStopReason() const override { return eStopReasonException; }
1037
1038 const char *GetDescription() override {
1039 if (m_description.empty())
1040 return "exception";
1041 else
1042 return m_description.c_str();
1043 }
Greg Claytonf4b47e12010-08-04 01:40:35 +00001044};
1045
Greg Claytonf4b47e12010-08-04 01:40:35 +00001046//----------------------------------------------------------------------
1047// StopInfoThreadPlan
1048//----------------------------------------------------------------------
1049
Kate Stoneb9c1b512016-09-06 20:57:50 +00001050class StopInfoThreadPlan : public StopInfo {
Greg Claytonf4b47e12010-08-04 01:40:35 +00001051public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001052 StopInfoThreadPlan(ThreadPlanSP &plan_sp, ValueObjectSP &return_valobj_sp,
1053 ExpressionVariableSP &expression_variable_sp)
1054 : StopInfo(plan_sp->GetThread(), LLDB_INVALID_UID), m_plan_sp(plan_sp),
1055 m_return_valobj_sp(return_valobj_sp),
1056 m_expression_variable_sp(expression_variable_sp) {}
Greg Claytonf4b47e12010-08-04 01:40:35 +00001057
Kate Stoneb9c1b512016-09-06 20:57:50 +00001058 ~StopInfoThreadPlan() override = default;
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001059
Kate Stoneb9c1b512016-09-06 20:57:50 +00001060 StopReason GetStopReason() const override { return eStopReasonPlanComplete; }
Greg Claytonf4b47e12010-08-04 01:40:35 +00001061
Kate Stoneb9c1b512016-09-06 20:57:50 +00001062 const char *GetDescription() override {
1063 if (m_description.empty()) {
1064 StreamString strm;
1065 m_plan_sp->GetDescription(&strm, eDescriptionLevelBrief);
Zachary Turnerc1564272016-11-16 21:15:24 +00001066 m_description = strm.GetString();
Greg Claytonf4b47e12010-08-04 01:40:35 +00001067 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001068 return m_description.c_str();
1069 }
1070
1071 ValueObjectSP GetReturnValueObject() { return m_return_valobj_sp; }
1072
1073 ExpressionVariableSP GetExpressionVariable() {
1074 return m_expression_variable_sp;
1075 }
1076
Jim Ingham0161b492013-02-09 01:29:05 +00001077protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001078 bool ShouldStop(Event *event_ptr) override {
1079 if (m_plan_sp)
1080 return m_plan_sp->ShouldStop(event_ptr);
1081 else
1082 return StopInfo::ShouldStop(event_ptr);
1083 }
Greg Claytonf4b47e12010-08-04 01:40:35 +00001084
1085private:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001086 ThreadPlanSP m_plan_sp;
1087 ValueObjectSP m_return_valobj_sp;
1088 ExpressionVariableSP m_expression_variable_sp;
Greg Claytonf4b47e12010-08-04 01:40:35 +00001089};
Kate Stoneb9c1b512016-09-06 20:57:50 +00001090
Jim Inghamba205c12017-12-05 02:50:45 +00001091//----------------------------------------------------------------------
1092// StopInfoExec
1093//----------------------------------------------------------------------
1094
Kate Stoneb9c1b512016-09-06 20:57:50 +00001095class StopInfoExec : public StopInfo {
Greg Clayton90ba8112012-12-05 00:16:59 +00001096public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001097 StopInfoExec(Thread &thread)
1098 : StopInfo(thread, LLDB_INVALID_UID), m_performed_action(false) {}
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001099
Kate Stoneb9c1b512016-09-06 20:57:50 +00001100 ~StopInfoExec() override = default;
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001101
Jim Inghamba205c12017-12-05 02:50:45 +00001102 bool ShouldStop(Event *event_ptr) override {
1103 ThreadSP thread_sp(m_thread_wp.lock());
1104 if (thread_sp)
1105 return thread_sp->GetProcess()->GetStopOnExec();
1106 return false;
1107 }
1108
Kate Stoneb9c1b512016-09-06 20:57:50 +00001109 StopReason GetStopReason() const override { return eStopReasonExec; }
1110
1111 const char *GetDescription() override { return "exec"; }
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001112
Greg Clayton90ba8112012-12-05 00:16:59 +00001113protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001114 void PerformAction(Event *event_ptr) override {
1115 // Only perform the action once
1116 if (m_performed_action)
1117 return;
1118 m_performed_action = true;
1119 ThreadSP thread_sp(m_thread_wp.lock());
1120 if (thread_sp)
1121 thread_sp->GetProcess()->DidExec();
1122 }
1123
1124 bool m_performed_action;
Greg Clayton90ba8112012-12-05 00:16:59 +00001125};
1126
Jim Ingham4b536182011-08-09 02:12:22 +00001127} // namespace lldb_private
Greg Claytonf4b47e12010-08-04 01:40:35 +00001128
Kate Stoneb9c1b512016-09-06 20:57:50 +00001129StopInfoSP StopInfo::CreateStopReasonWithBreakpointSiteID(Thread &thread,
1130 break_id_t break_id) {
1131 return StopInfoSP(new StopInfoBreakpoint(thread, break_id));
1132}
1133
1134StopInfoSP StopInfo::CreateStopReasonWithBreakpointSiteID(Thread &thread,
1135 break_id_t break_id,
1136 bool should_stop) {
1137 return StopInfoSP(new StopInfoBreakpoint(thread, break_id, should_stop));
Greg Claytonf4b47e12010-08-04 01:40:35 +00001138}
1139
1140StopInfoSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001141StopInfo::CreateStopReasonWithWatchpointID(Thread &thread, break_id_t watch_id,
1142 lldb::addr_t watch_hit_addr) {
1143 return StopInfoSP(new StopInfoWatchpoint(thread, watch_id, watch_hit_addr));
Jim Ingham36f3b362010-10-14 23:45:03 +00001144}
1145
Kate Stoneb9c1b512016-09-06 20:57:50 +00001146StopInfoSP StopInfo::CreateStopReasonWithSignal(Thread &thread, int signo,
1147 const char *description) {
1148 return StopInfoSP(new StopInfoUnixSignal(thread, signo, description));
Greg Claytonf4b47e12010-08-04 01:40:35 +00001149}
1150
Kate Stoneb9c1b512016-09-06 20:57:50 +00001151StopInfoSP StopInfo::CreateStopReasonToTrace(Thread &thread) {
1152 return StopInfoSP(new StopInfoTrace(thread));
Greg Claytonf4b47e12010-08-04 01:40:35 +00001153}
1154
Kate Stoneb9c1b512016-09-06 20:57:50 +00001155StopInfoSP StopInfo::CreateStopReasonWithPlan(
1156 ThreadPlanSP &plan_sp, ValueObjectSP return_valobj_sp,
1157 ExpressionVariableSP expression_variable_sp) {
1158 return StopInfoSP(new StopInfoThreadPlan(plan_sp, return_valobj_sp,
1159 expression_variable_sp));
Greg Claytonf4b47e12010-08-04 01:40:35 +00001160}
1161
Kate Stoneb9c1b512016-09-06 20:57:50 +00001162StopInfoSP StopInfo::CreateStopReasonWithException(Thread &thread,
1163 const char *description) {
1164 return StopInfoSP(new StopInfoException(thread, description));
Greg Claytonf4b47e12010-08-04 01:40:35 +00001165}
Greg Claytona658fd22011-06-04 01:26:29 +00001166
Kate Stoneb9c1b512016-09-06 20:57:50 +00001167StopInfoSP StopInfo::CreateStopReasonWithExec(Thread &thread) {
1168 return StopInfoSP(new StopInfoExec(thread));
Greg Claytona658fd22011-06-04 01:26:29 +00001169}
Jim Ingham73ca05a2011-12-17 01:35:57 +00001170
Kate Stoneb9c1b512016-09-06 20:57:50 +00001171ValueObjectSP StopInfo::GetReturnValueObject(StopInfoSP &stop_info_sp) {
1172 if (stop_info_sp &&
1173 stop_info_sp->GetStopReason() == eStopReasonPlanComplete) {
1174 StopInfoThreadPlan *plan_stop_info =
1175 static_cast<StopInfoThreadPlan *>(stop_info_sp.get());
1176 return plan_stop_info->GetReturnValueObject();
1177 } else
1178 return ValueObjectSP();
Greg Clayton90ba8112012-12-05 00:16:59 +00001179}
1180
Kate Stoneb9c1b512016-09-06 20:57:50 +00001181ExpressionVariableSP StopInfo::GetExpressionVariable(StopInfoSP &stop_info_sp) {
1182 if (stop_info_sp &&
1183 stop_info_sp->GetStopReason() == eStopReasonPlanComplete) {
1184 StopInfoThreadPlan *plan_stop_info =
1185 static_cast<StopInfoThreadPlan *>(stop_info_sp.get());
1186 return plan_stop_info->GetExpressionVariable();
1187 } else
1188 return ExpressionVariableSP();
Jim Ingham30fadaf2014-07-08 01:07:32 +00001189}
Sean Callanan4740a732016-09-06 04:48:36 +00001190
1191lldb::ValueObjectSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001192StopInfo::GetCrashingDereference(StopInfoSP &stop_info_sp,
1193 lldb::addr_t *crashing_address) {
1194 if (!stop_info_sp) {
1195 return ValueObjectSP();
1196 }
Sean Callanan4740a732016-09-06 04:48:36 +00001197
Kate Stoneb9c1b512016-09-06 20:57:50 +00001198 const char *description = stop_info_sp->GetDescription();
1199 if (!description) {
1200 return ValueObjectSP();
1201 }
1202
1203 ThreadSP thread_sp = stop_info_sp->GetThread();
1204 if (!thread_sp) {
1205 return ValueObjectSP();
1206 }
1207
1208 StackFrameSP frame_sp = thread_sp->GetSelectedFrame();
1209
1210 if (!frame_sp) {
1211 return ValueObjectSP();
1212 }
1213
1214 const char address_string[] = "address=";
1215
1216 const char *address_loc = strstr(description, address_string);
1217 if (!address_loc) {
1218 return ValueObjectSP();
1219 }
1220
1221 address_loc += (sizeof(address_string) - 1);
1222
1223 uint64_t address = strtoull(address_loc, 0, 0);
1224 if (crashing_address) {
1225 *crashing_address = address;
1226 }
1227
1228 return frame_sp->GuessValueForAddress(address);
Sean Callanan4740a732016-09-06 04:48:36 +00001229}