1//===-- CommandObjectWatchpoint.cpp ---------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "CommandObjectWatchpoint.h"
10#include "CommandObjectWatchpointCommand.h"
11
12#include <memory>
13#include <vector>
14
15#include "llvm/ADT/StringRef.h"
16
17#include "lldb/Breakpoint/Watchpoint.h"
18#include "lldb/Breakpoint/WatchpointList.h"
19#include "lldb/Core/ValueObject.h"
20#include "lldb/Host/OptionParser.h"
21#include "lldb/Interpreter/CommandInterpreter.h"
22#include "lldb/Interpreter/CommandOptionArgumentTable.h"
23#include "lldb/Interpreter/CommandReturnObject.h"
24#include "lldb/Symbol/Function.h"
25#include "lldb/Symbol/Variable.h"
26#include "lldb/Symbol/VariableList.h"
27#include "lldb/Target/StackFrame.h"
28#include "lldb/Target/Target.h"
29#include "lldb/Utility/StreamString.h"
30
31using namespace lldb;
32using namespace lldb_private;
33
34static void AddWatchpointDescription(Stream &s, Watchpoint &wp,
35 lldb::DescriptionLevel level) {
36 s.IndentMore();
37 wp.GetDescription(s: &s, level);
38 s.IndentLess();
39 s.EOL();
40}
41
42static bool CheckTargetForWatchpointOperations(Target *target,
43 CommandReturnObject &result) {
44 bool process_is_valid =
45 target->GetProcessSP() && target->GetProcessSP()->IsAlive();
46 if (!process_is_valid) {
47 result.AppendError(in_string: "There's no process or it is not alive.");
48 return false;
49 }
50 // Target passes our checks, return true.
51 return true;
52}
53
54// Equivalent class: {"-", "to", "To", "TO"} of range specifier array.
55static const char *RSA[4] = {"-", "to", "To", "TO"};
56
57// Return the index to RSA if found; otherwise -1 is returned.
58static int32_t WithRSAIndex(llvm::StringRef Arg) {
59
60 uint32_t i;
61 for (i = 0; i < 4; ++i)
62 if (Arg.contains(Other: RSA[i]))
63 return i;
64 return -1;
65}
66
67// Return true if wp_ids is successfully populated with the watch ids. False
68// otherwise.
69bool CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(
70 Target *target, Args &args, std::vector<uint32_t> &wp_ids) {
71 // Pre-condition: args.GetArgumentCount() > 0.
72 if (args.GetArgumentCount() == 0) {
73 if (target == nullptr)
74 return false;
75 WatchpointSP watch_sp = target->GetLastCreatedWatchpoint();
76 if (watch_sp) {
77 wp_ids.push_back(x: watch_sp->GetID());
78 return true;
79 } else
80 return false;
81 }
82
83 llvm::StringRef Minus("-");
84 std::vector<llvm::StringRef> StrRefArgs;
85 llvm::StringRef first;
86 llvm::StringRef second;
87 size_t i;
88 int32_t idx;
89 // Go through the arguments and make a canonical form of arg list containing
90 // only numbers with possible "-" in between.
91 for (auto &entry : args.entries()) {
92 if ((idx = WithRSAIndex(Arg: entry.ref())) == -1) {
93 StrRefArgs.push_back(x: entry.ref());
94 continue;
95 }
96 // The Arg contains the range specifier, split it, then.
97 std::tie(args&: first, args&: second) = entry.ref().split(Separator: RSA[idx]);
98 if (!first.empty())
99 StrRefArgs.push_back(x: first);
100 StrRefArgs.push_back(x: Minus);
101 if (!second.empty())
102 StrRefArgs.push_back(x: second);
103 }
104 // Now process the canonical list and fill in the vector of uint32_t's. If
105 // there is any error, return false and the client should ignore wp_ids.
106 uint32_t beg, end, id;
107 size_t size = StrRefArgs.size();
108 bool in_range = false;
109 for (i = 0; i < size; ++i) {
110 llvm::StringRef Arg = StrRefArgs[i];
111 if (in_range) {
112 // Look for the 'end' of the range. Note StringRef::getAsInteger()
113 // returns true to signify error while parsing.
114 if (Arg.getAsInteger(Radix: 0, Result&: end))
115 return false;
116 // Found a range! Now append the elements.
117 for (id = beg; id <= end; ++id)
118 wp_ids.push_back(x: id);
119 in_range = false;
120 continue;
121 }
122 if (i < (size - 1) && StrRefArgs[i + 1] == Minus) {
123 if (Arg.getAsInteger(Radix: 0, Result&: beg))
124 return false;
125 // Turn on the in_range flag, we are looking for end of range next.
126 ++i;
127 in_range = true;
128 continue;
129 }
130 // Otherwise, we have a simple ID. Just append it.
131 if (Arg.getAsInteger(Radix: 0, Result&: beg))
132 return false;
133 wp_ids.push_back(x: beg);
134 }
135
136 // It is an error if after the loop, we're still in_range.
137 return !in_range;
138}
139
140// CommandObjectWatchpointList
141
142// CommandObjectWatchpointList::Options
143#pragma mark List::CommandOptions
144#define LLDB_OPTIONS_watchpoint_list
145#include "CommandOptions.inc"
146
147#pragma mark List
148
149class CommandObjectWatchpointList : public CommandObjectParsed {
150public:
151 CommandObjectWatchpointList(CommandInterpreter &interpreter)
152 : CommandObjectParsed(
153 interpreter, "watchpoint list",
154 "List all watchpoints at configurable levels of detail.", nullptr,
155 eCommandRequiresTarget) {
156 CommandArgumentEntry arg;
157 CommandObject::AddIDsArgumentData(arg&: arg, ID: eArgTypeWatchpointID,
158 IDRange: eArgTypeWatchpointIDRange);
159 // Add the entry for the first argument for this command to the object's
160 // arguments vector.
161 m_arguments.push_back(arg);
162 }
163
164 ~CommandObjectWatchpointList() override = default;
165
166 Options *GetOptions() override { return &m_options; }
167
168 class CommandOptions : public Options {
169 public:
170 CommandOptions() = default;
171
172 ~CommandOptions() override = default;
173
174 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
175 ExecutionContext *execution_context) override {
176 Status error;
177 const int short_option = m_getopt_table[option_idx].val;
178
179 switch (short_option) {
180 case 'b':
181 m_level = lldb::eDescriptionLevelBrief;
182 break;
183 case 'f':
184 m_level = lldb::eDescriptionLevelFull;
185 break;
186 case 'v':
187 m_level = lldb::eDescriptionLevelVerbose;
188 break;
189 default:
190 llvm_unreachable("Unimplemented option");
191 }
192
193 return error;
194 }
195
196 void OptionParsingStarting(ExecutionContext *execution_context) override {
197 m_level = lldb::eDescriptionLevelFull;
198 }
199
200 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
201 return llvm::ArrayRef(g_watchpoint_list_options);
202 }
203
204 // Instance variables to hold the values for command options.
205
206 lldb::DescriptionLevel m_level = lldb::eDescriptionLevelBrief;
207 };
208
209protected:
210 void DoExecute(Args &command, CommandReturnObject &result) override {
211 Target *target = &GetSelectedTarget();
212
213 if (target->GetProcessSP() && target->GetProcessSP()->IsAlive()) {
214 std::optional<uint32_t> num_supported_hardware_watchpoints =
215 target->GetProcessSP()->GetWatchpointSlotCount();
216
217 if (num_supported_hardware_watchpoints)
218 result.AppendMessageWithFormat(
219 format: "Number of supported hardware watchpoints: %u\n",
220 *num_supported_hardware_watchpoints);
221 }
222
223 const WatchpointList &watchpoints = target->GetWatchpointList();
224
225 std::unique_lock<std::recursive_mutex> lock;
226 target->GetWatchpointList().GetListMutex(lock);
227
228 size_t num_watchpoints = watchpoints.GetSize();
229
230 if (num_watchpoints == 0) {
231 result.AppendMessage(in_string: "No watchpoints currently set.");
232 result.SetStatus(eReturnStatusSuccessFinishNoResult);
233 return;
234 }
235
236 Stream &output_stream = result.GetOutputStream();
237
238 if (command.GetArgumentCount() == 0) {
239 // No watchpoint selected; show info about all currently set watchpoints.
240 result.AppendMessage(in_string: "Current watchpoints:");
241 for (size_t i = 0; i < num_watchpoints; ++i) {
242 WatchpointSP watch_sp = watchpoints.GetByIndex(i);
243 AddWatchpointDescription(s&: output_stream, wp&: *watch_sp, level: m_options.m_level);
244 }
245 result.SetStatus(eReturnStatusSuccessFinishNoResult);
246 } else {
247 // Particular watchpoints selected; enable them.
248 std::vector<uint32_t> wp_ids;
249 if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(
250 target, args&: command, wp_ids&: wp_ids)) {
251 result.AppendError(in_string: "Invalid watchpoints specification.");
252 return;
253 }
254
255 const size_t size = wp_ids.size();
256 for (size_t i = 0; i < size; ++i) {
257 WatchpointSP watch_sp = watchpoints.FindByID(watchID: wp_ids[i]);
258 if (watch_sp)
259 AddWatchpointDescription(s&: output_stream, wp&: *watch_sp, level: m_options.m_level);
260 result.SetStatus(eReturnStatusSuccessFinishNoResult);
261 }
262 }
263 }
264
265private:
266 CommandOptions m_options;
267};
268
269// CommandObjectWatchpointEnable
270#pragma mark Enable
271
272class CommandObjectWatchpointEnable : public CommandObjectParsed {
273public:
274 CommandObjectWatchpointEnable(CommandInterpreter &interpreter)
275 : CommandObjectParsed(interpreter, "enable",
276 "Enable the specified disabled watchpoint(s). If "
277 "no watchpoints are specified, enable all of them.",
278 nullptr, eCommandRequiresTarget) {
279 CommandArgumentEntry arg;
280 CommandObject::AddIDsArgumentData(arg, ID: eArgTypeWatchpointID,
281 IDRange: eArgTypeWatchpointIDRange);
282 // Add the entry for the first argument for this command to the object's
283 // arguments vector.
284 m_arguments.push_back(x: arg);
285 }
286
287 ~CommandObjectWatchpointEnable() override = default;
288
289 void
290 HandleArgumentCompletion(CompletionRequest &request,
291 OptionElementVector &opt_element_vector) override {
292 lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
293 interpreter&: GetCommandInterpreter(), completion_mask: lldb::eWatchpointIDCompletion, request,
294 searcher: nullptr);
295 }
296
297protected:
298 void DoExecute(Args &command, CommandReturnObject &result) override {
299 Target *target = &GetSelectedTarget();
300 if (!CheckTargetForWatchpointOperations(target, result))
301 return;
302
303 std::unique_lock<std::recursive_mutex> lock;
304 target->GetWatchpointList().GetListMutex(lock);
305
306 const WatchpointList &watchpoints = target->GetWatchpointList();
307
308 size_t num_watchpoints = watchpoints.GetSize();
309
310 if (num_watchpoints == 0) {
311 result.AppendError(in_string: "No watchpoints exist to be enabled.");
312 return;
313 }
314
315 if (command.GetArgumentCount() == 0) {
316 // No watchpoint selected; enable all currently set watchpoints.
317 target->EnableAllWatchpoints();
318 result.AppendMessageWithFormat(format: "All watchpoints enabled. (%" PRIu64
319 " watchpoints)\n",
320 (uint64_t)num_watchpoints);
321 result.SetStatus(eReturnStatusSuccessFinishNoResult);
322 } else {
323 // Particular watchpoints selected; enable them.
324 std::vector<uint32_t> wp_ids;
325 if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(
326 target, args&: command, wp_ids)) {
327 result.AppendError(in_string: "Invalid watchpoints specification.");
328 return;
329 }
330
331 int count = 0;
332 const size_t size = wp_ids.size();
333 for (size_t i = 0; i < size; ++i)
334 if (target->EnableWatchpointByID(watch_id: wp_ids[i]))
335 ++count;
336 result.AppendMessageWithFormat(format: "%d watchpoints enabled.\n", count);
337 result.SetStatus(eReturnStatusSuccessFinishNoResult);
338 }
339 }
340};
341
342// CommandObjectWatchpointDisable
343#pragma mark Disable
344
345class CommandObjectWatchpointDisable : public CommandObjectParsed {
346public:
347 CommandObjectWatchpointDisable(CommandInterpreter &interpreter)
348 : CommandObjectParsed(interpreter, "watchpoint disable",
349 "Disable the specified watchpoint(s) without "
350 "removing it/them. If no watchpoints are "
351 "specified, disable them all.",
352 nullptr, eCommandRequiresTarget) {
353 CommandArgumentEntry arg;
354 CommandObject::AddIDsArgumentData(arg, ID: eArgTypeWatchpointID,
355 IDRange: eArgTypeWatchpointIDRange);
356 // Add the entry for the first argument for this command to the object's
357 // arguments vector.
358 m_arguments.push_back(x: arg);
359 }
360
361 ~CommandObjectWatchpointDisable() override = default;
362
363 void
364 HandleArgumentCompletion(CompletionRequest &request,
365 OptionElementVector &opt_element_vector) override {
366 lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
367 interpreter&: GetCommandInterpreter(), completion_mask: lldb::eWatchpointIDCompletion, request,
368 searcher: nullptr);
369 }
370
371protected:
372 void DoExecute(Args &command, CommandReturnObject &result) override {
373 Target *target = &GetSelectedTarget();
374 if (!CheckTargetForWatchpointOperations(target, result))
375 return;
376
377 std::unique_lock<std::recursive_mutex> lock;
378 target->GetWatchpointList().GetListMutex(lock);
379
380 const WatchpointList &watchpoints = target->GetWatchpointList();
381 size_t num_watchpoints = watchpoints.GetSize();
382
383 if (num_watchpoints == 0) {
384 result.AppendError(in_string: "No watchpoints exist to be disabled.");
385 return;
386 }
387
388 if (command.GetArgumentCount() == 0) {
389 // No watchpoint selected; disable all currently set watchpoints.
390 if (target->DisableAllWatchpoints()) {
391 result.AppendMessageWithFormat(format: "All watchpoints disabled. (%" PRIu64
392 " watchpoints)\n",
393 (uint64_t)num_watchpoints);
394 result.SetStatus(eReturnStatusSuccessFinishNoResult);
395 } else {
396 result.AppendError(in_string: "Disable all watchpoints failed\n");
397 }
398 } else {
399 // Particular watchpoints selected; disable them.
400 std::vector<uint32_t> wp_ids;
401 if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(
402 target, args&: command, wp_ids)) {
403 result.AppendError(in_string: "Invalid watchpoints specification.");
404 return;
405 }
406
407 int count = 0;
408 const size_t size = wp_ids.size();
409 for (size_t i = 0; i < size; ++i)
410 if (target->DisableWatchpointByID(watch_id: wp_ids[i]))
411 ++count;
412 result.AppendMessageWithFormat(format: "%d watchpoints disabled.\n", count);
413 result.SetStatus(eReturnStatusSuccessFinishNoResult);
414 }
415 }
416};
417
418// CommandObjectWatchpointDelete
419#define LLDB_OPTIONS_watchpoint_delete
420#include "CommandOptions.inc"
421
422// CommandObjectWatchpointDelete
423#pragma mark Delete
424
425class CommandObjectWatchpointDelete : public CommandObjectParsed {
426public:
427 CommandObjectWatchpointDelete(CommandInterpreter &interpreter)
428 : CommandObjectParsed(interpreter, "watchpoint delete",
429 "Delete the specified watchpoint(s). If no "
430 "watchpoints are specified, delete them all.",
431 nullptr, eCommandRequiresTarget) {
432 CommandArgumentEntry arg;
433 CommandObject::AddIDsArgumentData(arg, ID: eArgTypeWatchpointID,
434 IDRange: eArgTypeWatchpointIDRange);
435 // Add the entry for the first argument for this command to the object's
436 // arguments vector.
437 m_arguments.push_back(x: arg);
438 }
439
440 ~CommandObjectWatchpointDelete() override = default;
441
442 void
443 HandleArgumentCompletion(CompletionRequest &request,
444 OptionElementVector &opt_element_vector) override {
445 lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
446 interpreter&: GetCommandInterpreter(), completion_mask: lldb::eWatchpointIDCompletion, request,
447 searcher: nullptr);
448 }
449
450 Options *GetOptions() override { return &m_options; }
451
452 class CommandOptions : public Options {
453 public:
454 CommandOptions() = default;
455
456 ~CommandOptions() override = default;
457
458 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
459 ExecutionContext *execution_context) override {
460 const int short_option = m_getopt_table[option_idx].val;
461
462 switch (short_option) {
463 case 'f':
464 m_force = true;
465 break;
466 default:
467 llvm_unreachable("Unimplemented option");
468 }
469
470 return {};
471 }
472
473 void OptionParsingStarting(ExecutionContext *execution_context) override {
474 m_force = false;
475 }
476
477 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
478 return llvm::ArrayRef(g_watchpoint_delete_options);
479 }
480
481 // Instance variables to hold the values for command options.
482 bool m_force = false;
483 };
484
485protected:
486 void DoExecute(Args &command, CommandReturnObject &result) override {
487 Target *target = &GetSelectedTarget();
488 if (!CheckTargetForWatchpointOperations(target, result))
489 return;
490
491 std::unique_lock<std::recursive_mutex> lock;
492 target->GetWatchpointList().GetListMutex(lock);
493
494 const WatchpointList &watchpoints = target->GetWatchpointList();
495
496 size_t num_watchpoints = watchpoints.GetSize();
497
498 if (num_watchpoints == 0) {
499 result.AppendError(in_string: "No watchpoints exist to be deleted.");
500 return;
501 }
502
503 if (command.empty()) {
504 if (!m_options.m_force &&
505 !m_interpreter.Confirm(
506 message: "About to delete all watchpoints, do you want to do that?",
507 default_answer: true)) {
508 result.AppendMessage(in_string: "Operation cancelled...");
509 } else {
510 target->RemoveAllWatchpoints();
511 result.AppendMessageWithFormat(format: "All watchpoints removed. (%" PRIu64
512 " watchpoints)\n",
513 (uint64_t)num_watchpoints);
514 }
515 result.SetStatus(eReturnStatusSuccessFinishNoResult);
516 return;
517 }
518
519 // Particular watchpoints selected; delete them.
520 std::vector<uint32_t> wp_ids;
521 if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target, args&: command,
522 wp_ids&: wp_ids)) {
523 result.AppendError(in_string: "Invalid watchpoints specification.");
524 return;
525 }
526
527 int count = 0;
528 const size_t size = wp_ids.size();
529 for (size_t i = 0; i < size; ++i)
530 if (target->RemoveWatchpointByID(watch_id: wp_ids[i]))
531 ++count;
532 result.AppendMessageWithFormat(format: "%d watchpoints deleted.\n", count);
533 result.SetStatus(eReturnStatusSuccessFinishNoResult);
534 }
535
536private:
537 CommandOptions m_options;
538};
539
540// CommandObjectWatchpointIgnore
541
542#pragma mark Ignore::CommandOptions
543#define LLDB_OPTIONS_watchpoint_ignore
544#include "CommandOptions.inc"
545
546class CommandObjectWatchpointIgnore : public CommandObjectParsed {
547public:
548 CommandObjectWatchpointIgnore(CommandInterpreter &interpreter)
549 : CommandObjectParsed(interpreter, "watchpoint ignore",
550 "Set ignore count on the specified watchpoint(s). "
551 "If no watchpoints are specified, set them all.",
552 nullptr, eCommandRequiresTarget) {
553 CommandArgumentEntry arg;
554 CommandObject::AddIDsArgumentData(arg, ID: eArgTypeWatchpointID,
555 IDRange: eArgTypeWatchpointIDRange);
556 // Add the entry for the first argument for this command to the object's
557 // arguments vector.
558 m_arguments.push_back(x: arg);
559 }
560
561 ~CommandObjectWatchpointIgnore() override = default;
562
563 void
564 HandleArgumentCompletion(CompletionRequest &request,
565 OptionElementVector &opt_element_vector) override {
566 lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
567 interpreter&: GetCommandInterpreter(), completion_mask: lldb::eWatchpointIDCompletion, request,
568 searcher: nullptr);
569 }
570
571 Options *GetOptions() override { return &m_options; }
572
573 class CommandOptions : public Options {
574 public:
575 CommandOptions() = default;
576
577 ~CommandOptions() override = default;
578
579 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
580 ExecutionContext *execution_context) override {
581 Status error;
582 const int short_option = m_getopt_table[option_idx].val;
583
584 switch (short_option) {
585 case 'i':
586 if (option_arg.getAsInteger(Radix: 0, Result&: m_ignore_count))
587 error.SetErrorStringWithFormat("invalid ignore count '%s'",
588 option_arg.str().c_str());
589 break;
590 default:
591 llvm_unreachable("Unimplemented option");
592 }
593
594 return error;
595 }
596
597 void OptionParsingStarting(ExecutionContext *execution_context) override {
598 m_ignore_count = 0;
599 }
600
601 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
602 return llvm::ArrayRef(g_watchpoint_ignore_options);
603 }
604
605 // Instance variables to hold the values for command options.
606
607 uint32_t m_ignore_count = 0;
608 };
609
610protected:
611 void DoExecute(Args &command, CommandReturnObject &result) override {
612 Target *target = &GetSelectedTarget();
613 if (!CheckTargetForWatchpointOperations(target, result))
614 return;
615
616 std::unique_lock<std::recursive_mutex> lock;
617 target->GetWatchpointList().GetListMutex(lock);
618
619 const WatchpointList &watchpoints = target->GetWatchpointList();
620
621 size_t num_watchpoints = watchpoints.GetSize();
622
623 if (num_watchpoints == 0) {
624 result.AppendError(in_string: "No watchpoints exist to be ignored.");
625 return;
626 }
627
628 if (command.GetArgumentCount() == 0) {
629 target->IgnoreAllWatchpoints(ignore_count: m_options.m_ignore_count);
630 result.AppendMessageWithFormat(format: "All watchpoints ignored. (%" PRIu64
631 " watchpoints)\n",
632 (uint64_t)num_watchpoints);
633 result.SetStatus(eReturnStatusSuccessFinishNoResult);
634 } else {
635 // Particular watchpoints selected; ignore them.
636 std::vector<uint32_t> wp_ids;
637 if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(
638 target, args&: command, wp_ids)) {
639 result.AppendError(in_string: "Invalid watchpoints specification.");
640 return;
641 }
642
643 int count = 0;
644 const size_t size = wp_ids.size();
645 for (size_t i = 0; i < size; ++i)
646 if (target->IgnoreWatchpointByID(watch_id: wp_ids[i], ignore_count: m_options.m_ignore_count))
647 ++count;
648 result.AppendMessageWithFormat(format: "%d watchpoints ignored.\n", count);
649 result.SetStatus(eReturnStatusSuccessFinishNoResult);
650 }
651 }
652
653private:
654 CommandOptions m_options;
655};
656
657// CommandObjectWatchpointModify
658
659#pragma mark Modify::CommandOptions
660#define LLDB_OPTIONS_watchpoint_modify
661#include "CommandOptions.inc"
662
663#pragma mark Modify
664
665class CommandObjectWatchpointModify : public CommandObjectParsed {
666public:
667 CommandObjectWatchpointModify(CommandInterpreter &interpreter)
668 : CommandObjectParsed(
669 interpreter, "watchpoint modify",
670 "Modify the options on a watchpoint or set of watchpoints in the "
671 "executable. "
672 "If no watchpoint is specified, act on the last created "
673 "watchpoint. "
674 "Passing an empty argument clears the modification.",
675 nullptr, eCommandRequiresTarget) {
676 CommandArgumentEntry arg;
677 CommandObject::AddIDsArgumentData(arg, ID: eArgTypeWatchpointID,
678 IDRange: eArgTypeWatchpointIDRange);
679 // Add the entry for the first argument for this command to the object's
680 // arguments vector.
681 m_arguments.push_back(x: arg);
682 }
683
684 ~CommandObjectWatchpointModify() override = default;
685
686 void
687 HandleArgumentCompletion(CompletionRequest &request,
688 OptionElementVector &opt_element_vector) override {
689 lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
690 interpreter&: GetCommandInterpreter(), completion_mask: lldb::eWatchpointIDCompletion, request,
691 searcher: nullptr);
692 }
693
694 Options *GetOptions() override { return &m_options; }
695
696 class CommandOptions : public Options {
697 public:
698 CommandOptions() = default;
699
700 ~CommandOptions() override = default;
701
702 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
703 ExecutionContext *execution_context) override {
704 Status error;
705 const int short_option = m_getopt_table[option_idx].val;
706
707 switch (short_option) {
708 case 'c':
709 m_condition = std::string(option_arg);
710 m_condition_passed = true;
711 break;
712 default:
713 llvm_unreachable("Unimplemented option");
714 }
715
716 return error;
717 }
718
719 void OptionParsingStarting(ExecutionContext *execution_context) override {
720 m_condition.clear();
721 m_condition_passed = false;
722 }
723
724 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
725 return llvm::ArrayRef(g_watchpoint_modify_options);
726 }
727
728 // Instance variables to hold the values for command options.
729
730 std::string m_condition;
731 bool m_condition_passed = false;
732 };
733
734protected:
735 void DoExecute(Args &command, CommandReturnObject &result) override {
736 Target *target = &GetSelectedTarget();
737 if (!CheckTargetForWatchpointOperations(target, result))
738 return;
739
740 std::unique_lock<std::recursive_mutex> lock;
741 target->GetWatchpointList().GetListMutex(lock);
742
743 const WatchpointList &watchpoints = target->GetWatchpointList();
744
745 size_t num_watchpoints = watchpoints.GetSize();
746
747 if (num_watchpoints == 0) {
748 result.AppendError(in_string: "No watchpoints exist to be modified.");
749 return;
750 }
751
752 if (command.GetArgumentCount() == 0) {
753 WatchpointSP watch_sp = target->GetLastCreatedWatchpoint();
754 watch_sp->SetCondition(m_options.m_condition.c_str());
755 result.SetStatus(eReturnStatusSuccessFinishNoResult);
756 } else {
757 // Particular watchpoints selected; set condition on them.
758 std::vector<uint32_t> wp_ids;
759 if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(
760 target, args&: command, wp_ids&: wp_ids)) {
761 result.AppendError(in_string: "Invalid watchpoints specification.");
762 return;
763 }
764
765 int count = 0;
766 const size_t size = wp_ids.size();
767 for (size_t i = 0; i < size; ++i) {
768 WatchpointSP watch_sp = watchpoints.FindByID(watchID: wp_ids[i]);
769 if (watch_sp) {
770 watch_sp->SetCondition(m_options.m_condition.c_str());
771 ++count;
772 }
773 }
774 result.AppendMessageWithFormat(format: "%d watchpoints modified.\n", count);
775 result.SetStatus(eReturnStatusSuccessFinishNoResult);
776 }
777 }
778
779private:
780 CommandOptions m_options;
781};
782
783// CommandObjectWatchpointSetVariable
784#pragma mark SetVariable
785
786class CommandObjectWatchpointSetVariable : public CommandObjectParsed {
787public:
788 CommandObjectWatchpointSetVariable(CommandInterpreter &interpreter)
789 : CommandObjectParsed(
790 interpreter, "watchpoint set variable",
791 "Set a watchpoint on a variable. "
792 "Use the '-w' option to specify the type of watchpoint and "
793 "the '-s' option to specify the byte size to watch for. "
794 "If no '-w' option is specified, it defaults to modify. "
795 "If no '-s' option is specified, it defaults to the variable's "
796 "byte size. "
797 "Note that there are limited hardware resources for watchpoints. "
798 "If watchpoint setting fails, consider disable/delete existing "
799 "ones "
800 "to free up resources.",
801 nullptr,
802 eCommandRequiresFrame | eCommandTryTargetAPILock |
803 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) {
804 SetHelpLong(
805 R"(
806Examples:
807
808(lldb) watchpoint set variable -w read_write my_global_var
809
810)"
811 " Watches my_global_var for read/write access, with the region to watch \
812corresponding to the byte size of the data type.");
813
814 CommandArgumentEntry arg;
815 CommandArgumentData var_name_arg;
816
817 // Define the only variant of this arg.
818 var_name_arg.arg_type = eArgTypeVarName;
819 var_name_arg.arg_repetition = eArgRepeatPlain;
820
821 // Push the variant into the argument entry.
822 arg.push_back(x: var_name_arg);
823
824 // Push the data for the only argument into the m_arguments vector.
825 m_arguments.push_back(x: arg);
826
827 // Absorb the '-w' and '-s' options into our option group.
828 m_option_group.Append(group: &m_option_watchpoint, LLDB_OPT_SET_1, LLDB_OPT_SET_1);
829 m_option_group.Finalize();
830 }
831
832 ~CommandObjectWatchpointSetVariable() override = default;
833
834 void
835 HandleArgumentCompletion(CompletionRequest &request,
836 OptionElementVector &opt_element_vector) override {
837 if (request.GetCursorIndex() != 0)
838 return;
839 lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
840 interpreter&: GetCommandInterpreter(), completion_mask: lldb::eVariablePathCompletion, request,
841 searcher: nullptr);
842 }
843
844 Options *GetOptions() override { return &m_option_group; }
845
846protected:
847 static size_t GetVariableCallback(void *baton, const char *name,
848 VariableList &variable_list) {
849 size_t old_size = variable_list.GetSize();
850 Target *target = static_cast<Target *>(baton);
851 if (target)
852 target->GetImages().FindGlobalVariables(name: ConstString(name), UINT32_MAX,
853 variable_list);
854 return variable_list.GetSize() - old_size;
855 }
856
857 void DoExecute(Args &command, CommandReturnObject &result) override {
858 Target *target = GetDebugger().GetSelectedTarget().get();
859 StackFrame *frame = m_exe_ctx.GetFramePtr();
860
861 // If no argument is present, issue an error message. There's no way to
862 // set a watchpoint.
863 if (command.GetArgumentCount() <= 0) {
864 result.AppendError(in_string: "required argument missing; "
865 "specify your program variable to watch for");
866 return;
867 }
868
869 // If no '-w' is specified, default to '-w modify'.
870 if (!m_option_watchpoint.watch_type_specified) {
871 m_option_watchpoint.watch_type = OptionGroupWatchpoint::eWatchModify;
872 }
873
874 // We passed the sanity check for the command. Proceed to set the
875 // watchpoint now.
876 lldb::addr_t addr = 0;
877 size_t size = 0;
878
879 VariableSP var_sp;
880 ValueObjectSP valobj_sp;
881 Stream &output_stream = result.GetOutputStream();
882
883 // A simple watch variable gesture allows only one argument.
884 if (command.GetArgumentCount() != 1) {
885 result.AppendError(in_string: "specify exactly one variable to watch for");
886 return;
887 }
888
889 // Things have checked out ok...
890 Status error;
891 uint32_t expr_path_options =
892 StackFrame::eExpressionPathOptionCheckPtrVsMember |
893 StackFrame::eExpressionPathOptionsAllowDirectIVarAccess;
894 valobj_sp = frame->GetValueForVariableExpressionPath(
895 var_expr: command.GetArgumentAtIndex(idx: 0), use_dynamic: eNoDynamicValues, options: expr_path_options,
896 var_sp, error);
897
898 if (!valobj_sp) {
899 // Not in the frame; let's check the globals.
900
901 VariableList variable_list;
902 ValueObjectList valobj_list;
903
904 Status error(Variable::GetValuesForVariableExpressionPath(
905 variable_expr_path: command.GetArgumentAtIndex(idx: 0),
906 scope: m_exe_ctx.GetBestExecutionContextScope(), callback: GetVariableCallback, baton: target,
907 variable_list, valobj_list));
908
909 if (valobj_list.GetSize())
910 valobj_sp = valobj_list.GetValueObjectAtIndex(idx: 0);
911 }
912
913 CompilerType compiler_type;
914
915 if (valobj_sp) {
916 AddressType addr_type;
917 addr = valobj_sp->GetAddressOf(scalar_is_load_address: false, address_type: &addr_type);
918 if (addr_type == eAddressTypeLoad) {
919 // We're in business.
920 // Find out the size of this variable.
921 size = m_option_watchpoint.watch_size.GetCurrentValue() == 0
922 ? valobj_sp->GetByteSize().value_or(u: 0)
923 : m_option_watchpoint.watch_size.GetCurrentValue();
924 }
925 compiler_type = valobj_sp->GetCompilerType();
926 } else {
927 const char *error_cstr = error.AsCString(default_error_str: nullptr);
928 if (error_cstr)
929 result.AppendError(in_string: error_cstr);
930 else
931 result.AppendErrorWithFormat(format: "unable to find any variable "
932 "expression path that matches '%s'",
933 command.GetArgumentAtIndex(idx: 0));
934 return;
935 }
936
937 // Now it's time to create the watchpoint.
938 uint32_t watch_type = 0;
939 switch (m_option_watchpoint.watch_type) {
940 case OptionGroupWatchpoint::eWatchModify:
941 watch_type |= LLDB_WATCH_TYPE_MODIFY;
942 break;
943 case OptionGroupWatchpoint::eWatchRead:
944 watch_type |= LLDB_WATCH_TYPE_READ;
945 break;
946 case OptionGroupWatchpoint::eWatchReadWrite:
947 watch_type |= LLDB_WATCH_TYPE_READ | LLDB_WATCH_TYPE_WRITE;
948 break;
949 case OptionGroupWatchpoint::eWatchWrite:
950 watch_type |= LLDB_WATCH_TYPE_WRITE;
951 break;
952 case OptionGroupWatchpoint::eWatchInvalid:
953 break;
954 };
955
956 error.Clear();
957 WatchpointSP watch_sp =
958 target->CreateWatchpoint(addr, size, type: &compiler_type, kind: watch_type, error);
959 if (!watch_sp) {
960 result.AppendErrorWithFormat(
961 format: "Watchpoint creation failed (addr=0x%" PRIx64 ", size=%" PRIu64
962 ", variable expression='%s').\n",
963 addr, static_cast<uint64_t>(size), command.GetArgumentAtIndex(idx: 0));
964 if (const char *error_message = error.AsCString(default_error_str: nullptr))
965 result.AppendError(in_string: error_message);
966 return;
967 }
968
969 watch_sp->SetWatchSpec(command.GetArgumentAtIndex(idx: 0));
970 watch_sp->SetWatchVariable(true);
971 if (var_sp) {
972 if (var_sp->GetDeclaration().GetFile()) {
973 StreamString ss;
974 // True to show fullpath for declaration file.
975 var_sp->GetDeclaration().DumpStopContext(s: &ss, show_fullpaths: true);
976 watch_sp->SetDeclInfo(std::string(ss.GetString()));
977 }
978 if (var_sp->GetScope() == eValueTypeVariableLocal)
979 watch_sp->SetupVariableWatchpointDisabler(m_exe_ctx.GetFrameSP());
980 }
981 output_stream.Printf(format: "Watchpoint created: ");
982 watch_sp->GetDescription(s: &output_stream, level: lldb::eDescriptionLevelFull);
983 output_stream.EOL();
984 result.SetStatus(eReturnStatusSuccessFinishResult);
985 }
986
987private:
988 OptionGroupOptions m_option_group;
989 OptionGroupWatchpoint m_option_watchpoint;
990};
991
992// CommandObjectWatchpointSetExpression
993#pragma mark Set
994
995class CommandObjectWatchpointSetExpression : public CommandObjectRaw {
996public:
997 CommandObjectWatchpointSetExpression(CommandInterpreter &interpreter)
998 : CommandObjectRaw(
999 interpreter, "watchpoint set expression",
1000 "Set a watchpoint on an address by supplying an expression. "
1001 "Use the '-l' option to specify the language of the expression. "
1002 "Use the '-w' option to specify the type of watchpoint and "
1003 "the '-s' option to specify the byte size to watch for. "
1004 "If no '-w' option is specified, it defaults to modify. "
1005 "If no '-s' option is specified, it defaults to the target's "
1006 "pointer byte size. "
1007 "Note that there are limited hardware resources for watchpoints. "
1008 "If watchpoint setting fails, consider disable/delete existing "
1009 "ones "
1010 "to free up resources.",
1011 "",
1012 eCommandRequiresFrame | eCommandTryTargetAPILock |
1013 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused) {
1014 SetHelpLong(
1015 R"(
1016Examples:
1017
1018(lldb) watchpoint set expression -w modify -s 1 -- foo + 32
1019
1020 Watches write access for the 1-byte region pointed to by the address 'foo + 32')");
1021
1022 CommandArgumentEntry arg;
1023 CommandArgumentData expression_arg;
1024
1025 // Define the only variant of this arg.
1026 expression_arg.arg_type = eArgTypeExpression;
1027 expression_arg.arg_repetition = eArgRepeatPlain;
1028
1029 // Push the only variant into the argument entry.
1030 arg.push_back(x: expression_arg);
1031
1032 // Push the data for the only argument into the m_arguments vector.
1033 m_arguments.push_back(x: arg);
1034
1035 // Absorb the '-w' and '-s' options into our option group.
1036 m_option_group.Append(group: &m_option_watchpoint, LLDB_OPT_SET_ALL,
1037 LLDB_OPT_SET_1);
1038 m_option_group.Finalize();
1039 }
1040
1041 ~CommandObjectWatchpointSetExpression() override = default;
1042
1043 // Overrides base class's behavior where WantsCompletion =
1044 // !WantsRawCommandString.
1045 bool WantsCompletion() override { return true; }
1046
1047 Options *GetOptions() override { return &m_option_group; }
1048
1049protected:
1050 void DoExecute(llvm::StringRef raw_command,
1051 CommandReturnObject &result) override {
1052 auto exe_ctx = GetCommandInterpreter().GetExecutionContext();
1053 m_option_group.NotifyOptionParsingStarting(
1054 execution_context: &exe_ctx); // This is a raw command, so notify the option group
1055
1056 Target *target = GetDebugger().GetSelectedTarget().get();
1057 StackFrame *frame = m_exe_ctx.GetFramePtr();
1058
1059 OptionsWithRaw args(raw_command);
1060
1061 llvm::StringRef expr = args.GetRawPart();
1062
1063 if (args.HasArgs())
1064 if (!ParseOptionsAndNotify(args&: args.GetArgs(), result, group_options&: m_option_group,
1065 exe_ctx))
1066 return;
1067
1068 // If no argument is present, issue an error message. There's no way to
1069 // set a watchpoint.
1070 if (raw_command.trim().empty()) {
1071 result.AppendError(in_string: "required argument missing; specify an expression "
1072 "to evaluate into the address to watch for");
1073 return;
1074 }
1075
1076 // If no '-w' is specified, default to '-w write'.
1077 if (!m_option_watchpoint.watch_type_specified) {
1078 m_option_watchpoint.watch_type = OptionGroupWatchpoint::eWatchModify;
1079 }
1080
1081 // We passed the sanity check for the command. Proceed to set the
1082 // watchpoint now.
1083 lldb::addr_t addr = 0;
1084 size_t size = 0;
1085
1086 ValueObjectSP valobj_sp;
1087
1088 // Use expression evaluation to arrive at the address to watch.
1089 EvaluateExpressionOptions options;
1090 options.SetCoerceToId(false);
1091 options.SetUnwindOnError(true);
1092 options.SetKeepInMemory(false);
1093 options.SetTryAllThreads(true);
1094 options.SetTimeout(std::nullopt);
1095 if (m_option_watchpoint.language_type != eLanguageTypeUnknown)
1096 options.SetLanguage(m_option_watchpoint.language_type);
1097
1098 ExpressionResults expr_result =
1099 target->EvaluateExpression(expression: expr, exe_scope: frame, result_valobj_sp&: valobj_sp, options);
1100 if (expr_result != eExpressionCompleted) {
1101 result.AppendError(in_string: "expression evaluation of address to watch failed");
1102 result.AppendErrorWithFormat(format: "expression evaluated: \n%s", expr.data());
1103 if (valobj_sp && !valobj_sp->GetError().Success())
1104 result.AppendError(in_string: valobj_sp->GetError().AsCString());
1105 return;
1106 }
1107
1108 // Get the address to watch.
1109 bool success = false;
1110 addr = valobj_sp->GetValueAsUnsigned(fail_value: 0, success: &success);
1111 if (!success) {
1112 result.AppendError(in_string: "expression did not evaluate to an address");
1113 return;
1114 }
1115
1116 if (m_option_watchpoint.watch_size.GetCurrentValue() != 0)
1117 size = m_option_watchpoint.watch_size.GetCurrentValue();
1118 else
1119 size = target->GetArchitecture().GetAddressByteSize();
1120
1121 // Now it's time to create the watchpoint.
1122 uint32_t watch_type;
1123 switch (m_option_watchpoint.watch_type) {
1124 case OptionGroupWatchpoint::eWatchRead:
1125 watch_type = LLDB_WATCH_TYPE_READ;
1126 break;
1127 case OptionGroupWatchpoint::eWatchWrite:
1128 watch_type = LLDB_WATCH_TYPE_WRITE;
1129 break;
1130 case OptionGroupWatchpoint::eWatchModify:
1131 watch_type = LLDB_WATCH_TYPE_MODIFY;
1132 break;
1133 case OptionGroupWatchpoint::eWatchReadWrite:
1134 watch_type = LLDB_WATCH_TYPE_READ | LLDB_WATCH_TYPE_WRITE;
1135 break;
1136 default:
1137 watch_type = LLDB_WATCH_TYPE_MODIFY;
1138 }
1139
1140 // Fetch the type from the value object, the type of the watched object is
1141 // the pointee type
1142 /// of the expression, so convert to that if we found a valid type.
1143 CompilerType compiler_type(valobj_sp->GetCompilerType());
1144
1145 std::optional<uint64_t> valobj_size = valobj_sp->GetByteSize();
1146 // Set the type as a uint8_t array if the size being watched is
1147 // larger than the ValueObject's size (which is probably the size
1148 // of a pointer).
1149 if (valobj_size && size > *valobj_size) {
1150 auto type_system = compiler_type.GetTypeSystem();
1151 if (type_system) {
1152 CompilerType clang_uint8_type =
1153 type_system->GetBuiltinTypeForEncodingAndBitSize(encoding: eEncodingUint, bit_size: 8);
1154 compiler_type = clang_uint8_type.GetArrayType(size);
1155 }
1156 }
1157
1158 Status error;
1159 WatchpointSP watch_sp =
1160 target->CreateWatchpoint(addr, size, type: &compiler_type, kind: watch_type, error);
1161 if (watch_sp) {
1162 watch_sp->SetWatchSpec(std::string(expr));
1163 Stream &output_stream = result.GetOutputStream();
1164 output_stream.Printf(format: "Watchpoint created: ");
1165 watch_sp->GetDescription(s: &output_stream, level: lldb::eDescriptionLevelFull);
1166 output_stream.EOL();
1167 result.SetStatus(eReturnStatusSuccessFinishResult);
1168 } else {
1169 result.AppendErrorWithFormat(format: "Watchpoint creation failed (addr=0x%" PRIx64
1170 ", size=%" PRIu64 ").\n",
1171 addr, (uint64_t)size);
1172 if (error.AsCString(default_error_str: nullptr))
1173 result.AppendError(in_string: error.AsCString());
1174 }
1175 }
1176
1177private:
1178 OptionGroupOptions m_option_group;
1179 OptionGroupWatchpoint m_option_watchpoint;
1180};
1181
1182// CommandObjectWatchpointSet
1183#pragma mark Set
1184
1185class CommandObjectWatchpointSet : public CommandObjectMultiword {
1186public:
1187 CommandObjectWatchpointSet(CommandInterpreter &interpreter)
1188 : CommandObjectMultiword(
1189 interpreter, "watchpoint set", "Commands for setting a watchpoint.",
1190 "watchpoint set <subcommand> [<subcommand-options>]") {
1191
1192 LoadSubCommand(
1193 cmd_name: "variable",
1194 command_obj: CommandObjectSP(new CommandObjectWatchpointSetVariable(interpreter)));
1195 LoadSubCommand(
1196 cmd_name: "expression",
1197 command_obj: CommandObjectSP(new CommandObjectWatchpointSetExpression(interpreter)));
1198 }
1199
1200 ~CommandObjectWatchpointSet() override = default;
1201};
1202
1203// CommandObjectMultiwordWatchpoint
1204#pragma mark MultiwordWatchpoint
1205
1206CommandObjectMultiwordWatchpoint::CommandObjectMultiwordWatchpoint(
1207 CommandInterpreter &interpreter)
1208 : CommandObjectMultiword(interpreter, "watchpoint",
1209 "Commands for operating on watchpoints.",
1210 "watchpoint <subcommand> [<command-options>]") {
1211 CommandObjectSP list_command_object(
1212 new CommandObjectWatchpointList(interpreter));
1213 CommandObjectSP enable_command_object(
1214 new CommandObjectWatchpointEnable(interpreter));
1215 CommandObjectSP disable_command_object(
1216 new CommandObjectWatchpointDisable(interpreter));
1217 CommandObjectSP delete_command_object(
1218 new CommandObjectWatchpointDelete(interpreter));
1219 CommandObjectSP ignore_command_object(
1220 new CommandObjectWatchpointIgnore(interpreter));
1221 CommandObjectSP command_command_object(
1222 new CommandObjectWatchpointCommand(interpreter));
1223 CommandObjectSP modify_command_object(
1224 new CommandObjectWatchpointModify(interpreter));
1225 CommandObjectSP set_command_object(
1226 new CommandObjectWatchpointSet(interpreter));
1227
1228 list_command_object->SetCommandName("watchpoint list");
1229 enable_command_object->SetCommandName("watchpoint enable");
1230 disable_command_object->SetCommandName("watchpoint disable");
1231 delete_command_object->SetCommandName("watchpoint delete");
1232 ignore_command_object->SetCommandName("watchpoint ignore");
1233 command_command_object->SetCommandName("watchpoint command");
1234 modify_command_object->SetCommandName("watchpoint modify");
1235 set_command_object->SetCommandName("watchpoint set");
1236
1237 LoadSubCommand(cmd_name: "list", command_obj: list_command_object);
1238 LoadSubCommand(cmd_name: "enable", command_obj: enable_command_object);
1239 LoadSubCommand(cmd_name: "disable", command_obj: disable_command_object);
1240 LoadSubCommand(cmd_name: "delete", command_obj: delete_command_object);
1241 LoadSubCommand(cmd_name: "ignore", command_obj: ignore_command_object);
1242 LoadSubCommand(cmd_name: "command", command_obj: command_command_object);
1243 LoadSubCommand(cmd_name: "modify", command_obj: modify_command_object);
1244 LoadSubCommand(cmd_name: "set", command_obj: set_command_object);
1245}
1246
1247CommandObjectMultiwordWatchpoint::~CommandObjectMultiwordWatchpoint() = default;
1248

source code of lldb/source/Commands/CommandObjectWatchpoint.cpp