1 //===-- CommandObjectWatchpoint.cpp -----------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "CommandObjectWatchpoint.h"
10 #include "CommandObjectWatchpointCommand.h"
14 #include "llvm/ADT/StringRef.h"
16 #include "lldb/Breakpoint/Watchpoint.h"
17 #include "lldb/Breakpoint/WatchpointList.h"
18 #include "lldb/Core/ValueObject.h"
19 #include "lldb/Host/OptionParser.h"
20 #include "lldb/Interpreter/CommandInterpreter.h"
21 #include "lldb/Interpreter/CommandReturnObject.h"
22 #include "lldb/Symbol/Variable.h"
23 #include "lldb/Symbol/VariableList.h"
24 #include "lldb/Target/StackFrame.h"
25 #include "lldb/Target/Target.h"
26 #include "lldb/Utility/StreamString.h"
29 using namespace lldb_private
;
31 static void AddWatchpointDescription(Stream
*s
, Watchpoint
*wp
,
32 lldb::DescriptionLevel level
) {
34 wp
->GetDescription(s
, level
);
39 static bool CheckTargetForWatchpointOperations(Target
*target
,
40 CommandReturnObject
&result
) {
41 bool process_is_valid
=
42 target
->GetProcessSP() && target
->GetProcessSP()->IsAlive();
43 if (!process_is_valid
) {
44 result
.AppendError("Thre's no process or it is not alive.");
45 result
.SetStatus(eReturnStatusFailed
);
48 // Target passes our checks, return true.
52 // Equivalent class: {"-", "to", "To", "TO"} of range specifier array.
53 static const char *RSA
[4] = {"-", "to", "To", "TO"};
55 // Return the index to RSA if found; otherwise -1 is returned.
56 static int32_t WithRSAIndex(llvm::StringRef Arg
) {
59 for (i
= 0; i
< 4; ++i
)
60 if (Arg
.find(RSA
[i
]) != llvm::StringRef::npos
)
65 // Return true if wp_ids is successfully populated with the watch ids. False
67 bool CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(
68 Target
*target
, Args
&args
, std::vector
<uint32_t> &wp_ids
) {
69 // Pre-condition: args.GetArgumentCount() > 0.
70 if (args
.GetArgumentCount() == 0) {
71 if (target
== nullptr)
73 WatchpointSP watch_sp
= target
->GetLastCreatedWatchpoint();
75 wp_ids
.push_back(watch_sp
->GetID());
81 llvm::StringRef
Minus("-");
82 std::vector
<llvm::StringRef
> StrRefArgs
;
83 llvm::StringRef first
;
84 llvm::StringRef second
;
87 // Go through the arguments and make a canonical form of arg list containing
88 // only numbers with possible "-" in between.
89 for (auto &entry
: args
.entries()) {
90 if ((idx
= WithRSAIndex(entry
.ref())) == -1) {
91 StrRefArgs
.push_back(entry
.ref());
94 // The Arg contains the range specifier, split it, then.
95 std::tie(first
, second
) = entry
.ref().split(RSA
[idx
]);
97 StrRefArgs
.push_back(first
);
98 StrRefArgs
.push_back(Minus
);
100 StrRefArgs
.push_back(second
);
102 // Now process the canonical list and fill in the vector of uint32_t's. If
103 // there is any error, return false and the client should ignore wp_ids.
104 uint32_t beg
, end
, id
;
105 size_t size
= StrRefArgs
.size();
106 bool in_range
= false;
107 for (i
= 0; i
< size
; ++i
) {
108 llvm::StringRef Arg
= StrRefArgs
[i
];
110 // Look for the 'end' of the range. Note StringRef::getAsInteger()
111 // returns true to signify error while parsing.
112 if (Arg
.getAsInteger(0, end
))
114 // Found a range! Now append the elements.
115 for (id
= beg
; id
<= end
; ++id
)
116 wp_ids
.push_back(id
);
120 if (i
< (size
- 1) && StrRefArgs
[i
+ 1] == Minus
) {
121 if (Arg
.getAsInteger(0, beg
))
123 // Turn on the in_range flag, we are looking for end of range next.
128 // Otherwise, we have a simple ID. Just append it.
129 if (Arg
.getAsInteger(0, beg
))
131 wp_ids
.push_back(beg
);
134 // It is an error if after the loop, we're still in_range.
138 // CommandObjectWatchpointList
140 // CommandObjectWatchpointList::Options
141 #pragma mark List::CommandOptions
142 #define LLDB_OPTIONS_watchpoint_list
143 #include "CommandOptions.inc"
147 class CommandObjectWatchpointList
: public CommandObjectParsed
{
149 CommandObjectWatchpointList(CommandInterpreter
&interpreter
)
150 : CommandObjectParsed(
151 interpreter
, "watchpoint list",
152 "List all watchpoints at configurable levels of detail.", nullptr,
153 eCommandRequiresTarget
),
155 CommandArgumentEntry arg
;
156 CommandObject::AddIDsArgumentData(arg
, eArgTypeWatchpointID
,
157 eArgTypeWatchpointIDRange
);
158 // Add the entry for the first argument for this command to the object's
160 m_arguments
.push_back(arg
);
163 ~CommandObjectWatchpointList() override
= default;
165 Options
*GetOptions() override
{ return &m_options
; }
167 class CommandOptions
: public Options
{
171 m_level(lldb::eDescriptionLevelBrief
) // Watchpoint List defaults to
172 // brief descriptions
175 ~CommandOptions() override
= default;
177 Status
SetOptionValue(uint32_t option_idx
, llvm::StringRef option_arg
,
178 ExecutionContext
*execution_context
) override
{
180 const int short_option
= m_getopt_table
[option_idx
].val
;
182 switch (short_option
) {
184 m_level
= lldb::eDescriptionLevelBrief
;
187 m_level
= lldb::eDescriptionLevelFull
;
190 m_level
= lldb::eDescriptionLevelVerbose
;
193 llvm_unreachable("Unimplemented option");
199 void OptionParsingStarting(ExecutionContext
*execution_context
) override
{
200 m_level
= lldb::eDescriptionLevelFull
;
203 llvm::ArrayRef
<OptionDefinition
> GetDefinitions() override
{
204 return llvm::makeArrayRef(g_watchpoint_list_options
);
207 // Instance variables to hold the values for command options.
209 lldb::DescriptionLevel m_level
;
213 bool DoExecute(Args
&command
, CommandReturnObject
&result
) override
{
214 Target
*target
= &GetSelectedTarget();
216 if (target
->GetProcessSP() && target
->GetProcessSP()->IsAlive()) {
217 uint32_t num_supported_hardware_watchpoints
;
218 Status error
= target
->GetProcessSP()->GetWatchpointSupportInfo(
219 num_supported_hardware_watchpoints
);
221 result
.AppendMessageWithFormat(
222 "Number of supported hardware watchpoints: %u\n",
223 num_supported_hardware_watchpoints
);
226 const WatchpointList
&watchpoints
= target
->GetWatchpointList();
228 std::unique_lock
<std::recursive_mutex
> lock
;
229 target
->GetWatchpointList().GetListMutex(lock
);
231 size_t num_watchpoints
= watchpoints
.GetSize();
233 if (num_watchpoints
== 0) {
234 result
.AppendMessage("No watchpoints currently set.");
235 result
.SetStatus(eReturnStatusSuccessFinishNoResult
);
239 Stream
&output_stream
= result
.GetOutputStream();
241 if (command
.GetArgumentCount() == 0) {
242 // No watchpoint selected; show info about all currently set watchpoints.
243 result
.AppendMessage("Current watchpoints:");
244 for (size_t i
= 0; i
< num_watchpoints
; ++i
) {
245 Watchpoint
*wp
= watchpoints
.GetByIndex(i
).get();
246 AddWatchpointDescription(&output_stream
, wp
, m_options
.m_level
);
248 result
.SetStatus(eReturnStatusSuccessFinishNoResult
);
250 // Particular watchpoints selected; enable them.
251 std::vector
<uint32_t> wp_ids
;
252 if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(
253 target
, command
, wp_ids
)) {
254 result
.AppendError("Invalid watchpoints specification.");
255 result
.SetStatus(eReturnStatusFailed
);
259 const size_t size
= wp_ids
.size();
260 for (size_t i
= 0; i
< size
; ++i
) {
261 Watchpoint
*wp
= watchpoints
.FindByID(wp_ids
[i
]).get();
263 AddWatchpointDescription(&output_stream
, wp
, m_options
.m_level
);
264 result
.SetStatus(eReturnStatusSuccessFinishNoResult
);
268 return result
.Succeeded();
272 CommandOptions m_options
;
275 // CommandObjectWatchpointEnable
278 class CommandObjectWatchpointEnable
: public CommandObjectParsed
{
280 CommandObjectWatchpointEnable(CommandInterpreter
&interpreter
)
281 : CommandObjectParsed(interpreter
, "enable",
282 "Enable the specified disabled watchpoint(s). If "
283 "no watchpoints are specified, enable all of them.",
284 nullptr, eCommandRequiresTarget
) {
285 CommandArgumentEntry arg
;
286 CommandObject::AddIDsArgumentData(arg
, eArgTypeWatchpointID
,
287 eArgTypeWatchpointIDRange
);
288 // Add the entry for the first argument for this command to the object's
290 m_arguments
.push_back(arg
);
293 ~CommandObjectWatchpointEnable() override
= default;
296 bool DoExecute(Args
&command
, CommandReturnObject
&result
) override
{
297 Target
*target
= &GetSelectedTarget();
298 if (!CheckTargetForWatchpointOperations(target
, result
))
301 std::unique_lock
<std::recursive_mutex
> lock
;
302 target
->GetWatchpointList().GetListMutex(lock
);
304 const WatchpointList
&watchpoints
= target
->GetWatchpointList();
306 size_t num_watchpoints
= watchpoints
.GetSize();
308 if (num_watchpoints
== 0) {
309 result
.AppendError("No watchpoints exist to be enabled.");
310 result
.SetStatus(eReturnStatusFailed
);
314 if (command
.GetArgumentCount() == 0) {
315 // No watchpoint selected; enable all currently set watchpoints.
316 target
->EnableAllWatchpoints();
317 result
.AppendMessageWithFormat("All watchpoints enabled. (%" PRIu64
319 (uint64_t)num_watchpoints
);
320 result
.SetStatus(eReturnStatusSuccessFinishNoResult
);
322 // Particular watchpoints selected; enable them.
323 std::vector
<uint32_t> wp_ids
;
324 if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(
325 target
, command
, wp_ids
)) {
326 result
.AppendError("Invalid watchpoints specification.");
327 result
.SetStatus(eReturnStatusFailed
);
332 const size_t size
= wp_ids
.size();
333 for (size_t i
= 0; i
< size
; ++i
)
334 if (target
->EnableWatchpointByID(wp_ids
[i
]))
336 result
.AppendMessageWithFormat("%d watchpoints enabled.\n", count
);
337 result
.SetStatus(eReturnStatusSuccessFinishNoResult
);
340 return result
.Succeeded();
344 // CommandObjectWatchpointDisable
347 class CommandObjectWatchpointDisable
: public CommandObjectParsed
{
349 CommandObjectWatchpointDisable(CommandInterpreter
&interpreter
)
350 : CommandObjectParsed(interpreter
, "watchpoint disable",
351 "Disable the specified watchpoint(s) without "
352 "removing it/them. If no watchpoints are "
353 "specified, disable them all.",
354 nullptr, eCommandRequiresTarget
) {
355 CommandArgumentEntry arg
;
356 CommandObject::AddIDsArgumentData(arg
, eArgTypeWatchpointID
,
357 eArgTypeWatchpointIDRange
);
358 // Add the entry for the first argument for this command to the object's
360 m_arguments
.push_back(arg
);
363 ~CommandObjectWatchpointDisable() override
= default;
366 bool DoExecute(Args
&command
, CommandReturnObject
&result
) override
{
367 Target
*target
= &GetSelectedTarget();
368 if (!CheckTargetForWatchpointOperations(target
, result
))
371 std::unique_lock
<std::recursive_mutex
> lock
;
372 target
->GetWatchpointList().GetListMutex(lock
);
374 const WatchpointList
&watchpoints
= target
->GetWatchpointList();
375 size_t num_watchpoints
= watchpoints
.GetSize();
377 if (num_watchpoints
== 0) {
378 result
.AppendError("No watchpoints exist to be disabled.");
379 result
.SetStatus(eReturnStatusFailed
);
383 if (command
.GetArgumentCount() == 0) {
384 // No watchpoint selected; disable all currently set watchpoints.
385 if (target
->DisableAllWatchpoints()) {
386 result
.AppendMessageWithFormat("All watchpoints disabled. (%" PRIu64
388 (uint64_t)num_watchpoints
);
389 result
.SetStatus(eReturnStatusSuccessFinishNoResult
);
391 result
.AppendError("Disable all watchpoints failed\n");
392 result
.SetStatus(eReturnStatusFailed
);
395 // Particular watchpoints selected; disable them.
396 std::vector
<uint32_t> wp_ids
;
397 if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(
398 target
, command
, wp_ids
)) {
399 result
.AppendError("Invalid watchpoints specification.");
400 result
.SetStatus(eReturnStatusFailed
);
405 const size_t size
= wp_ids
.size();
406 for (size_t i
= 0; i
< size
; ++i
)
407 if (target
->DisableWatchpointByID(wp_ids
[i
]))
409 result
.AppendMessageWithFormat("%d watchpoints disabled.\n", count
);
410 result
.SetStatus(eReturnStatusSuccessFinishNoResult
);
413 return result
.Succeeded();
417 // CommandObjectWatchpointDelete
418 #define LLDB_OPTIONS_watchpoint_delete
419 #include "CommandOptions.inc"
421 // CommandObjectWatchpointDelete
424 class CommandObjectWatchpointDelete
: public CommandObjectParsed
{
426 CommandObjectWatchpointDelete(CommandInterpreter
&interpreter
)
427 : CommandObjectParsed(interpreter
, "watchpoint delete",
428 "Delete the specified watchpoint(s). If no "
429 "watchpoints are specified, delete them all.",
430 nullptr, eCommandRequiresTarget
),
432 CommandArgumentEntry arg
;
433 CommandObject::AddIDsArgumentData(arg
, eArgTypeWatchpointID
,
434 eArgTypeWatchpointIDRange
);
435 // Add the entry for the first argument for this command to the object's
437 m_arguments
.push_back(arg
);
440 ~CommandObjectWatchpointDelete() override
= default;
442 Options
*GetOptions() override
{ return &m_options
; }
444 class CommandOptions
: public Options
{
446 CommandOptions() : Options(), m_force(false) {}
448 ~CommandOptions() override
= default;
450 Status
SetOptionValue(uint32_t option_idx
, llvm::StringRef option_arg
,
451 ExecutionContext
*execution_context
) override
{
452 const int short_option
= m_getopt_table
[option_idx
].val
;
454 switch (short_option
) {
459 llvm_unreachable("Unimplemented option");
465 void OptionParsingStarting(ExecutionContext
*execution_context
) override
{
469 llvm::ArrayRef
<OptionDefinition
> GetDefinitions() override
{
470 return llvm::makeArrayRef(g_watchpoint_delete_options
);
473 // Instance variables to hold the values for command options.
478 bool DoExecute(Args
&command
, CommandReturnObject
&result
) override
{
479 Target
*target
= &GetSelectedTarget();
480 if (!CheckTargetForWatchpointOperations(target
, result
))
483 std::unique_lock
<std::recursive_mutex
> lock
;
484 target
->GetWatchpointList().GetListMutex(lock
);
486 const WatchpointList
&watchpoints
= target
->GetWatchpointList();
488 size_t num_watchpoints
= watchpoints
.GetSize();
490 if (num_watchpoints
== 0) {
491 result
.AppendError("No watchpoints exist to be deleted.");
492 result
.SetStatus(eReturnStatusFailed
);
496 if (command
.empty()) {
497 if (!m_options
.m_force
&&
498 !m_interpreter
.Confirm(
499 "About to delete all watchpoints, do you want to do that?",
501 result
.AppendMessage("Operation cancelled...");
503 target
->RemoveAllWatchpoints();
504 result
.AppendMessageWithFormat("All watchpoints removed. (%" PRIu64
506 (uint64_t)num_watchpoints
);
508 result
.SetStatus(eReturnStatusSuccessFinishNoResult
);
509 return result
.Succeeded();
512 // Particular watchpoints selected; delete them.
513 std::vector
<uint32_t> wp_ids
;
514 if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(target
, command
,
516 result
.AppendError("Invalid watchpoints specification.");
517 result
.SetStatus(eReturnStatusFailed
);
522 const size_t size
= wp_ids
.size();
523 for (size_t i
= 0; i
< size
; ++i
)
524 if (target
->RemoveWatchpointByID(wp_ids
[i
]))
526 result
.AppendMessageWithFormat("%d watchpoints deleted.\n", count
);
527 result
.SetStatus(eReturnStatusSuccessFinishNoResult
);
529 return result
.Succeeded();
533 CommandOptions m_options
;
536 // CommandObjectWatchpointIgnore
538 #pragma mark Ignore::CommandOptions
539 #define LLDB_OPTIONS_watchpoint_ignore
540 #include "CommandOptions.inc"
542 class CommandObjectWatchpointIgnore
: public CommandObjectParsed
{
544 CommandObjectWatchpointIgnore(CommandInterpreter
&interpreter
)
545 : CommandObjectParsed(interpreter
, "watchpoint ignore",
546 "Set ignore count on the specified watchpoint(s). "
547 "If no watchpoints are specified, set them all.",
548 nullptr, eCommandRequiresTarget
),
550 CommandArgumentEntry arg
;
551 CommandObject::AddIDsArgumentData(arg
, eArgTypeWatchpointID
,
552 eArgTypeWatchpointIDRange
);
553 // Add the entry for the first argument for this command to the object's
555 m_arguments
.push_back(arg
);
558 ~CommandObjectWatchpointIgnore() override
= default;
560 Options
*GetOptions() override
{ return &m_options
; }
562 class CommandOptions
: public Options
{
564 CommandOptions() : Options(), m_ignore_count(0) {}
566 ~CommandOptions() override
= default;
568 Status
SetOptionValue(uint32_t option_idx
, llvm::StringRef option_arg
,
569 ExecutionContext
*execution_context
) override
{
571 const int short_option
= m_getopt_table
[option_idx
].val
;
573 switch (short_option
) {
575 if (option_arg
.getAsInteger(0, m_ignore_count
))
576 error
.SetErrorStringWithFormat("invalid ignore count '%s'",
577 option_arg
.str().c_str());
580 llvm_unreachable("Unimplemented option");
586 void OptionParsingStarting(ExecutionContext
*execution_context
) override
{
590 llvm::ArrayRef
<OptionDefinition
> GetDefinitions() override
{
591 return llvm::makeArrayRef(g_watchpoint_ignore_options
);
594 // Instance variables to hold the values for command options.
596 uint32_t m_ignore_count
;
600 bool DoExecute(Args
&command
, CommandReturnObject
&result
) override
{
601 Target
*target
= &GetSelectedTarget();
602 if (!CheckTargetForWatchpointOperations(target
, result
))
605 std::unique_lock
<std::recursive_mutex
> lock
;
606 target
->GetWatchpointList().GetListMutex(lock
);
608 const WatchpointList
&watchpoints
= target
->GetWatchpointList();
610 size_t num_watchpoints
= watchpoints
.GetSize();
612 if (num_watchpoints
== 0) {
613 result
.AppendError("No watchpoints exist to be ignored.");
614 result
.SetStatus(eReturnStatusFailed
);
618 if (command
.GetArgumentCount() == 0) {
619 target
->IgnoreAllWatchpoints(m_options
.m_ignore_count
);
620 result
.AppendMessageWithFormat("All watchpoints ignored. (%" PRIu64
622 (uint64_t)num_watchpoints
);
623 result
.SetStatus(eReturnStatusSuccessFinishNoResult
);
625 // Particular watchpoints selected; ignore them.
626 std::vector
<uint32_t> wp_ids
;
627 if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(
628 target
, command
, wp_ids
)) {
629 result
.AppendError("Invalid watchpoints specification.");
630 result
.SetStatus(eReturnStatusFailed
);
635 const size_t size
= wp_ids
.size();
636 for (size_t i
= 0; i
< size
; ++i
)
637 if (target
->IgnoreWatchpointByID(wp_ids
[i
], m_options
.m_ignore_count
))
639 result
.AppendMessageWithFormat("%d watchpoints ignored.\n", count
);
640 result
.SetStatus(eReturnStatusSuccessFinishNoResult
);
643 return result
.Succeeded();
647 CommandOptions m_options
;
650 // CommandObjectWatchpointModify
652 #pragma mark Modify::CommandOptions
653 #define LLDB_OPTIONS_watchpoint_modify
654 #include "CommandOptions.inc"
658 class CommandObjectWatchpointModify
: public CommandObjectParsed
{
660 CommandObjectWatchpointModify(CommandInterpreter
&interpreter
)
661 : CommandObjectParsed(
662 interpreter
, "watchpoint modify",
663 "Modify the options on a watchpoint or set of watchpoints in the "
665 "If no watchpoint is specified, act on the last created "
667 "Passing an empty argument clears the modification.",
668 nullptr, eCommandRequiresTarget
),
670 CommandArgumentEntry arg
;
671 CommandObject::AddIDsArgumentData(arg
, eArgTypeWatchpointID
,
672 eArgTypeWatchpointIDRange
);
673 // Add the entry for the first argument for this command to the object's
675 m_arguments
.push_back(arg
);
678 ~CommandObjectWatchpointModify() override
= default;
680 Options
*GetOptions() override
{ return &m_options
; }
682 class CommandOptions
: public Options
{
684 CommandOptions() : Options(), m_condition(), m_condition_passed(false) {}
686 ~CommandOptions() override
= default;
688 Status
SetOptionValue(uint32_t option_idx
, llvm::StringRef option_arg
,
689 ExecutionContext
*execution_context
) override
{
691 const int short_option
= m_getopt_table
[option_idx
].val
;
693 switch (short_option
) {
695 m_condition
= option_arg
;
696 m_condition_passed
= true;
699 llvm_unreachable("Unimplemented option");
705 void OptionParsingStarting(ExecutionContext
*execution_context
) override
{
707 m_condition_passed
= false;
710 llvm::ArrayRef
<OptionDefinition
> GetDefinitions() override
{
711 return llvm::makeArrayRef(g_watchpoint_modify_options
);
714 // Instance variables to hold the values for command options.
716 std::string m_condition
;
717 bool m_condition_passed
;
721 bool DoExecute(Args
&command
, CommandReturnObject
&result
) override
{
722 Target
*target
= &GetSelectedTarget();
723 if (!CheckTargetForWatchpointOperations(target
, result
))
726 std::unique_lock
<std::recursive_mutex
> lock
;
727 target
->GetWatchpointList().GetListMutex(lock
);
729 const WatchpointList
&watchpoints
= target
->GetWatchpointList();
731 size_t num_watchpoints
= watchpoints
.GetSize();
733 if (num_watchpoints
== 0) {
734 result
.AppendError("No watchpoints exist to be modified.");
735 result
.SetStatus(eReturnStatusFailed
);
739 if (command
.GetArgumentCount() == 0) {
740 WatchpointSP wp_sp
= target
->GetLastCreatedWatchpoint();
741 wp_sp
->SetCondition(m_options
.m_condition
.c_str());
742 result
.SetStatus(eReturnStatusSuccessFinishNoResult
);
744 // Particular watchpoints selected; set condition on them.
745 std::vector
<uint32_t> wp_ids
;
746 if (!CommandObjectMultiwordWatchpoint::VerifyWatchpointIDs(
747 target
, command
, wp_ids
)) {
748 result
.AppendError("Invalid watchpoints specification.");
749 result
.SetStatus(eReturnStatusFailed
);
754 const size_t size
= wp_ids
.size();
755 for (size_t i
= 0; i
< size
; ++i
) {
756 WatchpointSP wp_sp
= watchpoints
.FindByID(wp_ids
[i
]);
758 wp_sp
->SetCondition(m_options
.m_condition
.c_str());
762 result
.AppendMessageWithFormat("%d watchpoints modified.\n", count
);
763 result
.SetStatus(eReturnStatusSuccessFinishNoResult
);
766 return result
.Succeeded();
770 CommandOptions m_options
;
773 // CommandObjectWatchpointSetVariable
774 #pragma mark SetVariable
776 class CommandObjectWatchpointSetVariable
: public CommandObjectParsed
{
778 CommandObjectWatchpointSetVariable(CommandInterpreter
&interpreter
)
779 : CommandObjectParsed(
780 interpreter
, "watchpoint set variable",
781 "Set a watchpoint on a variable. "
782 "Use the '-w' option to specify the type of watchpoint and "
783 "the '-s' option to specify the byte size to watch for. "
784 "If no '-w' option is specified, it defaults to write. "
785 "If no '-s' option is specified, it defaults to the variable's "
787 "Note that there are limited hardware resources for watchpoints. "
788 "If watchpoint setting fails, consider disable/delete existing "
790 "to free up resources.",
792 eCommandRequiresFrame
| eCommandTryTargetAPILock
|
793 eCommandProcessMustBeLaunched
| eCommandProcessMustBePaused
),
794 m_option_group(), m_option_watchpoint() {
799 (lldb) watchpoint set variable -w read_write my_global_var
802 " Watches my_global_var for read/write access, with the region to watch \
803 corresponding to the byte size of the data type.");
805 CommandArgumentEntry arg
;
806 CommandArgumentData var_name_arg
;
808 // Define the only variant of this arg.
809 var_name_arg
.arg_type
= eArgTypeVarName
;
810 var_name_arg
.arg_repetition
= eArgRepeatPlain
;
812 // Push the variant into the argument entry.
813 arg
.push_back(var_name_arg
);
815 // Push the data for the only argument into the m_arguments vector.
816 m_arguments
.push_back(arg
);
818 // Absorb the '-w' and '-s' options into our option group.
819 m_option_group
.Append(&m_option_watchpoint
, LLDB_OPT_SET_ALL
,
821 m_option_group
.Finalize();
824 ~CommandObjectWatchpointSetVariable() override
= default;
826 Options
*GetOptions() override
{ return &m_option_group
; }
829 static size_t GetVariableCallback(void *baton
, const char *name
,
830 VariableList
&variable_list
) {
831 size_t old_size
= variable_list
.GetSize();
832 Target
*target
= static_cast<Target
*>(baton
);
834 target
->GetImages().FindGlobalVariables(ConstString(name
), UINT32_MAX
,
836 return variable_list
.GetSize() - old_size
;
839 bool DoExecute(Args
&command
, CommandReturnObject
&result
) override
{
840 Target
*target
= GetDebugger().GetSelectedTarget().get();
841 StackFrame
*frame
= m_exe_ctx
.GetFramePtr();
843 // If no argument is present, issue an error message. There's no way to
845 if (command
.GetArgumentCount() <= 0) {
846 result
.GetErrorStream().Printf("error: required argument missing; "
847 "specify your program variable to watch "
849 result
.SetStatus(eReturnStatusFailed
);
853 // If no '-w' is specified, default to '-w write'.
854 if (!m_option_watchpoint
.watch_type_specified
) {
855 m_option_watchpoint
.watch_type
= OptionGroupWatchpoint::eWatchWrite
;
858 // We passed the sanity check for the command. Proceed to set the
860 lldb::addr_t addr
= 0;
864 ValueObjectSP valobj_sp
;
865 Stream
&output_stream
= result
.GetOutputStream();
867 // A simple watch variable gesture allows only one argument.
868 if (command
.GetArgumentCount() != 1) {
869 result
.GetErrorStream().Printf(
870 "error: specify exactly one variable to watch for\n");
871 result
.SetStatus(eReturnStatusFailed
);
875 // Things have checked out ok...
877 uint32_t expr_path_options
=
878 StackFrame::eExpressionPathOptionCheckPtrVsMember
|
879 StackFrame::eExpressionPathOptionsAllowDirectIVarAccess
;
880 valobj_sp
= frame
->GetValueForVariableExpressionPath(
881 command
.GetArgumentAtIndex(0), eNoDynamicValues
, expr_path_options
,
885 // Not in the frame; let's check the globals.
887 VariableList variable_list
;
888 ValueObjectList valobj_list
;
890 Status
error(Variable::GetValuesForVariableExpressionPath(
891 command
.GetArgumentAtIndex(0),
892 m_exe_ctx
.GetBestExecutionContextScope(), GetVariableCallback
, target
,
893 variable_list
, valobj_list
));
895 if (valobj_list
.GetSize())
896 valobj_sp
= valobj_list
.GetValueObjectAtIndex(0);
899 CompilerType compiler_type
;
902 AddressType addr_type
;
903 addr
= valobj_sp
->GetAddressOf(false, &addr_type
);
904 if (addr_type
== eAddressTypeLoad
) {
905 // We're in business.
906 // Find out the size of this variable.
907 size
= m_option_watchpoint
.watch_size
== 0
908 ? valobj_sp
->GetByteSize()
909 : m_option_watchpoint
.watch_size
;
911 compiler_type
= valobj_sp
->GetCompilerType();
913 const char *error_cstr
= error
.AsCString(nullptr);
915 result
.GetErrorStream().Printf("error: %s\n", error_cstr
);
917 result
.GetErrorStream().Printf("error: unable to find any variable "
918 "expression path that matches '%s'\n",
919 command
.GetArgumentAtIndex(0));
923 // Now it's time to create the watchpoint.
924 uint32_t watch_type
= m_option_watchpoint
.watch_type
;
928 target
->CreateWatchpoint(addr
, size
, &compiler_type
, watch_type
, error
)
931 wp
->SetWatchSpec(command
.GetArgumentAtIndex(0));
932 wp
->SetWatchVariable(true);
933 if (var_sp
&& var_sp
->GetDeclaration().GetFile()) {
935 // True to show fullpath for declaration file.
936 var_sp
->GetDeclaration().DumpStopContext(&ss
, true);
937 wp
->SetDeclInfo(ss
.GetString());
939 output_stream
.Printf("Watchpoint created: ");
940 wp
->GetDescription(&output_stream
, lldb::eDescriptionLevelFull
);
942 result
.SetStatus(eReturnStatusSuccessFinishResult
);
944 result
.AppendErrorWithFormat(
945 "Watchpoint creation failed (addr=0x%" PRIx64
", size=%" PRIu64
946 ", variable expression='%s').\n",
947 addr
, (uint64_t)size
, command
.GetArgumentAtIndex(0));
948 if (error
.AsCString(nullptr))
949 result
.AppendError(error
.AsCString());
950 result
.SetStatus(eReturnStatusFailed
);
953 return result
.Succeeded();
957 OptionGroupOptions m_option_group
;
958 OptionGroupWatchpoint m_option_watchpoint
;
961 // CommandObjectWatchpointSetExpression
964 class CommandObjectWatchpointSetExpression
: public CommandObjectRaw
{
966 CommandObjectWatchpointSetExpression(CommandInterpreter
&interpreter
)
968 interpreter
, "watchpoint set expression",
969 "Set a watchpoint on an address by supplying an expression. "
970 "Use the '-w' option to specify the type of watchpoint and "
971 "the '-s' option to specify the byte size to watch for. "
972 "If no '-w' option is specified, it defaults to write. "
973 "If no '-s' option is specified, it defaults to the target's "
974 "pointer byte size. "
975 "Note that there are limited hardware resources for watchpoints. "
976 "If watchpoint setting fails, consider disable/delete existing "
978 "to free up resources.",
980 eCommandRequiresFrame
| eCommandTryTargetAPILock
|
981 eCommandProcessMustBeLaunched
| eCommandProcessMustBePaused
),
982 m_option_group(), m_option_watchpoint() {
987 (lldb) watchpoint set expression -w write -s 1 -- foo + 32
989 Watches write access for the 1-byte region pointed to by the address 'foo + 32')");
991 CommandArgumentEntry arg
;
992 CommandArgumentData expression_arg
;
994 // Define the only variant of this arg.
995 expression_arg
.arg_type
= eArgTypeExpression
;
996 expression_arg
.arg_repetition
= eArgRepeatPlain
;
998 // Push the only variant into the argument entry.
999 arg
.push_back(expression_arg
);
1001 // Push the data for the only argument into the m_arguments vector.
1002 m_arguments
.push_back(arg
);
1004 // Absorb the '-w' and '-s' options into our option group.
1005 m_option_group
.Append(&m_option_watchpoint
, LLDB_OPT_SET_ALL
,
1007 m_option_group
.Finalize();
1010 ~CommandObjectWatchpointSetExpression() override
= default;
1012 // Overrides base class's behavior where WantsCompletion =
1013 // !WantsRawCommandString.
1014 bool WantsCompletion() override
{ return true; }
1016 Options
*GetOptions() override
{ return &m_option_group
; }
1019 bool DoExecute(llvm::StringRef raw_command
,
1020 CommandReturnObject
&result
) override
{
1021 auto exe_ctx
= GetCommandInterpreter().GetExecutionContext();
1022 m_option_group
.NotifyOptionParsingStarting(
1023 &exe_ctx
); // This is a raw command, so notify the option group
1025 Target
*target
= GetDebugger().GetSelectedTarget().get();
1026 StackFrame
*frame
= m_exe_ctx
.GetFramePtr();
1028 OptionsWithRaw
args(raw_command
);
1030 llvm::StringRef expr
= args
.GetRawPart();
1033 if (!ParseOptionsAndNotify(args
.GetArgs(), result
, m_option_group
,
1037 // If no argument is present, issue an error message. There's no way to
1038 // set a watchpoint.
1039 if (raw_command
.trim().empty()) {
1040 result
.GetErrorStream().Printf("error: required argument missing; "
1041 "specify an expression to evaulate into "
1042 "the address to watch for\n");
1043 result
.SetStatus(eReturnStatusFailed
);
1047 // If no '-w' is specified, default to '-w write'.
1048 if (!m_option_watchpoint
.watch_type_specified
) {
1049 m_option_watchpoint
.watch_type
= OptionGroupWatchpoint::eWatchWrite
;
1052 // We passed the sanity check for the command. Proceed to set the
1054 lldb::addr_t addr
= 0;
1057 ValueObjectSP valobj_sp
;
1059 // Use expression evaluation to arrive at the address to watch.
1060 EvaluateExpressionOptions options
;
1061 options
.SetCoerceToId(false);
1062 options
.SetUnwindOnError(true);
1063 options
.SetKeepInMemory(false);
1064 options
.SetTryAllThreads(true);
1065 options
.SetTimeout(llvm::None
);
1067 ExpressionResults expr_result
=
1068 target
->EvaluateExpression(expr
, frame
, valobj_sp
, options
);
1069 if (expr_result
!= eExpressionCompleted
) {
1070 result
.GetErrorStream().Printf(
1071 "error: expression evaluation of address to watch failed\n");
1072 result
.GetErrorStream() << "expression evaluated: \n" << expr
<< "\n";
1073 result
.SetStatus(eReturnStatusFailed
);
1077 // Get the address to watch.
1078 bool success
= false;
1079 addr
= valobj_sp
->GetValueAsUnsigned(0, &success
);
1081 result
.GetErrorStream().Printf(
1082 "error: expression did not evaluate to an address\n");
1083 result
.SetStatus(eReturnStatusFailed
);
1087 if (m_option_watchpoint
.watch_size
!= 0)
1088 size
= m_option_watchpoint
.watch_size
;
1090 size
= target
->GetArchitecture().GetAddressByteSize();
1092 // Now it's time to create the watchpoint.
1093 uint32_t watch_type
= m_option_watchpoint
.watch_type
;
1095 // Fetch the type from the value object, the type of the watched object is
1097 /// of the expression, so convert to that if we found a valid type.
1098 CompilerType
compiler_type(valobj_sp
->GetCompilerType());
1102 target
->CreateWatchpoint(addr
, size
, &compiler_type
, watch_type
, error
)
1105 Stream
&output_stream
= result
.GetOutputStream();
1106 output_stream
.Printf("Watchpoint created: ");
1107 wp
->GetDescription(&output_stream
, lldb::eDescriptionLevelFull
);
1108 output_stream
.EOL();
1109 result
.SetStatus(eReturnStatusSuccessFinishResult
);
1111 result
.AppendErrorWithFormat("Watchpoint creation failed (addr=0x%" PRIx64
1112 ", size=%" PRIu64
").\n",
1113 addr
, (uint64_t)size
);
1114 if (error
.AsCString(nullptr))
1115 result
.AppendError(error
.AsCString());
1116 result
.SetStatus(eReturnStatusFailed
);
1119 return result
.Succeeded();
1123 OptionGroupOptions m_option_group
;
1124 OptionGroupWatchpoint m_option_watchpoint
;
1127 // CommandObjectWatchpointSet
1130 class CommandObjectWatchpointSet
: public CommandObjectMultiword
{
1132 CommandObjectWatchpointSet(CommandInterpreter
&interpreter
)
1133 : CommandObjectMultiword(
1134 interpreter
, "watchpoint set", "Commands for setting a watchpoint.",
1135 "watchpoint set <subcommand> [<subcommand-options>]") {
1139 CommandObjectSP(new CommandObjectWatchpointSetVariable(interpreter
)));
1142 CommandObjectSP(new CommandObjectWatchpointSetExpression(interpreter
)));
1145 ~CommandObjectWatchpointSet() override
= default;
1148 // CommandObjectMultiwordWatchpoint
1149 #pragma mark MultiwordWatchpoint
1151 CommandObjectMultiwordWatchpoint::CommandObjectMultiwordWatchpoint(
1152 CommandInterpreter
&interpreter
)
1153 : CommandObjectMultiword(interpreter
, "watchpoint",
1154 "Commands for operating on watchpoints.",
1155 "watchpoint <subcommand> [<command-options>]") {
1156 CommandObjectSP
list_command_object(
1157 new CommandObjectWatchpointList(interpreter
));
1158 CommandObjectSP
enable_command_object(
1159 new CommandObjectWatchpointEnable(interpreter
));
1160 CommandObjectSP
disable_command_object(
1161 new CommandObjectWatchpointDisable(interpreter
));
1162 CommandObjectSP
delete_command_object(
1163 new CommandObjectWatchpointDelete(interpreter
));
1164 CommandObjectSP
ignore_command_object(
1165 new CommandObjectWatchpointIgnore(interpreter
));
1166 CommandObjectSP
command_command_object(
1167 new CommandObjectWatchpointCommand(interpreter
));
1168 CommandObjectSP
modify_command_object(
1169 new CommandObjectWatchpointModify(interpreter
));
1170 CommandObjectSP
set_command_object(
1171 new CommandObjectWatchpointSet(interpreter
));
1173 list_command_object
->SetCommandName("watchpoint list");
1174 enable_command_object
->SetCommandName("watchpoint enable");
1175 disable_command_object
->SetCommandName("watchpoint disable");
1176 delete_command_object
->SetCommandName("watchpoint delete");
1177 ignore_command_object
->SetCommandName("watchpoint ignore");
1178 command_command_object
->SetCommandName("watchpoint command");
1179 modify_command_object
->SetCommandName("watchpoint modify");
1180 set_command_object
->SetCommandName("watchpoint set");
1182 LoadSubCommand("list", list_command_object
);
1183 LoadSubCommand("enable", enable_command_object
);
1184 LoadSubCommand("disable", disable_command_object
);
1185 LoadSubCommand("delete", delete_command_object
);
1186 LoadSubCommand("ignore", ignore_command_object
);
1187 LoadSubCommand("command", command_command_object
);
1188 LoadSubCommand("modify", modify_command_object
);
1189 LoadSubCommand("set", set_command_object
);
1192 CommandObjectMultiwordWatchpoint::~CommandObjectMultiwordWatchpoint() = default;