180814287SRaphael Isemann //===-- CommandObjectBreakpoint.cpp ---------------------------------------===//
230fdc8d8SChris Lattner //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
630fdc8d8SChris Lattner //
730fdc8d8SChris Lattner //===----------------------------------------------------------------------===//
830fdc8d8SChris Lattner 
99e85e5a8SEugene Zelenko #include "CommandObjectBreakpoint.h"
109e85e5a8SEugene Zelenko #include "CommandObjectBreakpointCommand.h"
1130fdc8d8SChris Lattner #include "lldb/Breakpoint/Breakpoint.h"
1230fdc8d8SChris Lattner #include "lldb/Breakpoint/BreakpointIDList.h"
1330fdc8d8SChris Lattner #include "lldb/Breakpoint/BreakpointLocation.h"
143eb2b44dSZachary Turner #include "lldb/Host/OptionParser.h"
15b9c1b51eSKate Stone #include "lldb/Interpreter/CommandInterpreter.h"
16b9c1b51eSKate Stone #include "lldb/Interpreter/CommandReturnObject.h"
1747cbf4a0SPavel Labath #include "lldb/Interpreter/OptionArgParser.h"
18943a2481SJim Ingham #include "lldb/Interpreter/OptionGroupPythonClassWithDict.h"
1932abc6edSZachary Turner #include "lldb/Interpreter/OptionValueBoolean.h"
205e09c8c3SJim Ingham #include "lldb/Interpreter/OptionValueString.h"
215e09c8c3SJim Ingham #include "lldb/Interpreter/OptionValueUInt64.h"
22b9c1b51eSKate Stone #include "lldb/Interpreter/Options.h"
230e0984eeSJim Ingham #include "lldb/Target/Language.h"
24b57e4a1bSJason Molenda #include "lldb/Target/StackFrame.h"
25b9c1b51eSKate Stone #include "lldb/Target/Target.h"
261b54c88cSJim Ingham #include "lldb/Target/ThreadSpec.h"
27bf9a7730SZachary Turner #include "lldb/Utility/RegularExpression.h"
28bf9a7730SZachary Turner #include "lldb/Utility/StreamString.h"
2930fdc8d8SChris Lattner 
30796ac80bSJonas Devlieghere #include <memory>
31796ac80bSJonas Devlieghere #include <vector>
32796ac80bSJonas Devlieghere 
3330fdc8d8SChris Lattner using namespace lldb;
3430fdc8d8SChris Lattner using namespace lldb_private;
3530fdc8d8SChris Lattner 
36b9c1b51eSKate Stone static void AddBreakpointDescription(Stream *s, Breakpoint *bp,
37b9c1b51eSKate Stone                                      lldb::DescriptionLevel level) {
3830fdc8d8SChris Lattner   s->IndentMore();
3930fdc8d8SChris Lattner   bp->GetDescription(s, level, true);
4030fdc8d8SChris Lattner   s->IndentLess();
4130fdc8d8SChris Lattner   s->EOL();
4230fdc8d8SChris Lattner }
4330fdc8d8SChris Lattner 
44b842f2ecSJim Ingham // Modifiable Breakpoint Options
45b842f2ecSJim Ingham #pragma mark Modify::CommandOptions
46f94668e3SRaphael Isemann #define LLDB_OPTIONS_breakpoint_modify
47f94668e3SRaphael Isemann #include "CommandOptions.inc"
48bd68a052SRaphael Isemann 
49a925974bSAdrian Prantl class lldb_private::BreakpointOptionGroup : public OptionGroup {
50b842f2ecSJim Ingham public:
51a925974bSAdrian Prantl   BreakpointOptionGroup() : OptionGroup(), m_bp_opts(false) {}
52b842f2ecSJim Ingham 
53b842f2ecSJim Ingham   ~BreakpointOptionGroup() override = default;
54b842f2ecSJim Ingham 
55b842f2ecSJim Ingham   llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
56b842f2ecSJim Ingham     return llvm::makeArrayRef(g_breakpoint_modify_options);
57b842f2ecSJim Ingham   }
58b842f2ecSJim Ingham 
59b842f2ecSJim Ingham   Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
60b842f2ecSJim Ingham                         ExecutionContext *execution_context) override {
61b842f2ecSJim Ingham     Status error;
62a925974bSAdrian Prantl     const int short_option =
63a925974bSAdrian Prantl         g_breakpoint_modify_options[option_idx].short_option;
64b842f2ecSJim Ingham 
65b842f2ecSJim Ingham     switch (short_option) {
66b842f2ecSJim Ingham     case 'c':
6705097246SAdrian Prantl       // Normally an empty breakpoint condition marks is as unset. But we need
6805097246SAdrian Prantl       // to say it was passed in.
69b842f2ecSJim Ingham       m_bp_opts.SetCondition(option_arg.str().c_str());
70b842f2ecSJim Ingham       m_bp_opts.m_set_flags.Set(BreakpointOptions::eCondition);
71b842f2ecSJim Ingham       break;
72b842f2ecSJim Ingham     case 'C':
73adcd0268SBenjamin Kramer       m_commands.push_back(std::string(option_arg));
74b842f2ecSJim Ingham       break;
75b842f2ecSJim Ingham     case 'd':
76b842f2ecSJim Ingham       m_bp_opts.SetEnabled(false);
77b842f2ecSJim Ingham       break;
78b842f2ecSJim Ingham     case 'e':
79b842f2ecSJim Ingham       m_bp_opts.SetEnabled(true);
80b842f2ecSJim Ingham       break;
81b842f2ecSJim Ingham     case 'G': {
82b842f2ecSJim Ingham       bool value, success;
8347cbf4a0SPavel Labath       value = OptionArgParser::ToBoolean(option_arg, false, &success);
84b842f2ecSJim Ingham       if (success) {
85b842f2ecSJim Ingham         m_bp_opts.SetAutoContinue(value);
86b842f2ecSJim Ingham       } else
87b842f2ecSJim Ingham         error.SetErrorStringWithFormat(
88b842f2ecSJim Ingham             "invalid boolean value '%s' passed for -G option",
89b842f2ecSJim Ingham             option_arg.str().c_str());
90a925974bSAdrian Prantl     } break;
91a925974bSAdrian Prantl     case 'i': {
92b842f2ecSJim Ingham       uint32_t ignore_count;
93b842f2ecSJim Ingham       if (option_arg.getAsInteger(0, ignore_count))
94b842f2ecSJim Ingham         error.SetErrorStringWithFormat("invalid ignore count '%s'",
95b842f2ecSJim Ingham                                        option_arg.str().c_str());
96b842f2ecSJim Ingham       else
97b842f2ecSJim Ingham         m_bp_opts.SetIgnoreCount(ignore_count);
98a925974bSAdrian Prantl     } break;
99b842f2ecSJim Ingham     case 'o': {
100b842f2ecSJim Ingham       bool value, success;
10147cbf4a0SPavel Labath       value = OptionArgParser::ToBoolean(option_arg, false, &success);
102b842f2ecSJim Ingham       if (success) {
103b842f2ecSJim Ingham         m_bp_opts.SetOneShot(value);
104b842f2ecSJim Ingham       } else
105b842f2ecSJim Ingham         error.SetErrorStringWithFormat(
106b842f2ecSJim Ingham             "invalid boolean value '%s' passed for -o option",
107b842f2ecSJim Ingham             option_arg.str().c_str());
108b842f2ecSJim Ingham     } break;
109a925974bSAdrian Prantl     case 't': {
110b842f2ecSJim Ingham       lldb::tid_t thread_id = LLDB_INVALID_THREAD_ID;
111b842f2ecSJim Ingham       if (option_arg[0] != '\0') {
112b842f2ecSJim Ingham         if (option_arg.getAsInteger(0, thread_id))
113b842f2ecSJim Ingham           error.SetErrorStringWithFormat("invalid thread id string '%s'",
114b842f2ecSJim Ingham                                          option_arg.str().c_str());
115b842f2ecSJim Ingham       }
116b842f2ecSJim Ingham       m_bp_opts.SetThreadID(thread_id);
117a925974bSAdrian Prantl     } break;
118b842f2ecSJim Ingham     case 'T':
119b842f2ecSJim Ingham       m_bp_opts.GetThreadSpec()->SetName(option_arg.str().c_str());
120b842f2ecSJim Ingham       break;
121b842f2ecSJim Ingham     case 'q':
122b842f2ecSJim Ingham       m_bp_opts.GetThreadSpec()->SetQueueName(option_arg.str().c_str());
123b842f2ecSJim Ingham       break;
124a925974bSAdrian Prantl     case 'x': {
125b842f2ecSJim Ingham       uint32_t thread_index = UINT32_MAX;
126b842f2ecSJim Ingham       if (option_arg[0] != '\n') {
127b842f2ecSJim Ingham         if (option_arg.getAsInteger(0, thread_index))
128b842f2ecSJim Ingham           error.SetErrorStringWithFormat("invalid thread index string '%s'",
129b842f2ecSJim Ingham                                          option_arg.str().c_str());
130b842f2ecSJim Ingham       }
131b842f2ecSJim Ingham       m_bp_opts.GetThreadSpec()->SetIndex(thread_index);
132a925974bSAdrian Prantl     } break;
133b842f2ecSJim Ingham     default:
13436162014SRaphael Isemann       llvm_unreachable("Unimplemented option");
135b842f2ecSJim Ingham     }
136b842f2ecSJim Ingham 
137b842f2ecSJim Ingham     return error;
138b842f2ecSJim Ingham   }
139b842f2ecSJim Ingham 
140b842f2ecSJim Ingham   void OptionParsingStarting(ExecutionContext *execution_context) override {
141b842f2ecSJim Ingham     m_bp_opts.Clear();
142b842f2ecSJim Ingham     m_commands.clear();
143b842f2ecSJim Ingham   }
144b842f2ecSJim Ingham 
145b842f2ecSJim Ingham   Status OptionParsingFinished(ExecutionContext *execution_context) override {
146a925974bSAdrian Prantl     if (!m_commands.empty()) {
147a8f3ae7cSJonas Devlieghere       auto cmd_data = std::make_unique<BreakpointOptions::CommandData>();
148b842f2ecSJim Ingham 
149b842f2ecSJim Ingham       for (std::string &str : m_commands)
150b842f2ecSJim Ingham         cmd_data->user_source.AppendString(str);
151b842f2ecSJim Ingham 
152b842f2ecSJim Ingham       cmd_data->stop_on_error = true;
153b842f2ecSJim Ingham       m_bp_opts.SetCommandDataCallback(cmd_data);
154b842f2ecSJim Ingham     }
155b842f2ecSJim Ingham     return Status();
156b842f2ecSJim Ingham   }
157b842f2ecSJim Ingham 
158a925974bSAdrian Prantl   const BreakpointOptions &GetBreakpointOptions() { return m_bp_opts; }
159b842f2ecSJim Ingham 
160b842f2ecSJim Ingham   std::vector<std::string> m_commands;
161b842f2ecSJim Ingham   BreakpointOptions m_bp_opts;
162b842f2ecSJim Ingham };
163bd68a052SRaphael Isemann 
164f94668e3SRaphael Isemann #define LLDB_OPTIONS_breakpoint_dummy
165f94668e3SRaphael Isemann #include "CommandOptions.inc"
166b842f2ecSJim Ingham 
167a925974bSAdrian Prantl class BreakpointDummyOptionGroup : public OptionGroup {
168b842f2ecSJim Ingham public:
169a925974bSAdrian Prantl   BreakpointDummyOptionGroup() : OptionGroup() {}
170b842f2ecSJim Ingham 
171b842f2ecSJim Ingham   ~BreakpointDummyOptionGroup() override = default;
172b842f2ecSJim Ingham 
173b842f2ecSJim Ingham   llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
174b842f2ecSJim Ingham     return llvm::makeArrayRef(g_breakpoint_dummy_options);
175b842f2ecSJim Ingham   }
176b842f2ecSJim Ingham 
177b842f2ecSJim Ingham   Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
178b842f2ecSJim Ingham                         ExecutionContext *execution_context) override {
179b842f2ecSJim Ingham     Status error;
180a925974bSAdrian Prantl     const int short_option =
181f1539b9dSJim Ingham         g_breakpoint_dummy_options[option_idx].short_option;
182b842f2ecSJim Ingham 
183b842f2ecSJim Ingham     switch (short_option) {
184b842f2ecSJim Ingham     case 'D':
185b842f2ecSJim Ingham       m_use_dummy = true;
186b842f2ecSJim Ingham       break;
187b842f2ecSJim Ingham     default:
18836162014SRaphael Isemann       llvm_unreachable("Unimplemented option");
189b842f2ecSJim Ingham     }
190b842f2ecSJim Ingham 
191b842f2ecSJim Ingham     return error;
192b842f2ecSJim Ingham   }
193b842f2ecSJim Ingham 
194b842f2ecSJim Ingham   void OptionParsingStarting(ExecutionContext *execution_context) override {
195b842f2ecSJim Ingham     m_use_dummy = false;
196b842f2ecSJim Ingham   }
197b842f2ecSJim Ingham 
198b842f2ecSJim Ingham   bool m_use_dummy;
199b842f2ecSJim Ingham };
200b842f2ecSJim Ingham 
201f94668e3SRaphael Isemann #define LLDB_OPTIONS_breakpoint_set
202f94668e3SRaphael Isemann #include "CommandOptions.inc"
2031f0f5b5bSZachary Turner 
2045a988416SJim Ingham // CommandObjectBreakpointSet
20530fdc8d8SChris Lattner 
206b9c1b51eSKate Stone class CommandObjectBreakpointSet : public CommandObjectParsed {
2075a988416SJim Ingham public:
208efe8e7e3SFangrui Song   enum BreakpointSetType {
2095a988416SJim Ingham     eSetTypeInvalid,
2105a988416SJim Ingham     eSetTypeFileAndLine,
2115a988416SJim Ingham     eSetTypeAddress,
2125a988416SJim Ingham     eSetTypeFunctionName,
2135a988416SJim Ingham     eSetTypeFunctionRegexp,
2145a988416SJim Ingham     eSetTypeSourceRegexp,
2153815e702SJim Ingham     eSetTypeException,
2163815e702SJim Ingham     eSetTypeScripted,
217efe8e7e3SFangrui Song   };
2185a988416SJim Ingham 
219b9c1b51eSKate Stone   CommandObjectBreakpointSet(CommandInterpreter &interpreter)
220b9c1b51eSKate Stone       : CommandObjectParsed(
221b9c1b51eSKate Stone             interpreter, "breakpoint set",
2225a988416SJim Ingham             "Sets a breakpoint or set of breakpoints in the executable.",
2235a988416SJim Ingham             "breakpoint set <cmd-options>"),
224738af7a6SJim Ingham         m_bp_opts(), m_python_class_options("scripted breakpoint", true, 'P'),
225f6a2086dSSam McCall         m_options() {
226b842f2ecSJim Ingham     // We're picking up all the normal options, commands and disable.
227a925974bSAdrian Prantl     m_all_options.Append(&m_python_class_options,
228a925974bSAdrian Prantl                          LLDB_OPT_SET_1 | LLDB_OPT_SET_2, LLDB_OPT_SET_11);
229b842f2ecSJim Ingham     m_all_options.Append(&m_bp_opts,
230b842f2ecSJim Ingham                          LLDB_OPT_SET_1 | LLDB_OPT_SET_3 | LLDB_OPT_SET_4,
231b842f2ecSJim Ingham                          LLDB_OPT_SET_ALL);
232f6a2086dSSam McCall     m_all_options.Append(&m_dummy_options, LLDB_OPT_SET_1, LLDB_OPT_SET_ALL);
233b842f2ecSJim Ingham     m_all_options.Append(&m_options);
234b842f2ecSJim Ingham     m_all_options.Finalize();
235b842f2ecSJim Ingham   }
2365a988416SJim Ingham 
2379e85e5a8SEugene Zelenko   ~CommandObjectBreakpointSet() override = default;
2385a988416SJim Ingham 
239b842f2ecSJim Ingham   Options *GetOptions() override { return &m_all_options; }
2405a988416SJim Ingham 
241b842f2ecSJim Ingham   class CommandOptions : public OptionGroup {
2425a988416SJim Ingham   public:
243b9c1b51eSKate Stone     CommandOptions()
244a925974bSAdrian Prantl         : OptionGroup(), m_condition(), m_filenames(), m_line_num(0),
245a925974bSAdrian Prantl           m_column(0), m_func_names(),
246a925974bSAdrian Prantl           m_func_name_type_mask(eFunctionNameTypeNone), m_func_regexp(),
247a925974bSAdrian Prantl           m_source_text_regexp(), m_modules(), m_load_addr(), m_catch_bp(false),
248a925974bSAdrian Prantl           m_throw_bp(true), m_hardware(false),
249a72b31c7SJim Ingham           m_exception_language(eLanguageTypeUnknown),
25023b1decbSDawn Perchik           m_language(lldb::eLanguageTypeUnknown),
251a925974bSAdrian Prantl           m_skip_prologue(eLazyBoolCalculate), m_all_files(false),
252a925974bSAdrian Prantl           m_move_to_nearest_code(eLazyBoolCalculate) {}
25330fdc8d8SChris Lattner 
2549e85e5a8SEugene Zelenko     ~CommandOptions() override = default;
25587df91b8SJim Ingham 
25697206d57SZachary Turner     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
257b9c1b51eSKate Stone                           ExecutionContext *execution_context) override {
25897206d57SZachary Turner       Status error;
259a925974bSAdrian Prantl       const int short_option =
260a925974bSAdrian Prantl           g_breakpoint_set_options[option_idx].short_option;
26130fdc8d8SChris Lattner 
262b9c1b51eSKate Stone       switch (short_option) {
263b9c1b51eSKate Stone       case 'a': {
26447cbf4a0SPavel Labath         m_load_addr = OptionArgParser::ToAddress(execution_context, option_arg,
265e1cfbc79STodd Fiala                                                  LLDB_INVALID_ADDRESS, &error);
266b9c1b51eSKate Stone       } break;
26730fdc8d8SChris Lattner 
268e732052fSJim Ingham       case 'A':
269e732052fSJim Ingham         m_all_files = true;
270e732052fSJim Ingham         break;
271e732052fSJim Ingham 
272ca36cd16SJim Ingham       case 'b':
273adcd0268SBenjamin Kramer         m_func_names.push_back(std::string(option_arg));
274ca36cd16SJim Ingham         m_func_name_type_mask |= eFunctionNameTypeBase;
275ca36cd16SJim Ingham         break;
276ca36cd16SJim Ingham 
2776672a4f5SJonas Devlieghere       case 'u':
278fe11483bSZachary Turner         if (option_arg.getAsInteger(0, m_column))
279b9c1b51eSKate Stone           error.SetErrorStringWithFormat("invalid column number: %s",
280fe11483bSZachary Turner                                          option_arg.str().c_str());
28130fdc8d8SChris Lattner         break;
2829e85e5a8SEugene Zelenko 
283b9c1b51eSKate Stone       case 'E': {
284fe11483bSZachary Turner         LanguageType language = Language::GetLanguageTypeFromString(option_arg);
285fab10e89SJim Ingham 
286b9c1b51eSKate Stone         switch (language) {
287fab10e89SJim Ingham         case eLanguageTypeC89:
288fab10e89SJim Ingham         case eLanguageTypeC:
289fab10e89SJim Ingham         case eLanguageTypeC99:
2901d0089faSBruce Mitchener         case eLanguageTypeC11:
291a72b31c7SJim Ingham           m_exception_language = eLanguageTypeC;
292fab10e89SJim Ingham           break;
293fab10e89SJim Ingham         case eLanguageTypeC_plus_plus:
2941d0089faSBruce Mitchener         case eLanguageTypeC_plus_plus_03:
2951d0089faSBruce Mitchener         case eLanguageTypeC_plus_plus_11:
2962ba84a6aSBruce Mitchener         case eLanguageTypeC_plus_plus_14:
297a72b31c7SJim Ingham           m_exception_language = eLanguageTypeC_plus_plus;
298fab10e89SJim Ingham           break;
299fab10e89SJim Ingham         case eLanguageTypeObjC:
300a72b31c7SJim Ingham           m_exception_language = eLanguageTypeObjC;
301fab10e89SJim Ingham           break;
302fab10e89SJim Ingham         case eLanguageTypeObjC_plus_plus:
303b9c1b51eSKate Stone           error.SetErrorStringWithFormat(
304b9c1b51eSKate Stone               "Set exception breakpoints separately for c++ and objective-c");
305fab10e89SJim Ingham           break;
306fab10e89SJim Ingham         case eLanguageTypeUnknown:
307b9c1b51eSKate Stone           error.SetErrorStringWithFormat(
308b9c1b51eSKate Stone               "Unknown language type: '%s' for exception breakpoint",
309fe11483bSZachary Turner               option_arg.str().c_str());
310fab10e89SJim Ingham           break;
311fab10e89SJim Ingham         default:
312b9c1b51eSKate Stone           error.SetErrorStringWithFormat(
313b9c1b51eSKate Stone               "Unsupported language type: '%s' for exception breakpoint",
314fe11483bSZachary Turner               option_arg.str().c_str());
315fab10e89SJim Ingham         }
316b9c1b51eSKate Stone       } break;
317ca36cd16SJim Ingham 
318ca36cd16SJim Ingham       case 'f':
3198f3be7a3SJonas Devlieghere         m_filenames.AppendIfUnique(FileSpec(option_arg));
320fab10e89SJim Ingham         break;
321ca36cd16SJim Ingham 
322ca36cd16SJim Ingham       case 'F':
323adcd0268SBenjamin Kramer         m_func_names.push_back(std::string(option_arg));
324ca36cd16SJim Ingham         m_func_name_type_mask |= eFunctionNameTypeFull;
325ca36cd16SJim Ingham         break;
326ca36cd16SJim Ingham 
327b9c1b51eSKate Stone       case 'h': {
328fab10e89SJim Ingham         bool success;
32947cbf4a0SPavel Labath         m_catch_bp = OptionArgParser::ToBoolean(option_arg, true, &success);
330fab10e89SJim Ingham         if (!success)
331b9c1b51eSKate Stone           error.SetErrorStringWithFormat(
332fe11483bSZachary Turner               "Invalid boolean value for on-catch option: '%s'",
333fe11483bSZachary Turner               option_arg.str().c_str());
334b9c1b51eSKate Stone       } break;
335eb023e75SGreg Clayton 
336eb023e75SGreg Clayton       case 'H':
337eb023e75SGreg Clayton         m_hardware = true;
338eb023e75SGreg Clayton         break;
339eb023e75SGreg Clayton 
340b9c1b51eSKate Stone       case 'K': {
341a8558b62SJim Ingham         bool success;
342a8558b62SJim Ingham         bool value;
34347cbf4a0SPavel Labath         value = OptionArgParser::ToBoolean(option_arg, true, &success);
344a8558b62SJim Ingham         if (value)
345a8558b62SJim Ingham           m_skip_prologue = eLazyBoolYes;
346a8558b62SJim Ingham         else
347a8558b62SJim Ingham           m_skip_prologue = eLazyBoolNo;
348a8558b62SJim Ingham 
349a8558b62SJim Ingham         if (!success)
350b9c1b51eSKate Stone           error.SetErrorStringWithFormat(
351b9c1b51eSKate Stone               "Invalid boolean value for skip prologue option: '%s'",
352fe11483bSZachary Turner               option_arg.str().c_str());
353b9c1b51eSKate Stone       } break;
354ca36cd16SJim Ingham 
355fe11483bSZachary Turner       case 'l':
356fe11483bSZachary Turner         if (option_arg.getAsInteger(0, m_line_num))
357b9c1b51eSKate Stone           error.SetErrorStringWithFormat("invalid line number: %s.",
358fe11483bSZachary Turner                                          option_arg.str().c_str());
359ca36cd16SJim Ingham         break;
360055ad9beSIlia K 
36123b1decbSDawn Perchik       case 'L':
362fe11483bSZachary Turner         m_language = Language::GetLanguageTypeFromString(option_arg);
36323b1decbSDawn Perchik         if (m_language == eLanguageTypeUnknown)
364b9c1b51eSKate Stone           error.SetErrorStringWithFormat(
365fe11483bSZachary Turner               "Unknown language type: '%s' for breakpoint",
366fe11483bSZachary Turner               option_arg.str().c_str());
36723b1decbSDawn Perchik         break;
36823b1decbSDawn Perchik 
369b9c1b51eSKate Stone       case 'm': {
370055ad9beSIlia K         bool success;
371055ad9beSIlia K         bool value;
37247cbf4a0SPavel Labath         value = OptionArgParser::ToBoolean(option_arg, true, &success);
373055ad9beSIlia K         if (value)
374055ad9beSIlia K           m_move_to_nearest_code = eLazyBoolYes;
375055ad9beSIlia K         else
376055ad9beSIlia K           m_move_to_nearest_code = eLazyBoolNo;
377055ad9beSIlia K 
378055ad9beSIlia K         if (!success)
379b9c1b51eSKate Stone           error.SetErrorStringWithFormat(
380b9c1b51eSKate Stone               "Invalid boolean value for move-to-nearest-code option: '%s'",
381fe11483bSZachary Turner               option_arg.str().c_str());
382055ad9beSIlia K         break;
383055ad9beSIlia K       }
384055ad9beSIlia K 
385ca36cd16SJim Ingham       case 'M':
386adcd0268SBenjamin Kramer         m_func_names.push_back(std::string(option_arg));
387ca36cd16SJim Ingham         m_func_name_type_mask |= eFunctionNameTypeMethod;
388ca36cd16SJim Ingham         break;
389ca36cd16SJim Ingham 
390ca36cd16SJim Ingham       case 'n':
391adcd0268SBenjamin Kramer         m_func_names.push_back(std::string(option_arg));
392ca36cd16SJim Ingham         m_func_name_type_mask |= eFunctionNameTypeAuto;
393ca36cd16SJim Ingham         break;
394ca36cd16SJim Ingham 
3956fa7681bSZachary Turner       case 'N': {
396fe11483bSZachary Turner         if (BreakpointID::StringIsBreakpointName(option_arg, error))
397adcd0268SBenjamin Kramer           m_breakpoint_names.push_back(std::string(option_arg));
398ff9a91eaSJim Ingham         else
399ff9a91eaSJim Ingham           error.SetErrorStringWithFormat("Invalid breakpoint name: %s",
400fe11483bSZachary Turner                                          option_arg.str().c_str());
4015e09c8c3SJim Ingham         break;
4026fa7681bSZachary Turner       }
4035e09c8c3SJim Ingham 
404b9c1b51eSKate Stone       case 'R': {
4052411167fSJim Ingham         lldb::addr_t tmp_offset_addr;
40647cbf4a0SPavel Labath         tmp_offset_addr = OptionArgParser::ToAddress(execution_context,
40747cbf4a0SPavel Labath                                                      option_arg, 0, &error);
4082411167fSJim Ingham         if (error.Success())
4092411167fSJim Ingham           m_offset_addr = tmp_offset_addr;
410b9c1b51eSKate Stone       } break;
4112411167fSJim Ingham 
412a72b31c7SJim Ingham       case 'O':
413fe11483bSZachary Turner         m_exception_extra_args.AppendArgument("-O");
414fe11483bSZachary Turner         m_exception_extra_args.AppendArgument(option_arg);
415a72b31c7SJim Ingham         break;
416a72b31c7SJim Ingham 
417ca36cd16SJim Ingham       case 'p':
418adcd0268SBenjamin Kramer         m_source_text_regexp.assign(std::string(option_arg));
419ca36cd16SJim Ingham         break;
420ca36cd16SJim Ingham 
421ca36cd16SJim Ingham       case 'r':
422adcd0268SBenjamin Kramer         m_func_regexp.assign(std::string(option_arg));
423ca36cd16SJim Ingham         break;
424ca36cd16SJim Ingham 
425ca36cd16SJim Ingham       case 's':
4268f3be7a3SJonas Devlieghere         m_modules.AppendIfUnique(FileSpec(option_arg));
427ca36cd16SJim Ingham         break;
428ca36cd16SJim Ingham 
429ca36cd16SJim Ingham       case 'S':
430adcd0268SBenjamin Kramer         m_func_names.push_back(std::string(option_arg));
431ca36cd16SJim Ingham         m_func_name_type_mask |= eFunctionNameTypeSelector;
432ca36cd16SJim Ingham         break;
433ca36cd16SJim Ingham 
434b9c1b51eSKate Stone       case 'w': {
435ca36cd16SJim Ingham         bool success;
43647cbf4a0SPavel Labath         m_throw_bp = OptionArgParser::ToBoolean(option_arg, true, &success);
437ca36cd16SJim Ingham         if (!success)
438b9c1b51eSKate Stone           error.SetErrorStringWithFormat(
439fe11483bSZachary Turner               "Invalid boolean value for on-throw option: '%s'",
440fe11483bSZachary Turner               option_arg.str().c_str());
441b9c1b51eSKate Stone       } break;
442ca36cd16SJim Ingham 
44376bb8d67SJim Ingham       case 'X':
444adcd0268SBenjamin Kramer         m_source_regex_func_names.insert(std::string(option_arg));
44576bb8d67SJim Ingham         break;
44676bb8d67SJim Ingham 
44730fdc8d8SChris Lattner       default:
44836162014SRaphael Isemann         llvm_unreachable("Unimplemented option");
44930fdc8d8SChris Lattner       }
45030fdc8d8SChris Lattner 
45130fdc8d8SChris Lattner       return error;
45230fdc8d8SChris Lattner     }
4539e85e5a8SEugene Zelenko 
454b9c1b51eSKate Stone     void OptionParsingStarting(ExecutionContext *execution_context) override {
45587df91b8SJim Ingham       m_filenames.Clear();
45630fdc8d8SChris Lattner       m_line_num = 0;
45730fdc8d8SChris Lattner       m_column = 0;
458fab10e89SJim Ingham       m_func_names.clear();
4591f746071SGreg Clayton       m_func_name_type_mask = eFunctionNameTypeNone;
46030fdc8d8SChris Lattner       m_func_regexp.clear();
4611f746071SGreg Clayton       m_source_text_regexp.clear();
46287df91b8SJim Ingham       m_modules.Clear();
4631f746071SGreg Clayton       m_load_addr = LLDB_INVALID_ADDRESS;
4642411167fSJim Ingham       m_offset_addr = 0;
465fab10e89SJim Ingham       m_catch_bp = false;
466fab10e89SJim Ingham       m_throw_bp = true;
467eb023e75SGreg Clayton       m_hardware = false;
468a72b31c7SJim Ingham       m_exception_language = eLanguageTypeUnknown;
46923b1decbSDawn Perchik       m_language = lldb::eLanguageTypeUnknown;
470a8558b62SJim Ingham       m_skip_prologue = eLazyBoolCalculate;
4715e09c8c3SJim Ingham       m_breakpoint_names.clear();
472e732052fSJim Ingham       m_all_files = false;
473a72b31c7SJim Ingham       m_exception_extra_args.Clear();
474055ad9beSIlia K       m_move_to_nearest_code = eLazyBoolCalculate;
47576bb8d67SJim Ingham       m_source_regex_func_names.clear();
4763815e702SJim Ingham       m_current_key.clear();
47730fdc8d8SChris Lattner     }
47830fdc8d8SChris Lattner 
4791f0f5b5bSZachary Turner     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
48070602439SZachary Turner       return llvm::makeArrayRef(g_breakpoint_set_options);
4811f0f5b5bSZachary Turner     }
48230fdc8d8SChris Lattner 
4835a988416SJim Ingham     // Instance variables to hold the values for command options.
484969795f1SJim Ingham 
4855a988416SJim Ingham     std::string m_condition;
4865a988416SJim Ingham     FileSpecList m_filenames;
4875a988416SJim Ingham     uint32_t m_line_num;
4885a988416SJim Ingham     uint32_t m_column;
4895a988416SJim Ingham     std::vector<std::string> m_func_names;
4905e09c8c3SJim Ingham     std::vector<std::string> m_breakpoint_names;
491117b1fa1SZachary Turner     lldb::FunctionNameType m_func_name_type_mask;
4925a988416SJim Ingham     std::string m_func_regexp;
4935a988416SJim Ingham     std::string m_source_text_regexp;
4945a988416SJim Ingham     FileSpecList m_modules;
4955a988416SJim Ingham     lldb::addr_t m_load_addr;
4962411167fSJim Ingham     lldb::addr_t m_offset_addr;
4975a988416SJim Ingham     bool m_catch_bp;
4985a988416SJim Ingham     bool m_throw_bp;
499eb023e75SGreg Clayton     bool m_hardware; // Request to use hardware breakpoints
500a72b31c7SJim Ingham     lldb::LanguageType m_exception_language;
50123b1decbSDawn Perchik     lldb::LanguageType m_language;
5025a988416SJim Ingham     LazyBool m_skip_prologue;
503e732052fSJim Ingham     bool m_all_files;
504a72b31c7SJim Ingham     Args m_exception_extra_args;
505055ad9beSIlia K     LazyBool m_move_to_nearest_code;
50676bb8d67SJim Ingham     std::unordered_set<std::string> m_source_regex_func_names;
5073815e702SJim Ingham     std::string m_current_key;
5085a988416SJim Ingham   };
5095a988416SJim Ingham 
5105a988416SJim Ingham protected:
511b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
512cb2380c9SRaphael Isemann     Target &target = GetSelectedOrDummyTarget(m_dummy_options.m_use_dummy);
51330fdc8d8SChris Lattner 
51430fdc8d8SChris Lattner     // The following are the various types of breakpoints that could be set:
51530fdc8d8SChris Lattner     //   1).  -f -l -p  [-s -g]   (setting breakpoint by source location)
51630fdc8d8SChris Lattner     //   2).  -a  [-s -g]         (setting breakpoint by address)
51730fdc8d8SChris Lattner     //   3).  -n  [-s -g]         (setting breakpoint by function name)
518b9c1b51eSKate Stone     //   4).  -r  [-s -g]         (setting breakpoint by function name regular
519b9c1b51eSKate Stone     //   expression)
520b9c1b51eSKate Stone     //   5).  -p -f               (setting a breakpoint by comparing a reg-exp
521b9c1b51eSKate Stone     //   to source text)
522b9c1b51eSKate Stone     //   6).  -E [-w -h]          (setting a breakpoint for exceptions for a
523b9c1b51eSKate Stone     //   given language.)
52430fdc8d8SChris Lattner 
52530fdc8d8SChris Lattner     BreakpointSetType break_type = eSetTypeInvalid;
52630fdc8d8SChris Lattner 
527738af7a6SJim Ingham     if (!m_python_class_options.GetName().empty())
5283815e702SJim Ingham       break_type = eSetTypeScripted;
5293815e702SJim Ingham     else if (m_options.m_line_num != 0)
53030fdc8d8SChris Lattner       break_type = eSetTypeFileAndLine;
53130fdc8d8SChris Lattner     else if (m_options.m_load_addr != LLDB_INVALID_ADDRESS)
53230fdc8d8SChris Lattner       break_type = eSetTypeAddress;
533fab10e89SJim Ingham     else if (!m_options.m_func_names.empty())
53430fdc8d8SChris Lattner       break_type = eSetTypeFunctionName;
53530fdc8d8SChris Lattner     else if (!m_options.m_func_regexp.empty())
53630fdc8d8SChris Lattner       break_type = eSetTypeFunctionRegexp;
537969795f1SJim Ingham     else if (!m_options.m_source_text_regexp.empty())
538969795f1SJim Ingham       break_type = eSetTypeSourceRegexp;
539a72b31c7SJim Ingham     else if (m_options.m_exception_language != eLanguageTypeUnknown)
540fab10e89SJim Ingham       break_type = eSetTypeException;
54130fdc8d8SChris Lattner 
542b842f2ecSJim Ingham     BreakpointSP bp_sp = nullptr;
543274060b6SGreg Clayton     FileSpec module_spec;
544a8558b62SJim Ingham     const bool internal = false;
545a8558b62SJim Ingham 
546b9c1b51eSKate Stone     // If the user didn't specify skip-prologue, having an offset should turn
547b9c1b51eSKate Stone     // that off.
548b9c1b51eSKate Stone     if (m_options.m_offset_addr != 0 &&
549b9c1b51eSKate Stone         m_options.m_skip_prologue == eLazyBoolCalculate)
5502411167fSJim Ingham       m_options.m_skip_prologue = eLazyBoolNo;
5512411167fSJim Ingham 
552b9c1b51eSKate Stone     switch (break_type) {
55330fdc8d8SChris Lattner     case eSetTypeFileAndLine: // Breakpoint by source position
55430fdc8d8SChris Lattner     {
55530fdc8d8SChris Lattner       FileSpec file;
556c7bece56SGreg Clayton       const size_t num_files = m_options.m_filenames.GetSize();
557b9c1b51eSKate Stone       if (num_files == 0) {
558b9c1b51eSKate Stone         if (!GetDefaultFile(target, file, result)) {
55987df91b8SJim Ingham           result.AppendError("No file supplied and no default file available.");
56087df91b8SJim Ingham           result.SetStatus(eReturnStatusFailed);
56187df91b8SJim Ingham           return false;
56287df91b8SJim Ingham         }
563b9c1b51eSKate Stone       } else if (num_files > 1) {
564b9c1b51eSKate Stone         result.AppendError("Only one file at a time is allowed for file and "
565b9c1b51eSKate Stone                            "line breakpoints.");
56687df91b8SJim Ingham         result.SetStatus(eReturnStatusFailed);
56787df91b8SJim Ingham         return false;
568b9c1b51eSKate Stone       } else
56987df91b8SJim Ingham         file = m_options.m_filenames.GetFileSpecAtIndex(0);
57030fdc8d8SChris Lattner 
5711f746071SGreg Clayton       // Only check for inline functions if
5721f746071SGreg Clayton       LazyBool check_inlines = eLazyBoolCalculate;
5731f746071SGreg Clayton 
574cb2380c9SRaphael Isemann       bp_sp = target.CreateBreakpoint(
575cb2380c9SRaphael Isemann           &(m_options.m_modules), file, m_options.m_line_num,
576cb2380c9SRaphael Isemann           m_options.m_column, m_options.m_offset_addr, check_inlines,
577cb2380c9SRaphael Isemann           m_options.m_skip_prologue, internal, m_options.m_hardware,
578b842f2ecSJim Ingham           m_options.m_move_to_nearest_code);
579b9c1b51eSKate Stone     } break;
5806eee5aa0SGreg Clayton 
58130fdc8d8SChris Lattner     case eSetTypeAddress: // Breakpoint by address
582055a08a4SJim Ingham     {
583b9c1b51eSKate Stone       // If a shared library has been specified, make an lldb_private::Address
584b842f2ecSJim Ingham       // with the library, and use that.  That way the address breakpoint
585b842f2ecSJim Ingham       //  will track the load location of the library.
586055a08a4SJim Ingham       size_t num_modules_specified = m_options.m_modules.GetSize();
587b9c1b51eSKate Stone       if (num_modules_specified == 1) {
588b9c1b51eSKate Stone         const FileSpec *file_spec =
589b9c1b51eSKate Stone             m_options.m_modules.GetFileSpecPointerAtIndex(0);
590cb2380c9SRaphael Isemann         bp_sp = target.CreateAddressInModuleBreakpoint(
591cb2380c9SRaphael Isemann             m_options.m_load_addr, internal, file_spec, m_options.m_hardware);
592b9c1b51eSKate Stone       } else if (num_modules_specified == 0) {
593cb2380c9SRaphael Isemann         bp_sp = target.CreateBreakpoint(m_options.m_load_addr, internal,
594b842f2ecSJim Ingham                                         m_options.m_hardware);
595b9c1b51eSKate Stone       } else {
596b9c1b51eSKate Stone         result.AppendError("Only one shared library can be specified for "
597b9c1b51eSKate Stone                            "address breakpoints.");
598055a08a4SJim Ingham         result.SetStatus(eReturnStatusFailed);
599055a08a4SJim Ingham         return false;
600055a08a4SJim Ingham       }
60130fdc8d8SChris Lattner       break;
602055a08a4SJim Ingham     }
60330fdc8d8SChris Lattner     case eSetTypeFunctionName: // Breakpoint by function name
6040c5cd90dSGreg Clayton     {
605117b1fa1SZachary Turner       FunctionNameType name_type_mask = m_options.m_func_name_type_mask;
6060c5cd90dSGreg Clayton 
6070c5cd90dSGreg Clayton       if (name_type_mask == 0)
608e02b8504SGreg Clayton         name_type_mask = eFunctionNameTypeAuto;
6090c5cd90dSGreg Clayton 
610cb2380c9SRaphael Isemann       bp_sp = target.CreateBreakpoint(
611cb2380c9SRaphael Isemann           &(m_options.m_modules), &(m_options.m_filenames),
612cb2380c9SRaphael Isemann           m_options.m_func_names, name_type_mask, m_options.m_language,
613cb2380c9SRaphael Isemann           m_options.m_offset_addr, m_options.m_skip_prologue, internal,
614b842f2ecSJim Ingham           m_options.m_hardware);
615b9c1b51eSKate Stone     } break;
6160c5cd90dSGreg Clayton 
617b9c1b51eSKate Stone     case eSetTypeFunctionRegexp: // Breakpoint by regular expression function
618b9c1b51eSKate Stone                                  // name
61930fdc8d8SChris Lattner     {
62095eae423SZachary Turner       RegularExpression regexp(m_options.m_func_regexp);
6213af3f1e8SJonas Devlieghere       if (llvm::Error err = regexp.GetError()) {
622b9c1b51eSKate Stone         result.AppendErrorWithFormat(
623b58af8d2SRaphael Isemann             "Function name regular expression could not be compiled: %s",
6243af3f1e8SJonas Devlieghere             llvm::toString(std::move(err)).c_str());
625*aaf68cd9SRaphael Isemann         // Check if the incorrect regex looks like a globbing expression and
626*aaf68cd9SRaphael Isemann         // warn the user about it.
627*aaf68cd9SRaphael Isemann         if (!m_options.m_func_regexp.empty()) {
628*aaf68cd9SRaphael Isemann           if (m_options.m_func_regexp[0] == '*' ||
629*aaf68cd9SRaphael Isemann               m_options.m_func_regexp[0] == '?')
630*aaf68cd9SRaphael Isemann             result.AppendWarning(
631*aaf68cd9SRaphael Isemann                 "Function name regex does not accept glob patterns.");
632*aaf68cd9SRaphael Isemann         }
63330fdc8d8SChris Lattner         result.SetStatus(eReturnStatusFailed);
634969795f1SJim Ingham         return false;
63530fdc8d8SChris Lattner       }
63687df91b8SJim Ingham 
637cb2380c9SRaphael Isemann       bp_sp = target.CreateFuncRegexBreakpoint(
6385aa1d819SJan Kratochvil           &(m_options.m_modules), &(m_options.m_filenames), std::move(regexp),
639cb2380c9SRaphael Isemann           m_options.m_language, m_options.m_skip_prologue, internal,
640b842f2ecSJim Ingham           m_options.m_hardware);
641a925974bSAdrian Prantl     } break;
642969795f1SJim Ingham     case eSetTypeSourceRegexp: // Breakpoint by regexp on source text.
643969795f1SJim Ingham     {
644c7bece56SGreg Clayton       const size_t num_files = m_options.m_filenames.GetSize();
64587df91b8SJim Ingham 
646b9c1b51eSKate Stone       if (num_files == 0 && !m_options.m_all_files) {
647969795f1SJim Ingham         FileSpec file;
648b9c1b51eSKate Stone         if (!GetDefaultFile(target, file, result)) {
649b9c1b51eSKate Stone           result.AppendError(
650b9c1b51eSKate Stone               "No files provided and could not find default file.");
65187df91b8SJim Ingham           result.SetStatus(eReturnStatusFailed);
65287df91b8SJim Ingham           return false;
653b9c1b51eSKate Stone         } else {
65487df91b8SJim Ingham           m_options.m_filenames.Append(file);
65587df91b8SJim Ingham         }
65687df91b8SJim Ingham       }
6570c5cd90dSGreg Clayton 
65895eae423SZachary Turner       RegularExpression regexp(m_options.m_source_text_regexp);
6593af3f1e8SJonas Devlieghere       if (llvm::Error err = regexp.GetError()) {
660b9c1b51eSKate Stone         result.AppendErrorWithFormat(
661b9c1b51eSKate Stone             "Source text regular expression could not be compiled: \"%s\"",
6623af3f1e8SJonas Devlieghere             llvm::toString(std::move(err)).c_str());
663969795f1SJim Ingham         result.SetStatus(eReturnStatusFailed);
664969795f1SJim Ingham         return false;
665969795f1SJim Ingham       }
666cb2380c9SRaphael Isemann       bp_sp = target.CreateSourceRegexBreakpoint(
667cb2380c9SRaphael Isemann           &(m_options.m_modules), &(m_options.m_filenames),
6685aa1d819SJan Kratochvil           m_options.m_source_regex_func_names, std::move(regexp), internal,
669cb2380c9SRaphael Isemann           m_options.m_hardware, m_options.m_move_to_nearest_code);
670b9c1b51eSKate Stone     } break;
671b9c1b51eSKate Stone     case eSetTypeException: {
67297206d57SZachary Turner       Status precond_error;
673cb2380c9SRaphael Isemann       bp_sp = target.CreateExceptionBreakpoint(
674cb2380c9SRaphael Isemann           m_options.m_exception_language, m_options.m_catch_bp,
675cb2380c9SRaphael Isemann           m_options.m_throw_bp, internal, &m_options.m_exception_extra_args,
676b842f2ecSJim Ingham           &precond_error);
677b9c1b51eSKate Stone       if (precond_error.Fail()) {
678b9c1b51eSKate Stone         result.AppendErrorWithFormat(
679b9c1b51eSKate Stone             "Error setting extra exception arguments: %s",
680a72b31c7SJim Ingham             precond_error.AsCString());
681cb2380c9SRaphael Isemann         target.RemoveBreakpointByID(bp_sp->GetID());
682a72b31c7SJim Ingham         result.SetStatus(eReturnStatusFailed);
683a72b31c7SJim Ingham         return false;
684a72b31c7SJim Ingham       }
685b9c1b51eSKate Stone     } break;
6863815e702SJim Ingham     case eSetTypeScripted: {
6873815e702SJim Ingham 
6883815e702SJim Ingham       Status error;
689cb2380c9SRaphael Isemann       bp_sp = target.CreateScriptedBreakpoint(
690738af7a6SJim Ingham           m_python_class_options.GetName().c_str(), &(m_options.m_modules),
691cb2380c9SRaphael Isemann           &(m_options.m_filenames), false, m_options.m_hardware,
692943a2481SJim Ingham           m_python_class_options.GetStructuredData(), &error);
6933815e702SJim Ingham       if (error.Fail()) {
6943815e702SJim Ingham         result.AppendErrorWithFormat(
695a925974bSAdrian Prantl             "Error setting extra exception arguments: %s", error.AsCString());
696cb2380c9SRaphael Isemann         target.RemoveBreakpointByID(bp_sp->GetID());
6973815e702SJim Ingham         result.SetStatus(eReturnStatusFailed);
6983815e702SJim Ingham         return false;
6993815e702SJim Ingham       }
7003815e702SJim Ingham     } break;
70130fdc8d8SChris Lattner     default:
70230fdc8d8SChris Lattner       break;
70330fdc8d8SChris Lattner     }
70430fdc8d8SChris Lattner 
7051b54c88cSJim Ingham     // Now set the various options that were passed in:
706b842f2ecSJim Ingham     if (bp_sp) {
707b842f2ecSJim Ingham       bp_sp->GetOptions()->CopyOverSetOptions(m_bp_opts.GetBreakpointOptions());
708ca36cd16SJim Ingham 
709b9c1b51eSKate Stone       if (!m_options.m_breakpoint_names.empty()) {
71097206d57SZachary Turner         Status name_error;
711ff9a91eaSJim Ingham         for (auto name : m_options.m_breakpoint_names) {
712cb2380c9SRaphael Isemann           target.AddNameToBreakpoint(bp_sp, name.c_str(), name_error);
713ff9a91eaSJim Ingham           if (name_error.Fail()) {
714ff9a91eaSJim Ingham             result.AppendErrorWithFormat("Invalid breakpoint name: %s",
715ff9a91eaSJim Ingham                                          name.c_str());
716cb2380c9SRaphael Isemann             target.RemoveBreakpointByID(bp_sp->GetID());
717ff9a91eaSJim Ingham             result.SetStatus(eReturnStatusFailed);
718ff9a91eaSJim Ingham             return false;
719ff9a91eaSJim Ingham           }
720ff9a91eaSJim Ingham         }
7215e09c8c3SJim Ingham       }
7221b54c88cSJim Ingham     }
7231b54c88cSJim Ingham 
724b842f2ecSJim Ingham     if (bp_sp) {
72585e8b814SJim Ingham       Stream &output_stream = result.GetOutputStream();
7261391cc7dSJim Ingham       const bool show_locations = false;
727b842f2ecSJim Ingham       bp_sp->GetDescription(&output_stream, lldb::eDescriptionLevelInitial,
728b9c1b51eSKate Stone                             show_locations);
729cb2380c9SRaphael Isemann       if (&target == &GetDummyTarget())
730b9c1b51eSKate Stone         output_stream.Printf("Breakpoint set in dummy target, will get copied "
731b9c1b51eSKate Stone                              "into future targets.\n");
732b9c1b51eSKate Stone       else {
73305097246SAdrian Prantl         // Don't print out this warning for exception breakpoints.  They can
73405097246SAdrian Prantl         // get set before the target is set, but we won't know how to actually
73505097246SAdrian Prantl         // set the breakpoint till we run.
736b842f2ecSJim Ingham         if (bp_sp->GetNumLocations() == 0 && break_type != eSetTypeException) {
737b9c1b51eSKate Stone           output_stream.Printf("WARNING:  Unable to resolve breakpoint to any "
738b9c1b51eSKate Stone                                "actual locations.\n");
7394aeb1989SJim Ingham         }
7404aeb1989SJim Ingham       }
74130fdc8d8SChris Lattner       result.SetStatus(eReturnStatusSuccessFinishResult);
742b842f2ecSJim Ingham     } else if (!bp_sp) {
74330fdc8d8SChris Lattner       result.AppendError("Breakpoint creation failed: No breakpoint created.");
74430fdc8d8SChris Lattner       result.SetStatus(eReturnStatusFailed);
74530fdc8d8SChris Lattner     }
74630fdc8d8SChris Lattner 
74730fdc8d8SChris Lattner     return result.Succeeded();
74830fdc8d8SChris Lattner   }
74930fdc8d8SChris Lattner 
7505a988416SJim Ingham private:
751cb2380c9SRaphael Isemann   bool GetDefaultFile(Target &target, FileSpec &file,
752b9c1b51eSKate Stone                       CommandReturnObject &result) {
7535a988416SJim Ingham     uint32_t default_line;
75405097246SAdrian Prantl     // First use the Source Manager's default file. Then use the current stack
75505097246SAdrian Prantl     // frame's file.
756cb2380c9SRaphael Isemann     if (!target.GetSourceManager().GetDefaultFileAndLine(file, default_line)) {
757b57e4a1bSJason Molenda       StackFrame *cur_frame = m_exe_ctx.GetFramePtr();
758b9c1b51eSKate Stone       if (cur_frame == nullptr) {
759b9c1b51eSKate Stone         result.AppendError(
760b9c1b51eSKate Stone             "No selected frame to use to find the default file.");
7615a988416SJim Ingham         result.SetStatus(eReturnStatusFailed);
7625a988416SJim Ingham         return false;
763b9c1b51eSKate Stone       } else if (!cur_frame->HasDebugInformation()) {
764b9c1b51eSKate Stone         result.AppendError("Cannot use the selected frame to find the default "
765b9c1b51eSKate Stone                            "file, it has no debug info.");
7665a988416SJim Ingham         result.SetStatus(eReturnStatusFailed);
7675a988416SJim Ingham         return false;
768b9c1b51eSKate Stone       } else {
769b9c1b51eSKate Stone         const SymbolContext &sc =
770b9c1b51eSKate Stone             cur_frame->GetSymbolContext(eSymbolContextLineEntry);
771b9c1b51eSKate Stone         if (sc.line_entry.file) {
7725a988416SJim Ingham           file = sc.line_entry.file;
773b9c1b51eSKate Stone         } else {
774b9c1b51eSKate Stone           result.AppendError("Can't find the file for the selected frame to "
775b9c1b51eSKate Stone                              "use as the default file.");
7765a988416SJim Ingham           result.SetStatus(eReturnStatusFailed);
7775a988416SJim Ingham           return false;
7785a988416SJim Ingham         }
7795a988416SJim Ingham       }
7805a988416SJim Ingham     }
7815a988416SJim Ingham     return true;
7825a988416SJim Ingham   }
7835a988416SJim Ingham 
784b842f2ecSJim Ingham   BreakpointOptionGroup m_bp_opts;
785b842f2ecSJim Ingham   BreakpointDummyOptionGroup m_dummy_options;
786943a2481SJim Ingham   OptionGroupPythonClassWithDict m_python_class_options;
7875a988416SJim Ingham   CommandOptions m_options;
788b842f2ecSJim Ingham   OptionGroupOptions m_all_options;
7895a988416SJim Ingham };
7909e85e5a8SEugene Zelenko 
7915a988416SJim Ingham // CommandObjectBreakpointModify
7925a988416SJim Ingham #pragma mark Modify
7935a988416SJim Ingham 
794b9c1b51eSKate Stone class CommandObjectBreakpointModify : public CommandObjectParsed {
7955a988416SJim Ingham public:
796b9c1b51eSKate Stone   CommandObjectBreakpointModify(CommandInterpreter &interpreter)
797b9c1b51eSKate Stone       : CommandObjectParsed(interpreter, "breakpoint modify",
798b9c1b51eSKate Stone                             "Modify the options on a breakpoint or set of "
799b9c1b51eSKate Stone                             "breakpoints in the executable.  "
800b9c1b51eSKate Stone                             "If no breakpoint is specified, acts on the last "
801b9c1b51eSKate Stone                             "created breakpoint.  "
802b9c1b51eSKate Stone                             "With the exception of -e, -d and -i, passing an "
803b9c1b51eSKate Stone                             "empty argument clears the modification.",
8049e85e5a8SEugene Zelenko                             nullptr),
805b9c1b51eSKate Stone         m_options() {
8065a988416SJim Ingham     CommandArgumentEntry arg;
807b9c1b51eSKate Stone     CommandObject::AddIDsArgumentData(arg, eArgTypeBreakpointID,
808b9c1b51eSKate Stone                                       eArgTypeBreakpointIDRange);
809b9c1b51eSKate Stone     // Add the entry for the first argument for this command to the object's
810b9c1b51eSKate Stone     // arguments vector.
8115a988416SJim Ingham     m_arguments.push_back(arg);
812b842f2ecSJim Ingham 
813b842f2ecSJim Ingham     m_options.Append(&m_bp_opts,
814b842f2ecSJim Ingham                      LLDB_OPT_SET_1 | LLDB_OPT_SET_2 | LLDB_OPT_SET_3,
815b842f2ecSJim Ingham                      LLDB_OPT_SET_ALL);
816b842f2ecSJim Ingham     m_options.Append(&m_dummy_opts, LLDB_OPT_SET_1, LLDB_OPT_SET_ALL);
817b842f2ecSJim Ingham     m_options.Finalize();
8185a988416SJim Ingham   }
8195a988416SJim Ingham 
8209e85e5a8SEugene Zelenko   ~CommandObjectBreakpointModify() override = default;
8215a988416SJim Ingham 
822b9c1b51eSKate Stone   Options *GetOptions() override { return &m_options; }
8235a988416SJim Ingham 
8245a988416SJim Ingham protected:
825b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
826cb2380c9SRaphael Isemann     Target &target = GetSelectedOrDummyTarget(m_dummy_opts.m_use_dummy);
8275a988416SJim Ingham 
828bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
829cb2380c9SRaphael Isemann     target.GetBreakpointList().GetListMutex(lock);
8305a988416SJim Ingham 
8315a988416SJim Ingham     BreakpointIDList valid_bp_ids;
8325a988416SJim Ingham 
833b9c1b51eSKate Stone     CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs(
834cb2380c9SRaphael Isemann         command, &target, result, &valid_bp_ids,
835b842f2ecSJim Ingham         BreakpointName::Permissions::PermissionKinds::disablePerm);
8365a988416SJim Ingham 
837b9c1b51eSKate Stone     if (result.Succeeded()) {
8385a988416SJim Ingham       const size_t count = valid_bp_ids.GetSize();
839b9c1b51eSKate Stone       for (size_t i = 0; i < count; ++i) {
8405a988416SJim Ingham         BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i);
8415a988416SJim Ingham 
842b9c1b51eSKate Stone         if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) {
843b9c1b51eSKate Stone           Breakpoint *bp =
844cb2380c9SRaphael Isemann               target.GetBreakpointByID(cur_bp_id.GetBreakpointID()).get();
845b9c1b51eSKate Stone           if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID) {
846b9c1b51eSKate Stone             BreakpointLocation *location =
847b9c1b51eSKate Stone                 bp->FindLocationByID(cur_bp_id.GetLocationID()).get();
848b842f2ecSJim Ingham             if (location)
849a925974bSAdrian Prantl               location->GetLocationOptions()->CopyOverSetOptions(
850a925974bSAdrian Prantl                   m_bp_opts.GetBreakpointOptions());
851b9c1b51eSKate Stone           } else {
852a925974bSAdrian Prantl             bp->GetOptions()->CopyOverSetOptions(
853a925974bSAdrian Prantl                 m_bp_opts.GetBreakpointOptions());
8545a988416SJim Ingham           }
8555a988416SJim Ingham         }
8565a988416SJim Ingham       }
8575a988416SJim Ingham     }
8585a988416SJim Ingham 
8595a988416SJim Ingham     return result.Succeeded();
8605a988416SJim Ingham   }
8615a988416SJim Ingham 
8625a988416SJim Ingham private:
863b842f2ecSJim Ingham   BreakpointOptionGroup m_bp_opts;
864b842f2ecSJim Ingham   BreakpointDummyOptionGroup m_dummy_opts;
865b842f2ecSJim Ingham   OptionGroupOptions m_options;
8665a988416SJim Ingham };
8675a988416SJim Ingham 
8685a988416SJim Ingham // CommandObjectBreakpointEnable
8695a988416SJim Ingham #pragma mark Enable
8705a988416SJim Ingham 
871b9c1b51eSKate Stone class CommandObjectBreakpointEnable : public CommandObjectParsed {
8725a988416SJim Ingham public:
873b9c1b51eSKate Stone   CommandObjectBreakpointEnable(CommandInterpreter &interpreter)
874b9c1b51eSKate Stone       : CommandObjectParsed(interpreter, "enable",
875b9c1b51eSKate Stone                             "Enable the specified disabled breakpoint(s). If "
876b9c1b51eSKate Stone                             "no breakpoints are specified, enable all of them.",
877b9c1b51eSKate Stone                             nullptr) {
8785a988416SJim Ingham     CommandArgumentEntry arg;
879b9c1b51eSKate Stone     CommandObject::AddIDsArgumentData(arg, eArgTypeBreakpointID,
880b9c1b51eSKate Stone                                       eArgTypeBreakpointIDRange);
881b9c1b51eSKate Stone     // Add the entry for the first argument for this command to the object's
882b9c1b51eSKate Stone     // arguments vector.
8835a988416SJim Ingham     m_arguments.push_back(arg);
8845a988416SJim Ingham   }
8855a988416SJim Ingham 
8869e85e5a8SEugene Zelenko   ~CommandObjectBreakpointEnable() override = default;
8875a988416SJim Ingham 
8885a988416SJim Ingham protected:
889b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
890cb2380c9SRaphael Isemann     Target &target = GetSelectedOrDummyTarget();
8915a988416SJim Ingham 
892bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
893cb2380c9SRaphael Isemann     target.GetBreakpointList().GetListMutex(lock);
8945a988416SJim Ingham 
895cb2380c9SRaphael Isemann     const BreakpointList &breakpoints = target.GetBreakpointList();
8965a988416SJim Ingham 
8975a988416SJim Ingham     size_t num_breakpoints = breakpoints.GetSize();
8985a988416SJim Ingham 
899b9c1b51eSKate Stone     if (num_breakpoints == 0) {
9005a988416SJim Ingham       result.AppendError("No breakpoints exist to be enabled.");
9015a988416SJim Ingham       result.SetStatus(eReturnStatusFailed);
9025a988416SJim Ingham       return false;
9035a988416SJim Ingham     }
9045a988416SJim Ingham 
90511eb9c64SZachary Turner     if (command.empty()) {
9065a988416SJim Ingham       // No breakpoint selected; enable all currently set breakpoints.
907cb2380c9SRaphael Isemann       target.EnableAllowedBreakpoints();
908b9c1b51eSKate Stone       result.AppendMessageWithFormat("All breakpoints enabled. (%" PRIu64
909b9c1b51eSKate Stone                                      " breakpoints)\n",
910b9c1b51eSKate Stone                                      (uint64_t)num_breakpoints);
9115a988416SJim Ingham       result.SetStatus(eReturnStatusSuccessFinishNoResult);
912b9c1b51eSKate Stone     } else {
9135a988416SJim Ingham       // Particular breakpoint selected; enable that breakpoint.
9145a988416SJim Ingham       BreakpointIDList valid_bp_ids;
915b9c1b51eSKate Stone       CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs(
916cb2380c9SRaphael Isemann           command, &target, result, &valid_bp_ids,
917b842f2ecSJim Ingham           BreakpointName::Permissions::PermissionKinds::disablePerm);
9185a988416SJim Ingham 
919b9c1b51eSKate Stone       if (result.Succeeded()) {
9205a988416SJim Ingham         int enable_count = 0;
9215a988416SJim Ingham         int loc_count = 0;
9225a988416SJim Ingham         const size_t count = valid_bp_ids.GetSize();
923b9c1b51eSKate Stone         for (size_t i = 0; i < count; ++i) {
9245a988416SJim Ingham           BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i);
9255a988416SJim Ingham 
926b9c1b51eSKate Stone           if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) {
927b9c1b51eSKate Stone             Breakpoint *breakpoint =
928cb2380c9SRaphael Isemann                 target.GetBreakpointByID(cur_bp_id.GetBreakpointID()).get();
929b9c1b51eSKate Stone             if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID) {
930b9c1b51eSKate Stone               BreakpointLocation *location =
931b9c1b51eSKate Stone                   breakpoint->FindLocationByID(cur_bp_id.GetLocationID()).get();
932b9c1b51eSKate Stone               if (location) {
9335a988416SJim Ingham                 location->SetEnabled(true);
9345a988416SJim Ingham                 ++loc_count;
9355a988416SJim Ingham               }
936b9c1b51eSKate Stone             } else {
9375a988416SJim Ingham               breakpoint->SetEnabled(true);
9385a988416SJim Ingham               ++enable_count;
9395a988416SJim Ingham             }
9405a988416SJim Ingham           }
9415a988416SJim Ingham         }
942b9c1b51eSKate Stone         result.AppendMessageWithFormat("%d breakpoints enabled.\n",
943b9c1b51eSKate Stone                                        enable_count + loc_count);
9445a988416SJim Ingham         result.SetStatus(eReturnStatusSuccessFinishNoResult);
9455a988416SJim Ingham       }
9465a988416SJim Ingham     }
9475a988416SJim Ingham 
9485a988416SJim Ingham     return result.Succeeded();
9495a988416SJim Ingham   }
9505a988416SJim Ingham };
9515a988416SJim Ingham 
9525a988416SJim Ingham // CommandObjectBreakpointDisable
9535a988416SJim Ingham #pragma mark Disable
9545a988416SJim Ingham 
955b9c1b51eSKate Stone class CommandObjectBreakpointDisable : public CommandObjectParsed {
9565a988416SJim Ingham public:
9577428a18cSKate Stone   CommandObjectBreakpointDisable(CommandInterpreter &interpreter)
958b9c1b51eSKate Stone       : CommandObjectParsed(
959b9c1b51eSKate Stone             interpreter, "breakpoint disable",
960b9c1b51eSKate Stone             "Disable the specified breakpoint(s) without deleting "
9617428a18cSKate Stone             "them.  If none are specified, disable all "
9627428a18cSKate Stone             "breakpoints.",
963b9c1b51eSKate Stone             nullptr) {
964b9c1b51eSKate Stone     SetHelpLong(
965b9c1b51eSKate Stone         "Disable the specified breakpoint(s) without deleting them.  \
9667428a18cSKate Stone If none are specified, disable all breakpoints."
9677428a18cSKate Stone         R"(
968ea671fbdSKate Stone 
9697428a18cSKate Stone )"
9707428a18cSKate Stone         "Note: disabling a breakpoint will cause none of its locations to be hit \
9717428a18cSKate Stone regardless of whether individual locations are enabled or disabled.  After the sequence:"
9727428a18cSKate Stone         R"(
973ea671fbdSKate Stone 
974ea671fbdSKate Stone     (lldb) break disable 1
975ea671fbdSKate Stone     (lldb) break enable 1.1
976ea671fbdSKate Stone 
977ea671fbdSKate Stone execution will NOT stop at location 1.1.  To achieve that, type:
978ea671fbdSKate Stone 
979ea671fbdSKate Stone     (lldb) break disable 1.*
980ea671fbdSKate Stone     (lldb) break enable 1.1
981ea671fbdSKate Stone 
9827428a18cSKate Stone )"
9837428a18cSKate Stone         "The first command disables all locations for breakpoint 1, \
9847428a18cSKate Stone the second re-enables the first location.");
985b0fac509SJim Ingham 
9865a988416SJim Ingham     CommandArgumentEntry arg;
987b9c1b51eSKate Stone     CommandObject::AddIDsArgumentData(arg, eArgTypeBreakpointID,
988b9c1b51eSKate Stone                                       eArgTypeBreakpointIDRange);
989b9c1b51eSKate Stone     // Add the entry for the first argument for this command to the object's
990b9c1b51eSKate Stone     // arguments vector.
9915a988416SJim Ingham     m_arguments.push_back(arg);
9925a988416SJim Ingham   }
9935a988416SJim Ingham 
9949e85e5a8SEugene Zelenko   ~CommandObjectBreakpointDisable() override = default;
9955a988416SJim Ingham 
9965a988416SJim Ingham protected:
997b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
998cb2380c9SRaphael Isemann     Target &target = GetSelectedOrDummyTarget();
999bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
1000cb2380c9SRaphael Isemann     target.GetBreakpointList().GetListMutex(lock);
10015a988416SJim Ingham 
1002cb2380c9SRaphael Isemann     const BreakpointList &breakpoints = target.GetBreakpointList();
10035a988416SJim Ingham     size_t num_breakpoints = breakpoints.GetSize();
10045a988416SJim Ingham 
1005b9c1b51eSKate Stone     if (num_breakpoints == 0) {
10065a988416SJim Ingham       result.AppendError("No breakpoints exist to be disabled.");
10075a988416SJim Ingham       result.SetStatus(eReturnStatusFailed);
10085a988416SJim Ingham       return false;
10095a988416SJim Ingham     }
10105a988416SJim Ingham 
101111eb9c64SZachary Turner     if (command.empty()) {
10125a988416SJim Ingham       // No breakpoint selected; disable all currently set breakpoints.
1013cb2380c9SRaphael Isemann       target.DisableAllowedBreakpoints();
1014b9c1b51eSKate Stone       result.AppendMessageWithFormat("All breakpoints disabled. (%" PRIu64
1015b9c1b51eSKate Stone                                      " breakpoints)\n",
1016b9c1b51eSKate Stone                                      (uint64_t)num_breakpoints);
10175a988416SJim Ingham       result.SetStatus(eReturnStatusSuccessFinishNoResult);
1018b9c1b51eSKate Stone     } else {
10195a988416SJim Ingham       // Particular breakpoint selected; disable that breakpoint.
10205a988416SJim Ingham       BreakpointIDList valid_bp_ids;
10215a988416SJim Ingham 
1022b9c1b51eSKate Stone       CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs(
1023cb2380c9SRaphael Isemann           command, &target, result, &valid_bp_ids,
1024b842f2ecSJim Ingham           BreakpointName::Permissions::PermissionKinds::disablePerm);
10255a988416SJim Ingham 
1026b9c1b51eSKate Stone       if (result.Succeeded()) {
10275a988416SJim Ingham         int disable_count = 0;
10285a988416SJim Ingham         int loc_count = 0;
10295a988416SJim Ingham         const size_t count = valid_bp_ids.GetSize();
1030b9c1b51eSKate Stone         for (size_t i = 0; i < count; ++i) {
10315a988416SJim Ingham           BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i);
10325a988416SJim Ingham 
1033b9c1b51eSKate Stone           if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) {
1034b9c1b51eSKate Stone             Breakpoint *breakpoint =
1035cb2380c9SRaphael Isemann                 target.GetBreakpointByID(cur_bp_id.GetBreakpointID()).get();
1036b9c1b51eSKate Stone             if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID) {
1037b9c1b51eSKate Stone               BreakpointLocation *location =
1038b9c1b51eSKate Stone                   breakpoint->FindLocationByID(cur_bp_id.GetLocationID()).get();
1039b9c1b51eSKate Stone               if (location) {
10405a988416SJim Ingham                 location->SetEnabled(false);
10415a988416SJim Ingham                 ++loc_count;
10425a988416SJim Ingham               }
1043b9c1b51eSKate Stone             } else {
10445a988416SJim Ingham               breakpoint->SetEnabled(false);
10455a988416SJim Ingham               ++disable_count;
10465a988416SJim Ingham             }
10475a988416SJim Ingham           }
10485a988416SJim Ingham         }
1049b9c1b51eSKate Stone         result.AppendMessageWithFormat("%d breakpoints disabled.\n",
1050b9c1b51eSKate Stone                                        disable_count + loc_count);
10515a988416SJim Ingham         result.SetStatus(eReturnStatusSuccessFinishNoResult);
10525a988416SJim Ingham       }
10535a988416SJim Ingham     }
10545a988416SJim Ingham 
10555a988416SJim Ingham     return result.Succeeded();
10565a988416SJim Ingham   }
10575a988416SJim Ingham };
10585a988416SJim Ingham 
10595a988416SJim Ingham // CommandObjectBreakpointList
10601f0f5b5bSZachary Turner 
10611f0f5b5bSZachary Turner #pragma mark List::CommandOptions
10626f4fb4e7SRaphael Isemann #define LLDB_OPTIONS_breakpoint_list
1063c5a2d747SRaphael Isemann #include "CommandOptions.inc"
10641f0f5b5bSZachary Turner 
10655a988416SJim Ingham #pragma mark List
10665a988416SJim Ingham 
1067b9c1b51eSKate Stone class CommandObjectBreakpointList : public CommandObjectParsed {
10685a988416SJim Ingham public:
1069b9c1b51eSKate Stone   CommandObjectBreakpointList(CommandInterpreter &interpreter)
1070b9c1b51eSKate Stone       : CommandObjectParsed(
1071b9c1b51eSKate Stone             interpreter, "breakpoint list",
10725a988416SJim Ingham             "List some or all breakpoints at configurable levels of detail.",
10739e85e5a8SEugene Zelenko             nullptr),
1074b9c1b51eSKate Stone         m_options() {
10755a988416SJim Ingham     CommandArgumentEntry arg;
10765a988416SJim Ingham     CommandArgumentData bp_id_arg;
10775a988416SJim Ingham 
10785a988416SJim Ingham     // Define the first (and only) variant of this arg.
10795a988416SJim Ingham     bp_id_arg.arg_type = eArgTypeBreakpointID;
10805a988416SJim Ingham     bp_id_arg.arg_repetition = eArgRepeatOptional;
10815a988416SJim Ingham 
1082b9c1b51eSKate Stone     // There is only one variant this argument could be; put it into the
1083b9c1b51eSKate Stone     // argument entry.
10845a988416SJim Ingham     arg.push_back(bp_id_arg);
10855a988416SJim Ingham 
10865a988416SJim Ingham     // Push the data for the first argument into the m_arguments vector.
10875a988416SJim Ingham     m_arguments.push_back(arg);
10885a988416SJim Ingham   }
10895a988416SJim Ingham 
10909e85e5a8SEugene Zelenko   ~CommandObjectBreakpointList() override = default;
10915a988416SJim Ingham 
1092b9c1b51eSKate Stone   Options *GetOptions() override { return &m_options; }
10935a988416SJim Ingham 
1094b9c1b51eSKate Stone   class CommandOptions : public Options {
10955a988416SJim Ingham   public:
1096b9c1b51eSKate Stone     CommandOptions()
1097b9c1b51eSKate Stone         : Options(), m_level(lldb::eDescriptionLevelBrief), m_use_dummy(false) {
10985a988416SJim Ingham     }
10995a988416SJim Ingham 
11009e85e5a8SEugene Zelenko     ~CommandOptions() override = default;
11015a988416SJim Ingham 
110297206d57SZachary Turner     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1103b9c1b51eSKate Stone                           ExecutionContext *execution_context) override {
110497206d57SZachary Turner       Status error;
11053bcdfc0eSGreg Clayton       const int short_option = m_getopt_table[option_idx].val;
11065a988416SJim Ingham 
1107b9c1b51eSKate Stone       switch (short_option) {
11085a988416SJim Ingham       case 'b':
11095a988416SJim Ingham         m_level = lldb::eDescriptionLevelBrief;
11105a988416SJim Ingham         break;
111133df7cd3SJim Ingham       case 'D':
111233df7cd3SJim Ingham         m_use_dummy = true;
111333df7cd3SJim Ingham         break;
11145a988416SJim Ingham       case 'f':
11155a988416SJim Ingham         m_level = lldb::eDescriptionLevelFull;
11165a988416SJim Ingham         break;
11175a988416SJim Ingham       case 'v':
11185a988416SJim Ingham         m_level = lldb::eDescriptionLevelVerbose;
11195a988416SJim Ingham         break;
11205a988416SJim Ingham       case 'i':
11215a988416SJim Ingham         m_internal = true;
11225a988416SJim Ingham         break;
11235a988416SJim Ingham       default:
112436162014SRaphael Isemann         llvm_unreachable("Unimplemented option");
11255a988416SJim Ingham       }
11265a988416SJim Ingham 
11275a988416SJim Ingham       return error;
11285a988416SJim Ingham     }
11295a988416SJim Ingham 
1130b9c1b51eSKate Stone     void OptionParsingStarting(ExecutionContext *execution_context) override {
11315a988416SJim Ingham       m_level = lldb::eDescriptionLevelFull;
11325a988416SJim Ingham       m_internal = false;
113333df7cd3SJim Ingham       m_use_dummy = false;
11345a988416SJim Ingham     }
11355a988416SJim Ingham 
11361f0f5b5bSZachary Turner     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
113770602439SZachary Turner       return llvm::makeArrayRef(g_breakpoint_list_options);
11381f0f5b5bSZachary Turner     }
11395a988416SJim Ingham 
11405a988416SJim Ingham     // Instance variables to hold the values for command options.
11415a988416SJim Ingham 
11425a988416SJim Ingham     lldb::DescriptionLevel m_level;
11435a988416SJim Ingham 
11445a988416SJim Ingham     bool m_internal;
114533df7cd3SJim Ingham     bool m_use_dummy;
11465a988416SJim Ingham   };
11475a988416SJim Ingham 
11485a988416SJim Ingham protected:
1149b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
1150cb2380c9SRaphael Isemann     Target &target = GetSelectedOrDummyTarget(m_options.m_use_dummy);
11515a988416SJim Ingham 
1152b9c1b51eSKate Stone     const BreakpointList &breakpoints =
1153cb2380c9SRaphael Isemann         target.GetBreakpointList(m_options.m_internal);
1154bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
1155cb2380c9SRaphael Isemann     target.GetBreakpointList(m_options.m_internal).GetListMutex(lock);
11565a988416SJim Ingham 
11575a988416SJim Ingham     size_t num_breakpoints = breakpoints.GetSize();
11585a988416SJim Ingham 
1159b9c1b51eSKate Stone     if (num_breakpoints == 0) {
11605a988416SJim Ingham       result.AppendMessage("No breakpoints currently set.");
11615a988416SJim Ingham       result.SetStatus(eReturnStatusSuccessFinishNoResult);
11625a988416SJim Ingham       return true;
11635a988416SJim Ingham     }
11645a988416SJim Ingham 
11655a988416SJim Ingham     Stream &output_stream = result.GetOutputStream();
11665a988416SJim Ingham 
116711eb9c64SZachary Turner     if (command.empty()) {
11685a988416SJim Ingham       // No breakpoint selected; show info about all currently set breakpoints.
11695a988416SJim Ingham       result.AppendMessage("Current breakpoints:");
1170b9c1b51eSKate Stone       for (size_t i = 0; i < num_breakpoints; ++i) {
11715a988416SJim Ingham         Breakpoint *breakpoint = breakpoints.GetBreakpointAtIndex(i).get();
1172b842f2ecSJim Ingham         if (breakpoint->AllowList())
1173b842f2ecSJim Ingham           AddBreakpointDescription(&output_stream, breakpoint,
1174b842f2ecSJim Ingham                                    m_options.m_level);
11755a988416SJim Ingham       }
11765a988416SJim Ingham       result.SetStatus(eReturnStatusSuccessFinishNoResult);
1177b9c1b51eSKate Stone     } else {
11785a988416SJim Ingham       // Particular breakpoints selected; show info about that breakpoint.
11795a988416SJim Ingham       BreakpointIDList valid_bp_ids;
1180b9c1b51eSKate Stone       CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs(
1181cb2380c9SRaphael Isemann           command, &target, result, &valid_bp_ids,
1182b842f2ecSJim Ingham           BreakpointName::Permissions::PermissionKinds::listPerm);
11835a988416SJim Ingham 
1184b9c1b51eSKate Stone       if (result.Succeeded()) {
1185b9c1b51eSKate Stone         for (size_t i = 0; i < valid_bp_ids.GetSize(); ++i) {
11865a988416SJim Ingham           BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i);
1187b9c1b51eSKate Stone           Breakpoint *breakpoint =
1188cb2380c9SRaphael Isemann               target.GetBreakpointByID(cur_bp_id.GetBreakpointID()).get();
1189b9c1b51eSKate Stone           AddBreakpointDescription(&output_stream, breakpoint,
1190b9c1b51eSKate Stone                                    m_options.m_level);
11915a988416SJim Ingham         }
11925a988416SJim Ingham         result.SetStatus(eReturnStatusSuccessFinishNoResult);
1193b9c1b51eSKate Stone       } else {
11947428a18cSKate Stone         result.AppendError("Invalid breakpoint ID.");
11955a988416SJim Ingham         result.SetStatus(eReturnStatusFailed);
11965a988416SJim Ingham       }
11975a988416SJim Ingham     }
11985a988416SJim Ingham 
11995a988416SJim Ingham     return result.Succeeded();
12005a988416SJim Ingham   }
12015a988416SJim Ingham 
12025a988416SJim Ingham private:
12035a988416SJim Ingham   CommandOptions m_options;
12045a988416SJim Ingham };
12055a988416SJim Ingham 
12065a988416SJim Ingham // CommandObjectBreakpointClear
12071f0f5b5bSZachary Turner #pragma mark Clear::CommandOptions
12081f0f5b5bSZachary Turner 
1209f94668e3SRaphael Isemann #define LLDB_OPTIONS_breakpoint_clear
1210f94668e3SRaphael Isemann #include "CommandOptions.inc"
12111f0f5b5bSZachary Turner 
12125a988416SJim Ingham #pragma mark Clear
12135a988416SJim Ingham 
1214b9c1b51eSKate Stone class CommandObjectBreakpointClear : public CommandObjectParsed {
12155a988416SJim Ingham public:
1216efe8e7e3SFangrui Song   enum BreakpointClearType { eClearTypeInvalid, eClearTypeFileAndLine };
12175a988416SJim Ingham 
12187428a18cSKate Stone   CommandObjectBreakpointClear(CommandInterpreter &interpreter)
12197428a18cSKate Stone       : CommandObjectParsed(interpreter, "breakpoint clear",
1220b9c1b51eSKate Stone                             "Delete or disable breakpoints matching the "
1221b9c1b51eSKate Stone                             "specified source file and line.",
12225a988416SJim Ingham                             "breakpoint clear <cmd-options>"),
1223b9c1b51eSKate Stone         m_options() {}
12245a988416SJim Ingham 
12259e85e5a8SEugene Zelenko   ~CommandObjectBreakpointClear() override = default;
12265a988416SJim Ingham 
1227b9c1b51eSKate Stone   Options *GetOptions() override { return &m_options; }
12285a988416SJim Ingham 
1229b9c1b51eSKate Stone   class CommandOptions : public Options {
12305a988416SJim Ingham   public:
1231b9c1b51eSKate Stone     CommandOptions() : Options(), m_filename(), m_line_num(0) {}
12325a988416SJim Ingham 
12339e85e5a8SEugene Zelenko     ~CommandOptions() override = default;
12345a988416SJim Ingham 
123597206d57SZachary Turner     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1236b9c1b51eSKate Stone                           ExecutionContext *execution_context) override {
123797206d57SZachary Turner       Status error;
12383bcdfc0eSGreg Clayton       const int short_option = m_getopt_table[option_idx].val;
12395a988416SJim Ingham 
1240b9c1b51eSKate Stone       switch (short_option) {
12415a988416SJim Ingham       case 'f':
1242adcd0268SBenjamin Kramer         m_filename.assign(std::string(option_arg));
12435a988416SJim Ingham         break;
12445a988416SJim Ingham 
12455a988416SJim Ingham       case 'l':
1246fe11483bSZachary Turner         option_arg.getAsInteger(0, m_line_num);
12475a988416SJim Ingham         break;
12485a988416SJim Ingham 
12495a988416SJim Ingham       default:
125036162014SRaphael Isemann         llvm_unreachable("Unimplemented option");
12515a988416SJim Ingham       }
12525a988416SJim Ingham 
12535a988416SJim Ingham       return error;
12545a988416SJim Ingham     }
12555a988416SJim Ingham 
1256b9c1b51eSKate Stone     void OptionParsingStarting(ExecutionContext *execution_context) override {
12575a988416SJim Ingham       m_filename.clear();
12585a988416SJim Ingham       m_line_num = 0;
12595a988416SJim Ingham     }
12605a988416SJim Ingham 
12611f0f5b5bSZachary Turner     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
126270602439SZachary Turner       return llvm::makeArrayRef(g_breakpoint_clear_options);
12631f0f5b5bSZachary Turner     }
12645a988416SJim Ingham 
12655a988416SJim Ingham     // Instance variables to hold the values for command options.
12665a988416SJim Ingham 
12675a988416SJim Ingham     std::string m_filename;
12685a988416SJim Ingham     uint32_t m_line_num;
12695a988416SJim Ingham   };
12705a988416SJim Ingham 
12715a988416SJim Ingham protected:
1272b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
1273cb2380c9SRaphael Isemann     Target &target = GetSelectedOrDummyTarget();
12745a988416SJim Ingham 
127505097246SAdrian Prantl     // The following are the various types of breakpoints that could be
127605097246SAdrian Prantl     // cleared:
12775a988416SJim Ingham     //   1). -f -l (clearing breakpoint by source location)
12785a988416SJim Ingham 
12795a988416SJim Ingham     BreakpointClearType break_type = eClearTypeInvalid;
12805a988416SJim Ingham 
12815a988416SJim Ingham     if (m_options.m_line_num != 0)
12825a988416SJim Ingham       break_type = eClearTypeFileAndLine;
12835a988416SJim Ingham 
1284bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
1285cb2380c9SRaphael Isemann     target.GetBreakpointList().GetListMutex(lock);
12865a988416SJim Ingham 
1287cb2380c9SRaphael Isemann     BreakpointList &breakpoints = target.GetBreakpointList();
12885a988416SJim Ingham     size_t num_breakpoints = breakpoints.GetSize();
12895a988416SJim Ingham 
12905a988416SJim Ingham     // Early return if there's no breakpoint at all.
1291b9c1b51eSKate Stone     if (num_breakpoints == 0) {
12925a988416SJim Ingham       result.AppendError("Breakpoint clear: No breakpoint cleared.");
12935a988416SJim Ingham       result.SetStatus(eReturnStatusFailed);
12945a988416SJim Ingham       return result.Succeeded();
12955a988416SJim Ingham     }
12965a988416SJim Ingham 
12975a988416SJim Ingham     // Find matching breakpoints and delete them.
12985a988416SJim Ingham 
12995a988416SJim Ingham     // First create a copy of all the IDs.
13005a988416SJim Ingham     std::vector<break_id_t> BreakIDs;
13015a988416SJim Ingham     for (size_t i = 0; i < num_breakpoints; ++i)
13029e85e5a8SEugene Zelenko       BreakIDs.push_back(breakpoints.GetBreakpointAtIndex(i)->GetID());
13035a988416SJim Ingham 
13045a988416SJim Ingham     int num_cleared = 0;
13055a988416SJim Ingham     StreamString ss;
1306b9c1b51eSKate Stone     switch (break_type) {
13075a988416SJim Ingham     case eClearTypeFileAndLine: // Breakpoint by source position
13085a988416SJim Ingham     {
13095a988416SJim Ingham       const ConstString filename(m_options.m_filename.c_str());
13105a988416SJim Ingham       BreakpointLocationCollection loc_coll;
13115a988416SJim Ingham 
1312b9c1b51eSKate Stone       for (size_t i = 0; i < num_breakpoints; ++i) {
13135a988416SJim Ingham         Breakpoint *bp = breakpoints.FindBreakpointByID(BreakIDs[i]).get();
13145a988416SJim Ingham 
1315b9c1b51eSKate Stone         if (bp->GetMatchingFileLine(filename, m_options.m_line_num, loc_coll)) {
1316b9c1b51eSKate Stone           // If the collection size is 0, it's a full match and we can just
1317b9c1b51eSKate Stone           // remove the breakpoint.
1318b9c1b51eSKate Stone           if (loc_coll.GetSize() == 0) {
13195a988416SJim Ingham             bp->GetDescription(&ss, lldb::eDescriptionLevelBrief);
13205a988416SJim Ingham             ss.EOL();
1321cb2380c9SRaphael Isemann             target.RemoveBreakpointByID(bp->GetID());
13225a988416SJim Ingham             ++num_cleared;
13235a988416SJim Ingham           }
13245a988416SJim Ingham         }
13255a988416SJim Ingham       }
1326b9c1b51eSKate Stone     } break;
13275a988416SJim Ingham 
13285a988416SJim Ingham     default:
13295a988416SJim Ingham       break;
13305a988416SJim Ingham     }
13315a988416SJim Ingham 
1332b9c1b51eSKate Stone     if (num_cleared > 0) {
13335a988416SJim Ingham       Stream &output_stream = result.GetOutputStream();
13345a988416SJim Ingham       output_stream.Printf("%d breakpoints cleared:\n", num_cleared);
1335c156427dSZachary Turner       output_stream << ss.GetString();
13365a988416SJim Ingham       output_stream.EOL();
13375a988416SJim Ingham       result.SetStatus(eReturnStatusSuccessFinishNoResult);
1338b9c1b51eSKate Stone     } else {
13395a988416SJim Ingham       result.AppendError("Breakpoint clear: No breakpoint cleared.");
13405a988416SJim Ingham       result.SetStatus(eReturnStatusFailed);
13415a988416SJim Ingham     }
13425a988416SJim Ingham 
13435a988416SJim Ingham     return result.Succeeded();
13445a988416SJim Ingham   }
13455a988416SJim Ingham 
13465a988416SJim Ingham private:
13475a988416SJim Ingham   CommandOptions m_options;
13485a988416SJim Ingham };
13495a988416SJim Ingham 
13505a988416SJim Ingham // CommandObjectBreakpointDelete
1351f94668e3SRaphael Isemann #define LLDB_OPTIONS_breakpoint_delete
1352f94668e3SRaphael Isemann #include "CommandOptions.inc"
13531f0f5b5bSZachary Turner 
13545a988416SJim Ingham #pragma mark Delete
13555a988416SJim Ingham 
1356b9c1b51eSKate Stone class CommandObjectBreakpointDelete : public CommandObjectParsed {
13575a988416SJim Ingham public:
1358b9c1b51eSKate Stone   CommandObjectBreakpointDelete(CommandInterpreter &interpreter)
1359b9c1b51eSKate Stone       : CommandObjectParsed(interpreter, "breakpoint delete",
1360b9c1b51eSKate Stone                             "Delete the specified breakpoint(s).  If no "
1361b9c1b51eSKate Stone                             "breakpoints are specified, delete them all.",
13629e85e5a8SEugene Zelenko                             nullptr),
1363b9c1b51eSKate Stone         m_options() {
13645a988416SJim Ingham     CommandArgumentEntry arg;
1365b9c1b51eSKate Stone     CommandObject::AddIDsArgumentData(arg, eArgTypeBreakpointID,
1366b9c1b51eSKate Stone                                       eArgTypeBreakpointIDRange);
1367b9c1b51eSKate Stone     // Add the entry for the first argument for this command to the object's
1368b9c1b51eSKate Stone     // arguments vector.
13695a988416SJim Ingham     m_arguments.push_back(arg);
13705a988416SJim Ingham   }
13715a988416SJim Ingham 
13729e85e5a8SEugene Zelenko   ~CommandObjectBreakpointDelete() override = default;
13735a988416SJim Ingham 
1374b9c1b51eSKate Stone   Options *GetOptions() override { return &m_options; }
137533df7cd3SJim Ingham 
1376b9c1b51eSKate Stone   class CommandOptions : public Options {
137733df7cd3SJim Ingham   public:
1378b9c1b51eSKate Stone     CommandOptions() : Options(), m_use_dummy(false), m_force(false) {}
137933df7cd3SJim Ingham 
13809e85e5a8SEugene Zelenko     ~CommandOptions() override = default;
138133df7cd3SJim Ingham 
138297206d57SZachary Turner     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1383b9c1b51eSKate Stone                           ExecutionContext *execution_context) override {
138497206d57SZachary Turner       Status error;
138533df7cd3SJim Ingham       const int short_option = m_getopt_table[option_idx].val;
138633df7cd3SJim Ingham 
1387b9c1b51eSKate Stone       switch (short_option) {
138833df7cd3SJim Ingham       case 'f':
138933df7cd3SJim Ingham         m_force = true;
139033df7cd3SJim Ingham         break;
139133df7cd3SJim Ingham 
139233df7cd3SJim Ingham       case 'D':
139333df7cd3SJim Ingham         m_use_dummy = true;
139433df7cd3SJim Ingham         break;
139533df7cd3SJim Ingham 
139633df7cd3SJim Ingham       default:
139736162014SRaphael Isemann         llvm_unreachable("Unimplemented option");
139833df7cd3SJim Ingham       }
139933df7cd3SJim Ingham 
140033df7cd3SJim Ingham       return error;
140133df7cd3SJim Ingham     }
140233df7cd3SJim Ingham 
1403b9c1b51eSKate Stone     void OptionParsingStarting(ExecutionContext *execution_context) override {
140433df7cd3SJim Ingham       m_use_dummy = false;
140533df7cd3SJim Ingham       m_force = false;
140633df7cd3SJim Ingham     }
140733df7cd3SJim Ingham 
14081f0f5b5bSZachary Turner     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
140970602439SZachary Turner       return llvm::makeArrayRef(g_breakpoint_delete_options);
14101f0f5b5bSZachary Turner     }
141133df7cd3SJim Ingham 
141233df7cd3SJim Ingham     // Instance variables to hold the values for command options.
141333df7cd3SJim Ingham     bool m_use_dummy;
141433df7cd3SJim Ingham     bool m_force;
141533df7cd3SJim Ingham   };
141633df7cd3SJim Ingham 
14175a988416SJim Ingham protected:
1418b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
1419cb2380c9SRaphael Isemann     Target &target = GetSelectedOrDummyTarget(m_options.m_use_dummy);
14205a988416SJim Ingham 
1421bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
1422cb2380c9SRaphael Isemann     target.GetBreakpointList().GetListMutex(lock);
14235a988416SJim Ingham 
1424cb2380c9SRaphael Isemann     const BreakpointList &breakpoints = target.GetBreakpointList();
14255a988416SJim Ingham 
14265a988416SJim Ingham     size_t num_breakpoints = breakpoints.GetSize();
14275a988416SJim Ingham 
1428b9c1b51eSKate Stone     if (num_breakpoints == 0) {
14295a988416SJim Ingham       result.AppendError("No breakpoints exist to be deleted.");
14305a988416SJim Ingham       result.SetStatus(eReturnStatusFailed);
14315a988416SJim Ingham       return false;
14325a988416SJim Ingham     }
14335a988416SJim Ingham 
143411eb9c64SZachary Turner     if (command.empty()) {
1435b9c1b51eSKate Stone       if (!m_options.m_force &&
1436b9c1b51eSKate Stone           !m_interpreter.Confirm(
1437b9c1b51eSKate Stone               "About to delete all breakpoints, do you want to do that?",
1438b9c1b51eSKate Stone               true)) {
14395a988416SJim Ingham         result.AppendMessage("Operation cancelled...");
1440b9c1b51eSKate Stone       } else {
1441cb2380c9SRaphael Isemann         target.RemoveAllowedBreakpoints();
1442b9c1b51eSKate Stone         result.AppendMessageWithFormat(
1443b9c1b51eSKate Stone             "All breakpoints removed. (%" PRIu64 " breakpoint%s)\n",
1444b9c1b51eSKate Stone             (uint64_t)num_breakpoints, num_breakpoints > 1 ? "s" : "");
14455a988416SJim Ingham       }
14465a988416SJim Ingham       result.SetStatus(eReturnStatusSuccessFinishNoResult);
1447b9c1b51eSKate Stone     } else {
14485a988416SJim Ingham       // Particular breakpoint selected; disable that breakpoint.
14495a988416SJim Ingham       BreakpointIDList valid_bp_ids;
1450b9c1b51eSKate Stone       CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs(
1451cb2380c9SRaphael Isemann           command, &target, result, &valid_bp_ids,
1452b842f2ecSJim Ingham           BreakpointName::Permissions::PermissionKinds::deletePerm);
14535a988416SJim Ingham 
1454b9c1b51eSKate Stone       if (result.Succeeded()) {
14555a988416SJim Ingham         int delete_count = 0;
14565a988416SJim Ingham         int disable_count = 0;
14575a988416SJim Ingham         const size_t count = valid_bp_ids.GetSize();
1458b9c1b51eSKate Stone         for (size_t i = 0; i < count; ++i) {
14595a988416SJim Ingham           BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i);
14605a988416SJim Ingham 
1461b9c1b51eSKate Stone           if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) {
1462b9c1b51eSKate Stone             if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID) {
1463b9c1b51eSKate Stone               Breakpoint *breakpoint =
1464cb2380c9SRaphael Isemann                   target.GetBreakpointByID(cur_bp_id.GetBreakpointID()).get();
1465b9c1b51eSKate Stone               BreakpointLocation *location =
1466b9c1b51eSKate Stone                   breakpoint->FindLocationByID(cur_bp_id.GetLocationID()).get();
1467b9c1b51eSKate Stone               // It makes no sense to try to delete individual locations, so we
1468b9c1b51eSKate Stone               // disable them instead.
1469b9c1b51eSKate Stone               if (location) {
14705a988416SJim Ingham                 location->SetEnabled(false);
14715a988416SJim Ingham                 ++disable_count;
14725a988416SJim Ingham               }
1473b9c1b51eSKate Stone             } else {
1474cb2380c9SRaphael Isemann               target.RemoveBreakpointByID(cur_bp_id.GetBreakpointID());
14755a988416SJim Ingham               ++delete_count;
14765a988416SJim Ingham             }
14775a988416SJim Ingham           }
14785a988416SJim Ingham         }
1479b9c1b51eSKate Stone         result.AppendMessageWithFormat(
1480b9c1b51eSKate Stone             "%d breakpoints deleted; %d breakpoint locations disabled.\n",
14815a988416SJim Ingham             delete_count, disable_count);
14825a988416SJim Ingham         result.SetStatus(eReturnStatusSuccessFinishNoResult);
14835a988416SJim Ingham       }
14845a988416SJim Ingham     }
14855a988416SJim Ingham     return result.Succeeded();
14865a988416SJim Ingham   }
14879e85e5a8SEugene Zelenko 
148833df7cd3SJim Ingham private:
148933df7cd3SJim Ingham   CommandOptions m_options;
149033df7cd3SJim Ingham };
149133df7cd3SJim Ingham 
14925e09c8c3SJim Ingham // CommandObjectBreakpointName
1493f94668e3SRaphael Isemann #define LLDB_OPTIONS_breakpoint_name
1494f94668e3SRaphael Isemann #include "CommandOptions.inc"
1495bd68a052SRaphael Isemann 
1496b9c1b51eSKate Stone class BreakpointNameOptionGroup : public OptionGroup {
14975e09c8c3SJim Ingham public:
1498b9c1b51eSKate Stone   BreakpointNameOptionGroup()
1499b9c1b51eSKate Stone       : OptionGroup(), m_breakpoint(LLDB_INVALID_BREAK_ID), m_use_dummy(false) {
15005e09c8c3SJim Ingham   }
15015e09c8c3SJim Ingham 
15029e85e5a8SEugene Zelenko   ~BreakpointNameOptionGroup() override = default;
15035e09c8c3SJim Ingham 
15041f0f5b5bSZachary Turner   llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
150570602439SZachary Turner     return llvm::makeArrayRef(g_breakpoint_name_options);
15065e09c8c3SJim Ingham   }
15075e09c8c3SJim Ingham 
150897206d57SZachary Turner   Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1509b9c1b51eSKate Stone                         ExecutionContext *execution_context) override {
151097206d57SZachary Turner     Status error;
15115e09c8c3SJim Ingham     const int short_option = g_breakpoint_name_options[option_idx].short_option;
15125e09c8c3SJim Ingham 
1513b9c1b51eSKate Stone     switch (short_option) {
15145e09c8c3SJim Ingham     case 'N':
1515fe11483bSZachary Turner       if (BreakpointID::StringIsBreakpointName(option_arg, error) &&
1516b9c1b51eSKate Stone           error.Success())
1517fe11483bSZachary Turner         m_name.SetValueFromString(option_arg);
15185e09c8c3SJim Ingham       break;
15195e09c8c3SJim Ingham     case 'B':
1520fe11483bSZachary Turner       if (m_breakpoint.SetValueFromString(option_arg).Fail())
1521b9c1b51eSKate Stone         error.SetErrorStringWithFormat(
15228cef4b0bSZachary Turner             "unrecognized value \"%s\" for breakpoint",
1523fe11483bSZachary Turner             option_arg.str().c_str());
15245e09c8c3SJim Ingham       break;
15255e09c8c3SJim Ingham     case 'D':
1526fe11483bSZachary Turner       if (m_use_dummy.SetValueFromString(option_arg).Fail())
1527b9c1b51eSKate Stone         error.SetErrorStringWithFormat(
15288cef4b0bSZachary Turner             "unrecognized value \"%s\" for use-dummy",
1529fe11483bSZachary Turner             option_arg.str().c_str());
15305e09c8c3SJim Ingham       break;
1531e9632ebaSJim Ingham     case 'H':
1532e9632ebaSJim Ingham       m_help_string.SetValueFromString(option_arg);
1533e9632ebaSJim Ingham       break;
15345e09c8c3SJim Ingham 
15355e09c8c3SJim Ingham     default:
153636162014SRaphael Isemann       llvm_unreachable("Unimplemented option");
15375e09c8c3SJim Ingham     }
15385e09c8c3SJim Ingham     return error;
15395e09c8c3SJim Ingham   }
15405e09c8c3SJim Ingham 
1541b9c1b51eSKate Stone   void OptionParsingStarting(ExecutionContext *execution_context) override {
15425e09c8c3SJim Ingham     m_name.Clear();
15435e09c8c3SJim Ingham     m_breakpoint.Clear();
15445e09c8c3SJim Ingham     m_use_dummy.Clear();
15455e09c8c3SJim Ingham     m_use_dummy.SetDefaultValue(false);
1546e9632ebaSJim Ingham     m_help_string.Clear();
15475e09c8c3SJim Ingham   }
15485e09c8c3SJim Ingham 
15495e09c8c3SJim Ingham   OptionValueString m_name;
15505e09c8c3SJim Ingham   OptionValueUInt64 m_breakpoint;
15515e09c8c3SJim Ingham   OptionValueBoolean m_use_dummy;
1552e9632ebaSJim Ingham   OptionValueString m_help_string;
15535e09c8c3SJim Ingham };
15545e09c8c3SJim Ingham 
1555f94668e3SRaphael Isemann #define LLDB_OPTIONS_breakpoint_access
1556f94668e3SRaphael Isemann #include "CommandOptions.inc"
1557b842f2ecSJim Ingham 
15588fe53c49STatyana Krasnukha class BreakpointAccessOptionGroup : public OptionGroup {
1559b842f2ecSJim Ingham public:
15608fe53c49STatyana Krasnukha   BreakpointAccessOptionGroup() : OptionGroup() {}
1561b842f2ecSJim Ingham 
1562b842f2ecSJim Ingham   ~BreakpointAccessOptionGroup() override = default;
1563b842f2ecSJim Ingham 
1564b842f2ecSJim Ingham   llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
1565b842f2ecSJim Ingham     return llvm::makeArrayRef(g_breakpoint_access_options);
1566b842f2ecSJim Ingham   }
1567b842f2ecSJim Ingham   Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1568b842f2ecSJim Ingham                         ExecutionContext *execution_context) override {
1569b842f2ecSJim Ingham     Status error;
1570a925974bSAdrian Prantl     const int short_option =
1571a925974bSAdrian Prantl         g_breakpoint_access_options[option_idx].short_option;
1572b842f2ecSJim Ingham 
1573b842f2ecSJim Ingham     switch (short_option) {
1574b842f2ecSJim Ingham     case 'L': {
1575b842f2ecSJim Ingham       bool value, success;
157647cbf4a0SPavel Labath       value = OptionArgParser::ToBoolean(option_arg, false, &success);
1577b842f2ecSJim Ingham       if (success) {
1578b842f2ecSJim Ingham         m_permissions.SetAllowList(value);
1579b842f2ecSJim Ingham       } else
1580b842f2ecSJim Ingham         error.SetErrorStringWithFormat(
1581b842f2ecSJim Ingham             "invalid boolean value '%s' passed for -L option",
1582b842f2ecSJim Ingham             option_arg.str().c_str());
1583b842f2ecSJim Ingham     } break;
1584b842f2ecSJim Ingham     case 'A': {
1585b842f2ecSJim Ingham       bool value, success;
158647cbf4a0SPavel Labath       value = OptionArgParser::ToBoolean(option_arg, false, &success);
1587b842f2ecSJim Ingham       if (success) {
1588b842f2ecSJim Ingham         m_permissions.SetAllowDisable(value);
1589b842f2ecSJim Ingham       } else
1590b842f2ecSJim Ingham         error.SetErrorStringWithFormat(
1591b842f2ecSJim Ingham             "invalid boolean value '%s' passed for -L option",
1592b842f2ecSJim Ingham             option_arg.str().c_str());
1593b842f2ecSJim Ingham     } break;
1594b842f2ecSJim Ingham     case 'D': {
1595b842f2ecSJim Ingham       bool value, success;
159647cbf4a0SPavel Labath       value = OptionArgParser::ToBoolean(option_arg, false, &success);
1597b842f2ecSJim Ingham       if (success) {
1598b842f2ecSJim Ingham         m_permissions.SetAllowDelete(value);
1599b842f2ecSJim Ingham       } else
1600b842f2ecSJim Ingham         error.SetErrorStringWithFormat(
1601b842f2ecSJim Ingham             "invalid boolean value '%s' passed for -L option",
1602b842f2ecSJim Ingham             option_arg.str().c_str());
1603b842f2ecSJim Ingham     } break;
160436162014SRaphael Isemann     default:
160536162014SRaphael Isemann       llvm_unreachable("Unimplemented option");
1606b842f2ecSJim Ingham     }
1607b842f2ecSJim Ingham 
1608b842f2ecSJim Ingham     return error;
1609b842f2ecSJim Ingham   }
1610b842f2ecSJim Ingham 
1611a925974bSAdrian Prantl   void OptionParsingStarting(ExecutionContext *execution_context) override {}
1612b842f2ecSJim Ingham 
1613a925974bSAdrian Prantl   const BreakpointName::Permissions &GetPermissions() const {
1614b842f2ecSJim Ingham     return m_permissions;
1615b842f2ecSJim Ingham   }
1616b842f2ecSJim Ingham   BreakpointName::Permissions m_permissions;
1617b842f2ecSJim Ingham };
1618b842f2ecSJim Ingham 
1619b842f2ecSJim Ingham class CommandObjectBreakpointNameConfigure : public CommandObjectParsed {
1620b842f2ecSJim Ingham public:
1621b842f2ecSJim Ingham   CommandObjectBreakpointNameConfigure(CommandInterpreter &interpreter)
1622b842f2ecSJim Ingham       : CommandObjectParsed(
1623a925974bSAdrian Prantl             interpreter, "configure",
1624a925974bSAdrian Prantl             "Configure the options for the breakpoint"
1625b842f2ecSJim Ingham             " name provided.  "
1626b842f2ecSJim Ingham             "If you provide a breakpoint id, the options will be copied from "
1627b842f2ecSJim Ingham             "the breakpoint, otherwise only the options specified will be set "
1628b842f2ecSJim Ingham             "on the name.",
1629b842f2ecSJim Ingham             "breakpoint name configure <command-options> "
1630b842f2ecSJim Ingham             "<breakpoint-name-list>"),
1631b842f2ecSJim Ingham         m_bp_opts(), m_option_group() {
1632b842f2ecSJim Ingham     // Create the first variant for the first (and only) argument for this
1633b842f2ecSJim Ingham     // command.
1634b842f2ecSJim Ingham     CommandArgumentEntry arg1;
1635b842f2ecSJim Ingham     CommandArgumentData id_arg;
1636b842f2ecSJim Ingham     id_arg.arg_type = eArgTypeBreakpointName;
1637b842f2ecSJim Ingham     id_arg.arg_repetition = eArgRepeatOptional;
1638b842f2ecSJim Ingham     arg1.push_back(id_arg);
1639b842f2ecSJim Ingham     m_arguments.push_back(arg1);
1640b842f2ecSJim Ingham 
1641a925974bSAdrian Prantl     m_option_group.Append(&m_bp_opts, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
1642a925974bSAdrian Prantl     m_option_group.Append(&m_access_options, LLDB_OPT_SET_ALL,
1643b842f2ecSJim Ingham                           LLDB_OPT_SET_ALL);
1644a925974bSAdrian Prantl     m_option_group.Append(&m_bp_id, LLDB_OPT_SET_2 | LLDB_OPT_SET_4,
1645e9632ebaSJim Ingham                           LLDB_OPT_SET_ALL);
1646b842f2ecSJim Ingham     m_option_group.Finalize();
1647b842f2ecSJim Ingham   }
1648b842f2ecSJim Ingham 
1649b842f2ecSJim Ingham   ~CommandObjectBreakpointNameConfigure() override = default;
1650b842f2ecSJim Ingham 
1651b842f2ecSJim Ingham   Options *GetOptions() override { return &m_option_group; }
1652b842f2ecSJim Ingham 
1653b842f2ecSJim Ingham protected:
1654b842f2ecSJim Ingham   bool DoExecute(Args &command, CommandReturnObject &result) override {
1655b842f2ecSJim Ingham 
1656b842f2ecSJim Ingham     const size_t argc = command.GetArgumentCount();
1657b842f2ecSJim Ingham     if (argc == 0) {
1658b842f2ecSJim Ingham       result.AppendError("No names provided.");
1659b842f2ecSJim Ingham       result.SetStatus(eReturnStatusFailed);
1660b842f2ecSJim Ingham       return false;
1661b842f2ecSJim Ingham     }
1662b842f2ecSJim Ingham 
1663cb2380c9SRaphael Isemann     Target &target = GetSelectedOrDummyTarget(false);
1664b842f2ecSJim Ingham 
1665b842f2ecSJim Ingham     std::unique_lock<std::recursive_mutex> lock;
1666cb2380c9SRaphael Isemann     target.GetBreakpointList().GetListMutex(lock);
1667b842f2ecSJim Ingham 
1668b842f2ecSJim Ingham     // Make a pass through first to see that all the names are legal.
1669b842f2ecSJim Ingham     for (auto &entry : command.entries()) {
1670b842f2ecSJim Ingham       Status error;
1671a925974bSAdrian Prantl       if (!BreakpointID::StringIsBreakpointName(entry.ref(), error)) {
1672b842f2ecSJim Ingham         result.AppendErrorWithFormat("Invalid breakpoint name: %s - %s",
1673b842f2ecSJim Ingham                                      entry.c_str(), error.AsCString());
1674b842f2ecSJim Ingham         result.SetStatus(eReturnStatusFailed);
1675b842f2ecSJim Ingham         return false;
1676b842f2ecSJim Ingham       }
1677b842f2ecSJim Ingham     }
167805097246SAdrian Prantl     // Now configure them, we already pre-checked the names so we don't need to
167905097246SAdrian Prantl     // check the error:
1680b842f2ecSJim Ingham     BreakpointSP bp_sp;
1681a925974bSAdrian Prantl     if (m_bp_id.m_breakpoint.OptionWasSet()) {
1682b842f2ecSJim Ingham       lldb::break_id_t bp_id = m_bp_id.m_breakpoint.GetUInt64Value();
1683cb2380c9SRaphael Isemann       bp_sp = target.GetBreakpointByID(bp_id);
1684a925974bSAdrian Prantl       if (!bp_sp) {
1685b842f2ecSJim Ingham         result.AppendErrorWithFormatv("Could not find specified breakpoint {0}",
1686b842f2ecSJim Ingham                                       bp_id);
1687b842f2ecSJim Ingham         result.SetStatus(eReturnStatusFailed);
1688b842f2ecSJim Ingham         return false;
1689b842f2ecSJim Ingham       }
1690b842f2ecSJim Ingham     }
1691b842f2ecSJim Ingham 
1692b842f2ecSJim Ingham     Status error;
1693b842f2ecSJim Ingham     for (auto &entry : command.entries()) {
1694b842f2ecSJim Ingham       ConstString name(entry.c_str());
1695cb2380c9SRaphael Isemann       BreakpointName *bp_name = target.FindBreakpointName(name, true, error);
1696b842f2ecSJim Ingham       if (!bp_name)
1697b842f2ecSJim Ingham         continue;
1698e9632ebaSJim Ingham       if (m_bp_id.m_help_string.OptionWasSet())
1699e9632ebaSJim Ingham         bp_name->SetHelp(m_bp_id.m_help_string.GetStringValue().str().c_str());
1700e9632ebaSJim Ingham 
1701b842f2ecSJim Ingham       if (bp_sp)
1702cb2380c9SRaphael Isemann         target.ConfigureBreakpointName(*bp_name, *bp_sp->GetOptions(),
1703b842f2ecSJim Ingham                                        m_access_options.GetPermissions());
1704b842f2ecSJim Ingham       else
1705cb2380c9SRaphael Isemann         target.ConfigureBreakpointName(*bp_name,
1706b842f2ecSJim Ingham                                        m_bp_opts.GetBreakpointOptions(),
1707b842f2ecSJim Ingham                                        m_access_options.GetPermissions());
1708b842f2ecSJim Ingham     }
1709b842f2ecSJim Ingham     return true;
1710b842f2ecSJim Ingham   }
1711b842f2ecSJim Ingham 
1712b842f2ecSJim Ingham private:
1713b842f2ecSJim Ingham   BreakpointNameOptionGroup m_bp_id; // Only using the id part of this.
1714b842f2ecSJim Ingham   BreakpointOptionGroup m_bp_opts;
1715b842f2ecSJim Ingham   BreakpointAccessOptionGroup m_access_options;
1716b842f2ecSJim Ingham   OptionGroupOptions m_option_group;
1717b842f2ecSJim Ingham };
1718b842f2ecSJim Ingham 
1719b9c1b51eSKate Stone class CommandObjectBreakpointNameAdd : public CommandObjectParsed {
17205e09c8c3SJim Ingham public:
1721b9c1b51eSKate Stone   CommandObjectBreakpointNameAdd(CommandInterpreter &interpreter)
1722b9c1b51eSKate Stone       : CommandObjectParsed(
1723b9c1b51eSKate Stone             interpreter, "add", "Add a name to the breakpoints provided.",
17245e09c8c3SJim Ingham             "breakpoint name add <command-options> <breakpoint-id-list>"),
1725b9c1b51eSKate Stone         m_name_options(), m_option_group() {
1726b9c1b51eSKate Stone     // Create the first variant for the first (and only) argument for this
1727b9c1b51eSKate Stone     // command.
17285e09c8c3SJim Ingham     CommandArgumentEntry arg1;
17295e09c8c3SJim Ingham     CommandArgumentData id_arg;
17305e09c8c3SJim Ingham     id_arg.arg_type = eArgTypeBreakpointID;
17315e09c8c3SJim Ingham     id_arg.arg_repetition = eArgRepeatOptional;
17325e09c8c3SJim Ingham     arg1.push_back(id_arg);
17335e09c8c3SJim Ingham     m_arguments.push_back(arg1);
17345e09c8c3SJim Ingham 
17355e09c8c3SJim Ingham     m_option_group.Append(&m_name_options, LLDB_OPT_SET_1, LLDB_OPT_SET_ALL);
17365e09c8c3SJim Ingham     m_option_group.Finalize();
17375e09c8c3SJim Ingham   }
17385e09c8c3SJim Ingham 
17399e85e5a8SEugene Zelenko   ~CommandObjectBreakpointNameAdd() override = default;
17405e09c8c3SJim Ingham 
1741b9c1b51eSKate Stone   Options *GetOptions() override { return &m_option_group; }
17425e09c8c3SJim Ingham 
17435e09c8c3SJim Ingham protected:
1744b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
1745b9c1b51eSKate Stone     if (!m_name_options.m_name.OptionWasSet()) {
17465e09c8c3SJim Ingham       result.SetError("No name option provided.");
17475e09c8c3SJim Ingham       return false;
17485e09c8c3SJim Ingham     }
17495e09c8c3SJim Ingham 
1750cb2380c9SRaphael Isemann     Target &target =
1751b9c1b51eSKate Stone         GetSelectedOrDummyTarget(m_name_options.m_use_dummy.GetCurrentValue());
17525e09c8c3SJim Ingham 
1753bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
1754cb2380c9SRaphael Isemann     target.GetBreakpointList().GetListMutex(lock);
17555e09c8c3SJim Ingham 
1756cb2380c9SRaphael Isemann     const BreakpointList &breakpoints = target.GetBreakpointList();
17575e09c8c3SJim Ingham 
17585e09c8c3SJim Ingham     size_t num_breakpoints = breakpoints.GetSize();
1759b9c1b51eSKate Stone     if (num_breakpoints == 0) {
17605e09c8c3SJim Ingham       result.SetError("No breakpoints, cannot add names.");
17615e09c8c3SJim Ingham       result.SetStatus(eReturnStatusFailed);
17625e09c8c3SJim Ingham       return false;
17635e09c8c3SJim Ingham     }
17645e09c8c3SJim Ingham 
17655e09c8c3SJim Ingham     // Particular breakpoint selected; disable that breakpoint.
17665e09c8c3SJim Ingham     BreakpointIDList valid_bp_ids;
1767b9c1b51eSKate Stone     CommandObjectMultiwordBreakpoint::VerifyBreakpointIDs(
1768cb2380c9SRaphael Isemann         command, &target, result, &valid_bp_ids,
1769b842f2ecSJim Ingham         BreakpointName::Permissions::PermissionKinds::listPerm);
17705e09c8c3SJim Ingham 
1771b9c1b51eSKate Stone     if (result.Succeeded()) {
1772b9c1b51eSKate Stone       if (valid_bp_ids.GetSize() == 0) {
17735e09c8c3SJim Ingham         result.SetError("No breakpoints specified, cannot add names.");
17745e09c8c3SJim Ingham         result.SetStatus(eReturnStatusFailed);
17755e09c8c3SJim Ingham         return false;
17765e09c8c3SJim Ingham       }
17775e09c8c3SJim Ingham       size_t num_valid_ids = valid_bp_ids.GetSize();
1778b842f2ecSJim Ingham       const char *bp_name = m_name_options.m_name.GetCurrentValue();
1779b842f2ecSJim Ingham       Status error; // This error reports illegal names, but we've already
1780b842f2ecSJim Ingham                     // checked that, so we don't need to check it again here.
1781b9c1b51eSKate Stone       for (size_t index = 0; index < num_valid_ids; index++) {
1782b9c1b51eSKate Stone         lldb::break_id_t bp_id =
1783b9c1b51eSKate Stone             valid_bp_ids.GetBreakpointIDAtIndex(index).GetBreakpointID();
17845e09c8c3SJim Ingham         BreakpointSP bp_sp = breakpoints.FindBreakpointByID(bp_id);
1785cb2380c9SRaphael Isemann         target.AddNameToBreakpoint(bp_sp, bp_name, error);
17865e09c8c3SJim Ingham       }
17875e09c8c3SJim Ingham     }
17885e09c8c3SJim Ingham 
17895e09c8c3SJim Ingham     return true;
17905e09c8c3SJim Ingham   }
17915e09c8c3SJim Ingham 
17925e09c8c3SJim Ingham private:
17935e09c8c3SJim Ingham   BreakpointNameOptionGroup m_name_options;
17945e09c8c3SJim Ingham   OptionGroupOptions m_option_group;
17955e09c8c3SJim Ingham };
17965e09c8c3SJim Ingham 
1797b9c1b51eSKate Stone class CommandObjectBreakpointNameDelete : public CommandObjectParsed {
17985e09c8c3SJim Ingham public:
1799b9c1b51eSKate Stone   CommandObjectBreakpointNameDelete(CommandInterpreter &interpreter)
1800b9c1b51eSKate Stone       : CommandObjectParsed(
1801b9c1b51eSKate Stone             interpreter, "delete",
18025e09c8c3SJim Ingham             "Delete a name from the breakpoints provided.",
18035e09c8c3SJim Ingham             "breakpoint name delete <command-options> <breakpoint-id-list>"),
1804b9c1b51eSKate Stone         m_name_options(), m_option_group() {
1805b9c1b51eSKate Stone     // Create the first variant for the first (and only) argument for this
1806b9c1b51eSKate Stone     // command.
18075e09c8c3SJim Ingham     CommandArgumentEntry arg1;
18085e09c8c3SJim Ingham     CommandArgumentData id_arg;
18095e09c8c3SJim Ingham     id_arg.arg_type = eArgTypeBreakpointID;
18105e09c8c3SJim Ingham     id_arg.arg_repetition = eArgRepeatOptional;
18115e09c8c3SJim Ingham     arg1.push_back(id_arg);
18125e09c8c3SJim Ingham     m_arguments.push_back(arg1);
18135e09c8c3SJim Ingham 
18145e09c8c3SJim Ingham     m_option_group.Append(&m_name_options, LLDB_OPT_SET_1, LLDB_OPT_SET_ALL);
18155e09c8c3SJim Ingham     m_option_group.Finalize();
18165e09c8c3SJim Ingham   }
18175e09c8c3SJim Ingham 
18189e85e5a8SEugene Zelenko   ~CommandObjectBreakpointNameDelete() override = default;
18195e09c8c3SJim Ingham 
1820b9c1b51eSKate Stone   Options *GetOptions() override { return &m_option_group; }
18215e09c8c3SJim Ingham 
18225e09c8c3SJim Ingham protected:
1823b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
1824b9c1b51eSKate Stone     if (!m_name_options.m_name.OptionWasSet()) {
18255e09c8c3SJim Ingham       result.SetError("No name option provided.");
18265e09c8c3SJim Ingham       return false;
18275e09c8c3SJim Ingham     }
18285e09c8c3SJim Ingham 
1829cb2380c9SRaphael Isemann     Target &target =
1830b9c1b51eSKate Stone         GetSelectedOrDummyTarget(m_name_options.m_use_dummy.GetCurrentValue());
18315e09c8c3SJim Ingham 
1832bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
1833cb2380c9SRaphael Isemann     target.GetBreakpointList().GetListMutex(lock);
18345e09c8c3SJim Ingham 
1835cb2380c9SRaphael Isemann     const BreakpointList &breakpoints = target.GetBreakpointList();
18365e09c8c3SJim Ingham 
18375e09c8c3SJim Ingham     size_t num_breakpoints = breakpoints.GetSize();
1838b9c1b51eSKate Stone     if (num_breakpoints == 0) {
18395e09c8c3SJim Ingham       result.SetError("No breakpoints, cannot delete names.");
18405e09c8c3SJim Ingham       result.SetStatus(eReturnStatusFailed);
18415e09c8c3SJim Ingham       return false;
18425e09c8c3SJim Ingham     }
18435e09c8c3SJim Ingham 
18445e09c8c3SJim Ingham     // Particular breakpoint selected; disable that breakpoint.
18455e09c8c3SJim Ingham     BreakpointIDList valid_bp_ids;
1846b9c1b51eSKate Stone     CommandObjectMultiwordBreakpoint::VerifyBreakpointIDs(
1847cb2380c9SRaphael Isemann         command, &target, result, &valid_bp_ids,
1848b842f2ecSJim Ingham         BreakpointName::Permissions::PermissionKinds::deletePerm);
18495e09c8c3SJim Ingham 
1850b9c1b51eSKate Stone     if (result.Succeeded()) {
1851b9c1b51eSKate Stone       if (valid_bp_ids.GetSize() == 0) {
18525e09c8c3SJim Ingham         result.SetError("No breakpoints specified, cannot delete names.");
18535e09c8c3SJim Ingham         result.SetStatus(eReturnStatusFailed);
18545e09c8c3SJim Ingham         return false;
18555e09c8c3SJim Ingham       }
1856b842f2ecSJim Ingham       ConstString bp_name(m_name_options.m_name.GetCurrentValue());
18575e09c8c3SJim Ingham       size_t num_valid_ids = valid_bp_ids.GetSize();
1858b9c1b51eSKate Stone       for (size_t index = 0; index < num_valid_ids; index++) {
1859b9c1b51eSKate Stone         lldb::break_id_t bp_id =
1860b9c1b51eSKate Stone             valid_bp_ids.GetBreakpointIDAtIndex(index).GetBreakpointID();
18615e09c8c3SJim Ingham         BreakpointSP bp_sp = breakpoints.FindBreakpointByID(bp_id);
1862cb2380c9SRaphael Isemann         target.RemoveNameFromBreakpoint(bp_sp, bp_name);
18635e09c8c3SJim Ingham       }
18645e09c8c3SJim Ingham     }
18655e09c8c3SJim Ingham 
18665e09c8c3SJim Ingham     return true;
18675e09c8c3SJim Ingham   }
18685e09c8c3SJim Ingham 
18695e09c8c3SJim Ingham private:
18705e09c8c3SJim Ingham   BreakpointNameOptionGroup m_name_options;
18715e09c8c3SJim Ingham   OptionGroupOptions m_option_group;
18725e09c8c3SJim Ingham };
18735e09c8c3SJim Ingham 
1874b9c1b51eSKate Stone class CommandObjectBreakpointNameList : public CommandObjectParsed {
18755e09c8c3SJim Ingham public:
1876b9c1b51eSKate Stone   CommandObjectBreakpointNameList(CommandInterpreter &interpreter)
1877b9c1b51eSKate Stone       : CommandObjectParsed(interpreter, "list",
1878b842f2ecSJim Ingham                             "List either the names for a breakpoint or info "
1879b842f2ecSJim Ingham                             "about a given name.  With no arguments, lists all "
1880b842f2ecSJim Ingham                             "names",
18815e09c8c3SJim Ingham                             "breakpoint name list <command-options>"),
1882b9c1b51eSKate Stone         m_name_options(), m_option_group() {
1883b842f2ecSJim Ingham     m_option_group.Append(&m_name_options, LLDB_OPT_SET_3, LLDB_OPT_SET_ALL);
18845e09c8c3SJim Ingham     m_option_group.Finalize();
18855e09c8c3SJim Ingham   }
18865e09c8c3SJim Ingham 
18879e85e5a8SEugene Zelenko   ~CommandObjectBreakpointNameList() override = default;
18885e09c8c3SJim Ingham 
1889b9c1b51eSKate Stone   Options *GetOptions() override { return &m_option_group; }
18905e09c8c3SJim Ingham 
18915e09c8c3SJim Ingham protected:
1892b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
1893cb2380c9SRaphael Isemann     Target &target =
1894b9c1b51eSKate Stone         GetSelectedOrDummyTarget(m_name_options.m_use_dummy.GetCurrentValue());
18955e09c8c3SJim Ingham 
1896b842f2ecSJim Ingham     std::vector<std::string> name_list;
1897b842f2ecSJim Ingham     if (command.empty()) {
1898cb2380c9SRaphael Isemann       target.GetBreakpointNames(name_list);
1899b842f2ecSJim Ingham     } else {
1900a925974bSAdrian Prantl       for (const Args::ArgEntry &arg : command) {
1901b842f2ecSJim Ingham         name_list.push_back(arg.c_str());
1902b842f2ecSJim Ingham       }
1903b842f2ecSJim Ingham     }
1904b842f2ecSJim Ingham 
1905b842f2ecSJim Ingham     if (name_list.empty()) {
1906b842f2ecSJim Ingham       result.AppendMessage("No breakpoint names found.");
1907b842f2ecSJim Ingham     } else {
1908b842f2ecSJim Ingham       for (const std::string &name_str : name_list) {
1909b842f2ecSJim Ingham         const char *name = name_str.c_str();
1910b842f2ecSJim Ingham         // First print out the options for the name:
1911b842f2ecSJim Ingham         Status error;
1912cb2380c9SRaphael Isemann         BreakpointName *bp_name =
1913cb2380c9SRaphael Isemann             target.FindBreakpointName(ConstString(name), false, error);
1914a925974bSAdrian Prantl         if (bp_name) {
1915b842f2ecSJim Ingham           StreamString s;
1916b842f2ecSJim Ingham           result.AppendMessageWithFormat("Name: %s\n", name);
1917a925974bSAdrian Prantl           if (bp_name->GetDescription(&s, eDescriptionLevelFull)) {
1918b842f2ecSJim Ingham             result.AppendMessage(s.GetString());
1919b842f2ecSJim Ingham           }
1920b842f2ecSJim Ingham 
1921bb19a13cSSaleem Abdulrasool           std::unique_lock<std::recursive_mutex> lock;
1922cb2380c9SRaphael Isemann           target.GetBreakpointList().GetListMutex(lock);
19235e09c8c3SJim Ingham 
1924cb2380c9SRaphael Isemann           BreakpointList &breakpoints = target.GetBreakpointList();
1925b842f2ecSJim Ingham           bool any_set = false;
1926b9c1b51eSKate Stone           for (BreakpointSP bp_sp : breakpoints.Breakpoints()) {
1927b9c1b51eSKate Stone             if (bp_sp->MatchesName(name)) {
19285e09c8c3SJim Ingham               StreamString s;
1929b842f2ecSJim Ingham               any_set = true;
19305e09c8c3SJim Ingham               bp_sp->GetDescription(&s, eDescriptionLevelBrief);
19315e09c8c3SJim Ingham               s.EOL();
1932c156427dSZachary Turner               result.AppendMessage(s.GetString());
19335e09c8c3SJim Ingham             }
19345e09c8c3SJim Ingham           }
1935b842f2ecSJim Ingham           if (!any_set)
1936b842f2ecSJim Ingham             result.AppendMessage("No breakpoints using this name.");
1937b9c1b51eSKate Stone         } else {
1938b842f2ecSJim Ingham           result.AppendMessageWithFormat("Name: %s not found.\n", name);
19395e09c8c3SJim Ingham         }
1940b842f2ecSJim Ingham       }
19415e09c8c3SJim Ingham     }
19425e09c8c3SJim Ingham     return true;
19435e09c8c3SJim Ingham   }
19445e09c8c3SJim Ingham 
19455e09c8c3SJim Ingham private:
19465e09c8c3SJim Ingham   BreakpointNameOptionGroup m_name_options;
19475e09c8c3SJim Ingham   OptionGroupOptions m_option_group;
19485e09c8c3SJim Ingham };
19495e09c8c3SJim Ingham 
1950e14dc268SJim Ingham // CommandObjectBreakpointName
1951b9c1b51eSKate Stone class CommandObjectBreakpointName : public CommandObjectMultiword {
19525e09c8c3SJim Ingham public:
19537428a18cSKate Stone   CommandObjectBreakpointName(CommandInterpreter &interpreter)
1954b9c1b51eSKate Stone       : CommandObjectMultiword(
1955b9c1b51eSKate Stone             interpreter, "name", "Commands to manage name tags for breakpoints",
1956b9c1b51eSKate Stone             "breakpoint name <subcommand> [<command-options>]") {
1957b9c1b51eSKate Stone     CommandObjectSP add_command_object(
1958b9c1b51eSKate Stone         new CommandObjectBreakpointNameAdd(interpreter));
1959b9c1b51eSKate Stone     CommandObjectSP delete_command_object(
1960b9c1b51eSKate Stone         new CommandObjectBreakpointNameDelete(interpreter));
1961b9c1b51eSKate Stone     CommandObjectSP list_command_object(
1962b9c1b51eSKate Stone         new CommandObjectBreakpointNameList(interpreter));
1963b842f2ecSJim Ingham     CommandObjectSP configure_command_object(
1964b842f2ecSJim Ingham         new CommandObjectBreakpointNameConfigure(interpreter));
19655e09c8c3SJim Ingham 
19665e09c8c3SJim Ingham     LoadSubCommand("add", add_command_object);
19675e09c8c3SJim Ingham     LoadSubCommand("delete", delete_command_object);
19685e09c8c3SJim Ingham     LoadSubCommand("list", list_command_object);
1969b842f2ecSJim Ingham     LoadSubCommand("configure", configure_command_object);
19705e09c8c3SJim Ingham   }
19715e09c8c3SJim Ingham 
19729e85e5a8SEugene Zelenko   ~CommandObjectBreakpointName() override = default;
19735e09c8c3SJim Ingham };
19745e09c8c3SJim Ingham 
1975e14dc268SJim Ingham // CommandObjectBreakpointRead
19763acdf385SJim Ingham #pragma mark Read::CommandOptions
1977f94668e3SRaphael Isemann #define LLDB_OPTIONS_breakpoint_read
1978f94668e3SRaphael Isemann #include "CommandOptions.inc"
19791f0f5b5bSZachary Turner 
19801f0f5b5bSZachary Turner #pragma mark Read
1981e14dc268SJim Ingham 
1982e14dc268SJim Ingham class CommandObjectBreakpointRead : public CommandObjectParsed {
1983e14dc268SJim Ingham public:
1984e14dc268SJim Ingham   CommandObjectBreakpointRead(CommandInterpreter &interpreter)
1985e14dc268SJim Ingham       : CommandObjectParsed(interpreter, "breakpoint read",
1986e14dc268SJim Ingham                             "Read and set the breakpoints previously saved to "
1987e14dc268SJim Ingham                             "a file with \"breakpoint write\".  ",
1988e14dc268SJim Ingham                             nullptr),
1989e14dc268SJim Ingham         m_options() {
1990e14dc268SJim Ingham     CommandArgumentEntry arg;
1991e14dc268SJim Ingham     CommandObject::AddIDsArgumentData(arg, eArgTypeBreakpointID,
1992e14dc268SJim Ingham                                       eArgTypeBreakpointIDRange);
1993e14dc268SJim Ingham     // Add the entry for the first argument for this command to the object's
1994e14dc268SJim Ingham     // arguments vector.
1995e14dc268SJim Ingham     m_arguments.push_back(arg);
1996e14dc268SJim Ingham   }
1997e14dc268SJim Ingham 
1998e14dc268SJim Ingham   ~CommandObjectBreakpointRead() override = default;
1999e14dc268SJim Ingham 
2000e14dc268SJim Ingham   Options *GetOptions() override { return &m_options; }
2001e14dc268SJim Ingham 
2002e14dc268SJim Ingham   class CommandOptions : public Options {
2003e14dc268SJim Ingham   public:
2004e14dc268SJim Ingham     CommandOptions() : Options() {}
2005e14dc268SJim Ingham 
2006e14dc268SJim Ingham     ~CommandOptions() override = default;
2007e14dc268SJim Ingham 
200897206d57SZachary Turner     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
2009e14dc268SJim Ingham                           ExecutionContext *execution_context) override {
201097206d57SZachary Turner       Status error;
2011e14dc268SJim Ingham       const int short_option = m_getopt_table[option_idx].val;
2012e14dc268SJim Ingham 
2013e14dc268SJim Ingham       switch (short_option) {
2014e14dc268SJim Ingham       case 'f':
2015adcd0268SBenjamin Kramer         m_filename.assign(std::string(option_arg));
2016e14dc268SJim Ingham         break;
20173acdf385SJim Ingham       case 'N': {
201897206d57SZachary Turner         Status name_error;
20193acdf385SJim Ingham         if (!BreakpointID::StringIsBreakpointName(llvm::StringRef(option_arg),
20203acdf385SJim Ingham                                                   name_error)) {
20213acdf385SJim Ingham           error.SetErrorStringWithFormat("Invalid breakpoint name: %s",
20223acdf385SJim Ingham                                          name_error.AsCString());
20233acdf385SJim Ingham         }
2024adcd0268SBenjamin Kramer         m_names.push_back(std::string(option_arg));
20253acdf385SJim Ingham         break;
20263acdf385SJim Ingham       }
2027e14dc268SJim Ingham       default:
202836162014SRaphael Isemann         llvm_unreachable("Unimplemented option");
2029e14dc268SJim Ingham       }
2030e14dc268SJim Ingham 
2031e14dc268SJim Ingham       return error;
2032e14dc268SJim Ingham     }
2033e14dc268SJim Ingham 
2034e14dc268SJim Ingham     void OptionParsingStarting(ExecutionContext *execution_context) override {
2035e14dc268SJim Ingham       m_filename.clear();
20363acdf385SJim Ingham       m_names.clear();
2037e14dc268SJim Ingham     }
2038e14dc268SJim Ingham 
20391f0f5b5bSZachary Turner     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
204070602439SZachary Turner       return llvm::makeArrayRef(g_breakpoint_read_options);
20411f0f5b5bSZachary Turner     }
2042e14dc268SJim Ingham 
2043e14dc268SJim Ingham     // Instance variables to hold the values for command options.
2044e14dc268SJim Ingham 
2045e14dc268SJim Ingham     std::string m_filename;
20463acdf385SJim Ingham     std::vector<std::string> m_names;
2047e14dc268SJim Ingham   };
2048e14dc268SJim Ingham 
2049e14dc268SJim Ingham protected:
2050e14dc268SJim Ingham   bool DoExecute(Args &command, CommandReturnObject &result) override {
2051cb2380c9SRaphael Isemann     Target &target = GetSelectedOrDummyTarget();
2052e14dc268SJim Ingham 
20533acdf385SJim Ingham     std::unique_lock<std::recursive_mutex> lock;
2054cb2380c9SRaphael Isemann     target.GetBreakpointList().GetListMutex(lock);
20553acdf385SJim Ingham 
20568f3be7a3SJonas Devlieghere     FileSpec input_spec(m_options.m_filename);
20578f3be7a3SJonas Devlieghere     FileSystem::Instance().Resolve(input_spec);
205801f16664SJim Ingham     BreakpointIDList new_bps;
2059cb2380c9SRaphael Isemann     Status error = target.CreateBreakpointsFromFile(input_spec,
2060cb2380c9SRaphael Isemann                                                     m_options.m_names, new_bps);
2061e14dc268SJim Ingham 
2062e14dc268SJim Ingham     if (!error.Success()) {
206301f16664SJim Ingham       result.AppendError(error.AsCString());
2064e14dc268SJim Ingham       result.SetStatus(eReturnStatusFailed);
206501f16664SJim Ingham       return false;
2066e14dc268SJim Ingham     }
20673acdf385SJim Ingham 
20683acdf385SJim Ingham     Stream &output_stream = result.GetOutputStream();
20693acdf385SJim Ingham 
20703acdf385SJim Ingham     size_t num_breakpoints = new_bps.GetSize();
20713acdf385SJim Ingham     if (num_breakpoints == 0) {
20723acdf385SJim Ingham       result.AppendMessage("No breakpoints added.");
20733acdf385SJim Ingham     } else {
20743acdf385SJim Ingham       // No breakpoint selected; show info about all currently set breakpoints.
20753acdf385SJim Ingham       result.AppendMessage("New breakpoints:");
20763acdf385SJim Ingham       for (size_t i = 0; i < num_breakpoints; ++i) {
20773acdf385SJim Ingham         BreakpointID bp_id = new_bps.GetBreakpointIDAtIndex(i);
2078cb2380c9SRaphael Isemann         Breakpoint *bp = target.GetBreakpointList()
20793acdf385SJim Ingham                              .FindBreakpointByID(bp_id.GetBreakpointID())
20803acdf385SJim Ingham                              .get();
20813acdf385SJim Ingham         if (bp)
20823acdf385SJim Ingham           bp->GetDescription(&output_stream, lldb::eDescriptionLevelInitial,
20833acdf385SJim Ingham                              false);
20843acdf385SJim Ingham       }
20853acdf385SJim Ingham     }
2086e14dc268SJim Ingham     return result.Succeeded();
2087e14dc268SJim Ingham   }
2088e14dc268SJim Ingham 
2089e14dc268SJim Ingham private:
2090e14dc268SJim Ingham   CommandOptions m_options;
2091e14dc268SJim Ingham };
2092e14dc268SJim Ingham 
2093e14dc268SJim Ingham // CommandObjectBreakpointWrite
20941f0f5b5bSZachary Turner #pragma mark Write::CommandOptions
2095f94668e3SRaphael Isemann #define LLDB_OPTIONS_breakpoint_write
2096f94668e3SRaphael Isemann #include "CommandOptions.inc"
20971f0f5b5bSZachary Turner 
20981f0f5b5bSZachary Turner #pragma mark Write
2099e14dc268SJim Ingham class CommandObjectBreakpointWrite : public CommandObjectParsed {
2100e14dc268SJim Ingham public:
2101e14dc268SJim Ingham   CommandObjectBreakpointWrite(CommandInterpreter &interpreter)
2102e14dc268SJim Ingham       : CommandObjectParsed(interpreter, "breakpoint write",
2103e14dc268SJim Ingham                             "Write the breakpoints listed to a file that can "
2104e14dc268SJim Ingham                             "be read in with \"breakpoint read\".  "
2105e14dc268SJim Ingham                             "If given no arguments, writes all breakpoints.",
2106e14dc268SJim Ingham                             nullptr),
2107e14dc268SJim Ingham         m_options() {
2108e14dc268SJim Ingham     CommandArgumentEntry arg;
2109e14dc268SJim Ingham     CommandObject::AddIDsArgumentData(arg, eArgTypeBreakpointID,
2110e14dc268SJim Ingham                                       eArgTypeBreakpointIDRange);
2111e14dc268SJim Ingham     // Add the entry for the first argument for this command to the object's
2112e14dc268SJim Ingham     // arguments vector.
2113e14dc268SJim Ingham     m_arguments.push_back(arg);
2114e14dc268SJim Ingham   }
2115e14dc268SJim Ingham 
2116e14dc268SJim Ingham   ~CommandObjectBreakpointWrite() override = default;
2117e14dc268SJim Ingham 
2118e14dc268SJim Ingham   Options *GetOptions() override { return &m_options; }
2119e14dc268SJim Ingham 
2120e14dc268SJim Ingham   class CommandOptions : public Options {
2121e14dc268SJim Ingham   public:
2122e14dc268SJim Ingham     CommandOptions() : Options() {}
2123e14dc268SJim Ingham 
2124e14dc268SJim Ingham     ~CommandOptions() override = default;
2125e14dc268SJim Ingham 
212697206d57SZachary Turner     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
2127e14dc268SJim Ingham                           ExecutionContext *execution_context) override {
212897206d57SZachary Turner       Status error;
2129e14dc268SJim Ingham       const int short_option = m_getopt_table[option_idx].val;
2130e14dc268SJim Ingham 
2131e14dc268SJim Ingham       switch (short_option) {
2132e14dc268SJim Ingham       case 'f':
2133adcd0268SBenjamin Kramer         m_filename.assign(std::string(option_arg));
2134e14dc268SJim Ingham         break;
21352d3628e1SJim Ingham       case 'a':
21362d3628e1SJim Ingham         m_append = true;
21372d3628e1SJim Ingham         break;
2138e14dc268SJim Ingham       default:
213936162014SRaphael Isemann         llvm_unreachable("Unimplemented option");
2140e14dc268SJim Ingham       }
2141e14dc268SJim Ingham 
2142e14dc268SJim Ingham       return error;
2143e14dc268SJim Ingham     }
2144e14dc268SJim Ingham 
2145e14dc268SJim Ingham     void OptionParsingStarting(ExecutionContext *execution_context) override {
2146e14dc268SJim Ingham       m_filename.clear();
21472d3628e1SJim Ingham       m_append = false;
2148e14dc268SJim Ingham     }
2149e14dc268SJim Ingham 
21501f0f5b5bSZachary Turner     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
215170602439SZachary Turner       return llvm::makeArrayRef(g_breakpoint_write_options);
21521f0f5b5bSZachary Turner     }
2153e14dc268SJim Ingham 
2154e14dc268SJim Ingham     // Instance variables to hold the values for command options.
2155e14dc268SJim Ingham 
2156e14dc268SJim Ingham     std::string m_filename;
21572d3628e1SJim Ingham     bool m_append = false;
2158e14dc268SJim Ingham   };
2159e14dc268SJim Ingham 
2160e14dc268SJim Ingham protected:
2161e14dc268SJim Ingham   bool DoExecute(Args &command, CommandReturnObject &result) override {
2162cb2380c9SRaphael Isemann     Target &target = GetSelectedOrDummyTarget();
2163e14dc268SJim Ingham 
2164e14dc268SJim Ingham     std::unique_lock<std::recursive_mutex> lock;
2165cb2380c9SRaphael Isemann     target.GetBreakpointList().GetListMutex(lock);
2166e14dc268SJim Ingham 
2167e14dc268SJim Ingham     BreakpointIDList valid_bp_ids;
216811eb9c64SZachary Turner     if (!command.empty()) {
2169e14dc268SJim Ingham       CommandObjectMultiwordBreakpoint::VerifyBreakpointIDs(
2170cb2380c9SRaphael Isemann           command, &target, result, &valid_bp_ids,
2171b842f2ecSJim Ingham           BreakpointName::Permissions::PermissionKinds::listPerm);
2172e14dc268SJim Ingham 
217301f16664SJim Ingham       if (!result.Succeeded()) {
2174e14dc268SJim Ingham         result.SetStatus(eReturnStatusFailed);
2175e14dc268SJim Ingham         return false;
2176e14dc268SJim Ingham       }
2177e14dc268SJim Ingham     }
21788f3be7a3SJonas Devlieghere     FileSpec file_spec(m_options.m_filename);
21798f3be7a3SJonas Devlieghere     FileSystem::Instance().Resolve(file_spec);
2180cb2380c9SRaphael Isemann     Status error = target.SerializeBreakpointsToFile(file_spec, valid_bp_ids,
21818f3be7a3SJonas Devlieghere                                                      m_options.m_append);
218201f16664SJim Ingham     if (!error.Success()) {
218301f16664SJim Ingham       result.AppendErrorWithFormat("error serializing breakpoints: %s.",
218401f16664SJim Ingham                                    error.AsCString());
218501f16664SJim Ingham       result.SetStatus(eReturnStatusFailed);
2186e14dc268SJim Ingham     }
2187e14dc268SJim Ingham     return result.Succeeded();
2188e14dc268SJim Ingham   }
2189e14dc268SJim Ingham 
2190e14dc268SJim Ingham private:
2191e14dc268SJim Ingham   CommandOptions m_options;
2192e14dc268SJim Ingham };
2193e14dc268SJim Ingham 
219430fdc8d8SChris Lattner // CommandObjectMultiwordBreakpoint
2195ae1c4cf5SJim Ingham #pragma mark MultiwordBreakpoint
219630fdc8d8SChris Lattner 
2197b9c1b51eSKate Stone CommandObjectMultiwordBreakpoint::CommandObjectMultiwordBreakpoint(
2198b9c1b51eSKate Stone     CommandInterpreter &interpreter)
2199b9c1b51eSKate Stone     : CommandObjectMultiword(
2200b9c1b51eSKate Stone           interpreter, "breakpoint",
22017428a18cSKate Stone           "Commands for operating on breakpoints (see 'help b' for shorthand.)",
2202b9c1b51eSKate Stone           "breakpoint <subcommand> [<command-options>]") {
2203b9c1b51eSKate Stone   CommandObjectSP list_command_object(
2204b9c1b51eSKate Stone       new CommandObjectBreakpointList(interpreter));
2205b9c1b51eSKate Stone   CommandObjectSP enable_command_object(
2206b9c1b51eSKate Stone       new CommandObjectBreakpointEnable(interpreter));
2207b9c1b51eSKate Stone   CommandObjectSP disable_command_object(
2208b9c1b51eSKate Stone       new CommandObjectBreakpointDisable(interpreter));
2209b9c1b51eSKate Stone   CommandObjectSP clear_command_object(
2210b9c1b51eSKate Stone       new CommandObjectBreakpointClear(interpreter));
2211b9c1b51eSKate Stone   CommandObjectSP delete_command_object(
2212b9c1b51eSKate Stone       new CommandObjectBreakpointDelete(interpreter));
2213b9c1b51eSKate Stone   CommandObjectSP set_command_object(
2214b9c1b51eSKate Stone       new CommandObjectBreakpointSet(interpreter));
2215b9c1b51eSKate Stone   CommandObjectSP command_command_object(
2216b9c1b51eSKate Stone       new CommandObjectBreakpointCommand(interpreter));
2217b9c1b51eSKate Stone   CommandObjectSP modify_command_object(
2218b9c1b51eSKate Stone       new CommandObjectBreakpointModify(interpreter));
2219b9c1b51eSKate Stone   CommandObjectSP name_command_object(
2220b9c1b51eSKate Stone       new CommandObjectBreakpointName(interpreter));
2221e14dc268SJim Ingham   CommandObjectSP write_command_object(
2222e14dc268SJim Ingham       new CommandObjectBreakpointWrite(interpreter));
2223e14dc268SJim Ingham   CommandObjectSP read_command_object(
2224e14dc268SJim Ingham       new CommandObjectBreakpointRead(interpreter));
222530fdc8d8SChris Lattner 
2226b7234e40SJohnny Chen   list_command_object->SetCommandName("breakpoint list");
222730fdc8d8SChris Lattner   enable_command_object->SetCommandName("breakpoint enable");
222830fdc8d8SChris Lattner   disable_command_object->SetCommandName("breakpoint disable");
2229b7234e40SJohnny Chen   clear_command_object->SetCommandName("breakpoint clear");
2230b7234e40SJohnny Chen   delete_command_object->SetCommandName("breakpoint delete");
2231ae1c4cf5SJim Ingham   set_command_object->SetCommandName("breakpoint set");
2232b7234e40SJohnny Chen   command_command_object->SetCommandName("breakpoint command");
2233b7234e40SJohnny Chen   modify_command_object->SetCommandName("breakpoint modify");
22345e09c8c3SJim Ingham   name_command_object->SetCommandName("breakpoint name");
2235e14dc268SJim Ingham   write_command_object->SetCommandName("breakpoint write");
2236e14dc268SJim Ingham   read_command_object->SetCommandName("breakpoint read");
223730fdc8d8SChris Lattner 
223823f59509SGreg Clayton   LoadSubCommand("list", list_command_object);
223923f59509SGreg Clayton   LoadSubCommand("enable", enable_command_object);
224023f59509SGreg Clayton   LoadSubCommand("disable", disable_command_object);
224123f59509SGreg Clayton   LoadSubCommand("clear", clear_command_object);
224223f59509SGreg Clayton   LoadSubCommand("delete", delete_command_object);
224323f59509SGreg Clayton   LoadSubCommand("set", set_command_object);
224423f59509SGreg Clayton   LoadSubCommand("command", command_command_object);
224523f59509SGreg Clayton   LoadSubCommand("modify", modify_command_object);
22465e09c8c3SJim Ingham   LoadSubCommand("name", name_command_object);
2247e14dc268SJim Ingham   LoadSubCommand("write", write_command_object);
2248e14dc268SJim Ingham   LoadSubCommand("read", read_command_object);
224930fdc8d8SChris Lattner }
225030fdc8d8SChris Lattner 
22519e85e5a8SEugene Zelenko CommandObjectMultiwordBreakpoint::~CommandObjectMultiwordBreakpoint() = default;
225230fdc8d8SChris Lattner 
2253a925974bSAdrian Prantl void CommandObjectMultiwordBreakpoint::VerifyIDs(
2254a925974bSAdrian Prantl     Args &args, Target *target, bool allow_locations,
2255a925974bSAdrian Prantl     CommandReturnObject &result, BreakpointIDList *valid_ids,
2256a925974bSAdrian Prantl     BreakpointName::Permissions ::PermissionKinds purpose) {
225730fdc8d8SChris Lattner   // args can be strings representing 1). integers (for breakpoint ids)
2258b9c1b51eSKate Stone   //                                  2). the full breakpoint & location
2259b9c1b51eSKate Stone   //                                  canonical representation
2260b9c1b51eSKate Stone   //                                  3). the word "to" or a hyphen,
2261b9c1b51eSKate Stone   //                                  representing a range (in which case there
2262b9c1b51eSKate Stone   //                                      had *better* be an entry both before &
2263b9c1b51eSKate Stone   //                                      after of one of the first two types.
22645e09c8c3SJim Ingham   //                                  4). A breakpoint name
2265b9c1b51eSKate Stone   // If args is empty, we will use the last created breakpoint (if there is
2266b9c1b51eSKate Stone   // one.)
226730fdc8d8SChris Lattner 
226830fdc8d8SChris Lattner   Args temp_args;
226930fdc8d8SChris Lattner 
227011eb9c64SZachary Turner   if (args.empty()) {
2271b9c1b51eSKate Stone     if (target->GetLastCreatedBreakpoint()) {
2272b9c1b51eSKate Stone       valid_ids->AddBreakpointID(BreakpointID(
2273b9c1b51eSKate Stone           target->GetLastCreatedBreakpoint()->GetID(), LLDB_INVALID_BREAK_ID));
227436f3b369SJim Ingham       result.SetStatus(eReturnStatusSuccessFinishNoResult);
2275b9c1b51eSKate Stone     } else {
2276b9c1b51eSKate Stone       result.AppendError(
2277b9c1b51eSKate Stone           "No breakpoint specified and no last created breakpoint.");
227836f3b369SJim Ingham       result.SetStatus(eReturnStatusFailed);
227936f3b369SJim Ingham     }
228036f3b369SJim Ingham     return;
228136f3b369SJim Ingham   }
228236f3b369SJim Ingham 
2283b9c1b51eSKate Stone   // Create a new Args variable to use; copy any non-breakpoint-id-ranges stuff
228405097246SAdrian Prantl   // directly from the old ARGS to the new TEMP_ARGS.  Do not copy breakpoint
228505097246SAdrian Prantl   // id range strings over; instead generate a list of strings for all the
228605097246SAdrian Prantl   // breakpoint ids in the range, and shove all of those breakpoint id strings
228705097246SAdrian Prantl   // into TEMP_ARGS.
228830fdc8d8SChris Lattner 
2289b9c1b51eSKate Stone   BreakpointIDList::FindAndReplaceIDRanges(args, target, allow_locations,
2290b842f2ecSJim Ingham                                            purpose, result, temp_args);
229130fdc8d8SChris Lattner 
2292b9c1b51eSKate Stone   // NOW, convert the list of breakpoint id strings in TEMP_ARGS into an actual
2293b9c1b51eSKate Stone   // BreakpointIDList:
229430fdc8d8SChris Lattner 
229516662f3cSPavel Labath   valid_ids->InsertStringArray(temp_args.GetArgumentArrayRef(), result);
229630fdc8d8SChris Lattner 
229705097246SAdrian Prantl   // At this point,  all of the breakpoint ids that the user passed in have
229805097246SAdrian Prantl   // been converted to breakpoint IDs and put into valid_ids.
229930fdc8d8SChris Lattner 
2300b9c1b51eSKate Stone   if (result.Succeeded()) {
2301b9c1b51eSKate Stone     // Now that we've converted everything from args into a list of breakpoint
230205097246SAdrian Prantl     // ids, go through our tentative list of breakpoint id's and verify that
230305097246SAdrian Prantl     // they correspond to valid/currently set breakpoints.
230430fdc8d8SChris Lattner 
2305c982c768SGreg Clayton     const size_t count = valid_ids->GetSize();
2306b9c1b51eSKate Stone     for (size_t i = 0; i < count; ++i) {
230730fdc8d8SChris Lattner       BreakpointID cur_bp_id = valid_ids->GetBreakpointIDAtIndex(i);
2308b9c1b51eSKate Stone       Breakpoint *breakpoint =
2309b9c1b51eSKate Stone           target->GetBreakpointByID(cur_bp_id.GetBreakpointID()).get();
2310b9c1b51eSKate Stone       if (breakpoint != nullptr) {
2311c7bece56SGreg Clayton         const size_t num_locations = breakpoint->GetNumLocations();
2312b9c1b51eSKate Stone         if (static_cast<size_t>(cur_bp_id.GetLocationID()) > num_locations) {
231330fdc8d8SChris Lattner           StreamString id_str;
2314b9c1b51eSKate Stone           BreakpointID::GetCanonicalReference(
2315b9c1b51eSKate Stone               &id_str, cur_bp_id.GetBreakpointID(), cur_bp_id.GetLocationID());
2316c982c768SGreg Clayton           i = valid_ids->GetSize() + 1;
2317b9c1b51eSKate Stone           result.AppendErrorWithFormat(
2318b9c1b51eSKate Stone               "'%s' is not a currently valid breakpoint/location id.\n",
231930fdc8d8SChris Lattner               id_str.GetData());
232030fdc8d8SChris Lattner           result.SetStatus(eReturnStatusFailed);
232130fdc8d8SChris Lattner         }
2322b9c1b51eSKate Stone       } else {
2323c982c768SGreg Clayton         i = valid_ids->GetSize() + 1;
2324b9c1b51eSKate Stone         result.AppendErrorWithFormat(
2325b9c1b51eSKate Stone             "'%d' is not a currently valid breakpoint ID.\n",
23267428a18cSKate Stone             cur_bp_id.GetBreakpointID());
232730fdc8d8SChris Lattner         result.SetStatus(eReturnStatusFailed);
232830fdc8d8SChris Lattner       }
232930fdc8d8SChris Lattner     }
233030fdc8d8SChris Lattner   }
233130fdc8d8SChris Lattner }
2332