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:
144*36162014SRaphael Isemann       llvm_unreachable("Unimplemented option");
145b842f2ecSJim Ingham     }
146b842f2ecSJim Ingham 
147b842f2ecSJim Ingham     return error;
148b842f2ecSJim Ingham   }
149b842f2ecSJim Ingham 
150b842f2ecSJim Ingham   void OptionParsingStarting(ExecutionContext *execution_context) override {
151b842f2ecSJim Ingham     m_bp_opts.Clear();
152b842f2ecSJim Ingham     m_commands.clear();
153b842f2ecSJim Ingham   }
154b842f2ecSJim Ingham 
155b842f2ecSJim Ingham   Status OptionParsingFinished(ExecutionContext *execution_context) override {
156b842f2ecSJim Ingham     if (!m_commands.empty())
157b842f2ecSJim Ingham     {
158b842f2ecSJim Ingham       if (!m_commands.empty())
159b842f2ecSJim Ingham       {
160a8f3ae7cSJonas Devlieghere           auto cmd_data = std::make_unique<BreakpointOptions::CommandData>();
161b842f2ecSJim Ingham 
162b842f2ecSJim Ingham           for (std::string &str : m_commands)
163b842f2ecSJim Ingham             cmd_data->user_source.AppendString(str);
164b842f2ecSJim Ingham 
165b842f2ecSJim Ingham           cmd_data->stop_on_error = true;
166b842f2ecSJim Ingham           m_bp_opts.SetCommandDataCallback(cmd_data);
167b842f2ecSJim Ingham       }
168b842f2ecSJim Ingham     }
169b842f2ecSJim Ingham     return Status();
170b842f2ecSJim Ingham   }
171b842f2ecSJim Ingham 
172b842f2ecSJim Ingham   const BreakpointOptions &GetBreakpointOptions()
173b842f2ecSJim Ingham   {
174b842f2ecSJim Ingham     return m_bp_opts;
175b842f2ecSJim Ingham   }
176b842f2ecSJim Ingham 
177b842f2ecSJim Ingham   std::vector<std::string> m_commands;
178b842f2ecSJim Ingham   BreakpointOptions m_bp_opts;
179b842f2ecSJim Ingham 
180b842f2ecSJim Ingham };
181bd68a052SRaphael Isemann 
182f94668e3SRaphael Isemann #define LLDB_OPTIONS_breakpoint_dummy
183f94668e3SRaphael Isemann #include "CommandOptions.inc"
184b842f2ecSJim Ingham 
185b842f2ecSJim Ingham class BreakpointDummyOptionGroup : public OptionGroup
186b842f2ecSJim Ingham {
187b842f2ecSJim Ingham public:
188b842f2ecSJim Ingham   BreakpointDummyOptionGroup() :
189b842f2ecSJim Ingham           OptionGroup() {}
190b842f2ecSJim Ingham 
191b842f2ecSJim Ingham   ~BreakpointDummyOptionGroup() override = default;
192b842f2ecSJim Ingham 
193b842f2ecSJim Ingham   llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
194b842f2ecSJim Ingham     return llvm::makeArrayRef(g_breakpoint_dummy_options);
195b842f2ecSJim Ingham   }
196b842f2ecSJim Ingham 
197b842f2ecSJim Ingham   Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
198b842f2ecSJim Ingham                           ExecutionContext *execution_context) override {
199b842f2ecSJim Ingham     Status error;
200b842f2ecSJim Ingham     const int short_option = g_breakpoint_modify_options[option_idx].short_option;
201b842f2ecSJim Ingham 
202b842f2ecSJim Ingham     switch (short_option) {
203b842f2ecSJim Ingham       case 'D':
204b842f2ecSJim Ingham         m_use_dummy = true;
205b842f2ecSJim Ingham         break;
206b842f2ecSJim Ingham     default:
207*36162014SRaphael Isemann       llvm_unreachable("Unimplemented option");
208b842f2ecSJim Ingham     }
209b842f2ecSJim Ingham 
210b842f2ecSJim Ingham     return error;
211b842f2ecSJim Ingham   }
212b842f2ecSJim Ingham 
213b842f2ecSJim Ingham   void OptionParsingStarting(ExecutionContext *execution_context) override {
214b842f2ecSJim Ingham     m_use_dummy = false;
215b842f2ecSJim Ingham   }
216b842f2ecSJim Ingham 
217b842f2ecSJim Ingham   bool m_use_dummy;
218b842f2ecSJim Ingham 
219b842f2ecSJim Ingham };
220b842f2ecSJim Ingham 
221f94668e3SRaphael Isemann #define LLDB_OPTIONS_breakpoint_set
222f94668e3SRaphael Isemann #include "CommandOptions.inc"
2231f0f5b5bSZachary Turner 
2245a988416SJim Ingham // CommandObjectBreakpointSet
22530fdc8d8SChris Lattner 
226b9c1b51eSKate Stone class CommandObjectBreakpointSet : public CommandObjectParsed {
2275a988416SJim Ingham public:
228efe8e7e3SFangrui Song   enum BreakpointSetType {
2295a988416SJim Ingham     eSetTypeInvalid,
2305a988416SJim Ingham     eSetTypeFileAndLine,
2315a988416SJim Ingham     eSetTypeAddress,
2325a988416SJim Ingham     eSetTypeFunctionName,
2335a988416SJim Ingham     eSetTypeFunctionRegexp,
2345a988416SJim Ingham     eSetTypeSourceRegexp,
2353815e702SJim Ingham     eSetTypeException,
2363815e702SJim Ingham     eSetTypeScripted,
237efe8e7e3SFangrui Song   };
2385a988416SJim Ingham 
239b9c1b51eSKate Stone   CommandObjectBreakpointSet(CommandInterpreter &interpreter)
240b9c1b51eSKate Stone       : CommandObjectParsed(
241b9c1b51eSKate Stone             interpreter, "breakpoint set",
2425a988416SJim Ingham             "Sets a breakpoint or set of breakpoints in the executable.",
2435a988416SJim Ingham             "breakpoint set <cmd-options>"),
244b842f2ecSJim Ingham         m_bp_opts(), m_options() {
245b842f2ecSJim Ingham           // We're picking up all the normal options, commands and disable.
246b842f2ecSJim Ingham           m_all_options.Append(&m_bp_opts,
247b842f2ecSJim Ingham                                LLDB_OPT_SET_1 | LLDB_OPT_SET_3 | LLDB_OPT_SET_4,
248b842f2ecSJim Ingham                                LLDB_OPT_SET_ALL);
249b842f2ecSJim Ingham           m_all_options.Append(&m_dummy_options, LLDB_OPT_SET_1, LLDB_OPT_SET_ALL);
250b842f2ecSJim Ingham           m_all_options.Append(&m_options);
251b842f2ecSJim Ingham           m_all_options.Finalize();
252b842f2ecSJim Ingham         }
2535a988416SJim Ingham 
2549e85e5a8SEugene Zelenko   ~CommandObjectBreakpointSet() override = default;
2555a988416SJim Ingham 
256b842f2ecSJim Ingham   Options *GetOptions() override { return &m_all_options; }
2575a988416SJim Ingham 
258b842f2ecSJim Ingham   class CommandOptions : public OptionGroup {
2595a988416SJim Ingham   public:
260b9c1b51eSKate Stone     CommandOptions()
261b842f2ecSJim Ingham         : OptionGroup(), m_condition(), m_filenames(), m_line_num(0), m_column(0),
262b9c1b51eSKate Stone           m_func_names(), m_func_name_type_mask(eFunctionNameTypeNone),
263b9c1b51eSKate Stone           m_func_regexp(), m_source_text_regexp(), m_modules(), m_load_addr(),
264b9c1b51eSKate Stone           m_catch_bp(false), m_throw_bp(true), m_hardware(false),
265a72b31c7SJim Ingham           m_exception_language(eLanguageTypeUnknown),
26623b1decbSDawn Perchik           m_language(lldb::eLanguageTypeUnknown),
267b842f2ecSJim Ingham           m_skip_prologue(eLazyBoolCalculate),
268b9c1b51eSKate Stone           m_all_files(false), m_move_to_nearest_code(eLazyBoolCalculate) {}
26930fdc8d8SChris Lattner 
2709e85e5a8SEugene Zelenko     ~CommandOptions() override = default;
27187df91b8SJim Ingham 
27297206d57SZachary Turner     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
273b9c1b51eSKate Stone                           ExecutionContext *execution_context) override {
27497206d57SZachary Turner       Status error;
275b842f2ecSJim Ingham       const int short_option = g_breakpoint_set_options[option_idx].short_option;
27630fdc8d8SChris Lattner 
277b9c1b51eSKate Stone       switch (short_option) {
278b9c1b51eSKate Stone       case 'a': {
27947cbf4a0SPavel Labath         m_load_addr = OptionArgParser::ToAddress(execution_context, option_arg,
280e1cfbc79STodd Fiala                                                  LLDB_INVALID_ADDRESS, &error);
281b9c1b51eSKate Stone       } break;
28230fdc8d8SChris Lattner 
283e732052fSJim Ingham       case 'A':
284e732052fSJim Ingham         m_all_files = true;
285e732052fSJim Ingham         break;
286e732052fSJim Ingham 
287ca36cd16SJim Ingham       case 'b':
288ca36cd16SJim Ingham         m_func_names.push_back(option_arg);
289ca36cd16SJim Ingham         m_func_name_type_mask |= eFunctionNameTypeBase;
290ca36cd16SJim Ingham         break;
291ca36cd16SJim Ingham 
292fe11483bSZachary Turner       case 'C':
293fe11483bSZachary Turner         if (option_arg.getAsInteger(0, m_column))
294b9c1b51eSKate Stone           error.SetErrorStringWithFormat("invalid column number: %s",
295fe11483bSZachary Turner                                          option_arg.str().c_str());
29630fdc8d8SChris Lattner         break;
2979e85e5a8SEugene Zelenko 
298b9c1b51eSKate Stone       case 'E': {
299fe11483bSZachary Turner         LanguageType language = Language::GetLanguageTypeFromString(option_arg);
300fab10e89SJim Ingham 
301b9c1b51eSKate Stone         switch (language) {
302fab10e89SJim Ingham         case eLanguageTypeC89:
303fab10e89SJim Ingham         case eLanguageTypeC:
304fab10e89SJim Ingham         case eLanguageTypeC99:
3051d0089faSBruce Mitchener         case eLanguageTypeC11:
306a72b31c7SJim Ingham           m_exception_language = eLanguageTypeC;
307fab10e89SJim Ingham           break;
308fab10e89SJim Ingham         case eLanguageTypeC_plus_plus:
3091d0089faSBruce Mitchener         case eLanguageTypeC_plus_plus_03:
3101d0089faSBruce Mitchener         case eLanguageTypeC_plus_plus_11:
3112ba84a6aSBruce Mitchener         case eLanguageTypeC_plus_plus_14:
312a72b31c7SJim Ingham           m_exception_language = eLanguageTypeC_plus_plus;
313fab10e89SJim Ingham           break;
314fab10e89SJim Ingham         case eLanguageTypeObjC:
315a72b31c7SJim Ingham           m_exception_language = eLanguageTypeObjC;
316fab10e89SJim Ingham           break;
317fab10e89SJim Ingham         case eLanguageTypeObjC_plus_plus:
318b9c1b51eSKate Stone           error.SetErrorStringWithFormat(
319b9c1b51eSKate Stone               "Set exception breakpoints separately for c++ and objective-c");
320fab10e89SJim Ingham           break;
321fab10e89SJim Ingham         case eLanguageTypeUnknown:
322b9c1b51eSKate Stone           error.SetErrorStringWithFormat(
323b9c1b51eSKate Stone               "Unknown language type: '%s' for exception breakpoint",
324fe11483bSZachary Turner               option_arg.str().c_str());
325fab10e89SJim Ingham           break;
326fab10e89SJim Ingham         default:
327b9c1b51eSKate Stone           error.SetErrorStringWithFormat(
328b9c1b51eSKate Stone               "Unsupported language type: '%s' for exception breakpoint",
329fe11483bSZachary Turner               option_arg.str().c_str());
330fab10e89SJim Ingham         }
331b9c1b51eSKate Stone       } break;
332ca36cd16SJim Ingham 
333ca36cd16SJim Ingham       case 'f':
3348f3be7a3SJonas Devlieghere         m_filenames.AppendIfUnique(FileSpec(option_arg));
335fab10e89SJim Ingham         break;
336ca36cd16SJim Ingham 
337ca36cd16SJim Ingham       case 'F':
338ca36cd16SJim Ingham         m_func_names.push_back(option_arg);
339ca36cd16SJim Ingham         m_func_name_type_mask |= eFunctionNameTypeFull;
340ca36cd16SJim Ingham         break;
341ca36cd16SJim Ingham 
342b9c1b51eSKate Stone       case 'h': {
343fab10e89SJim Ingham         bool success;
34447cbf4a0SPavel Labath         m_catch_bp = OptionArgParser::ToBoolean(option_arg, true, &success);
345fab10e89SJim Ingham         if (!success)
346b9c1b51eSKate Stone           error.SetErrorStringWithFormat(
347fe11483bSZachary Turner               "Invalid boolean value for on-catch option: '%s'",
348fe11483bSZachary Turner               option_arg.str().c_str());
349b9c1b51eSKate Stone       } break;
350eb023e75SGreg Clayton 
351eb023e75SGreg Clayton       case 'H':
352eb023e75SGreg Clayton         m_hardware = true;
353eb023e75SGreg Clayton         break;
354eb023e75SGreg Clayton 
3553815e702SJim Ingham       case 'k': {
3563815e702SJim Ingham           if (m_current_key.empty())
3573815e702SJim Ingham             m_current_key.assign(option_arg);
3583815e702SJim Ingham           else
3593815e702SJim Ingham             error.SetErrorStringWithFormat("Key: %s missing value.",
3603815e702SJim Ingham                                             m_current_key.c_str());
3613815e702SJim Ingham 
3623815e702SJim Ingham       } break;
363b9c1b51eSKate Stone       case 'K': {
364a8558b62SJim Ingham         bool success;
365a8558b62SJim Ingham         bool value;
36647cbf4a0SPavel Labath         value = OptionArgParser::ToBoolean(option_arg, true, &success);
367a8558b62SJim Ingham         if (value)
368a8558b62SJim Ingham           m_skip_prologue = eLazyBoolYes;
369a8558b62SJim Ingham         else
370a8558b62SJim Ingham           m_skip_prologue = eLazyBoolNo;
371a8558b62SJim Ingham 
372a8558b62SJim Ingham         if (!success)
373b9c1b51eSKate Stone           error.SetErrorStringWithFormat(
374b9c1b51eSKate Stone               "Invalid boolean value for skip prologue option: '%s'",
375fe11483bSZachary Turner               option_arg.str().c_str());
376b9c1b51eSKate Stone       } break;
377ca36cd16SJim Ingham 
378fe11483bSZachary Turner       case 'l':
379fe11483bSZachary Turner         if (option_arg.getAsInteger(0, m_line_num))
380b9c1b51eSKate Stone           error.SetErrorStringWithFormat("invalid line number: %s.",
381fe11483bSZachary Turner                                          option_arg.str().c_str());
382ca36cd16SJim Ingham         break;
383055ad9beSIlia K 
38423b1decbSDawn Perchik       case 'L':
385fe11483bSZachary Turner         m_language = Language::GetLanguageTypeFromString(option_arg);
38623b1decbSDawn Perchik         if (m_language == eLanguageTypeUnknown)
387b9c1b51eSKate Stone           error.SetErrorStringWithFormat(
388fe11483bSZachary Turner               "Unknown language type: '%s' for breakpoint",
389fe11483bSZachary Turner               option_arg.str().c_str());
39023b1decbSDawn Perchik         break;
39123b1decbSDawn Perchik 
392b9c1b51eSKate Stone       case 'm': {
393055ad9beSIlia K         bool success;
394055ad9beSIlia K         bool value;
39547cbf4a0SPavel Labath         value = OptionArgParser::ToBoolean(option_arg, true, &success);
396055ad9beSIlia K         if (value)
397055ad9beSIlia K           m_move_to_nearest_code = eLazyBoolYes;
398055ad9beSIlia K         else
399055ad9beSIlia K           m_move_to_nearest_code = eLazyBoolNo;
400055ad9beSIlia K 
401055ad9beSIlia K         if (!success)
402b9c1b51eSKate Stone           error.SetErrorStringWithFormat(
403b9c1b51eSKate Stone               "Invalid boolean value for move-to-nearest-code option: '%s'",
404fe11483bSZachary Turner               option_arg.str().c_str());
405055ad9beSIlia K         break;
406055ad9beSIlia K       }
407055ad9beSIlia K 
408ca36cd16SJim Ingham       case 'M':
409ca36cd16SJim Ingham         m_func_names.push_back(option_arg);
410ca36cd16SJim Ingham         m_func_name_type_mask |= eFunctionNameTypeMethod;
411ca36cd16SJim Ingham         break;
412ca36cd16SJim Ingham 
413ca36cd16SJim Ingham       case 'n':
414ca36cd16SJim Ingham         m_func_names.push_back(option_arg);
415ca36cd16SJim Ingham         m_func_name_type_mask |= eFunctionNameTypeAuto;
416ca36cd16SJim Ingham         break;
417ca36cd16SJim Ingham 
4186fa7681bSZachary Turner       case 'N': {
419fe11483bSZachary Turner         if (BreakpointID::StringIsBreakpointName(option_arg, error))
4205e09c8c3SJim Ingham           m_breakpoint_names.push_back(option_arg);
421ff9a91eaSJim Ingham         else
422ff9a91eaSJim Ingham           error.SetErrorStringWithFormat("Invalid breakpoint name: %s",
423fe11483bSZachary Turner                                          option_arg.str().c_str());
4245e09c8c3SJim Ingham         break;
4256fa7681bSZachary Turner       }
4265e09c8c3SJim Ingham 
427b9c1b51eSKate Stone       case 'R': {
4282411167fSJim Ingham         lldb::addr_t tmp_offset_addr;
42947cbf4a0SPavel Labath         tmp_offset_addr = OptionArgParser::ToAddress(execution_context,
43047cbf4a0SPavel Labath                                                      option_arg, 0, &error);
4312411167fSJim Ingham         if (error.Success())
4322411167fSJim Ingham           m_offset_addr = tmp_offset_addr;
433b9c1b51eSKate Stone       } break;
4342411167fSJim Ingham 
435a72b31c7SJim Ingham       case 'O':
436fe11483bSZachary Turner         m_exception_extra_args.AppendArgument("-O");
437fe11483bSZachary Turner         m_exception_extra_args.AppendArgument(option_arg);
438a72b31c7SJim Ingham         break;
439a72b31c7SJim Ingham 
440ca36cd16SJim Ingham       case 'p':
441ca36cd16SJim Ingham         m_source_text_regexp.assign(option_arg);
442ca36cd16SJim Ingham         break;
443ca36cd16SJim Ingham 
4443815e702SJim Ingham       case 'P':
4453815e702SJim Ingham         m_python_class.assign(option_arg);
4463815e702SJim Ingham         break;
4473815e702SJim Ingham 
448ca36cd16SJim Ingham       case 'r':
449ca36cd16SJim Ingham         m_func_regexp.assign(option_arg);
450ca36cd16SJim Ingham         break;
451ca36cd16SJim Ingham 
452ca36cd16SJim Ingham       case 's':
4538f3be7a3SJonas Devlieghere         m_modules.AppendIfUnique(FileSpec(option_arg));
454ca36cd16SJim Ingham         break;
455ca36cd16SJim Ingham 
456ca36cd16SJim Ingham       case 'S':
457ca36cd16SJim Ingham         m_func_names.push_back(option_arg);
458ca36cd16SJim Ingham         m_func_name_type_mask |= eFunctionNameTypeSelector;
459ca36cd16SJim Ingham         break;
460ca36cd16SJim Ingham 
4613815e702SJim Ingham       case 'v': {
4623815e702SJim Ingham           if (!m_current_key.empty()) {
4633815e702SJim Ingham               m_extra_args_sp->AddStringItem(m_current_key, option_arg);
4643815e702SJim Ingham               m_current_key.clear();
4653815e702SJim Ingham           }
4663815e702SJim Ingham           else
4673815e702SJim Ingham             error.SetErrorStringWithFormat("Value \"%s\" missing matching key.",
4683815e702SJim Ingham                                            option_arg.str().c_str());
4693815e702SJim Ingham       } break;
4703815e702SJim Ingham 
471b9c1b51eSKate Stone       case 'w': {
472ca36cd16SJim Ingham         bool success;
47347cbf4a0SPavel Labath         m_throw_bp = OptionArgParser::ToBoolean(option_arg, true, &success);
474ca36cd16SJim Ingham         if (!success)
475b9c1b51eSKate Stone           error.SetErrorStringWithFormat(
476fe11483bSZachary Turner               "Invalid boolean value for on-throw option: '%s'",
477fe11483bSZachary Turner               option_arg.str().c_str());
478b9c1b51eSKate Stone       } break;
479ca36cd16SJim Ingham 
48076bb8d67SJim Ingham       case 'X':
48176bb8d67SJim Ingham         m_source_regex_func_names.insert(option_arg);
48276bb8d67SJim Ingham         break;
48376bb8d67SJim Ingham 
48430fdc8d8SChris Lattner       default:
485*36162014SRaphael Isemann         llvm_unreachable("Unimplemented option");
48630fdc8d8SChris Lattner       }
48730fdc8d8SChris Lattner 
48830fdc8d8SChris Lattner       return error;
48930fdc8d8SChris Lattner     }
4909e85e5a8SEugene Zelenko 
491b9c1b51eSKate Stone     void OptionParsingStarting(ExecutionContext *execution_context) override {
49287df91b8SJim Ingham       m_filenames.Clear();
49330fdc8d8SChris Lattner       m_line_num = 0;
49430fdc8d8SChris Lattner       m_column = 0;
495fab10e89SJim Ingham       m_func_names.clear();
4961f746071SGreg Clayton       m_func_name_type_mask = eFunctionNameTypeNone;
49730fdc8d8SChris Lattner       m_func_regexp.clear();
4981f746071SGreg Clayton       m_source_text_regexp.clear();
49987df91b8SJim Ingham       m_modules.Clear();
5001f746071SGreg Clayton       m_load_addr = LLDB_INVALID_ADDRESS;
5012411167fSJim Ingham       m_offset_addr = 0;
502fab10e89SJim Ingham       m_catch_bp = false;
503fab10e89SJim Ingham       m_throw_bp = true;
504eb023e75SGreg Clayton       m_hardware = false;
505a72b31c7SJim Ingham       m_exception_language = eLanguageTypeUnknown;
50623b1decbSDawn Perchik       m_language = lldb::eLanguageTypeUnknown;
507a8558b62SJim Ingham       m_skip_prologue = eLazyBoolCalculate;
5085e09c8c3SJim Ingham       m_breakpoint_names.clear();
509e732052fSJim Ingham       m_all_files = false;
510a72b31c7SJim Ingham       m_exception_extra_args.Clear();
511055ad9beSIlia K       m_move_to_nearest_code = eLazyBoolCalculate;
51276bb8d67SJim Ingham       m_source_regex_func_names.clear();
5133815e702SJim Ingham       m_python_class.clear();
514796ac80bSJonas Devlieghere       m_extra_args_sp = std::make_shared<StructuredData::Dictionary>();
5153815e702SJim Ingham       m_current_key.clear();
51630fdc8d8SChris Lattner     }
51730fdc8d8SChris Lattner 
5181f0f5b5bSZachary Turner     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
51970602439SZachary Turner       return llvm::makeArrayRef(g_breakpoint_set_options);
5201f0f5b5bSZachary Turner     }
52130fdc8d8SChris Lattner 
5225a988416SJim Ingham     // Instance variables to hold the values for command options.
523969795f1SJim Ingham 
5245a988416SJim Ingham     std::string m_condition;
5255a988416SJim Ingham     FileSpecList m_filenames;
5265a988416SJim Ingham     uint32_t m_line_num;
5275a988416SJim Ingham     uint32_t m_column;
5285a988416SJim Ingham     std::vector<std::string> m_func_names;
5295e09c8c3SJim Ingham     std::vector<std::string> m_breakpoint_names;
530117b1fa1SZachary Turner     lldb::FunctionNameType m_func_name_type_mask;
5315a988416SJim Ingham     std::string m_func_regexp;
5325a988416SJim Ingham     std::string m_source_text_regexp;
5335a988416SJim Ingham     FileSpecList m_modules;
5345a988416SJim Ingham     lldb::addr_t m_load_addr;
5352411167fSJim Ingham     lldb::addr_t m_offset_addr;
5365a988416SJim Ingham     bool m_catch_bp;
5375a988416SJim Ingham     bool m_throw_bp;
538eb023e75SGreg Clayton     bool m_hardware; // Request to use hardware breakpoints
539a72b31c7SJim Ingham     lldb::LanguageType m_exception_language;
54023b1decbSDawn Perchik     lldb::LanguageType m_language;
5415a988416SJim Ingham     LazyBool m_skip_prologue;
542e732052fSJim Ingham     bool m_all_files;
543a72b31c7SJim Ingham     Args m_exception_extra_args;
544055ad9beSIlia K     LazyBool m_move_to_nearest_code;
54576bb8d67SJim Ingham     std::unordered_set<std::string> m_source_regex_func_names;
5463815e702SJim Ingham     std::string m_python_class;
5473815e702SJim Ingham     StructuredData::DictionarySP m_extra_args_sp;
5483815e702SJim Ingham     std::string m_current_key;
5495a988416SJim Ingham   };
5505a988416SJim Ingham 
5515a988416SJim Ingham protected:
552b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
553b842f2ecSJim Ingham     Target *target = GetSelectedOrDummyTarget(m_dummy_options.m_use_dummy);
55433df7cd3SJim Ingham 
555b9c1b51eSKate Stone     if (target == nullptr) {
556b9c1b51eSKate Stone       result.AppendError("Invalid target.  Must set target before setting "
557b9c1b51eSKate Stone                          "breakpoints (see 'target create' command).");
55830fdc8d8SChris Lattner       result.SetStatus(eReturnStatusFailed);
55930fdc8d8SChris Lattner       return false;
56030fdc8d8SChris Lattner     }
56130fdc8d8SChris Lattner 
56230fdc8d8SChris Lattner     // The following are the various types of breakpoints that could be set:
56330fdc8d8SChris Lattner     //   1).  -f -l -p  [-s -g]   (setting breakpoint by source location)
56430fdc8d8SChris Lattner     //   2).  -a  [-s -g]         (setting breakpoint by address)
56530fdc8d8SChris Lattner     //   3).  -n  [-s -g]         (setting breakpoint by function name)
566b9c1b51eSKate Stone     //   4).  -r  [-s -g]         (setting breakpoint by function name regular
567b9c1b51eSKate Stone     //   expression)
568b9c1b51eSKate Stone     //   5).  -p -f               (setting a breakpoint by comparing a reg-exp
569b9c1b51eSKate Stone     //   to source text)
570b9c1b51eSKate Stone     //   6).  -E [-w -h]          (setting a breakpoint for exceptions for a
571b9c1b51eSKate Stone     //   given language.)
57230fdc8d8SChris Lattner 
57330fdc8d8SChris Lattner     BreakpointSetType break_type = eSetTypeInvalid;
57430fdc8d8SChris Lattner 
5753815e702SJim Ingham     if (!m_options.m_python_class.empty())
5763815e702SJim Ingham       break_type = eSetTypeScripted;
5773815e702SJim Ingham     else if (m_options.m_line_num != 0)
57830fdc8d8SChris Lattner       break_type = eSetTypeFileAndLine;
57930fdc8d8SChris Lattner     else if (m_options.m_load_addr != LLDB_INVALID_ADDRESS)
58030fdc8d8SChris Lattner       break_type = eSetTypeAddress;
581fab10e89SJim Ingham     else if (!m_options.m_func_names.empty())
58230fdc8d8SChris Lattner       break_type = eSetTypeFunctionName;
58330fdc8d8SChris Lattner     else if (!m_options.m_func_regexp.empty())
58430fdc8d8SChris Lattner       break_type = eSetTypeFunctionRegexp;
585969795f1SJim Ingham     else if (!m_options.m_source_text_regexp.empty())
586969795f1SJim Ingham       break_type = eSetTypeSourceRegexp;
587a72b31c7SJim Ingham     else if (m_options.m_exception_language != eLanguageTypeUnknown)
588fab10e89SJim Ingham       break_type = eSetTypeException;
58930fdc8d8SChris Lattner 
590b842f2ecSJim Ingham     BreakpointSP bp_sp = nullptr;
591274060b6SGreg Clayton     FileSpec module_spec;
592a8558b62SJim Ingham     const bool internal = false;
593a8558b62SJim Ingham 
594b9c1b51eSKate Stone     // If the user didn't specify skip-prologue, having an offset should turn
595b9c1b51eSKate Stone     // that off.
596b9c1b51eSKate Stone     if (m_options.m_offset_addr != 0 &&
597b9c1b51eSKate Stone         m_options.m_skip_prologue == eLazyBoolCalculate)
5982411167fSJim Ingham       m_options.m_skip_prologue = eLazyBoolNo;
5992411167fSJim Ingham 
600b9c1b51eSKate Stone     switch (break_type) {
60130fdc8d8SChris Lattner     case eSetTypeFileAndLine: // Breakpoint by source position
60230fdc8d8SChris Lattner     {
60330fdc8d8SChris Lattner       FileSpec file;
604c7bece56SGreg Clayton       const size_t num_files = m_options.m_filenames.GetSize();
605b9c1b51eSKate Stone       if (num_files == 0) {
606b9c1b51eSKate Stone         if (!GetDefaultFile(target, file, result)) {
60787df91b8SJim Ingham           result.AppendError("No file supplied and no default file available.");
60887df91b8SJim Ingham           result.SetStatus(eReturnStatusFailed);
60987df91b8SJim Ingham           return false;
61087df91b8SJim Ingham         }
611b9c1b51eSKate Stone       } else if (num_files > 1) {
612b9c1b51eSKate Stone         result.AppendError("Only one file at a time is allowed for file and "
613b9c1b51eSKate Stone                            "line breakpoints.");
61487df91b8SJim Ingham         result.SetStatus(eReturnStatusFailed);
61587df91b8SJim Ingham         return false;
616b9c1b51eSKate Stone       } else
61787df91b8SJim Ingham         file = m_options.m_filenames.GetFileSpecAtIndex(0);
61830fdc8d8SChris Lattner 
6191f746071SGreg Clayton       // Only check for inline functions if
6201f746071SGreg Clayton       LazyBool check_inlines = eLazyBoolCalculate;
6211f746071SGreg Clayton 
622b842f2ecSJim Ingham       bp_sp = target->CreateBreakpoint(&(m_options.m_modules),
623b842f2ecSJim Ingham                                        file,
624b842f2ecSJim Ingham                                        m_options.m_line_num,
625431b1584SAdrian Prantl                                        m_options.m_column,
626b842f2ecSJim Ingham                                        m_options.m_offset_addr,
627b842f2ecSJim Ingham                                        check_inlines,
628b842f2ecSJim Ingham                                        m_options.m_skip_prologue,
629b842f2ecSJim Ingham                                        internal,
630b842f2ecSJim Ingham                                        m_options.m_hardware,
631b842f2ecSJim Ingham                                        m_options.m_move_to_nearest_code);
632b9c1b51eSKate Stone     } break;
6336eee5aa0SGreg Clayton 
63430fdc8d8SChris Lattner     case eSetTypeAddress: // Breakpoint by address
635055a08a4SJim Ingham     {
636b9c1b51eSKate Stone       // If a shared library has been specified, make an lldb_private::Address
637b842f2ecSJim Ingham       // with the library, and use that.  That way the address breakpoint
638b842f2ecSJim Ingham       //  will track the load location of the library.
639055a08a4SJim Ingham       size_t num_modules_specified = m_options.m_modules.GetSize();
640b9c1b51eSKate Stone       if (num_modules_specified == 1) {
641b9c1b51eSKate Stone         const FileSpec *file_spec =
642b9c1b51eSKate Stone             m_options.m_modules.GetFileSpecPointerAtIndex(0);
643b842f2ecSJim Ingham         bp_sp = target->CreateAddressInModuleBreakpoint(m_options.m_load_addr,
644b9c1b51eSKate Stone                                                         internal, file_spec,
645b842f2ecSJim Ingham                                                         m_options.m_hardware);
646b9c1b51eSKate Stone       } else if (num_modules_specified == 0) {
647b842f2ecSJim Ingham         bp_sp = target->CreateBreakpoint(m_options.m_load_addr, internal,
648b842f2ecSJim Ingham                                          m_options.m_hardware);
649b9c1b51eSKate Stone       } else {
650b9c1b51eSKate Stone         result.AppendError("Only one shared library can be specified for "
651b9c1b51eSKate Stone                            "address breakpoints.");
652055a08a4SJim Ingham         result.SetStatus(eReturnStatusFailed);
653055a08a4SJim Ingham         return false;
654055a08a4SJim Ingham       }
65530fdc8d8SChris Lattner       break;
656055a08a4SJim Ingham     }
65730fdc8d8SChris Lattner     case eSetTypeFunctionName: // Breakpoint by function name
6580c5cd90dSGreg Clayton     {
659117b1fa1SZachary Turner       FunctionNameType name_type_mask = m_options.m_func_name_type_mask;
6600c5cd90dSGreg Clayton 
6610c5cd90dSGreg Clayton       if (name_type_mask == 0)
662e02b8504SGreg Clayton         name_type_mask = eFunctionNameTypeAuto;
6630c5cd90dSGreg Clayton 
664b842f2ecSJim Ingham       bp_sp = target->CreateBreakpoint(&(m_options.m_modules),
665b842f2ecSJim Ingham                                        &(m_options.m_filenames),
666b842f2ecSJim Ingham                                        m_options.m_func_names,
667b842f2ecSJim Ingham                                        name_type_mask,
668b842f2ecSJim Ingham                                        m_options.m_language,
669b842f2ecSJim Ingham                                        m_options.m_offset_addr,
670b842f2ecSJim Ingham                                        m_options.m_skip_prologue,
671b842f2ecSJim Ingham                                        internal,
672b842f2ecSJim Ingham                                        m_options.m_hardware);
673b9c1b51eSKate Stone     } break;
6740c5cd90dSGreg Clayton 
675b9c1b51eSKate Stone     case eSetTypeFunctionRegexp: // Breakpoint by regular expression function
676b9c1b51eSKate Stone                                  // name
67730fdc8d8SChris Lattner       {
67895eae423SZachary Turner         RegularExpression regexp(m_options.m_func_regexp);
6793af3f1e8SJonas Devlieghere         if (llvm::Error err = regexp.GetError()) {
680b9c1b51eSKate Stone           result.AppendErrorWithFormat(
681b9c1b51eSKate Stone               "Function name regular expression could not be compiled: \"%s\"",
6823af3f1e8SJonas Devlieghere               llvm::toString(std::move(err)).c_str());
68330fdc8d8SChris Lattner           result.SetStatus(eReturnStatusFailed);
684969795f1SJim Ingham           return false;
68530fdc8d8SChris Lattner         }
68687df91b8SJim Ingham 
687b842f2ecSJim Ingham         bp_sp = target->CreateFuncRegexBreakpoint(&(m_options.m_modules),
688b842f2ecSJim Ingham                                                   &(m_options.m_filenames),
689b842f2ecSJim Ingham                                                   regexp,
690b842f2ecSJim Ingham                                                   m_options.m_language,
691b842f2ecSJim Ingham                                                   m_options.m_skip_prologue,
692b842f2ecSJim Ingham                                                   internal,
693b842f2ecSJim Ingham                                                   m_options.m_hardware);
694e14dc268SJim Ingham       }
695e14dc268SJim Ingham       break;
696969795f1SJim Ingham     case eSetTypeSourceRegexp: // Breakpoint by regexp on source text.
697969795f1SJim Ingham     {
698c7bece56SGreg Clayton       const size_t num_files = m_options.m_filenames.GetSize();
69987df91b8SJim Ingham 
700b9c1b51eSKate Stone       if (num_files == 0 && !m_options.m_all_files) {
701969795f1SJim Ingham         FileSpec file;
702b9c1b51eSKate Stone         if (!GetDefaultFile(target, file, result)) {
703b9c1b51eSKate Stone           result.AppendError(
704b9c1b51eSKate Stone               "No files provided and could not find default file.");
70587df91b8SJim Ingham           result.SetStatus(eReturnStatusFailed);
70687df91b8SJim Ingham           return false;
707b9c1b51eSKate Stone         } else {
70887df91b8SJim Ingham           m_options.m_filenames.Append(file);
70987df91b8SJim Ingham         }
71087df91b8SJim Ingham       }
7110c5cd90dSGreg Clayton 
71295eae423SZachary Turner       RegularExpression regexp(m_options.m_source_text_regexp);
7133af3f1e8SJonas Devlieghere       if (llvm::Error err = regexp.GetError()) {
714b9c1b51eSKate Stone         result.AppendErrorWithFormat(
715b9c1b51eSKate Stone             "Source text regular expression could not be compiled: \"%s\"",
7163af3f1e8SJonas Devlieghere             llvm::toString(std::move(err)).c_str());
717969795f1SJim Ingham         result.SetStatus(eReturnStatusFailed);
718969795f1SJim Ingham         return false;
719969795f1SJim Ingham       }
720b842f2ecSJim Ingham       bp_sp =
721b842f2ecSJim Ingham           target->CreateSourceRegexBreakpoint(&(m_options.m_modules),
722b842f2ecSJim Ingham                                               &(m_options.m_filenames),
723b842f2ecSJim Ingham                                               m_options
724b842f2ecSJim Ingham                                                   .m_source_regex_func_names,
725b842f2ecSJim Ingham                                               regexp,
726b842f2ecSJim Ingham                                               internal,
727b842f2ecSJim Ingham                                               m_options.m_hardware,
728b842f2ecSJim Ingham                                               m_options.m_move_to_nearest_code);
729b9c1b51eSKate Stone     } break;
730b9c1b51eSKate Stone     case eSetTypeException: {
73197206d57SZachary Turner       Status precond_error;
732b842f2ecSJim Ingham       bp_sp = target->CreateExceptionBreakpoint(m_options.m_exception_language,
733b842f2ecSJim Ingham                                                 m_options.m_catch_bp,
734b842f2ecSJim Ingham                                                 m_options.m_throw_bp,
735b842f2ecSJim Ingham                                                 internal,
736b842f2ecSJim Ingham                                                 &m_options
737b842f2ecSJim Ingham                                                     .m_exception_extra_args,
738b842f2ecSJim Ingham                                                 &precond_error);
739b9c1b51eSKate Stone       if (precond_error.Fail()) {
740b9c1b51eSKate Stone         result.AppendErrorWithFormat(
741b9c1b51eSKate Stone             "Error setting extra exception arguments: %s",
742a72b31c7SJim Ingham             precond_error.AsCString());
743b842f2ecSJim Ingham         target->RemoveBreakpointByID(bp_sp->GetID());
744a72b31c7SJim Ingham         result.SetStatus(eReturnStatusFailed);
745a72b31c7SJim Ingham         return false;
746a72b31c7SJim Ingham       }
747b9c1b51eSKate Stone     } break;
7483815e702SJim Ingham     case eSetTypeScripted: {
7493815e702SJim Ingham 
7503815e702SJim Ingham       Status error;
7513815e702SJim Ingham       bp_sp = target->CreateScriptedBreakpoint(m_options.m_python_class,
7523815e702SJim Ingham                                                &(m_options.m_modules),
7533815e702SJim Ingham                                                &(m_options.m_filenames),
7543815e702SJim Ingham                                                false,
7553815e702SJim Ingham                                                m_options.m_hardware,
7563815e702SJim Ingham                                                m_options.m_extra_args_sp,
7573815e702SJim Ingham                                                &error);
7583815e702SJim Ingham       if (error.Fail()) {
7593815e702SJim Ingham         result.AppendErrorWithFormat(
7603815e702SJim Ingham             "Error setting extra exception arguments: %s",
7613815e702SJim Ingham             error.AsCString());
7623815e702SJim Ingham         target->RemoveBreakpointByID(bp_sp->GetID());
7633815e702SJim Ingham         result.SetStatus(eReturnStatusFailed);
7643815e702SJim Ingham         return false;
7653815e702SJim Ingham       }
7663815e702SJim Ingham     } break;
76730fdc8d8SChris Lattner     default:
76830fdc8d8SChris Lattner       break;
76930fdc8d8SChris Lattner     }
77030fdc8d8SChris Lattner 
7711b54c88cSJim Ingham     // Now set the various options that were passed in:
772b842f2ecSJim Ingham     if (bp_sp) {
773b842f2ecSJim Ingham       bp_sp->GetOptions()->CopyOverSetOptions(m_bp_opts.GetBreakpointOptions());
774ca36cd16SJim Ingham 
775b9c1b51eSKate Stone       if (!m_options.m_breakpoint_names.empty()) {
77697206d57SZachary Turner         Status name_error;
777ff9a91eaSJim Ingham         for (auto name : m_options.m_breakpoint_names) {
778b842f2ecSJim Ingham           target->AddNameToBreakpoint(bp_sp, name.c_str(), name_error);
779ff9a91eaSJim Ingham           if (name_error.Fail()) {
780ff9a91eaSJim Ingham             result.AppendErrorWithFormat("Invalid breakpoint name: %s",
781ff9a91eaSJim Ingham                                          name.c_str());
782b842f2ecSJim Ingham             target->RemoveBreakpointByID(bp_sp->GetID());
783ff9a91eaSJim Ingham             result.SetStatus(eReturnStatusFailed);
784ff9a91eaSJim Ingham             return false;
785ff9a91eaSJim Ingham           }
786ff9a91eaSJim Ingham         }
7875e09c8c3SJim Ingham       }
7881b54c88cSJim Ingham     }
7891b54c88cSJim Ingham 
790b842f2ecSJim Ingham     if (bp_sp) {
79185e8b814SJim Ingham       Stream &output_stream = result.GetOutputStream();
7921391cc7dSJim Ingham       const bool show_locations = false;
793b842f2ecSJim Ingham       bp_sp->GetDescription(&output_stream, lldb::eDescriptionLevelInitial,
794b9c1b51eSKate Stone                          show_locations);
79557179860SJonas Devlieghere       if (target == GetDebugger().GetDummyTarget())
796b9c1b51eSKate Stone         output_stream.Printf("Breakpoint set in dummy target, will get copied "
797b9c1b51eSKate Stone                              "into future targets.\n");
798b9c1b51eSKate Stone       else {
79905097246SAdrian Prantl         // Don't print out this warning for exception breakpoints.  They can
80005097246SAdrian Prantl         // get set before the target is set, but we won't know how to actually
80105097246SAdrian Prantl         // set the breakpoint till we run.
802b842f2ecSJim Ingham         if (bp_sp->GetNumLocations() == 0 && break_type != eSetTypeException) {
803b9c1b51eSKate Stone           output_stream.Printf("WARNING:  Unable to resolve breakpoint to any "
804b9c1b51eSKate Stone                                "actual locations.\n");
8054aeb1989SJim Ingham         }
8064aeb1989SJim Ingham       }
80730fdc8d8SChris Lattner       result.SetStatus(eReturnStatusSuccessFinishResult);
808b842f2ecSJim Ingham     } else if (!bp_sp) {
80930fdc8d8SChris Lattner       result.AppendError("Breakpoint creation failed: No breakpoint created.");
81030fdc8d8SChris Lattner       result.SetStatus(eReturnStatusFailed);
81130fdc8d8SChris Lattner     }
81230fdc8d8SChris Lattner 
81330fdc8d8SChris Lattner     return result.Succeeded();
81430fdc8d8SChris Lattner   }
81530fdc8d8SChris Lattner 
8165a988416SJim Ingham private:
817b9c1b51eSKate Stone   bool GetDefaultFile(Target *target, FileSpec &file,
818b9c1b51eSKate Stone                       CommandReturnObject &result) {
8195a988416SJim Ingham     uint32_t default_line;
82005097246SAdrian Prantl     // First use the Source Manager's default file. Then use the current stack
82105097246SAdrian Prantl     // frame's file.
822b9c1b51eSKate Stone     if (!target->GetSourceManager().GetDefaultFileAndLine(file, default_line)) {
823b57e4a1bSJason Molenda       StackFrame *cur_frame = m_exe_ctx.GetFramePtr();
824b9c1b51eSKate Stone       if (cur_frame == nullptr) {
825b9c1b51eSKate Stone         result.AppendError(
826b9c1b51eSKate Stone             "No selected frame to use to find the default file.");
8275a988416SJim Ingham         result.SetStatus(eReturnStatusFailed);
8285a988416SJim Ingham         return false;
829b9c1b51eSKate Stone       } else if (!cur_frame->HasDebugInformation()) {
830b9c1b51eSKate Stone         result.AppendError("Cannot use the selected frame to find the default "
831b9c1b51eSKate Stone                            "file, it has no debug info.");
8325a988416SJim Ingham         result.SetStatus(eReturnStatusFailed);
8335a988416SJim Ingham         return false;
834b9c1b51eSKate Stone       } else {
835b9c1b51eSKate Stone         const SymbolContext &sc =
836b9c1b51eSKate Stone             cur_frame->GetSymbolContext(eSymbolContextLineEntry);
837b9c1b51eSKate Stone         if (sc.line_entry.file) {
8385a988416SJim Ingham           file = sc.line_entry.file;
839b9c1b51eSKate Stone         } else {
840b9c1b51eSKate Stone           result.AppendError("Can't find the file for the selected frame to "
841b9c1b51eSKate Stone                              "use as the default file.");
8425a988416SJim Ingham           result.SetStatus(eReturnStatusFailed);
8435a988416SJim Ingham           return false;
8445a988416SJim Ingham         }
8455a988416SJim Ingham       }
8465a988416SJim Ingham     }
8475a988416SJim Ingham     return true;
8485a988416SJim Ingham   }
8495a988416SJim Ingham 
850b842f2ecSJim Ingham   BreakpointOptionGroup m_bp_opts;
851b842f2ecSJim Ingham   BreakpointDummyOptionGroup m_dummy_options;
8525a988416SJim Ingham   CommandOptions m_options;
853b842f2ecSJim Ingham   OptionGroupOptions m_all_options;
8545a988416SJim Ingham };
8559e85e5a8SEugene Zelenko 
8565a988416SJim Ingham // CommandObjectBreakpointModify
8575a988416SJim Ingham #pragma mark Modify
8585a988416SJim Ingham 
859b9c1b51eSKate Stone class CommandObjectBreakpointModify : public CommandObjectParsed {
8605a988416SJim Ingham public:
861b9c1b51eSKate Stone   CommandObjectBreakpointModify(CommandInterpreter &interpreter)
862b9c1b51eSKate Stone       : CommandObjectParsed(interpreter, "breakpoint modify",
863b9c1b51eSKate Stone                             "Modify the options on a breakpoint or set of "
864b9c1b51eSKate Stone                             "breakpoints in the executable.  "
865b9c1b51eSKate Stone                             "If no breakpoint is specified, acts on the last "
866b9c1b51eSKate Stone                             "created breakpoint.  "
867b9c1b51eSKate Stone                             "With the exception of -e, -d and -i, passing an "
868b9c1b51eSKate Stone                             "empty argument clears the modification.",
8699e85e5a8SEugene Zelenko                             nullptr),
870b9c1b51eSKate Stone         m_options() {
8715a988416SJim Ingham     CommandArgumentEntry arg;
872b9c1b51eSKate Stone     CommandObject::AddIDsArgumentData(arg, eArgTypeBreakpointID,
873b9c1b51eSKate Stone                                       eArgTypeBreakpointIDRange);
874b9c1b51eSKate Stone     // Add the entry for the first argument for this command to the object's
875b9c1b51eSKate Stone     // arguments vector.
8765a988416SJim Ingham     m_arguments.push_back(arg);
877b842f2ecSJim Ingham 
878b842f2ecSJim Ingham     m_options.Append(&m_bp_opts,
879b842f2ecSJim Ingham                      LLDB_OPT_SET_1 | LLDB_OPT_SET_2 | LLDB_OPT_SET_3,
880b842f2ecSJim Ingham                      LLDB_OPT_SET_ALL);
881b842f2ecSJim Ingham     m_options.Append(&m_dummy_opts, LLDB_OPT_SET_1, LLDB_OPT_SET_ALL);
882b842f2ecSJim Ingham     m_options.Finalize();
8835a988416SJim Ingham   }
8845a988416SJim Ingham 
8859e85e5a8SEugene Zelenko   ~CommandObjectBreakpointModify() override = default;
8865a988416SJim Ingham 
887b9c1b51eSKate Stone   Options *GetOptions() override { return &m_options; }
8885a988416SJim Ingham 
8895a988416SJim Ingham protected:
890b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
891b842f2ecSJim Ingham     Target *target = GetSelectedOrDummyTarget(m_dummy_opts.m_use_dummy);
892b9c1b51eSKate Stone     if (target == nullptr) {
8935a988416SJim Ingham       result.AppendError("Invalid target.  No existing target or breakpoints.");
8945a988416SJim Ingham       result.SetStatus(eReturnStatusFailed);
8955a988416SJim Ingham       return false;
8965a988416SJim Ingham     }
8975a988416SJim Ingham 
898bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
899bb19a13cSSaleem Abdulrasool     target->GetBreakpointList().GetListMutex(lock);
9005a988416SJim Ingham 
9015a988416SJim Ingham     BreakpointIDList valid_bp_ids;
9025a988416SJim Ingham 
903b9c1b51eSKate Stone     CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs(
904b842f2ecSJim Ingham         command, target, result, &valid_bp_ids,
905b842f2ecSJim Ingham         BreakpointName::Permissions::PermissionKinds::disablePerm);
9065a988416SJim Ingham 
907b9c1b51eSKate Stone     if (result.Succeeded()) {
9085a988416SJim Ingham       const size_t count = valid_bp_ids.GetSize();
909b9c1b51eSKate Stone       for (size_t i = 0; i < count; ++i) {
9105a988416SJim Ingham         BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i);
9115a988416SJim Ingham 
912b9c1b51eSKate Stone         if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) {
913b9c1b51eSKate Stone           Breakpoint *bp =
914b9c1b51eSKate Stone               target->GetBreakpointByID(cur_bp_id.GetBreakpointID()).get();
915b9c1b51eSKate Stone           if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID) {
916b9c1b51eSKate Stone             BreakpointLocation *location =
917b9c1b51eSKate Stone                 bp->FindLocationByID(cur_bp_id.GetLocationID()).get();
918b842f2ecSJim Ingham             if (location)
919b842f2ecSJim Ingham               location->GetLocationOptions()
920b842f2ecSJim Ingham                   ->CopyOverSetOptions(m_bp_opts.GetBreakpointOptions());
921b9c1b51eSKate Stone           } else {
922b842f2ecSJim Ingham             bp->GetOptions()
923b842f2ecSJim Ingham                 ->CopyOverSetOptions(m_bp_opts.GetBreakpointOptions());
9245a988416SJim Ingham           }
9255a988416SJim Ingham         }
9265a988416SJim Ingham       }
9275a988416SJim Ingham     }
9285a988416SJim Ingham 
9295a988416SJim Ingham     return result.Succeeded();
9305a988416SJim Ingham   }
9315a988416SJim Ingham 
9325a988416SJim Ingham private:
933b842f2ecSJim Ingham   BreakpointOptionGroup m_bp_opts;
934b842f2ecSJim Ingham   BreakpointDummyOptionGroup m_dummy_opts;
935b842f2ecSJim Ingham   OptionGroupOptions m_options;
9365a988416SJim Ingham };
9375a988416SJim Ingham 
9385a988416SJim Ingham // CommandObjectBreakpointEnable
9395a988416SJim Ingham #pragma mark Enable
9405a988416SJim Ingham 
941b9c1b51eSKate Stone class CommandObjectBreakpointEnable : public CommandObjectParsed {
9425a988416SJim Ingham public:
943b9c1b51eSKate Stone   CommandObjectBreakpointEnable(CommandInterpreter &interpreter)
944b9c1b51eSKate Stone       : CommandObjectParsed(interpreter, "enable",
945b9c1b51eSKate Stone                             "Enable the specified disabled breakpoint(s). If "
946b9c1b51eSKate Stone                             "no breakpoints are specified, enable all of them.",
947b9c1b51eSKate Stone                             nullptr) {
9485a988416SJim Ingham     CommandArgumentEntry arg;
949b9c1b51eSKate Stone     CommandObject::AddIDsArgumentData(arg, eArgTypeBreakpointID,
950b9c1b51eSKate Stone                                       eArgTypeBreakpointIDRange);
951b9c1b51eSKate Stone     // Add the entry for the first argument for this command to the object's
952b9c1b51eSKate Stone     // arguments vector.
9535a988416SJim Ingham     m_arguments.push_back(arg);
9545a988416SJim Ingham   }
9555a988416SJim Ingham 
9569e85e5a8SEugene Zelenko   ~CommandObjectBreakpointEnable() override = default;
9575a988416SJim Ingham 
9585a988416SJim Ingham protected:
959b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
960893c932aSJim Ingham     Target *target = GetSelectedOrDummyTarget();
961b9c1b51eSKate Stone     if (target == nullptr) {
9625a988416SJim Ingham       result.AppendError("Invalid target.  No existing target or breakpoints.");
9635a988416SJim Ingham       result.SetStatus(eReturnStatusFailed);
9645a988416SJim Ingham       return false;
9655a988416SJim Ingham     }
9665a988416SJim Ingham 
967bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
968bb19a13cSSaleem Abdulrasool     target->GetBreakpointList().GetListMutex(lock);
9695a988416SJim Ingham 
9705a988416SJim Ingham     const BreakpointList &breakpoints = target->GetBreakpointList();
9715a988416SJim Ingham 
9725a988416SJim Ingham     size_t num_breakpoints = breakpoints.GetSize();
9735a988416SJim Ingham 
974b9c1b51eSKate Stone     if (num_breakpoints == 0) {
9755a988416SJim Ingham       result.AppendError("No breakpoints exist to be enabled.");
9765a988416SJim Ingham       result.SetStatus(eReturnStatusFailed);
9775a988416SJim Ingham       return false;
9785a988416SJim Ingham     }
9795a988416SJim Ingham 
98011eb9c64SZachary Turner     if (command.empty()) {
9815a988416SJim Ingham       // No breakpoint selected; enable all currently set breakpoints.
982b842f2ecSJim Ingham       target->EnableAllowedBreakpoints();
983b9c1b51eSKate Stone       result.AppendMessageWithFormat("All breakpoints enabled. (%" PRIu64
984b9c1b51eSKate Stone                                      " breakpoints)\n",
985b9c1b51eSKate Stone                                      (uint64_t)num_breakpoints);
9865a988416SJim Ingham       result.SetStatus(eReturnStatusSuccessFinishNoResult);
987b9c1b51eSKate Stone     } else {
9885a988416SJim Ingham       // Particular breakpoint selected; enable that breakpoint.
9895a988416SJim Ingham       BreakpointIDList valid_bp_ids;
990b9c1b51eSKate Stone       CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs(
991b842f2ecSJim Ingham           command, target, result, &valid_bp_ids,
992b842f2ecSJim Ingham           BreakpointName::Permissions::PermissionKinds::disablePerm);
9935a988416SJim Ingham 
994b9c1b51eSKate Stone       if (result.Succeeded()) {
9955a988416SJim Ingham         int enable_count = 0;
9965a988416SJim Ingham         int loc_count = 0;
9975a988416SJim Ingham         const size_t count = valid_bp_ids.GetSize();
998b9c1b51eSKate Stone         for (size_t i = 0; i < count; ++i) {
9995a988416SJim Ingham           BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i);
10005a988416SJim Ingham 
1001b9c1b51eSKate Stone           if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) {
1002b9c1b51eSKate Stone             Breakpoint *breakpoint =
1003b9c1b51eSKate Stone                 target->GetBreakpointByID(cur_bp_id.GetBreakpointID()).get();
1004b9c1b51eSKate Stone             if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID) {
1005b9c1b51eSKate Stone               BreakpointLocation *location =
1006b9c1b51eSKate Stone                   breakpoint->FindLocationByID(cur_bp_id.GetLocationID()).get();
1007b9c1b51eSKate Stone               if (location) {
10085a988416SJim Ingham                 location->SetEnabled(true);
10095a988416SJim Ingham                 ++loc_count;
10105a988416SJim Ingham               }
1011b9c1b51eSKate Stone             } else {
10125a988416SJim Ingham               breakpoint->SetEnabled(true);
10135a988416SJim Ingham               ++enable_count;
10145a988416SJim Ingham             }
10155a988416SJim Ingham           }
10165a988416SJim Ingham         }
1017b9c1b51eSKate Stone         result.AppendMessageWithFormat("%d breakpoints enabled.\n",
1018b9c1b51eSKate Stone                                        enable_count + loc_count);
10195a988416SJim Ingham         result.SetStatus(eReturnStatusSuccessFinishNoResult);
10205a988416SJim Ingham       }
10215a988416SJim Ingham     }
10225a988416SJim Ingham 
10235a988416SJim Ingham     return result.Succeeded();
10245a988416SJim Ingham   }
10255a988416SJim Ingham };
10265a988416SJim Ingham 
10275a988416SJim Ingham // CommandObjectBreakpointDisable
10285a988416SJim Ingham #pragma mark Disable
10295a988416SJim Ingham 
1030b9c1b51eSKate Stone class CommandObjectBreakpointDisable : public CommandObjectParsed {
10315a988416SJim Ingham public:
10327428a18cSKate Stone   CommandObjectBreakpointDisable(CommandInterpreter &interpreter)
1033b9c1b51eSKate Stone       : CommandObjectParsed(
1034b9c1b51eSKate Stone             interpreter, "breakpoint disable",
1035b9c1b51eSKate Stone             "Disable the specified breakpoint(s) without deleting "
10367428a18cSKate Stone             "them.  If none are specified, disable all "
10377428a18cSKate Stone             "breakpoints.",
1038b9c1b51eSKate Stone             nullptr) {
1039b9c1b51eSKate Stone     SetHelpLong(
1040b9c1b51eSKate Stone         "Disable the specified breakpoint(s) without deleting them.  \
10417428a18cSKate Stone If none are specified, disable all breakpoints."
10427428a18cSKate Stone         R"(
1043ea671fbdSKate Stone 
10447428a18cSKate Stone )"
10457428a18cSKate Stone         "Note: disabling a breakpoint will cause none of its locations to be hit \
10467428a18cSKate Stone regardless of whether individual locations are enabled or disabled.  After the sequence:"
10477428a18cSKate Stone         R"(
1048ea671fbdSKate Stone 
1049ea671fbdSKate Stone     (lldb) break disable 1
1050ea671fbdSKate Stone     (lldb) break enable 1.1
1051ea671fbdSKate Stone 
1052ea671fbdSKate Stone execution will NOT stop at location 1.1.  To achieve that, type:
1053ea671fbdSKate Stone 
1054ea671fbdSKate Stone     (lldb) break disable 1.*
1055ea671fbdSKate Stone     (lldb) break enable 1.1
1056ea671fbdSKate Stone 
10577428a18cSKate Stone )"
10587428a18cSKate Stone         "The first command disables all locations for breakpoint 1, \
10597428a18cSKate Stone the second re-enables the first location.");
1060b0fac509SJim Ingham 
10615a988416SJim Ingham     CommandArgumentEntry arg;
1062b9c1b51eSKate Stone     CommandObject::AddIDsArgumentData(arg, eArgTypeBreakpointID,
1063b9c1b51eSKate Stone                                       eArgTypeBreakpointIDRange);
1064b9c1b51eSKate Stone     // Add the entry for the first argument for this command to the object's
1065b9c1b51eSKate Stone     // arguments vector.
10665a988416SJim Ingham     m_arguments.push_back(arg);
10675a988416SJim Ingham   }
10685a988416SJim Ingham 
10699e85e5a8SEugene Zelenko   ~CommandObjectBreakpointDisable() override = default;
10705a988416SJim Ingham 
10715a988416SJim Ingham protected:
1072b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
1073893c932aSJim Ingham     Target *target = GetSelectedOrDummyTarget();
1074b9c1b51eSKate Stone     if (target == nullptr) {
10755a988416SJim Ingham       result.AppendError("Invalid target.  No existing target or breakpoints.");
10765a988416SJim Ingham       result.SetStatus(eReturnStatusFailed);
10775a988416SJim Ingham       return false;
10785a988416SJim Ingham     }
10795a988416SJim Ingham 
1080bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
1081bb19a13cSSaleem Abdulrasool     target->GetBreakpointList().GetListMutex(lock);
10825a988416SJim Ingham 
10835a988416SJim Ingham     const BreakpointList &breakpoints = target->GetBreakpointList();
10845a988416SJim Ingham     size_t num_breakpoints = breakpoints.GetSize();
10855a988416SJim Ingham 
1086b9c1b51eSKate Stone     if (num_breakpoints == 0) {
10875a988416SJim Ingham       result.AppendError("No breakpoints exist to be disabled.");
10885a988416SJim Ingham       result.SetStatus(eReturnStatusFailed);
10895a988416SJim Ingham       return false;
10905a988416SJim Ingham     }
10915a988416SJim Ingham 
109211eb9c64SZachary Turner     if (command.empty()) {
10935a988416SJim Ingham       // No breakpoint selected; disable all currently set breakpoints.
1094b842f2ecSJim Ingham       target->DisableAllowedBreakpoints();
1095b9c1b51eSKate Stone       result.AppendMessageWithFormat("All breakpoints disabled. (%" PRIu64
1096b9c1b51eSKate Stone                                      " breakpoints)\n",
1097b9c1b51eSKate Stone                                      (uint64_t)num_breakpoints);
10985a988416SJim Ingham       result.SetStatus(eReturnStatusSuccessFinishNoResult);
1099b9c1b51eSKate Stone     } else {
11005a988416SJim Ingham       // Particular breakpoint selected; disable that breakpoint.
11015a988416SJim Ingham       BreakpointIDList valid_bp_ids;
11025a988416SJim Ingham 
1103b9c1b51eSKate Stone       CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs(
1104b842f2ecSJim Ingham           command, target, result, &valid_bp_ids,
1105b842f2ecSJim Ingham           BreakpointName::Permissions::PermissionKinds::disablePerm);
11065a988416SJim Ingham 
1107b9c1b51eSKate Stone       if (result.Succeeded()) {
11085a988416SJim Ingham         int disable_count = 0;
11095a988416SJim Ingham         int loc_count = 0;
11105a988416SJim Ingham         const size_t count = valid_bp_ids.GetSize();
1111b9c1b51eSKate Stone         for (size_t i = 0; i < count; ++i) {
11125a988416SJim Ingham           BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i);
11135a988416SJim Ingham 
1114b9c1b51eSKate Stone           if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) {
1115b9c1b51eSKate Stone             Breakpoint *breakpoint =
1116b9c1b51eSKate Stone                 target->GetBreakpointByID(cur_bp_id.GetBreakpointID()).get();
1117b9c1b51eSKate Stone             if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID) {
1118b9c1b51eSKate Stone               BreakpointLocation *location =
1119b9c1b51eSKate Stone                   breakpoint->FindLocationByID(cur_bp_id.GetLocationID()).get();
1120b9c1b51eSKate Stone               if (location) {
11215a988416SJim Ingham                 location->SetEnabled(false);
11225a988416SJim Ingham                 ++loc_count;
11235a988416SJim Ingham               }
1124b9c1b51eSKate Stone             } else {
11255a988416SJim Ingham               breakpoint->SetEnabled(false);
11265a988416SJim Ingham               ++disable_count;
11275a988416SJim Ingham             }
11285a988416SJim Ingham           }
11295a988416SJim Ingham         }
1130b9c1b51eSKate Stone         result.AppendMessageWithFormat("%d breakpoints disabled.\n",
1131b9c1b51eSKate Stone                                        disable_count + loc_count);
11325a988416SJim Ingham         result.SetStatus(eReturnStatusSuccessFinishNoResult);
11335a988416SJim Ingham       }
11345a988416SJim Ingham     }
11355a988416SJim Ingham 
11365a988416SJim Ingham     return result.Succeeded();
11375a988416SJim Ingham   }
11385a988416SJim Ingham };
11395a988416SJim Ingham 
11405a988416SJim Ingham // CommandObjectBreakpointList
11411f0f5b5bSZachary Turner 
11421f0f5b5bSZachary Turner #pragma mark List::CommandOptions
11436f4fb4e7SRaphael Isemann #define LLDB_OPTIONS_breakpoint_list
1144c5a2d747SRaphael Isemann #include "CommandOptions.inc"
11451f0f5b5bSZachary Turner 
11465a988416SJim Ingham #pragma mark List
11475a988416SJim Ingham 
1148b9c1b51eSKate Stone class CommandObjectBreakpointList : public CommandObjectParsed {
11495a988416SJim Ingham public:
1150b9c1b51eSKate Stone   CommandObjectBreakpointList(CommandInterpreter &interpreter)
1151b9c1b51eSKate Stone       : CommandObjectParsed(
1152b9c1b51eSKate Stone             interpreter, "breakpoint list",
11535a988416SJim Ingham             "List some or all breakpoints at configurable levels of detail.",
11549e85e5a8SEugene Zelenko             nullptr),
1155b9c1b51eSKate Stone         m_options() {
11565a988416SJim Ingham     CommandArgumentEntry arg;
11575a988416SJim Ingham     CommandArgumentData bp_id_arg;
11585a988416SJim Ingham 
11595a988416SJim Ingham     // Define the first (and only) variant of this arg.
11605a988416SJim Ingham     bp_id_arg.arg_type = eArgTypeBreakpointID;
11615a988416SJim Ingham     bp_id_arg.arg_repetition = eArgRepeatOptional;
11625a988416SJim Ingham 
1163b9c1b51eSKate Stone     // There is only one variant this argument could be; put it into the
1164b9c1b51eSKate Stone     // argument entry.
11655a988416SJim Ingham     arg.push_back(bp_id_arg);
11665a988416SJim Ingham 
11675a988416SJim Ingham     // Push the data for the first argument into the m_arguments vector.
11685a988416SJim Ingham     m_arguments.push_back(arg);
11695a988416SJim Ingham   }
11705a988416SJim Ingham 
11719e85e5a8SEugene Zelenko   ~CommandObjectBreakpointList() override = default;
11725a988416SJim Ingham 
1173b9c1b51eSKate Stone   Options *GetOptions() override { return &m_options; }
11745a988416SJim Ingham 
1175b9c1b51eSKate Stone   class CommandOptions : public Options {
11765a988416SJim Ingham   public:
1177b9c1b51eSKate Stone     CommandOptions()
1178b9c1b51eSKate Stone         : Options(), m_level(lldb::eDescriptionLevelBrief), m_use_dummy(false) {
11795a988416SJim Ingham     }
11805a988416SJim Ingham 
11819e85e5a8SEugene Zelenko     ~CommandOptions() override = default;
11825a988416SJim Ingham 
118397206d57SZachary Turner     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1184b9c1b51eSKate Stone                           ExecutionContext *execution_context) override {
118597206d57SZachary Turner       Status error;
11863bcdfc0eSGreg Clayton       const int short_option = m_getopt_table[option_idx].val;
11875a988416SJim Ingham 
1188b9c1b51eSKate Stone       switch (short_option) {
11895a988416SJim Ingham       case 'b':
11905a988416SJim Ingham         m_level = lldb::eDescriptionLevelBrief;
11915a988416SJim Ingham         break;
119233df7cd3SJim Ingham       case 'D':
119333df7cd3SJim Ingham         m_use_dummy = true;
119433df7cd3SJim Ingham         break;
11955a988416SJim Ingham       case 'f':
11965a988416SJim Ingham         m_level = lldb::eDescriptionLevelFull;
11975a988416SJim Ingham         break;
11985a988416SJim Ingham       case 'v':
11995a988416SJim Ingham         m_level = lldb::eDescriptionLevelVerbose;
12005a988416SJim Ingham         break;
12015a988416SJim Ingham       case 'i':
12025a988416SJim Ingham         m_internal = true;
12035a988416SJim Ingham         break;
12045a988416SJim Ingham       default:
1205*36162014SRaphael Isemann         llvm_unreachable("Unimplemented option");
12065a988416SJim Ingham       }
12075a988416SJim Ingham 
12085a988416SJim Ingham       return error;
12095a988416SJim Ingham     }
12105a988416SJim Ingham 
1211b9c1b51eSKate Stone     void OptionParsingStarting(ExecutionContext *execution_context) override {
12125a988416SJim Ingham       m_level = lldb::eDescriptionLevelFull;
12135a988416SJim Ingham       m_internal = false;
121433df7cd3SJim Ingham       m_use_dummy = false;
12155a988416SJim Ingham     }
12165a988416SJim Ingham 
12171f0f5b5bSZachary Turner     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
121870602439SZachary Turner       return llvm::makeArrayRef(g_breakpoint_list_options);
12191f0f5b5bSZachary Turner     }
12205a988416SJim Ingham 
12215a988416SJim Ingham     // Instance variables to hold the values for command options.
12225a988416SJim Ingham 
12235a988416SJim Ingham     lldb::DescriptionLevel m_level;
12245a988416SJim Ingham 
12255a988416SJim Ingham     bool m_internal;
122633df7cd3SJim Ingham     bool m_use_dummy;
12275a988416SJim Ingham   };
12285a988416SJim Ingham 
12295a988416SJim Ingham protected:
1230b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
123133df7cd3SJim Ingham     Target *target = GetSelectedOrDummyTarget(m_options.m_use_dummy);
123233df7cd3SJim Ingham 
1233b9c1b51eSKate Stone     if (target == nullptr) {
12345a988416SJim Ingham       result.AppendError("Invalid target. No current target or breakpoints.");
12355a988416SJim Ingham       result.SetStatus(eReturnStatusSuccessFinishNoResult);
12365a988416SJim Ingham       return true;
12375a988416SJim Ingham     }
12385a988416SJim Ingham 
1239b9c1b51eSKate Stone     const BreakpointList &breakpoints =
1240b9c1b51eSKate Stone         target->GetBreakpointList(m_options.m_internal);
1241bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
1242bb19a13cSSaleem Abdulrasool     target->GetBreakpointList(m_options.m_internal).GetListMutex(lock);
12435a988416SJim Ingham 
12445a988416SJim Ingham     size_t num_breakpoints = breakpoints.GetSize();
12455a988416SJim Ingham 
1246b9c1b51eSKate Stone     if (num_breakpoints == 0) {
12475a988416SJim Ingham       result.AppendMessage("No breakpoints currently set.");
12485a988416SJim Ingham       result.SetStatus(eReturnStatusSuccessFinishNoResult);
12495a988416SJim Ingham       return true;
12505a988416SJim Ingham     }
12515a988416SJim Ingham 
12525a988416SJim Ingham     Stream &output_stream = result.GetOutputStream();
12535a988416SJim Ingham 
125411eb9c64SZachary Turner     if (command.empty()) {
12555a988416SJim Ingham       // No breakpoint selected; show info about all currently set breakpoints.
12565a988416SJim Ingham       result.AppendMessage("Current breakpoints:");
1257b9c1b51eSKate Stone       for (size_t i = 0; i < num_breakpoints; ++i) {
12585a988416SJim Ingham         Breakpoint *breakpoint = breakpoints.GetBreakpointAtIndex(i).get();
1259b842f2ecSJim Ingham         if (breakpoint->AllowList())
1260b842f2ecSJim Ingham           AddBreakpointDescription(&output_stream, breakpoint,
1261b842f2ecSJim Ingham                                    m_options.m_level);
12625a988416SJim Ingham       }
12635a988416SJim Ingham       result.SetStatus(eReturnStatusSuccessFinishNoResult);
1264b9c1b51eSKate Stone     } else {
12655a988416SJim Ingham       // Particular breakpoints selected; show info about that breakpoint.
12665a988416SJim Ingham       BreakpointIDList valid_bp_ids;
1267b9c1b51eSKate Stone       CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs(
1268b842f2ecSJim Ingham           command, target, result, &valid_bp_ids,
1269b842f2ecSJim Ingham           BreakpointName::Permissions::PermissionKinds::listPerm);
12705a988416SJim Ingham 
1271b9c1b51eSKate Stone       if (result.Succeeded()) {
1272b9c1b51eSKate Stone         for (size_t i = 0; i < valid_bp_ids.GetSize(); ++i) {
12735a988416SJim Ingham           BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i);
1274b9c1b51eSKate Stone           Breakpoint *breakpoint =
1275b9c1b51eSKate Stone               target->GetBreakpointByID(cur_bp_id.GetBreakpointID()).get();
1276b9c1b51eSKate Stone           AddBreakpointDescription(&output_stream, breakpoint,
1277b9c1b51eSKate Stone                                    m_options.m_level);
12785a988416SJim Ingham         }
12795a988416SJim Ingham         result.SetStatus(eReturnStatusSuccessFinishNoResult);
1280b9c1b51eSKate Stone       } else {
12817428a18cSKate Stone         result.AppendError("Invalid breakpoint ID.");
12825a988416SJim Ingham         result.SetStatus(eReturnStatusFailed);
12835a988416SJim Ingham       }
12845a988416SJim Ingham     }
12855a988416SJim Ingham 
12865a988416SJim Ingham     return result.Succeeded();
12875a988416SJim Ingham   }
12885a988416SJim Ingham 
12895a988416SJim Ingham private:
12905a988416SJim Ingham   CommandOptions m_options;
12915a988416SJim Ingham };
12925a988416SJim Ingham 
12935a988416SJim Ingham // CommandObjectBreakpointClear
12941f0f5b5bSZachary Turner #pragma mark Clear::CommandOptions
12951f0f5b5bSZachary Turner 
1296f94668e3SRaphael Isemann #define LLDB_OPTIONS_breakpoint_clear
1297f94668e3SRaphael Isemann #include "CommandOptions.inc"
12981f0f5b5bSZachary Turner 
12995a988416SJim Ingham #pragma mark Clear
13005a988416SJim Ingham 
1301b9c1b51eSKate Stone class CommandObjectBreakpointClear : public CommandObjectParsed {
13025a988416SJim Ingham public:
1303efe8e7e3SFangrui Song   enum BreakpointClearType { eClearTypeInvalid, eClearTypeFileAndLine };
13045a988416SJim Ingham 
13057428a18cSKate Stone   CommandObjectBreakpointClear(CommandInterpreter &interpreter)
13067428a18cSKate Stone       : CommandObjectParsed(interpreter, "breakpoint clear",
1307b9c1b51eSKate Stone                             "Delete or disable breakpoints matching the "
1308b9c1b51eSKate Stone                             "specified source file and line.",
13095a988416SJim Ingham                             "breakpoint clear <cmd-options>"),
1310b9c1b51eSKate Stone         m_options() {}
13115a988416SJim Ingham 
13129e85e5a8SEugene Zelenko   ~CommandObjectBreakpointClear() override = default;
13135a988416SJim Ingham 
1314b9c1b51eSKate Stone   Options *GetOptions() override { return &m_options; }
13155a988416SJim Ingham 
1316b9c1b51eSKate Stone   class CommandOptions : public Options {
13175a988416SJim Ingham   public:
1318b9c1b51eSKate Stone     CommandOptions() : Options(), m_filename(), m_line_num(0) {}
13195a988416SJim Ingham 
13209e85e5a8SEugene Zelenko     ~CommandOptions() override = default;
13215a988416SJim Ingham 
132297206d57SZachary Turner     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1323b9c1b51eSKate Stone                           ExecutionContext *execution_context) override {
132497206d57SZachary Turner       Status error;
13253bcdfc0eSGreg Clayton       const int short_option = m_getopt_table[option_idx].val;
13265a988416SJim Ingham 
1327b9c1b51eSKate Stone       switch (short_option) {
13285a988416SJim Ingham       case 'f':
13295a988416SJim Ingham         m_filename.assign(option_arg);
13305a988416SJim Ingham         break;
13315a988416SJim Ingham 
13325a988416SJim Ingham       case 'l':
1333fe11483bSZachary Turner         option_arg.getAsInteger(0, m_line_num);
13345a988416SJim Ingham         break;
13355a988416SJim Ingham 
13365a988416SJim Ingham       default:
1337*36162014SRaphael Isemann         llvm_unreachable("Unimplemented option");
13385a988416SJim Ingham       }
13395a988416SJim Ingham 
13405a988416SJim Ingham       return error;
13415a988416SJim Ingham     }
13425a988416SJim Ingham 
1343b9c1b51eSKate Stone     void OptionParsingStarting(ExecutionContext *execution_context) override {
13445a988416SJim Ingham       m_filename.clear();
13455a988416SJim Ingham       m_line_num = 0;
13465a988416SJim Ingham     }
13475a988416SJim Ingham 
13481f0f5b5bSZachary Turner     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
134970602439SZachary Turner       return llvm::makeArrayRef(g_breakpoint_clear_options);
13501f0f5b5bSZachary Turner     }
13515a988416SJim Ingham 
13525a988416SJim Ingham     // Instance variables to hold the values for command options.
13535a988416SJim Ingham 
13545a988416SJim Ingham     std::string m_filename;
13555a988416SJim Ingham     uint32_t m_line_num;
13565a988416SJim Ingham   };
13575a988416SJim Ingham 
13585a988416SJim Ingham protected:
1359b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
1360893c932aSJim Ingham     Target *target = GetSelectedOrDummyTarget();
1361b9c1b51eSKate Stone     if (target == nullptr) {
13625a988416SJim Ingham       result.AppendError("Invalid target. No existing target or breakpoints.");
13635a988416SJim Ingham       result.SetStatus(eReturnStatusFailed);
13645a988416SJim Ingham       return false;
13655a988416SJim Ingham     }
13665a988416SJim Ingham 
136705097246SAdrian Prantl     // The following are the various types of breakpoints that could be
136805097246SAdrian Prantl     // cleared:
13695a988416SJim Ingham     //   1). -f -l (clearing breakpoint by source location)
13705a988416SJim Ingham 
13715a988416SJim Ingham     BreakpointClearType break_type = eClearTypeInvalid;
13725a988416SJim Ingham 
13735a988416SJim Ingham     if (m_options.m_line_num != 0)
13745a988416SJim Ingham       break_type = eClearTypeFileAndLine;
13755a988416SJim Ingham 
1376bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
1377bb19a13cSSaleem Abdulrasool     target->GetBreakpointList().GetListMutex(lock);
13785a988416SJim Ingham 
13795a988416SJim Ingham     BreakpointList &breakpoints = target->GetBreakpointList();
13805a988416SJim Ingham     size_t num_breakpoints = breakpoints.GetSize();
13815a988416SJim Ingham 
13825a988416SJim Ingham     // Early return if there's no breakpoint at all.
1383b9c1b51eSKate Stone     if (num_breakpoints == 0) {
13845a988416SJim Ingham       result.AppendError("Breakpoint clear: No breakpoint cleared.");
13855a988416SJim Ingham       result.SetStatus(eReturnStatusFailed);
13865a988416SJim Ingham       return result.Succeeded();
13875a988416SJim Ingham     }
13885a988416SJim Ingham 
13895a988416SJim Ingham     // Find matching breakpoints and delete them.
13905a988416SJim Ingham 
13915a988416SJim Ingham     // First create a copy of all the IDs.
13925a988416SJim Ingham     std::vector<break_id_t> BreakIDs;
13935a988416SJim Ingham     for (size_t i = 0; i < num_breakpoints; ++i)
13949e85e5a8SEugene Zelenko       BreakIDs.push_back(breakpoints.GetBreakpointAtIndex(i)->GetID());
13955a988416SJim Ingham 
13965a988416SJim Ingham     int num_cleared = 0;
13975a988416SJim Ingham     StreamString ss;
1398b9c1b51eSKate Stone     switch (break_type) {
13995a988416SJim Ingham     case eClearTypeFileAndLine: // Breakpoint by source position
14005a988416SJim Ingham     {
14015a988416SJim Ingham       const ConstString filename(m_options.m_filename.c_str());
14025a988416SJim Ingham       BreakpointLocationCollection loc_coll;
14035a988416SJim Ingham 
1404b9c1b51eSKate Stone       for (size_t i = 0; i < num_breakpoints; ++i) {
14055a988416SJim Ingham         Breakpoint *bp = breakpoints.FindBreakpointByID(BreakIDs[i]).get();
14065a988416SJim Ingham 
1407b9c1b51eSKate Stone         if (bp->GetMatchingFileLine(filename, m_options.m_line_num, loc_coll)) {
1408b9c1b51eSKate Stone           // If the collection size is 0, it's a full match and we can just
1409b9c1b51eSKate Stone           // remove the breakpoint.
1410b9c1b51eSKate Stone           if (loc_coll.GetSize() == 0) {
14115a988416SJim Ingham             bp->GetDescription(&ss, lldb::eDescriptionLevelBrief);
14125a988416SJim Ingham             ss.EOL();
14135a988416SJim Ingham             target->RemoveBreakpointByID(bp->GetID());
14145a988416SJim Ingham             ++num_cleared;
14155a988416SJim Ingham           }
14165a988416SJim Ingham         }
14175a988416SJim Ingham       }
1418b9c1b51eSKate Stone     } break;
14195a988416SJim Ingham 
14205a988416SJim Ingham     default:
14215a988416SJim Ingham       break;
14225a988416SJim Ingham     }
14235a988416SJim Ingham 
1424b9c1b51eSKate Stone     if (num_cleared > 0) {
14255a988416SJim Ingham       Stream &output_stream = result.GetOutputStream();
14265a988416SJim Ingham       output_stream.Printf("%d breakpoints cleared:\n", num_cleared);
1427c156427dSZachary Turner       output_stream << ss.GetString();
14285a988416SJim Ingham       output_stream.EOL();
14295a988416SJim Ingham       result.SetStatus(eReturnStatusSuccessFinishNoResult);
1430b9c1b51eSKate Stone     } else {
14315a988416SJim Ingham       result.AppendError("Breakpoint clear: No breakpoint cleared.");
14325a988416SJim Ingham       result.SetStatus(eReturnStatusFailed);
14335a988416SJim Ingham     }
14345a988416SJim Ingham 
14355a988416SJim Ingham     return result.Succeeded();
14365a988416SJim Ingham   }
14375a988416SJim Ingham 
14385a988416SJim Ingham private:
14395a988416SJim Ingham   CommandOptions m_options;
14405a988416SJim Ingham };
14415a988416SJim Ingham 
14425a988416SJim Ingham // CommandObjectBreakpointDelete
1443f94668e3SRaphael Isemann #define LLDB_OPTIONS_breakpoint_delete
1444f94668e3SRaphael Isemann #include "CommandOptions.inc"
14451f0f5b5bSZachary Turner 
14465a988416SJim Ingham #pragma mark Delete
14475a988416SJim Ingham 
1448b9c1b51eSKate Stone class CommandObjectBreakpointDelete : public CommandObjectParsed {
14495a988416SJim Ingham public:
1450b9c1b51eSKate Stone   CommandObjectBreakpointDelete(CommandInterpreter &interpreter)
1451b9c1b51eSKate Stone       : CommandObjectParsed(interpreter, "breakpoint delete",
1452b9c1b51eSKate Stone                             "Delete the specified breakpoint(s).  If no "
1453b9c1b51eSKate Stone                             "breakpoints are specified, delete them all.",
14549e85e5a8SEugene Zelenko                             nullptr),
1455b9c1b51eSKate Stone         m_options() {
14565a988416SJim Ingham     CommandArgumentEntry arg;
1457b9c1b51eSKate Stone     CommandObject::AddIDsArgumentData(arg, eArgTypeBreakpointID,
1458b9c1b51eSKate Stone                                       eArgTypeBreakpointIDRange);
1459b9c1b51eSKate Stone     // Add the entry for the first argument for this command to the object's
1460b9c1b51eSKate Stone     // arguments vector.
14615a988416SJim Ingham     m_arguments.push_back(arg);
14625a988416SJim Ingham   }
14635a988416SJim Ingham 
14649e85e5a8SEugene Zelenko   ~CommandObjectBreakpointDelete() override = default;
14655a988416SJim Ingham 
1466b9c1b51eSKate Stone   Options *GetOptions() override { return &m_options; }
146733df7cd3SJim Ingham 
1468b9c1b51eSKate Stone   class CommandOptions : public Options {
146933df7cd3SJim Ingham   public:
1470b9c1b51eSKate Stone     CommandOptions() : Options(), m_use_dummy(false), m_force(false) {}
147133df7cd3SJim Ingham 
14729e85e5a8SEugene Zelenko     ~CommandOptions() override = default;
147333df7cd3SJim Ingham 
147497206d57SZachary Turner     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1475b9c1b51eSKate Stone                           ExecutionContext *execution_context) override {
147697206d57SZachary Turner       Status error;
147733df7cd3SJim Ingham       const int short_option = m_getopt_table[option_idx].val;
147833df7cd3SJim Ingham 
1479b9c1b51eSKate Stone       switch (short_option) {
148033df7cd3SJim Ingham       case 'f':
148133df7cd3SJim Ingham         m_force = true;
148233df7cd3SJim Ingham         break;
148333df7cd3SJim Ingham 
148433df7cd3SJim Ingham       case 'D':
148533df7cd3SJim Ingham         m_use_dummy = true;
148633df7cd3SJim Ingham         break;
148733df7cd3SJim Ingham 
148833df7cd3SJim Ingham       default:
1489*36162014SRaphael Isemann         llvm_unreachable("Unimplemented option");
149033df7cd3SJim Ingham       }
149133df7cd3SJim Ingham 
149233df7cd3SJim Ingham       return error;
149333df7cd3SJim Ingham     }
149433df7cd3SJim Ingham 
1495b9c1b51eSKate Stone     void OptionParsingStarting(ExecutionContext *execution_context) override {
149633df7cd3SJim Ingham       m_use_dummy = false;
149733df7cd3SJim Ingham       m_force = false;
149833df7cd3SJim Ingham     }
149933df7cd3SJim Ingham 
15001f0f5b5bSZachary Turner     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
150170602439SZachary Turner       return llvm::makeArrayRef(g_breakpoint_delete_options);
15021f0f5b5bSZachary Turner     }
150333df7cd3SJim Ingham 
150433df7cd3SJim Ingham     // Instance variables to hold the values for command options.
150533df7cd3SJim Ingham     bool m_use_dummy;
150633df7cd3SJim Ingham     bool m_force;
150733df7cd3SJim Ingham   };
150833df7cd3SJim Ingham 
15095a988416SJim Ingham protected:
1510b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
151133df7cd3SJim Ingham     Target *target = GetSelectedOrDummyTarget(m_options.m_use_dummy);
151233df7cd3SJim Ingham 
1513b9c1b51eSKate Stone     if (target == nullptr) {
15145a988416SJim Ingham       result.AppendError("Invalid target. No existing target or breakpoints.");
15155a988416SJim Ingham       result.SetStatus(eReturnStatusFailed);
15165a988416SJim Ingham       return false;
15175a988416SJim Ingham     }
15185a988416SJim Ingham 
1519bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
1520bb19a13cSSaleem Abdulrasool     target->GetBreakpointList().GetListMutex(lock);
15215a988416SJim Ingham 
15225a988416SJim Ingham     const BreakpointList &breakpoints = target->GetBreakpointList();
15235a988416SJim Ingham 
15245a988416SJim Ingham     size_t num_breakpoints = breakpoints.GetSize();
15255a988416SJim Ingham 
1526b9c1b51eSKate Stone     if (num_breakpoints == 0) {
15275a988416SJim Ingham       result.AppendError("No breakpoints exist to be deleted.");
15285a988416SJim Ingham       result.SetStatus(eReturnStatusFailed);
15295a988416SJim Ingham       return false;
15305a988416SJim Ingham     }
15315a988416SJim Ingham 
153211eb9c64SZachary Turner     if (command.empty()) {
1533b9c1b51eSKate Stone       if (!m_options.m_force &&
1534b9c1b51eSKate Stone           !m_interpreter.Confirm(
1535b9c1b51eSKate Stone               "About to delete all breakpoints, do you want to do that?",
1536b9c1b51eSKate Stone               true)) {
15375a988416SJim Ingham         result.AppendMessage("Operation cancelled...");
1538b9c1b51eSKate Stone       } else {
1539b842f2ecSJim Ingham         target->RemoveAllowedBreakpoints();
1540b9c1b51eSKate Stone         result.AppendMessageWithFormat(
1541b9c1b51eSKate Stone             "All breakpoints removed. (%" PRIu64 " breakpoint%s)\n",
1542b9c1b51eSKate Stone             (uint64_t)num_breakpoints, num_breakpoints > 1 ? "s" : "");
15435a988416SJim Ingham       }
15445a988416SJim Ingham       result.SetStatus(eReturnStatusSuccessFinishNoResult);
1545b9c1b51eSKate Stone     } else {
15465a988416SJim Ingham       // Particular breakpoint selected; disable that breakpoint.
15475a988416SJim Ingham       BreakpointIDList valid_bp_ids;
1548b9c1b51eSKate Stone       CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs(
1549b842f2ecSJim Ingham           command, target, result, &valid_bp_ids,
1550b842f2ecSJim Ingham           BreakpointName::Permissions::PermissionKinds::deletePerm);
15515a988416SJim Ingham 
1552b9c1b51eSKate Stone       if (result.Succeeded()) {
15535a988416SJim Ingham         int delete_count = 0;
15545a988416SJim Ingham         int disable_count = 0;
15555a988416SJim Ingham         const size_t count = valid_bp_ids.GetSize();
1556b9c1b51eSKate Stone         for (size_t i = 0; i < count; ++i) {
15575a988416SJim Ingham           BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i);
15585a988416SJim Ingham 
1559b9c1b51eSKate Stone           if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) {
1560b9c1b51eSKate Stone             if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID) {
1561b9c1b51eSKate Stone               Breakpoint *breakpoint =
1562b9c1b51eSKate Stone                   target->GetBreakpointByID(cur_bp_id.GetBreakpointID()).get();
1563b9c1b51eSKate Stone               BreakpointLocation *location =
1564b9c1b51eSKate Stone                   breakpoint->FindLocationByID(cur_bp_id.GetLocationID()).get();
1565b9c1b51eSKate Stone               // It makes no sense to try to delete individual locations, so we
1566b9c1b51eSKate Stone               // disable them instead.
1567b9c1b51eSKate Stone               if (location) {
15685a988416SJim Ingham                 location->SetEnabled(false);
15695a988416SJim Ingham                 ++disable_count;
15705a988416SJim Ingham               }
1571b9c1b51eSKate Stone             } else {
15725a988416SJim Ingham               target->RemoveBreakpointByID(cur_bp_id.GetBreakpointID());
15735a988416SJim Ingham               ++delete_count;
15745a988416SJim Ingham             }
15755a988416SJim Ingham           }
15765a988416SJim Ingham         }
1577b9c1b51eSKate Stone         result.AppendMessageWithFormat(
1578b9c1b51eSKate Stone             "%d breakpoints deleted; %d breakpoint locations disabled.\n",
15795a988416SJim Ingham             delete_count, disable_count);
15805a988416SJim Ingham         result.SetStatus(eReturnStatusSuccessFinishNoResult);
15815a988416SJim Ingham       }
15825a988416SJim Ingham     }
15835a988416SJim Ingham     return result.Succeeded();
15845a988416SJim Ingham   }
15859e85e5a8SEugene Zelenko 
158633df7cd3SJim Ingham private:
158733df7cd3SJim Ingham   CommandOptions m_options;
158833df7cd3SJim Ingham };
158933df7cd3SJim Ingham 
15905e09c8c3SJim Ingham // CommandObjectBreakpointName
1591f94668e3SRaphael Isemann #define LLDB_OPTIONS_breakpoint_name
1592f94668e3SRaphael Isemann #include "CommandOptions.inc"
1593bd68a052SRaphael Isemann 
1594b9c1b51eSKate Stone class BreakpointNameOptionGroup : public OptionGroup {
15955e09c8c3SJim Ingham public:
1596b9c1b51eSKate Stone   BreakpointNameOptionGroup()
1597b9c1b51eSKate Stone       : OptionGroup(), m_breakpoint(LLDB_INVALID_BREAK_ID), m_use_dummy(false) {
15985e09c8c3SJim Ingham   }
15995e09c8c3SJim Ingham 
16009e85e5a8SEugene Zelenko   ~BreakpointNameOptionGroup() override = default;
16015e09c8c3SJim Ingham 
16021f0f5b5bSZachary Turner   llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
160370602439SZachary Turner     return llvm::makeArrayRef(g_breakpoint_name_options);
16045e09c8c3SJim Ingham   }
16055e09c8c3SJim Ingham 
160697206d57SZachary Turner   Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1607b9c1b51eSKate Stone                         ExecutionContext *execution_context) override {
160897206d57SZachary Turner     Status error;
16095e09c8c3SJim Ingham     const int short_option = g_breakpoint_name_options[option_idx].short_option;
16105e09c8c3SJim Ingham 
1611b9c1b51eSKate Stone     switch (short_option) {
16125e09c8c3SJim Ingham     case 'N':
1613fe11483bSZachary Turner       if (BreakpointID::StringIsBreakpointName(option_arg, error) &&
1614b9c1b51eSKate Stone           error.Success())
1615fe11483bSZachary Turner         m_name.SetValueFromString(option_arg);
16165e09c8c3SJim Ingham       break;
16175e09c8c3SJim Ingham     case 'B':
1618fe11483bSZachary Turner       if (m_breakpoint.SetValueFromString(option_arg).Fail())
1619b9c1b51eSKate Stone         error.SetErrorStringWithFormat(
16208cef4b0bSZachary Turner             "unrecognized value \"%s\" for breakpoint",
1621fe11483bSZachary Turner             option_arg.str().c_str());
16225e09c8c3SJim Ingham       break;
16235e09c8c3SJim Ingham     case 'D':
1624fe11483bSZachary Turner       if (m_use_dummy.SetValueFromString(option_arg).Fail())
1625b9c1b51eSKate Stone         error.SetErrorStringWithFormat(
16268cef4b0bSZachary Turner             "unrecognized value \"%s\" for use-dummy",
1627fe11483bSZachary Turner             option_arg.str().c_str());
16285e09c8c3SJim Ingham       break;
1629e9632ebaSJim Ingham     case 'H':
1630e9632ebaSJim Ingham       m_help_string.SetValueFromString(option_arg);
1631e9632ebaSJim Ingham       break;
16325e09c8c3SJim Ingham 
16335e09c8c3SJim Ingham     default:
1634*36162014SRaphael Isemann       llvm_unreachable("Unimplemented option");
16355e09c8c3SJim Ingham     }
16365e09c8c3SJim Ingham     return error;
16375e09c8c3SJim Ingham   }
16385e09c8c3SJim Ingham 
1639b9c1b51eSKate Stone   void OptionParsingStarting(ExecutionContext *execution_context) override {
16405e09c8c3SJim Ingham     m_name.Clear();
16415e09c8c3SJim Ingham     m_breakpoint.Clear();
16425e09c8c3SJim Ingham     m_use_dummy.Clear();
16435e09c8c3SJim Ingham     m_use_dummy.SetDefaultValue(false);
1644e9632ebaSJim Ingham     m_help_string.Clear();
16455e09c8c3SJim Ingham   }
16465e09c8c3SJim Ingham 
16475e09c8c3SJim Ingham   OptionValueString m_name;
16485e09c8c3SJim Ingham   OptionValueUInt64 m_breakpoint;
16495e09c8c3SJim Ingham   OptionValueBoolean m_use_dummy;
1650e9632ebaSJim Ingham   OptionValueString m_help_string;
16515e09c8c3SJim Ingham };
16525e09c8c3SJim Ingham 
1653f94668e3SRaphael Isemann #define LLDB_OPTIONS_breakpoint_access
1654f94668e3SRaphael Isemann #include "CommandOptions.inc"
1655b842f2ecSJim Ingham 
16568fe53c49STatyana Krasnukha class BreakpointAccessOptionGroup : public OptionGroup {
1657b842f2ecSJim Ingham public:
16588fe53c49STatyana Krasnukha   BreakpointAccessOptionGroup() : OptionGroup() {}
1659b842f2ecSJim Ingham 
1660b842f2ecSJim Ingham   ~BreakpointAccessOptionGroup() override = default;
1661b842f2ecSJim Ingham 
1662b842f2ecSJim Ingham   llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
1663b842f2ecSJim Ingham     return llvm::makeArrayRef(g_breakpoint_access_options);
1664b842f2ecSJim Ingham   }
1665b842f2ecSJim Ingham   Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1666b842f2ecSJim Ingham                         ExecutionContext *execution_context) override {
1667b842f2ecSJim Ingham     Status error;
1668b842f2ecSJim Ingham     const int short_option
1669b842f2ecSJim Ingham         = g_breakpoint_access_options[option_idx].short_option;
1670b842f2ecSJim Ingham 
1671b842f2ecSJim Ingham     switch (short_option) {
1672b842f2ecSJim Ingham       case 'L': {
1673b842f2ecSJim Ingham         bool value, success;
167447cbf4a0SPavel Labath         value = OptionArgParser::ToBoolean(option_arg, false, &success);
1675b842f2ecSJim Ingham         if (success) {
1676b842f2ecSJim Ingham           m_permissions.SetAllowList(value);
1677b842f2ecSJim Ingham         } else
1678b842f2ecSJim Ingham           error.SetErrorStringWithFormat(
1679b842f2ecSJim Ingham               "invalid boolean value '%s' passed for -L option",
1680b842f2ecSJim Ingham               option_arg.str().c_str());
1681b842f2ecSJim Ingham       } break;
1682b842f2ecSJim Ingham       case 'A': {
1683b842f2ecSJim Ingham         bool value, success;
168447cbf4a0SPavel Labath         value = OptionArgParser::ToBoolean(option_arg, false, &success);
1685b842f2ecSJim Ingham         if (success) {
1686b842f2ecSJim Ingham           m_permissions.SetAllowDisable(value);
1687b842f2ecSJim Ingham         } else
1688b842f2ecSJim Ingham           error.SetErrorStringWithFormat(
1689b842f2ecSJim Ingham               "invalid boolean value '%s' passed for -L option",
1690b842f2ecSJim Ingham               option_arg.str().c_str());
1691b842f2ecSJim Ingham       } break;
1692b842f2ecSJim Ingham       case 'D': {
1693b842f2ecSJim Ingham         bool value, success;
169447cbf4a0SPavel Labath         value = OptionArgParser::ToBoolean(option_arg, false, &success);
1695b842f2ecSJim Ingham         if (success) {
1696b842f2ecSJim Ingham           m_permissions.SetAllowDelete(value);
1697b842f2ecSJim Ingham         } else
1698b842f2ecSJim Ingham           error.SetErrorStringWithFormat(
1699b842f2ecSJim Ingham               "invalid boolean value '%s' passed for -L option",
1700b842f2ecSJim Ingham               option_arg.str().c_str());
1701b842f2ecSJim Ingham       } break;
1702*36162014SRaphael Isemann       default:
1703*36162014SRaphael Isemann         llvm_unreachable("Unimplemented option");
1704b842f2ecSJim Ingham     }
1705b842f2ecSJim Ingham 
1706b842f2ecSJim Ingham     return error;
1707b842f2ecSJim Ingham   }
1708b842f2ecSJim Ingham 
1709b842f2ecSJim Ingham   void OptionParsingStarting(ExecutionContext *execution_context) override {
1710b842f2ecSJim Ingham   }
1711b842f2ecSJim Ingham 
1712b842f2ecSJim Ingham   const BreakpointName::Permissions &GetPermissions() const
1713b842f2ecSJim Ingham   {
1714b842f2ecSJim Ingham     return m_permissions;
1715b842f2ecSJim Ingham   }
1716b842f2ecSJim Ingham   BreakpointName::Permissions m_permissions;
1717b842f2ecSJim Ingham };
1718b842f2ecSJim Ingham 
1719b842f2ecSJim Ingham class CommandObjectBreakpointNameConfigure : public CommandObjectParsed {
1720b842f2ecSJim Ingham public:
1721b842f2ecSJim Ingham   CommandObjectBreakpointNameConfigure(CommandInterpreter &interpreter)
1722b842f2ecSJim Ingham       : CommandObjectParsed(
1723b842f2ecSJim Ingham             interpreter, "configure", "Configure the options for the breakpoint"
1724b842f2ecSJim Ingham             " name provided.  "
1725b842f2ecSJim Ingham             "If you provide a breakpoint id, the options will be copied from "
1726b842f2ecSJim Ingham             "the breakpoint, otherwise only the options specified will be set "
1727b842f2ecSJim Ingham             "on the name.",
1728b842f2ecSJim Ingham             "breakpoint name configure <command-options> "
1729b842f2ecSJim Ingham             "<breakpoint-name-list>"),
1730b842f2ecSJim Ingham         m_bp_opts(), m_option_group() {
1731b842f2ecSJim Ingham     // Create the first variant for the first (and only) argument for this
1732b842f2ecSJim Ingham     // command.
1733b842f2ecSJim Ingham     CommandArgumentEntry arg1;
1734b842f2ecSJim Ingham     CommandArgumentData id_arg;
1735b842f2ecSJim Ingham     id_arg.arg_type = eArgTypeBreakpointName;
1736b842f2ecSJim Ingham     id_arg.arg_repetition = eArgRepeatOptional;
1737b842f2ecSJim Ingham     arg1.push_back(id_arg);
1738b842f2ecSJim Ingham     m_arguments.push_back(arg1);
1739b842f2ecSJim Ingham 
1740b842f2ecSJim Ingham     m_option_group.Append(&m_bp_opts,
1741b842f2ecSJim Ingham                           LLDB_OPT_SET_ALL,
1742b842f2ecSJim Ingham                           LLDB_OPT_SET_1);
1743b842f2ecSJim Ingham     m_option_group.Append(&m_access_options,
1744b842f2ecSJim Ingham                           LLDB_OPT_SET_ALL,
1745b842f2ecSJim Ingham                           LLDB_OPT_SET_ALL);
1746e9632ebaSJim Ingham     m_option_group.Append(&m_bp_id,
1747e9632ebaSJim Ingham                           LLDB_OPT_SET_2|LLDB_OPT_SET_4,
1748e9632ebaSJim Ingham                           LLDB_OPT_SET_ALL);
1749b842f2ecSJim Ingham     m_option_group.Finalize();
1750b842f2ecSJim Ingham   }
1751b842f2ecSJim Ingham 
1752b842f2ecSJim Ingham   ~CommandObjectBreakpointNameConfigure() override = default;
1753b842f2ecSJim Ingham 
1754b842f2ecSJim Ingham   Options *GetOptions() override { return &m_option_group; }
1755b842f2ecSJim Ingham 
1756b842f2ecSJim Ingham protected:
1757b842f2ecSJim Ingham   bool DoExecute(Args &command, CommandReturnObject &result) override {
1758b842f2ecSJim Ingham 
1759b842f2ecSJim Ingham     const size_t argc = command.GetArgumentCount();
1760b842f2ecSJim Ingham     if (argc == 0) {
1761b842f2ecSJim Ingham       result.AppendError("No names provided.");
1762b842f2ecSJim Ingham       result.SetStatus(eReturnStatusFailed);
1763b842f2ecSJim Ingham       return false;
1764b842f2ecSJim Ingham     }
1765b842f2ecSJim Ingham 
1766b842f2ecSJim Ingham     Target *target =
1767b842f2ecSJim Ingham         GetSelectedOrDummyTarget(false);
1768b842f2ecSJim Ingham 
1769b842f2ecSJim Ingham     if (target == nullptr) {
1770b842f2ecSJim Ingham       result.AppendError("Invalid target. No existing target or breakpoints.");
1771b842f2ecSJim Ingham       result.SetStatus(eReturnStatusFailed);
1772b842f2ecSJim Ingham       return false;
1773b842f2ecSJim Ingham     }
1774b842f2ecSJim Ingham 
1775b842f2ecSJim Ingham     std::unique_lock<std::recursive_mutex> lock;
1776b842f2ecSJim Ingham     target->GetBreakpointList().GetListMutex(lock);
1777b842f2ecSJim Ingham 
1778b842f2ecSJim Ingham     // Make a pass through first to see that all the names are legal.
1779b842f2ecSJim Ingham     for (auto &entry : command.entries()) {
1780b842f2ecSJim Ingham       Status error;
1781b842f2ecSJim Ingham       if (!BreakpointID::StringIsBreakpointName(entry.ref, error))
1782b842f2ecSJim Ingham       {
1783b842f2ecSJim Ingham         result.AppendErrorWithFormat("Invalid breakpoint name: %s - %s",
1784b842f2ecSJim Ingham                                      entry.c_str(), error.AsCString());
1785b842f2ecSJim Ingham         result.SetStatus(eReturnStatusFailed);
1786b842f2ecSJim Ingham         return false;
1787b842f2ecSJim Ingham       }
1788b842f2ecSJim Ingham     }
178905097246SAdrian Prantl     // Now configure them, we already pre-checked the names so we don't need to
179005097246SAdrian Prantl     // check the error:
1791b842f2ecSJim Ingham     BreakpointSP bp_sp;
1792b842f2ecSJim Ingham     if (m_bp_id.m_breakpoint.OptionWasSet())
1793b842f2ecSJim Ingham     {
1794b842f2ecSJim Ingham       lldb::break_id_t bp_id = m_bp_id.m_breakpoint.GetUInt64Value();
1795b842f2ecSJim Ingham       bp_sp = target->GetBreakpointByID(bp_id);
1796b842f2ecSJim Ingham       if (!bp_sp)
1797b842f2ecSJim Ingham       {
1798b842f2ecSJim Ingham         result.AppendErrorWithFormatv("Could not find specified breakpoint {0}",
1799b842f2ecSJim Ingham                            bp_id);
1800b842f2ecSJim Ingham         result.SetStatus(eReturnStatusFailed);
1801b842f2ecSJim Ingham         return false;
1802b842f2ecSJim Ingham       }
1803b842f2ecSJim Ingham     }
1804b842f2ecSJim Ingham 
1805b842f2ecSJim Ingham     Status error;
1806b842f2ecSJim Ingham     for (auto &entry : command.entries()) {
1807b842f2ecSJim Ingham       ConstString name(entry.c_str());
1808b842f2ecSJim Ingham       BreakpointName *bp_name = target->FindBreakpointName(name, true, error);
1809b842f2ecSJim Ingham       if (!bp_name)
1810b842f2ecSJim Ingham         continue;
1811e9632ebaSJim Ingham       if (m_bp_id.m_help_string.OptionWasSet())
1812e9632ebaSJim Ingham         bp_name->SetHelp(m_bp_id.m_help_string.GetStringValue().str().c_str());
1813e9632ebaSJim Ingham 
1814b842f2ecSJim Ingham       if (bp_sp)
1815b842f2ecSJim Ingham         target->ConfigureBreakpointName(*bp_name,
1816b842f2ecSJim Ingham                                        *bp_sp->GetOptions(),
1817b842f2ecSJim Ingham                                        m_access_options.GetPermissions());
1818b842f2ecSJim Ingham       else
1819b842f2ecSJim Ingham         target->ConfigureBreakpointName(*bp_name,
1820b842f2ecSJim Ingham                                        m_bp_opts.GetBreakpointOptions(),
1821b842f2ecSJim Ingham                                        m_access_options.GetPermissions());
1822b842f2ecSJim Ingham     }
1823b842f2ecSJim Ingham     return true;
1824b842f2ecSJim Ingham   }
1825b842f2ecSJim Ingham 
1826b842f2ecSJim Ingham private:
1827b842f2ecSJim Ingham   BreakpointNameOptionGroup m_bp_id; // Only using the id part of this.
1828b842f2ecSJim Ingham   BreakpointOptionGroup m_bp_opts;
1829b842f2ecSJim Ingham   BreakpointAccessOptionGroup m_access_options;
1830b842f2ecSJim Ingham   OptionGroupOptions m_option_group;
1831b842f2ecSJim Ingham };
1832b842f2ecSJim Ingham 
1833b9c1b51eSKate Stone class CommandObjectBreakpointNameAdd : public CommandObjectParsed {
18345e09c8c3SJim Ingham public:
1835b9c1b51eSKate Stone   CommandObjectBreakpointNameAdd(CommandInterpreter &interpreter)
1836b9c1b51eSKate Stone       : CommandObjectParsed(
1837b9c1b51eSKate Stone             interpreter, "add", "Add a name to the breakpoints provided.",
18385e09c8c3SJim Ingham             "breakpoint name add <command-options> <breakpoint-id-list>"),
1839b9c1b51eSKate Stone         m_name_options(), m_option_group() {
1840b9c1b51eSKate Stone     // Create the first variant for the first (and only) argument for this
1841b9c1b51eSKate Stone     // command.
18425e09c8c3SJim Ingham     CommandArgumentEntry arg1;
18435e09c8c3SJim Ingham     CommandArgumentData id_arg;
18445e09c8c3SJim Ingham     id_arg.arg_type = eArgTypeBreakpointID;
18455e09c8c3SJim Ingham     id_arg.arg_repetition = eArgRepeatOptional;
18465e09c8c3SJim Ingham     arg1.push_back(id_arg);
18475e09c8c3SJim Ingham     m_arguments.push_back(arg1);
18485e09c8c3SJim Ingham 
18495e09c8c3SJim Ingham     m_option_group.Append(&m_name_options, LLDB_OPT_SET_1, LLDB_OPT_SET_ALL);
18505e09c8c3SJim Ingham     m_option_group.Finalize();
18515e09c8c3SJim Ingham   }
18525e09c8c3SJim Ingham 
18539e85e5a8SEugene Zelenko   ~CommandObjectBreakpointNameAdd() override = default;
18545e09c8c3SJim Ingham 
1855b9c1b51eSKate Stone   Options *GetOptions() override { return &m_option_group; }
18565e09c8c3SJim Ingham 
18575e09c8c3SJim Ingham protected:
1858b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
1859b9c1b51eSKate Stone     if (!m_name_options.m_name.OptionWasSet()) {
18605e09c8c3SJim Ingham       result.SetError("No name option provided.");
18615e09c8c3SJim Ingham       return false;
18625e09c8c3SJim Ingham     }
18635e09c8c3SJim Ingham 
1864b9c1b51eSKate Stone     Target *target =
1865b9c1b51eSKate Stone         GetSelectedOrDummyTarget(m_name_options.m_use_dummy.GetCurrentValue());
18665e09c8c3SJim Ingham 
1867b9c1b51eSKate Stone     if (target == nullptr) {
18685e09c8c3SJim Ingham       result.AppendError("Invalid target. No existing target or breakpoints.");
18695e09c8c3SJim Ingham       result.SetStatus(eReturnStatusFailed);
18705e09c8c3SJim Ingham       return false;
18715e09c8c3SJim Ingham     }
18725e09c8c3SJim Ingham 
1873bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
1874bb19a13cSSaleem Abdulrasool     target->GetBreakpointList().GetListMutex(lock);
18755e09c8c3SJim Ingham 
18765e09c8c3SJim Ingham     const BreakpointList &breakpoints = target->GetBreakpointList();
18775e09c8c3SJim Ingham 
18785e09c8c3SJim Ingham     size_t num_breakpoints = breakpoints.GetSize();
1879b9c1b51eSKate Stone     if (num_breakpoints == 0) {
18805e09c8c3SJim Ingham       result.SetError("No breakpoints, cannot add names.");
18815e09c8c3SJim Ingham       result.SetStatus(eReturnStatusFailed);
18825e09c8c3SJim Ingham       return false;
18835e09c8c3SJim Ingham     }
18845e09c8c3SJim Ingham 
18855e09c8c3SJim Ingham     // Particular breakpoint selected; disable that breakpoint.
18865e09c8c3SJim Ingham     BreakpointIDList valid_bp_ids;
1887b9c1b51eSKate Stone     CommandObjectMultiwordBreakpoint::VerifyBreakpointIDs(
1888b842f2ecSJim Ingham         command, target, result, &valid_bp_ids,
1889b842f2ecSJim Ingham         BreakpointName::Permissions::PermissionKinds::listPerm);
18905e09c8c3SJim Ingham 
1891b9c1b51eSKate Stone     if (result.Succeeded()) {
1892b9c1b51eSKate Stone       if (valid_bp_ids.GetSize() == 0) {
18935e09c8c3SJim Ingham         result.SetError("No breakpoints specified, cannot add names.");
18945e09c8c3SJim Ingham         result.SetStatus(eReturnStatusFailed);
18955e09c8c3SJim Ingham         return false;
18965e09c8c3SJim Ingham       }
18975e09c8c3SJim Ingham       size_t num_valid_ids = valid_bp_ids.GetSize();
1898b842f2ecSJim Ingham       const char *bp_name = m_name_options.m_name.GetCurrentValue();
1899b842f2ecSJim Ingham       Status error; // This error reports illegal names, but we've already
1900b842f2ecSJim Ingham                     // checked that, so we don't need to check it again here.
1901b9c1b51eSKate Stone       for (size_t index = 0; index < num_valid_ids; index++) {
1902b9c1b51eSKate Stone         lldb::break_id_t bp_id =
1903b9c1b51eSKate Stone             valid_bp_ids.GetBreakpointIDAtIndex(index).GetBreakpointID();
19045e09c8c3SJim Ingham         BreakpointSP bp_sp = breakpoints.FindBreakpointByID(bp_id);
1905b842f2ecSJim Ingham         target->AddNameToBreakpoint(bp_sp, bp_name, error);
19065e09c8c3SJim Ingham       }
19075e09c8c3SJim Ingham     }
19085e09c8c3SJim Ingham 
19095e09c8c3SJim Ingham     return true;
19105e09c8c3SJim Ingham   }
19115e09c8c3SJim Ingham 
19125e09c8c3SJim Ingham private:
19135e09c8c3SJim Ingham   BreakpointNameOptionGroup m_name_options;
19145e09c8c3SJim Ingham   OptionGroupOptions m_option_group;
19155e09c8c3SJim Ingham };
19165e09c8c3SJim Ingham 
1917b9c1b51eSKate Stone class CommandObjectBreakpointNameDelete : public CommandObjectParsed {
19185e09c8c3SJim Ingham public:
1919b9c1b51eSKate Stone   CommandObjectBreakpointNameDelete(CommandInterpreter &interpreter)
1920b9c1b51eSKate Stone       : CommandObjectParsed(
1921b9c1b51eSKate Stone             interpreter, "delete",
19225e09c8c3SJim Ingham             "Delete a name from the breakpoints provided.",
19235e09c8c3SJim Ingham             "breakpoint name delete <command-options> <breakpoint-id-list>"),
1924b9c1b51eSKate Stone         m_name_options(), m_option_group() {
1925b9c1b51eSKate Stone     // Create the first variant for the first (and only) argument for this
1926b9c1b51eSKate Stone     // command.
19275e09c8c3SJim Ingham     CommandArgumentEntry arg1;
19285e09c8c3SJim Ingham     CommandArgumentData id_arg;
19295e09c8c3SJim Ingham     id_arg.arg_type = eArgTypeBreakpointID;
19305e09c8c3SJim Ingham     id_arg.arg_repetition = eArgRepeatOptional;
19315e09c8c3SJim Ingham     arg1.push_back(id_arg);
19325e09c8c3SJim Ingham     m_arguments.push_back(arg1);
19335e09c8c3SJim Ingham 
19345e09c8c3SJim Ingham     m_option_group.Append(&m_name_options, LLDB_OPT_SET_1, LLDB_OPT_SET_ALL);
19355e09c8c3SJim Ingham     m_option_group.Finalize();
19365e09c8c3SJim Ingham   }
19375e09c8c3SJim Ingham 
19389e85e5a8SEugene Zelenko   ~CommandObjectBreakpointNameDelete() override = default;
19395e09c8c3SJim Ingham 
1940b9c1b51eSKate Stone   Options *GetOptions() override { return &m_option_group; }
19415e09c8c3SJim Ingham 
19425e09c8c3SJim Ingham protected:
1943b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
1944b9c1b51eSKate Stone     if (!m_name_options.m_name.OptionWasSet()) {
19455e09c8c3SJim Ingham       result.SetError("No name option provided.");
19465e09c8c3SJim Ingham       return false;
19475e09c8c3SJim Ingham     }
19485e09c8c3SJim Ingham 
1949b9c1b51eSKate Stone     Target *target =
1950b9c1b51eSKate Stone         GetSelectedOrDummyTarget(m_name_options.m_use_dummy.GetCurrentValue());
19515e09c8c3SJim Ingham 
1952b9c1b51eSKate Stone     if (target == nullptr) {
19535e09c8c3SJim Ingham       result.AppendError("Invalid target. No existing target or breakpoints.");
19545e09c8c3SJim Ingham       result.SetStatus(eReturnStatusFailed);
19555e09c8c3SJim Ingham       return false;
19565e09c8c3SJim Ingham     }
19575e09c8c3SJim Ingham 
1958bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
1959bb19a13cSSaleem Abdulrasool     target->GetBreakpointList().GetListMutex(lock);
19605e09c8c3SJim Ingham 
19615e09c8c3SJim Ingham     const BreakpointList &breakpoints = target->GetBreakpointList();
19625e09c8c3SJim Ingham 
19635e09c8c3SJim Ingham     size_t num_breakpoints = breakpoints.GetSize();
1964b9c1b51eSKate Stone     if (num_breakpoints == 0) {
19655e09c8c3SJim Ingham       result.SetError("No breakpoints, cannot delete names.");
19665e09c8c3SJim Ingham       result.SetStatus(eReturnStatusFailed);
19675e09c8c3SJim Ingham       return false;
19685e09c8c3SJim Ingham     }
19695e09c8c3SJim Ingham 
19705e09c8c3SJim Ingham     // Particular breakpoint selected; disable that breakpoint.
19715e09c8c3SJim Ingham     BreakpointIDList valid_bp_ids;
1972b9c1b51eSKate Stone     CommandObjectMultiwordBreakpoint::VerifyBreakpointIDs(
1973b842f2ecSJim Ingham         command, target, result, &valid_bp_ids,
1974b842f2ecSJim Ingham         BreakpointName::Permissions::PermissionKinds::deletePerm);
19755e09c8c3SJim Ingham 
1976b9c1b51eSKate Stone     if (result.Succeeded()) {
1977b9c1b51eSKate Stone       if (valid_bp_ids.GetSize() == 0) {
19785e09c8c3SJim Ingham         result.SetError("No breakpoints specified, cannot delete names.");
19795e09c8c3SJim Ingham         result.SetStatus(eReturnStatusFailed);
19805e09c8c3SJim Ingham         return false;
19815e09c8c3SJim Ingham       }
1982b842f2ecSJim Ingham       ConstString bp_name(m_name_options.m_name.GetCurrentValue());
19835e09c8c3SJim Ingham       size_t num_valid_ids = valid_bp_ids.GetSize();
1984b9c1b51eSKate Stone       for (size_t index = 0; index < num_valid_ids; index++) {
1985b9c1b51eSKate Stone         lldb::break_id_t bp_id =
1986b9c1b51eSKate Stone             valid_bp_ids.GetBreakpointIDAtIndex(index).GetBreakpointID();
19875e09c8c3SJim Ingham         BreakpointSP bp_sp = breakpoints.FindBreakpointByID(bp_id);
1988b842f2ecSJim Ingham         target->RemoveNameFromBreakpoint(bp_sp, bp_name);
19895e09c8c3SJim Ingham       }
19905e09c8c3SJim Ingham     }
19915e09c8c3SJim Ingham 
19925e09c8c3SJim Ingham     return true;
19935e09c8c3SJim Ingham   }
19945e09c8c3SJim Ingham 
19955e09c8c3SJim Ingham private:
19965e09c8c3SJim Ingham   BreakpointNameOptionGroup m_name_options;
19975e09c8c3SJim Ingham   OptionGroupOptions m_option_group;
19985e09c8c3SJim Ingham };
19995e09c8c3SJim Ingham 
2000b9c1b51eSKate Stone class CommandObjectBreakpointNameList : public CommandObjectParsed {
20015e09c8c3SJim Ingham public:
2002b9c1b51eSKate Stone   CommandObjectBreakpointNameList(CommandInterpreter &interpreter)
2003b9c1b51eSKate Stone       : CommandObjectParsed(interpreter, "list",
2004b842f2ecSJim Ingham                             "List either the names for a breakpoint or info "
2005b842f2ecSJim Ingham                             "about a given name.  With no arguments, lists all "
2006b842f2ecSJim Ingham                             "names",
20075e09c8c3SJim Ingham                             "breakpoint name list <command-options>"),
2008b9c1b51eSKate Stone         m_name_options(), m_option_group() {
2009b842f2ecSJim Ingham     m_option_group.Append(&m_name_options, LLDB_OPT_SET_3, LLDB_OPT_SET_ALL);
20105e09c8c3SJim Ingham     m_option_group.Finalize();
20115e09c8c3SJim Ingham   }
20125e09c8c3SJim Ingham 
20139e85e5a8SEugene Zelenko   ~CommandObjectBreakpointNameList() override = default;
20145e09c8c3SJim Ingham 
2015b9c1b51eSKate Stone   Options *GetOptions() override { return &m_option_group; }
20165e09c8c3SJim Ingham 
20175e09c8c3SJim Ingham protected:
2018b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
2019b9c1b51eSKate Stone     Target *target =
2020b9c1b51eSKate Stone         GetSelectedOrDummyTarget(m_name_options.m_use_dummy.GetCurrentValue());
20215e09c8c3SJim Ingham 
2022b9c1b51eSKate Stone     if (target == nullptr) {
20235e09c8c3SJim Ingham       result.AppendError("Invalid target. No existing target or breakpoints.");
20245e09c8c3SJim Ingham       result.SetStatus(eReturnStatusFailed);
20255e09c8c3SJim Ingham       return false;
20265e09c8c3SJim Ingham     }
20275e09c8c3SJim Ingham 
2028b842f2ecSJim Ingham 
2029b842f2ecSJim Ingham     std::vector<std::string> name_list;
2030b842f2ecSJim Ingham     if (command.empty()) {
2031b842f2ecSJim Ingham       target->GetBreakpointNames(name_list);
2032b842f2ecSJim Ingham     } else {
2033b842f2ecSJim Ingham       for (const Args::ArgEntry &arg : command)
2034b842f2ecSJim Ingham       {
2035b842f2ecSJim Ingham         name_list.push_back(arg.c_str());
2036b842f2ecSJim Ingham       }
2037b842f2ecSJim Ingham     }
2038b842f2ecSJim Ingham 
2039b842f2ecSJim Ingham     if (name_list.empty()) {
2040b842f2ecSJim Ingham       result.AppendMessage("No breakpoint names found.");
2041b842f2ecSJim Ingham     } else {
2042b842f2ecSJim Ingham       for (const std::string &name_str : name_list) {
2043b842f2ecSJim Ingham         const char *name = name_str.c_str();
2044b842f2ecSJim Ingham         // First print out the options for the name:
2045b842f2ecSJim Ingham         Status error;
2046b842f2ecSJim Ingham         BreakpointName *bp_name = target->FindBreakpointName(ConstString(name),
2047b842f2ecSJim Ingham                                                              false,
2048b842f2ecSJim Ingham                                                              error);
2049b842f2ecSJim Ingham         if (bp_name)
2050b842f2ecSJim Ingham         {
2051b842f2ecSJim Ingham           StreamString s;
2052b842f2ecSJim Ingham           result.AppendMessageWithFormat("Name: %s\n", name);
2053b842f2ecSJim Ingham           if (bp_name->GetDescription(&s, eDescriptionLevelFull))
2054b842f2ecSJim Ingham           {
2055b842f2ecSJim Ingham             result.AppendMessage(s.GetString());
2056b842f2ecSJim Ingham           }
2057b842f2ecSJim Ingham 
2058bb19a13cSSaleem Abdulrasool           std::unique_lock<std::recursive_mutex> lock;
2059bb19a13cSSaleem Abdulrasool           target->GetBreakpointList().GetListMutex(lock);
20605e09c8c3SJim Ingham 
20615e09c8c3SJim Ingham           BreakpointList &breakpoints = target->GetBreakpointList();
2062b842f2ecSJim Ingham           bool any_set = false;
2063b9c1b51eSKate Stone           for (BreakpointSP bp_sp : breakpoints.Breakpoints()) {
2064b9c1b51eSKate Stone             if (bp_sp->MatchesName(name)) {
20655e09c8c3SJim Ingham               StreamString s;
2066b842f2ecSJim Ingham               any_set = true;
20675e09c8c3SJim Ingham               bp_sp->GetDescription(&s, eDescriptionLevelBrief);
20685e09c8c3SJim Ingham               s.EOL();
2069c156427dSZachary Turner               result.AppendMessage(s.GetString());
20705e09c8c3SJim Ingham             }
20715e09c8c3SJim Ingham           }
2072b842f2ecSJim Ingham           if (!any_set)
2073b842f2ecSJim Ingham             result.AppendMessage("No breakpoints using this name.");
2074b9c1b51eSKate Stone         } else {
2075b842f2ecSJim Ingham           result.AppendMessageWithFormat("Name: %s not found.\n", name);
20765e09c8c3SJim Ingham         }
2077b842f2ecSJim Ingham       }
20785e09c8c3SJim Ingham     }
20795e09c8c3SJim Ingham     return true;
20805e09c8c3SJim Ingham   }
20815e09c8c3SJim Ingham 
20825e09c8c3SJim Ingham private:
20835e09c8c3SJim Ingham   BreakpointNameOptionGroup m_name_options;
20845e09c8c3SJim Ingham   OptionGroupOptions m_option_group;
20855e09c8c3SJim Ingham };
20865e09c8c3SJim Ingham 
2087e14dc268SJim Ingham // CommandObjectBreakpointName
2088b9c1b51eSKate Stone class CommandObjectBreakpointName : public CommandObjectMultiword {
20895e09c8c3SJim Ingham public:
20907428a18cSKate Stone   CommandObjectBreakpointName(CommandInterpreter &interpreter)
2091b9c1b51eSKate Stone       : CommandObjectMultiword(
2092b9c1b51eSKate Stone             interpreter, "name", "Commands to manage name tags for breakpoints",
2093b9c1b51eSKate Stone             "breakpoint name <subcommand> [<command-options>]") {
2094b9c1b51eSKate Stone     CommandObjectSP add_command_object(
2095b9c1b51eSKate Stone         new CommandObjectBreakpointNameAdd(interpreter));
2096b9c1b51eSKate Stone     CommandObjectSP delete_command_object(
2097b9c1b51eSKate Stone         new CommandObjectBreakpointNameDelete(interpreter));
2098b9c1b51eSKate Stone     CommandObjectSP list_command_object(
2099b9c1b51eSKate Stone         new CommandObjectBreakpointNameList(interpreter));
2100b842f2ecSJim Ingham     CommandObjectSP configure_command_object(
2101b842f2ecSJim Ingham         new CommandObjectBreakpointNameConfigure(interpreter));
21025e09c8c3SJim Ingham 
21035e09c8c3SJim Ingham     LoadSubCommand("add", add_command_object);
21045e09c8c3SJim Ingham     LoadSubCommand("delete", delete_command_object);
21055e09c8c3SJim Ingham     LoadSubCommand("list", list_command_object);
2106b842f2ecSJim Ingham     LoadSubCommand("configure", configure_command_object);
21075e09c8c3SJim Ingham   }
21085e09c8c3SJim Ingham 
21099e85e5a8SEugene Zelenko   ~CommandObjectBreakpointName() override = default;
21105e09c8c3SJim Ingham };
21115e09c8c3SJim Ingham 
2112e14dc268SJim Ingham // CommandObjectBreakpointRead
21133acdf385SJim Ingham #pragma mark Read::CommandOptions
2114f94668e3SRaphael Isemann #define LLDB_OPTIONS_breakpoint_read
2115f94668e3SRaphael Isemann #include "CommandOptions.inc"
21161f0f5b5bSZachary Turner 
21171f0f5b5bSZachary Turner #pragma mark Read
2118e14dc268SJim Ingham 
2119e14dc268SJim Ingham class CommandObjectBreakpointRead : public CommandObjectParsed {
2120e14dc268SJim Ingham public:
2121e14dc268SJim Ingham   CommandObjectBreakpointRead(CommandInterpreter &interpreter)
2122e14dc268SJim Ingham       : CommandObjectParsed(interpreter, "breakpoint read",
2123e14dc268SJim Ingham                             "Read and set the breakpoints previously saved to "
2124e14dc268SJim Ingham                             "a file with \"breakpoint write\".  ",
2125e14dc268SJim Ingham                             nullptr),
2126e14dc268SJim Ingham         m_options() {
2127e14dc268SJim Ingham     CommandArgumentEntry arg;
2128e14dc268SJim Ingham     CommandObject::AddIDsArgumentData(arg, eArgTypeBreakpointID,
2129e14dc268SJim Ingham                                       eArgTypeBreakpointIDRange);
2130e14dc268SJim Ingham     // Add the entry for the first argument for this command to the object's
2131e14dc268SJim Ingham     // arguments vector.
2132e14dc268SJim Ingham     m_arguments.push_back(arg);
2133e14dc268SJim Ingham   }
2134e14dc268SJim Ingham 
2135e14dc268SJim Ingham   ~CommandObjectBreakpointRead() override = default;
2136e14dc268SJim Ingham 
2137e14dc268SJim Ingham   Options *GetOptions() override { return &m_options; }
2138e14dc268SJim Ingham 
2139e14dc268SJim Ingham   class CommandOptions : public Options {
2140e14dc268SJim Ingham   public:
2141e14dc268SJim Ingham     CommandOptions() : Options() {}
2142e14dc268SJim Ingham 
2143e14dc268SJim Ingham     ~CommandOptions() override = default;
2144e14dc268SJim Ingham 
214597206d57SZachary Turner     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
2146e14dc268SJim Ingham                           ExecutionContext *execution_context) override {
214797206d57SZachary Turner       Status error;
2148e14dc268SJim Ingham       const int short_option = m_getopt_table[option_idx].val;
2149e14dc268SJim Ingham 
2150e14dc268SJim Ingham       switch (short_option) {
2151e14dc268SJim Ingham       case 'f':
2152e14dc268SJim Ingham         m_filename.assign(option_arg);
2153e14dc268SJim Ingham         break;
21543acdf385SJim Ingham       case 'N': {
215597206d57SZachary Turner         Status name_error;
21563acdf385SJim Ingham         if (!BreakpointID::StringIsBreakpointName(llvm::StringRef(option_arg),
21573acdf385SJim Ingham                                                   name_error)) {
21583acdf385SJim Ingham           error.SetErrorStringWithFormat("Invalid breakpoint name: %s",
21593acdf385SJim Ingham                                          name_error.AsCString());
21603acdf385SJim Ingham         }
21613acdf385SJim Ingham         m_names.push_back(option_arg);
21623acdf385SJim Ingham         break;
21633acdf385SJim Ingham       }
2164e14dc268SJim Ingham       default:
2165*36162014SRaphael Isemann         llvm_unreachable("Unimplemented option");
2166e14dc268SJim Ingham       }
2167e14dc268SJim Ingham 
2168e14dc268SJim Ingham       return error;
2169e14dc268SJim Ingham     }
2170e14dc268SJim Ingham 
2171e14dc268SJim Ingham     void OptionParsingStarting(ExecutionContext *execution_context) override {
2172e14dc268SJim Ingham       m_filename.clear();
21733acdf385SJim Ingham       m_names.clear();
2174e14dc268SJim Ingham     }
2175e14dc268SJim Ingham 
21761f0f5b5bSZachary Turner     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
217770602439SZachary Turner       return llvm::makeArrayRef(g_breakpoint_read_options);
21781f0f5b5bSZachary Turner     }
2179e14dc268SJim Ingham 
2180e14dc268SJim Ingham     // Instance variables to hold the values for command options.
2181e14dc268SJim Ingham 
2182e14dc268SJim Ingham     std::string m_filename;
21833acdf385SJim Ingham     std::vector<std::string> m_names;
2184e14dc268SJim Ingham   };
2185e14dc268SJim Ingham 
2186e14dc268SJim Ingham protected:
2187e14dc268SJim Ingham   bool DoExecute(Args &command, CommandReturnObject &result) override {
2188e14dc268SJim Ingham     Target *target = GetSelectedOrDummyTarget();
2189e14dc268SJim Ingham     if (target == nullptr) {
2190e14dc268SJim Ingham       result.AppendError("Invalid target.  No existing target or breakpoints.");
2191e14dc268SJim Ingham       result.SetStatus(eReturnStatusFailed);
2192e14dc268SJim Ingham       return false;
2193e14dc268SJim Ingham     }
2194e14dc268SJim Ingham 
21953acdf385SJim Ingham     std::unique_lock<std::recursive_mutex> lock;
21963acdf385SJim Ingham     target->GetBreakpointList().GetListMutex(lock);
21973acdf385SJim Ingham 
21988f3be7a3SJonas Devlieghere     FileSpec input_spec(m_options.m_filename);
21998f3be7a3SJonas Devlieghere     FileSystem::Instance().Resolve(input_spec);
220001f16664SJim Ingham     BreakpointIDList new_bps;
220197206d57SZachary Turner     Status error = target->CreateBreakpointsFromFile(
220297206d57SZachary Turner         input_spec, m_options.m_names, new_bps);
2203e14dc268SJim Ingham 
2204e14dc268SJim Ingham     if (!error.Success()) {
220501f16664SJim Ingham       result.AppendError(error.AsCString());
2206e14dc268SJim Ingham       result.SetStatus(eReturnStatusFailed);
220701f16664SJim Ingham       return false;
2208e14dc268SJim Ingham     }
22093acdf385SJim Ingham 
22103acdf385SJim Ingham     Stream &output_stream = result.GetOutputStream();
22113acdf385SJim Ingham 
22123acdf385SJim Ingham     size_t num_breakpoints = new_bps.GetSize();
22133acdf385SJim Ingham     if (num_breakpoints == 0) {
22143acdf385SJim Ingham       result.AppendMessage("No breakpoints added.");
22153acdf385SJim Ingham     } else {
22163acdf385SJim Ingham       // No breakpoint selected; show info about all currently set breakpoints.
22173acdf385SJim Ingham       result.AppendMessage("New breakpoints:");
22183acdf385SJim Ingham       for (size_t i = 0; i < num_breakpoints; ++i) {
22193acdf385SJim Ingham         BreakpointID bp_id = new_bps.GetBreakpointIDAtIndex(i);
22203acdf385SJim Ingham         Breakpoint *bp = target->GetBreakpointList()
22213acdf385SJim Ingham                              .FindBreakpointByID(bp_id.GetBreakpointID())
22223acdf385SJim Ingham                              .get();
22233acdf385SJim Ingham         if (bp)
22243acdf385SJim Ingham           bp->GetDescription(&output_stream, lldb::eDescriptionLevelInitial,
22253acdf385SJim Ingham                              false);
22263acdf385SJim Ingham       }
22273acdf385SJim Ingham     }
2228e14dc268SJim Ingham     return result.Succeeded();
2229e14dc268SJim Ingham   }
2230e14dc268SJim Ingham 
2231e14dc268SJim Ingham private:
2232e14dc268SJim Ingham   CommandOptions m_options;
2233e14dc268SJim Ingham };
2234e14dc268SJim Ingham 
2235e14dc268SJim Ingham // CommandObjectBreakpointWrite
22361f0f5b5bSZachary Turner #pragma mark Write::CommandOptions
2237f94668e3SRaphael Isemann #define LLDB_OPTIONS_breakpoint_write
2238f94668e3SRaphael Isemann #include "CommandOptions.inc"
22391f0f5b5bSZachary Turner 
22401f0f5b5bSZachary Turner #pragma mark Write
2241e14dc268SJim Ingham class CommandObjectBreakpointWrite : public CommandObjectParsed {
2242e14dc268SJim Ingham public:
2243e14dc268SJim Ingham   CommandObjectBreakpointWrite(CommandInterpreter &interpreter)
2244e14dc268SJim Ingham       : CommandObjectParsed(interpreter, "breakpoint write",
2245e14dc268SJim Ingham                             "Write the breakpoints listed to a file that can "
2246e14dc268SJim Ingham                             "be read in with \"breakpoint read\".  "
2247e14dc268SJim Ingham                             "If given no arguments, writes all breakpoints.",
2248e14dc268SJim Ingham                             nullptr),
2249e14dc268SJim Ingham         m_options() {
2250e14dc268SJim Ingham     CommandArgumentEntry arg;
2251e14dc268SJim Ingham     CommandObject::AddIDsArgumentData(arg, eArgTypeBreakpointID,
2252e14dc268SJim Ingham                                       eArgTypeBreakpointIDRange);
2253e14dc268SJim Ingham     // Add the entry for the first argument for this command to the object's
2254e14dc268SJim Ingham     // arguments vector.
2255e14dc268SJim Ingham     m_arguments.push_back(arg);
2256e14dc268SJim Ingham   }
2257e14dc268SJim Ingham 
2258e14dc268SJim Ingham   ~CommandObjectBreakpointWrite() override = default;
2259e14dc268SJim Ingham 
2260e14dc268SJim Ingham   Options *GetOptions() override { return &m_options; }
2261e14dc268SJim Ingham 
2262e14dc268SJim Ingham   class CommandOptions : public Options {
2263e14dc268SJim Ingham   public:
2264e14dc268SJim Ingham     CommandOptions() : Options() {}
2265e14dc268SJim Ingham 
2266e14dc268SJim Ingham     ~CommandOptions() override = default;
2267e14dc268SJim Ingham 
226897206d57SZachary Turner     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
2269e14dc268SJim Ingham                           ExecutionContext *execution_context) override {
227097206d57SZachary Turner       Status error;
2271e14dc268SJim Ingham       const int short_option = m_getopt_table[option_idx].val;
2272e14dc268SJim Ingham 
2273e14dc268SJim Ingham       switch (short_option) {
2274e14dc268SJim Ingham       case 'f':
2275e14dc268SJim Ingham         m_filename.assign(option_arg);
2276e14dc268SJim Ingham         break;
22772d3628e1SJim Ingham       case 'a':
22782d3628e1SJim Ingham         m_append = true;
22792d3628e1SJim Ingham         break;
2280e14dc268SJim Ingham       default:
2281*36162014SRaphael Isemann         llvm_unreachable("Unimplemented option");
2282e14dc268SJim Ingham       }
2283e14dc268SJim Ingham 
2284e14dc268SJim Ingham       return error;
2285e14dc268SJim Ingham     }
2286e14dc268SJim Ingham 
2287e14dc268SJim Ingham     void OptionParsingStarting(ExecutionContext *execution_context) override {
2288e14dc268SJim Ingham       m_filename.clear();
22892d3628e1SJim Ingham       m_append = false;
2290e14dc268SJim Ingham     }
2291e14dc268SJim Ingham 
22921f0f5b5bSZachary Turner     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
229370602439SZachary Turner       return llvm::makeArrayRef(g_breakpoint_write_options);
22941f0f5b5bSZachary Turner     }
2295e14dc268SJim Ingham 
2296e14dc268SJim Ingham     // Instance variables to hold the values for command options.
2297e14dc268SJim Ingham 
2298e14dc268SJim Ingham     std::string m_filename;
22992d3628e1SJim Ingham     bool m_append = false;
2300e14dc268SJim Ingham   };
2301e14dc268SJim Ingham 
2302e14dc268SJim Ingham protected:
2303e14dc268SJim Ingham   bool DoExecute(Args &command, CommandReturnObject &result) override {
2304e14dc268SJim Ingham     Target *target = GetSelectedOrDummyTarget();
2305e14dc268SJim Ingham     if (target == nullptr) {
2306e14dc268SJim Ingham       result.AppendError("Invalid target.  No existing target or breakpoints.");
2307e14dc268SJim Ingham       result.SetStatus(eReturnStatusFailed);
2308e14dc268SJim Ingham       return false;
2309e14dc268SJim Ingham     }
2310e14dc268SJim Ingham 
2311e14dc268SJim Ingham     std::unique_lock<std::recursive_mutex> lock;
2312e14dc268SJim Ingham     target->GetBreakpointList().GetListMutex(lock);
2313e14dc268SJim Ingham 
2314e14dc268SJim Ingham     BreakpointIDList valid_bp_ids;
231511eb9c64SZachary Turner     if (!command.empty()) {
2316e14dc268SJim Ingham       CommandObjectMultiwordBreakpoint::VerifyBreakpointIDs(
2317b842f2ecSJim Ingham           command, target, result, &valid_bp_ids,
2318b842f2ecSJim Ingham           BreakpointName::Permissions::PermissionKinds::listPerm);
2319e14dc268SJim Ingham 
232001f16664SJim Ingham       if (!result.Succeeded()) {
2321e14dc268SJim Ingham         result.SetStatus(eReturnStatusFailed);
2322e14dc268SJim Ingham         return false;
2323e14dc268SJim Ingham       }
2324e14dc268SJim Ingham     }
23258f3be7a3SJonas Devlieghere     FileSpec file_spec(m_options.m_filename);
23268f3be7a3SJonas Devlieghere     FileSystem::Instance().Resolve(file_spec);
23278f3be7a3SJonas Devlieghere     Status error = target->SerializeBreakpointsToFile(file_spec, valid_bp_ids,
23288f3be7a3SJonas Devlieghere                                                       m_options.m_append);
232901f16664SJim Ingham     if (!error.Success()) {
233001f16664SJim Ingham       result.AppendErrorWithFormat("error serializing breakpoints: %s.",
233101f16664SJim Ingham                                    error.AsCString());
233201f16664SJim Ingham       result.SetStatus(eReturnStatusFailed);
2333e14dc268SJim Ingham     }
2334e14dc268SJim Ingham     return result.Succeeded();
2335e14dc268SJim Ingham   }
2336e14dc268SJim Ingham 
2337e14dc268SJim Ingham private:
2338e14dc268SJim Ingham   CommandOptions m_options;
2339e14dc268SJim Ingham };
2340e14dc268SJim Ingham 
234130fdc8d8SChris Lattner // CommandObjectMultiwordBreakpoint
2342ae1c4cf5SJim Ingham #pragma mark MultiwordBreakpoint
234330fdc8d8SChris Lattner 
2344b9c1b51eSKate Stone CommandObjectMultiwordBreakpoint::CommandObjectMultiwordBreakpoint(
2345b9c1b51eSKate Stone     CommandInterpreter &interpreter)
2346b9c1b51eSKate Stone     : CommandObjectMultiword(
2347b9c1b51eSKate Stone           interpreter, "breakpoint",
23487428a18cSKate Stone           "Commands for operating on breakpoints (see 'help b' for shorthand.)",
2349b9c1b51eSKate Stone           "breakpoint <subcommand> [<command-options>]") {
2350b9c1b51eSKate Stone   CommandObjectSP list_command_object(
2351b9c1b51eSKate Stone       new CommandObjectBreakpointList(interpreter));
2352b9c1b51eSKate Stone   CommandObjectSP enable_command_object(
2353b9c1b51eSKate Stone       new CommandObjectBreakpointEnable(interpreter));
2354b9c1b51eSKate Stone   CommandObjectSP disable_command_object(
2355b9c1b51eSKate Stone       new CommandObjectBreakpointDisable(interpreter));
2356b9c1b51eSKate Stone   CommandObjectSP clear_command_object(
2357b9c1b51eSKate Stone       new CommandObjectBreakpointClear(interpreter));
2358b9c1b51eSKate Stone   CommandObjectSP delete_command_object(
2359b9c1b51eSKate Stone       new CommandObjectBreakpointDelete(interpreter));
2360b9c1b51eSKate Stone   CommandObjectSP set_command_object(
2361b9c1b51eSKate Stone       new CommandObjectBreakpointSet(interpreter));
2362b9c1b51eSKate Stone   CommandObjectSP command_command_object(
2363b9c1b51eSKate Stone       new CommandObjectBreakpointCommand(interpreter));
2364b9c1b51eSKate Stone   CommandObjectSP modify_command_object(
2365b9c1b51eSKate Stone       new CommandObjectBreakpointModify(interpreter));
2366b9c1b51eSKate Stone   CommandObjectSP name_command_object(
2367b9c1b51eSKate Stone       new CommandObjectBreakpointName(interpreter));
2368e14dc268SJim Ingham   CommandObjectSP write_command_object(
2369e14dc268SJim Ingham       new CommandObjectBreakpointWrite(interpreter));
2370e14dc268SJim Ingham   CommandObjectSP read_command_object(
2371e14dc268SJim Ingham       new CommandObjectBreakpointRead(interpreter));
237230fdc8d8SChris Lattner 
2373b7234e40SJohnny Chen   list_command_object->SetCommandName("breakpoint list");
237430fdc8d8SChris Lattner   enable_command_object->SetCommandName("breakpoint enable");
237530fdc8d8SChris Lattner   disable_command_object->SetCommandName("breakpoint disable");
2376b7234e40SJohnny Chen   clear_command_object->SetCommandName("breakpoint clear");
2377b7234e40SJohnny Chen   delete_command_object->SetCommandName("breakpoint delete");
2378ae1c4cf5SJim Ingham   set_command_object->SetCommandName("breakpoint set");
2379b7234e40SJohnny Chen   command_command_object->SetCommandName("breakpoint command");
2380b7234e40SJohnny Chen   modify_command_object->SetCommandName("breakpoint modify");
23815e09c8c3SJim Ingham   name_command_object->SetCommandName("breakpoint name");
2382e14dc268SJim Ingham   write_command_object->SetCommandName("breakpoint write");
2383e14dc268SJim Ingham   read_command_object->SetCommandName("breakpoint read");
238430fdc8d8SChris Lattner 
238523f59509SGreg Clayton   LoadSubCommand("list", list_command_object);
238623f59509SGreg Clayton   LoadSubCommand("enable", enable_command_object);
238723f59509SGreg Clayton   LoadSubCommand("disable", disable_command_object);
238823f59509SGreg Clayton   LoadSubCommand("clear", clear_command_object);
238923f59509SGreg Clayton   LoadSubCommand("delete", delete_command_object);
239023f59509SGreg Clayton   LoadSubCommand("set", set_command_object);
239123f59509SGreg Clayton   LoadSubCommand("command", command_command_object);
239223f59509SGreg Clayton   LoadSubCommand("modify", modify_command_object);
23935e09c8c3SJim Ingham   LoadSubCommand("name", name_command_object);
2394e14dc268SJim Ingham   LoadSubCommand("write", write_command_object);
2395e14dc268SJim Ingham   LoadSubCommand("read", read_command_object);
239630fdc8d8SChris Lattner }
239730fdc8d8SChris Lattner 
23989e85e5a8SEugene Zelenko CommandObjectMultiwordBreakpoint::~CommandObjectMultiwordBreakpoint() = default;
239930fdc8d8SChris Lattner 
2400b9c1b51eSKate Stone void CommandObjectMultiwordBreakpoint::VerifyIDs(Args &args, Target *target,
24015e09c8c3SJim Ingham                                                  bool allow_locations,
24025e09c8c3SJim Ingham                                                  CommandReturnObject &result,
2403b842f2ecSJim Ingham                                                  BreakpointIDList *valid_ids,
2404b842f2ecSJim Ingham                                                  BreakpointName::Permissions
2405b842f2ecSJim Ingham                                                      ::PermissionKinds
2406b842f2ecSJim Ingham                                                      purpose) {
240730fdc8d8SChris Lattner   // args can be strings representing 1). integers (for breakpoint ids)
2408b9c1b51eSKate Stone   //                                  2). the full breakpoint & location
2409b9c1b51eSKate Stone   //                                  canonical representation
2410b9c1b51eSKate Stone   //                                  3). the word "to" or a hyphen,
2411b9c1b51eSKate Stone   //                                  representing a range (in which case there
2412b9c1b51eSKate Stone   //                                      had *better* be an entry both before &
2413b9c1b51eSKate Stone   //                                      after of one of the first two types.
24145e09c8c3SJim Ingham   //                                  4). A breakpoint name
2415b9c1b51eSKate Stone   // If args is empty, we will use the last created breakpoint (if there is
2416b9c1b51eSKate Stone   // one.)
241730fdc8d8SChris Lattner 
241830fdc8d8SChris Lattner   Args temp_args;
241930fdc8d8SChris Lattner 
242011eb9c64SZachary Turner   if (args.empty()) {
2421b9c1b51eSKate Stone     if (target->GetLastCreatedBreakpoint()) {
2422b9c1b51eSKate Stone       valid_ids->AddBreakpointID(BreakpointID(
2423b9c1b51eSKate Stone           target->GetLastCreatedBreakpoint()->GetID(), LLDB_INVALID_BREAK_ID));
242436f3b369SJim Ingham       result.SetStatus(eReturnStatusSuccessFinishNoResult);
2425b9c1b51eSKate Stone     } else {
2426b9c1b51eSKate Stone       result.AppendError(
2427b9c1b51eSKate Stone           "No breakpoint specified and no last created breakpoint.");
242836f3b369SJim Ingham       result.SetStatus(eReturnStatusFailed);
242936f3b369SJim Ingham     }
243036f3b369SJim Ingham     return;
243136f3b369SJim Ingham   }
243236f3b369SJim Ingham 
2433b9c1b51eSKate Stone   // Create a new Args variable to use; copy any non-breakpoint-id-ranges stuff
243405097246SAdrian Prantl   // directly from the old ARGS to the new TEMP_ARGS.  Do not copy breakpoint
243505097246SAdrian Prantl   // id range strings over; instead generate a list of strings for all the
243605097246SAdrian Prantl   // breakpoint ids in the range, and shove all of those breakpoint id strings
243705097246SAdrian Prantl   // into TEMP_ARGS.
243830fdc8d8SChris Lattner 
2439b9c1b51eSKate Stone   BreakpointIDList::FindAndReplaceIDRanges(args, target, allow_locations,
2440b842f2ecSJim Ingham                                            purpose, result, temp_args);
244130fdc8d8SChris Lattner 
2442b9c1b51eSKate Stone   // NOW, convert the list of breakpoint id strings in TEMP_ARGS into an actual
2443b9c1b51eSKate Stone   // BreakpointIDList:
244430fdc8d8SChris Lattner 
244516662f3cSPavel Labath   valid_ids->InsertStringArray(temp_args.GetArgumentArrayRef(), result);
244630fdc8d8SChris Lattner 
244705097246SAdrian Prantl   // At this point,  all of the breakpoint ids that the user passed in have
244805097246SAdrian Prantl   // been converted to breakpoint IDs and put into valid_ids.
244930fdc8d8SChris Lattner 
2450b9c1b51eSKate Stone   if (result.Succeeded()) {
2451b9c1b51eSKate Stone     // Now that we've converted everything from args into a list of breakpoint
245205097246SAdrian Prantl     // ids, go through our tentative list of breakpoint id's and verify that
245305097246SAdrian Prantl     // they correspond to valid/currently set breakpoints.
245430fdc8d8SChris Lattner 
2455c982c768SGreg Clayton     const size_t count = valid_ids->GetSize();
2456b9c1b51eSKate Stone     for (size_t i = 0; i < count; ++i) {
245730fdc8d8SChris Lattner       BreakpointID cur_bp_id = valid_ids->GetBreakpointIDAtIndex(i);
2458b9c1b51eSKate Stone       Breakpoint *breakpoint =
2459b9c1b51eSKate Stone           target->GetBreakpointByID(cur_bp_id.GetBreakpointID()).get();
2460b9c1b51eSKate Stone       if (breakpoint != nullptr) {
2461c7bece56SGreg Clayton         const size_t num_locations = breakpoint->GetNumLocations();
2462b9c1b51eSKate Stone         if (static_cast<size_t>(cur_bp_id.GetLocationID()) > num_locations) {
246330fdc8d8SChris Lattner           StreamString id_str;
2464b9c1b51eSKate Stone           BreakpointID::GetCanonicalReference(
2465b9c1b51eSKate Stone               &id_str, cur_bp_id.GetBreakpointID(), cur_bp_id.GetLocationID());
2466c982c768SGreg Clayton           i = valid_ids->GetSize() + 1;
2467b9c1b51eSKate Stone           result.AppendErrorWithFormat(
2468b9c1b51eSKate Stone               "'%s' is not a currently valid breakpoint/location id.\n",
246930fdc8d8SChris Lattner               id_str.GetData());
247030fdc8d8SChris Lattner           result.SetStatus(eReturnStatusFailed);
247130fdc8d8SChris Lattner         }
2472b9c1b51eSKate Stone       } else {
2473c982c768SGreg Clayton         i = valid_ids->GetSize() + 1;
2474b9c1b51eSKate Stone         result.AppendErrorWithFormat(
2475b9c1b51eSKate Stone             "'%d' is not a currently valid breakpoint ID.\n",
24767428a18cSKate Stone             cur_bp_id.GetBreakpointID());
247730fdc8d8SChris Lattner         result.SetStatus(eReturnStatusFailed);
247830fdc8d8SChris Lattner       }
247930fdc8d8SChris Lattner     }
248030fdc8d8SChris Lattner   }
248130fdc8d8SChris Lattner }
2482