1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
|
//===-- Watchpoint.cpp ----------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "lldb/Breakpoint/Watchpoint.h"
#include "lldb/Breakpoint/StoppointCallbackContext.h"
#include "lldb/Breakpoint/WatchpointResource.h"
#include "lldb/Core/Debugger.h"
#include "lldb/Core/Value.h"
#include "lldb/DataFormatters/DumpValueObjectOptions.h"
#include "lldb/Expression/UserExpression.h"
#include "lldb/Symbol/TypeSystem.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/Target.h"
#include "lldb/Target/ThreadSpec.h"
#include "lldb/Utility/LLDBLog.h"
#include "lldb/Utility/Log.h"
#include "lldb/Utility/Stream.h"
#include "lldb/ValueObject/ValueObject.h"
#include "lldb/ValueObject/ValueObjectMemory.h"
using namespace lldb;
using namespace lldb_private;
static bool IsValueObjectsEqual(lldb::ValueObjectSP lhs,
lldb::ValueObjectSP rhs) {
lldbassert(lhs);
lldbassert(rhs);
Status error;
DataExtractor lhs_data;
lhs->GetData(lhs_data, error);
if (error.Fail())
return false;
DataExtractor rhs_data;
rhs->GetData(rhs_data, error);
if (error.Fail())
return false;
return llvm::equal(
llvm::iterator_range(lhs_data.GetDataStart(), lhs_data.GetDataEnd()),
llvm::iterator_range(rhs_data.GetDataStart(), rhs_data.GetDataEnd()));
}
static CompilerType DeriveCompilerType(Target &target, uint32_t size) {
auto type_system_or_err =
target.GetScratchTypeSystemForLanguage(eLanguageTypeC);
auto err = type_system_or_err.takeError();
if (err) {
LLDB_LOG_ERROR(GetLog(LLDBLog::Watchpoints), std::move(err),
"Failed to set type: {0}");
return CompilerType();
}
auto ts = *type_system_or_err;
if (!ts) {
LLDB_LOG_ERROR(GetLog(LLDBLog::Watchpoints), std::move(err),
"Failed to set type: Typesystem is no longer live: {0}");
return CompilerType();
}
if (size <= target.GetArchitecture().GetAddressByteSize())
return ts->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 8 * size);
CompilerType clang_uint8_type =
ts->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 8);
return clang_uint8_type.GetArrayType(size);
}
static CompilerType CreateCompilerType(Target &target, uint32_t size,
const CompilerType *type) {
if (type && type->IsValid())
return *type;
// If we don't have a known type, then we force it to unsigned int of the
// right size.
return DeriveCompilerType(target, size);
}
lldb::ValueObjectSP Watchpoint::CalculateWatchedValue() const {
if (!m_type.IsValid()) {
// Don't know how to report new & old values, since we couldn't make a
// scalar type for this watchpoint. This works around an assert in
// ValueObjectMemory::Create.
// FIXME: This should not happen, but if it does in some case we care about,
// we can go grab the value raw and print it as unsigned.
return nullptr;
}
// To obtain a value that represents memory at a given address, we only need
// an information about process.
// Create here an ExecutionContext from the current process.
ExecutionContext exe_ctx;
lldb::ProcessSP process_sp = m_target.GetProcessSP();
if (!process_sp)
return nullptr;
process_sp->CalculateExecutionContext(exe_ctx);
ConstString g_watch_name("$__lldb__watch_value");
Address watch_address(m_addr);
auto new_value_sp = ValueObjectMemory::Create(
exe_ctx.GetBestExecutionContextScope(), g_watch_name.GetStringRef(),
watch_address, m_type);
new_value_sp = new_value_sp->CreateConstantValue(g_watch_name);
if (!new_value_sp)
Debugger::ReportError("Watchpoint %u: Failed to calculate watched value! "
"The behavior of this watchpoint may be disrupted!",
m_id);
return new_value_sp;
}
Watchpoint::Watchpoint(Target &target, lldb::addr_t addr, uint32_t size,
const CompilerType *type, bool hardware)
: StoppointSite(0, addr, size, hardware), m_target(target),
m_enabled(false), m_is_hardware(hardware), m_is_watch_variable(false),
m_is_ephemeral(false), m_disabled_count(0u), m_watch_type(0u),
m_ignore_count(0u), m_type(CreateCompilerType(target, size, type)) {
// Set the initial value of the watched variable:
UpdateWatchedValue(CalculateWatchedValue());
}
Watchpoint::~Watchpoint() = default;
// This function is used when "baton" doesn't need to be freed
void Watchpoint::SetCallback(WatchpointHitCallback callback, void *baton,
bool is_synchronous) {
// The default "Baton" class will keep a copy of "baton" and won't free or
// delete it when it goes out of scope.
m_options.SetCallback(callback, std::make_shared<UntypedBaton>(baton),
is_synchronous);
SendWatchpointChangedEvent(eWatchpointEventTypeCommandChanged);
}
// This function is used when a baton needs to be freed and therefore is
// contained in a "Baton" subclass.
void Watchpoint::SetCallback(WatchpointHitCallback callback,
const BatonSP &callback_baton_sp,
bool is_synchronous) {
m_options.SetCallback(callback, callback_baton_sp, is_synchronous);
SendWatchpointChangedEvent(eWatchpointEventTypeCommandChanged);
}
bool Watchpoint::SetupVariableWatchpointDisabler(StackFrameSP frame_sp) const {
if (!frame_sp)
return false;
ThreadSP thread_sp = frame_sp->GetThread();
if (!thread_sp)
return false;
uint32_t return_frame_index =
thread_sp->GetSelectedFrameIndex(DoNoSelectMostRelevantFrame) + 1;
if (return_frame_index >= LLDB_INVALID_FRAME_ID)
return false;
StackFrameSP return_frame_sp(
thread_sp->GetStackFrameAtIndex(return_frame_index));
if (!return_frame_sp)
return false;
ExecutionContext exe_ctx(return_frame_sp);
TargetSP target_sp = exe_ctx.GetTargetSP();
if (!target_sp)
return false;
Address return_address(return_frame_sp->GetFrameCodeAddress());
lldb::addr_t return_addr = return_address.GetLoadAddress(target_sp.get());
if (return_addr == LLDB_INVALID_ADDRESS)
return false;
BreakpointSP bp_sp = target_sp->CreateBreakpoint(
return_addr, /*internal=*/true, /*request_hardware=*/false);
if (!bp_sp || !bp_sp->HasResolvedLocations())
return false;
auto wvc_up = std::make_unique<WatchpointVariableContext>(GetID(), exe_ctx);
auto baton_sp = std::make_shared<WatchpointVariableBaton>(std::move(wvc_up));
bp_sp->SetCallback(VariableWatchpointDisabler, baton_sp);
bp_sp->SetOneShot(true);
bp_sp->SetBreakpointKind("variable watchpoint disabler");
return true;
}
bool Watchpoint::VariableWatchpointDisabler(void *baton,
StoppointCallbackContext *context,
user_id_t break_id,
user_id_t break_loc_id) {
assert(baton && "null baton");
if (!baton || !context)
return false;
Log *log = GetLog(LLDBLog::Watchpoints);
WatchpointVariableContext *wvc =
static_cast<WatchpointVariableContext *>(baton);
LLDB_LOGF(log, "called by breakpoint %" PRIu64 ".%" PRIu64, break_id,
break_loc_id);
if (wvc->watch_id == LLDB_INVALID_WATCH_ID)
return false;
TargetSP target_sp = context->exe_ctx_ref.GetTargetSP();
if (!target_sp)
return false;
ProcessSP process_sp = target_sp->GetProcessSP();
if (!process_sp)
return false;
WatchpointSP watch_sp =
target_sp->GetWatchpointList().FindByID(wvc->watch_id);
if (!watch_sp)
return false;
if (wvc->exe_ctx == context->exe_ctx_ref) {
LLDB_LOGF(log,
"callback for watchpoint %" PRId32
" matched internal breakpoint execution context",
watch_sp->GetID());
process_sp->DisableWatchpoint(watch_sp);
return false;
}
LLDB_LOGF(log,
"callback for watchpoint %" PRId32
" didn't match internal breakpoint execution context",
watch_sp->GetID());
return false;
}
void Watchpoint::ClearCallback() {
m_options.ClearCallback();
SendWatchpointChangedEvent(eWatchpointEventTypeCommandChanged);
}
void Watchpoint::SetDeclInfo(const std::string &str) { m_decl_str = str; }
std::string Watchpoint::GetWatchSpec() const { return m_watch_spec_str; }
void Watchpoint::SetWatchSpec(const std::string &str) {
m_watch_spec_str = str;
}
bool Watchpoint::IsHardware() const {
lldbassert(m_is_hardware || !HardwareRequired());
return m_is_hardware;
}
bool Watchpoint::IsWatchVariable() const { return m_is_watch_variable; }
void Watchpoint::SetWatchVariable(bool val) { m_is_watch_variable = val; }
bool Watchpoint::CaptureWatchedValue(const ExecutionContext &exe_ctx) {
ConstString g_watch_name("$__lldb__watch_value");
m_old_value_sp = m_new_value_sp;
Address watch_address(GetLoadAddress());
if (!m_type.IsValid()) {
// Don't know how to report new & old values, since we couldn't make a
// scalar type for this watchpoint. This works around an assert in
// ValueObjectMemory::Create.
// FIXME: This should not happen, but if it does in some case we care about,
// we can go grab the value raw and print it as unsigned.
return false;
}
m_new_value_sp = ValueObjectMemory::Create(
exe_ctx.GetBestExecutionContextScope(), g_watch_name.GetStringRef(),
watch_address, m_type);
m_new_value_sp = m_new_value_sp->CreateConstantValue(g_watch_name);
return (m_new_value_sp && m_new_value_sp->GetError().Success());
}
bool Watchpoint::WatchedValueReportable(const ExecutionContext &exe_ctx) {
if (!WatchpointModify() || WatchpointRead())
return true;
if (!m_type.IsValid())
return true;
ConstString g_watch_name("$__lldb__watch_value");
Address watch_address(GetLoadAddress());
ValueObjectSP newest_valueobj_sp = ValueObjectMemory::Create(
exe_ctx.GetBestExecutionContextScope(), g_watch_name.GetStringRef(),
watch_address, m_type);
newest_valueobj_sp = newest_valueobj_sp->CreateConstantValue(g_watch_name);
Status error;
DataExtractor new_data;
DataExtractor old_data;
newest_valueobj_sp->GetData(new_data, error);
if (error.Fail())
return true;
m_new_value_sp->GetData(old_data, error);
if (error.Fail())
return true;
if (new_data.GetByteSize() != old_data.GetByteSize() ||
new_data.GetByteSize() == 0)
return true;
if (memcmp(new_data.GetDataStart(), old_data.GetDataStart(),
old_data.GetByteSize()) == 0)
return false; // Value has not changed, user requested modify watchpoint
return true;
}
// RETURNS - true if we should stop at this breakpoint, false if we
// should continue.
bool Watchpoint::ShouldStop(StoppointCallbackContext *context) {
m_hit_counter.Increment();
return IsEnabled();
}
void Watchpoint::GetDescription(Stream *s, lldb::DescriptionLevel level) const {
DumpWithLevel(s, level);
}
void Watchpoint::Dump(Stream *s) const {
DumpWithLevel(s, lldb::eDescriptionLevelBrief);
}
// If prefix is nullptr, we display the watch id and ignore the prefix
// altogether.
bool Watchpoint::DumpSnapshots(Stream *s, const char *prefix) const {
bool printed_anything = false;
// For read watchpoints, don't display any before/after value changes.
if (WatchpointRead() && !WatchpointModify() && !WatchpointWrite())
return printed_anything;
s->Printf("\n");
s->Printf("Watchpoint %u hit:\n", GetID());
StreamString values_ss;
if (prefix)
values_ss.Indent(prefix);
if (m_old_value_sp) {
if (auto *old_value_cstr = m_old_value_sp->GetValueAsCString()) {
values_ss.Printf("old value: %s", old_value_cstr);
} else {
if (auto *old_summary_cstr = m_old_value_sp->GetSummaryAsCString())
values_ss.Printf("old value: %s", old_summary_cstr);
else {
StreamString strm;
DumpValueObjectOptions options;
options.SetUseDynamicType(eNoDynamicValues)
.SetHideRootType(true)
.SetHideRootName(true)
.SetHideName(true);
if (llvm::Error error = m_old_value_sp->Dump(strm, options))
strm << "error: " << toString(std::move(error));
if (strm.GetData())
values_ss.Printf("old value: %s", strm.GetData());
}
}
}
if (m_new_value_sp) {
if (values_ss.GetSize())
values_ss.Printf("\n");
if (auto *new_value_cstr = m_new_value_sp->GetValueAsCString())
values_ss.Printf("new value: %s", new_value_cstr);
else {
if (auto *new_summary_cstr = m_new_value_sp->GetSummaryAsCString())
values_ss.Printf("new value: %s", new_summary_cstr);
else {
StreamString strm;
DumpValueObjectOptions options;
options.SetUseDynamicType(eNoDynamicValues)
.SetHideRootType(true)
.SetHideRootName(true)
.SetHideName(true);
if (llvm::Error error = m_new_value_sp->Dump(strm, options))
strm << "error: " << toString(std::move(error));
if (strm.GetData())
values_ss.Printf("new value: %s", strm.GetData());
}
}
}
if (values_ss.GetSize()) {
s->Printf("%s", values_ss.GetData());
printed_anything = true;
}
return printed_anything;
}
void Watchpoint::DumpWithLevel(Stream *s,
lldb::DescriptionLevel description_level) const {
if (s == nullptr)
return;
assert(description_level >= lldb::eDescriptionLevelBrief &&
description_level <= lldb::eDescriptionLevelVerbose);
s->Printf("Watchpoint %u: addr = 0x%8.8" PRIx64
" size = %u state = %s type = %s%s%s",
GetID(), GetLoadAddress(), m_byte_size,
IsEnabled() ? "enabled" : "disabled", WatchpointRead() ? "r" : "",
WatchpointWrite() ? "w" : "", WatchpointModify() ? "m" : "");
if (description_level >= lldb::eDescriptionLevelFull) {
if (!m_decl_str.empty())
s->Printf("\n declare @ '%s'", m_decl_str.c_str());
if (!m_watch_spec_str.empty())
s->Printf("\n watchpoint spec = '%s'", m_watch_spec_str.c_str());
if (IsEnabled()) {
if (ProcessSP process_sp = m_target.GetProcessSP()) {
auto &resourcelist = process_sp->GetWatchpointResourceList();
size_t idx = 0;
s->Printf("\n watchpoint resources:");
for (WatchpointResourceSP &wpres : resourcelist.Sites()) {
if (wpres->ConstituentsContains(this)) {
s->Printf("\n #%zu: ", idx);
wpres->Dump(s);
}
idx++;
}
}
}
// Dump the snapshots we have taken.
DumpSnapshots(s, " ");
if (GetConditionText())
s->Printf("\n condition = '%s'", GetConditionText());
m_options.GetCallbackDescription(s, description_level);
}
if (description_level >= lldb::eDescriptionLevelVerbose) {
s->Printf("\n hit_count = %-4u ignore_count = %-4u", GetHitCount(),
GetIgnoreCount());
}
}
bool Watchpoint::IsEnabled() const { return m_enabled; }
// Within StopInfo.cpp, we purposely turn on the ephemeral mode right before
// temporarily disable the watchpoint in order to perform possible watchpoint
// actions without triggering further watchpoint events. After the temporary
// disabled watchpoint is enabled, we then turn off the ephemeral mode.
void Watchpoint::TurnOnEphemeralMode() { m_is_ephemeral = true; }
void Watchpoint::TurnOffEphemeralMode() {
m_is_ephemeral = false;
// Leaving ephemeral mode, reset the m_disabled_count!
m_disabled_count = 0;
}
bool Watchpoint::IsDisabledDuringEphemeralMode() {
return m_disabled_count > 1 && m_is_ephemeral;
}
void Watchpoint::SetEnabled(bool enabled, bool notify) {
if (!enabled) {
if (m_is_ephemeral)
++m_disabled_count;
// Don't clear the snapshots for now.
// Within StopInfo.cpp, we purposely do disable/enable watchpoint while
// performing watchpoint actions.
}
bool changed = enabled != m_enabled;
m_enabled = enabled;
if (notify && !m_is_ephemeral && changed)
SendWatchpointChangedEvent(enabled ? eWatchpointEventTypeEnabled
: eWatchpointEventTypeDisabled);
}
void Watchpoint::SetWatchpointType(uint32_t type, bool notify) {
if (notify && m_watch_type != type)
SendWatchpointChangedEvent(eWatchpointEventTypeTypeChanged);
m_watch_type = type;
}
uint32_t Watchpoint::GetIgnoreCount() const { return m_ignore_count; }
void Watchpoint::SetIgnoreCount(uint32_t n) {
bool changed = m_ignore_count != n;
m_ignore_count = n;
if (changed)
SendWatchpointChangedEvent(eWatchpointEventTypeIgnoreChanged);
}
bool Watchpoint::InvokeCallback(StoppointCallbackContext *context) {
return m_options.InvokeCallback(context, GetID());
}
void Watchpoint::SetCondition(const char *condition) {
if (condition == nullptr || condition[0] == '\0') {
if (m_condition_up)
m_condition_up.reset();
} else {
// Pass nullptr for expr_prefix (no translation-unit level definitions).
Status error;
m_condition_up.reset(m_target.GetUserExpressionForLanguage(
condition, {}, {}, UserExpression::eResultTypeAny,
EvaluateExpressionOptions(), nullptr, error));
if (error.Fail()) {
// FIXME: Log something...
m_condition_up.reset();
}
}
SendWatchpointChangedEvent(eWatchpointEventTypeConditionChanged);
}
const char *Watchpoint::GetConditionText() const {
if (m_condition_up)
return m_condition_up->GetUserText();
return nullptr;
}
void Watchpoint::SendWatchpointChangedEvent(
lldb::WatchpointEventType eventKind) {
if (GetTarget().EventTypeHasListeners(
Target::eBroadcastBitWatchpointChanged)) {
auto data_sp =
std::make_shared<WatchpointEventData>(eventKind, shared_from_this());
GetTarget().BroadcastEvent(Target::eBroadcastBitWatchpointChanged, data_sp);
}
}
Watchpoint::WatchpointEventData::WatchpointEventData(
WatchpointEventType sub_type, const WatchpointSP &new_watchpoint_sp)
: m_watchpoint_event(sub_type), m_new_watchpoint_sp(new_watchpoint_sp) {}
Watchpoint::WatchpointEventData::~WatchpointEventData() = default;
llvm::StringRef Watchpoint::WatchpointEventData::GetFlavorString() {
return "Watchpoint::WatchpointEventData";
}
llvm::StringRef Watchpoint::WatchpointEventData::GetFlavor() const {
return WatchpointEventData::GetFlavorString();
}
WatchpointSP &Watchpoint::WatchpointEventData::GetWatchpoint() {
return m_new_watchpoint_sp;
}
WatchpointEventType
Watchpoint::WatchpointEventData::GetWatchpointEventType() const {
return m_watchpoint_event;
}
void Watchpoint::WatchpointEventData::Dump(Stream *s) const {}
const Watchpoint::WatchpointEventData *
Watchpoint::WatchpointEventData::GetEventDataFromEvent(const Event *event) {
if (event) {
const EventData *event_data = event->GetData();
if (event_data &&
event_data->GetFlavor() == WatchpointEventData::GetFlavorString())
return static_cast<const WatchpointEventData *>(event->GetData());
}
return nullptr;
}
WatchpointEventType
Watchpoint::WatchpointEventData::GetWatchpointEventTypeFromEvent(
const EventSP &event_sp) {
const WatchpointEventData *data = GetEventDataFromEvent(event_sp.get());
if (data == nullptr)
return eWatchpointEventTypeInvalidType;
return data->GetWatchpointEventType();
}
WatchpointSP Watchpoint::WatchpointEventData::GetWatchpointFromEvent(
const EventSP &event_sp) {
WatchpointSP wp_sp;
const WatchpointEventData *data = GetEventDataFromEvent(event_sp.get());
if (data)
wp_sp = data->m_new_watchpoint_sp;
return wp_sp;
}
bool Watchpoint::CheckWatchpointCondition(
const ExecutionContext &exe_ctx) const {
if (GetConditionText() == nullptr)
return true;
Log *log = GetLog(LLDBLog::Watchpoints);
// We need to make sure the user sees any parse errors in their
// condition, so we'll hook the constructor errors up to the
// debugger's Async I/O.
EvaluateExpressionOptions expr_options;
expr_options.SetUnwindOnError(true);
expr_options.SetIgnoreBreakpoints(true);
ValueObjectSP result_value_sp;
ExpressionResults result_code = m_target.EvaluateExpression(
GetConditionText(), exe_ctx.GetBestExecutionContextScope(),
result_value_sp, expr_options);
if (!result_value_sp || result_code != eExpressionCompleted) {
LLDB_LOGF(log, "Error evaluating condition:\n");
StreamString strm;
strm << "stopped due to an error evaluating condition of "
"watchpoint ";
GetDescription(&strm, eDescriptionLevelBrief);
strm << ": \"" << GetConditionText() << "\"\n";
Debugger::ReportError(strm.GetString().str(),
exe_ctx.GetTargetRef().GetDebugger().GetID());
return true;
}
Scalar scalar_value;
if (!result_value_sp->ResolveValue(scalar_value)) {
LLDB_LOGF(log, "Failed to get an integer result from the expression.");
return true;
}
bool should_stop = scalar_value.ULongLong(1) != 0;
LLDB_LOGF(log, "Condition successfully evaluated, result is %s.\n",
should_stop ? "true" : "false");
return should_stop;
}
// The HasTargetRunSinceMe class remembers the process resume_id before a
// watchpoint callback execution. After the execution, HasTargetRunSinceMe uses
// this stored value to determine whether callback itself caused the target to
// resume.
class HasTargetRunSinceMe {
public:
HasTargetRunSinceMe(ProcessSP process_sp)
: m_process_sp(process_sp),
m_resume_id(m_process_sp ? m_process_sp->GetResumeID() : 0) {}
bool operator()() const {
if (!m_process_sp)
return false;
lldb::StateType ret_type = m_process_sp->GetState();
if (ret_type == eStateRunning)
return true;
if (ret_type == eStateStopped) {
// This is a little tricky. We want to count "run and stopped again
// before you could ask this question as a "TRUE" answer to
// HasTargetRunSinceMe. But we don't want to include any running of the
// target done for expressions. So we track both resumes, and resumes
// caused by expressions, and check if there are any resumes
// NOT caused
// by expressions.
uint32_t curr_resume_id = m_process_sp->GetResumeID();
uint32_t last_user_expression_id =
m_process_sp->GetLastUserExpressionResumeID();
if (curr_resume_id == m_resume_id)
return false;
if (curr_resume_id > last_user_expression_id)
return true;
}
return false;
}
private:
ProcessSP m_process_sp;
uint32_t m_resume_id;
};
bool Watchpoint::EvaluateWatchpointCallback(StoppointCallbackContext &context) {
// FIXME: For now the callbacks have to run in async mode - the
// first time we restart we need
// to get out of there. So set it here.
// When we figure out how to nest watchpoint hits then this will
// change.
HasTargetRunSinceMe has_target_run_since_me(
context.exe_ctx_ref.GetProcessSP());
Debugger &debugger = m_target.GetDebugger();
bool old_async = debugger.GetAsyncExecution();
debugger.SetAsyncExecution(true);
bool stop_requested = InvokeCallback(&context);
debugger.SetAsyncExecution(old_async);
// Also make sure that the callback hasn't continued the target. If
// it did, when we'll not report this watchpoint hit.
if (has_target_run_since_me())
return false;
return stop_requested;
}
bool Watchpoint::ShouldReport(StoppointCallbackContext &context) {
if (!CheckWatchpointCondition(context.exe_ctx_ref)) {
// The condition failed, which we consider "not having hit
// the watchpoint".
return false;
}
lldb::ValueObjectSP current_value_sp = CalculateWatchedValue();
if (!current_value_sp || current_value_sp->GetError().Fail())
return false;
// Don't stop if the watched region value is unmodified, and
// this is a Modify-type watchpoint.
if (WatchpointModify() && !WatchpointRead() &&
IsValueObjectsEqual(current_value_sp, m_new_value_sp))
return false;
// All checks are passed. Hit!
m_hit_counter.Increment();
// Even if ignore count check failed consider it as a hit anyway,
// but don't stop at this watchpoint.
if (GetHitCount() <= m_ignore_count)
return false;
// Run the callback to decide whether to stop.
if (!EvaluateWatchpointCallback(context))
return false;
UpdateWatchedValue(current_value_sp);
return true;
}
|