blob: 0ac4576f723c0467ea10c0e9cd4de190a72e1ec7 [file] [log] [blame]
Johnny Chen01acfa72011-09-22 18:04:58 +00001//===-- CommandObjectWatchpoint.cpp -----------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "CommandObjectWatchpoint.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
Johnny Chenecd4feb2011-10-14 00:42:25 +000016#include "lldb/Breakpoint/Watchpoint.h"
17#include "lldb/Breakpoint/WatchpointList.h"
Johnny Chen01acfa72011-09-22 18:04:58 +000018#include "lldb/Core/StreamString.h"
Johnny Chen55a2d5a2012-01-30 21:46:17 +000019#include "lldb/Core/ValueObject.h"
20#include "lldb/Core/ValueObjectVariable.h"
Johnny Chen01acfa72011-09-22 18:04:58 +000021#include "lldb/Interpreter/CommandInterpreter.h"
22#include "lldb/Interpreter/CommandReturnObject.h"
Johnny Chen01acfa72011-09-22 18:04:58 +000023#include "lldb/Interpreter/CommandCompletions.h"
Johnny Chen55a2d5a2012-01-30 21:46:17 +000024#include "lldb/Symbol/Variable.h"
25#include "lldb/Symbol/VariableList.h"
26#include "lldb/Target/Target.h"
Johnny Chen01acfa72011-09-22 18:04:58 +000027
28#include <vector>
29
30using namespace lldb;
31using namespace lldb_private;
32
33static void
Johnny Chenecd4feb2011-10-14 00:42:25 +000034AddWatchpointDescription(Stream *s, Watchpoint *wp, lldb::DescriptionLevel level)
Johnny Chen01acfa72011-09-22 18:04:58 +000035{
36 s->IndentMore();
Johnny Chenecd4feb2011-10-14 00:42:25 +000037 wp->GetDescription(s, level);
Johnny Chen01acfa72011-09-22 18:04:58 +000038 s->IndentLess();
39 s->EOL();
40}
41
42static bool
43CheckTargetForWatchpointOperations(Target *target, CommandReturnObject &result)
44{
45 if (target == NULL)
46 {
47 result.AppendError ("Invalid target. No existing target or watchpoints.");
48 result.SetStatus (eReturnStatusFailed);
49 return false;
50 }
51 bool process_is_valid = target->GetProcessSP() && target->GetProcessSP()->IsAlive();
52 if (!process_is_valid)
53 {
54 result.AppendError ("Thre's no process or it is not alive.");
55 result.SetStatus (eReturnStatusFailed);
56 return false;
57 }
58 // Target passes our checks, return true.
59 return true;
60}
61
62#include "llvm/ADT/StringRef.h"
63
64// Equivalent class: {"-", "to", "To", "TO"} of range specifier array.
65static const char* RSA[4] = { "-", "to", "To", "TO" };
66
67// Return the index to RSA if found; otherwise -1 is returned.
68static int32_t
69WithRSAIndex(llvm::StringRef &Arg)
70{
71
72 uint32_t i;
73 for (i = 0; i < 4; ++i)
74 if (Arg.find(RSA[i]) != llvm::StringRef::npos)
75 return i;
76 return -1;
77}
78
79// Return true if wp_ids is successfully populated with the watch ids.
80// False otherwise.
81static bool
82VerifyWatchpointIDs(Args &args, std::vector<uint32_t> &wp_ids)
83{
84 // Pre-condition: args.GetArgumentCount() > 0.
85 assert(args.GetArgumentCount() > 0);
86
87 llvm::StringRef Minus("-");
88 std::vector<llvm::StringRef> StrRefArgs;
89 std::pair<llvm::StringRef, llvm::StringRef> Pair;
90 size_t i;
91 int32_t idx;
92 // Go through the argments and make a canonical form of arg list containing
93 // only numbers with possible "-" in between.
94 for (i = 0; i < args.GetArgumentCount(); ++i) {
95 llvm::StringRef Arg(args.GetArgumentAtIndex(i));
96 if ((idx = WithRSAIndex(Arg)) == -1) {
97 StrRefArgs.push_back(Arg);
98 continue;
99 }
100 // The Arg contains the range specifier, split it, then.
101 Pair = Arg.split(RSA[idx]);
102 if (!Pair.first.empty())
103 StrRefArgs.push_back(Pair.first);
104 StrRefArgs.push_back(Minus);
105 if (!Pair.second.empty())
106 StrRefArgs.push_back(Pair.second);
107 }
108 // Now process the canonical list and fill in the vector of uint32_t's.
109 // If there is any error, return false and the client should ignore wp_ids.
110 uint32_t beg, end, id;
111 size_t size = StrRefArgs.size();
112 bool in_range = false;
113 for (i = 0; i < size; ++i) {
114 llvm::StringRef Arg = StrRefArgs[i];
115 if (in_range) {
116 // Look for the 'end' of the range. Note StringRef::getAsInteger()
117 // returns true to signify error while parsing.
118 if (Arg.getAsInteger(0, end))
119 return false;
120 // Found a range! Now append the elements.
121 for (id = beg; id <= end; ++id)
122 wp_ids.push_back(id);
123 in_range = false;
124 continue;
125 }
126 if (i < (size - 1) && StrRefArgs[i+1] == Minus) {
127 if (Arg.getAsInteger(0, beg))
128 return false;
129 // Turn on the in_range flag, we are looking for end of range next.
130 ++i; in_range = true;
131 continue;
132 }
133 // Otherwise, we have a simple ID. Just append it.
134 if (Arg.getAsInteger(0, beg))
135 return false;
136 wp_ids.push_back(beg);
137 }
138 // It is an error if after the loop, we're still in_range.
139 if (in_range)
140 return false;
141
142 return true; // Success!
143}
144
145//-------------------------------------------------------------------------
146// CommandObjectMultiwordWatchpoint
147//-------------------------------------------------------------------------
148#pragma mark MultiwordWatchpoint
149
150CommandObjectMultiwordWatchpoint::CommandObjectMultiwordWatchpoint(CommandInterpreter &interpreter) :
151 CommandObjectMultiword (interpreter,
152 "watchpoint",
153 "A set of commands for operating on watchpoints.",
154 "watchpoint <command> [<command-options>]")
155{
156 bool status;
157
158 CommandObjectSP list_command_object (new CommandObjectWatchpointList (interpreter));
159 CommandObjectSP enable_command_object (new CommandObjectWatchpointEnable (interpreter));
160 CommandObjectSP disable_command_object (new CommandObjectWatchpointDisable (interpreter));
161 CommandObjectSP delete_command_object (new CommandObjectWatchpointDelete (interpreter));
Johnny Chene14cf4e2011-10-05 21:35:46 +0000162 CommandObjectSP ignore_command_object (new CommandObjectWatchpointIgnore (interpreter));
Johnny Chen712a6282011-10-17 18:58:00 +0000163 CommandObjectSP modify_command_object (new CommandObjectWatchpointModify (interpreter));
Johnny Chen55a2d5a2012-01-30 21:46:17 +0000164 CommandObjectSP set_command_object (new CommandObjectWatchpointSet (interpreter));
Johnny Chen01acfa72011-09-22 18:04:58 +0000165
166 list_command_object->SetCommandName ("watchpoint list");
167 enable_command_object->SetCommandName("watchpoint enable");
168 disable_command_object->SetCommandName("watchpoint disable");
169 delete_command_object->SetCommandName("watchpoint delete");
Johnny Chene14cf4e2011-10-05 21:35:46 +0000170 ignore_command_object->SetCommandName("watchpoint ignore");
Johnny Chen712a6282011-10-17 18:58:00 +0000171 modify_command_object->SetCommandName("watchpoint modify");
Johnny Chen55a2d5a2012-01-30 21:46:17 +0000172 set_command_object->SetCommandName("watchpoint set");
Johnny Chen01acfa72011-09-22 18:04:58 +0000173
174 status = LoadSubCommand ("list", list_command_object);
175 status = LoadSubCommand ("enable", enable_command_object);
176 status = LoadSubCommand ("disable", disable_command_object);
177 status = LoadSubCommand ("delete", delete_command_object);
Johnny Chene14cf4e2011-10-05 21:35:46 +0000178 status = LoadSubCommand ("ignore", ignore_command_object);
Johnny Chen712a6282011-10-17 18:58:00 +0000179 status = LoadSubCommand ("modify", modify_command_object);
Johnny Chen55a2d5a2012-01-30 21:46:17 +0000180 status = LoadSubCommand ("set", set_command_object);
Johnny Chen01acfa72011-09-22 18:04:58 +0000181}
182
183CommandObjectMultiwordWatchpoint::~CommandObjectMultiwordWatchpoint()
184{
185}
186
187//-------------------------------------------------------------------------
188// CommandObjectWatchpointList::Options
189//-------------------------------------------------------------------------
190#pragma mark List::CommandOptions
191
192CommandObjectWatchpointList::CommandOptions::CommandOptions(CommandInterpreter &interpreter) :
193 Options(interpreter),
194 m_level(lldb::eDescriptionLevelBrief) // Watchpoint List defaults to brief descriptions
195{
196}
197
198CommandObjectWatchpointList::CommandOptions::~CommandOptions()
199{
200}
201
202OptionDefinition
203CommandObjectWatchpointList::CommandOptions::g_option_table[] =
204{
205 { LLDB_OPT_SET_1, false, "brief", 'b', no_argument, NULL, 0, eArgTypeNone,
206 "Give a brief description of the watchpoint (no location info)."},
207
208 { LLDB_OPT_SET_2, false, "full", 'f', no_argument, NULL, 0, eArgTypeNone,
209 "Give a full description of the watchpoint and its locations."},
210
211 { LLDB_OPT_SET_3, false, "verbose", 'v', no_argument, NULL, 0, eArgTypeNone,
212 "Explain everything we know about the watchpoint (for debugging debugger bugs)." },
213
214 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
215};
216
217const OptionDefinition*
218CommandObjectWatchpointList::CommandOptions::GetDefinitions()
219{
220 return g_option_table;
221}
222
223Error
224CommandObjectWatchpointList::CommandOptions::SetOptionValue(uint32_t option_idx, const char *option_arg)
225{
226 Error error;
227 char short_option = (char) m_getopt_table[option_idx].val;
228
229 switch (short_option)
230 {
231 case 'b':
232 m_level = lldb::eDescriptionLevelBrief;
233 break;
234 case 'f':
235 m_level = lldb::eDescriptionLevelFull;
236 break;
237 case 'v':
238 m_level = lldb::eDescriptionLevelVerbose;
239 break;
240 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000241 error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
Johnny Chen01acfa72011-09-22 18:04:58 +0000242 break;
243 }
244
245 return error;
246}
247
248void
249CommandObjectWatchpointList::CommandOptions::OptionParsingStarting()
250{
251 m_level = lldb::eDescriptionLevelFull;
252}
253
254//-------------------------------------------------------------------------
255// CommandObjectWatchpointList
256//-------------------------------------------------------------------------
257#pragma mark List
258
259CommandObjectWatchpointList::CommandObjectWatchpointList(CommandInterpreter &interpreter) :
260 CommandObject(interpreter,
261 "watchpoint list",
262 "List all watchpoints at configurable levels of detail.",
263 NULL),
264 m_options(interpreter)
265{
266 CommandArgumentEntry arg;
Johnny Chencacedfb2011-09-22 22:34:09 +0000267 CommandObject::AddIDsArgumentData(arg, eArgTypeWatchpointID, eArgTypeWatchpointIDRange);
Johnny Chen01acfa72011-09-22 18:04:58 +0000268 // Add the entry for the first argument for this command to the object's arguments vector.
269 m_arguments.push_back(arg);
270}
271
272CommandObjectWatchpointList::~CommandObjectWatchpointList()
273{
274}
275
276Options *
277CommandObjectWatchpointList::GetOptions()
278{
279 return &m_options;
280}
281
282bool
283CommandObjectWatchpointList::Execute(Args& args, CommandReturnObject &result)
284{
285 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
286 if (target == NULL)
287 {
288 result.AppendError ("Invalid target. No current target or watchpoints.");
289 result.SetStatus (eReturnStatusSuccessFinishNoResult);
290 return true;
291 }
292
Johnny Chen7cbdcfb2012-05-23 21:09:52 +0000293 if (target->GetProcessSP() && target->GetProcessSP()->IsAlive())
294 {
295 uint32_t num_supported_hardware_watchpoints;
296 Error error = target->GetProcessSP()->GetWatchpointSupportInfo(num_supported_hardware_watchpoints);
297 if (error.Success())
298 result.AppendMessageWithFormat("Number of supported hardware watchpoints: %u\n",
299 num_supported_hardware_watchpoints);
300 }
301
Johnny Chenecd4feb2011-10-14 00:42:25 +0000302 const WatchpointList &watchpoints = target->GetWatchpointList();
Johnny Chen01acfa72011-09-22 18:04:58 +0000303 Mutex::Locker locker;
Johnny Chenecd4feb2011-10-14 00:42:25 +0000304 target->GetWatchpointList().GetListMutex(locker);
Johnny Chen01acfa72011-09-22 18:04:58 +0000305
306 size_t num_watchpoints = watchpoints.GetSize();
307
308 if (num_watchpoints == 0)
309 {
310 result.AppendMessage("No watchpoints currently set.");
311 result.SetStatus(eReturnStatusSuccessFinishNoResult);
312 return true;
313 }
314
315 Stream &output_stream = result.GetOutputStream();
316
317 if (args.GetArgumentCount() == 0)
318 {
319 // No watchpoint selected; show info about all currently set watchpoints.
320 result.AppendMessage ("Current watchpoints:");
321 for (size_t i = 0; i < num_watchpoints; ++i)
322 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000323 Watchpoint *wp = watchpoints.GetByIndex(i).get();
324 AddWatchpointDescription(&output_stream, wp, m_options.m_level);
Johnny Chen01acfa72011-09-22 18:04:58 +0000325 }
326 result.SetStatus(eReturnStatusSuccessFinishNoResult);
327 }
328 else
329 {
330 // Particular watchpoints selected; enable them.
331 std::vector<uint32_t> wp_ids;
332 if (!VerifyWatchpointIDs(args, wp_ids))
333 {
334 result.AppendError("Invalid watchpoints specification.");
335 result.SetStatus(eReturnStatusFailed);
336 return false;
337 }
338
339 const size_t size = wp_ids.size();
340 for (size_t i = 0; i < size; ++i)
341 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000342 Watchpoint *wp = watchpoints.FindByID(wp_ids[i]).get();
343 if (wp)
344 AddWatchpointDescription(&output_stream, wp, m_options.m_level);
Johnny Chen01acfa72011-09-22 18:04:58 +0000345 result.SetStatus(eReturnStatusSuccessFinishNoResult);
346 }
347 }
348
349 return result.Succeeded();
350}
351
352//-------------------------------------------------------------------------
353// CommandObjectWatchpointEnable
354//-------------------------------------------------------------------------
355#pragma mark Enable
356
357CommandObjectWatchpointEnable::CommandObjectWatchpointEnable(CommandInterpreter &interpreter) :
358 CommandObject(interpreter,
359 "enable",
360 "Enable the specified disabled watchpoint(s). If no watchpoints are specified, enable all of them.",
361 NULL)
362{
363 CommandArgumentEntry arg;
Johnny Chencacedfb2011-09-22 22:34:09 +0000364 CommandObject::AddIDsArgumentData(arg, eArgTypeWatchpointID, eArgTypeWatchpointIDRange);
Johnny Chen01acfa72011-09-22 18:04:58 +0000365 // Add the entry for the first argument for this command to the object's arguments vector.
366 m_arguments.push_back(arg);
367}
368
369CommandObjectWatchpointEnable::~CommandObjectWatchpointEnable()
370{
371}
372
373bool
374CommandObjectWatchpointEnable::Execute(Args& args, CommandReturnObject &result)
375{
376 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
377 if (!CheckTargetForWatchpointOperations(target, result))
378 return false;
379
380 Mutex::Locker locker;
Johnny Chenecd4feb2011-10-14 00:42:25 +0000381 target->GetWatchpointList().GetListMutex(locker);
Johnny Chen01acfa72011-09-22 18:04:58 +0000382
Johnny Chenecd4feb2011-10-14 00:42:25 +0000383 const WatchpointList &watchpoints = target->GetWatchpointList();
Johnny Chen01acfa72011-09-22 18:04:58 +0000384
385 size_t num_watchpoints = watchpoints.GetSize();
386
387 if (num_watchpoints == 0)
388 {
389 result.AppendError("No watchpoints exist to be enabled.");
390 result.SetStatus(eReturnStatusFailed);
391 return false;
392 }
393
394 if (args.GetArgumentCount() == 0)
395 {
396 // No watchpoint selected; enable all currently set watchpoints.
Johnny Chenecd4feb2011-10-14 00:42:25 +0000397 target->EnableAllWatchpoints();
Johnny Chen01acfa72011-09-22 18:04:58 +0000398 result.AppendMessageWithFormat("All watchpoints enabled. (%lu watchpoints)\n", num_watchpoints);
399 result.SetStatus(eReturnStatusSuccessFinishNoResult);
400 }
401 else
402 {
403 // Particular watchpoints selected; enable them.
404 std::vector<uint32_t> wp_ids;
405 if (!VerifyWatchpointIDs(args, wp_ids))
406 {
407 result.AppendError("Invalid watchpoints specification.");
408 result.SetStatus(eReturnStatusFailed);
409 return false;
410 }
411
412 int count = 0;
413 const size_t size = wp_ids.size();
414 for (size_t i = 0; i < size; ++i)
Johnny Chenecd4feb2011-10-14 00:42:25 +0000415 if (target->EnableWatchpointByID(wp_ids[i]))
Johnny Chen01acfa72011-09-22 18:04:58 +0000416 ++count;
417 result.AppendMessageWithFormat("%d watchpoints enabled.\n", count);
418 result.SetStatus(eReturnStatusSuccessFinishNoResult);
419 }
420
421 return result.Succeeded();
422}
423
424//-------------------------------------------------------------------------
425// CommandObjectWatchpointDisable
426//-------------------------------------------------------------------------
427#pragma mark Disable
428
429CommandObjectWatchpointDisable::CommandObjectWatchpointDisable(CommandInterpreter &interpreter) :
430 CommandObject(interpreter,
431 "watchpoint disable",
432 "Disable the specified watchpoint(s) without removing it/them. If no watchpoints are specified, disable them all.",
433 NULL)
434{
435 CommandArgumentEntry arg;
Johnny Chencacedfb2011-09-22 22:34:09 +0000436 CommandObject::AddIDsArgumentData(arg, eArgTypeWatchpointID, eArgTypeWatchpointIDRange);
Johnny Chen01acfa72011-09-22 18:04:58 +0000437 // Add the entry for the first argument for this command to the object's arguments vector.
438 m_arguments.push_back(arg);
439}
440
441CommandObjectWatchpointDisable::~CommandObjectWatchpointDisable()
442{
443}
444
445bool
446CommandObjectWatchpointDisable::Execute(Args& args, CommandReturnObject &result)
447{
448 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
449 if (!CheckTargetForWatchpointOperations(target, result))
450 return false;
451
452 Mutex::Locker locker;
Johnny Chenecd4feb2011-10-14 00:42:25 +0000453 target->GetWatchpointList().GetListMutex(locker);
Johnny Chen01acfa72011-09-22 18:04:58 +0000454
Johnny Chenecd4feb2011-10-14 00:42:25 +0000455 const WatchpointList &watchpoints = target->GetWatchpointList();
Johnny Chen01acfa72011-09-22 18:04:58 +0000456 size_t num_watchpoints = watchpoints.GetSize();
457
458 if (num_watchpoints == 0)
459 {
460 result.AppendError("No watchpoints exist to be disabled.");
461 result.SetStatus(eReturnStatusFailed);
462 return false;
463 }
464
465 if (args.GetArgumentCount() == 0)
466 {
467 // No watchpoint selected; disable all currently set watchpoints.
Johnny Chenecd4feb2011-10-14 00:42:25 +0000468 if (target->DisableAllWatchpoints())
Johnny Chen01acfa72011-09-22 18:04:58 +0000469 {
470 result.AppendMessageWithFormat("All watchpoints disabled. (%lu watchpoints)\n", num_watchpoints);
471 result.SetStatus(eReturnStatusSuccessFinishNoResult);
472 }
473 else
474 {
475 result.AppendError("Disable all watchpoints failed\n");
476 result.SetStatus(eReturnStatusFailed);
477 }
478 }
479 else
480 {
481 // Particular watchpoints selected; disable them.
482 std::vector<uint32_t> wp_ids;
483 if (!VerifyWatchpointIDs(args, wp_ids))
484 {
485 result.AppendError("Invalid watchpoints specification.");
486 result.SetStatus(eReturnStatusFailed);
487 return false;
488 }
489
490 int count = 0;
491 const size_t size = wp_ids.size();
492 for (size_t i = 0; i < size; ++i)
Johnny Chenecd4feb2011-10-14 00:42:25 +0000493 if (target->DisableWatchpointByID(wp_ids[i]))
Johnny Chen01acfa72011-09-22 18:04:58 +0000494 ++count;
495 result.AppendMessageWithFormat("%d watchpoints disabled.\n", count);
496 result.SetStatus(eReturnStatusSuccessFinishNoResult);
497 }
498
499 return result.Succeeded();
500}
501
502//-------------------------------------------------------------------------
503// CommandObjectWatchpointDelete
504//-------------------------------------------------------------------------
505#pragma mark Delete
506
507CommandObjectWatchpointDelete::CommandObjectWatchpointDelete(CommandInterpreter &interpreter) :
508 CommandObject(interpreter,
509 "watchpoint delete",
510 "Delete the specified watchpoint(s). If no watchpoints are specified, delete them all.",
511 NULL)
512{
513 CommandArgumentEntry arg;
Johnny Chencacedfb2011-09-22 22:34:09 +0000514 CommandObject::AddIDsArgumentData(arg, eArgTypeWatchpointID, eArgTypeWatchpointIDRange);
Johnny Chen01acfa72011-09-22 18:04:58 +0000515 // Add the entry for the first argument for this command to the object's arguments vector.
516 m_arguments.push_back(arg);
517}
518
519CommandObjectWatchpointDelete::~CommandObjectWatchpointDelete()
520{
521}
522
523bool
524CommandObjectWatchpointDelete::Execute(Args& args, CommandReturnObject &result)
525{
526 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
527 if (!CheckTargetForWatchpointOperations(target, result))
528 return false;
529
530 Mutex::Locker locker;
Johnny Chenecd4feb2011-10-14 00:42:25 +0000531 target->GetWatchpointList().GetListMutex(locker);
Johnny Chen01acfa72011-09-22 18:04:58 +0000532
Johnny Chenecd4feb2011-10-14 00:42:25 +0000533 const WatchpointList &watchpoints = target->GetWatchpointList();
Johnny Chen01acfa72011-09-22 18:04:58 +0000534
535 size_t num_watchpoints = watchpoints.GetSize();
536
537 if (num_watchpoints == 0)
538 {
539 result.AppendError("No watchpoints exist to be deleted.");
540 result.SetStatus(eReturnStatusFailed);
541 return false;
542 }
543
544 if (args.GetArgumentCount() == 0)
545 {
546 if (!m_interpreter.Confirm("About to delete all watchpoints, do you want to do that?", true))
547 {
548 result.AppendMessage("Operation cancelled...");
549 }
550 else
551 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000552 target->RemoveAllWatchpoints();
Johnny Chen01acfa72011-09-22 18:04:58 +0000553 result.AppendMessageWithFormat("All watchpoints removed. (%lu watchpoints)\n", num_watchpoints);
554 }
555 result.SetStatus (eReturnStatusSuccessFinishNoResult);
556 }
557 else
558 {
559 // Particular watchpoints selected; delete them.
560 std::vector<uint32_t> wp_ids;
561 if (!VerifyWatchpointIDs(args, wp_ids))
562 {
563 result.AppendError("Invalid watchpoints specification.");
564 result.SetStatus(eReturnStatusFailed);
565 return false;
566 }
567
568 int count = 0;
569 const size_t size = wp_ids.size();
570 for (size_t i = 0; i < size; ++i)
Johnny Chenecd4feb2011-10-14 00:42:25 +0000571 if (target->RemoveWatchpointByID(wp_ids[i]))
Johnny Chen01acfa72011-09-22 18:04:58 +0000572 ++count;
573 result.AppendMessageWithFormat("%d watchpoints deleted.\n",count);
574 result.SetStatus (eReturnStatusSuccessFinishNoResult);
575 }
576
577 return result.Succeeded();
578}
579
Johnny Chene14cf4e2011-10-05 21:35:46 +0000580//-------------------------------------------------------------------------
581// CommandObjectWatchpointIgnore::CommandOptions
582//-------------------------------------------------------------------------
583#pragma mark Ignore::CommandOptions
584
585CommandObjectWatchpointIgnore::CommandOptions::CommandOptions(CommandInterpreter &interpreter) :
586 Options (interpreter),
587 m_ignore_count (0)
588{
589}
590
591CommandObjectWatchpointIgnore::CommandOptions::~CommandOptions ()
592{
593}
594
595OptionDefinition
596CommandObjectWatchpointIgnore::CommandOptions::g_option_table[] =
597{
598 { LLDB_OPT_SET_ALL, true, "ignore-count", 'i', required_argument, NULL, NULL, eArgTypeCount, "Set the number of times this watchpoint is skipped before stopping." },
599 { 0, false, NULL, 0 , 0, NULL, 0, eArgTypeNone, NULL }
600};
601
602const OptionDefinition*
603CommandObjectWatchpointIgnore::CommandOptions::GetDefinitions ()
604{
605 return g_option_table;
606}
607
608Error
609CommandObjectWatchpointIgnore::CommandOptions::SetOptionValue (uint32_t option_idx, const char *option_arg)
610{
611 Error error;
612 char short_option = (char) m_getopt_table[option_idx].val;
613
614 switch (short_option)
615 {
616 case 'i':
617 {
618 m_ignore_count = Args::StringToUInt32(option_arg, UINT32_MAX, 0);
619 if (m_ignore_count == UINT32_MAX)
Greg Clayton9c236732011-10-26 00:56:27 +0000620 error.SetErrorStringWithFormat ("invalid ignore count '%s'", option_arg);
Johnny Chene14cf4e2011-10-05 21:35:46 +0000621 }
622 break;
623 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000624 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
Johnny Chene14cf4e2011-10-05 21:35:46 +0000625 break;
626 }
627
628 return error;
629}
630
631void
632CommandObjectWatchpointIgnore::CommandOptions::OptionParsingStarting ()
633{
634 m_ignore_count = 0;
635}
636
637//-------------------------------------------------------------------------
638// CommandObjectWatchpointIgnore
639//-------------------------------------------------------------------------
640#pragma mark Ignore
641
642CommandObjectWatchpointIgnore::CommandObjectWatchpointIgnore(CommandInterpreter &interpreter) :
643 CommandObject(interpreter,
644 "watchpoint ignore",
645 "Set ignore count on the specified watchpoint(s). If no watchpoints are specified, set them all.",
646 NULL),
647 m_options (interpreter)
648{
649 CommandArgumentEntry arg;
650 CommandObject::AddIDsArgumentData(arg, eArgTypeWatchpointID, eArgTypeWatchpointIDRange);
651 // Add the entry for the first argument for this command to the object's arguments vector.
652 m_arguments.push_back(arg);
653}
654
655CommandObjectWatchpointIgnore::~CommandObjectWatchpointIgnore()
656{
657}
658
659Options *
660CommandObjectWatchpointIgnore::GetOptions ()
661{
662 return &m_options;
663}
664
665bool
666CommandObjectWatchpointIgnore::Execute(Args& args, CommandReturnObject &result)
667{
668 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
669 if (!CheckTargetForWatchpointOperations(target, result))
670 return false;
671
672 Mutex::Locker locker;
Johnny Chenecd4feb2011-10-14 00:42:25 +0000673 target->GetWatchpointList().GetListMutex(locker);
Johnny Chene14cf4e2011-10-05 21:35:46 +0000674
Johnny Chenecd4feb2011-10-14 00:42:25 +0000675 const WatchpointList &watchpoints = target->GetWatchpointList();
Johnny Chene14cf4e2011-10-05 21:35:46 +0000676
677 size_t num_watchpoints = watchpoints.GetSize();
678
679 if (num_watchpoints == 0)
680 {
681 result.AppendError("No watchpoints exist to be ignored.");
682 result.SetStatus(eReturnStatusFailed);
683 return false;
684 }
685
686 if (args.GetArgumentCount() == 0)
687 {
Johnny Chenecd4feb2011-10-14 00:42:25 +0000688 target->IgnoreAllWatchpoints(m_options.m_ignore_count);
Johnny Chene14cf4e2011-10-05 21:35:46 +0000689 result.AppendMessageWithFormat("All watchpoints ignored. (%lu watchpoints)\n", num_watchpoints);
690 result.SetStatus (eReturnStatusSuccessFinishNoResult);
691 }
692 else
693 {
694 // Particular watchpoints selected; ignore them.
695 std::vector<uint32_t> wp_ids;
696 if (!VerifyWatchpointIDs(args, wp_ids))
697 {
698 result.AppendError("Invalid watchpoints specification.");
699 result.SetStatus(eReturnStatusFailed);
700 return false;
701 }
702
703 int count = 0;
704 const size_t size = wp_ids.size();
705 for (size_t i = 0; i < size; ++i)
Johnny Chenecd4feb2011-10-14 00:42:25 +0000706 if (target->IgnoreWatchpointByID(wp_ids[i], m_options.m_ignore_count))
Johnny Chene14cf4e2011-10-05 21:35:46 +0000707 ++count;
708 result.AppendMessageWithFormat("%d watchpoints ignored.\n",count);
709 result.SetStatus (eReturnStatusSuccessFinishNoResult);
710 }
711
712 return result.Succeeded();
713}
714
Johnny Chen712a6282011-10-17 18:58:00 +0000715//-------------------------------------------------------------------------
716// CommandObjectWatchpointModify::CommandOptions
717//-------------------------------------------------------------------------
718#pragma mark Modify::CommandOptions
719
720CommandObjectWatchpointModify::CommandOptions::CommandOptions(CommandInterpreter &interpreter) :
721 Options (interpreter),
722 m_condition (),
723 m_condition_passed (false)
724{
725}
726
727CommandObjectWatchpointModify::CommandOptions::~CommandOptions ()
728{
729}
730
731OptionDefinition
732CommandObjectWatchpointModify::CommandOptions::g_option_table[] =
733{
734{ LLDB_OPT_SET_ALL, false, "condition", 'c', required_argument, NULL, NULL, eArgTypeExpression, "The watchpoint stops only if this condition expression evaluates to true."},
735{ 0, false, NULL, 0 , 0, NULL, 0, eArgTypeNone, NULL }
736};
737
738const OptionDefinition*
739CommandObjectWatchpointModify::CommandOptions::GetDefinitions ()
740{
741 return g_option_table;
742}
743
744Error
745CommandObjectWatchpointModify::CommandOptions::SetOptionValue (uint32_t option_idx, const char *option_arg)
746{
747 Error error;
748 char short_option = (char) m_getopt_table[option_idx].val;
749
750 switch (short_option)
751 {
752 case 'c':
753 if (option_arg != NULL)
754 m_condition.assign (option_arg);
755 else
756 m_condition.clear();
757 m_condition_passed = true;
758 break;
759 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000760 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
Johnny Chen712a6282011-10-17 18:58:00 +0000761 break;
762 }
763
764 return error;
765}
766
767void
768CommandObjectWatchpointModify::CommandOptions::OptionParsingStarting ()
769{
770 m_condition.clear();
771 m_condition_passed = false;
772}
773
774//-------------------------------------------------------------------------
775// CommandObjectWatchpointModify
776//-------------------------------------------------------------------------
777#pragma mark Modify
778
779CommandObjectWatchpointModify::CommandObjectWatchpointModify (CommandInterpreter &interpreter) :
780 CommandObject (interpreter,
781 "watchpoint modify",
782 "Modify the options on a watchpoint or set of watchpoints in the executable. "
783 "If no watchpoint is specified, act on the last created watchpoint. "
784 "Passing an empty argument clears the modification.",
785 NULL),
786 m_options (interpreter)
787{
788 CommandArgumentEntry arg;
789 CommandObject::AddIDsArgumentData(arg, eArgTypeWatchpointID, eArgTypeWatchpointIDRange);
790 // Add the entry for the first argument for this command to the object's arguments vector.
791 m_arguments.push_back (arg);
792}
793
794CommandObjectWatchpointModify::~CommandObjectWatchpointModify ()
795{
796}
797
798Options *
799CommandObjectWatchpointModify::GetOptions ()
800{
801 return &m_options;
802}
803
804bool
805CommandObjectWatchpointModify::Execute
806(
807 Args& args,
808 CommandReturnObject &result
809)
810{
811 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
812 if (!CheckTargetForWatchpointOperations(target, result))
813 return false;
814
815 Mutex::Locker locker;
816 target->GetWatchpointList().GetListMutex(locker);
817
818 const WatchpointList &watchpoints = target->GetWatchpointList();
819
820 size_t num_watchpoints = watchpoints.GetSize();
821
822 if (num_watchpoints == 0)
823 {
824 result.AppendError("No watchpoints exist to be modified.");
825 result.SetStatus(eReturnStatusFailed);
826 return false;
827 }
828
829 if (args.GetArgumentCount() == 0)
830 {
831 WatchpointSP wp_sp = target->GetLastCreatedWatchpoint();
832 wp_sp->SetCondition(m_options.m_condition.c_str());
833 result.SetStatus (eReturnStatusSuccessFinishNoResult);
834 }
835 else
836 {
837 // Particular watchpoints selected; set condition on them.
838 std::vector<uint32_t> wp_ids;
839 if (!VerifyWatchpointIDs(args, wp_ids))
840 {
841 result.AppendError("Invalid watchpoints specification.");
842 result.SetStatus(eReturnStatusFailed);
843 return false;
844 }
845
846 int count = 0;
847 const size_t size = wp_ids.size();
848 for (size_t i = 0; i < size; ++i)
849 {
850 WatchpointSP wp_sp = watchpoints.FindByID(wp_ids[i]);
851 if (wp_sp)
852 {
853 wp_sp->SetCondition(m_options.m_condition.c_str());
854 ++count;
855 }
856 }
857 result.AppendMessageWithFormat("%d watchpoints modified.\n",count);
858 result.SetStatus (eReturnStatusSuccessFinishNoResult);
859 }
860
861 return result.Succeeded();
862}
Johnny Chen55a2d5a2012-01-30 21:46:17 +0000863
864//-------------------------------------------------------------------------
865// CommandObjectWatchpointSet
866//-------------------------------------------------------------------------
Johnny Chen55a2d5a2012-01-30 21:46:17 +0000867
868CommandObjectWatchpointSet::CommandObjectWatchpointSet (CommandInterpreter &interpreter) :
Johnny Chen42404d22012-02-08 22:37:48 +0000869 CommandObjectMultiword (interpreter,
870 "watchpoint set",
871 "A set of commands for setting a watchpoint.",
872 "watchpoint set <subcommand> [<subcommand-options>]")
Johnny Chen55a2d5a2012-01-30 21:46:17 +0000873{
Johnny Chen42404d22012-02-08 22:37:48 +0000874
875 LoadSubCommand ("variable", CommandObjectSP (new CommandObjectWatchpointSetVariable (interpreter)));
876 LoadSubCommand ("expression", CommandObjectSP (new CommandObjectWatchpointSetExpression (interpreter)));
Johnny Chen55a2d5a2012-01-30 21:46:17 +0000877}
878
879CommandObjectWatchpointSet::~CommandObjectWatchpointSet ()
880{
881}
882
Johnny Chen42404d22012-02-08 22:37:48 +0000883//-------------------------------------------------------------------------
884// CommandObjectWatchpointSetVariable
885//-------------------------------------------------------------------------
886#pragma mark Set
887
888CommandObjectWatchpointSetVariable::CommandObjectWatchpointSetVariable (CommandInterpreter &interpreter) :
889 CommandObject (interpreter,
890 "watchpoint set variable",
891 "Set a watchpoint on a variable. "
892 "Use the '-w' option to specify the type of watchpoint and "
893 "the '-x' option to specify the byte size to watch for. "
894 "If no '-w' option is specified, it defaults to read_write. "
895 "If no '-x' option is specified, it defaults to the variable's "
896 "byte size. "
Johnny Chen5564b582012-04-27 05:27:07 +0000897 "Note that there are limited hardware resources for watchpoints. "
898 "If watchpoint setting fails, consider disable/delete existing ones "
899 "to free up resources.",
Johnny Chen42404d22012-02-08 22:37:48 +0000900 NULL,
901 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused),
902 m_option_group (interpreter),
903 m_option_watchpoint ()
904{
905 SetHelpLong(
906"Examples: \n\
907\n\
908 watchpoint set variable -w read_wriate my_global_var \n\
909 # Watch my_global_var for read/write access, with the region to watch corresponding to the byte size of the data type.\n");
910
911 CommandArgumentEntry arg;
912 CommandArgumentData var_name_arg;
913
914 // Define the only variant of this arg.
915 var_name_arg.arg_type = eArgTypeVarName;
916 var_name_arg.arg_repetition = eArgRepeatPlain;
917
918 // Push the variant into the argument entry.
919 arg.push_back (var_name_arg);
920
921 // Push the data for the only argument into the m_arguments vector.
922 m_arguments.push_back (arg);
923
924 // Absorb the '-w' and '-x' options into our option group.
925 m_option_group.Append (&m_option_watchpoint, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
926 m_option_group.Finalize();
927}
928
929CommandObjectWatchpointSetVariable::~CommandObjectWatchpointSetVariable ()
930{
931}
932
Johnny Chen55a2d5a2012-01-30 21:46:17 +0000933Options *
Johnny Chen42404d22012-02-08 22:37:48 +0000934CommandObjectWatchpointSetVariable::GetOptions ()
Johnny Chen55a2d5a2012-01-30 21:46:17 +0000935{
936 return &m_option_group;
937}
938
939bool
Johnny Chen42404d22012-02-08 22:37:48 +0000940CommandObjectWatchpointSetVariable::Execute
Johnny Chen55a2d5a2012-01-30 21:46:17 +0000941(
942 Args& command,
943 CommandReturnObject &result
944)
945{
Johnny Chen1e0e73a2012-06-04 20:08:23 +0000946 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Johnny Chen42404d22012-02-08 22:37:48 +0000947 ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
948 StackFrame *frame = exe_ctx.GetFramePtr();
949 if (frame == NULL)
950 {
951 result.AppendError ("you must be stopped in a valid stack frame to set a watchpoint.");
952 result.SetStatus (eReturnStatusFailed);
953 return false;
954 }
955
956 // If no argument is present, issue an error message. There's no way to set a watchpoint.
957 if (command.GetArgumentCount() <= 0)
958 {
959 result.GetErrorStream().Printf("error: required argument missing; specify your program variable to watch for\n");
960 result.SetStatus(eReturnStatusFailed);
961 return false;
962 }
963
964 // If no '-w' is specified, default to '-w read_write'.
965 if (!m_option_watchpoint.watch_type_specified)
966 {
967 m_option_watchpoint.watch_type = OptionGroupWatchpoint::eWatchReadWrite;
968 }
969
970 // We passed the sanity check for the command.
971 // Proceed to set the watchpoint now.
972 lldb::addr_t addr = 0;
973 size_t size = 0;
974
975 VariableSP var_sp;
976 ValueObjectSP valobj_sp;
977 Stream &output_stream = result.GetOutputStream();
978
979 // A simple watch variable gesture allows only one argument.
980 if (command.GetArgumentCount() != 1) {
981 result.GetErrorStream().Printf("error: specify exactly one variable to watch for\n");
982 result.SetStatus(eReturnStatusFailed);
983 return false;
984 }
985
986 // Things have checked out ok...
987 Error error;
988 uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember;
989 valobj_sp = frame->GetValueForVariableExpressionPath (command.GetArgumentAtIndex(0),
990 eNoDynamicValues,
991 expr_path_options,
992 var_sp,
993 error);
994 if (valobj_sp) {
995 AddressType addr_type;
996 addr = valobj_sp->GetAddressOf(false, &addr_type);
997 if (addr_type == eAddressTypeLoad) {
998 // We're in business.
999 // Find out the size of this variable.
1000 size = m_option_watchpoint.watch_size == 0 ? valobj_sp->GetByteSize()
1001 : m_option_watchpoint.watch_size;
1002 }
1003 } else {
1004 const char *error_cstr = error.AsCString(NULL);
1005 if (error_cstr)
1006 result.GetErrorStream().Printf("error: %s\n", error_cstr);
1007 else
1008 result.GetErrorStream().Printf ("error: unable to find any variable expression path that matches '%s'\n",
1009 command.GetArgumentAtIndex(0));
1010 return false;
1011 }
1012
1013 // Now it's time to create the watchpoint.
1014 uint32_t watch_type = m_option_watchpoint.watch_type;
Johnny Chen3f883492012-06-04 23:19:54 +00001015 error.Clear();
1016 Watchpoint *wp = target->CreateWatchpoint(addr, size, watch_type, error).get();
Johnny Chen42404d22012-02-08 22:37:48 +00001017 if (wp) {
1018 if (var_sp && var_sp->GetDeclaration().GetFile()) {
1019 StreamString ss;
1020 // True to show fullpath for declaration file.
1021 var_sp->GetDeclaration().DumpStopContext(&ss, true);
1022 wp->SetDeclInfo(ss.GetString());
1023 }
1024 StreamString ss;
1025 output_stream.Printf("Watchpoint created: ");
1026 wp->GetDescription(&output_stream, lldb::eDescriptionLevelFull);
1027 output_stream.EOL();
1028 result.SetStatus(eReturnStatusSuccessFinishResult);
1029 } else {
1030 result.AppendErrorWithFormat("Watchpoint creation failed (addr=0x%llx, size=%lu).\n",
1031 addr, size);
Johnny Chen3f883492012-06-04 23:19:54 +00001032 if (error.AsCString(NULL))
1033 result.AppendError(error.AsCString());
Johnny Chen42404d22012-02-08 22:37:48 +00001034 result.SetStatus(eReturnStatusFailed);
1035 }
1036
1037 return result.Succeeded();
1038}
1039
1040//-------------------------------------------------------------------------
1041// CommandObjectWatchpointSetExpression
1042//-------------------------------------------------------------------------
1043#pragma mark Set
1044
1045CommandObjectWatchpointSetExpression::CommandObjectWatchpointSetExpression (CommandInterpreter &interpreter) :
1046 CommandObject (interpreter,
1047 "watchpoint set expression",
1048 "Set a watchpoint on an address by supplying an expression. "
1049 "Use the '-w' option to specify the type of watchpoint and "
1050 "the '-x' option to specify the byte size to watch for. "
1051 "If no '-w' option is specified, it defaults to read_write. "
1052 "If no '-x' option is specified, it defaults to the target's "
1053 "pointer byte size. "
Johnny Chen5564b582012-04-27 05:27:07 +00001054 "Note that there are limited hardware resources for watchpoints. "
1055 "If watchpoint setting fails, consider disable/delete existing ones "
1056 "to free up resources.",
Johnny Chen42404d22012-02-08 22:37:48 +00001057 NULL,
1058 eFlagProcessMustBeLaunched | eFlagProcessMustBePaused),
1059 m_option_group (interpreter),
1060 m_option_watchpoint ()
1061{
1062 SetHelpLong(
1063"Examples: \n\
1064\n\
1065 watchpoint set expression -w write -x 1 -- foo + 32\n\
1066 # Watch write access for the 1-byte region pointed to by the address 'foo + 32'.\n");
1067
1068 CommandArgumentEntry arg;
1069 CommandArgumentData expression_arg;
1070
1071 // Define the only variant of this arg.
1072 expression_arg.arg_type = eArgTypeExpression;
1073 expression_arg.arg_repetition = eArgRepeatPlain;
1074
1075 // Push the only variant into the argument entry.
1076 arg.push_back (expression_arg);
1077
1078 // Push the data for the only argument into the m_arguments vector.
1079 m_arguments.push_back (arg);
1080
1081 // Absorb the '-w' and '-x' options into our option group.
1082 m_option_group.Append (&m_option_watchpoint, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
1083 m_option_group.Finalize();
1084}
1085
1086CommandObjectWatchpointSetExpression::~CommandObjectWatchpointSetExpression ()
1087{
1088}
1089
1090Options *
1091CommandObjectWatchpointSetExpression::GetOptions ()
1092{
1093 return &m_option_group;
1094}
1095
1096#include "llvm/ADT/StringRef.h"
1097static inline void StripLeadingSpaces(llvm::StringRef &Str)
1098{
1099 while (!Str.empty() && isspace(Str[0]))
1100 Str = Str.substr(1);
1101}
1102static inline llvm::StringRef StripOptionTerminator(llvm::StringRef &Str, bool with_dash_w, bool with_dash_x)
1103{
1104 llvm::StringRef ExprStr = Str;
1105
1106 // Get rid of the leading spaces first.
1107 StripLeadingSpaces(ExprStr);
1108
1109 // If there's no '-w' and no '-x', we can just return.
1110 if (!with_dash_w && !with_dash_x)
1111 return ExprStr;
1112
1113 // Otherwise, split on the "--" option terminator string, and return the rest of the string.
1114 ExprStr = ExprStr.split("--").second;
1115 StripLeadingSpaces(ExprStr);
1116 return ExprStr;
1117}
1118bool
1119CommandObjectWatchpointSetExpression::ExecuteRawCommandString
1120(
1121 const char *raw_command,
1122 CommandReturnObject &result
1123)
1124{
Johnny Chen55a2d5a2012-01-30 21:46:17 +00001125 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
1126 ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
1127 StackFrame *frame = exe_ctx.GetFramePtr();
1128 if (frame == NULL)
1129 {
1130 result.AppendError ("you must be stopped in a valid stack frame to set a watchpoint.");
1131 result.SetStatus (eReturnStatusFailed);
1132 return false;
1133 }
1134
Johnny Chen42404d22012-02-08 22:37:48 +00001135 Args command(raw_command);
1136
1137 // Process possible options.
1138 if (!ParseOptions (command, result))
1139 return false;
1140
Johnny Chen6183fcc2012-02-08 01:13:31 +00001141 // If no argument is present, issue an error message. There's no way to set a watchpoint.
1142 if (command.GetArgumentCount() <= 0)
1143 {
Johnny Chen42404d22012-02-08 22:37:48 +00001144 result.GetErrorStream().Printf("error: required argument missing; specify an expression to evaulate into the addres to watch for\n");
Johnny Chen6183fcc2012-02-08 01:13:31 +00001145 result.SetStatus(eReturnStatusFailed);
1146 return false;
1147 }
Johnny Chen55a2d5a2012-01-30 21:46:17 +00001148
Johnny Chen8c1af792012-02-09 18:44:27 +00001149 bool with_dash_w = m_option_watchpoint.watch_type_specified;
1150 bool with_dash_x = (m_option_watchpoint.watch_size != 0);
Johnny Chen6183fcc2012-02-08 01:13:31 +00001151
Johnny Chen55a2d5a2012-01-30 21:46:17 +00001152 // If no '-w' is specified, default to '-w read_write'.
Johnny Chen8c1af792012-02-09 18:44:27 +00001153 if (!with_dash_w)
Johnny Chen55a2d5a2012-01-30 21:46:17 +00001154 {
Johnny Chen55a2d5a2012-01-30 21:46:17 +00001155 m_option_watchpoint.watch_type = OptionGroupWatchpoint::eWatchReadWrite;
1156 }
Johnny Chen55a2d5a2012-01-30 21:46:17 +00001157
Johnny Chen6183fcc2012-02-08 01:13:31 +00001158 // We passed the sanity check for the command.
Johnny Chen55a2d5a2012-01-30 21:46:17 +00001159 // Proceed to set the watchpoint now.
1160 lldb::addr_t addr = 0;
1161 size_t size = 0;
1162
1163 VariableSP var_sp;
1164 ValueObjectSP valobj_sp;
1165 Stream &output_stream = result.GetOutputStream();
1166
Johnny Chen42404d22012-02-08 22:37:48 +00001167 // We will process the raw command string to rid of the '-w', '-x', or '--'
1168 llvm::StringRef raw_expr_str(raw_command);
Johnny Chen8c1af792012-02-09 18:44:27 +00001169 std::string expr_str = StripOptionTerminator(raw_expr_str, with_dash_w, with_dash_x).str();
1170
1171 // Sanity check for when the user forgets to terminate the option strings with a '--'.
1172 if ((with_dash_w || with_dash_w) && expr_str.empty())
1173 {
1174 result.GetErrorStream().Printf("error: did you forget to enter the option terminator string \"--\"?\n");
1175 result.SetStatus(eReturnStatusFailed);
1176 return false;
1177 }
Johnny Chen55a2d5a2012-01-30 21:46:17 +00001178
Johnny Chen42404d22012-02-08 22:37:48 +00001179 // Use expression evaluation to arrive at the address to watch.
1180 const bool coerce_to_id = true;
1181 const bool unwind_on_error = true;
1182 const bool keep_in_memory = false;
1183 ExecutionResults expr_result = target->EvaluateExpression (expr_str.c_str(),
1184 frame,
1185 eExecutionPolicyOnlyWhenNeeded,
1186 coerce_to_id,
1187 unwind_on_error,
1188 keep_in_memory,
1189 eNoDynamicValues,
1190 valobj_sp);
1191 if (expr_result != eExecutionCompleted) {
1192 result.GetErrorStream().Printf("error: expression evaluation of address to watch failed\n");
1193 result.GetErrorStream().Printf("expression evaluated: %s\n", expr_str.c_str());
1194 result.SetStatus(eReturnStatusFailed);
Johnny Chen8c1af792012-02-09 18:44:27 +00001195 return false;
Johnny Chen55a2d5a2012-01-30 21:46:17 +00001196 }
1197
Johnny Chen42404d22012-02-08 22:37:48 +00001198 // Get the address to watch.
Johnny Chene4b5ec02012-06-05 19:37:43 +00001199 bool success = false;
1200 addr = valobj_sp->GetValueAsUnsigned(0, &success);
1201 if (!success) {
Johnny Chen42404d22012-02-08 22:37:48 +00001202 result.GetErrorStream().Printf("error: expression did not evaluate to an address\n");
1203 result.SetStatus(eReturnStatusFailed);
1204 return false;
1205 }
Johnny Chen8c1af792012-02-09 18:44:27 +00001206 size = with_dash_x ? m_option_watchpoint.watch_size
1207 : target->GetArchitecture().GetAddressByteSize();
Johnny Chen42404d22012-02-08 22:37:48 +00001208
Johnny Chen55a2d5a2012-01-30 21:46:17 +00001209 // Now it's time to create the watchpoint.
1210 uint32_t watch_type = m_option_watchpoint.watch_type;
Johnny Chen3f883492012-06-04 23:19:54 +00001211 Error error;
1212 Watchpoint *wp = target->CreateWatchpoint(addr, size, watch_type, error).get();
Johnny Chen55a2d5a2012-01-30 21:46:17 +00001213 if (wp) {
1214 if (var_sp && var_sp->GetDeclaration().GetFile()) {
1215 StreamString ss;
1216 // True to show fullpath for declaration file.
1217 var_sp->GetDeclaration().DumpStopContext(&ss, true);
1218 wp->SetDeclInfo(ss.GetString());
1219 }
1220 StreamString ss;
1221 output_stream.Printf("Watchpoint created: ");
1222 wp->GetDescription(&output_stream, lldb::eDescriptionLevelFull);
1223 output_stream.EOL();
1224 result.SetStatus(eReturnStatusSuccessFinishResult);
1225 } else {
Johnny Chen6183fcc2012-02-08 01:13:31 +00001226 result.AppendErrorWithFormat("Watchpoint creation failed (addr=0x%llx, size=%lu).\n",
1227 addr, size);
Johnny Chen3f883492012-06-04 23:19:54 +00001228 if (error.AsCString(NULL))
1229 result.AppendError(error.AsCString());
Johnny Chen55a2d5a2012-01-30 21:46:17 +00001230 result.SetStatus(eReturnStatusFailed);
1231 }
1232
1233 return result.Succeeded();
1234}