130fdc8d8SChris Lattner //===-- CommandObjectBreakpoint.cpp -----------------------------*- C++ -*-===//
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/CommandCompletions.h"
16b9c1b51eSKate Stone #include "lldb/Interpreter/CommandInterpreter.h"
17b9c1b51eSKate Stone #include "lldb/Interpreter/CommandReturnObject.h"
1847cbf4a0SPavel Labath #include "lldb/Interpreter/OptionArgParser.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/Thread.h"
271b54c88cSJim Ingham #include "lldb/Target/ThreadSpec.h"
28bf9a7730SZachary Turner #include "lldb/Utility/RegularExpression.h"
29bf9a7730SZachary Turner #include "lldb/Utility/StreamString.h"
3030fdc8d8SChris Lattner 
31796ac80bSJonas Devlieghere #include <memory>
32796ac80bSJonas Devlieghere #include <vector>
33796ac80bSJonas Devlieghere 
3430fdc8d8SChris Lattner using namespace lldb;
3530fdc8d8SChris Lattner using namespace lldb_private;
3630fdc8d8SChris Lattner 
37b9c1b51eSKate Stone static void AddBreakpointDescription(Stream *s, Breakpoint *bp,
38b9c1b51eSKate Stone                                      lldb::DescriptionLevel level) {
3930fdc8d8SChris Lattner   s->IndentMore();
4030fdc8d8SChris Lattner   bp->GetDescription(s, level, true);
4130fdc8d8SChris Lattner   s->IndentLess();
4230fdc8d8SChris Lattner   s->EOL();
4330fdc8d8SChris Lattner }
4430fdc8d8SChris Lattner 
45b842f2ecSJim Ingham // Modifiable Breakpoint Options
46b842f2ecSJim Ingham #pragma mark Modify::CommandOptions
47f94668e3SRaphael Isemann #define LLDB_OPTIONS_breakpoint_modify
48f94668e3SRaphael Isemann #include "CommandOptions.inc"
49bd68a052SRaphael Isemann 
50b842f2ecSJim Ingham class lldb_private::BreakpointOptionGroup : public OptionGroup
51b842f2ecSJim Ingham {
52b842f2ecSJim Ingham public:
53b842f2ecSJim Ingham   BreakpointOptionGroup() :
54b842f2ecSJim Ingham           OptionGroup(),
55b842f2ecSJim Ingham           m_bp_opts(false) {}
56b842f2ecSJim Ingham 
57b842f2ecSJim Ingham   ~BreakpointOptionGroup() override = default;
58b842f2ecSJim Ingham 
59b842f2ecSJim Ingham   llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
60b842f2ecSJim Ingham     return llvm::makeArrayRef(g_breakpoint_modify_options);
61b842f2ecSJim Ingham   }
62b842f2ecSJim Ingham 
63b842f2ecSJim Ingham   Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
64b842f2ecSJim Ingham                           ExecutionContext *execution_context) override {
65b842f2ecSJim Ingham     Status error;
66b842f2ecSJim Ingham     const int short_option = g_breakpoint_modify_options[option_idx].short_option;
67b842f2ecSJim Ingham 
68b842f2ecSJim Ingham     switch (short_option) {
69b842f2ecSJim Ingham     case 'c':
7005097246SAdrian Prantl       // Normally an empty breakpoint condition marks is as unset. But we need
7105097246SAdrian Prantl       // to say it was passed in.
72b842f2ecSJim Ingham       m_bp_opts.SetCondition(option_arg.str().c_str());
73b842f2ecSJim Ingham       m_bp_opts.m_set_flags.Set(BreakpointOptions::eCondition);
74b842f2ecSJim Ingham       break;
75b842f2ecSJim Ingham     case 'C':
76b842f2ecSJim Ingham       m_commands.push_back(option_arg);
77b842f2ecSJim Ingham       break;
78b842f2ecSJim Ingham     case 'd':
79b842f2ecSJim Ingham       m_bp_opts.SetEnabled(false);
80b842f2ecSJim Ingham       break;
81b842f2ecSJim Ingham     case 'e':
82b842f2ecSJim Ingham       m_bp_opts.SetEnabled(true);
83b842f2ecSJim Ingham       break;
84b842f2ecSJim Ingham     case 'G': {
85b842f2ecSJim Ingham       bool value, success;
8647cbf4a0SPavel Labath       value = OptionArgParser::ToBoolean(option_arg, false, &success);
87b842f2ecSJim Ingham       if (success) {
88b842f2ecSJim Ingham         m_bp_opts.SetAutoContinue(value);
89b842f2ecSJim Ingham       } else
90b842f2ecSJim Ingham         error.SetErrorStringWithFormat(
91b842f2ecSJim Ingham             "invalid boolean value '%s' passed for -G option",
92b842f2ecSJim Ingham             option_arg.str().c_str());
93b842f2ecSJim Ingham     }
94b842f2ecSJim Ingham     break;
95b842f2ecSJim Ingham     case 'i':
96b842f2ecSJim Ingham     {
97b842f2ecSJim Ingham       uint32_t ignore_count;
98b842f2ecSJim Ingham       if (option_arg.getAsInteger(0, ignore_count))
99b842f2ecSJim Ingham         error.SetErrorStringWithFormat("invalid ignore count '%s'",
100b842f2ecSJim Ingham                                        option_arg.str().c_str());
101b842f2ecSJim Ingham       else
102b842f2ecSJim Ingham         m_bp_opts.SetIgnoreCount(ignore_count);
103b842f2ecSJim Ingham     }
104b842f2ecSJim Ingham     break;
105b842f2ecSJim Ingham     case 'o': {
106b842f2ecSJim Ingham       bool value, success;
10747cbf4a0SPavel Labath       value = OptionArgParser::ToBoolean(option_arg, false, &success);
108b842f2ecSJim Ingham       if (success) {
109b842f2ecSJim Ingham         m_bp_opts.SetOneShot(value);
110b842f2ecSJim Ingham       } else
111b842f2ecSJim Ingham         error.SetErrorStringWithFormat(
112b842f2ecSJim Ingham             "invalid boolean value '%s' passed for -o option",
113b842f2ecSJim Ingham             option_arg.str().c_str());
114b842f2ecSJim Ingham     } break;
115b842f2ecSJim Ingham     case 't':
116b842f2ecSJim Ingham     {
117b842f2ecSJim Ingham       lldb::tid_t thread_id = LLDB_INVALID_THREAD_ID;
118b842f2ecSJim Ingham       if (option_arg[0] != '\0') {
119b842f2ecSJim Ingham         if (option_arg.getAsInteger(0, thread_id))
120b842f2ecSJim Ingham           error.SetErrorStringWithFormat("invalid thread id string '%s'",
121b842f2ecSJim Ingham                                          option_arg.str().c_str());
122b842f2ecSJim Ingham       }
123b842f2ecSJim Ingham       m_bp_opts.SetThreadID(thread_id);
124b842f2ecSJim Ingham     }
125b842f2ecSJim Ingham     break;
126b842f2ecSJim Ingham     case 'T':
127b842f2ecSJim Ingham       m_bp_opts.GetThreadSpec()->SetName(option_arg.str().c_str());
128b842f2ecSJim Ingham       break;
129b842f2ecSJim Ingham     case 'q':
130b842f2ecSJim Ingham       m_bp_opts.GetThreadSpec()->SetQueueName(option_arg.str().c_str());
131b842f2ecSJim Ingham       break;
132b842f2ecSJim Ingham     case 'x':
133b842f2ecSJim Ingham     {
134b842f2ecSJim Ingham       uint32_t thread_index = UINT32_MAX;
135b842f2ecSJim Ingham       if (option_arg[0] != '\n') {
136b842f2ecSJim Ingham         if (option_arg.getAsInteger(0, thread_index))
137b842f2ecSJim Ingham           error.SetErrorStringWithFormat("invalid thread index string '%s'",
138b842f2ecSJim Ingham                                          option_arg.str().c_str());
139b842f2ecSJim Ingham       }
140b842f2ecSJim Ingham       m_bp_opts.GetThreadSpec()->SetIndex(thread_index);
141b842f2ecSJim Ingham     }
142b842f2ecSJim Ingham     break;
143b842f2ecSJim Ingham     default:
144b842f2ecSJim Ingham       error.SetErrorStringWithFormat("unrecognized option '%c'",
145b842f2ecSJim Ingham                                      short_option);
146b842f2ecSJim Ingham       break;
147b842f2ecSJim Ingham     }
148b842f2ecSJim Ingham 
149b842f2ecSJim Ingham     return error;
150b842f2ecSJim Ingham   }
151b842f2ecSJim Ingham 
152b842f2ecSJim Ingham   void OptionParsingStarting(ExecutionContext *execution_context) override {
153b842f2ecSJim Ingham     m_bp_opts.Clear();
154b842f2ecSJim Ingham     m_commands.clear();
155b842f2ecSJim Ingham   }
156b842f2ecSJim Ingham 
157b842f2ecSJim Ingham   Status OptionParsingFinished(ExecutionContext *execution_context) override {
158b842f2ecSJim Ingham     if (!m_commands.empty())
159b842f2ecSJim Ingham     {
160b842f2ecSJim Ingham       if (!m_commands.empty())
161b842f2ecSJim Ingham       {
162a8f3ae7cSJonas Devlieghere           auto cmd_data = std::make_unique<BreakpointOptions::CommandData>();
163b842f2ecSJim Ingham 
164b842f2ecSJim Ingham           for (std::string &str : m_commands)
165b842f2ecSJim Ingham             cmd_data->user_source.AppendString(str);
166b842f2ecSJim Ingham 
167b842f2ecSJim Ingham           cmd_data->stop_on_error = true;
168b842f2ecSJim Ingham           m_bp_opts.SetCommandDataCallback(cmd_data);
169b842f2ecSJim Ingham       }
170b842f2ecSJim Ingham     }
171b842f2ecSJim Ingham     return Status();
172b842f2ecSJim Ingham   }
173b842f2ecSJim Ingham 
174b842f2ecSJim Ingham   const BreakpointOptions &GetBreakpointOptions()
175b842f2ecSJim Ingham   {
176b842f2ecSJim Ingham     return m_bp_opts;
177b842f2ecSJim Ingham   }
178b842f2ecSJim Ingham 
179b842f2ecSJim Ingham   std::vector<std::string> m_commands;
180b842f2ecSJim Ingham   BreakpointOptions m_bp_opts;
181b842f2ecSJim Ingham 
182b842f2ecSJim Ingham };
183bd68a052SRaphael Isemann 
184f94668e3SRaphael Isemann #define LLDB_OPTIONS_breakpoint_dummy
185f94668e3SRaphael Isemann #include "CommandOptions.inc"
186b842f2ecSJim Ingham 
187b842f2ecSJim Ingham class BreakpointDummyOptionGroup : public OptionGroup
188b842f2ecSJim Ingham {
189b842f2ecSJim Ingham public:
190b842f2ecSJim Ingham   BreakpointDummyOptionGroup() :
191b842f2ecSJim Ingham           OptionGroup() {}
192b842f2ecSJim Ingham 
193b842f2ecSJim Ingham   ~BreakpointDummyOptionGroup() override = default;
194b842f2ecSJim Ingham 
195b842f2ecSJim Ingham   llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
196b842f2ecSJim Ingham     return llvm::makeArrayRef(g_breakpoint_dummy_options);
197b842f2ecSJim Ingham   }
198b842f2ecSJim Ingham 
199b842f2ecSJim Ingham   Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
200b842f2ecSJim Ingham                           ExecutionContext *execution_context) override {
201b842f2ecSJim Ingham     Status error;
202b842f2ecSJim Ingham     const int short_option = g_breakpoint_modify_options[option_idx].short_option;
203b842f2ecSJim Ingham 
204b842f2ecSJim Ingham     switch (short_option) {
205b842f2ecSJim Ingham       case 'D':
206b842f2ecSJim Ingham         m_use_dummy = true;
207b842f2ecSJim Ingham         break;
208b842f2ecSJim Ingham     default:
209b842f2ecSJim Ingham       error.SetErrorStringWithFormat("unrecognized option '%c'",
210b842f2ecSJim Ingham                                      short_option);
211b842f2ecSJim Ingham       break;
212b842f2ecSJim Ingham     }
213b842f2ecSJim Ingham 
214b842f2ecSJim Ingham     return error;
215b842f2ecSJim Ingham   }
216b842f2ecSJim Ingham 
217b842f2ecSJim Ingham   void OptionParsingStarting(ExecutionContext *execution_context) override {
218b842f2ecSJim Ingham     m_use_dummy = false;
219b842f2ecSJim Ingham   }
220b842f2ecSJim Ingham 
221b842f2ecSJim Ingham   bool m_use_dummy;
222b842f2ecSJim Ingham 
223b842f2ecSJim Ingham };
224b842f2ecSJim Ingham 
225f94668e3SRaphael Isemann #define LLDB_OPTIONS_breakpoint_set
226f94668e3SRaphael Isemann #include "CommandOptions.inc"
2271f0f5b5bSZachary Turner 
2285a988416SJim Ingham // CommandObjectBreakpointSet
22930fdc8d8SChris Lattner 
230b9c1b51eSKate Stone class CommandObjectBreakpointSet : public CommandObjectParsed {
2315a988416SJim Ingham public:
232efe8e7e3SFangrui Song   enum BreakpointSetType {
2335a988416SJim Ingham     eSetTypeInvalid,
2345a988416SJim Ingham     eSetTypeFileAndLine,
2355a988416SJim Ingham     eSetTypeAddress,
2365a988416SJim Ingham     eSetTypeFunctionName,
2375a988416SJim Ingham     eSetTypeFunctionRegexp,
2385a988416SJim Ingham     eSetTypeSourceRegexp,
2393815e702SJim Ingham     eSetTypeException,
2403815e702SJim Ingham     eSetTypeScripted,
241efe8e7e3SFangrui Song   };
2425a988416SJim Ingham 
243b9c1b51eSKate Stone   CommandObjectBreakpointSet(CommandInterpreter &interpreter)
244b9c1b51eSKate Stone       : CommandObjectParsed(
245b9c1b51eSKate Stone             interpreter, "breakpoint set",
2465a988416SJim Ingham             "Sets a breakpoint or set of breakpoints in the executable.",
2475a988416SJim Ingham             "breakpoint set <cmd-options>"),
248b842f2ecSJim Ingham         m_bp_opts(), m_options() {
249b842f2ecSJim Ingham           // We're picking up all the normal options, commands and disable.
250b842f2ecSJim Ingham           m_all_options.Append(&m_bp_opts,
251b842f2ecSJim Ingham                                LLDB_OPT_SET_1 | LLDB_OPT_SET_3 | LLDB_OPT_SET_4,
252b842f2ecSJim Ingham                                LLDB_OPT_SET_ALL);
253b842f2ecSJim Ingham           m_all_options.Append(&m_dummy_options, LLDB_OPT_SET_1, LLDB_OPT_SET_ALL);
254b842f2ecSJim Ingham           m_all_options.Append(&m_options);
255b842f2ecSJim Ingham           m_all_options.Finalize();
256b842f2ecSJim Ingham         }
2575a988416SJim Ingham 
2589e85e5a8SEugene Zelenko   ~CommandObjectBreakpointSet() override = default;
2595a988416SJim Ingham 
260b842f2ecSJim Ingham   Options *GetOptions() override { return &m_all_options; }
2615a988416SJim Ingham 
262b842f2ecSJim Ingham   class CommandOptions : public OptionGroup {
2635a988416SJim Ingham   public:
264b9c1b51eSKate Stone     CommandOptions()
265b842f2ecSJim Ingham         : OptionGroup(), m_condition(), m_filenames(), m_line_num(0), m_column(0),
266b9c1b51eSKate Stone           m_func_names(), m_func_name_type_mask(eFunctionNameTypeNone),
267b9c1b51eSKate Stone           m_func_regexp(), m_source_text_regexp(), m_modules(), m_load_addr(),
268b9c1b51eSKate Stone           m_catch_bp(false), m_throw_bp(true), m_hardware(false),
269a72b31c7SJim Ingham           m_exception_language(eLanguageTypeUnknown),
27023b1decbSDawn Perchik           m_language(lldb::eLanguageTypeUnknown),
271b842f2ecSJim Ingham           m_skip_prologue(eLazyBoolCalculate),
272b9c1b51eSKate Stone           m_all_files(false), m_move_to_nearest_code(eLazyBoolCalculate) {}
27330fdc8d8SChris Lattner 
2749e85e5a8SEugene Zelenko     ~CommandOptions() override = default;
27587df91b8SJim Ingham 
27697206d57SZachary Turner     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
277b9c1b51eSKate Stone                           ExecutionContext *execution_context) override {
27897206d57SZachary Turner       Status error;
279b842f2ecSJim Ingham       const int short_option = g_breakpoint_set_options[option_idx].short_option;
28030fdc8d8SChris Lattner 
281b9c1b51eSKate Stone       switch (short_option) {
282b9c1b51eSKate Stone       case 'a': {
28347cbf4a0SPavel Labath         m_load_addr = OptionArgParser::ToAddress(execution_context, option_arg,
284e1cfbc79STodd Fiala                                                  LLDB_INVALID_ADDRESS, &error);
285b9c1b51eSKate Stone       } break;
28630fdc8d8SChris Lattner 
287e732052fSJim Ingham       case 'A':
288e732052fSJim Ingham         m_all_files = true;
289e732052fSJim Ingham         break;
290e732052fSJim Ingham 
291ca36cd16SJim Ingham       case 'b':
292ca36cd16SJim Ingham         m_func_names.push_back(option_arg);
293ca36cd16SJim Ingham         m_func_name_type_mask |= eFunctionNameTypeBase;
294ca36cd16SJim Ingham         break;
295ca36cd16SJim Ingham 
296fe11483bSZachary Turner       case 'C':
297fe11483bSZachary Turner         if (option_arg.getAsInteger(0, m_column))
298b9c1b51eSKate Stone           error.SetErrorStringWithFormat("invalid column number: %s",
299fe11483bSZachary Turner                                          option_arg.str().c_str());
30030fdc8d8SChris Lattner         break;
3019e85e5a8SEugene Zelenko 
302b9c1b51eSKate Stone       case 'E': {
303fe11483bSZachary Turner         LanguageType language = Language::GetLanguageTypeFromString(option_arg);
304fab10e89SJim Ingham 
305b9c1b51eSKate Stone         switch (language) {
306fab10e89SJim Ingham         case eLanguageTypeC89:
307fab10e89SJim Ingham         case eLanguageTypeC:
308fab10e89SJim Ingham         case eLanguageTypeC99:
3091d0089faSBruce Mitchener         case eLanguageTypeC11:
310a72b31c7SJim Ingham           m_exception_language = eLanguageTypeC;
311fab10e89SJim Ingham           break;
312fab10e89SJim Ingham         case eLanguageTypeC_plus_plus:
3131d0089faSBruce Mitchener         case eLanguageTypeC_plus_plus_03:
3141d0089faSBruce Mitchener         case eLanguageTypeC_plus_plus_11:
3152ba84a6aSBruce Mitchener         case eLanguageTypeC_plus_plus_14:
316a72b31c7SJim Ingham           m_exception_language = eLanguageTypeC_plus_plus;
317fab10e89SJim Ingham           break;
318fab10e89SJim Ingham         case eLanguageTypeObjC:
319a72b31c7SJim Ingham           m_exception_language = eLanguageTypeObjC;
320fab10e89SJim Ingham           break;
321fab10e89SJim Ingham         case eLanguageTypeObjC_plus_plus:
322b9c1b51eSKate Stone           error.SetErrorStringWithFormat(
323b9c1b51eSKate Stone               "Set exception breakpoints separately for c++ and objective-c");
324fab10e89SJim Ingham           break;
325fab10e89SJim Ingham         case eLanguageTypeUnknown:
326b9c1b51eSKate Stone           error.SetErrorStringWithFormat(
327b9c1b51eSKate Stone               "Unknown language type: '%s' for exception breakpoint",
328fe11483bSZachary Turner               option_arg.str().c_str());
329fab10e89SJim Ingham           break;
330fab10e89SJim Ingham         default:
331b9c1b51eSKate Stone           error.SetErrorStringWithFormat(
332b9c1b51eSKate Stone               "Unsupported language type: '%s' for exception breakpoint",
333fe11483bSZachary Turner               option_arg.str().c_str());
334fab10e89SJim Ingham         }
335b9c1b51eSKate Stone       } break;
336ca36cd16SJim Ingham 
337ca36cd16SJim Ingham       case 'f':
3388f3be7a3SJonas Devlieghere         m_filenames.AppendIfUnique(FileSpec(option_arg));
339fab10e89SJim Ingham         break;
340ca36cd16SJim Ingham 
341ca36cd16SJim Ingham       case 'F':
342ca36cd16SJim Ingham         m_func_names.push_back(option_arg);
343ca36cd16SJim Ingham         m_func_name_type_mask |= eFunctionNameTypeFull;
344ca36cd16SJim Ingham         break;
345ca36cd16SJim Ingham 
346b9c1b51eSKate Stone       case 'h': {
347fab10e89SJim Ingham         bool success;
34847cbf4a0SPavel Labath         m_catch_bp = OptionArgParser::ToBoolean(option_arg, true, &success);
349fab10e89SJim Ingham         if (!success)
350b9c1b51eSKate Stone           error.SetErrorStringWithFormat(
351fe11483bSZachary Turner               "Invalid boolean value for on-catch option: '%s'",
352fe11483bSZachary Turner               option_arg.str().c_str());
353b9c1b51eSKate Stone       } break;
354eb023e75SGreg Clayton 
355eb023e75SGreg Clayton       case 'H':
356eb023e75SGreg Clayton         m_hardware = true;
357eb023e75SGreg Clayton         break;
358eb023e75SGreg Clayton 
3593815e702SJim Ingham       case 'k': {
3603815e702SJim Ingham           if (m_current_key.empty())
3613815e702SJim Ingham             m_current_key.assign(option_arg);
3623815e702SJim Ingham           else
3633815e702SJim Ingham             error.SetErrorStringWithFormat("Key: %s missing value.",
3643815e702SJim Ingham                                             m_current_key.c_str());
3653815e702SJim Ingham 
3663815e702SJim Ingham       } break;
367b9c1b51eSKate Stone       case 'K': {
368a8558b62SJim Ingham         bool success;
369a8558b62SJim Ingham         bool value;
37047cbf4a0SPavel Labath         value = OptionArgParser::ToBoolean(option_arg, true, &success);
371a8558b62SJim Ingham         if (value)
372a8558b62SJim Ingham           m_skip_prologue = eLazyBoolYes;
373a8558b62SJim Ingham         else
374a8558b62SJim Ingham           m_skip_prologue = eLazyBoolNo;
375a8558b62SJim Ingham 
376a8558b62SJim Ingham         if (!success)
377b9c1b51eSKate Stone           error.SetErrorStringWithFormat(
378b9c1b51eSKate Stone               "Invalid boolean value for skip prologue option: '%s'",
379fe11483bSZachary Turner               option_arg.str().c_str());
380b9c1b51eSKate Stone       } break;
381ca36cd16SJim Ingham 
382fe11483bSZachary Turner       case 'l':
383fe11483bSZachary Turner         if (option_arg.getAsInteger(0, m_line_num))
384b9c1b51eSKate Stone           error.SetErrorStringWithFormat("invalid line number: %s.",
385fe11483bSZachary Turner                                          option_arg.str().c_str());
386ca36cd16SJim Ingham         break;
387055ad9beSIlia K 
38823b1decbSDawn Perchik       case 'L':
389fe11483bSZachary Turner         m_language = Language::GetLanguageTypeFromString(option_arg);
39023b1decbSDawn Perchik         if (m_language == eLanguageTypeUnknown)
391b9c1b51eSKate Stone           error.SetErrorStringWithFormat(
392fe11483bSZachary Turner               "Unknown language type: '%s' for breakpoint",
393fe11483bSZachary Turner               option_arg.str().c_str());
39423b1decbSDawn Perchik         break;
39523b1decbSDawn Perchik 
396b9c1b51eSKate Stone       case 'm': {
397055ad9beSIlia K         bool success;
398055ad9beSIlia K         bool value;
39947cbf4a0SPavel Labath         value = OptionArgParser::ToBoolean(option_arg, true, &success);
400055ad9beSIlia K         if (value)
401055ad9beSIlia K           m_move_to_nearest_code = eLazyBoolYes;
402055ad9beSIlia K         else
403055ad9beSIlia K           m_move_to_nearest_code = eLazyBoolNo;
404055ad9beSIlia K 
405055ad9beSIlia K         if (!success)
406b9c1b51eSKate Stone           error.SetErrorStringWithFormat(
407b9c1b51eSKate Stone               "Invalid boolean value for move-to-nearest-code option: '%s'",
408fe11483bSZachary Turner               option_arg.str().c_str());
409055ad9beSIlia K         break;
410055ad9beSIlia K       }
411055ad9beSIlia K 
412ca36cd16SJim Ingham       case 'M':
413ca36cd16SJim Ingham         m_func_names.push_back(option_arg);
414ca36cd16SJim Ingham         m_func_name_type_mask |= eFunctionNameTypeMethod;
415ca36cd16SJim Ingham         break;
416ca36cd16SJim Ingham 
417ca36cd16SJim Ingham       case 'n':
418ca36cd16SJim Ingham         m_func_names.push_back(option_arg);
419ca36cd16SJim Ingham         m_func_name_type_mask |= eFunctionNameTypeAuto;
420ca36cd16SJim Ingham         break;
421ca36cd16SJim Ingham 
4226fa7681bSZachary Turner       case 'N': {
423fe11483bSZachary Turner         if (BreakpointID::StringIsBreakpointName(option_arg, error))
4245e09c8c3SJim Ingham           m_breakpoint_names.push_back(option_arg);
425ff9a91eaSJim Ingham         else
426ff9a91eaSJim Ingham           error.SetErrorStringWithFormat("Invalid breakpoint name: %s",
427fe11483bSZachary Turner                                          option_arg.str().c_str());
4285e09c8c3SJim Ingham         break;
4296fa7681bSZachary Turner       }
4305e09c8c3SJim Ingham 
431b9c1b51eSKate Stone       case 'R': {
4322411167fSJim Ingham         lldb::addr_t tmp_offset_addr;
43347cbf4a0SPavel Labath         tmp_offset_addr = OptionArgParser::ToAddress(execution_context,
43447cbf4a0SPavel Labath                                                      option_arg, 0, &error);
4352411167fSJim Ingham         if (error.Success())
4362411167fSJim Ingham           m_offset_addr = tmp_offset_addr;
437b9c1b51eSKate Stone       } break;
4382411167fSJim Ingham 
439a72b31c7SJim Ingham       case 'O':
440fe11483bSZachary Turner         m_exception_extra_args.AppendArgument("-O");
441fe11483bSZachary Turner         m_exception_extra_args.AppendArgument(option_arg);
442a72b31c7SJim Ingham         break;
443a72b31c7SJim Ingham 
444ca36cd16SJim Ingham       case 'p':
445ca36cd16SJim Ingham         m_source_text_regexp.assign(option_arg);
446ca36cd16SJim Ingham         break;
447ca36cd16SJim Ingham 
4483815e702SJim Ingham       case 'P':
4493815e702SJim Ingham         m_python_class.assign(option_arg);
4503815e702SJim Ingham         break;
4513815e702SJim Ingham 
452ca36cd16SJim Ingham       case 'r':
453ca36cd16SJim Ingham         m_func_regexp.assign(option_arg);
454ca36cd16SJim Ingham         break;
455ca36cd16SJim Ingham 
456ca36cd16SJim Ingham       case 's':
4578f3be7a3SJonas Devlieghere         m_modules.AppendIfUnique(FileSpec(option_arg));
458ca36cd16SJim Ingham         break;
459ca36cd16SJim Ingham 
460ca36cd16SJim Ingham       case 'S':
461ca36cd16SJim Ingham         m_func_names.push_back(option_arg);
462ca36cd16SJim Ingham         m_func_name_type_mask |= eFunctionNameTypeSelector;
463ca36cd16SJim Ingham         break;
464ca36cd16SJim Ingham 
4653815e702SJim Ingham       case 'v': {
4663815e702SJim Ingham           if (!m_current_key.empty()) {
4673815e702SJim Ingham               m_extra_args_sp->AddStringItem(m_current_key, option_arg);
4683815e702SJim Ingham               m_current_key.clear();
4693815e702SJim Ingham           }
4703815e702SJim Ingham           else
4713815e702SJim Ingham             error.SetErrorStringWithFormat("Value \"%s\" missing matching key.",
4723815e702SJim Ingham                                            option_arg.str().c_str());
4733815e702SJim Ingham       } break;
4743815e702SJim Ingham 
475b9c1b51eSKate Stone       case 'w': {
476ca36cd16SJim Ingham         bool success;
47747cbf4a0SPavel Labath         m_throw_bp = OptionArgParser::ToBoolean(option_arg, true, &success);
478ca36cd16SJim Ingham         if (!success)
479b9c1b51eSKate Stone           error.SetErrorStringWithFormat(
480fe11483bSZachary Turner               "Invalid boolean value for on-throw option: '%s'",
481fe11483bSZachary Turner               option_arg.str().c_str());
482b9c1b51eSKate Stone       } break;
483ca36cd16SJim Ingham 
48476bb8d67SJim Ingham       case 'X':
48576bb8d67SJim Ingham         m_source_regex_func_names.insert(option_arg);
48676bb8d67SJim Ingham         break;
48776bb8d67SJim Ingham 
48830fdc8d8SChris Lattner       default:
489b9c1b51eSKate Stone         error.SetErrorStringWithFormat("unrecognized option '%c'",
490b9c1b51eSKate Stone                                        short_option);
49130fdc8d8SChris Lattner         break;
49230fdc8d8SChris Lattner       }
49330fdc8d8SChris Lattner 
49430fdc8d8SChris Lattner       return error;
49530fdc8d8SChris Lattner     }
4969e85e5a8SEugene Zelenko 
497b9c1b51eSKate Stone     void OptionParsingStarting(ExecutionContext *execution_context) override {
49887df91b8SJim Ingham       m_filenames.Clear();
49930fdc8d8SChris Lattner       m_line_num = 0;
50030fdc8d8SChris Lattner       m_column = 0;
501fab10e89SJim Ingham       m_func_names.clear();
5021f746071SGreg Clayton       m_func_name_type_mask = eFunctionNameTypeNone;
50330fdc8d8SChris Lattner       m_func_regexp.clear();
5041f746071SGreg Clayton       m_source_text_regexp.clear();
50587df91b8SJim Ingham       m_modules.Clear();
5061f746071SGreg Clayton       m_load_addr = LLDB_INVALID_ADDRESS;
5072411167fSJim Ingham       m_offset_addr = 0;
508fab10e89SJim Ingham       m_catch_bp = false;
509fab10e89SJim Ingham       m_throw_bp = true;
510eb023e75SGreg Clayton       m_hardware = false;
511a72b31c7SJim Ingham       m_exception_language = eLanguageTypeUnknown;
51223b1decbSDawn Perchik       m_language = lldb::eLanguageTypeUnknown;
513a8558b62SJim Ingham       m_skip_prologue = eLazyBoolCalculate;
5145e09c8c3SJim Ingham       m_breakpoint_names.clear();
515e732052fSJim Ingham       m_all_files = false;
516a72b31c7SJim Ingham       m_exception_extra_args.Clear();
517055ad9beSIlia K       m_move_to_nearest_code = eLazyBoolCalculate;
51876bb8d67SJim Ingham       m_source_regex_func_names.clear();
5193815e702SJim Ingham       m_python_class.clear();
520796ac80bSJonas Devlieghere       m_extra_args_sp = std::make_shared<StructuredData::Dictionary>();
5213815e702SJim Ingham       m_current_key.clear();
52230fdc8d8SChris Lattner     }
52330fdc8d8SChris Lattner 
5241f0f5b5bSZachary Turner     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
52570602439SZachary Turner       return llvm::makeArrayRef(g_breakpoint_set_options);
5261f0f5b5bSZachary Turner     }
52730fdc8d8SChris Lattner 
5285a988416SJim Ingham     // Instance variables to hold the values for command options.
529969795f1SJim Ingham 
5305a988416SJim Ingham     std::string m_condition;
5315a988416SJim Ingham     FileSpecList m_filenames;
5325a988416SJim Ingham     uint32_t m_line_num;
5335a988416SJim Ingham     uint32_t m_column;
5345a988416SJim Ingham     std::vector<std::string> m_func_names;
5355e09c8c3SJim Ingham     std::vector<std::string> m_breakpoint_names;
536117b1fa1SZachary Turner     lldb::FunctionNameType m_func_name_type_mask;
5375a988416SJim Ingham     std::string m_func_regexp;
5385a988416SJim Ingham     std::string m_source_text_regexp;
5395a988416SJim Ingham     FileSpecList m_modules;
5405a988416SJim Ingham     lldb::addr_t m_load_addr;
5412411167fSJim Ingham     lldb::addr_t m_offset_addr;
5425a988416SJim Ingham     bool m_catch_bp;
5435a988416SJim Ingham     bool m_throw_bp;
544eb023e75SGreg Clayton     bool m_hardware; // Request to use hardware breakpoints
545a72b31c7SJim Ingham     lldb::LanguageType m_exception_language;
54623b1decbSDawn Perchik     lldb::LanguageType m_language;
5475a988416SJim Ingham     LazyBool m_skip_prologue;
548e732052fSJim Ingham     bool m_all_files;
549a72b31c7SJim Ingham     Args m_exception_extra_args;
550055ad9beSIlia K     LazyBool m_move_to_nearest_code;
55176bb8d67SJim Ingham     std::unordered_set<std::string> m_source_regex_func_names;
5523815e702SJim Ingham     std::string m_python_class;
5533815e702SJim Ingham     StructuredData::DictionarySP m_extra_args_sp;
5543815e702SJim Ingham     std::string m_current_key;
5555a988416SJim Ingham   };
5565a988416SJim Ingham 
5575a988416SJim Ingham protected:
558b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
559b842f2ecSJim Ingham     Target *target = GetSelectedOrDummyTarget(m_dummy_options.m_use_dummy);
56033df7cd3SJim Ingham 
561b9c1b51eSKate Stone     if (target == nullptr) {
562b9c1b51eSKate Stone       result.AppendError("Invalid target.  Must set target before setting "
563b9c1b51eSKate Stone                          "breakpoints (see 'target create' command).");
56430fdc8d8SChris Lattner       result.SetStatus(eReturnStatusFailed);
56530fdc8d8SChris Lattner       return false;
56630fdc8d8SChris Lattner     }
56730fdc8d8SChris Lattner 
56830fdc8d8SChris Lattner     // The following are the various types of breakpoints that could be set:
56930fdc8d8SChris Lattner     //   1).  -f -l -p  [-s -g]   (setting breakpoint by source location)
57030fdc8d8SChris Lattner     //   2).  -a  [-s -g]         (setting breakpoint by address)
57130fdc8d8SChris Lattner     //   3).  -n  [-s -g]         (setting breakpoint by function name)
572b9c1b51eSKate Stone     //   4).  -r  [-s -g]         (setting breakpoint by function name regular
573b9c1b51eSKate Stone     //   expression)
574b9c1b51eSKate Stone     //   5).  -p -f               (setting a breakpoint by comparing a reg-exp
575b9c1b51eSKate Stone     //   to source text)
576b9c1b51eSKate Stone     //   6).  -E [-w -h]          (setting a breakpoint for exceptions for a
577b9c1b51eSKate Stone     //   given language.)
57830fdc8d8SChris Lattner 
57930fdc8d8SChris Lattner     BreakpointSetType break_type = eSetTypeInvalid;
58030fdc8d8SChris Lattner 
5813815e702SJim Ingham     if (!m_options.m_python_class.empty())
5823815e702SJim Ingham       break_type = eSetTypeScripted;
5833815e702SJim Ingham     else if (m_options.m_line_num != 0)
58430fdc8d8SChris Lattner       break_type = eSetTypeFileAndLine;
58530fdc8d8SChris Lattner     else if (m_options.m_load_addr != LLDB_INVALID_ADDRESS)
58630fdc8d8SChris Lattner       break_type = eSetTypeAddress;
587fab10e89SJim Ingham     else if (!m_options.m_func_names.empty())
58830fdc8d8SChris Lattner       break_type = eSetTypeFunctionName;
58930fdc8d8SChris Lattner     else if (!m_options.m_func_regexp.empty())
59030fdc8d8SChris Lattner       break_type = eSetTypeFunctionRegexp;
591969795f1SJim Ingham     else if (!m_options.m_source_text_regexp.empty())
592969795f1SJim Ingham       break_type = eSetTypeSourceRegexp;
593a72b31c7SJim Ingham     else if (m_options.m_exception_language != eLanguageTypeUnknown)
594fab10e89SJim Ingham       break_type = eSetTypeException;
59530fdc8d8SChris Lattner 
596b842f2ecSJim Ingham     BreakpointSP bp_sp = nullptr;
597274060b6SGreg Clayton     FileSpec module_spec;
598a8558b62SJim Ingham     const bool internal = false;
599a8558b62SJim Ingham 
600b9c1b51eSKate Stone     // If the user didn't specify skip-prologue, having an offset should turn
601b9c1b51eSKate Stone     // that off.
602b9c1b51eSKate Stone     if (m_options.m_offset_addr != 0 &&
603b9c1b51eSKate Stone         m_options.m_skip_prologue == eLazyBoolCalculate)
6042411167fSJim Ingham       m_options.m_skip_prologue = eLazyBoolNo;
6052411167fSJim Ingham 
606b9c1b51eSKate Stone     switch (break_type) {
60730fdc8d8SChris Lattner     case eSetTypeFileAndLine: // Breakpoint by source position
60830fdc8d8SChris Lattner     {
60930fdc8d8SChris Lattner       FileSpec file;
610c7bece56SGreg Clayton       const size_t num_files = m_options.m_filenames.GetSize();
611b9c1b51eSKate Stone       if (num_files == 0) {
612b9c1b51eSKate Stone         if (!GetDefaultFile(target, file, result)) {
61387df91b8SJim Ingham           result.AppendError("No file supplied and no default file available.");
61487df91b8SJim Ingham           result.SetStatus(eReturnStatusFailed);
61587df91b8SJim Ingham           return false;
61687df91b8SJim Ingham         }
617b9c1b51eSKate Stone       } else if (num_files > 1) {
618b9c1b51eSKate Stone         result.AppendError("Only one file at a time is allowed for file and "
619b9c1b51eSKate Stone                            "line breakpoints.");
62087df91b8SJim Ingham         result.SetStatus(eReturnStatusFailed);
62187df91b8SJim Ingham         return false;
622b9c1b51eSKate Stone       } else
62387df91b8SJim Ingham         file = m_options.m_filenames.GetFileSpecAtIndex(0);
62430fdc8d8SChris Lattner 
6251f746071SGreg Clayton       // Only check for inline functions if
6261f746071SGreg Clayton       LazyBool check_inlines = eLazyBoolCalculate;
6271f746071SGreg Clayton 
628b842f2ecSJim Ingham       bp_sp = target->CreateBreakpoint(&(m_options.m_modules),
629b842f2ecSJim Ingham                                        file,
630b842f2ecSJim Ingham                                        m_options.m_line_num,
631431b1584SAdrian Prantl                                        m_options.m_column,
632b842f2ecSJim Ingham                                        m_options.m_offset_addr,
633b842f2ecSJim Ingham                                        check_inlines,
634b842f2ecSJim Ingham                                        m_options.m_skip_prologue,
635b842f2ecSJim Ingham                                        internal,
636b842f2ecSJim Ingham                                        m_options.m_hardware,
637b842f2ecSJim Ingham                                        m_options.m_move_to_nearest_code);
638b9c1b51eSKate Stone     } break;
6396eee5aa0SGreg Clayton 
64030fdc8d8SChris Lattner     case eSetTypeAddress: // Breakpoint by address
641055a08a4SJim Ingham     {
642b9c1b51eSKate Stone       // If a shared library has been specified, make an lldb_private::Address
643b842f2ecSJim Ingham       // with the library, and use that.  That way the address breakpoint
644b842f2ecSJim Ingham       //  will track the load location of the library.
645055a08a4SJim Ingham       size_t num_modules_specified = m_options.m_modules.GetSize();
646b9c1b51eSKate Stone       if (num_modules_specified == 1) {
647b9c1b51eSKate Stone         const FileSpec *file_spec =
648b9c1b51eSKate Stone             m_options.m_modules.GetFileSpecPointerAtIndex(0);
649b842f2ecSJim Ingham         bp_sp = target->CreateAddressInModuleBreakpoint(m_options.m_load_addr,
650b9c1b51eSKate Stone                                                         internal, file_spec,
651b842f2ecSJim Ingham                                                         m_options.m_hardware);
652b9c1b51eSKate Stone       } else if (num_modules_specified == 0) {
653b842f2ecSJim Ingham         bp_sp = target->CreateBreakpoint(m_options.m_load_addr, internal,
654b842f2ecSJim Ingham                                          m_options.m_hardware);
655b9c1b51eSKate Stone       } else {
656b9c1b51eSKate Stone         result.AppendError("Only one shared library can be specified for "
657b9c1b51eSKate Stone                            "address breakpoints.");
658055a08a4SJim Ingham         result.SetStatus(eReturnStatusFailed);
659055a08a4SJim Ingham         return false;
660055a08a4SJim Ingham       }
66130fdc8d8SChris Lattner       break;
662055a08a4SJim Ingham     }
66330fdc8d8SChris Lattner     case eSetTypeFunctionName: // Breakpoint by function name
6640c5cd90dSGreg Clayton     {
665117b1fa1SZachary Turner       FunctionNameType name_type_mask = m_options.m_func_name_type_mask;
6660c5cd90dSGreg Clayton 
6670c5cd90dSGreg Clayton       if (name_type_mask == 0)
668e02b8504SGreg Clayton         name_type_mask = eFunctionNameTypeAuto;
6690c5cd90dSGreg Clayton 
670b842f2ecSJim Ingham       bp_sp = target->CreateBreakpoint(&(m_options.m_modules),
671b842f2ecSJim Ingham                                        &(m_options.m_filenames),
672b842f2ecSJim Ingham                                        m_options.m_func_names,
673b842f2ecSJim Ingham                                        name_type_mask,
674b842f2ecSJim Ingham                                        m_options.m_language,
675b842f2ecSJim Ingham                                        m_options.m_offset_addr,
676b842f2ecSJim Ingham                                        m_options.m_skip_prologue,
677b842f2ecSJim Ingham                                        internal,
678b842f2ecSJim Ingham                                        m_options.m_hardware);
679b9c1b51eSKate Stone     } break;
6800c5cd90dSGreg Clayton 
681b9c1b51eSKate Stone     case eSetTypeFunctionRegexp: // Breakpoint by regular expression function
682b9c1b51eSKate Stone                                  // name
68330fdc8d8SChris Lattner       {
68495eae423SZachary Turner         RegularExpression regexp(m_options.m_func_regexp);
685*3af3f1e8SJonas Devlieghere         if (llvm::Error err = regexp.GetError()) {
686b9c1b51eSKate Stone           result.AppendErrorWithFormat(
687b9c1b51eSKate Stone               "Function name regular expression could not be compiled: \"%s\"",
688*3af3f1e8SJonas Devlieghere               llvm::toString(std::move(err)).c_str());
68930fdc8d8SChris Lattner           result.SetStatus(eReturnStatusFailed);
690969795f1SJim Ingham           return false;
69130fdc8d8SChris Lattner         }
69287df91b8SJim Ingham 
693b842f2ecSJim Ingham         bp_sp = target->CreateFuncRegexBreakpoint(&(m_options.m_modules),
694b842f2ecSJim Ingham                                                   &(m_options.m_filenames),
695b842f2ecSJim Ingham                                                   regexp,
696b842f2ecSJim Ingham                                                   m_options.m_language,
697b842f2ecSJim Ingham                                                   m_options.m_skip_prologue,
698b842f2ecSJim Ingham                                                   internal,
699b842f2ecSJim Ingham                                                   m_options.m_hardware);
700e14dc268SJim Ingham       }
701e14dc268SJim Ingham       break;
702969795f1SJim Ingham     case eSetTypeSourceRegexp: // Breakpoint by regexp on source text.
703969795f1SJim Ingham     {
704c7bece56SGreg Clayton       const size_t num_files = m_options.m_filenames.GetSize();
70587df91b8SJim Ingham 
706b9c1b51eSKate Stone       if (num_files == 0 && !m_options.m_all_files) {
707969795f1SJim Ingham         FileSpec file;
708b9c1b51eSKate Stone         if (!GetDefaultFile(target, file, result)) {
709b9c1b51eSKate Stone           result.AppendError(
710b9c1b51eSKate Stone               "No files provided and could not find default file.");
71187df91b8SJim Ingham           result.SetStatus(eReturnStatusFailed);
71287df91b8SJim Ingham           return false;
713b9c1b51eSKate Stone         } else {
71487df91b8SJim Ingham           m_options.m_filenames.Append(file);
71587df91b8SJim Ingham         }
71687df91b8SJim Ingham       }
7170c5cd90dSGreg Clayton 
71895eae423SZachary Turner       RegularExpression regexp(m_options.m_source_text_regexp);
719*3af3f1e8SJonas Devlieghere       if (llvm::Error err = regexp.GetError()) {
720b9c1b51eSKate Stone         result.AppendErrorWithFormat(
721b9c1b51eSKate Stone             "Source text regular expression could not be compiled: \"%s\"",
722*3af3f1e8SJonas Devlieghere             llvm::toString(std::move(err)).c_str());
723969795f1SJim Ingham         result.SetStatus(eReturnStatusFailed);
724969795f1SJim Ingham         return false;
725969795f1SJim Ingham       }
726b842f2ecSJim Ingham       bp_sp =
727b842f2ecSJim Ingham           target->CreateSourceRegexBreakpoint(&(m_options.m_modules),
728b842f2ecSJim Ingham                                               &(m_options.m_filenames),
729b842f2ecSJim Ingham                                               m_options
730b842f2ecSJim Ingham                                                   .m_source_regex_func_names,
731b842f2ecSJim Ingham                                               regexp,
732b842f2ecSJim Ingham                                               internal,
733b842f2ecSJim Ingham                                               m_options.m_hardware,
734b842f2ecSJim Ingham                                               m_options.m_move_to_nearest_code);
735b9c1b51eSKate Stone     } break;
736b9c1b51eSKate Stone     case eSetTypeException: {
73797206d57SZachary Turner       Status precond_error;
738b842f2ecSJim Ingham       bp_sp = target->CreateExceptionBreakpoint(m_options.m_exception_language,
739b842f2ecSJim Ingham                                                 m_options.m_catch_bp,
740b842f2ecSJim Ingham                                                 m_options.m_throw_bp,
741b842f2ecSJim Ingham                                                 internal,
742b842f2ecSJim Ingham                                                 &m_options
743b842f2ecSJim Ingham                                                     .m_exception_extra_args,
744b842f2ecSJim Ingham                                                 &precond_error);
745b9c1b51eSKate Stone       if (precond_error.Fail()) {
746b9c1b51eSKate Stone         result.AppendErrorWithFormat(
747b9c1b51eSKate Stone             "Error setting extra exception arguments: %s",
748a72b31c7SJim Ingham             precond_error.AsCString());
749b842f2ecSJim Ingham         target->RemoveBreakpointByID(bp_sp->GetID());
750a72b31c7SJim Ingham         result.SetStatus(eReturnStatusFailed);
751a72b31c7SJim Ingham         return false;
752a72b31c7SJim Ingham       }
753b9c1b51eSKate Stone     } break;
7543815e702SJim Ingham     case eSetTypeScripted: {
7553815e702SJim Ingham 
7563815e702SJim Ingham       Status error;
7573815e702SJim Ingham       bp_sp = target->CreateScriptedBreakpoint(m_options.m_python_class,
7583815e702SJim Ingham                                                &(m_options.m_modules),
7593815e702SJim Ingham                                                &(m_options.m_filenames),
7603815e702SJim Ingham                                                false,
7613815e702SJim Ingham                                                m_options.m_hardware,
7623815e702SJim Ingham                                                m_options.m_extra_args_sp,
7633815e702SJim Ingham                                                &error);
7643815e702SJim Ingham       if (error.Fail()) {
7653815e702SJim Ingham         result.AppendErrorWithFormat(
7663815e702SJim Ingham             "Error setting extra exception arguments: %s",
7673815e702SJim Ingham             error.AsCString());
7683815e702SJim Ingham         target->RemoveBreakpointByID(bp_sp->GetID());
7693815e702SJim Ingham         result.SetStatus(eReturnStatusFailed);
7703815e702SJim Ingham         return false;
7713815e702SJim Ingham       }
7723815e702SJim Ingham     } break;
77330fdc8d8SChris Lattner     default:
77430fdc8d8SChris Lattner       break;
77530fdc8d8SChris Lattner     }
77630fdc8d8SChris Lattner 
7771b54c88cSJim Ingham     // Now set the various options that were passed in:
778b842f2ecSJim Ingham     if (bp_sp) {
779b842f2ecSJim Ingham       bp_sp->GetOptions()->CopyOverSetOptions(m_bp_opts.GetBreakpointOptions());
780ca36cd16SJim Ingham 
781b9c1b51eSKate Stone       if (!m_options.m_breakpoint_names.empty()) {
78297206d57SZachary Turner         Status name_error;
783ff9a91eaSJim Ingham         for (auto name : m_options.m_breakpoint_names) {
784b842f2ecSJim Ingham           target->AddNameToBreakpoint(bp_sp, name.c_str(), name_error);
785ff9a91eaSJim Ingham           if (name_error.Fail()) {
786ff9a91eaSJim Ingham             result.AppendErrorWithFormat("Invalid breakpoint name: %s",
787ff9a91eaSJim Ingham                                          name.c_str());
788b842f2ecSJim Ingham             target->RemoveBreakpointByID(bp_sp->GetID());
789ff9a91eaSJim Ingham             result.SetStatus(eReturnStatusFailed);
790ff9a91eaSJim Ingham             return false;
791ff9a91eaSJim Ingham           }
792ff9a91eaSJim Ingham         }
7935e09c8c3SJim Ingham       }
7941b54c88cSJim Ingham     }
7951b54c88cSJim Ingham 
796b842f2ecSJim Ingham     if (bp_sp) {
79785e8b814SJim Ingham       Stream &output_stream = result.GetOutputStream();
7981391cc7dSJim Ingham       const bool show_locations = false;
799b842f2ecSJim Ingham       bp_sp->GetDescription(&output_stream, lldb::eDescriptionLevelInitial,
800b9c1b51eSKate Stone                          show_locations);
80157179860SJonas Devlieghere       if (target == GetDebugger().GetDummyTarget())
802b9c1b51eSKate Stone         output_stream.Printf("Breakpoint set in dummy target, will get copied "
803b9c1b51eSKate Stone                              "into future targets.\n");
804b9c1b51eSKate Stone       else {
80505097246SAdrian Prantl         // Don't print out this warning for exception breakpoints.  They can
80605097246SAdrian Prantl         // get set before the target is set, but we won't know how to actually
80705097246SAdrian Prantl         // set the breakpoint till we run.
808b842f2ecSJim Ingham         if (bp_sp->GetNumLocations() == 0 && break_type != eSetTypeException) {
809b9c1b51eSKate Stone           output_stream.Printf("WARNING:  Unable to resolve breakpoint to any "
810b9c1b51eSKate Stone                                "actual locations.\n");
8114aeb1989SJim Ingham         }
8124aeb1989SJim Ingham       }
81330fdc8d8SChris Lattner       result.SetStatus(eReturnStatusSuccessFinishResult);
814b842f2ecSJim Ingham     } else if (!bp_sp) {
81530fdc8d8SChris Lattner       result.AppendError("Breakpoint creation failed: No breakpoint created.");
81630fdc8d8SChris Lattner       result.SetStatus(eReturnStatusFailed);
81730fdc8d8SChris Lattner     }
81830fdc8d8SChris Lattner 
81930fdc8d8SChris Lattner     return result.Succeeded();
82030fdc8d8SChris Lattner   }
82130fdc8d8SChris Lattner 
8225a988416SJim Ingham private:
823b9c1b51eSKate Stone   bool GetDefaultFile(Target *target, FileSpec &file,
824b9c1b51eSKate Stone                       CommandReturnObject &result) {
8255a988416SJim Ingham     uint32_t default_line;
82605097246SAdrian Prantl     // First use the Source Manager's default file. Then use the current stack
82705097246SAdrian Prantl     // frame's file.
828b9c1b51eSKate Stone     if (!target->GetSourceManager().GetDefaultFileAndLine(file, default_line)) {
829b57e4a1bSJason Molenda       StackFrame *cur_frame = m_exe_ctx.GetFramePtr();
830b9c1b51eSKate Stone       if (cur_frame == nullptr) {
831b9c1b51eSKate Stone         result.AppendError(
832b9c1b51eSKate Stone             "No selected frame to use to find the default file.");
8335a988416SJim Ingham         result.SetStatus(eReturnStatusFailed);
8345a988416SJim Ingham         return false;
835b9c1b51eSKate Stone       } else if (!cur_frame->HasDebugInformation()) {
836b9c1b51eSKate Stone         result.AppendError("Cannot use the selected frame to find the default "
837b9c1b51eSKate Stone                            "file, it has no debug info.");
8385a988416SJim Ingham         result.SetStatus(eReturnStatusFailed);
8395a988416SJim Ingham         return false;
840b9c1b51eSKate Stone       } else {
841b9c1b51eSKate Stone         const SymbolContext &sc =
842b9c1b51eSKate Stone             cur_frame->GetSymbolContext(eSymbolContextLineEntry);
843b9c1b51eSKate Stone         if (sc.line_entry.file) {
8445a988416SJim Ingham           file = sc.line_entry.file;
845b9c1b51eSKate Stone         } else {
846b9c1b51eSKate Stone           result.AppendError("Can't find the file for the selected frame to "
847b9c1b51eSKate Stone                              "use as the default file.");
8485a988416SJim Ingham           result.SetStatus(eReturnStatusFailed);
8495a988416SJim Ingham           return false;
8505a988416SJim Ingham         }
8515a988416SJim Ingham       }
8525a988416SJim Ingham     }
8535a988416SJim Ingham     return true;
8545a988416SJim Ingham   }
8555a988416SJim Ingham 
856b842f2ecSJim Ingham   BreakpointOptionGroup m_bp_opts;
857b842f2ecSJim Ingham   BreakpointDummyOptionGroup m_dummy_options;
8585a988416SJim Ingham   CommandOptions m_options;
859b842f2ecSJim Ingham   OptionGroupOptions m_all_options;
8605a988416SJim Ingham };
8619e85e5a8SEugene Zelenko 
8625a988416SJim Ingham // CommandObjectBreakpointModify
8635a988416SJim Ingham #pragma mark Modify
8645a988416SJim Ingham 
865b9c1b51eSKate Stone class CommandObjectBreakpointModify : public CommandObjectParsed {
8665a988416SJim Ingham public:
867b9c1b51eSKate Stone   CommandObjectBreakpointModify(CommandInterpreter &interpreter)
868b9c1b51eSKate Stone       : CommandObjectParsed(interpreter, "breakpoint modify",
869b9c1b51eSKate Stone                             "Modify the options on a breakpoint or set of "
870b9c1b51eSKate Stone                             "breakpoints in the executable.  "
871b9c1b51eSKate Stone                             "If no breakpoint is specified, acts on the last "
872b9c1b51eSKate Stone                             "created breakpoint.  "
873b9c1b51eSKate Stone                             "With the exception of -e, -d and -i, passing an "
874b9c1b51eSKate Stone                             "empty argument clears the modification.",
8759e85e5a8SEugene Zelenko                             nullptr),
876b9c1b51eSKate Stone         m_options() {
8775a988416SJim Ingham     CommandArgumentEntry arg;
878b9c1b51eSKate Stone     CommandObject::AddIDsArgumentData(arg, eArgTypeBreakpointID,
879b9c1b51eSKate Stone                                       eArgTypeBreakpointIDRange);
880b9c1b51eSKate Stone     // Add the entry for the first argument for this command to the object's
881b9c1b51eSKate Stone     // arguments vector.
8825a988416SJim Ingham     m_arguments.push_back(arg);
883b842f2ecSJim Ingham 
884b842f2ecSJim Ingham     m_options.Append(&m_bp_opts,
885b842f2ecSJim Ingham                      LLDB_OPT_SET_1 | LLDB_OPT_SET_2 | LLDB_OPT_SET_3,
886b842f2ecSJim Ingham                      LLDB_OPT_SET_ALL);
887b842f2ecSJim Ingham     m_options.Append(&m_dummy_opts, LLDB_OPT_SET_1, LLDB_OPT_SET_ALL);
888b842f2ecSJim Ingham     m_options.Finalize();
8895a988416SJim Ingham   }
8905a988416SJim Ingham 
8919e85e5a8SEugene Zelenko   ~CommandObjectBreakpointModify() override = default;
8925a988416SJim Ingham 
893b9c1b51eSKate Stone   Options *GetOptions() override { return &m_options; }
8945a988416SJim Ingham 
8955a988416SJim Ingham protected:
896b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
897b842f2ecSJim Ingham     Target *target = GetSelectedOrDummyTarget(m_dummy_opts.m_use_dummy);
898b9c1b51eSKate Stone     if (target == nullptr) {
8995a988416SJim Ingham       result.AppendError("Invalid target.  No existing target or breakpoints.");
9005a988416SJim Ingham       result.SetStatus(eReturnStatusFailed);
9015a988416SJim Ingham       return false;
9025a988416SJim Ingham     }
9035a988416SJim Ingham 
904bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
905bb19a13cSSaleem Abdulrasool     target->GetBreakpointList().GetListMutex(lock);
9065a988416SJim Ingham 
9075a988416SJim Ingham     BreakpointIDList valid_bp_ids;
9085a988416SJim Ingham 
909b9c1b51eSKate Stone     CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs(
910b842f2ecSJim Ingham         command, target, result, &valid_bp_ids,
911b842f2ecSJim Ingham         BreakpointName::Permissions::PermissionKinds::disablePerm);
9125a988416SJim Ingham 
913b9c1b51eSKate Stone     if (result.Succeeded()) {
9145a988416SJim Ingham       const size_t count = valid_bp_ids.GetSize();
915b9c1b51eSKate Stone       for (size_t i = 0; i < count; ++i) {
9165a988416SJim Ingham         BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i);
9175a988416SJim Ingham 
918b9c1b51eSKate Stone         if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) {
919b9c1b51eSKate Stone           Breakpoint *bp =
920b9c1b51eSKate Stone               target->GetBreakpointByID(cur_bp_id.GetBreakpointID()).get();
921b9c1b51eSKate Stone           if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID) {
922b9c1b51eSKate Stone             BreakpointLocation *location =
923b9c1b51eSKate Stone                 bp->FindLocationByID(cur_bp_id.GetLocationID()).get();
924b842f2ecSJim Ingham             if (location)
925b842f2ecSJim Ingham               location->GetLocationOptions()
926b842f2ecSJim Ingham                   ->CopyOverSetOptions(m_bp_opts.GetBreakpointOptions());
927b9c1b51eSKate Stone           } else {
928b842f2ecSJim Ingham             bp->GetOptions()
929b842f2ecSJim Ingham                 ->CopyOverSetOptions(m_bp_opts.GetBreakpointOptions());
9305a988416SJim Ingham           }
9315a988416SJim Ingham         }
9325a988416SJim Ingham       }
9335a988416SJim Ingham     }
9345a988416SJim Ingham 
9355a988416SJim Ingham     return result.Succeeded();
9365a988416SJim Ingham   }
9375a988416SJim Ingham 
9385a988416SJim Ingham private:
939b842f2ecSJim Ingham   BreakpointOptionGroup m_bp_opts;
940b842f2ecSJim Ingham   BreakpointDummyOptionGroup m_dummy_opts;
941b842f2ecSJim Ingham   OptionGroupOptions m_options;
9425a988416SJim Ingham };
9435a988416SJim Ingham 
9445a988416SJim Ingham // CommandObjectBreakpointEnable
9455a988416SJim Ingham #pragma mark Enable
9465a988416SJim Ingham 
947b9c1b51eSKate Stone class CommandObjectBreakpointEnable : public CommandObjectParsed {
9485a988416SJim Ingham public:
949b9c1b51eSKate Stone   CommandObjectBreakpointEnable(CommandInterpreter &interpreter)
950b9c1b51eSKate Stone       : CommandObjectParsed(interpreter, "enable",
951b9c1b51eSKate Stone                             "Enable the specified disabled breakpoint(s). If "
952b9c1b51eSKate Stone                             "no breakpoints are specified, enable all of them.",
953b9c1b51eSKate Stone                             nullptr) {
9545a988416SJim Ingham     CommandArgumentEntry arg;
955b9c1b51eSKate Stone     CommandObject::AddIDsArgumentData(arg, eArgTypeBreakpointID,
956b9c1b51eSKate Stone                                       eArgTypeBreakpointIDRange);
957b9c1b51eSKate Stone     // Add the entry for the first argument for this command to the object's
958b9c1b51eSKate Stone     // arguments vector.
9595a988416SJim Ingham     m_arguments.push_back(arg);
9605a988416SJim Ingham   }
9615a988416SJim Ingham 
9629e85e5a8SEugene Zelenko   ~CommandObjectBreakpointEnable() override = default;
9635a988416SJim Ingham 
9645a988416SJim Ingham protected:
965b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
966893c932aSJim Ingham     Target *target = GetSelectedOrDummyTarget();
967b9c1b51eSKate Stone     if (target == nullptr) {
9685a988416SJim Ingham       result.AppendError("Invalid target.  No existing target or breakpoints.");
9695a988416SJim Ingham       result.SetStatus(eReturnStatusFailed);
9705a988416SJim Ingham       return false;
9715a988416SJim Ingham     }
9725a988416SJim Ingham 
973bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
974bb19a13cSSaleem Abdulrasool     target->GetBreakpointList().GetListMutex(lock);
9755a988416SJim Ingham 
9765a988416SJim Ingham     const BreakpointList &breakpoints = target->GetBreakpointList();
9775a988416SJim Ingham 
9785a988416SJim Ingham     size_t num_breakpoints = breakpoints.GetSize();
9795a988416SJim Ingham 
980b9c1b51eSKate Stone     if (num_breakpoints == 0) {
9815a988416SJim Ingham       result.AppendError("No breakpoints exist to be enabled.");
9825a988416SJim Ingham       result.SetStatus(eReturnStatusFailed);
9835a988416SJim Ingham       return false;
9845a988416SJim Ingham     }
9855a988416SJim Ingham 
98611eb9c64SZachary Turner     if (command.empty()) {
9875a988416SJim Ingham       // No breakpoint selected; enable all currently set breakpoints.
988b842f2ecSJim Ingham       target->EnableAllowedBreakpoints();
989b9c1b51eSKate Stone       result.AppendMessageWithFormat("All breakpoints enabled. (%" PRIu64
990b9c1b51eSKate Stone                                      " breakpoints)\n",
991b9c1b51eSKate Stone                                      (uint64_t)num_breakpoints);
9925a988416SJim Ingham       result.SetStatus(eReturnStatusSuccessFinishNoResult);
993b9c1b51eSKate Stone     } else {
9945a988416SJim Ingham       // Particular breakpoint selected; enable that breakpoint.
9955a988416SJim Ingham       BreakpointIDList valid_bp_ids;
996b9c1b51eSKate Stone       CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs(
997b842f2ecSJim Ingham           command, target, result, &valid_bp_ids,
998b842f2ecSJim Ingham           BreakpointName::Permissions::PermissionKinds::disablePerm);
9995a988416SJim Ingham 
1000b9c1b51eSKate Stone       if (result.Succeeded()) {
10015a988416SJim Ingham         int enable_count = 0;
10025a988416SJim Ingham         int loc_count = 0;
10035a988416SJim Ingham         const size_t count = valid_bp_ids.GetSize();
1004b9c1b51eSKate Stone         for (size_t i = 0; i < count; ++i) {
10055a988416SJim Ingham           BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i);
10065a988416SJim Ingham 
1007b9c1b51eSKate Stone           if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) {
1008b9c1b51eSKate Stone             Breakpoint *breakpoint =
1009b9c1b51eSKate Stone                 target->GetBreakpointByID(cur_bp_id.GetBreakpointID()).get();
1010b9c1b51eSKate Stone             if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID) {
1011b9c1b51eSKate Stone               BreakpointLocation *location =
1012b9c1b51eSKate Stone                   breakpoint->FindLocationByID(cur_bp_id.GetLocationID()).get();
1013b9c1b51eSKate Stone               if (location) {
10145a988416SJim Ingham                 location->SetEnabled(true);
10155a988416SJim Ingham                 ++loc_count;
10165a988416SJim Ingham               }
1017b9c1b51eSKate Stone             } else {
10185a988416SJim Ingham               breakpoint->SetEnabled(true);
10195a988416SJim Ingham               ++enable_count;
10205a988416SJim Ingham             }
10215a988416SJim Ingham           }
10225a988416SJim Ingham         }
1023b9c1b51eSKate Stone         result.AppendMessageWithFormat("%d breakpoints enabled.\n",
1024b9c1b51eSKate Stone                                        enable_count + loc_count);
10255a988416SJim Ingham         result.SetStatus(eReturnStatusSuccessFinishNoResult);
10265a988416SJim Ingham       }
10275a988416SJim Ingham     }
10285a988416SJim Ingham 
10295a988416SJim Ingham     return result.Succeeded();
10305a988416SJim Ingham   }
10315a988416SJim Ingham };
10325a988416SJim Ingham 
10335a988416SJim Ingham // CommandObjectBreakpointDisable
10345a988416SJim Ingham #pragma mark Disable
10355a988416SJim Ingham 
1036b9c1b51eSKate Stone class CommandObjectBreakpointDisable : public CommandObjectParsed {
10375a988416SJim Ingham public:
10387428a18cSKate Stone   CommandObjectBreakpointDisable(CommandInterpreter &interpreter)
1039b9c1b51eSKate Stone       : CommandObjectParsed(
1040b9c1b51eSKate Stone             interpreter, "breakpoint disable",
1041b9c1b51eSKate Stone             "Disable the specified breakpoint(s) without deleting "
10427428a18cSKate Stone             "them.  If none are specified, disable all "
10437428a18cSKate Stone             "breakpoints.",
1044b9c1b51eSKate Stone             nullptr) {
1045b9c1b51eSKate Stone     SetHelpLong(
1046b9c1b51eSKate Stone         "Disable the specified breakpoint(s) without deleting them.  \
10477428a18cSKate Stone If none are specified, disable all breakpoints."
10487428a18cSKate Stone         R"(
1049ea671fbdSKate Stone 
10507428a18cSKate Stone )"
10517428a18cSKate Stone         "Note: disabling a breakpoint will cause none of its locations to be hit \
10527428a18cSKate Stone regardless of whether individual locations are enabled or disabled.  After the sequence:"
10537428a18cSKate Stone         R"(
1054ea671fbdSKate Stone 
1055ea671fbdSKate Stone     (lldb) break disable 1
1056ea671fbdSKate Stone     (lldb) break enable 1.1
1057ea671fbdSKate Stone 
1058ea671fbdSKate Stone execution will NOT stop at location 1.1.  To achieve that, type:
1059ea671fbdSKate Stone 
1060ea671fbdSKate Stone     (lldb) break disable 1.*
1061ea671fbdSKate Stone     (lldb) break enable 1.1
1062ea671fbdSKate Stone 
10637428a18cSKate Stone )"
10647428a18cSKate Stone         "The first command disables all locations for breakpoint 1, \
10657428a18cSKate Stone the second re-enables the first location.");
1066b0fac509SJim Ingham 
10675a988416SJim Ingham     CommandArgumentEntry arg;
1068b9c1b51eSKate Stone     CommandObject::AddIDsArgumentData(arg, eArgTypeBreakpointID,
1069b9c1b51eSKate Stone                                       eArgTypeBreakpointIDRange);
1070b9c1b51eSKate Stone     // Add the entry for the first argument for this command to the object's
1071b9c1b51eSKate Stone     // arguments vector.
10725a988416SJim Ingham     m_arguments.push_back(arg);
10735a988416SJim Ingham   }
10745a988416SJim Ingham 
10759e85e5a8SEugene Zelenko   ~CommandObjectBreakpointDisable() override = default;
10765a988416SJim Ingham 
10775a988416SJim Ingham protected:
1078b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
1079893c932aSJim Ingham     Target *target = GetSelectedOrDummyTarget();
1080b9c1b51eSKate Stone     if (target == nullptr) {
10815a988416SJim Ingham       result.AppendError("Invalid target.  No existing target or breakpoints.");
10825a988416SJim Ingham       result.SetStatus(eReturnStatusFailed);
10835a988416SJim Ingham       return false;
10845a988416SJim Ingham     }
10855a988416SJim Ingham 
1086bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
1087bb19a13cSSaleem Abdulrasool     target->GetBreakpointList().GetListMutex(lock);
10885a988416SJim Ingham 
10895a988416SJim Ingham     const BreakpointList &breakpoints = target->GetBreakpointList();
10905a988416SJim Ingham     size_t num_breakpoints = breakpoints.GetSize();
10915a988416SJim Ingham 
1092b9c1b51eSKate Stone     if (num_breakpoints == 0) {
10935a988416SJim Ingham       result.AppendError("No breakpoints exist to be disabled.");
10945a988416SJim Ingham       result.SetStatus(eReturnStatusFailed);
10955a988416SJim Ingham       return false;
10965a988416SJim Ingham     }
10975a988416SJim Ingham 
109811eb9c64SZachary Turner     if (command.empty()) {
10995a988416SJim Ingham       // No breakpoint selected; disable all currently set breakpoints.
1100b842f2ecSJim Ingham       target->DisableAllowedBreakpoints();
1101b9c1b51eSKate Stone       result.AppendMessageWithFormat("All breakpoints disabled. (%" PRIu64
1102b9c1b51eSKate Stone                                      " breakpoints)\n",
1103b9c1b51eSKate Stone                                      (uint64_t)num_breakpoints);
11045a988416SJim Ingham       result.SetStatus(eReturnStatusSuccessFinishNoResult);
1105b9c1b51eSKate Stone     } else {
11065a988416SJim Ingham       // Particular breakpoint selected; disable that breakpoint.
11075a988416SJim Ingham       BreakpointIDList valid_bp_ids;
11085a988416SJim Ingham 
1109b9c1b51eSKate Stone       CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs(
1110b842f2ecSJim Ingham           command, target, result, &valid_bp_ids,
1111b842f2ecSJim Ingham           BreakpointName::Permissions::PermissionKinds::disablePerm);
11125a988416SJim Ingham 
1113b9c1b51eSKate Stone       if (result.Succeeded()) {
11145a988416SJim Ingham         int disable_count = 0;
11155a988416SJim Ingham         int loc_count = 0;
11165a988416SJim Ingham         const size_t count = valid_bp_ids.GetSize();
1117b9c1b51eSKate Stone         for (size_t i = 0; i < count; ++i) {
11185a988416SJim Ingham           BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i);
11195a988416SJim Ingham 
1120b9c1b51eSKate Stone           if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) {
1121b9c1b51eSKate Stone             Breakpoint *breakpoint =
1122b9c1b51eSKate Stone                 target->GetBreakpointByID(cur_bp_id.GetBreakpointID()).get();
1123b9c1b51eSKate Stone             if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID) {
1124b9c1b51eSKate Stone               BreakpointLocation *location =
1125b9c1b51eSKate Stone                   breakpoint->FindLocationByID(cur_bp_id.GetLocationID()).get();
1126b9c1b51eSKate Stone               if (location) {
11275a988416SJim Ingham                 location->SetEnabled(false);
11285a988416SJim Ingham                 ++loc_count;
11295a988416SJim Ingham               }
1130b9c1b51eSKate Stone             } else {
11315a988416SJim Ingham               breakpoint->SetEnabled(false);
11325a988416SJim Ingham               ++disable_count;
11335a988416SJim Ingham             }
11345a988416SJim Ingham           }
11355a988416SJim Ingham         }
1136b9c1b51eSKate Stone         result.AppendMessageWithFormat("%d breakpoints disabled.\n",
1137b9c1b51eSKate Stone                                        disable_count + loc_count);
11385a988416SJim Ingham         result.SetStatus(eReturnStatusSuccessFinishNoResult);
11395a988416SJim Ingham       }
11405a988416SJim Ingham     }
11415a988416SJim Ingham 
11425a988416SJim Ingham     return result.Succeeded();
11435a988416SJim Ingham   }
11445a988416SJim Ingham };
11455a988416SJim Ingham 
11465a988416SJim Ingham // CommandObjectBreakpointList
11471f0f5b5bSZachary Turner 
11481f0f5b5bSZachary Turner #pragma mark List::CommandOptions
11496f4fb4e7SRaphael Isemann #define LLDB_OPTIONS_breakpoint_list
1150c5a2d747SRaphael Isemann #include "CommandOptions.inc"
11511f0f5b5bSZachary Turner 
11525a988416SJim Ingham #pragma mark List
11535a988416SJim Ingham 
1154b9c1b51eSKate Stone class CommandObjectBreakpointList : public CommandObjectParsed {
11555a988416SJim Ingham public:
1156b9c1b51eSKate Stone   CommandObjectBreakpointList(CommandInterpreter &interpreter)
1157b9c1b51eSKate Stone       : CommandObjectParsed(
1158b9c1b51eSKate Stone             interpreter, "breakpoint list",
11595a988416SJim Ingham             "List some or all breakpoints at configurable levels of detail.",
11609e85e5a8SEugene Zelenko             nullptr),
1161b9c1b51eSKate Stone         m_options() {
11625a988416SJim Ingham     CommandArgumentEntry arg;
11635a988416SJim Ingham     CommandArgumentData bp_id_arg;
11645a988416SJim Ingham 
11655a988416SJim Ingham     // Define the first (and only) variant of this arg.
11665a988416SJim Ingham     bp_id_arg.arg_type = eArgTypeBreakpointID;
11675a988416SJim Ingham     bp_id_arg.arg_repetition = eArgRepeatOptional;
11685a988416SJim Ingham 
1169b9c1b51eSKate Stone     // There is only one variant this argument could be; put it into the
1170b9c1b51eSKate Stone     // argument entry.
11715a988416SJim Ingham     arg.push_back(bp_id_arg);
11725a988416SJim Ingham 
11735a988416SJim Ingham     // Push the data for the first argument into the m_arguments vector.
11745a988416SJim Ingham     m_arguments.push_back(arg);
11755a988416SJim Ingham   }
11765a988416SJim Ingham 
11779e85e5a8SEugene Zelenko   ~CommandObjectBreakpointList() override = default;
11785a988416SJim Ingham 
1179b9c1b51eSKate Stone   Options *GetOptions() override { return &m_options; }
11805a988416SJim Ingham 
1181b9c1b51eSKate Stone   class CommandOptions : public Options {
11825a988416SJim Ingham   public:
1183b9c1b51eSKate Stone     CommandOptions()
1184b9c1b51eSKate Stone         : Options(), m_level(lldb::eDescriptionLevelBrief), m_use_dummy(false) {
11855a988416SJim Ingham     }
11865a988416SJim Ingham 
11879e85e5a8SEugene Zelenko     ~CommandOptions() override = default;
11885a988416SJim Ingham 
118997206d57SZachary Turner     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1190b9c1b51eSKate Stone                           ExecutionContext *execution_context) override {
119197206d57SZachary Turner       Status error;
11923bcdfc0eSGreg Clayton       const int short_option = m_getopt_table[option_idx].val;
11935a988416SJim Ingham 
1194b9c1b51eSKate Stone       switch (short_option) {
11955a988416SJim Ingham       case 'b':
11965a988416SJim Ingham         m_level = lldb::eDescriptionLevelBrief;
11975a988416SJim Ingham         break;
119833df7cd3SJim Ingham       case 'D':
119933df7cd3SJim Ingham         m_use_dummy = true;
120033df7cd3SJim Ingham         break;
12015a988416SJim Ingham       case 'f':
12025a988416SJim Ingham         m_level = lldb::eDescriptionLevelFull;
12035a988416SJim Ingham         break;
12045a988416SJim Ingham       case 'v':
12055a988416SJim Ingham         m_level = lldb::eDescriptionLevelVerbose;
12065a988416SJim Ingham         break;
12075a988416SJim Ingham       case 'i':
12085a988416SJim Ingham         m_internal = true;
12095a988416SJim Ingham         break;
12105a988416SJim Ingham       default:
1211b9c1b51eSKate Stone         error.SetErrorStringWithFormat("unrecognized option '%c'",
1212b9c1b51eSKate Stone                                        short_option);
12135a988416SJim Ingham         break;
12145a988416SJim Ingham       }
12155a988416SJim Ingham 
12165a988416SJim Ingham       return error;
12175a988416SJim Ingham     }
12185a988416SJim Ingham 
1219b9c1b51eSKate Stone     void OptionParsingStarting(ExecutionContext *execution_context) override {
12205a988416SJim Ingham       m_level = lldb::eDescriptionLevelFull;
12215a988416SJim Ingham       m_internal = false;
122233df7cd3SJim Ingham       m_use_dummy = false;
12235a988416SJim Ingham     }
12245a988416SJim Ingham 
12251f0f5b5bSZachary Turner     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
122670602439SZachary Turner       return llvm::makeArrayRef(g_breakpoint_list_options);
12271f0f5b5bSZachary Turner     }
12285a988416SJim Ingham 
12295a988416SJim Ingham     // Instance variables to hold the values for command options.
12305a988416SJim Ingham 
12315a988416SJim Ingham     lldb::DescriptionLevel m_level;
12325a988416SJim Ingham 
12335a988416SJim Ingham     bool m_internal;
123433df7cd3SJim Ingham     bool m_use_dummy;
12355a988416SJim Ingham   };
12365a988416SJim Ingham 
12375a988416SJim Ingham protected:
1238b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
123933df7cd3SJim Ingham     Target *target = GetSelectedOrDummyTarget(m_options.m_use_dummy);
124033df7cd3SJim Ingham 
1241b9c1b51eSKate Stone     if (target == nullptr) {
12425a988416SJim Ingham       result.AppendError("Invalid target. No current target or breakpoints.");
12435a988416SJim Ingham       result.SetStatus(eReturnStatusSuccessFinishNoResult);
12445a988416SJim Ingham       return true;
12455a988416SJim Ingham     }
12465a988416SJim Ingham 
1247b9c1b51eSKate Stone     const BreakpointList &breakpoints =
1248b9c1b51eSKate Stone         target->GetBreakpointList(m_options.m_internal);
1249bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
1250bb19a13cSSaleem Abdulrasool     target->GetBreakpointList(m_options.m_internal).GetListMutex(lock);
12515a988416SJim Ingham 
12525a988416SJim Ingham     size_t num_breakpoints = breakpoints.GetSize();
12535a988416SJim Ingham 
1254b9c1b51eSKate Stone     if (num_breakpoints == 0) {
12555a988416SJim Ingham       result.AppendMessage("No breakpoints currently set.");
12565a988416SJim Ingham       result.SetStatus(eReturnStatusSuccessFinishNoResult);
12575a988416SJim Ingham       return true;
12585a988416SJim Ingham     }
12595a988416SJim Ingham 
12605a988416SJim Ingham     Stream &output_stream = result.GetOutputStream();
12615a988416SJim Ingham 
126211eb9c64SZachary Turner     if (command.empty()) {
12635a988416SJim Ingham       // No breakpoint selected; show info about all currently set breakpoints.
12645a988416SJim Ingham       result.AppendMessage("Current breakpoints:");
1265b9c1b51eSKate Stone       for (size_t i = 0; i < num_breakpoints; ++i) {
12665a988416SJim Ingham         Breakpoint *breakpoint = breakpoints.GetBreakpointAtIndex(i).get();
1267b842f2ecSJim Ingham         if (breakpoint->AllowList())
1268b842f2ecSJim Ingham           AddBreakpointDescription(&output_stream, breakpoint,
1269b842f2ecSJim Ingham                                    m_options.m_level);
12705a988416SJim Ingham       }
12715a988416SJim Ingham       result.SetStatus(eReturnStatusSuccessFinishNoResult);
1272b9c1b51eSKate Stone     } else {
12735a988416SJim Ingham       // Particular breakpoints selected; show info about that breakpoint.
12745a988416SJim Ingham       BreakpointIDList valid_bp_ids;
1275b9c1b51eSKate Stone       CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs(
1276b842f2ecSJim Ingham           command, target, result, &valid_bp_ids,
1277b842f2ecSJim Ingham           BreakpointName::Permissions::PermissionKinds::listPerm);
12785a988416SJim Ingham 
1279b9c1b51eSKate Stone       if (result.Succeeded()) {
1280b9c1b51eSKate Stone         for (size_t i = 0; i < valid_bp_ids.GetSize(); ++i) {
12815a988416SJim Ingham           BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i);
1282b9c1b51eSKate Stone           Breakpoint *breakpoint =
1283b9c1b51eSKate Stone               target->GetBreakpointByID(cur_bp_id.GetBreakpointID()).get();
1284b9c1b51eSKate Stone           AddBreakpointDescription(&output_stream, breakpoint,
1285b9c1b51eSKate Stone                                    m_options.m_level);
12865a988416SJim Ingham         }
12875a988416SJim Ingham         result.SetStatus(eReturnStatusSuccessFinishNoResult);
1288b9c1b51eSKate Stone       } else {
12897428a18cSKate Stone         result.AppendError("Invalid breakpoint ID.");
12905a988416SJim Ingham         result.SetStatus(eReturnStatusFailed);
12915a988416SJim Ingham       }
12925a988416SJim Ingham     }
12935a988416SJim Ingham 
12945a988416SJim Ingham     return result.Succeeded();
12955a988416SJim Ingham   }
12965a988416SJim Ingham 
12975a988416SJim Ingham private:
12985a988416SJim Ingham   CommandOptions m_options;
12995a988416SJim Ingham };
13005a988416SJim Ingham 
13015a988416SJim Ingham // CommandObjectBreakpointClear
13021f0f5b5bSZachary Turner #pragma mark Clear::CommandOptions
13031f0f5b5bSZachary Turner 
1304f94668e3SRaphael Isemann #define LLDB_OPTIONS_breakpoint_clear
1305f94668e3SRaphael Isemann #include "CommandOptions.inc"
13061f0f5b5bSZachary Turner 
13075a988416SJim Ingham #pragma mark Clear
13085a988416SJim Ingham 
1309b9c1b51eSKate Stone class CommandObjectBreakpointClear : public CommandObjectParsed {
13105a988416SJim Ingham public:
1311efe8e7e3SFangrui Song   enum BreakpointClearType { eClearTypeInvalid, eClearTypeFileAndLine };
13125a988416SJim Ingham 
13137428a18cSKate Stone   CommandObjectBreakpointClear(CommandInterpreter &interpreter)
13147428a18cSKate Stone       : CommandObjectParsed(interpreter, "breakpoint clear",
1315b9c1b51eSKate Stone                             "Delete or disable breakpoints matching the "
1316b9c1b51eSKate Stone                             "specified source file and line.",
13175a988416SJim Ingham                             "breakpoint clear <cmd-options>"),
1318b9c1b51eSKate Stone         m_options() {}
13195a988416SJim Ingham 
13209e85e5a8SEugene Zelenko   ~CommandObjectBreakpointClear() override = default;
13215a988416SJim Ingham 
1322b9c1b51eSKate Stone   Options *GetOptions() override { return &m_options; }
13235a988416SJim Ingham 
1324b9c1b51eSKate Stone   class CommandOptions : public Options {
13255a988416SJim Ingham   public:
1326b9c1b51eSKate Stone     CommandOptions() : Options(), m_filename(), m_line_num(0) {}
13275a988416SJim Ingham 
13289e85e5a8SEugene Zelenko     ~CommandOptions() override = default;
13295a988416SJim Ingham 
133097206d57SZachary Turner     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1331b9c1b51eSKate Stone                           ExecutionContext *execution_context) override {
133297206d57SZachary Turner       Status error;
13333bcdfc0eSGreg Clayton       const int short_option = m_getopt_table[option_idx].val;
13345a988416SJim Ingham 
1335b9c1b51eSKate Stone       switch (short_option) {
13365a988416SJim Ingham       case 'f':
13375a988416SJim Ingham         m_filename.assign(option_arg);
13385a988416SJim Ingham         break;
13395a988416SJim Ingham 
13405a988416SJim Ingham       case 'l':
1341fe11483bSZachary Turner         option_arg.getAsInteger(0, m_line_num);
13425a988416SJim Ingham         break;
13435a988416SJim Ingham 
13445a988416SJim Ingham       default:
1345b9c1b51eSKate Stone         error.SetErrorStringWithFormat("unrecognized option '%c'",
1346b9c1b51eSKate Stone                                        short_option);
13475a988416SJim Ingham         break;
13485a988416SJim Ingham       }
13495a988416SJim Ingham 
13505a988416SJim Ingham       return error;
13515a988416SJim Ingham     }
13525a988416SJim Ingham 
1353b9c1b51eSKate Stone     void OptionParsingStarting(ExecutionContext *execution_context) override {
13545a988416SJim Ingham       m_filename.clear();
13555a988416SJim Ingham       m_line_num = 0;
13565a988416SJim Ingham     }
13575a988416SJim Ingham 
13581f0f5b5bSZachary Turner     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
135970602439SZachary Turner       return llvm::makeArrayRef(g_breakpoint_clear_options);
13601f0f5b5bSZachary Turner     }
13615a988416SJim Ingham 
13625a988416SJim Ingham     // Instance variables to hold the values for command options.
13635a988416SJim Ingham 
13645a988416SJim Ingham     std::string m_filename;
13655a988416SJim Ingham     uint32_t m_line_num;
13665a988416SJim Ingham   };
13675a988416SJim Ingham 
13685a988416SJim Ingham protected:
1369b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
1370893c932aSJim Ingham     Target *target = GetSelectedOrDummyTarget();
1371b9c1b51eSKate Stone     if (target == nullptr) {
13725a988416SJim Ingham       result.AppendError("Invalid target. No existing target or breakpoints.");
13735a988416SJim Ingham       result.SetStatus(eReturnStatusFailed);
13745a988416SJim Ingham       return false;
13755a988416SJim Ingham     }
13765a988416SJim Ingham 
137705097246SAdrian Prantl     // The following are the various types of breakpoints that could be
137805097246SAdrian Prantl     // cleared:
13795a988416SJim Ingham     //   1). -f -l (clearing breakpoint by source location)
13805a988416SJim Ingham 
13815a988416SJim Ingham     BreakpointClearType break_type = eClearTypeInvalid;
13825a988416SJim Ingham 
13835a988416SJim Ingham     if (m_options.m_line_num != 0)
13845a988416SJim Ingham       break_type = eClearTypeFileAndLine;
13855a988416SJim Ingham 
1386bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
1387bb19a13cSSaleem Abdulrasool     target->GetBreakpointList().GetListMutex(lock);
13885a988416SJim Ingham 
13895a988416SJim Ingham     BreakpointList &breakpoints = target->GetBreakpointList();
13905a988416SJim Ingham     size_t num_breakpoints = breakpoints.GetSize();
13915a988416SJim Ingham 
13925a988416SJim Ingham     // Early return if there's no breakpoint at all.
1393b9c1b51eSKate Stone     if (num_breakpoints == 0) {
13945a988416SJim Ingham       result.AppendError("Breakpoint clear: No breakpoint cleared.");
13955a988416SJim Ingham       result.SetStatus(eReturnStatusFailed);
13965a988416SJim Ingham       return result.Succeeded();
13975a988416SJim Ingham     }
13985a988416SJim Ingham 
13995a988416SJim Ingham     // Find matching breakpoints and delete them.
14005a988416SJim Ingham 
14015a988416SJim Ingham     // First create a copy of all the IDs.
14025a988416SJim Ingham     std::vector<break_id_t> BreakIDs;
14035a988416SJim Ingham     for (size_t i = 0; i < num_breakpoints; ++i)
14049e85e5a8SEugene Zelenko       BreakIDs.push_back(breakpoints.GetBreakpointAtIndex(i)->GetID());
14055a988416SJim Ingham 
14065a988416SJim Ingham     int num_cleared = 0;
14075a988416SJim Ingham     StreamString ss;
1408b9c1b51eSKate Stone     switch (break_type) {
14095a988416SJim Ingham     case eClearTypeFileAndLine: // Breakpoint by source position
14105a988416SJim Ingham     {
14115a988416SJim Ingham       const ConstString filename(m_options.m_filename.c_str());
14125a988416SJim Ingham       BreakpointLocationCollection loc_coll;
14135a988416SJim Ingham 
1414b9c1b51eSKate Stone       for (size_t i = 0; i < num_breakpoints; ++i) {
14155a988416SJim Ingham         Breakpoint *bp = breakpoints.FindBreakpointByID(BreakIDs[i]).get();
14165a988416SJim Ingham 
1417b9c1b51eSKate Stone         if (bp->GetMatchingFileLine(filename, m_options.m_line_num, loc_coll)) {
1418b9c1b51eSKate Stone           // If the collection size is 0, it's a full match and we can just
1419b9c1b51eSKate Stone           // remove the breakpoint.
1420b9c1b51eSKate Stone           if (loc_coll.GetSize() == 0) {
14215a988416SJim Ingham             bp->GetDescription(&ss, lldb::eDescriptionLevelBrief);
14225a988416SJim Ingham             ss.EOL();
14235a988416SJim Ingham             target->RemoveBreakpointByID(bp->GetID());
14245a988416SJim Ingham             ++num_cleared;
14255a988416SJim Ingham           }
14265a988416SJim Ingham         }
14275a988416SJim Ingham       }
1428b9c1b51eSKate Stone     } break;
14295a988416SJim Ingham 
14305a988416SJim Ingham     default:
14315a988416SJim Ingham       break;
14325a988416SJim Ingham     }
14335a988416SJim Ingham 
1434b9c1b51eSKate Stone     if (num_cleared > 0) {
14355a988416SJim Ingham       Stream &output_stream = result.GetOutputStream();
14365a988416SJim Ingham       output_stream.Printf("%d breakpoints cleared:\n", num_cleared);
1437c156427dSZachary Turner       output_stream << ss.GetString();
14385a988416SJim Ingham       output_stream.EOL();
14395a988416SJim Ingham       result.SetStatus(eReturnStatusSuccessFinishNoResult);
1440b9c1b51eSKate Stone     } else {
14415a988416SJim Ingham       result.AppendError("Breakpoint clear: No breakpoint cleared.");
14425a988416SJim Ingham       result.SetStatus(eReturnStatusFailed);
14435a988416SJim Ingham     }
14445a988416SJim Ingham 
14455a988416SJim Ingham     return result.Succeeded();
14465a988416SJim Ingham   }
14475a988416SJim Ingham 
14485a988416SJim Ingham private:
14495a988416SJim Ingham   CommandOptions m_options;
14505a988416SJim Ingham };
14515a988416SJim Ingham 
14525a988416SJim Ingham // CommandObjectBreakpointDelete
1453f94668e3SRaphael Isemann #define LLDB_OPTIONS_breakpoint_delete
1454f94668e3SRaphael Isemann #include "CommandOptions.inc"
14551f0f5b5bSZachary Turner 
14565a988416SJim Ingham #pragma mark Delete
14575a988416SJim Ingham 
1458b9c1b51eSKate Stone class CommandObjectBreakpointDelete : public CommandObjectParsed {
14595a988416SJim Ingham public:
1460b9c1b51eSKate Stone   CommandObjectBreakpointDelete(CommandInterpreter &interpreter)
1461b9c1b51eSKate Stone       : CommandObjectParsed(interpreter, "breakpoint delete",
1462b9c1b51eSKate Stone                             "Delete the specified breakpoint(s).  If no "
1463b9c1b51eSKate Stone                             "breakpoints are specified, delete them all.",
14649e85e5a8SEugene Zelenko                             nullptr),
1465b9c1b51eSKate Stone         m_options() {
14665a988416SJim Ingham     CommandArgumentEntry arg;
1467b9c1b51eSKate Stone     CommandObject::AddIDsArgumentData(arg, eArgTypeBreakpointID,
1468b9c1b51eSKate Stone                                       eArgTypeBreakpointIDRange);
1469b9c1b51eSKate Stone     // Add the entry for the first argument for this command to the object's
1470b9c1b51eSKate Stone     // arguments vector.
14715a988416SJim Ingham     m_arguments.push_back(arg);
14725a988416SJim Ingham   }
14735a988416SJim Ingham 
14749e85e5a8SEugene Zelenko   ~CommandObjectBreakpointDelete() override = default;
14755a988416SJim Ingham 
1476b9c1b51eSKate Stone   Options *GetOptions() override { return &m_options; }
147733df7cd3SJim Ingham 
1478b9c1b51eSKate Stone   class CommandOptions : public Options {
147933df7cd3SJim Ingham   public:
1480b9c1b51eSKate Stone     CommandOptions() : Options(), m_use_dummy(false), m_force(false) {}
148133df7cd3SJim Ingham 
14829e85e5a8SEugene Zelenko     ~CommandOptions() override = default;
148333df7cd3SJim Ingham 
148497206d57SZachary Turner     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1485b9c1b51eSKate Stone                           ExecutionContext *execution_context) override {
148697206d57SZachary Turner       Status error;
148733df7cd3SJim Ingham       const int short_option = m_getopt_table[option_idx].val;
148833df7cd3SJim Ingham 
1489b9c1b51eSKate Stone       switch (short_option) {
149033df7cd3SJim Ingham       case 'f':
149133df7cd3SJim Ingham         m_force = true;
149233df7cd3SJim Ingham         break;
149333df7cd3SJim Ingham 
149433df7cd3SJim Ingham       case 'D':
149533df7cd3SJim Ingham         m_use_dummy = true;
149633df7cd3SJim Ingham         break;
149733df7cd3SJim Ingham 
149833df7cd3SJim Ingham       default:
1499b9c1b51eSKate Stone         error.SetErrorStringWithFormat("unrecognized option '%c'",
1500b9c1b51eSKate Stone                                        short_option);
150133df7cd3SJim Ingham         break;
150233df7cd3SJim Ingham       }
150333df7cd3SJim Ingham 
150433df7cd3SJim Ingham       return error;
150533df7cd3SJim Ingham     }
150633df7cd3SJim Ingham 
1507b9c1b51eSKate Stone     void OptionParsingStarting(ExecutionContext *execution_context) override {
150833df7cd3SJim Ingham       m_use_dummy = false;
150933df7cd3SJim Ingham       m_force = false;
151033df7cd3SJim Ingham     }
151133df7cd3SJim Ingham 
15121f0f5b5bSZachary Turner     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
151370602439SZachary Turner       return llvm::makeArrayRef(g_breakpoint_delete_options);
15141f0f5b5bSZachary Turner     }
151533df7cd3SJim Ingham 
151633df7cd3SJim Ingham     // Instance variables to hold the values for command options.
151733df7cd3SJim Ingham     bool m_use_dummy;
151833df7cd3SJim Ingham     bool m_force;
151933df7cd3SJim Ingham   };
152033df7cd3SJim Ingham 
15215a988416SJim Ingham protected:
1522b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
152333df7cd3SJim Ingham     Target *target = GetSelectedOrDummyTarget(m_options.m_use_dummy);
152433df7cd3SJim Ingham 
1525b9c1b51eSKate Stone     if (target == nullptr) {
15265a988416SJim Ingham       result.AppendError("Invalid target. No existing target or breakpoints.");
15275a988416SJim Ingham       result.SetStatus(eReturnStatusFailed);
15285a988416SJim Ingham       return false;
15295a988416SJim Ingham     }
15305a988416SJim Ingham 
1531bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
1532bb19a13cSSaleem Abdulrasool     target->GetBreakpointList().GetListMutex(lock);
15335a988416SJim Ingham 
15345a988416SJim Ingham     const BreakpointList &breakpoints = target->GetBreakpointList();
15355a988416SJim Ingham 
15365a988416SJim Ingham     size_t num_breakpoints = breakpoints.GetSize();
15375a988416SJim Ingham 
1538b9c1b51eSKate Stone     if (num_breakpoints == 0) {
15395a988416SJim Ingham       result.AppendError("No breakpoints exist to be deleted.");
15405a988416SJim Ingham       result.SetStatus(eReturnStatusFailed);
15415a988416SJim Ingham       return false;
15425a988416SJim Ingham     }
15435a988416SJim Ingham 
154411eb9c64SZachary Turner     if (command.empty()) {
1545b9c1b51eSKate Stone       if (!m_options.m_force &&
1546b9c1b51eSKate Stone           !m_interpreter.Confirm(
1547b9c1b51eSKate Stone               "About to delete all breakpoints, do you want to do that?",
1548b9c1b51eSKate Stone               true)) {
15495a988416SJim Ingham         result.AppendMessage("Operation cancelled...");
1550b9c1b51eSKate Stone       } else {
1551b842f2ecSJim Ingham         target->RemoveAllowedBreakpoints();
1552b9c1b51eSKate Stone         result.AppendMessageWithFormat(
1553b9c1b51eSKate Stone             "All breakpoints removed. (%" PRIu64 " breakpoint%s)\n",
1554b9c1b51eSKate Stone             (uint64_t)num_breakpoints, num_breakpoints > 1 ? "s" : "");
15555a988416SJim Ingham       }
15565a988416SJim Ingham       result.SetStatus(eReturnStatusSuccessFinishNoResult);
1557b9c1b51eSKate Stone     } else {
15585a988416SJim Ingham       // Particular breakpoint selected; disable that breakpoint.
15595a988416SJim Ingham       BreakpointIDList valid_bp_ids;
1560b9c1b51eSKate Stone       CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs(
1561b842f2ecSJim Ingham           command, target, result, &valid_bp_ids,
1562b842f2ecSJim Ingham           BreakpointName::Permissions::PermissionKinds::deletePerm);
15635a988416SJim Ingham 
1564b9c1b51eSKate Stone       if (result.Succeeded()) {
15655a988416SJim Ingham         int delete_count = 0;
15665a988416SJim Ingham         int disable_count = 0;
15675a988416SJim Ingham         const size_t count = valid_bp_ids.GetSize();
1568b9c1b51eSKate Stone         for (size_t i = 0; i < count; ++i) {
15695a988416SJim Ingham           BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i);
15705a988416SJim Ingham 
1571b9c1b51eSKate Stone           if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) {
1572b9c1b51eSKate Stone             if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID) {
1573b9c1b51eSKate Stone               Breakpoint *breakpoint =
1574b9c1b51eSKate Stone                   target->GetBreakpointByID(cur_bp_id.GetBreakpointID()).get();
1575b9c1b51eSKate Stone               BreakpointLocation *location =
1576b9c1b51eSKate Stone                   breakpoint->FindLocationByID(cur_bp_id.GetLocationID()).get();
1577b9c1b51eSKate Stone               // It makes no sense to try to delete individual locations, so we
1578b9c1b51eSKate Stone               // disable them instead.
1579b9c1b51eSKate Stone               if (location) {
15805a988416SJim Ingham                 location->SetEnabled(false);
15815a988416SJim Ingham                 ++disable_count;
15825a988416SJim Ingham               }
1583b9c1b51eSKate Stone             } else {
15845a988416SJim Ingham               target->RemoveBreakpointByID(cur_bp_id.GetBreakpointID());
15855a988416SJim Ingham               ++delete_count;
15865a988416SJim Ingham             }
15875a988416SJim Ingham           }
15885a988416SJim Ingham         }
1589b9c1b51eSKate Stone         result.AppendMessageWithFormat(
1590b9c1b51eSKate Stone             "%d breakpoints deleted; %d breakpoint locations disabled.\n",
15915a988416SJim Ingham             delete_count, disable_count);
15925a988416SJim Ingham         result.SetStatus(eReturnStatusSuccessFinishNoResult);
15935a988416SJim Ingham       }
15945a988416SJim Ingham     }
15955a988416SJim Ingham     return result.Succeeded();
15965a988416SJim Ingham   }
15979e85e5a8SEugene Zelenko 
159833df7cd3SJim Ingham private:
159933df7cd3SJim Ingham   CommandOptions m_options;
160033df7cd3SJim Ingham };
160133df7cd3SJim Ingham 
16025e09c8c3SJim Ingham // CommandObjectBreakpointName
1603f94668e3SRaphael Isemann #define LLDB_OPTIONS_breakpoint_name
1604f94668e3SRaphael Isemann #include "CommandOptions.inc"
1605bd68a052SRaphael Isemann 
1606b9c1b51eSKate Stone class BreakpointNameOptionGroup : public OptionGroup {
16075e09c8c3SJim Ingham public:
1608b9c1b51eSKate Stone   BreakpointNameOptionGroup()
1609b9c1b51eSKate Stone       : OptionGroup(), m_breakpoint(LLDB_INVALID_BREAK_ID), m_use_dummy(false) {
16105e09c8c3SJim Ingham   }
16115e09c8c3SJim Ingham 
16129e85e5a8SEugene Zelenko   ~BreakpointNameOptionGroup() override = default;
16135e09c8c3SJim Ingham 
16141f0f5b5bSZachary Turner   llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
161570602439SZachary Turner     return llvm::makeArrayRef(g_breakpoint_name_options);
16165e09c8c3SJim Ingham   }
16175e09c8c3SJim Ingham 
161897206d57SZachary Turner   Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1619b9c1b51eSKate Stone                         ExecutionContext *execution_context) override {
162097206d57SZachary Turner     Status error;
16215e09c8c3SJim Ingham     const int short_option = g_breakpoint_name_options[option_idx].short_option;
16225e09c8c3SJim Ingham 
1623b9c1b51eSKate Stone     switch (short_option) {
16245e09c8c3SJim Ingham     case 'N':
1625fe11483bSZachary Turner       if (BreakpointID::StringIsBreakpointName(option_arg, error) &&
1626b9c1b51eSKate Stone           error.Success())
1627fe11483bSZachary Turner         m_name.SetValueFromString(option_arg);
16285e09c8c3SJim Ingham       break;
16295e09c8c3SJim Ingham     case 'B':
1630fe11483bSZachary Turner       if (m_breakpoint.SetValueFromString(option_arg).Fail())
1631b9c1b51eSKate Stone         error.SetErrorStringWithFormat(
16328cef4b0bSZachary Turner             "unrecognized value \"%s\" for breakpoint",
1633fe11483bSZachary Turner             option_arg.str().c_str());
16345e09c8c3SJim Ingham       break;
16355e09c8c3SJim Ingham     case 'D':
1636fe11483bSZachary Turner       if (m_use_dummy.SetValueFromString(option_arg).Fail())
1637b9c1b51eSKate Stone         error.SetErrorStringWithFormat(
16388cef4b0bSZachary Turner             "unrecognized value \"%s\" for use-dummy",
1639fe11483bSZachary Turner             option_arg.str().c_str());
16405e09c8c3SJim Ingham       break;
1641e9632ebaSJim Ingham     case 'H':
1642e9632ebaSJim Ingham       m_help_string.SetValueFromString(option_arg);
1643e9632ebaSJim Ingham       break;
16445e09c8c3SJim Ingham 
16455e09c8c3SJim Ingham     default:
1646b9c1b51eSKate Stone       error.SetErrorStringWithFormat("unrecognized short option '%c'",
1647b9c1b51eSKate Stone                                      short_option);
16485e09c8c3SJim Ingham       break;
16495e09c8c3SJim Ingham     }
16505e09c8c3SJim Ingham     return error;
16515e09c8c3SJim Ingham   }
16525e09c8c3SJim Ingham 
1653b9c1b51eSKate Stone   void OptionParsingStarting(ExecutionContext *execution_context) override {
16545e09c8c3SJim Ingham     m_name.Clear();
16555e09c8c3SJim Ingham     m_breakpoint.Clear();
16565e09c8c3SJim Ingham     m_use_dummy.Clear();
16575e09c8c3SJim Ingham     m_use_dummy.SetDefaultValue(false);
1658e9632ebaSJim Ingham     m_help_string.Clear();
16595e09c8c3SJim Ingham   }
16605e09c8c3SJim Ingham 
16615e09c8c3SJim Ingham   OptionValueString m_name;
16625e09c8c3SJim Ingham   OptionValueUInt64 m_breakpoint;
16635e09c8c3SJim Ingham   OptionValueBoolean m_use_dummy;
1664e9632ebaSJim Ingham   OptionValueString m_help_string;
16655e09c8c3SJim Ingham };
16665e09c8c3SJim Ingham 
1667f94668e3SRaphael Isemann #define LLDB_OPTIONS_breakpoint_access
1668f94668e3SRaphael Isemann #include "CommandOptions.inc"
1669b842f2ecSJim Ingham 
16708fe53c49STatyana Krasnukha class BreakpointAccessOptionGroup : public OptionGroup {
1671b842f2ecSJim Ingham public:
16728fe53c49STatyana Krasnukha   BreakpointAccessOptionGroup() : OptionGroup() {}
1673b842f2ecSJim Ingham 
1674b842f2ecSJim Ingham   ~BreakpointAccessOptionGroup() override = default;
1675b842f2ecSJim Ingham 
1676b842f2ecSJim Ingham   llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
1677b842f2ecSJim Ingham     return llvm::makeArrayRef(g_breakpoint_access_options);
1678b842f2ecSJim Ingham   }
1679b842f2ecSJim Ingham   Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1680b842f2ecSJim Ingham                         ExecutionContext *execution_context) override {
1681b842f2ecSJim Ingham     Status error;
1682b842f2ecSJim Ingham     const int short_option
1683b842f2ecSJim Ingham         = g_breakpoint_access_options[option_idx].short_option;
1684b842f2ecSJim Ingham 
1685b842f2ecSJim Ingham     switch (short_option) {
1686b842f2ecSJim Ingham       case 'L': {
1687b842f2ecSJim Ingham         bool value, success;
168847cbf4a0SPavel Labath         value = OptionArgParser::ToBoolean(option_arg, false, &success);
1689b842f2ecSJim Ingham         if (success) {
1690b842f2ecSJim Ingham           m_permissions.SetAllowList(value);
1691b842f2ecSJim Ingham         } else
1692b842f2ecSJim Ingham           error.SetErrorStringWithFormat(
1693b842f2ecSJim Ingham               "invalid boolean value '%s' passed for -L option",
1694b842f2ecSJim Ingham               option_arg.str().c_str());
1695b842f2ecSJim Ingham       } break;
1696b842f2ecSJim Ingham       case 'A': {
1697b842f2ecSJim Ingham         bool value, success;
169847cbf4a0SPavel Labath         value = OptionArgParser::ToBoolean(option_arg, false, &success);
1699b842f2ecSJim Ingham         if (success) {
1700b842f2ecSJim Ingham           m_permissions.SetAllowDisable(value);
1701b842f2ecSJim Ingham         } else
1702b842f2ecSJim Ingham           error.SetErrorStringWithFormat(
1703b842f2ecSJim Ingham               "invalid boolean value '%s' passed for -L option",
1704b842f2ecSJim Ingham               option_arg.str().c_str());
1705b842f2ecSJim Ingham       } break;
1706b842f2ecSJim Ingham       case 'D': {
1707b842f2ecSJim Ingham         bool value, success;
170847cbf4a0SPavel Labath         value = OptionArgParser::ToBoolean(option_arg, false, &success);
1709b842f2ecSJim Ingham         if (success) {
1710b842f2ecSJim Ingham           m_permissions.SetAllowDelete(value);
1711b842f2ecSJim Ingham         } else
1712b842f2ecSJim Ingham           error.SetErrorStringWithFormat(
1713b842f2ecSJim Ingham               "invalid boolean value '%s' passed for -L option",
1714b842f2ecSJim Ingham               option_arg.str().c_str());
1715b842f2ecSJim Ingham       } break;
1716b842f2ecSJim Ingham 
1717b842f2ecSJim Ingham     }
1718b842f2ecSJim Ingham 
1719b842f2ecSJim Ingham     return error;
1720b842f2ecSJim Ingham   }
1721b842f2ecSJim Ingham 
1722b842f2ecSJim Ingham   void OptionParsingStarting(ExecutionContext *execution_context) override {
1723b842f2ecSJim Ingham   }
1724b842f2ecSJim Ingham 
1725b842f2ecSJim Ingham   const BreakpointName::Permissions &GetPermissions() const
1726b842f2ecSJim Ingham   {
1727b842f2ecSJim Ingham     return m_permissions;
1728b842f2ecSJim Ingham   }
1729b842f2ecSJim Ingham   BreakpointName::Permissions m_permissions;
1730b842f2ecSJim Ingham };
1731b842f2ecSJim Ingham 
1732b842f2ecSJim Ingham class CommandObjectBreakpointNameConfigure : public CommandObjectParsed {
1733b842f2ecSJim Ingham public:
1734b842f2ecSJim Ingham   CommandObjectBreakpointNameConfigure(CommandInterpreter &interpreter)
1735b842f2ecSJim Ingham       : CommandObjectParsed(
1736b842f2ecSJim Ingham             interpreter, "configure", "Configure the options for the breakpoint"
1737b842f2ecSJim Ingham             " name provided.  "
1738b842f2ecSJim Ingham             "If you provide a breakpoint id, the options will be copied from "
1739b842f2ecSJim Ingham             "the breakpoint, otherwise only the options specified will be set "
1740b842f2ecSJim Ingham             "on the name.",
1741b842f2ecSJim Ingham             "breakpoint name configure <command-options> "
1742b842f2ecSJim Ingham             "<breakpoint-name-list>"),
1743b842f2ecSJim Ingham         m_bp_opts(), m_option_group() {
1744b842f2ecSJim Ingham     // Create the first variant for the first (and only) argument for this
1745b842f2ecSJim Ingham     // command.
1746b842f2ecSJim Ingham     CommandArgumentEntry arg1;
1747b842f2ecSJim Ingham     CommandArgumentData id_arg;
1748b842f2ecSJim Ingham     id_arg.arg_type = eArgTypeBreakpointName;
1749b842f2ecSJim Ingham     id_arg.arg_repetition = eArgRepeatOptional;
1750b842f2ecSJim Ingham     arg1.push_back(id_arg);
1751b842f2ecSJim Ingham     m_arguments.push_back(arg1);
1752b842f2ecSJim Ingham 
1753b842f2ecSJim Ingham     m_option_group.Append(&m_bp_opts,
1754b842f2ecSJim Ingham                           LLDB_OPT_SET_ALL,
1755b842f2ecSJim Ingham                           LLDB_OPT_SET_1);
1756b842f2ecSJim Ingham     m_option_group.Append(&m_access_options,
1757b842f2ecSJim Ingham                           LLDB_OPT_SET_ALL,
1758b842f2ecSJim Ingham                           LLDB_OPT_SET_ALL);
1759e9632ebaSJim Ingham     m_option_group.Append(&m_bp_id,
1760e9632ebaSJim Ingham                           LLDB_OPT_SET_2|LLDB_OPT_SET_4,
1761e9632ebaSJim Ingham                           LLDB_OPT_SET_ALL);
1762b842f2ecSJim Ingham     m_option_group.Finalize();
1763b842f2ecSJim Ingham   }
1764b842f2ecSJim Ingham 
1765b842f2ecSJim Ingham   ~CommandObjectBreakpointNameConfigure() override = default;
1766b842f2ecSJim Ingham 
1767b842f2ecSJim Ingham   Options *GetOptions() override { return &m_option_group; }
1768b842f2ecSJim Ingham 
1769b842f2ecSJim Ingham protected:
1770b842f2ecSJim Ingham   bool DoExecute(Args &command, CommandReturnObject &result) override {
1771b842f2ecSJim Ingham 
1772b842f2ecSJim Ingham     const size_t argc = command.GetArgumentCount();
1773b842f2ecSJim Ingham     if (argc == 0) {
1774b842f2ecSJim Ingham       result.AppendError("No names provided.");
1775b842f2ecSJim Ingham       result.SetStatus(eReturnStatusFailed);
1776b842f2ecSJim Ingham       return false;
1777b842f2ecSJim Ingham     }
1778b842f2ecSJim Ingham 
1779b842f2ecSJim Ingham     Target *target =
1780b842f2ecSJim Ingham         GetSelectedOrDummyTarget(false);
1781b842f2ecSJim Ingham 
1782b842f2ecSJim Ingham     if (target == nullptr) {
1783b842f2ecSJim Ingham       result.AppendError("Invalid target. No existing target or breakpoints.");
1784b842f2ecSJim Ingham       result.SetStatus(eReturnStatusFailed);
1785b842f2ecSJim Ingham       return false;
1786b842f2ecSJim Ingham     }
1787b842f2ecSJim Ingham 
1788b842f2ecSJim Ingham     std::unique_lock<std::recursive_mutex> lock;
1789b842f2ecSJim Ingham     target->GetBreakpointList().GetListMutex(lock);
1790b842f2ecSJim Ingham 
1791b842f2ecSJim Ingham     // Make a pass through first to see that all the names are legal.
1792b842f2ecSJim Ingham     for (auto &entry : command.entries()) {
1793b842f2ecSJim Ingham       Status error;
1794b842f2ecSJim Ingham       if (!BreakpointID::StringIsBreakpointName(entry.ref, error))
1795b842f2ecSJim Ingham       {
1796b842f2ecSJim Ingham         result.AppendErrorWithFormat("Invalid breakpoint name: %s - %s",
1797b842f2ecSJim Ingham                                      entry.c_str(), error.AsCString());
1798b842f2ecSJim Ingham         result.SetStatus(eReturnStatusFailed);
1799b842f2ecSJim Ingham         return false;
1800b842f2ecSJim Ingham       }
1801b842f2ecSJim Ingham     }
180205097246SAdrian Prantl     // Now configure them, we already pre-checked the names so we don't need to
180305097246SAdrian Prantl     // check the error:
1804b842f2ecSJim Ingham     BreakpointSP bp_sp;
1805b842f2ecSJim Ingham     if (m_bp_id.m_breakpoint.OptionWasSet())
1806b842f2ecSJim Ingham     {
1807b842f2ecSJim Ingham       lldb::break_id_t bp_id = m_bp_id.m_breakpoint.GetUInt64Value();
1808b842f2ecSJim Ingham       bp_sp = target->GetBreakpointByID(bp_id);
1809b842f2ecSJim Ingham       if (!bp_sp)
1810b842f2ecSJim Ingham       {
1811b842f2ecSJim Ingham         result.AppendErrorWithFormatv("Could not find specified breakpoint {0}",
1812b842f2ecSJim Ingham                            bp_id);
1813b842f2ecSJim Ingham         result.SetStatus(eReturnStatusFailed);
1814b842f2ecSJim Ingham         return false;
1815b842f2ecSJim Ingham       }
1816b842f2ecSJim Ingham     }
1817b842f2ecSJim Ingham 
1818b842f2ecSJim Ingham     Status error;
1819b842f2ecSJim Ingham     for (auto &entry : command.entries()) {
1820b842f2ecSJim Ingham       ConstString name(entry.c_str());
1821b842f2ecSJim Ingham       BreakpointName *bp_name = target->FindBreakpointName(name, true, error);
1822b842f2ecSJim Ingham       if (!bp_name)
1823b842f2ecSJim Ingham         continue;
1824e9632ebaSJim Ingham       if (m_bp_id.m_help_string.OptionWasSet())
1825e9632ebaSJim Ingham         bp_name->SetHelp(m_bp_id.m_help_string.GetStringValue().str().c_str());
1826e9632ebaSJim Ingham 
1827b842f2ecSJim Ingham       if (bp_sp)
1828b842f2ecSJim Ingham         target->ConfigureBreakpointName(*bp_name,
1829b842f2ecSJim Ingham                                        *bp_sp->GetOptions(),
1830b842f2ecSJim Ingham                                        m_access_options.GetPermissions());
1831b842f2ecSJim Ingham       else
1832b842f2ecSJim Ingham         target->ConfigureBreakpointName(*bp_name,
1833b842f2ecSJim Ingham                                        m_bp_opts.GetBreakpointOptions(),
1834b842f2ecSJim Ingham                                        m_access_options.GetPermissions());
1835b842f2ecSJim Ingham     }
1836b842f2ecSJim Ingham     return true;
1837b842f2ecSJim Ingham   }
1838b842f2ecSJim Ingham 
1839b842f2ecSJim Ingham private:
1840b842f2ecSJim Ingham   BreakpointNameOptionGroup m_bp_id; // Only using the id part of this.
1841b842f2ecSJim Ingham   BreakpointOptionGroup m_bp_opts;
1842b842f2ecSJim Ingham   BreakpointAccessOptionGroup m_access_options;
1843b842f2ecSJim Ingham   OptionGroupOptions m_option_group;
1844b842f2ecSJim Ingham };
1845b842f2ecSJim Ingham 
1846b9c1b51eSKate Stone class CommandObjectBreakpointNameAdd : public CommandObjectParsed {
18475e09c8c3SJim Ingham public:
1848b9c1b51eSKate Stone   CommandObjectBreakpointNameAdd(CommandInterpreter &interpreter)
1849b9c1b51eSKate Stone       : CommandObjectParsed(
1850b9c1b51eSKate Stone             interpreter, "add", "Add a name to the breakpoints provided.",
18515e09c8c3SJim Ingham             "breakpoint name add <command-options> <breakpoint-id-list>"),
1852b9c1b51eSKate Stone         m_name_options(), m_option_group() {
1853b9c1b51eSKate Stone     // Create the first variant for the first (and only) argument for this
1854b9c1b51eSKate Stone     // command.
18555e09c8c3SJim Ingham     CommandArgumentEntry arg1;
18565e09c8c3SJim Ingham     CommandArgumentData id_arg;
18575e09c8c3SJim Ingham     id_arg.arg_type = eArgTypeBreakpointID;
18585e09c8c3SJim Ingham     id_arg.arg_repetition = eArgRepeatOptional;
18595e09c8c3SJim Ingham     arg1.push_back(id_arg);
18605e09c8c3SJim Ingham     m_arguments.push_back(arg1);
18615e09c8c3SJim Ingham 
18625e09c8c3SJim Ingham     m_option_group.Append(&m_name_options, LLDB_OPT_SET_1, LLDB_OPT_SET_ALL);
18635e09c8c3SJim Ingham     m_option_group.Finalize();
18645e09c8c3SJim Ingham   }
18655e09c8c3SJim Ingham 
18669e85e5a8SEugene Zelenko   ~CommandObjectBreakpointNameAdd() override = default;
18675e09c8c3SJim Ingham 
1868b9c1b51eSKate Stone   Options *GetOptions() override { return &m_option_group; }
18695e09c8c3SJim Ingham 
18705e09c8c3SJim Ingham protected:
1871b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
1872b9c1b51eSKate Stone     if (!m_name_options.m_name.OptionWasSet()) {
18735e09c8c3SJim Ingham       result.SetError("No name option provided.");
18745e09c8c3SJim Ingham       return false;
18755e09c8c3SJim Ingham     }
18765e09c8c3SJim Ingham 
1877b9c1b51eSKate Stone     Target *target =
1878b9c1b51eSKate Stone         GetSelectedOrDummyTarget(m_name_options.m_use_dummy.GetCurrentValue());
18795e09c8c3SJim Ingham 
1880b9c1b51eSKate Stone     if (target == nullptr) {
18815e09c8c3SJim Ingham       result.AppendError("Invalid target. No existing target or breakpoints.");
18825e09c8c3SJim Ingham       result.SetStatus(eReturnStatusFailed);
18835e09c8c3SJim Ingham       return false;
18845e09c8c3SJim Ingham     }
18855e09c8c3SJim Ingham 
1886bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
1887bb19a13cSSaleem Abdulrasool     target->GetBreakpointList().GetListMutex(lock);
18885e09c8c3SJim Ingham 
18895e09c8c3SJim Ingham     const BreakpointList &breakpoints = target->GetBreakpointList();
18905e09c8c3SJim Ingham 
18915e09c8c3SJim Ingham     size_t num_breakpoints = breakpoints.GetSize();
1892b9c1b51eSKate Stone     if (num_breakpoints == 0) {
18935e09c8c3SJim Ingham       result.SetError("No breakpoints, cannot add names.");
18945e09c8c3SJim Ingham       result.SetStatus(eReturnStatusFailed);
18955e09c8c3SJim Ingham       return false;
18965e09c8c3SJim Ingham     }
18975e09c8c3SJim Ingham 
18985e09c8c3SJim Ingham     // Particular breakpoint selected; disable that breakpoint.
18995e09c8c3SJim Ingham     BreakpointIDList valid_bp_ids;
1900b9c1b51eSKate Stone     CommandObjectMultiwordBreakpoint::VerifyBreakpointIDs(
1901b842f2ecSJim Ingham         command, target, result, &valid_bp_ids,
1902b842f2ecSJim Ingham         BreakpointName::Permissions::PermissionKinds::listPerm);
19035e09c8c3SJim Ingham 
1904b9c1b51eSKate Stone     if (result.Succeeded()) {
1905b9c1b51eSKate Stone       if (valid_bp_ids.GetSize() == 0) {
19065e09c8c3SJim Ingham         result.SetError("No breakpoints specified, cannot add names.");
19075e09c8c3SJim Ingham         result.SetStatus(eReturnStatusFailed);
19085e09c8c3SJim Ingham         return false;
19095e09c8c3SJim Ingham       }
19105e09c8c3SJim Ingham       size_t num_valid_ids = valid_bp_ids.GetSize();
1911b842f2ecSJim Ingham       const char *bp_name = m_name_options.m_name.GetCurrentValue();
1912b842f2ecSJim Ingham       Status error; // This error reports illegal names, but we've already
1913b842f2ecSJim Ingham                     // checked that, so we don't need to check it again here.
1914b9c1b51eSKate Stone       for (size_t index = 0; index < num_valid_ids; index++) {
1915b9c1b51eSKate Stone         lldb::break_id_t bp_id =
1916b9c1b51eSKate Stone             valid_bp_ids.GetBreakpointIDAtIndex(index).GetBreakpointID();
19175e09c8c3SJim Ingham         BreakpointSP bp_sp = breakpoints.FindBreakpointByID(bp_id);
1918b842f2ecSJim Ingham         target->AddNameToBreakpoint(bp_sp, bp_name, error);
19195e09c8c3SJim Ingham       }
19205e09c8c3SJim Ingham     }
19215e09c8c3SJim Ingham 
19225e09c8c3SJim Ingham     return true;
19235e09c8c3SJim Ingham   }
19245e09c8c3SJim Ingham 
19255e09c8c3SJim Ingham private:
19265e09c8c3SJim Ingham   BreakpointNameOptionGroup m_name_options;
19275e09c8c3SJim Ingham   OptionGroupOptions m_option_group;
19285e09c8c3SJim Ingham };
19295e09c8c3SJim Ingham 
1930b9c1b51eSKate Stone class CommandObjectBreakpointNameDelete : public CommandObjectParsed {
19315e09c8c3SJim Ingham public:
1932b9c1b51eSKate Stone   CommandObjectBreakpointNameDelete(CommandInterpreter &interpreter)
1933b9c1b51eSKate Stone       : CommandObjectParsed(
1934b9c1b51eSKate Stone             interpreter, "delete",
19355e09c8c3SJim Ingham             "Delete a name from the breakpoints provided.",
19365e09c8c3SJim Ingham             "breakpoint name delete <command-options> <breakpoint-id-list>"),
1937b9c1b51eSKate Stone         m_name_options(), m_option_group() {
1938b9c1b51eSKate Stone     // Create the first variant for the first (and only) argument for this
1939b9c1b51eSKate Stone     // command.
19405e09c8c3SJim Ingham     CommandArgumentEntry arg1;
19415e09c8c3SJim Ingham     CommandArgumentData id_arg;
19425e09c8c3SJim Ingham     id_arg.arg_type = eArgTypeBreakpointID;
19435e09c8c3SJim Ingham     id_arg.arg_repetition = eArgRepeatOptional;
19445e09c8c3SJim Ingham     arg1.push_back(id_arg);
19455e09c8c3SJim Ingham     m_arguments.push_back(arg1);
19465e09c8c3SJim Ingham 
19475e09c8c3SJim Ingham     m_option_group.Append(&m_name_options, LLDB_OPT_SET_1, LLDB_OPT_SET_ALL);
19485e09c8c3SJim Ingham     m_option_group.Finalize();
19495e09c8c3SJim Ingham   }
19505e09c8c3SJim Ingham 
19519e85e5a8SEugene Zelenko   ~CommandObjectBreakpointNameDelete() override = default;
19525e09c8c3SJim Ingham 
1953b9c1b51eSKate Stone   Options *GetOptions() override { return &m_option_group; }
19545e09c8c3SJim Ingham 
19555e09c8c3SJim Ingham protected:
1956b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
1957b9c1b51eSKate Stone     if (!m_name_options.m_name.OptionWasSet()) {
19585e09c8c3SJim Ingham       result.SetError("No name option provided.");
19595e09c8c3SJim Ingham       return false;
19605e09c8c3SJim Ingham     }
19615e09c8c3SJim Ingham 
1962b9c1b51eSKate Stone     Target *target =
1963b9c1b51eSKate Stone         GetSelectedOrDummyTarget(m_name_options.m_use_dummy.GetCurrentValue());
19645e09c8c3SJim Ingham 
1965b9c1b51eSKate Stone     if (target == nullptr) {
19665e09c8c3SJim Ingham       result.AppendError("Invalid target. No existing target or breakpoints.");
19675e09c8c3SJim Ingham       result.SetStatus(eReturnStatusFailed);
19685e09c8c3SJim Ingham       return false;
19695e09c8c3SJim Ingham     }
19705e09c8c3SJim Ingham 
1971bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
1972bb19a13cSSaleem Abdulrasool     target->GetBreakpointList().GetListMutex(lock);
19735e09c8c3SJim Ingham 
19745e09c8c3SJim Ingham     const BreakpointList &breakpoints = target->GetBreakpointList();
19755e09c8c3SJim Ingham 
19765e09c8c3SJim Ingham     size_t num_breakpoints = breakpoints.GetSize();
1977b9c1b51eSKate Stone     if (num_breakpoints == 0) {
19785e09c8c3SJim Ingham       result.SetError("No breakpoints, cannot delete names.");
19795e09c8c3SJim Ingham       result.SetStatus(eReturnStatusFailed);
19805e09c8c3SJim Ingham       return false;
19815e09c8c3SJim Ingham     }
19825e09c8c3SJim Ingham 
19835e09c8c3SJim Ingham     // Particular breakpoint selected; disable that breakpoint.
19845e09c8c3SJim Ingham     BreakpointIDList valid_bp_ids;
1985b9c1b51eSKate Stone     CommandObjectMultiwordBreakpoint::VerifyBreakpointIDs(
1986b842f2ecSJim Ingham         command, target, result, &valid_bp_ids,
1987b842f2ecSJim Ingham         BreakpointName::Permissions::PermissionKinds::deletePerm);
19885e09c8c3SJim Ingham 
1989b9c1b51eSKate Stone     if (result.Succeeded()) {
1990b9c1b51eSKate Stone       if (valid_bp_ids.GetSize() == 0) {
19915e09c8c3SJim Ingham         result.SetError("No breakpoints specified, cannot delete names.");
19925e09c8c3SJim Ingham         result.SetStatus(eReturnStatusFailed);
19935e09c8c3SJim Ingham         return false;
19945e09c8c3SJim Ingham       }
1995b842f2ecSJim Ingham       ConstString bp_name(m_name_options.m_name.GetCurrentValue());
19965e09c8c3SJim Ingham       size_t num_valid_ids = valid_bp_ids.GetSize();
1997b9c1b51eSKate Stone       for (size_t index = 0; index < num_valid_ids; index++) {
1998b9c1b51eSKate Stone         lldb::break_id_t bp_id =
1999b9c1b51eSKate Stone             valid_bp_ids.GetBreakpointIDAtIndex(index).GetBreakpointID();
20005e09c8c3SJim Ingham         BreakpointSP bp_sp = breakpoints.FindBreakpointByID(bp_id);
2001b842f2ecSJim Ingham         target->RemoveNameFromBreakpoint(bp_sp, bp_name);
20025e09c8c3SJim Ingham       }
20035e09c8c3SJim Ingham     }
20045e09c8c3SJim Ingham 
20055e09c8c3SJim Ingham     return true;
20065e09c8c3SJim Ingham   }
20075e09c8c3SJim Ingham 
20085e09c8c3SJim Ingham private:
20095e09c8c3SJim Ingham   BreakpointNameOptionGroup m_name_options;
20105e09c8c3SJim Ingham   OptionGroupOptions m_option_group;
20115e09c8c3SJim Ingham };
20125e09c8c3SJim Ingham 
2013b9c1b51eSKate Stone class CommandObjectBreakpointNameList : public CommandObjectParsed {
20145e09c8c3SJim Ingham public:
2015b9c1b51eSKate Stone   CommandObjectBreakpointNameList(CommandInterpreter &interpreter)
2016b9c1b51eSKate Stone       : CommandObjectParsed(interpreter, "list",
2017b842f2ecSJim Ingham                             "List either the names for a breakpoint or info "
2018b842f2ecSJim Ingham                             "about a given name.  With no arguments, lists all "
2019b842f2ecSJim Ingham                             "names",
20205e09c8c3SJim Ingham                             "breakpoint name list <command-options>"),
2021b9c1b51eSKate Stone         m_name_options(), m_option_group() {
2022b842f2ecSJim Ingham     m_option_group.Append(&m_name_options, LLDB_OPT_SET_3, LLDB_OPT_SET_ALL);
20235e09c8c3SJim Ingham     m_option_group.Finalize();
20245e09c8c3SJim Ingham   }
20255e09c8c3SJim Ingham 
20269e85e5a8SEugene Zelenko   ~CommandObjectBreakpointNameList() override = default;
20275e09c8c3SJim Ingham 
2028b9c1b51eSKate Stone   Options *GetOptions() override { return &m_option_group; }
20295e09c8c3SJim Ingham 
20305e09c8c3SJim Ingham protected:
2031b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
2032b9c1b51eSKate Stone     Target *target =
2033b9c1b51eSKate Stone         GetSelectedOrDummyTarget(m_name_options.m_use_dummy.GetCurrentValue());
20345e09c8c3SJim Ingham 
2035b9c1b51eSKate Stone     if (target == nullptr) {
20365e09c8c3SJim Ingham       result.AppendError("Invalid target. No existing target or breakpoints.");
20375e09c8c3SJim Ingham       result.SetStatus(eReturnStatusFailed);
20385e09c8c3SJim Ingham       return false;
20395e09c8c3SJim Ingham     }
20405e09c8c3SJim Ingham 
2041b842f2ecSJim Ingham 
2042b842f2ecSJim Ingham     std::vector<std::string> name_list;
2043b842f2ecSJim Ingham     if (command.empty()) {
2044b842f2ecSJim Ingham       target->GetBreakpointNames(name_list);
2045b842f2ecSJim Ingham     } else {
2046b842f2ecSJim Ingham       for (const Args::ArgEntry &arg : command)
2047b842f2ecSJim Ingham       {
2048b842f2ecSJim Ingham         name_list.push_back(arg.c_str());
2049b842f2ecSJim Ingham       }
2050b842f2ecSJim Ingham     }
2051b842f2ecSJim Ingham 
2052b842f2ecSJim Ingham     if (name_list.empty()) {
2053b842f2ecSJim Ingham       result.AppendMessage("No breakpoint names found.");
2054b842f2ecSJim Ingham     } else {
2055b842f2ecSJim Ingham       for (const std::string &name_str : name_list) {
2056b842f2ecSJim Ingham         const char *name = name_str.c_str();
2057b842f2ecSJim Ingham         // First print out the options for the name:
2058b842f2ecSJim Ingham         Status error;
2059b842f2ecSJim Ingham         BreakpointName *bp_name = target->FindBreakpointName(ConstString(name),
2060b842f2ecSJim Ingham                                                              false,
2061b842f2ecSJim Ingham                                                              error);
2062b842f2ecSJim Ingham         if (bp_name)
2063b842f2ecSJim Ingham         {
2064b842f2ecSJim Ingham           StreamString s;
2065b842f2ecSJim Ingham           result.AppendMessageWithFormat("Name: %s\n", name);
2066b842f2ecSJim Ingham           if (bp_name->GetDescription(&s, eDescriptionLevelFull))
2067b842f2ecSJim Ingham           {
2068b842f2ecSJim Ingham             result.AppendMessage(s.GetString());
2069b842f2ecSJim Ingham           }
2070b842f2ecSJim Ingham 
2071bb19a13cSSaleem Abdulrasool           std::unique_lock<std::recursive_mutex> lock;
2072bb19a13cSSaleem Abdulrasool           target->GetBreakpointList().GetListMutex(lock);
20735e09c8c3SJim Ingham 
20745e09c8c3SJim Ingham           BreakpointList &breakpoints = target->GetBreakpointList();
2075b842f2ecSJim Ingham           bool any_set = false;
2076b9c1b51eSKate Stone           for (BreakpointSP bp_sp : breakpoints.Breakpoints()) {
2077b9c1b51eSKate Stone             if (bp_sp->MatchesName(name)) {
20785e09c8c3SJim Ingham               StreamString s;
2079b842f2ecSJim Ingham               any_set = true;
20805e09c8c3SJim Ingham               bp_sp->GetDescription(&s, eDescriptionLevelBrief);
20815e09c8c3SJim Ingham               s.EOL();
2082c156427dSZachary Turner               result.AppendMessage(s.GetString());
20835e09c8c3SJim Ingham             }
20845e09c8c3SJim Ingham           }
2085b842f2ecSJim Ingham           if (!any_set)
2086b842f2ecSJim Ingham             result.AppendMessage("No breakpoints using this name.");
2087b9c1b51eSKate Stone         } else {
2088b842f2ecSJim Ingham           result.AppendMessageWithFormat("Name: %s not found.\n", name);
20895e09c8c3SJim Ingham         }
2090b842f2ecSJim Ingham       }
20915e09c8c3SJim Ingham     }
20925e09c8c3SJim Ingham     return true;
20935e09c8c3SJim Ingham   }
20945e09c8c3SJim Ingham 
20955e09c8c3SJim Ingham private:
20965e09c8c3SJim Ingham   BreakpointNameOptionGroup m_name_options;
20975e09c8c3SJim Ingham   OptionGroupOptions m_option_group;
20985e09c8c3SJim Ingham };
20995e09c8c3SJim Ingham 
2100e14dc268SJim Ingham // CommandObjectBreakpointName
2101b9c1b51eSKate Stone class CommandObjectBreakpointName : public CommandObjectMultiword {
21025e09c8c3SJim Ingham public:
21037428a18cSKate Stone   CommandObjectBreakpointName(CommandInterpreter &interpreter)
2104b9c1b51eSKate Stone       : CommandObjectMultiword(
2105b9c1b51eSKate Stone             interpreter, "name", "Commands to manage name tags for breakpoints",
2106b9c1b51eSKate Stone             "breakpoint name <subcommand> [<command-options>]") {
2107b9c1b51eSKate Stone     CommandObjectSP add_command_object(
2108b9c1b51eSKate Stone         new CommandObjectBreakpointNameAdd(interpreter));
2109b9c1b51eSKate Stone     CommandObjectSP delete_command_object(
2110b9c1b51eSKate Stone         new CommandObjectBreakpointNameDelete(interpreter));
2111b9c1b51eSKate Stone     CommandObjectSP list_command_object(
2112b9c1b51eSKate Stone         new CommandObjectBreakpointNameList(interpreter));
2113b842f2ecSJim Ingham     CommandObjectSP configure_command_object(
2114b842f2ecSJim Ingham         new CommandObjectBreakpointNameConfigure(interpreter));
21155e09c8c3SJim Ingham 
21165e09c8c3SJim Ingham     LoadSubCommand("add", add_command_object);
21175e09c8c3SJim Ingham     LoadSubCommand("delete", delete_command_object);
21185e09c8c3SJim Ingham     LoadSubCommand("list", list_command_object);
2119b842f2ecSJim Ingham     LoadSubCommand("configure", configure_command_object);
21205e09c8c3SJim Ingham   }
21215e09c8c3SJim Ingham 
21229e85e5a8SEugene Zelenko   ~CommandObjectBreakpointName() override = default;
21235e09c8c3SJim Ingham };
21245e09c8c3SJim Ingham 
2125e14dc268SJim Ingham // CommandObjectBreakpointRead
21263acdf385SJim Ingham #pragma mark Read::CommandOptions
2127f94668e3SRaphael Isemann #define LLDB_OPTIONS_breakpoint_read
2128f94668e3SRaphael Isemann #include "CommandOptions.inc"
21291f0f5b5bSZachary Turner 
21301f0f5b5bSZachary Turner #pragma mark Read
2131e14dc268SJim Ingham 
2132e14dc268SJim Ingham class CommandObjectBreakpointRead : public CommandObjectParsed {
2133e14dc268SJim Ingham public:
2134e14dc268SJim Ingham   CommandObjectBreakpointRead(CommandInterpreter &interpreter)
2135e14dc268SJim Ingham       : CommandObjectParsed(interpreter, "breakpoint read",
2136e14dc268SJim Ingham                             "Read and set the breakpoints previously saved to "
2137e14dc268SJim Ingham                             "a file with \"breakpoint write\".  ",
2138e14dc268SJim Ingham                             nullptr),
2139e14dc268SJim Ingham         m_options() {
2140e14dc268SJim Ingham     CommandArgumentEntry arg;
2141e14dc268SJim Ingham     CommandObject::AddIDsArgumentData(arg, eArgTypeBreakpointID,
2142e14dc268SJim Ingham                                       eArgTypeBreakpointIDRange);
2143e14dc268SJim Ingham     // Add the entry for the first argument for this command to the object's
2144e14dc268SJim Ingham     // arguments vector.
2145e14dc268SJim Ingham     m_arguments.push_back(arg);
2146e14dc268SJim Ingham   }
2147e14dc268SJim Ingham 
2148e14dc268SJim Ingham   ~CommandObjectBreakpointRead() override = default;
2149e14dc268SJim Ingham 
2150e14dc268SJim Ingham   Options *GetOptions() override { return &m_options; }
2151e14dc268SJim Ingham 
2152e14dc268SJim Ingham   class CommandOptions : public Options {
2153e14dc268SJim Ingham   public:
2154e14dc268SJim Ingham     CommandOptions() : Options() {}
2155e14dc268SJim Ingham 
2156e14dc268SJim Ingham     ~CommandOptions() override = default;
2157e14dc268SJim Ingham 
215897206d57SZachary Turner     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
2159e14dc268SJim Ingham                           ExecutionContext *execution_context) override {
216097206d57SZachary Turner       Status error;
2161e14dc268SJim Ingham       const int short_option = m_getopt_table[option_idx].val;
2162e14dc268SJim Ingham 
2163e14dc268SJim Ingham       switch (short_option) {
2164e14dc268SJim Ingham       case 'f':
2165e14dc268SJim Ingham         m_filename.assign(option_arg);
2166e14dc268SJim Ingham         break;
21673acdf385SJim Ingham       case 'N': {
216897206d57SZachary Turner         Status name_error;
21693acdf385SJim Ingham         if (!BreakpointID::StringIsBreakpointName(llvm::StringRef(option_arg),
21703acdf385SJim Ingham                                                   name_error)) {
21713acdf385SJim Ingham           error.SetErrorStringWithFormat("Invalid breakpoint name: %s",
21723acdf385SJim Ingham                                          name_error.AsCString());
21733acdf385SJim Ingham         }
21743acdf385SJim Ingham         m_names.push_back(option_arg);
21753acdf385SJim Ingham         break;
21763acdf385SJim Ingham       }
2177e14dc268SJim Ingham       default:
2178e14dc268SJim Ingham         error.SetErrorStringWithFormat("unrecognized option '%c'",
2179e14dc268SJim Ingham                                        short_option);
2180e14dc268SJim Ingham         break;
2181e14dc268SJim Ingham       }
2182e14dc268SJim Ingham 
2183e14dc268SJim Ingham       return error;
2184e14dc268SJim Ingham     }
2185e14dc268SJim Ingham 
2186e14dc268SJim Ingham     void OptionParsingStarting(ExecutionContext *execution_context) override {
2187e14dc268SJim Ingham       m_filename.clear();
21883acdf385SJim Ingham       m_names.clear();
2189e14dc268SJim Ingham     }
2190e14dc268SJim Ingham 
21911f0f5b5bSZachary Turner     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
219270602439SZachary Turner       return llvm::makeArrayRef(g_breakpoint_read_options);
21931f0f5b5bSZachary Turner     }
2194e14dc268SJim Ingham 
2195e14dc268SJim Ingham     // Instance variables to hold the values for command options.
2196e14dc268SJim Ingham 
2197e14dc268SJim Ingham     std::string m_filename;
21983acdf385SJim Ingham     std::vector<std::string> m_names;
2199e14dc268SJim Ingham   };
2200e14dc268SJim Ingham 
2201e14dc268SJim Ingham protected:
2202e14dc268SJim Ingham   bool DoExecute(Args &command, CommandReturnObject &result) override {
2203e14dc268SJim Ingham     Target *target = GetSelectedOrDummyTarget();
2204e14dc268SJim Ingham     if (target == nullptr) {
2205e14dc268SJim Ingham       result.AppendError("Invalid target.  No existing target or breakpoints.");
2206e14dc268SJim Ingham       result.SetStatus(eReturnStatusFailed);
2207e14dc268SJim Ingham       return false;
2208e14dc268SJim Ingham     }
2209e14dc268SJim Ingham 
22103acdf385SJim Ingham     std::unique_lock<std::recursive_mutex> lock;
22113acdf385SJim Ingham     target->GetBreakpointList().GetListMutex(lock);
22123acdf385SJim Ingham 
22138f3be7a3SJonas Devlieghere     FileSpec input_spec(m_options.m_filename);
22148f3be7a3SJonas Devlieghere     FileSystem::Instance().Resolve(input_spec);
221501f16664SJim Ingham     BreakpointIDList new_bps;
221697206d57SZachary Turner     Status error = target->CreateBreakpointsFromFile(
221797206d57SZachary Turner         input_spec, m_options.m_names, new_bps);
2218e14dc268SJim Ingham 
2219e14dc268SJim Ingham     if (!error.Success()) {
222001f16664SJim Ingham       result.AppendError(error.AsCString());
2221e14dc268SJim Ingham       result.SetStatus(eReturnStatusFailed);
222201f16664SJim Ingham       return false;
2223e14dc268SJim Ingham     }
22243acdf385SJim Ingham 
22253acdf385SJim Ingham     Stream &output_stream = result.GetOutputStream();
22263acdf385SJim Ingham 
22273acdf385SJim Ingham     size_t num_breakpoints = new_bps.GetSize();
22283acdf385SJim Ingham     if (num_breakpoints == 0) {
22293acdf385SJim Ingham       result.AppendMessage("No breakpoints added.");
22303acdf385SJim Ingham     } else {
22313acdf385SJim Ingham       // No breakpoint selected; show info about all currently set breakpoints.
22323acdf385SJim Ingham       result.AppendMessage("New breakpoints:");
22333acdf385SJim Ingham       for (size_t i = 0; i < num_breakpoints; ++i) {
22343acdf385SJim Ingham         BreakpointID bp_id = new_bps.GetBreakpointIDAtIndex(i);
22353acdf385SJim Ingham         Breakpoint *bp = target->GetBreakpointList()
22363acdf385SJim Ingham                              .FindBreakpointByID(bp_id.GetBreakpointID())
22373acdf385SJim Ingham                              .get();
22383acdf385SJim Ingham         if (bp)
22393acdf385SJim Ingham           bp->GetDescription(&output_stream, lldb::eDescriptionLevelInitial,
22403acdf385SJim Ingham                              false);
22413acdf385SJim Ingham       }
22423acdf385SJim Ingham     }
2243e14dc268SJim Ingham     return result.Succeeded();
2244e14dc268SJim Ingham   }
2245e14dc268SJim Ingham 
2246e14dc268SJim Ingham private:
2247e14dc268SJim Ingham   CommandOptions m_options;
2248e14dc268SJim Ingham };
2249e14dc268SJim Ingham 
2250e14dc268SJim Ingham // CommandObjectBreakpointWrite
22511f0f5b5bSZachary Turner #pragma mark Write::CommandOptions
2252f94668e3SRaphael Isemann #define LLDB_OPTIONS_breakpoint_write
2253f94668e3SRaphael Isemann #include "CommandOptions.inc"
22541f0f5b5bSZachary Turner 
22551f0f5b5bSZachary Turner #pragma mark Write
2256e14dc268SJim Ingham class CommandObjectBreakpointWrite : public CommandObjectParsed {
2257e14dc268SJim Ingham public:
2258e14dc268SJim Ingham   CommandObjectBreakpointWrite(CommandInterpreter &interpreter)
2259e14dc268SJim Ingham       : CommandObjectParsed(interpreter, "breakpoint write",
2260e14dc268SJim Ingham                             "Write the breakpoints listed to a file that can "
2261e14dc268SJim Ingham                             "be read in with \"breakpoint read\".  "
2262e14dc268SJim Ingham                             "If given no arguments, writes all breakpoints.",
2263e14dc268SJim Ingham                             nullptr),
2264e14dc268SJim Ingham         m_options() {
2265e14dc268SJim Ingham     CommandArgumentEntry arg;
2266e14dc268SJim Ingham     CommandObject::AddIDsArgumentData(arg, eArgTypeBreakpointID,
2267e14dc268SJim Ingham                                       eArgTypeBreakpointIDRange);
2268e14dc268SJim Ingham     // Add the entry for the first argument for this command to the object's
2269e14dc268SJim Ingham     // arguments vector.
2270e14dc268SJim Ingham     m_arguments.push_back(arg);
2271e14dc268SJim Ingham   }
2272e14dc268SJim Ingham 
2273e14dc268SJim Ingham   ~CommandObjectBreakpointWrite() override = default;
2274e14dc268SJim Ingham 
2275e14dc268SJim Ingham   Options *GetOptions() override { return &m_options; }
2276e14dc268SJim Ingham 
2277e14dc268SJim Ingham   class CommandOptions : public Options {
2278e14dc268SJim Ingham   public:
2279e14dc268SJim Ingham     CommandOptions() : Options() {}
2280e14dc268SJim Ingham 
2281e14dc268SJim Ingham     ~CommandOptions() override = default;
2282e14dc268SJim Ingham 
228397206d57SZachary Turner     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
2284e14dc268SJim Ingham                           ExecutionContext *execution_context) override {
228597206d57SZachary Turner       Status error;
2286e14dc268SJim Ingham       const int short_option = m_getopt_table[option_idx].val;
2287e14dc268SJim Ingham 
2288e14dc268SJim Ingham       switch (short_option) {
2289e14dc268SJim Ingham       case 'f':
2290e14dc268SJim Ingham         m_filename.assign(option_arg);
2291e14dc268SJim Ingham         break;
22922d3628e1SJim Ingham       case 'a':
22932d3628e1SJim Ingham         m_append = true;
22942d3628e1SJim Ingham         break;
2295e14dc268SJim Ingham       default:
2296e14dc268SJim Ingham         error.SetErrorStringWithFormat("unrecognized option '%c'",
2297e14dc268SJim Ingham                                        short_option);
2298e14dc268SJim Ingham         break;
2299e14dc268SJim Ingham       }
2300e14dc268SJim Ingham 
2301e14dc268SJim Ingham       return error;
2302e14dc268SJim Ingham     }
2303e14dc268SJim Ingham 
2304e14dc268SJim Ingham     void OptionParsingStarting(ExecutionContext *execution_context) override {
2305e14dc268SJim Ingham       m_filename.clear();
23062d3628e1SJim Ingham       m_append = false;
2307e14dc268SJim Ingham     }
2308e14dc268SJim Ingham 
23091f0f5b5bSZachary Turner     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
231070602439SZachary Turner       return llvm::makeArrayRef(g_breakpoint_write_options);
23111f0f5b5bSZachary Turner     }
2312e14dc268SJim Ingham 
2313e14dc268SJim Ingham     // Instance variables to hold the values for command options.
2314e14dc268SJim Ingham 
2315e14dc268SJim Ingham     std::string m_filename;
23162d3628e1SJim Ingham     bool m_append = false;
2317e14dc268SJim Ingham   };
2318e14dc268SJim Ingham 
2319e14dc268SJim Ingham protected:
2320e14dc268SJim Ingham   bool DoExecute(Args &command, CommandReturnObject &result) override {
2321e14dc268SJim Ingham     Target *target = GetSelectedOrDummyTarget();
2322e14dc268SJim Ingham     if (target == nullptr) {
2323e14dc268SJim Ingham       result.AppendError("Invalid target.  No existing target or breakpoints.");
2324e14dc268SJim Ingham       result.SetStatus(eReturnStatusFailed);
2325e14dc268SJim Ingham       return false;
2326e14dc268SJim Ingham     }
2327e14dc268SJim Ingham 
2328e14dc268SJim Ingham     std::unique_lock<std::recursive_mutex> lock;
2329e14dc268SJim Ingham     target->GetBreakpointList().GetListMutex(lock);
2330e14dc268SJim Ingham 
2331e14dc268SJim Ingham     BreakpointIDList valid_bp_ids;
233211eb9c64SZachary Turner     if (!command.empty()) {
2333e14dc268SJim Ingham       CommandObjectMultiwordBreakpoint::VerifyBreakpointIDs(
2334b842f2ecSJim Ingham           command, target, result, &valid_bp_ids,
2335b842f2ecSJim Ingham           BreakpointName::Permissions::PermissionKinds::listPerm);
2336e14dc268SJim Ingham 
233701f16664SJim Ingham       if (!result.Succeeded()) {
2338e14dc268SJim Ingham         result.SetStatus(eReturnStatusFailed);
2339e14dc268SJim Ingham         return false;
2340e14dc268SJim Ingham       }
2341e14dc268SJim Ingham     }
23428f3be7a3SJonas Devlieghere     FileSpec file_spec(m_options.m_filename);
23438f3be7a3SJonas Devlieghere     FileSystem::Instance().Resolve(file_spec);
23448f3be7a3SJonas Devlieghere     Status error = target->SerializeBreakpointsToFile(file_spec, valid_bp_ids,
23458f3be7a3SJonas Devlieghere                                                       m_options.m_append);
234601f16664SJim Ingham     if (!error.Success()) {
234701f16664SJim Ingham       result.AppendErrorWithFormat("error serializing breakpoints: %s.",
234801f16664SJim Ingham                                    error.AsCString());
234901f16664SJim Ingham       result.SetStatus(eReturnStatusFailed);
2350e14dc268SJim Ingham     }
2351e14dc268SJim Ingham     return result.Succeeded();
2352e14dc268SJim Ingham   }
2353e14dc268SJim Ingham 
2354e14dc268SJim Ingham private:
2355e14dc268SJim Ingham   CommandOptions m_options;
2356e14dc268SJim Ingham };
2357e14dc268SJim Ingham 
235830fdc8d8SChris Lattner // CommandObjectMultiwordBreakpoint
2359ae1c4cf5SJim Ingham #pragma mark MultiwordBreakpoint
236030fdc8d8SChris Lattner 
2361b9c1b51eSKate Stone CommandObjectMultiwordBreakpoint::CommandObjectMultiwordBreakpoint(
2362b9c1b51eSKate Stone     CommandInterpreter &interpreter)
2363b9c1b51eSKate Stone     : CommandObjectMultiword(
2364b9c1b51eSKate Stone           interpreter, "breakpoint",
23657428a18cSKate Stone           "Commands for operating on breakpoints (see 'help b' for shorthand.)",
2366b9c1b51eSKate Stone           "breakpoint <subcommand> [<command-options>]") {
2367b9c1b51eSKate Stone   CommandObjectSP list_command_object(
2368b9c1b51eSKate Stone       new CommandObjectBreakpointList(interpreter));
2369b9c1b51eSKate Stone   CommandObjectSP enable_command_object(
2370b9c1b51eSKate Stone       new CommandObjectBreakpointEnable(interpreter));
2371b9c1b51eSKate Stone   CommandObjectSP disable_command_object(
2372b9c1b51eSKate Stone       new CommandObjectBreakpointDisable(interpreter));
2373b9c1b51eSKate Stone   CommandObjectSP clear_command_object(
2374b9c1b51eSKate Stone       new CommandObjectBreakpointClear(interpreter));
2375b9c1b51eSKate Stone   CommandObjectSP delete_command_object(
2376b9c1b51eSKate Stone       new CommandObjectBreakpointDelete(interpreter));
2377b9c1b51eSKate Stone   CommandObjectSP set_command_object(
2378b9c1b51eSKate Stone       new CommandObjectBreakpointSet(interpreter));
2379b9c1b51eSKate Stone   CommandObjectSP command_command_object(
2380b9c1b51eSKate Stone       new CommandObjectBreakpointCommand(interpreter));
2381b9c1b51eSKate Stone   CommandObjectSP modify_command_object(
2382b9c1b51eSKate Stone       new CommandObjectBreakpointModify(interpreter));
2383b9c1b51eSKate Stone   CommandObjectSP name_command_object(
2384b9c1b51eSKate Stone       new CommandObjectBreakpointName(interpreter));
2385e14dc268SJim Ingham   CommandObjectSP write_command_object(
2386e14dc268SJim Ingham       new CommandObjectBreakpointWrite(interpreter));
2387e14dc268SJim Ingham   CommandObjectSP read_command_object(
2388e14dc268SJim Ingham       new CommandObjectBreakpointRead(interpreter));
238930fdc8d8SChris Lattner 
2390b7234e40SJohnny Chen   list_command_object->SetCommandName("breakpoint list");
239130fdc8d8SChris Lattner   enable_command_object->SetCommandName("breakpoint enable");
239230fdc8d8SChris Lattner   disable_command_object->SetCommandName("breakpoint disable");
2393b7234e40SJohnny Chen   clear_command_object->SetCommandName("breakpoint clear");
2394b7234e40SJohnny Chen   delete_command_object->SetCommandName("breakpoint delete");
2395ae1c4cf5SJim Ingham   set_command_object->SetCommandName("breakpoint set");
2396b7234e40SJohnny Chen   command_command_object->SetCommandName("breakpoint command");
2397b7234e40SJohnny Chen   modify_command_object->SetCommandName("breakpoint modify");
23985e09c8c3SJim Ingham   name_command_object->SetCommandName("breakpoint name");
2399e14dc268SJim Ingham   write_command_object->SetCommandName("breakpoint write");
2400e14dc268SJim Ingham   read_command_object->SetCommandName("breakpoint read");
240130fdc8d8SChris Lattner 
240223f59509SGreg Clayton   LoadSubCommand("list", list_command_object);
240323f59509SGreg Clayton   LoadSubCommand("enable", enable_command_object);
240423f59509SGreg Clayton   LoadSubCommand("disable", disable_command_object);
240523f59509SGreg Clayton   LoadSubCommand("clear", clear_command_object);
240623f59509SGreg Clayton   LoadSubCommand("delete", delete_command_object);
240723f59509SGreg Clayton   LoadSubCommand("set", set_command_object);
240823f59509SGreg Clayton   LoadSubCommand("command", command_command_object);
240923f59509SGreg Clayton   LoadSubCommand("modify", modify_command_object);
24105e09c8c3SJim Ingham   LoadSubCommand("name", name_command_object);
2411e14dc268SJim Ingham   LoadSubCommand("write", write_command_object);
2412e14dc268SJim Ingham   LoadSubCommand("read", read_command_object);
241330fdc8d8SChris Lattner }
241430fdc8d8SChris Lattner 
24159e85e5a8SEugene Zelenko CommandObjectMultiwordBreakpoint::~CommandObjectMultiwordBreakpoint() = default;
241630fdc8d8SChris Lattner 
2417b9c1b51eSKate Stone void CommandObjectMultiwordBreakpoint::VerifyIDs(Args &args, Target *target,
24185e09c8c3SJim Ingham                                                  bool allow_locations,
24195e09c8c3SJim Ingham                                                  CommandReturnObject &result,
2420b842f2ecSJim Ingham                                                  BreakpointIDList *valid_ids,
2421b842f2ecSJim Ingham                                                  BreakpointName::Permissions
2422b842f2ecSJim Ingham                                                      ::PermissionKinds
2423b842f2ecSJim Ingham                                                      purpose) {
242430fdc8d8SChris Lattner   // args can be strings representing 1). integers (for breakpoint ids)
2425b9c1b51eSKate Stone   //                                  2). the full breakpoint & location
2426b9c1b51eSKate Stone   //                                  canonical representation
2427b9c1b51eSKate Stone   //                                  3). the word "to" or a hyphen,
2428b9c1b51eSKate Stone   //                                  representing a range (in which case there
2429b9c1b51eSKate Stone   //                                      had *better* be an entry both before &
2430b9c1b51eSKate Stone   //                                      after of one of the first two types.
24315e09c8c3SJim Ingham   //                                  4). A breakpoint name
2432b9c1b51eSKate Stone   // If args is empty, we will use the last created breakpoint (if there is
2433b9c1b51eSKate Stone   // one.)
243430fdc8d8SChris Lattner 
243530fdc8d8SChris Lattner   Args temp_args;
243630fdc8d8SChris Lattner 
243711eb9c64SZachary Turner   if (args.empty()) {
2438b9c1b51eSKate Stone     if (target->GetLastCreatedBreakpoint()) {
2439b9c1b51eSKate Stone       valid_ids->AddBreakpointID(BreakpointID(
2440b9c1b51eSKate Stone           target->GetLastCreatedBreakpoint()->GetID(), LLDB_INVALID_BREAK_ID));
244136f3b369SJim Ingham       result.SetStatus(eReturnStatusSuccessFinishNoResult);
2442b9c1b51eSKate Stone     } else {
2443b9c1b51eSKate Stone       result.AppendError(
2444b9c1b51eSKate Stone           "No breakpoint specified and no last created breakpoint.");
244536f3b369SJim Ingham       result.SetStatus(eReturnStatusFailed);
244636f3b369SJim Ingham     }
244736f3b369SJim Ingham     return;
244836f3b369SJim Ingham   }
244936f3b369SJim Ingham 
2450b9c1b51eSKate Stone   // Create a new Args variable to use; copy any non-breakpoint-id-ranges stuff
245105097246SAdrian Prantl   // directly from the old ARGS to the new TEMP_ARGS.  Do not copy breakpoint
245205097246SAdrian Prantl   // id range strings over; instead generate a list of strings for all the
245305097246SAdrian Prantl   // breakpoint ids in the range, and shove all of those breakpoint id strings
245405097246SAdrian Prantl   // into TEMP_ARGS.
245530fdc8d8SChris Lattner 
2456b9c1b51eSKate Stone   BreakpointIDList::FindAndReplaceIDRanges(args, target, allow_locations,
2457b842f2ecSJim Ingham                                            purpose, result, temp_args);
245830fdc8d8SChris Lattner 
2459b9c1b51eSKate Stone   // NOW, convert the list of breakpoint id strings in TEMP_ARGS into an actual
2460b9c1b51eSKate Stone   // BreakpointIDList:
246130fdc8d8SChris Lattner 
246216662f3cSPavel Labath   valid_ids->InsertStringArray(temp_args.GetArgumentArrayRef(), result);
246330fdc8d8SChris Lattner 
246405097246SAdrian Prantl   // At this point,  all of the breakpoint ids that the user passed in have
246505097246SAdrian Prantl   // been converted to breakpoint IDs and put into valid_ids.
246630fdc8d8SChris Lattner 
2467b9c1b51eSKate Stone   if (result.Succeeded()) {
2468b9c1b51eSKate Stone     // Now that we've converted everything from args into a list of breakpoint
246905097246SAdrian Prantl     // ids, go through our tentative list of breakpoint id's and verify that
247005097246SAdrian Prantl     // they correspond to valid/currently set breakpoints.
247130fdc8d8SChris Lattner 
2472c982c768SGreg Clayton     const size_t count = valid_ids->GetSize();
2473b9c1b51eSKate Stone     for (size_t i = 0; i < count; ++i) {
247430fdc8d8SChris Lattner       BreakpointID cur_bp_id = valid_ids->GetBreakpointIDAtIndex(i);
2475b9c1b51eSKate Stone       Breakpoint *breakpoint =
2476b9c1b51eSKate Stone           target->GetBreakpointByID(cur_bp_id.GetBreakpointID()).get();
2477b9c1b51eSKate Stone       if (breakpoint != nullptr) {
2478c7bece56SGreg Clayton         const size_t num_locations = breakpoint->GetNumLocations();
2479b9c1b51eSKate Stone         if (static_cast<size_t>(cur_bp_id.GetLocationID()) > num_locations) {
248030fdc8d8SChris Lattner           StreamString id_str;
2481b9c1b51eSKate Stone           BreakpointID::GetCanonicalReference(
2482b9c1b51eSKate Stone               &id_str, cur_bp_id.GetBreakpointID(), cur_bp_id.GetLocationID());
2483c982c768SGreg Clayton           i = valid_ids->GetSize() + 1;
2484b9c1b51eSKate Stone           result.AppendErrorWithFormat(
2485b9c1b51eSKate Stone               "'%s' is not a currently valid breakpoint/location id.\n",
248630fdc8d8SChris Lattner               id_str.GetData());
248730fdc8d8SChris Lattner           result.SetStatus(eReturnStatusFailed);
248830fdc8d8SChris Lattner         }
2489b9c1b51eSKate Stone       } else {
2490c982c768SGreg Clayton         i = valid_ids->GetSize() + 1;
2491b9c1b51eSKate Stone         result.AppendErrorWithFormat(
2492b9c1b51eSKate Stone             "'%d' is not a currently valid breakpoint ID.\n",
24937428a18cSKate Stone             cur_bp_id.GetBreakpointID());
249430fdc8d8SChris Lattner         result.SetStatus(eReturnStatusFailed);
249530fdc8d8SChris Lattner       }
249630fdc8d8SChris Lattner     }
249730fdc8d8SChris Lattner   }
249830fdc8d8SChris Lattner }
2499