130fdc8d8SChris Lattner //===-- CommandObjectBreakpoint.cpp -----------------------------*- C++ -*-===//
230fdc8d8SChris Lattner //
330fdc8d8SChris Lattner //                     The LLVM Compiler Infrastructure
430fdc8d8SChris Lattner //
530fdc8d8SChris Lattner // This file is distributed under the University of Illinois Open Source
630fdc8d8SChris Lattner // License. See LICENSE.TXT for details.
730fdc8d8SChris Lattner //
830fdc8d8SChris Lattner //===----------------------------------------------------------------------===//
930fdc8d8SChris Lattner 
1030fdc8d8SChris Lattner // C Includes
1130fdc8d8SChris Lattner // C++ Includes
129e85e5a8SEugene Zelenko #include <vector>
139e85e5a8SEugene Zelenko 
1430fdc8d8SChris Lattner // Other libraries and framework includes
1530fdc8d8SChris Lattner // Project includes
169e85e5a8SEugene Zelenko #include "CommandObjectBreakpoint.h"
179e85e5a8SEugene Zelenko #include "CommandObjectBreakpointCommand.h"
1830fdc8d8SChris Lattner #include "lldb/Breakpoint/Breakpoint.h"
1930fdc8d8SChris Lattner #include "lldb/Breakpoint/BreakpointIDList.h"
2030fdc8d8SChris Lattner #include "lldb/Breakpoint/BreakpointLocation.h"
213eb2b44dSZachary Turner #include "lldb/Host/OptionParser.h"
22b9c1b51eSKate Stone #include "lldb/Interpreter/CommandCompletions.h"
23b9c1b51eSKate Stone #include "lldb/Interpreter/CommandInterpreter.h"
24b9c1b51eSKate Stone #include "lldb/Interpreter/CommandReturnObject.h"
2547cbf4a0SPavel Labath #include "lldb/Interpreter/OptionArgParser.h"
2632abc6edSZachary Turner #include "lldb/Interpreter/OptionValueBoolean.h"
275e09c8c3SJim Ingham #include "lldb/Interpreter/OptionValueString.h"
285e09c8c3SJim Ingham #include "lldb/Interpreter/OptionValueUInt64.h"
29b9c1b51eSKate Stone #include "lldb/Interpreter/Options.h"
300e0984eeSJim Ingham #include "lldb/Target/Language.h"
31b57e4a1bSJason Molenda #include "lldb/Target/StackFrame.h"
32b9c1b51eSKate Stone #include "lldb/Target/Target.h"
331b54c88cSJim Ingham #include "lldb/Target/Thread.h"
341b54c88cSJim Ingham #include "lldb/Target/ThreadSpec.h"
35bf9a7730SZachary Turner #include "lldb/Utility/RegularExpression.h"
36bf9a7730SZachary Turner #include "lldb/Utility/StreamString.h"
3730fdc8d8SChris Lattner 
3830fdc8d8SChris Lattner using namespace lldb;
3930fdc8d8SChris Lattner using namespace lldb_private;
4030fdc8d8SChris Lattner 
41b9c1b51eSKate Stone static void AddBreakpointDescription(Stream *s, Breakpoint *bp,
42b9c1b51eSKate Stone                                      lldb::DescriptionLevel level) {
4330fdc8d8SChris Lattner   s->IndentMore();
4430fdc8d8SChris Lattner   bp->GetDescription(s, level, true);
4530fdc8d8SChris Lattner   s->IndentLess();
4630fdc8d8SChris Lattner   s->EOL();
4730fdc8d8SChris Lattner }
4830fdc8d8SChris Lattner 
49b842f2ecSJim Ingham //-------------------------------------------------------------------------
50b842f2ecSJim Ingham // Modifiable Breakpoint Options
51b842f2ecSJim Ingham //-------------------------------------------------------------------------
52b842f2ecSJim Ingham #pragma mark Modify::CommandOptions
53b842f2ecSJim Ingham static OptionDefinition g_breakpoint_modify_options[] = {
54b842f2ecSJim Ingham     // clang-format off
55b842f2ecSJim Ingham   { LLDB_OPT_SET_1, false, "ignore-count", 'i', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeCount,       "Set the number of times this breakpoint is skipped before stopping." },
56b842f2ecSJim Ingham   { LLDB_OPT_SET_1, false, "one-shot",     'o', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean,     "The breakpoint is deleted the first time it stop causes a stop." },
57b842f2ecSJim Ingham   { LLDB_OPT_SET_1, false, "thread-index", 'x', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeThreadIndex, "The breakpoint stops only for the thread whose index matches this argument." },
58b842f2ecSJim Ingham   { LLDB_OPT_SET_1, false, "thread-id",    't', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeThreadID,    "The breakpoint stops only for the thread whose TID matches this argument." },
59b842f2ecSJim Ingham   { LLDB_OPT_SET_1, false, "thread-name",  'T', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeThreadName,  "The breakpoint stops only for the thread whose thread name matches this argument." },
60b842f2ecSJim Ingham   { LLDB_OPT_SET_1, false, "queue-name",   'q', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeQueueName,   "The breakpoint stops only for threads in the queue whose name is given by this argument." },
61b842f2ecSJim Ingham   { LLDB_OPT_SET_1, false, "condition",    'c', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeExpression,  "The breakpoint stops only if this condition expression evaluates to true." },
62b842f2ecSJim Ingham   { LLDB_OPT_SET_1, false, "auto-continue",'G', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean,     "The breakpoint will auto-continue after running its commands." },
63b842f2ecSJim Ingham   { LLDB_OPT_SET_2, false, "enable",       'e', OptionParser::eNoArgument,       nullptr, nullptr, 0, eArgTypeNone,        "Enable the breakpoint." },
64b842f2ecSJim Ingham   { LLDB_OPT_SET_3, false, "disable",      'd', OptionParser::eNoArgument,       nullptr, nullptr, 0, eArgTypeNone,        "Disable the breakpoint." },
65b842f2ecSJim Ingham   { LLDB_OPT_SET_4, false, "command",      'C', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeCommand,     "A command to run when the breakpoint is hit, can be provided more than once, the commands will get run in order left to right." },
66b842f2ecSJim Ingham     // clang-format on
67b842f2ecSJim Ingham };
68b842f2ecSJim Ingham class lldb_private::BreakpointOptionGroup : public OptionGroup
69b842f2ecSJim Ingham {
70b842f2ecSJim Ingham public:
71b842f2ecSJim Ingham   BreakpointOptionGroup() :
72b842f2ecSJim Ingham           OptionGroup(),
73b842f2ecSJim Ingham           m_bp_opts(false) {}
74b842f2ecSJim Ingham 
75b842f2ecSJim Ingham   ~BreakpointOptionGroup() override = default;
76b842f2ecSJim Ingham 
77b842f2ecSJim Ingham   llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
78b842f2ecSJim Ingham     return llvm::makeArrayRef(g_breakpoint_modify_options);
79b842f2ecSJim Ingham   }
80b842f2ecSJim Ingham 
81b842f2ecSJim Ingham   Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
82b842f2ecSJim Ingham                           ExecutionContext *execution_context) override {
83b842f2ecSJim Ingham     Status error;
84b842f2ecSJim Ingham     const int short_option = g_breakpoint_modify_options[option_idx].short_option;
85b842f2ecSJim Ingham 
86b842f2ecSJim Ingham     switch (short_option) {
87b842f2ecSJim Ingham     case 'c':
8805097246SAdrian Prantl       // Normally an empty breakpoint condition marks is as unset. But we need
8905097246SAdrian Prantl       // to say it was passed in.
90b842f2ecSJim Ingham       m_bp_opts.SetCondition(option_arg.str().c_str());
91b842f2ecSJim Ingham       m_bp_opts.m_set_flags.Set(BreakpointOptions::eCondition);
92b842f2ecSJim Ingham       break;
93b842f2ecSJim Ingham     case 'C':
94b842f2ecSJim Ingham       m_commands.push_back(option_arg);
95b842f2ecSJim Ingham       break;
96b842f2ecSJim Ingham     case 'd':
97b842f2ecSJim Ingham       m_bp_opts.SetEnabled(false);
98b842f2ecSJim Ingham       break;
99b842f2ecSJim Ingham     case 'e':
100b842f2ecSJim Ingham       m_bp_opts.SetEnabled(true);
101b842f2ecSJim Ingham       break;
102b842f2ecSJim Ingham     case 'G': {
103b842f2ecSJim Ingham       bool value, success;
10447cbf4a0SPavel Labath       value = OptionArgParser::ToBoolean(option_arg, false, &success);
105b842f2ecSJim Ingham       if (success) {
106b842f2ecSJim Ingham         m_bp_opts.SetAutoContinue(value);
107b842f2ecSJim Ingham       } else
108b842f2ecSJim Ingham         error.SetErrorStringWithFormat(
109b842f2ecSJim Ingham             "invalid boolean value '%s' passed for -G option",
110b842f2ecSJim Ingham             option_arg.str().c_str());
111b842f2ecSJim Ingham     }
112b842f2ecSJim Ingham     break;
113b842f2ecSJim Ingham     case 'i':
114b842f2ecSJim Ingham     {
115b842f2ecSJim Ingham       uint32_t ignore_count;
116b842f2ecSJim Ingham       if (option_arg.getAsInteger(0, ignore_count))
117b842f2ecSJim Ingham         error.SetErrorStringWithFormat("invalid ignore count '%s'",
118b842f2ecSJim Ingham                                        option_arg.str().c_str());
119b842f2ecSJim Ingham       else
120b842f2ecSJim Ingham         m_bp_opts.SetIgnoreCount(ignore_count);
121b842f2ecSJim Ingham     }
122b842f2ecSJim Ingham     break;
123b842f2ecSJim Ingham     case 'o': {
124b842f2ecSJim Ingham       bool value, success;
12547cbf4a0SPavel Labath       value = OptionArgParser::ToBoolean(option_arg, false, &success);
126b842f2ecSJim Ingham       if (success) {
127b842f2ecSJim Ingham         m_bp_opts.SetOneShot(value);
128b842f2ecSJim Ingham       } else
129b842f2ecSJim Ingham         error.SetErrorStringWithFormat(
130b842f2ecSJim Ingham             "invalid boolean value '%s' passed for -o option",
131b842f2ecSJim Ingham             option_arg.str().c_str());
132b842f2ecSJim Ingham     } break;
133b842f2ecSJim Ingham     case 't':
134b842f2ecSJim Ingham     {
135b842f2ecSJim Ingham       lldb::tid_t thread_id = LLDB_INVALID_THREAD_ID;
136b842f2ecSJim Ingham       if (option_arg[0] != '\0') {
137b842f2ecSJim Ingham         if (option_arg.getAsInteger(0, thread_id))
138b842f2ecSJim Ingham           error.SetErrorStringWithFormat("invalid thread id string '%s'",
139b842f2ecSJim Ingham                                          option_arg.str().c_str());
140b842f2ecSJim Ingham       }
141b842f2ecSJim Ingham       m_bp_opts.SetThreadID(thread_id);
142b842f2ecSJim Ingham     }
143b842f2ecSJim Ingham     break;
144b842f2ecSJim Ingham     case 'T':
145b842f2ecSJim Ingham       m_bp_opts.GetThreadSpec()->SetName(option_arg.str().c_str());
146b842f2ecSJim Ingham       break;
147b842f2ecSJim Ingham     case 'q':
148b842f2ecSJim Ingham       m_bp_opts.GetThreadSpec()->SetQueueName(option_arg.str().c_str());
149b842f2ecSJim Ingham       break;
150b842f2ecSJim Ingham     case 'x':
151b842f2ecSJim Ingham     {
152b842f2ecSJim Ingham       uint32_t thread_index = UINT32_MAX;
153b842f2ecSJim Ingham       if (option_arg[0] != '\n') {
154b842f2ecSJim Ingham         if (option_arg.getAsInteger(0, thread_index))
155b842f2ecSJim Ingham           error.SetErrorStringWithFormat("invalid thread index string '%s'",
156b842f2ecSJim Ingham                                          option_arg.str().c_str());
157b842f2ecSJim Ingham       }
158b842f2ecSJim Ingham       m_bp_opts.GetThreadSpec()->SetIndex(thread_index);
159b842f2ecSJim Ingham     }
160b842f2ecSJim Ingham     break;
161b842f2ecSJim Ingham     default:
162b842f2ecSJim Ingham       error.SetErrorStringWithFormat("unrecognized option '%c'",
163b842f2ecSJim Ingham                                      short_option);
164b842f2ecSJim Ingham       break;
165b842f2ecSJim Ingham     }
166b842f2ecSJim Ingham 
167b842f2ecSJim Ingham     return error;
168b842f2ecSJim Ingham   }
169b842f2ecSJim Ingham 
170b842f2ecSJim Ingham   void OptionParsingStarting(ExecutionContext *execution_context) override {
171b842f2ecSJim Ingham     m_bp_opts.Clear();
172b842f2ecSJim Ingham     m_commands.clear();
173b842f2ecSJim Ingham   }
174b842f2ecSJim Ingham 
175b842f2ecSJim Ingham   Status OptionParsingFinished(ExecutionContext *execution_context) override {
176b842f2ecSJim Ingham     if (!m_commands.empty())
177b842f2ecSJim Ingham     {
178b842f2ecSJim Ingham       if (!m_commands.empty())
179b842f2ecSJim Ingham       {
180b842f2ecSJim Ingham           auto cmd_data = llvm::make_unique<BreakpointOptions::CommandData>();
181b842f2ecSJim Ingham 
182b842f2ecSJim Ingham           for (std::string &str : m_commands)
183b842f2ecSJim Ingham             cmd_data->user_source.AppendString(str);
184b842f2ecSJim Ingham 
185b842f2ecSJim Ingham           cmd_data->stop_on_error = true;
186b842f2ecSJim Ingham           m_bp_opts.SetCommandDataCallback(cmd_data);
187b842f2ecSJim Ingham       }
188b842f2ecSJim Ingham     }
189b842f2ecSJim Ingham     return Status();
190b842f2ecSJim Ingham   }
191b842f2ecSJim Ingham 
192b842f2ecSJim Ingham   const BreakpointOptions &GetBreakpointOptions()
193b842f2ecSJim Ingham   {
194b842f2ecSJim Ingham     return m_bp_opts;
195b842f2ecSJim Ingham   }
196b842f2ecSJim Ingham 
197b842f2ecSJim Ingham   std::vector<std::string> m_commands;
198b842f2ecSJim Ingham   BreakpointOptions m_bp_opts;
199b842f2ecSJim Ingham 
200b842f2ecSJim Ingham };
201b842f2ecSJim Ingham static OptionDefinition g_breakpoint_dummy_options[] = {
202b842f2ecSJim Ingham     // clang-format off
203b842f2ecSJim Ingham   { LLDB_OPT_SET_1, false, "dummy-breakpoints", 'D', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Act on Dummy breakpoints - i.e. breakpoints set before a file is provided, "
204b842f2ecSJim Ingham   "which prime new targets." },
205b842f2ecSJim Ingham     // clang-format on
206b842f2ecSJim Ingham };
207b842f2ecSJim Ingham 
208b842f2ecSJim Ingham class BreakpointDummyOptionGroup : public OptionGroup
209b842f2ecSJim Ingham {
210b842f2ecSJim Ingham public:
211b842f2ecSJim Ingham   BreakpointDummyOptionGroup() :
212b842f2ecSJim Ingham           OptionGroup() {}
213b842f2ecSJim Ingham 
214b842f2ecSJim Ingham   ~BreakpointDummyOptionGroup() override = default;
215b842f2ecSJim Ingham 
216b842f2ecSJim Ingham   llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
217b842f2ecSJim Ingham     return llvm::makeArrayRef(g_breakpoint_dummy_options);
218b842f2ecSJim Ingham   }
219b842f2ecSJim Ingham 
220b842f2ecSJim Ingham   Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
221b842f2ecSJim Ingham                           ExecutionContext *execution_context) override {
222b842f2ecSJim Ingham     Status error;
223b842f2ecSJim Ingham     const int short_option = g_breakpoint_modify_options[option_idx].short_option;
224b842f2ecSJim Ingham 
225b842f2ecSJim Ingham     switch (short_option) {
226b842f2ecSJim Ingham       case 'D':
227b842f2ecSJim Ingham         m_use_dummy = true;
228b842f2ecSJim Ingham         break;
229b842f2ecSJim Ingham     default:
230b842f2ecSJim Ingham       error.SetErrorStringWithFormat("unrecognized option '%c'",
231b842f2ecSJim Ingham                                      short_option);
232b842f2ecSJim Ingham       break;
233b842f2ecSJim Ingham     }
234b842f2ecSJim Ingham 
235b842f2ecSJim Ingham     return error;
236b842f2ecSJim Ingham   }
237b842f2ecSJim Ingham 
238b842f2ecSJim Ingham   void OptionParsingStarting(ExecutionContext *execution_context) override {
239b842f2ecSJim Ingham     m_use_dummy = false;
240b842f2ecSJim Ingham   }
241b842f2ecSJim Ingham 
242b842f2ecSJim Ingham   bool m_use_dummy;
243b842f2ecSJim Ingham 
244b842f2ecSJim Ingham };
245b842f2ecSJim Ingham 
2461f0f5b5bSZachary Turner // If an additional option set beyond LLDB_OPTION_SET_10 is added, make sure to
2471f0f5b5bSZachary Turner // update the numbers passed to LLDB_OPT_SET_FROM_TO(...) appropriately.
2481f0f5b5bSZachary Turner #define LLDB_OPT_FILE (LLDB_OPT_SET_FROM_TO(1, 9) & ~LLDB_OPT_SET_2)
2491f0f5b5bSZachary Turner #define LLDB_OPT_NOT_10 (LLDB_OPT_SET_FROM_TO(1, 10) & ~LLDB_OPT_SET_10)
2501f0f5b5bSZachary Turner #define LLDB_OPT_SKIP_PROLOGUE (LLDB_OPT_SET_1 | LLDB_OPT_SET_FROM_TO(3, 8))
2511f0f5b5bSZachary Turner #define LLDB_OPT_OFFSET_APPLIES (LLDB_OPT_SET_1 | LLDB_OPT_SET_FROM_TO(3, 8))
2521f0f5b5bSZachary Turner #define LLDB_OPT_MOVE_TO_NEAREST_CODE (LLDB_OPT_SET_1 | LLDB_OPT_SET_9)
2531f0f5b5bSZachary Turner #define LLDB_OPT_EXPR_LANGUAGE (LLDB_OPT_SET_FROM_TO(3, 8))
2541f0f5b5bSZachary Turner 
2551f0f5b5bSZachary Turner static OptionDefinition g_breakpoint_set_options[] = {
2561f0f5b5bSZachary Turner     // clang-format off
2571f0f5b5bSZachary Turner   { LLDB_OPT_NOT_10,               false, "shlib",                  's', OptionParser::eRequiredArgument, nullptr, nullptr, CommandCompletions::eModuleCompletion,     eArgTypeShlibName,           "Set the breakpoint only in this shared library.  Can repeat this option "
2581f0f5b5bSZachary Turner   "multiple times to specify multiple shared libraries." },
2591f0f5b5bSZachary Turner   { LLDB_OPT_SET_ALL,              false, "hardware",               'H', OptionParser::eNoArgument,       nullptr, nullptr, 0,                                         eArgTypeNone,                "Require the breakpoint to use hardware breakpoints." },
2601f0f5b5bSZachary Turner   { LLDB_OPT_FILE,                 false, "file",                   'f', OptionParser::eRequiredArgument, nullptr, nullptr, CommandCompletions::eSourceFileCompletion, eArgTypeFilename,            "Specifies the source file in which to set this breakpoint.  Note, by default "
2611f0f5b5bSZachary Turner   "lldb only looks for files that are #included if they use the standard include "
2621f0f5b5bSZachary Turner   "file extensions.  To set breakpoints on .c/.cpp/.m/.mm files that are "
2631f0f5b5bSZachary Turner   "#included, set target.inline-breakpoint-strategy to \"always\"." },
2641f0f5b5bSZachary Turner   { LLDB_OPT_SET_1,                true,  "line",                   'l', OptionParser::eRequiredArgument, nullptr, nullptr, 0,                                         eArgTypeLineNum,             "Specifies the line number on which to set this breakpoint." },
2651f0f5b5bSZachary Turner 
26605097246SAdrian Prantl   // Comment out this option for the moment, as we don't actually use it, but
26705097246SAdrian Prantl   // will in the future. This way users won't see it, but the infrastructure is
26805097246SAdrian Prantl   // left in place.
2691f0f5b5bSZachary Turner   //    { 0, false, "column",     'C', OptionParser::eRequiredArgument, nullptr, "<column>",
2701f0f5b5bSZachary Turner   //    "Set the breakpoint by source location at this particular column."},
2711f0f5b5bSZachary Turner 
2721f0f5b5bSZachary Turner   { LLDB_OPT_SET_2,                true,  "address",                'a', OptionParser::eRequiredArgument, nullptr, nullptr, 0,                                         eArgTypeAddressOrExpression, "Set the breakpoint at the specified address.  If the address maps uniquely to "
2731f0f5b5bSZachary Turner   "a particular binary, then the address will be converted to a \"file\" "
2741f0f5b5bSZachary Turner   "address, so that the breakpoint will track that binary+offset no matter where "
2751f0f5b5bSZachary Turner   "the binary eventually loads.  Alternately, if you also specify the module - "
2761f0f5b5bSZachary Turner   "with the -s option - then the address will be treated as a file address in "
2771f0f5b5bSZachary Turner   "that module, and resolved accordingly.  Again, this will allow lldb to track "
2781f0f5b5bSZachary Turner   "that offset on subsequent reloads.  The module need not have been loaded at "
2791f0f5b5bSZachary Turner   "the time you specify this breakpoint, and will get resolved when the module "
2801f0f5b5bSZachary Turner   "is loaded." },
2811f0f5b5bSZachary Turner   { LLDB_OPT_SET_3,                true,  "name",                   'n', OptionParser::eRequiredArgument, nullptr, nullptr, CommandCompletions::eSymbolCompletion,     eArgTypeFunctionName,        "Set the breakpoint by function name.  Can be repeated multiple times to make "
2821f0f5b5bSZachary Turner   "one breakpoint for multiple names" },
2831f0f5b5bSZachary Turner   { LLDB_OPT_SET_9,                false, "source-regexp-function", 'X', OptionParser::eRequiredArgument, nullptr, nullptr, CommandCompletions::eSymbolCompletion,     eArgTypeFunctionName,        "When used with '-p' limits the source regex to source contained in the named "
2841f0f5b5bSZachary Turner   "functions.  Can be repeated multiple times." },
2851f0f5b5bSZachary Turner   { LLDB_OPT_SET_4,                true,  "fullname",               'F', OptionParser::eRequiredArgument, nullptr, nullptr, CommandCompletions::eSymbolCompletion,     eArgTypeFullName,            "Set the breakpoint by fully qualified function names. For C++ this means "
286*4e8be2c9SAdrian Prantl   "namespaces and all arguments, and for Objective-C this means a full function "
2871f0f5b5bSZachary Turner   "prototype with class and selector.  Can be repeated multiple times to make "
2881f0f5b5bSZachary Turner   "one breakpoint for multiple names." },
2891f0f5b5bSZachary Turner   { LLDB_OPT_SET_5,                true,  "selector",               'S', OptionParser::eRequiredArgument, nullptr, nullptr, 0,                                         eArgTypeSelector,            "Set the breakpoint by ObjC selector name. Can be repeated multiple times to "
2901f0f5b5bSZachary Turner   "make one breakpoint for multiple Selectors." },
2911f0f5b5bSZachary Turner   { LLDB_OPT_SET_6,                true,  "method",                 'M', OptionParser::eRequiredArgument, nullptr, nullptr, 0,                                         eArgTypeMethod,              "Set the breakpoint by C++ method names.  Can be repeated multiple times to "
2921f0f5b5bSZachary Turner   "make one breakpoint for multiple methods." },
2931f0f5b5bSZachary Turner   { LLDB_OPT_SET_7,                true,  "func-regex",             'r', OptionParser::eRequiredArgument, nullptr, nullptr, 0,                                         eArgTypeRegularExpression,   "Set the breakpoint by function name, evaluating a regular-expression to find "
2941f0f5b5bSZachary Turner   "the function name(s)." },
2951f0f5b5bSZachary Turner   { LLDB_OPT_SET_8,                true,  "basename",               'b', OptionParser::eRequiredArgument, nullptr, nullptr, CommandCompletions::eSymbolCompletion,     eArgTypeFunctionName,        "Set the breakpoint by function basename (C++ namespaces and arguments will be "
2961f0f5b5bSZachary Turner   "ignored).  Can be repeated multiple times to make one breakpoint for multiple "
2971f0f5b5bSZachary Turner   "symbols." },
2981f0f5b5bSZachary Turner   { LLDB_OPT_SET_9,                true,  "source-pattern-regexp",  'p', OptionParser::eRequiredArgument, nullptr, nullptr, 0,                                         eArgTypeRegularExpression,   "Set the breakpoint by specifying a regular expression which is matched "
2991f0f5b5bSZachary Turner   "against the source text in a source file or files specified with the -f "
3001f0f5b5bSZachary Turner   "option.  The -f option can be specified more than once.  If no source files "
3011f0f5b5bSZachary Turner   "are specified, uses the current \"default source file\".  If you want to "
3021f0f5b5bSZachary Turner   "match against all source files, pass the \"--all-files\" option." },
3031f0f5b5bSZachary Turner   { LLDB_OPT_SET_9,                false, "all-files",              'A', OptionParser::eNoArgument,       nullptr, nullptr, 0,                                         eArgTypeNone,                "All files are searched for source pattern matches." },
3041f0f5b5bSZachary Turner   { LLDB_OPT_SET_10,               true,  "language-exception",     'E', OptionParser::eRequiredArgument, nullptr, nullptr, 0,                                         eArgTypeLanguage,            "Set the breakpoint on exceptions thrown by the specified language (without "
3051f0f5b5bSZachary Turner   "options, on throw but not catch.)" },
3061f0f5b5bSZachary Turner   { LLDB_OPT_SET_10,               false, "on-throw",               'w', OptionParser::eRequiredArgument, nullptr, nullptr, 0,                                         eArgTypeBoolean,             "Set the breakpoint on exception throW." },
3071f0f5b5bSZachary Turner   { LLDB_OPT_SET_10,               false, "on-catch",               'h', OptionParser::eRequiredArgument, nullptr, nullptr, 0,                                         eArgTypeBoolean,             "Set the breakpoint on exception catcH." },
3081f0f5b5bSZachary Turner 
3091f0f5b5bSZachary Turner   //  Don't add this option till it actually does something useful...
3101f0f5b5bSZachary Turner   //    { LLDB_OPT_SET_10, false, "exception-typename", 'O', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeTypeName,
3111f0f5b5bSZachary Turner   //        "The breakpoint will only stop if an exception Object of this type is thrown.  Can be repeated multiple times to stop for multiple object types" },
3121f0f5b5bSZachary Turner 
3131f0f5b5bSZachary Turner   { LLDB_OPT_EXPR_LANGUAGE,        false, "language",               'L', OptionParser::eRequiredArgument, nullptr, nullptr, 0,                                         eArgTypeLanguage,            "Specifies the Language to use when interpreting the breakpoint's expression "
3141f0f5b5bSZachary Turner   "(note: currently only implemented for setting breakpoints on identifiers).  "
3151f0f5b5bSZachary Turner   "If not set the target.language setting is used." },
3161f0f5b5bSZachary Turner   { LLDB_OPT_SKIP_PROLOGUE,        false, "skip-prologue",          'K', OptionParser::eRequiredArgument, nullptr, nullptr, 0,                                         eArgTypeBoolean,             "sKip the prologue if the breakpoint is at the beginning of a function.  "
3171f0f5b5bSZachary Turner   "If not set the target.skip-prologue setting is used." },
3181f0f5b5bSZachary Turner   { LLDB_OPT_SET_ALL,              false, "breakpoint-name",        'N', OptionParser::eRequiredArgument, nullptr, nullptr, 0,                                         eArgTypeBreakpointName,      "Adds this to the list of names for this breakpoint." },
3191f0f5b5bSZachary Turner   { LLDB_OPT_OFFSET_APPLIES,       false, "address-slide",          'R', OptionParser::eRequiredArgument, nullptr, nullptr, 0,                                         eArgTypeAddress,             "Add the specified offset to whatever address(es) the breakpoint resolves to.  "
3201f0f5b5bSZachary Turner   "At present this applies the offset directly as given, and doesn't try to align it to instruction boundaries." },
3211f0f5b5bSZachary Turner   { LLDB_OPT_MOVE_TO_NEAREST_CODE, false, "move-to-nearest-code", 'm', OptionParser::eRequiredArgument,   nullptr, nullptr, 0,                                         eArgTypeBoolean,             "Move breakpoints to nearest code. If not set the target.move-to-nearest-code "
3221f0f5b5bSZachary Turner   "setting is used." },
3231f0f5b5bSZachary Turner     // clang-format on
3241f0f5b5bSZachary Turner };
3251f0f5b5bSZachary Turner 
32630fdc8d8SChris Lattner //-------------------------------------------------------------------------
3275a988416SJim Ingham // CommandObjectBreakpointSet
32830fdc8d8SChris Lattner //-------------------------------------------------------------------------
32930fdc8d8SChris Lattner 
330b9c1b51eSKate Stone class CommandObjectBreakpointSet : public CommandObjectParsed {
3315a988416SJim Ingham public:
332b9c1b51eSKate Stone   typedef enum BreakpointSetType {
3335a988416SJim Ingham     eSetTypeInvalid,
3345a988416SJim Ingham     eSetTypeFileAndLine,
3355a988416SJim Ingham     eSetTypeAddress,
3365a988416SJim Ingham     eSetTypeFunctionName,
3375a988416SJim Ingham     eSetTypeFunctionRegexp,
3385a988416SJim Ingham     eSetTypeSourceRegexp,
3395a988416SJim Ingham     eSetTypeException
3405a988416SJim Ingham   } BreakpointSetType;
3415a988416SJim Ingham 
342b9c1b51eSKate Stone   CommandObjectBreakpointSet(CommandInterpreter &interpreter)
343b9c1b51eSKate Stone       : CommandObjectParsed(
344b9c1b51eSKate Stone             interpreter, "breakpoint set",
3455a988416SJim Ingham             "Sets a breakpoint or set of breakpoints in the executable.",
3465a988416SJim Ingham             "breakpoint set <cmd-options>"),
347b842f2ecSJim Ingham         m_bp_opts(), m_options() {
348b842f2ecSJim Ingham           // We're picking up all the normal options, commands and disable.
349b842f2ecSJim Ingham           m_all_options.Append(&m_bp_opts,
350b842f2ecSJim Ingham                                LLDB_OPT_SET_1 | LLDB_OPT_SET_3 | LLDB_OPT_SET_4,
351b842f2ecSJim Ingham                                LLDB_OPT_SET_ALL);
352b842f2ecSJim Ingham           m_all_options.Append(&m_dummy_options, LLDB_OPT_SET_1, LLDB_OPT_SET_ALL);
353b842f2ecSJim Ingham           m_all_options.Append(&m_options);
354b842f2ecSJim Ingham           m_all_options.Finalize();
355b842f2ecSJim Ingham         }
3565a988416SJim Ingham 
3579e85e5a8SEugene Zelenko   ~CommandObjectBreakpointSet() override = default;
3585a988416SJim Ingham 
359b842f2ecSJim Ingham   Options *GetOptions() override { return &m_all_options; }
3605a988416SJim Ingham 
361b842f2ecSJim Ingham   class CommandOptions : public OptionGroup {
3625a988416SJim Ingham   public:
363b9c1b51eSKate Stone     CommandOptions()
364b842f2ecSJim Ingham         : OptionGroup(), m_condition(), m_filenames(), m_line_num(0), m_column(0),
365b9c1b51eSKate Stone           m_func_names(), m_func_name_type_mask(eFunctionNameTypeNone),
366b9c1b51eSKate Stone           m_func_regexp(), m_source_text_regexp(), m_modules(), m_load_addr(),
367b9c1b51eSKate Stone           m_catch_bp(false), m_throw_bp(true), m_hardware(false),
368a72b31c7SJim Ingham           m_exception_language(eLanguageTypeUnknown),
36923b1decbSDawn Perchik           m_language(lldb::eLanguageTypeUnknown),
370b842f2ecSJim Ingham           m_skip_prologue(eLazyBoolCalculate),
371b9c1b51eSKate Stone           m_all_files(false), m_move_to_nearest_code(eLazyBoolCalculate) {}
37230fdc8d8SChris Lattner 
3739e85e5a8SEugene Zelenko     ~CommandOptions() override = default;
37487df91b8SJim Ingham 
37597206d57SZachary Turner     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
376b9c1b51eSKate Stone                           ExecutionContext *execution_context) override {
37797206d57SZachary Turner       Status error;
378b842f2ecSJim Ingham       const int short_option = g_breakpoint_set_options[option_idx].short_option;
37930fdc8d8SChris Lattner 
380b9c1b51eSKate Stone       switch (short_option) {
381b9c1b51eSKate Stone       case 'a': {
38247cbf4a0SPavel Labath         m_load_addr = OptionArgParser::ToAddress(execution_context, option_arg,
383e1cfbc79STodd Fiala                                                  LLDB_INVALID_ADDRESS, &error);
384b9c1b51eSKate Stone       } break;
38530fdc8d8SChris Lattner 
386e732052fSJim Ingham       case 'A':
387e732052fSJim Ingham         m_all_files = true;
388e732052fSJim Ingham         break;
389e732052fSJim Ingham 
390ca36cd16SJim Ingham       case 'b':
391ca36cd16SJim Ingham         m_func_names.push_back(option_arg);
392ca36cd16SJim Ingham         m_func_name_type_mask |= eFunctionNameTypeBase;
393ca36cd16SJim Ingham         break;
394ca36cd16SJim Ingham 
395fe11483bSZachary Turner       case 'C':
396fe11483bSZachary Turner         if (option_arg.getAsInteger(0, m_column))
397b9c1b51eSKate Stone           error.SetErrorStringWithFormat("invalid column number: %s",
398fe11483bSZachary Turner                                          option_arg.str().c_str());
39930fdc8d8SChris Lattner         break;
4009e85e5a8SEugene Zelenko 
401b9c1b51eSKate Stone       case 'E': {
402fe11483bSZachary Turner         LanguageType language = Language::GetLanguageTypeFromString(option_arg);
403fab10e89SJim Ingham 
404b9c1b51eSKate Stone         switch (language) {
405fab10e89SJim Ingham         case eLanguageTypeC89:
406fab10e89SJim Ingham         case eLanguageTypeC:
407fab10e89SJim Ingham         case eLanguageTypeC99:
4081d0089faSBruce Mitchener         case eLanguageTypeC11:
409a72b31c7SJim Ingham           m_exception_language = eLanguageTypeC;
410fab10e89SJim Ingham           break;
411fab10e89SJim Ingham         case eLanguageTypeC_plus_plus:
4121d0089faSBruce Mitchener         case eLanguageTypeC_plus_plus_03:
4131d0089faSBruce Mitchener         case eLanguageTypeC_plus_plus_11:
4142ba84a6aSBruce Mitchener         case eLanguageTypeC_plus_plus_14:
415a72b31c7SJim Ingham           m_exception_language = eLanguageTypeC_plus_plus;
416fab10e89SJim Ingham           break;
417fab10e89SJim Ingham         case eLanguageTypeObjC:
418a72b31c7SJim Ingham           m_exception_language = eLanguageTypeObjC;
419fab10e89SJim Ingham           break;
420fab10e89SJim Ingham         case eLanguageTypeObjC_plus_plus:
421b9c1b51eSKate Stone           error.SetErrorStringWithFormat(
422b9c1b51eSKate Stone               "Set exception breakpoints separately for c++ and objective-c");
423fab10e89SJim Ingham           break;
424fab10e89SJim Ingham         case eLanguageTypeUnknown:
425b9c1b51eSKate Stone           error.SetErrorStringWithFormat(
426b9c1b51eSKate Stone               "Unknown language type: '%s' for exception breakpoint",
427fe11483bSZachary Turner               option_arg.str().c_str());
428fab10e89SJim Ingham           break;
429fab10e89SJim Ingham         default:
430b9c1b51eSKate Stone           error.SetErrorStringWithFormat(
431b9c1b51eSKate Stone               "Unsupported language type: '%s' for exception breakpoint",
432fe11483bSZachary Turner               option_arg.str().c_str());
433fab10e89SJim Ingham         }
434b9c1b51eSKate Stone       } break;
435ca36cd16SJim Ingham 
436ca36cd16SJim Ingham       case 'f':
437ca36cd16SJim Ingham         m_filenames.AppendIfUnique(FileSpec(option_arg, false));
438fab10e89SJim Ingham         break;
439ca36cd16SJim Ingham 
440ca36cd16SJim Ingham       case 'F':
441ca36cd16SJim Ingham         m_func_names.push_back(option_arg);
442ca36cd16SJim Ingham         m_func_name_type_mask |= eFunctionNameTypeFull;
443ca36cd16SJim Ingham         break;
444ca36cd16SJim Ingham 
445b9c1b51eSKate Stone       case 'h': {
446fab10e89SJim Ingham         bool success;
44747cbf4a0SPavel Labath         m_catch_bp = OptionArgParser::ToBoolean(option_arg, true, &success);
448fab10e89SJim Ingham         if (!success)
449b9c1b51eSKate Stone           error.SetErrorStringWithFormat(
450fe11483bSZachary Turner               "Invalid boolean value for on-catch option: '%s'",
451fe11483bSZachary Turner               option_arg.str().c_str());
452b9c1b51eSKate Stone       } break;
453eb023e75SGreg Clayton 
454eb023e75SGreg Clayton       case 'H':
455eb023e75SGreg Clayton         m_hardware = true;
456eb023e75SGreg Clayton         break;
457eb023e75SGreg Clayton 
458b9c1b51eSKate Stone       case 'K': {
459a8558b62SJim Ingham         bool success;
460a8558b62SJim Ingham         bool value;
46147cbf4a0SPavel Labath         value = OptionArgParser::ToBoolean(option_arg, true, &success);
462a8558b62SJim Ingham         if (value)
463a8558b62SJim Ingham           m_skip_prologue = eLazyBoolYes;
464a8558b62SJim Ingham         else
465a8558b62SJim Ingham           m_skip_prologue = eLazyBoolNo;
466a8558b62SJim Ingham 
467a8558b62SJim Ingham         if (!success)
468b9c1b51eSKate Stone           error.SetErrorStringWithFormat(
469b9c1b51eSKate Stone               "Invalid boolean value for skip prologue option: '%s'",
470fe11483bSZachary Turner               option_arg.str().c_str());
471b9c1b51eSKate Stone       } break;
472ca36cd16SJim Ingham 
473fe11483bSZachary Turner       case 'l':
474fe11483bSZachary Turner         if (option_arg.getAsInteger(0, m_line_num))
475b9c1b51eSKate Stone           error.SetErrorStringWithFormat("invalid line number: %s.",
476fe11483bSZachary Turner                                          option_arg.str().c_str());
477ca36cd16SJim Ingham         break;
478055ad9beSIlia K 
47923b1decbSDawn Perchik       case 'L':
480fe11483bSZachary Turner         m_language = Language::GetLanguageTypeFromString(option_arg);
48123b1decbSDawn Perchik         if (m_language == eLanguageTypeUnknown)
482b9c1b51eSKate Stone           error.SetErrorStringWithFormat(
483fe11483bSZachary Turner               "Unknown language type: '%s' for breakpoint",
484fe11483bSZachary Turner               option_arg.str().c_str());
48523b1decbSDawn Perchik         break;
48623b1decbSDawn Perchik 
487b9c1b51eSKate Stone       case 'm': {
488055ad9beSIlia K         bool success;
489055ad9beSIlia K         bool value;
49047cbf4a0SPavel Labath         value = OptionArgParser::ToBoolean(option_arg, true, &success);
491055ad9beSIlia K         if (value)
492055ad9beSIlia K           m_move_to_nearest_code = eLazyBoolYes;
493055ad9beSIlia K         else
494055ad9beSIlia K           m_move_to_nearest_code = eLazyBoolNo;
495055ad9beSIlia K 
496055ad9beSIlia K         if (!success)
497b9c1b51eSKate Stone           error.SetErrorStringWithFormat(
498b9c1b51eSKate Stone               "Invalid boolean value for move-to-nearest-code option: '%s'",
499fe11483bSZachary Turner               option_arg.str().c_str());
500055ad9beSIlia K         break;
501055ad9beSIlia K       }
502055ad9beSIlia K 
503ca36cd16SJim Ingham       case 'M':
504ca36cd16SJim Ingham         m_func_names.push_back(option_arg);
505ca36cd16SJim Ingham         m_func_name_type_mask |= eFunctionNameTypeMethod;
506ca36cd16SJim Ingham         break;
507ca36cd16SJim Ingham 
508ca36cd16SJim Ingham       case 'n':
509ca36cd16SJim Ingham         m_func_names.push_back(option_arg);
510ca36cd16SJim Ingham         m_func_name_type_mask |= eFunctionNameTypeAuto;
511ca36cd16SJim Ingham         break;
512ca36cd16SJim Ingham 
5136fa7681bSZachary Turner       case 'N': {
514fe11483bSZachary Turner         if (BreakpointID::StringIsBreakpointName(option_arg, error))
5155e09c8c3SJim Ingham           m_breakpoint_names.push_back(option_arg);
516ff9a91eaSJim Ingham         else
517ff9a91eaSJim Ingham           error.SetErrorStringWithFormat("Invalid breakpoint name: %s",
518fe11483bSZachary Turner                                          option_arg.str().c_str());
5195e09c8c3SJim Ingham         break;
5206fa7681bSZachary Turner       }
5215e09c8c3SJim Ingham 
522b9c1b51eSKate Stone       case 'R': {
5232411167fSJim Ingham         lldb::addr_t tmp_offset_addr;
52447cbf4a0SPavel Labath         tmp_offset_addr = OptionArgParser::ToAddress(execution_context,
52547cbf4a0SPavel Labath                                                      option_arg, 0, &error);
5262411167fSJim Ingham         if (error.Success())
5272411167fSJim Ingham           m_offset_addr = tmp_offset_addr;
528b9c1b51eSKate Stone       } break;
5292411167fSJim Ingham 
530a72b31c7SJim Ingham       case 'O':
531fe11483bSZachary Turner         m_exception_extra_args.AppendArgument("-O");
532fe11483bSZachary Turner         m_exception_extra_args.AppendArgument(option_arg);
533a72b31c7SJim Ingham         break;
534a72b31c7SJim Ingham 
535ca36cd16SJim Ingham       case 'p':
536ca36cd16SJim Ingham         m_source_text_regexp.assign(option_arg);
537ca36cd16SJim Ingham         break;
538ca36cd16SJim Ingham 
539ca36cd16SJim Ingham       case 'r':
540ca36cd16SJim Ingham         m_func_regexp.assign(option_arg);
541ca36cd16SJim Ingham         break;
542ca36cd16SJim Ingham 
543ca36cd16SJim Ingham       case 's':
544ca36cd16SJim Ingham         m_modules.AppendIfUnique(FileSpec(option_arg, false));
545ca36cd16SJim Ingham         break;
546ca36cd16SJim Ingham 
547ca36cd16SJim Ingham       case 'S':
548ca36cd16SJim Ingham         m_func_names.push_back(option_arg);
549ca36cd16SJim Ingham         m_func_name_type_mask |= eFunctionNameTypeSelector;
550ca36cd16SJim Ingham         break;
551ca36cd16SJim Ingham 
552b9c1b51eSKate Stone       case 'w': {
553ca36cd16SJim Ingham         bool success;
55447cbf4a0SPavel Labath         m_throw_bp = OptionArgParser::ToBoolean(option_arg, true, &success);
555ca36cd16SJim Ingham         if (!success)
556b9c1b51eSKate Stone           error.SetErrorStringWithFormat(
557fe11483bSZachary Turner               "Invalid boolean value for on-throw option: '%s'",
558fe11483bSZachary Turner               option_arg.str().c_str());
559b9c1b51eSKate Stone       } break;
560ca36cd16SJim Ingham 
56176bb8d67SJim Ingham       case 'X':
56276bb8d67SJim Ingham         m_source_regex_func_names.insert(option_arg);
56376bb8d67SJim Ingham         break;
56476bb8d67SJim Ingham 
56530fdc8d8SChris Lattner       default:
566b9c1b51eSKate Stone         error.SetErrorStringWithFormat("unrecognized option '%c'",
567b9c1b51eSKate Stone                                        short_option);
56830fdc8d8SChris Lattner         break;
56930fdc8d8SChris Lattner       }
57030fdc8d8SChris Lattner 
57130fdc8d8SChris Lattner       return error;
57230fdc8d8SChris Lattner     }
5739e85e5a8SEugene Zelenko 
574b9c1b51eSKate Stone     void OptionParsingStarting(ExecutionContext *execution_context) override {
57587df91b8SJim Ingham       m_filenames.Clear();
57630fdc8d8SChris Lattner       m_line_num = 0;
57730fdc8d8SChris Lattner       m_column = 0;
578fab10e89SJim Ingham       m_func_names.clear();
5791f746071SGreg Clayton       m_func_name_type_mask = eFunctionNameTypeNone;
58030fdc8d8SChris Lattner       m_func_regexp.clear();
5811f746071SGreg Clayton       m_source_text_regexp.clear();
58287df91b8SJim Ingham       m_modules.Clear();
5831f746071SGreg Clayton       m_load_addr = LLDB_INVALID_ADDRESS;
5842411167fSJim Ingham       m_offset_addr = 0;
585fab10e89SJim Ingham       m_catch_bp = false;
586fab10e89SJim Ingham       m_throw_bp = true;
587eb023e75SGreg Clayton       m_hardware = false;
588a72b31c7SJim Ingham       m_exception_language = eLanguageTypeUnknown;
58923b1decbSDawn Perchik       m_language = lldb::eLanguageTypeUnknown;
590a8558b62SJim Ingham       m_skip_prologue = eLazyBoolCalculate;
5915e09c8c3SJim Ingham       m_breakpoint_names.clear();
592e732052fSJim Ingham       m_all_files = false;
593a72b31c7SJim Ingham       m_exception_extra_args.Clear();
594055ad9beSIlia K       m_move_to_nearest_code = eLazyBoolCalculate;
59576bb8d67SJim Ingham       m_source_regex_func_names.clear();
59630fdc8d8SChris Lattner     }
59730fdc8d8SChris Lattner 
5981f0f5b5bSZachary Turner     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
59970602439SZachary Turner       return llvm::makeArrayRef(g_breakpoint_set_options);
6001f0f5b5bSZachary Turner     }
60130fdc8d8SChris Lattner 
6025a988416SJim Ingham     // Instance variables to hold the values for command options.
603969795f1SJim Ingham 
6045a988416SJim Ingham     std::string m_condition;
6055a988416SJim Ingham     FileSpecList m_filenames;
6065a988416SJim Ingham     uint32_t m_line_num;
6075a988416SJim Ingham     uint32_t m_column;
6085a988416SJim Ingham     std::vector<std::string> m_func_names;
6095e09c8c3SJim Ingham     std::vector<std::string> m_breakpoint_names;
6105a988416SJim Ingham     uint32_t m_func_name_type_mask;
6115a988416SJim Ingham     std::string m_func_regexp;
6125a988416SJim Ingham     std::string m_source_text_regexp;
6135a988416SJim Ingham     FileSpecList m_modules;
6145a988416SJim Ingham     lldb::addr_t m_load_addr;
6152411167fSJim Ingham     lldb::addr_t m_offset_addr;
6165a988416SJim Ingham     bool m_catch_bp;
6175a988416SJim Ingham     bool m_throw_bp;
618eb023e75SGreg Clayton     bool m_hardware; // Request to use hardware breakpoints
619a72b31c7SJim Ingham     lldb::LanguageType m_exception_language;
62023b1decbSDawn Perchik     lldb::LanguageType m_language;
6215a988416SJim Ingham     LazyBool m_skip_prologue;
622e732052fSJim Ingham     bool m_all_files;
623a72b31c7SJim Ingham     Args m_exception_extra_args;
624055ad9beSIlia K     LazyBool m_move_to_nearest_code;
62576bb8d67SJim Ingham     std::unordered_set<std::string> m_source_regex_func_names;
6265a988416SJim Ingham   };
6275a988416SJim Ingham 
6285a988416SJim Ingham protected:
629b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
630b842f2ecSJim Ingham     Target *target = GetSelectedOrDummyTarget(m_dummy_options.m_use_dummy);
63133df7cd3SJim Ingham 
632b9c1b51eSKate Stone     if (target == nullptr) {
633b9c1b51eSKate Stone       result.AppendError("Invalid target.  Must set target before setting "
634b9c1b51eSKate Stone                          "breakpoints (see 'target create' command).");
63530fdc8d8SChris Lattner       result.SetStatus(eReturnStatusFailed);
63630fdc8d8SChris Lattner       return false;
63730fdc8d8SChris Lattner     }
63830fdc8d8SChris Lattner 
63930fdc8d8SChris Lattner     // The following are the various types of breakpoints that could be set:
64030fdc8d8SChris Lattner     //   1).  -f -l -p  [-s -g]   (setting breakpoint by source location)
64130fdc8d8SChris Lattner     //   2).  -a  [-s -g]         (setting breakpoint by address)
64230fdc8d8SChris Lattner     //   3).  -n  [-s -g]         (setting breakpoint by function name)
643b9c1b51eSKate Stone     //   4).  -r  [-s -g]         (setting breakpoint by function name regular
644b9c1b51eSKate Stone     //   expression)
645b9c1b51eSKate Stone     //   5).  -p -f               (setting a breakpoint by comparing a reg-exp
646b9c1b51eSKate Stone     //   to source text)
647b9c1b51eSKate Stone     //   6).  -E [-w -h]          (setting a breakpoint for exceptions for a
648b9c1b51eSKate Stone     //   given language.)
64930fdc8d8SChris Lattner 
65030fdc8d8SChris Lattner     BreakpointSetType break_type = eSetTypeInvalid;
65130fdc8d8SChris Lattner 
65230fdc8d8SChris Lattner     if (m_options.m_line_num != 0)
65330fdc8d8SChris Lattner       break_type = eSetTypeFileAndLine;
65430fdc8d8SChris Lattner     else if (m_options.m_load_addr != LLDB_INVALID_ADDRESS)
65530fdc8d8SChris Lattner       break_type = eSetTypeAddress;
656fab10e89SJim Ingham     else if (!m_options.m_func_names.empty())
65730fdc8d8SChris Lattner       break_type = eSetTypeFunctionName;
65830fdc8d8SChris Lattner     else if (!m_options.m_func_regexp.empty())
65930fdc8d8SChris Lattner       break_type = eSetTypeFunctionRegexp;
660969795f1SJim Ingham     else if (!m_options.m_source_text_regexp.empty())
661969795f1SJim Ingham       break_type = eSetTypeSourceRegexp;
662a72b31c7SJim Ingham     else if (m_options.m_exception_language != eLanguageTypeUnknown)
663fab10e89SJim Ingham       break_type = eSetTypeException;
66430fdc8d8SChris Lattner 
665b842f2ecSJim Ingham     BreakpointSP bp_sp = nullptr;
666274060b6SGreg Clayton     FileSpec module_spec;
667a8558b62SJim Ingham     const bool internal = false;
668a8558b62SJim Ingham 
669b9c1b51eSKate Stone     // If the user didn't specify skip-prologue, having an offset should turn
670b9c1b51eSKate Stone     // that off.
671b9c1b51eSKate Stone     if (m_options.m_offset_addr != 0 &&
672b9c1b51eSKate Stone         m_options.m_skip_prologue == eLazyBoolCalculate)
6732411167fSJim Ingham       m_options.m_skip_prologue = eLazyBoolNo;
6742411167fSJim Ingham 
675b9c1b51eSKate Stone     switch (break_type) {
67630fdc8d8SChris Lattner     case eSetTypeFileAndLine: // Breakpoint by source position
67730fdc8d8SChris Lattner     {
67830fdc8d8SChris Lattner       FileSpec file;
679c7bece56SGreg Clayton       const size_t num_files = m_options.m_filenames.GetSize();
680b9c1b51eSKate Stone       if (num_files == 0) {
681b9c1b51eSKate Stone         if (!GetDefaultFile(target, file, result)) {
68287df91b8SJim Ingham           result.AppendError("No file supplied and no default file available.");
68387df91b8SJim Ingham           result.SetStatus(eReturnStatusFailed);
68487df91b8SJim Ingham           return false;
68587df91b8SJim Ingham         }
686b9c1b51eSKate Stone       } else if (num_files > 1) {
687b9c1b51eSKate Stone         result.AppendError("Only one file at a time is allowed for file and "
688b9c1b51eSKate Stone                            "line breakpoints.");
68987df91b8SJim Ingham         result.SetStatus(eReturnStatusFailed);
69087df91b8SJim Ingham         return false;
691b9c1b51eSKate Stone       } else
69287df91b8SJim Ingham         file = m_options.m_filenames.GetFileSpecAtIndex(0);
69330fdc8d8SChris Lattner 
6941f746071SGreg Clayton       // Only check for inline functions if
6951f746071SGreg Clayton       LazyBool check_inlines = eLazyBoolCalculate;
6961f746071SGreg Clayton 
697b842f2ecSJim Ingham       bp_sp = target->CreateBreakpoint(&(m_options.m_modules),
698b842f2ecSJim Ingham                                        file,
699b842f2ecSJim Ingham                                        m_options.m_line_num,
700b842f2ecSJim Ingham                                        m_options.m_offset_addr,
701b842f2ecSJim Ingham                                        check_inlines,
702b842f2ecSJim Ingham                                        m_options.m_skip_prologue,
703b842f2ecSJim Ingham                                        internal,
704b842f2ecSJim Ingham                                        m_options.m_hardware,
705b842f2ecSJim Ingham                                        m_options.m_move_to_nearest_code);
706b9c1b51eSKate Stone     } break;
7076eee5aa0SGreg Clayton 
70830fdc8d8SChris Lattner     case eSetTypeAddress: // Breakpoint by address
709055a08a4SJim Ingham     {
710b9c1b51eSKate Stone       // If a shared library has been specified, make an lldb_private::Address
711b842f2ecSJim Ingham       // with the library, and use that.  That way the address breakpoint
712b842f2ecSJim Ingham       //  will track the load location of the library.
713055a08a4SJim Ingham       size_t num_modules_specified = m_options.m_modules.GetSize();
714b9c1b51eSKate Stone       if (num_modules_specified == 1) {
715b9c1b51eSKate Stone         const FileSpec *file_spec =
716b9c1b51eSKate Stone             m_options.m_modules.GetFileSpecPointerAtIndex(0);
717b842f2ecSJim Ingham         bp_sp = target->CreateAddressInModuleBreakpoint(m_options.m_load_addr,
718b9c1b51eSKate Stone                                                         internal, file_spec,
719b842f2ecSJim Ingham                                                         m_options.m_hardware);
720b9c1b51eSKate Stone       } else if (num_modules_specified == 0) {
721b842f2ecSJim Ingham         bp_sp = target->CreateBreakpoint(m_options.m_load_addr, internal,
722b842f2ecSJim Ingham                                          m_options.m_hardware);
723b9c1b51eSKate Stone       } else {
724b9c1b51eSKate Stone         result.AppendError("Only one shared library can be specified for "
725b9c1b51eSKate Stone                            "address breakpoints.");
726055a08a4SJim Ingham         result.SetStatus(eReturnStatusFailed);
727055a08a4SJim Ingham         return false;
728055a08a4SJim Ingham       }
72930fdc8d8SChris Lattner       break;
730055a08a4SJim Ingham     }
73130fdc8d8SChris Lattner     case eSetTypeFunctionName: // Breakpoint by function name
7320c5cd90dSGreg Clayton     {
7330c5cd90dSGreg Clayton       uint32_t name_type_mask = m_options.m_func_name_type_mask;
7340c5cd90dSGreg Clayton 
7350c5cd90dSGreg Clayton       if (name_type_mask == 0)
736e02b8504SGreg Clayton         name_type_mask = eFunctionNameTypeAuto;
7370c5cd90dSGreg Clayton 
738b842f2ecSJim Ingham       bp_sp = target->CreateBreakpoint(&(m_options.m_modules),
739b842f2ecSJim Ingham                                        &(m_options.m_filenames),
740b842f2ecSJim Ingham                                        m_options.m_func_names,
741b842f2ecSJim Ingham                                        name_type_mask,
742b842f2ecSJim Ingham                                        m_options.m_language,
743b842f2ecSJim Ingham                                        m_options.m_offset_addr,
744b842f2ecSJim Ingham                                        m_options.m_skip_prologue,
745b842f2ecSJim Ingham                                        internal,
746b842f2ecSJim Ingham                                        m_options.m_hardware);
747b9c1b51eSKate Stone     } break;
7480c5cd90dSGreg Clayton 
749b9c1b51eSKate Stone     case eSetTypeFunctionRegexp: // Breakpoint by regular expression function
750b9c1b51eSKate Stone                                  // name
75130fdc8d8SChris Lattner       {
75295eae423SZachary Turner         RegularExpression regexp(m_options.m_func_regexp);
753b9c1b51eSKate Stone         if (!regexp.IsValid()) {
754969795f1SJim Ingham           char err_str[1024];
755969795f1SJim Ingham           regexp.GetErrorAsCString(err_str, sizeof(err_str));
756b9c1b51eSKate Stone           result.AppendErrorWithFormat(
757b9c1b51eSKate Stone               "Function name regular expression could not be compiled: \"%s\"",
758969795f1SJim Ingham               err_str);
75930fdc8d8SChris Lattner           result.SetStatus(eReturnStatusFailed);
760969795f1SJim Ingham           return false;
76130fdc8d8SChris Lattner         }
76287df91b8SJim Ingham 
763b842f2ecSJim Ingham         bp_sp = target->CreateFuncRegexBreakpoint(&(m_options.m_modules),
764b842f2ecSJim Ingham                                                   &(m_options.m_filenames),
765b842f2ecSJim Ingham                                                   regexp,
766b842f2ecSJim Ingham                                                   m_options.m_language,
767b842f2ecSJim Ingham                                                   m_options.m_skip_prologue,
768b842f2ecSJim Ingham                                                   internal,
769b842f2ecSJim Ingham                                                   m_options.m_hardware);
770e14dc268SJim Ingham       }
771e14dc268SJim Ingham       break;
772969795f1SJim Ingham     case eSetTypeSourceRegexp: // Breakpoint by regexp on source text.
773969795f1SJim Ingham     {
774c7bece56SGreg Clayton       const size_t num_files = m_options.m_filenames.GetSize();
77587df91b8SJim Ingham 
776b9c1b51eSKate Stone       if (num_files == 0 && !m_options.m_all_files) {
777969795f1SJim Ingham         FileSpec file;
778b9c1b51eSKate Stone         if (!GetDefaultFile(target, file, result)) {
779b9c1b51eSKate Stone           result.AppendError(
780b9c1b51eSKate Stone               "No files provided and could not find default file.");
78187df91b8SJim Ingham           result.SetStatus(eReturnStatusFailed);
78287df91b8SJim Ingham           return false;
783b9c1b51eSKate Stone         } else {
78487df91b8SJim Ingham           m_options.m_filenames.Append(file);
78587df91b8SJim Ingham         }
78687df91b8SJim Ingham       }
7870c5cd90dSGreg Clayton 
78895eae423SZachary Turner       RegularExpression regexp(m_options.m_source_text_regexp);
789b9c1b51eSKate Stone       if (!regexp.IsValid()) {
790969795f1SJim Ingham         char err_str[1024];
791969795f1SJim Ingham         regexp.GetErrorAsCString(err_str, sizeof(err_str));
792b9c1b51eSKate Stone         result.AppendErrorWithFormat(
793b9c1b51eSKate Stone             "Source text regular expression could not be compiled: \"%s\"",
794969795f1SJim Ingham             err_str);
795969795f1SJim Ingham         result.SetStatus(eReturnStatusFailed);
796969795f1SJim Ingham         return false;
797969795f1SJim Ingham       }
798b842f2ecSJim Ingham       bp_sp =
799b842f2ecSJim Ingham           target->CreateSourceRegexBreakpoint(&(m_options.m_modules),
800b842f2ecSJim Ingham                                               &(m_options.m_filenames),
801b842f2ecSJim Ingham                                               m_options
802b842f2ecSJim Ingham                                                   .m_source_regex_func_names,
803b842f2ecSJim Ingham                                               regexp,
804b842f2ecSJim Ingham                                               internal,
805b842f2ecSJim Ingham                                               m_options.m_hardware,
806b842f2ecSJim Ingham                                               m_options.m_move_to_nearest_code);
807b9c1b51eSKate Stone     } break;
808b9c1b51eSKate Stone     case eSetTypeException: {
80997206d57SZachary Turner       Status precond_error;
810b842f2ecSJim Ingham       bp_sp = target->CreateExceptionBreakpoint(m_options.m_exception_language,
811b842f2ecSJim Ingham                                                 m_options.m_catch_bp,
812b842f2ecSJim Ingham                                                 m_options.m_throw_bp,
813b842f2ecSJim Ingham                                                 internal,
814b842f2ecSJim Ingham                                                 &m_options
815b842f2ecSJim Ingham                                                     .m_exception_extra_args,
816b842f2ecSJim Ingham                                                 &precond_error);
817b9c1b51eSKate Stone       if (precond_error.Fail()) {
818b9c1b51eSKate Stone         result.AppendErrorWithFormat(
819b9c1b51eSKate Stone             "Error setting extra exception arguments: %s",
820a72b31c7SJim Ingham             precond_error.AsCString());
821b842f2ecSJim Ingham         target->RemoveBreakpointByID(bp_sp->GetID());
822a72b31c7SJim Ingham         result.SetStatus(eReturnStatusFailed);
823a72b31c7SJim Ingham         return false;
824a72b31c7SJim Ingham       }
825b9c1b51eSKate Stone     } break;
82630fdc8d8SChris Lattner     default:
82730fdc8d8SChris Lattner       break;
82830fdc8d8SChris Lattner     }
82930fdc8d8SChris Lattner 
8301b54c88cSJim Ingham     // Now set the various options that were passed in:
831b842f2ecSJim Ingham     if (bp_sp) {
832b842f2ecSJim Ingham       bp_sp->GetOptions()->CopyOverSetOptions(m_bp_opts.GetBreakpointOptions());
833ca36cd16SJim Ingham 
834b9c1b51eSKate Stone       if (!m_options.m_breakpoint_names.empty()) {
83597206d57SZachary Turner         Status name_error;
836ff9a91eaSJim Ingham         for (auto name : m_options.m_breakpoint_names) {
837b842f2ecSJim Ingham           target->AddNameToBreakpoint(bp_sp, name.c_str(), name_error);
838ff9a91eaSJim Ingham           if (name_error.Fail()) {
839ff9a91eaSJim Ingham             result.AppendErrorWithFormat("Invalid breakpoint name: %s",
840ff9a91eaSJim Ingham                                          name.c_str());
841b842f2ecSJim Ingham             target->RemoveBreakpointByID(bp_sp->GetID());
842ff9a91eaSJim Ingham             result.SetStatus(eReturnStatusFailed);
843ff9a91eaSJim Ingham             return false;
844ff9a91eaSJim Ingham           }
845ff9a91eaSJim Ingham         }
8465e09c8c3SJim Ingham       }
8471b54c88cSJim Ingham     }
8481b54c88cSJim Ingham 
849b842f2ecSJim Ingham     if (bp_sp) {
85085e8b814SJim Ingham       Stream &output_stream = result.GetOutputStream();
8511391cc7dSJim Ingham       const bool show_locations = false;
852b842f2ecSJim Ingham       bp_sp->GetDescription(&output_stream, lldb::eDescriptionLevelInitial,
853b9c1b51eSKate Stone                          show_locations);
8544aeb1989SJim Ingham       if (target == m_interpreter.GetDebugger().GetDummyTarget())
855b9c1b51eSKate Stone         output_stream.Printf("Breakpoint set in dummy target, will get copied "
856b9c1b51eSKate Stone                              "into future targets.\n");
857b9c1b51eSKate Stone       else {
85805097246SAdrian Prantl         // Don't print out this warning for exception breakpoints.  They can
85905097246SAdrian Prantl         // get set before the target is set, but we won't know how to actually
86005097246SAdrian Prantl         // set the breakpoint till we run.
861b842f2ecSJim Ingham         if (bp_sp->GetNumLocations() == 0 && break_type != eSetTypeException) {
862b9c1b51eSKate Stone           output_stream.Printf("WARNING:  Unable to resolve breakpoint to any "
863b9c1b51eSKate Stone                                "actual locations.\n");
8644aeb1989SJim Ingham         }
8654aeb1989SJim Ingham       }
86630fdc8d8SChris Lattner       result.SetStatus(eReturnStatusSuccessFinishResult);
867b842f2ecSJim Ingham     } else if (!bp_sp) {
86830fdc8d8SChris Lattner       result.AppendError("Breakpoint creation failed: No breakpoint created.");
86930fdc8d8SChris Lattner       result.SetStatus(eReturnStatusFailed);
87030fdc8d8SChris Lattner     }
87130fdc8d8SChris Lattner 
87230fdc8d8SChris Lattner     return result.Succeeded();
87330fdc8d8SChris Lattner   }
87430fdc8d8SChris Lattner 
8755a988416SJim Ingham private:
876b9c1b51eSKate Stone   bool GetDefaultFile(Target *target, FileSpec &file,
877b9c1b51eSKate Stone                       CommandReturnObject &result) {
8785a988416SJim Ingham     uint32_t default_line;
87905097246SAdrian Prantl     // First use the Source Manager's default file. Then use the current stack
88005097246SAdrian Prantl     // frame's file.
881b9c1b51eSKate Stone     if (!target->GetSourceManager().GetDefaultFileAndLine(file, default_line)) {
882b57e4a1bSJason Molenda       StackFrame *cur_frame = m_exe_ctx.GetFramePtr();
883b9c1b51eSKate Stone       if (cur_frame == nullptr) {
884b9c1b51eSKate Stone         result.AppendError(
885b9c1b51eSKate Stone             "No selected frame to use to find the default file.");
8865a988416SJim Ingham         result.SetStatus(eReturnStatusFailed);
8875a988416SJim Ingham         return false;
888b9c1b51eSKate Stone       } else if (!cur_frame->HasDebugInformation()) {
889b9c1b51eSKate Stone         result.AppendError("Cannot use the selected frame to find the default "
890b9c1b51eSKate Stone                            "file, it has no debug info.");
8915a988416SJim Ingham         result.SetStatus(eReturnStatusFailed);
8925a988416SJim Ingham         return false;
893b9c1b51eSKate Stone       } else {
894b9c1b51eSKate Stone         const SymbolContext &sc =
895b9c1b51eSKate Stone             cur_frame->GetSymbolContext(eSymbolContextLineEntry);
896b9c1b51eSKate Stone         if (sc.line_entry.file) {
8975a988416SJim Ingham           file = sc.line_entry.file;
898b9c1b51eSKate Stone         } else {
899b9c1b51eSKate Stone           result.AppendError("Can't find the file for the selected frame to "
900b9c1b51eSKate Stone                              "use as the default file.");
9015a988416SJim Ingham           result.SetStatus(eReturnStatusFailed);
9025a988416SJim Ingham           return false;
9035a988416SJim Ingham         }
9045a988416SJim Ingham       }
9055a988416SJim Ingham     }
9065a988416SJim Ingham     return true;
9075a988416SJim Ingham   }
9085a988416SJim Ingham 
909b842f2ecSJim Ingham   BreakpointOptionGroup m_bp_opts;
910b842f2ecSJim Ingham   BreakpointDummyOptionGroup m_dummy_options;
9115a988416SJim Ingham   CommandOptions m_options;
912b842f2ecSJim Ingham   OptionGroupOptions m_all_options;
9135a988416SJim Ingham };
9149e85e5a8SEugene Zelenko 
9155a988416SJim Ingham //-------------------------------------------------------------------------
9165a988416SJim Ingham // CommandObjectBreakpointModify
9175a988416SJim Ingham //-------------------------------------------------------------------------
9185a988416SJim Ingham #pragma mark Modify
9195a988416SJim Ingham 
920b9c1b51eSKate Stone class CommandObjectBreakpointModify : public CommandObjectParsed {
9215a988416SJim Ingham public:
922b9c1b51eSKate Stone   CommandObjectBreakpointModify(CommandInterpreter &interpreter)
923b9c1b51eSKate Stone       : CommandObjectParsed(interpreter, "breakpoint modify",
924b9c1b51eSKate Stone                             "Modify the options on a breakpoint or set of "
925b9c1b51eSKate Stone                             "breakpoints in the executable.  "
926b9c1b51eSKate Stone                             "If no breakpoint is specified, acts on the last "
927b9c1b51eSKate Stone                             "created breakpoint.  "
928b9c1b51eSKate Stone                             "With the exception of -e, -d and -i, passing an "
929b9c1b51eSKate Stone                             "empty argument clears the modification.",
9309e85e5a8SEugene Zelenko                             nullptr),
931b9c1b51eSKate Stone         m_options() {
9325a988416SJim Ingham     CommandArgumentEntry arg;
933b9c1b51eSKate Stone     CommandObject::AddIDsArgumentData(arg, eArgTypeBreakpointID,
934b9c1b51eSKate Stone                                       eArgTypeBreakpointIDRange);
935b9c1b51eSKate Stone     // Add the entry for the first argument for this command to the object's
936b9c1b51eSKate Stone     // arguments vector.
9375a988416SJim Ingham     m_arguments.push_back(arg);
938b842f2ecSJim Ingham 
939b842f2ecSJim Ingham     m_options.Append(&m_bp_opts,
940b842f2ecSJim Ingham                      LLDB_OPT_SET_1 | LLDB_OPT_SET_2 | LLDB_OPT_SET_3,
941b842f2ecSJim Ingham                      LLDB_OPT_SET_ALL);
942b842f2ecSJim Ingham     m_options.Append(&m_dummy_opts, LLDB_OPT_SET_1, LLDB_OPT_SET_ALL);
943b842f2ecSJim Ingham     m_options.Finalize();
9445a988416SJim Ingham   }
9455a988416SJim Ingham 
9469e85e5a8SEugene Zelenko   ~CommandObjectBreakpointModify() override = default;
9475a988416SJim Ingham 
948b9c1b51eSKate Stone   Options *GetOptions() override { return &m_options; }
9495a988416SJim Ingham 
9505a988416SJim Ingham protected:
951b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
952b842f2ecSJim Ingham     Target *target = GetSelectedOrDummyTarget(m_dummy_opts.m_use_dummy);
953b9c1b51eSKate Stone     if (target == nullptr) {
9545a988416SJim Ingham       result.AppendError("Invalid target.  No existing target or breakpoints.");
9555a988416SJim Ingham       result.SetStatus(eReturnStatusFailed);
9565a988416SJim Ingham       return false;
9575a988416SJim Ingham     }
9585a988416SJim Ingham 
959bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
960bb19a13cSSaleem Abdulrasool     target->GetBreakpointList().GetListMutex(lock);
9615a988416SJim Ingham 
9625a988416SJim Ingham     BreakpointIDList valid_bp_ids;
9635a988416SJim Ingham 
964b9c1b51eSKate Stone     CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs(
965b842f2ecSJim Ingham         command, target, result, &valid_bp_ids,
966b842f2ecSJim Ingham         BreakpointName::Permissions::PermissionKinds::disablePerm);
9675a988416SJim Ingham 
968b9c1b51eSKate Stone     if (result.Succeeded()) {
9695a988416SJim Ingham       const size_t count = valid_bp_ids.GetSize();
970b9c1b51eSKate Stone       for (size_t i = 0; i < count; ++i) {
9715a988416SJim Ingham         BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i);
9725a988416SJim Ingham 
973b9c1b51eSKate Stone         if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) {
974b9c1b51eSKate Stone           Breakpoint *bp =
975b9c1b51eSKate Stone               target->GetBreakpointByID(cur_bp_id.GetBreakpointID()).get();
976b9c1b51eSKate Stone           if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID) {
977b9c1b51eSKate Stone             BreakpointLocation *location =
978b9c1b51eSKate Stone                 bp->FindLocationByID(cur_bp_id.GetLocationID()).get();
979b842f2ecSJim Ingham             if (location)
980b842f2ecSJim Ingham               location->GetLocationOptions()
981b842f2ecSJim Ingham                   ->CopyOverSetOptions(m_bp_opts.GetBreakpointOptions());
982b9c1b51eSKate Stone           } else {
983b842f2ecSJim Ingham             bp->GetOptions()
984b842f2ecSJim Ingham                 ->CopyOverSetOptions(m_bp_opts.GetBreakpointOptions());
9855a988416SJim Ingham           }
9865a988416SJim Ingham         }
9875a988416SJim Ingham       }
9885a988416SJim Ingham     }
9895a988416SJim Ingham 
9905a988416SJim Ingham     return result.Succeeded();
9915a988416SJim Ingham   }
9925a988416SJim Ingham 
9935a988416SJim Ingham private:
994b842f2ecSJim Ingham   BreakpointOptionGroup m_bp_opts;
995b842f2ecSJim Ingham   BreakpointDummyOptionGroup m_dummy_opts;
996b842f2ecSJim Ingham   OptionGroupOptions m_options;
9975a988416SJim Ingham };
9985a988416SJim Ingham 
9995a988416SJim Ingham //-------------------------------------------------------------------------
10005a988416SJim Ingham // CommandObjectBreakpointEnable
10015a988416SJim Ingham //-------------------------------------------------------------------------
10025a988416SJim Ingham #pragma mark Enable
10035a988416SJim Ingham 
1004b9c1b51eSKate Stone class CommandObjectBreakpointEnable : public CommandObjectParsed {
10055a988416SJim Ingham public:
1006b9c1b51eSKate Stone   CommandObjectBreakpointEnable(CommandInterpreter &interpreter)
1007b9c1b51eSKate Stone       : CommandObjectParsed(interpreter, "enable",
1008b9c1b51eSKate Stone                             "Enable the specified disabled breakpoint(s). If "
1009b9c1b51eSKate Stone                             "no breakpoints are specified, enable all of them.",
1010b9c1b51eSKate Stone                             nullptr) {
10115a988416SJim Ingham     CommandArgumentEntry arg;
1012b9c1b51eSKate Stone     CommandObject::AddIDsArgumentData(arg, eArgTypeBreakpointID,
1013b9c1b51eSKate Stone                                       eArgTypeBreakpointIDRange);
1014b9c1b51eSKate Stone     // Add the entry for the first argument for this command to the object's
1015b9c1b51eSKate Stone     // arguments vector.
10165a988416SJim Ingham     m_arguments.push_back(arg);
10175a988416SJim Ingham   }
10185a988416SJim Ingham 
10199e85e5a8SEugene Zelenko   ~CommandObjectBreakpointEnable() override = default;
10205a988416SJim Ingham 
10215a988416SJim Ingham protected:
1022b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
1023893c932aSJim Ingham     Target *target = GetSelectedOrDummyTarget();
1024b9c1b51eSKate Stone     if (target == nullptr) {
10255a988416SJim Ingham       result.AppendError("Invalid target.  No existing target or breakpoints.");
10265a988416SJim Ingham       result.SetStatus(eReturnStatusFailed);
10275a988416SJim Ingham       return false;
10285a988416SJim Ingham     }
10295a988416SJim Ingham 
1030bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
1031bb19a13cSSaleem Abdulrasool     target->GetBreakpointList().GetListMutex(lock);
10325a988416SJim Ingham 
10335a988416SJim Ingham     const BreakpointList &breakpoints = target->GetBreakpointList();
10345a988416SJim Ingham 
10355a988416SJim Ingham     size_t num_breakpoints = breakpoints.GetSize();
10365a988416SJim Ingham 
1037b9c1b51eSKate Stone     if (num_breakpoints == 0) {
10385a988416SJim Ingham       result.AppendError("No breakpoints exist to be enabled.");
10395a988416SJim Ingham       result.SetStatus(eReturnStatusFailed);
10405a988416SJim Ingham       return false;
10415a988416SJim Ingham     }
10425a988416SJim Ingham 
104311eb9c64SZachary Turner     if (command.empty()) {
10445a988416SJim Ingham       // No breakpoint selected; enable all currently set breakpoints.
1045b842f2ecSJim Ingham       target->EnableAllowedBreakpoints();
1046b9c1b51eSKate Stone       result.AppendMessageWithFormat("All breakpoints enabled. (%" PRIu64
1047b9c1b51eSKate Stone                                      " breakpoints)\n",
1048b9c1b51eSKate Stone                                      (uint64_t)num_breakpoints);
10495a988416SJim Ingham       result.SetStatus(eReturnStatusSuccessFinishNoResult);
1050b9c1b51eSKate Stone     } else {
10515a988416SJim Ingham       // Particular breakpoint selected; enable that breakpoint.
10525a988416SJim Ingham       BreakpointIDList valid_bp_ids;
1053b9c1b51eSKate Stone       CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs(
1054b842f2ecSJim Ingham           command, target, result, &valid_bp_ids,
1055b842f2ecSJim Ingham           BreakpointName::Permissions::PermissionKinds::disablePerm);
10565a988416SJim Ingham 
1057b9c1b51eSKate Stone       if (result.Succeeded()) {
10585a988416SJim Ingham         int enable_count = 0;
10595a988416SJim Ingham         int loc_count = 0;
10605a988416SJim Ingham         const size_t count = valid_bp_ids.GetSize();
1061b9c1b51eSKate Stone         for (size_t i = 0; i < count; ++i) {
10625a988416SJim Ingham           BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i);
10635a988416SJim Ingham 
1064b9c1b51eSKate Stone           if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) {
1065b9c1b51eSKate Stone             Breakpoint *breakpoint =
1066b9c1b51eSKate Stone                 target->GetBreakpointByID(cur_bp_id.GetBreakpointID()).get();
1067b9c1b51eSKate Stone             if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID) {
1068b9c1b51eSKate Stone               BreakpointLocation *location =
1069b9c1b51eSKate Stone                   breakpoint->FindLocationByID(cur_bp_id.GetLocationID()).get();
1070b9c1b51eSKate Stone               if (location) {
10715a988416SJim Ingham                 location->SetEnabled(true);
10725a988416SJim Ingham                 ++loc_count;
10735a988416SJim Ingham               }
1074b9c1b51eSKate Stone             } else {
10755a988416SJim Ingham               breakpoint->SetEnabled(true);
10765a988416SJim Ingham               ++enable_count;
10775a988416SJim Ingham             }
10785a988416SJim Ingham           }
10795a988416SJim Ingham         }
1080b9c1b51eSKate Stone         result.AppendMessageWithFormat("%d breakpoints enabled.\n",
1081b9c1b51eSKate Stone                                        enable_count + loc_count);
10825a988416SJim Ingham         result.SetStatus(eReturnStatusSuccessFinishNoResult);
10835a988416SJim Ingham       }
10845a988416SJim Ingham     }
10855a988416SJim Ingham 
10865a988416SJim Ingham     return result.Succeeded();
10875a988416SJim Ingham   }
10885a988416SJim Ingham };
10895a988416SJim Ingham 
10905a988416SJim Ingham //-------------------------------------------------------------------------
10915a988416SJim Ingham // CommandObjectBreakpointDisable
10925a988416SJim Ingham //-------------------------------------------------------------------------
10935a988416SJim Ingham #pragma mark Disable
10945a988416SJim Ingham 
1095b9c1b51eSKate Stone class CommandObjectBreakpointDisable : public CommandObjectParsed {
10965a988416SJim Ingham public:
10977428a18cSKate Stone   CommandObjectBreakpointDisable(CommandInterpreter &interpreter)
1098b9c1b51eSKate Stone       : CommandObjectParsed(
1099b9c1b51eSKate Stone             interpreter, "breakpoint disable",
1100b9c1b51eSKate Stone             "Disable the specified breakpoint(s) without deleting "
11017428a18cSKate Stone             "them.  If none are specified, disable all "
11027428a18cSKate Stone             "breakpoints.",
1103b9c1b51eSKate Stone             nullptr) {
1104b9c1b51eSKate Stone     SetHelpLong(
1105b9c1b51eSKate Stone         "Disable the specified breakpoint(s) without deleting them.  \
11067428a18cSKate Stone If none are specified, disable all breakpoints."
11077428a18cSKate Stone         R"(
1108ea671fbdSKate Stone 
11097428a18cSKate Stone )"
11107428a18cSKate Stone         "Note: disabling a breakpoint will cause none of its locations to be hit \
11117428a18cSKate Stone regardless of whether individual locations are enabled or disabled.  After the sequence:"
11127428a18cSKate Stone         R"(
1113ea671fbdSKate Stone 
1114ea671fbdSKate Stone     (lldb) break disable 1
1115ea671fbdSKate Stone     (lldb) break enable 1.1
1116ea671fbdSKate Stone 
1117ea671fbdSKate Stone execution will NOT stop at location 1.1.  To achieve that, type:
1118ea671fbdSKate Stone 
1119ea671fbdSKate Stone     (lldb) break disable 1.*
1120ea671fbdSKate Stone     (lldb) break enable 1.1
1121ea671fbdSKate Stone 
11227428a18cSKate Stone )"
11237428a18cSKate Stone         "The first command disables all locations for breakpoint 1, \
11247428a18cSKate Stone the second re-enables the first location.");
1125b0fac509SJim Ingham 
11265a988416SJim Ingham     CommandArgumentEntry arg;
1127b9c1b51eSKate Stone     CommandObject::AddIDsArgumentData(arg, eArgTypeBreakpointID,
1128b9c1b51eSKate Stone                                       eArgTypeBreakpointIDRange);
1129b9c1b51eSKate Stone     // Add the entry for the first argument for this command to the object's
1130b9c1b51eSKate Stone     // arguments vector.
11315a988416SJim Ingham     m_arguments.push_back(arg);
11325a988416SJim Ingham   }
11335a988416SJim Ingham 
11349e85e5a8SEugene Zelenko   ~CommandObjectBreakpointDisable() override = default;
11355a988416SJim Ingham 
11365a988416SJim Ingham protected:
1137b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
1138893c932aSJim Ingham     Target *target = GetSelectedOrDummyTarget();
1139b9c1b51eSKate Stone     if (target == nullptr) {
11405a988416SJim Ingham       result.AppendError("Invalid target.  No existing target or breakpoints.");
11415a988416SJim Ingham       result.SetStatus(eReturnStatusFailed);
11425a988416SJim Ingham       return false;
11435a988416SJim Ingham     }
11445a988416SJim Ingham 
1145bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
1146bb19a13cSSaleem Abdulrasool     target->GetBreakpointList().GetListMutex(lock);
11475a988416SJim Ingham 
11485a988416SJim Ingham     const BreakpointList &breakpoints = target->GetBreakpointList();
11495a988416SJim Ingham     size_t num_breakpoints = breakpoints.GetSize();
11505a988416SJim Ingham 
1151b9c1b51eSKate Stone     if (num_breakpoints == 0) {
11525a988416SJim Ingham       result.AppendError("No breakpoints exist to be disabled.");
11535a988416SJim Ingham       result.SetStatus(eReturnStatusFailed);
11545a988416SJim Ingham       return false;
11555a988416SJim Ingham     }
11565a988416SJim Ingham 
115711eb9c64SZachary Turner     if (command.empty()) {
11585a988416SJim Ingham       // No breakpoint selected; disable all currently set breakpoints.
1159b842f2ecSJim Ingham       target->DisableAllowedBreakpoints();
1160b9c1b51eSKate Stone       result.AppendMessageWithFormat("All breakpoints disabled. (%" PRIu64
1161b9c1b51eSKate Stone                                      " breakpoints)\n",
1162b9c1b51eSKate Stone                                      (uint64_t)num_breakpoints);
11635a988416SJim Ingham       result.SetStatus(eReturnStatusSuccessFinishNoResult);
1164b9c1b51eSKate Stone     } else {
11655a988416SJim Ingham       // Particular breakpoint selected; disable that breakpoint.
11665a988416SJim Ingham       BreakpointIDList valid_bp_ids;
11675a988416SJim Ingham 
1168b9c1b51eSKate Stone       CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs(
1169b842f2ecSJim Ingham           command, target, result, &valid_bp_ids,
1170b842f2ecSJim Ingham           BreakpointName::Permissions::PermissionKinds::disablePerm);
11715a988416SJim Ingham 
1172b9c1b51eSKate Stone       if (result.Succeeded()) {
11735a988416SJim Ingham         int disable_count = 0;
11745a988416SJim Ingham         int loc_count = 0;
11755a988416SJim Ingham         const size_t count = valid_bp_ids.GetSize();
1176b9c1b51eSKate Stone         for (size_t i = 0; i < count; ++i) {
11775a988416SJim Ingham           BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i);
11785a988416SJim Ingham 
1179b9c1b51eSKate Stone           if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) {
1180b9c1b51eSKate Stone             Breakpoint *breakpoint =
1181b9c1b51eSKate Stone                 target->GetBreakpointByID(cur_bp_id.GetBreakpointID()).get();
1182b9c1b51eSKate Stone             if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID) {
1183b9c1b51eSKate Stone               BreakpointLocation *location =
1184b9c1b51eSKate Stone                   breakpoint->FindLocationByID(cur_bp_id.GetLocationID()).get();
1185b9c1b51eSKate Stone               if (location) {
11865a988416SJim Ingham                 location->SetEnabled(false);
11875a988416SJim Ingham                 ++loc_count;
11885a988416SJim Ingham               }
1189b9c1b51eSKate Stone             } else {
11905a988416SJim Ingham               breakpoint->SetEnabled(false);
11915a988416SJim Ingham               ++disable_count;
11925a988416SJim Ingham             }
11935a988416SJim Ingham           }
11945a988416SJim Ingham         }
1195b9c1b51eSKate Stone         result.AppendMessageWithFormat("%d breakpoints disabled.\n",
1196b9c1b51eSKate Stone                                        disable_count + loc_count);
11975a988416SJim Ingham         result.SetStatus(eReturnStatusSuccessFinishNoResult);
11985a988416SJim Ingham       }
11995a988416SJim Ingham     }
12005a988416SJim Ingham 
12015a988416SJim Ingham     return result.Succeeded();
12025a988416SJim Ingham   }
12035a988416SJim Ingham };
12045a988416SJim Ingham 
12055a988416SJim Ingham //-------------------------------------------------------------------------
12065a988416SJim Ingham // CommandObjectBreakpointList
12075a988416SJim Ingham //-------------------------------------------------------------------------
12081f0f5b5bSZachary Turner 
12091f0f5b5bSZachary Turner #pragma mark List::CommandOptions
12101f0f5b5bSZachary Turner static OptionDefinition g_breakpoint_list_options[] = {
12111f0f5b5bSZachary Turner     // clang-format off
12121f0f5b5bSZachary Turner   { LLDB_OPT_SET_ALL, false, "internal",          'i', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Show debugger internal breakpoints" },
12131f0f5b5bSZachary Turner   { LLDB_OPT_SET_1,   false, "brief",             'b', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Give a brief description of the breakpoint (no location info)." },
12141f0f5b5bSZachary Turner   // FIXME: We need to add an "internal" command, and then add this sort of thing to it.
12151f0f5b5bSZachary Turner   // But I need to see it for now, and don't want to wait.
12161f0f5b5bSZachary Turner   { LLDB_OPT_SET_2,   false, "full",              'f', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Give a full description of the breakpoint and its locations." },
12171f0f5b5bSZachary Turner   { LLDB_OPT_SET_3,   false, "verbose",           'v', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Explain everything we know about the breakpoint (for debugging debugger bugs)." },
12181f0f5b5bSZachary Turner   { LLDB_OPT_SET_ALL, false, "dummy-breakpoints", 'D', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "List Dummy breakpoints - i.e. breakpoints set before a file is provided, which prime new targets." },
12191f0f5b5bSZachary Turner     // clang-format on
12201f0f5b5bSZachary Turner };
12211f0f5b5bSZachary Turner 
12225a988416SJim Ingham #pragma mark List
12235a988416SJim Ingham 
1224b9c1b51eSKate Stone class CommandObjectBreakpointList : public CommandObjectParsed {
12255a988416SJim Ingham public:
1226b9c1b51eSKate Stone   CommandObjectBreakpointList(CommandInterpreter &interpreter)
1227b9c1b51eSKate Stone       : CommandObjectParsed(
1228b9c1b51eSKate Stone             interpreter, "breakpoint list",
12295a988416SJim Ingham             "List some or all breakpoints at configurable levels of detail.",
12309e85e5a8SEugene Zelenko             nullptr),
1231b9c1b51eSKate Stone         m_options() {
12325a988416SJim Ingham     CommandArgumentEntry arg;
12335a988416SJim Ingham     CommandArgumentData bp_id_arg;
12345a988416SJim Ingham 
12355a988416SJim Ingham     // Define the first (and only) variant of this arg.
12365a988416SJim Ingham     bp_id_arg.arg_type = eArgTypeBreakpointID;
12375a988416SJim Ingham     bp_id_arg.arg_repetition = eArgRepeatOptional;
12385a988416SJim Ingham 
1239b9c1b51eSKate Stone     // There is only one variant this argument could be; put it into the
1240b9c1b51eSKate Stone     // argument entry.
12415a988416SJim Ingham     arg.push_back(bp_id_arg);
12425a988416SJim Ingham 
12435a988416SJim Ingham     // Push the data for the first argument into the m_arguments vector.
12445a988416SJim Ingham     m_arguments.push_back(arg);
12455a988416SJim Ingham   }
12465a988416SJim Ingham 
12479e85e5a8SEugene Zelenko   ~CommandObjectBreakpointList() override = default;
12485a988416SJim Ingham 
1249b9c1b51eSKate Stone   Options *GetOptions() override { return &m_options; }
12505a988416SJim Ingham 
1251b9c1b51eSKate Stone   class CommandOptions : public Options {
12525a988416SJim Ingham   public:
1253b9c1b51eSKate Stone     CommandOptions()
1254b9c1b51eSKate Stone         : Options(), m_level(lldb::eDescriptionLevelBrief), m_use_dummy(false) {
12555a988416SJim Ingham     }
12565a988416SJim Ingham 
12579e85e5a8SEugene Zelenko     ~CommandOptions() override = default;
12585a988416SJim Ingham 
125997206d57SZachary Turner     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1260b9c1b51eSKate Stone                           ExecutionContext *execution_context) override {
126197206d57SZachary Turner       Status error;
12623bcdfc0eSGreg Clayton       const int short_option = m_getopt_table[option_idx].val;
12635a988416SJim Ingham 
1264b9c1b51eSKate Stone       switch (short_option) {
12655a988416SJim Ingham       case 'b':
12665a988416SJim Ingham         m_level = lldb::eDescriptionLevelBrief;
12675a988416SJim Ingham         break;
126833df7cd3SJim Ingham       case 'D':
126933df7cd3SJim Ingham         m_use_dummy = true;
127033df7cd3SJim Ingham         break;
12715a988416SJim Ingham       case 'f':
12725a988416SJim Ingham         m_level = lldb::eDescriptionLevelFull;
12735a988416SJim Ingham         break;
12745a988416SJim Ingham       case 'v':
12755a988416SJim Ingham         m_level = lldb::eDescriptionLevelVerbose;
12765a988416SJim Ingham         break;
12775a988416SJim Ingham       case 'i':
12785a988416SJim Ingham         m_internal = true;
12795a988416SJim Ingham         break;
12805a988416SJim Ingham       default:
1281b9c1b51eSKate Stone         error.SetErrorStringWithFormat("unrecognized option '%c'",
1282b9c1b51eSKate Stone                                        short_option);
12835a988416SJim Ingham         break;
12845a988416SJim Ingham       }
12855a988416SJim Ingham 
12865a988416SJim Ingham       return error;
12875a988416SJim Ingham     }
12885a988416SJim Ingham 
1289b9c1b51eSKate Stone     void OptionParsingStarting(ExecutionContext *execution_context) override {
12905a988416SJim Ingham       m_level = lldb::eDescriptionLevelFull;
12915a988416SJim Ingham       m_internal = false;
129233df7cd3SJim Ingham       m_use_dummy = false;
12935a988416SJim Ingham     }
12945a988416SJim Ingham 
12951f0f5b5bSZachary Turner     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
129670602439SZachary Turner       return llvm::makeArrayRef(g_breakpoint_list_options);
12971f0f5b5bSZachary Turner     }
12985a988416SJim Ingham 
12995a988416SJim Ingham     // Instance variables to hold the values for command options.
13005a988416SJim Ingham 
13015a988416SJim Ingham     lldb::DescriptionLevel m_level;
13025a988416SJim Ingham 
13035a988416SJim Ingham     bool m_internal;
130433df7cd3SJim Ingham     bool m_use_dummy;
13055a988416SJim Ingham   };
13065a988416SJim Ingham 
13075a988416SJim Ingham protected:
1308b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
130933df7cd3SJim Ingham     Target *target = GetSelectedOrDummyTarget(m_options.m_use_dummy);
131033df7cd3SJim Ingham 
1311b9c1b51eSKate Stone     if (target == nullptr) {
13125a988416SJim Ingham       result.AppendError("Invalid target. No current target or breakpoints.");
13135a988416SJim Ingham       result.SetStatus(eReturnStatusSuccessFinishNoResult);
13145a988416SJim Ingham       return true;
13155a988416SJim Ingham     }
13165a988416SJim Ingham 
1317b9c1b51eSKate Stone     const BreakpointList &breakpoints =
1318b9c1b51eSKate Stone         target->GetBreakpointList(m_options.m_internal);
1319bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
1320bb19a13cSSaleem Abdulrasool     target->GetBreakpointList(m_options.m_internal).GetListMutex(lock);
13215a988416SJim Ingham 
13225a988416SJim Ingham     size_t num_breakpoints = breakpoints.GetSize();
13235a988416SJim Ingham 
1324b9c1b51eSKate Stone     if (num_breakpoints == 0) {
13255a988416SJim Ingham       result.AppendMessage("No breakpoints currently set.");
13265a988416SJim Ingham       result.SetStatus(eReturnStatusSuccessFinishNoResult);
13275a988416SJim Ingham       return true;
13285a988416SJim Ingham     }
13295a988416SJim Ingham 
13305a988416SJim Ingham     Stream &output_stream = result.GetOutputStream();
13315a988416SJim Ingham 
133211eb9c64SZachary Turner     if (command.empty()) {
13335a988416SJim Ingham       // No breakpoint selected; show info about all currently set breakpoints.
13345a988416SJim Ingham       result.AppendMessage("Current breakpoints:");
1335b9c1b51eSKate Stone       for (size_t i = 0; i < num_breakpoints; ++i) {
13365a988416SJim Ingham         Breakpoint *breakpoint = breakpoints.GetBreakpointAtIndex(i).get();
1337b842f2ecSJim Ingham         if (breakpoint->AllowList())
1338b842f2ecSJim Ingham           AddBreakpointDescription(&output_stream, breakpoint,
1339b842f2ecSJim Ingham                                    m_options.m_level);
13405a988416SJim Ingham       }
13415a988416SJim Ingham       result.SetStatus(eReturnStatusSuccessFinishNoResult);
1342b9c1b51eSKate Stone     } else {
13435a988416SJim Ingham       // Particular breakpoints selected; show info about that breakpoint.
13445a988416SJim Ingham       BreakpointIDList valid_bp_ids;
1345b9c1b51eSKate Stone       CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs(
1346b842f2ecSJim Ingham           command, target, result, &valid_bp_ids,
1347b842f2ecSJim Ingham           BreakpointName::Permissions::PermissionKinds::listPerm);
13485a988416SJim Ingham 
1349b9c1b51eSKate Stone       if (result.Succeeded()) {
1350b9c1b51eSKate Stone         for (size_t i = 0; i < valid_bp_ids.GetSize(); ++i) {
13515a988416SJim Ingham           BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i);
1352b9c1b51eSKate Stone           Breakpoint *breakpoint =
1353b9c1b51eSKate Stone               target->GetBreakpointByID(cur_bp_id.GetBreakpointID()).get();
1354b9c1b51eSKate Stone           AddBreakpointDescription(&output_stream, breakpoint,
1355b9c1b51eSKate Stone                                    m_options.m_level);
13565a988416SJim Ingham         }
13575a988416SJim Ingham         result.SetStatus(eReturnStatusSuccessFinishNoResult);
1358b9c1b51eSKate Stone       } else {
13597428a18cSKate Stone         result.AppendError("Invalid breakpoint ID.");
13605a988416SJim Ingham         result.SetStatus(eReturnStatusFailed);
13615a988416SJim Ingham       }
13625a988416SJim Ingham     }
13635a988416SJim Ingham 
13645a988416SJim Ingham     return result.Succeeded();
13655a988416SJim Ingham   }
13665a988416SJim Ingham 
13675a988416SJim Ingham private:
13685a988416SJim Ingham   CommandOptions m_options;
13695a988416SJim Ingham };
13705a988416SJim Ingham 
13715a988416SJim Ingham //-------------------------------------------------------------------------
13725a988416SJim Ingham // CommandObjectBreakpointClear
13735a988416SJim Ingham //-------------------------------------------------------------------------
13741f0f5b5bSZachary Turner #pragma mark Clear::CommandOptions
13751f0f5b5bSZachary Turner 
13761f0f5b5bSZachary Turner static OptionDefinition g_breakpoint_clear_options[] = {
13771f0f5b5bSZachary Turner     // clang-format off
13781f0f5b5bSZachary Turner   { LLDB_OPT_SET_1, false, "file", 'f', OptionParser::eRequiredArgument, nullptr, nullptr, CommandCompletions::eSourceFileCompletion, eArgTypeFilename, "Specify the breakpoint by source location in this particular file." },
13791f0f5b5bSZachary Turner   { LLDB_OPT_SET_1, true,  "line", 'l', OptionParser::eRequiredArgument, nullptr, nullptr, 0,                                         eArgTypeLineNum,  "Specify the breakpoint by source location at this particular line." }
13801f0f5b5bSZachary Turner     // clang-format on
13811f0f5b5bSZachary Turner };
13821f0f5b5bSZachary Turner 
13835a988416SJim Ingham #pragma mark Clear
13845a988416SJim Ingham 
1385b9c1b51eSKate Stone class CommandObjectBreakpointClear : public CommandObjectParsed {
13865a988416SJim Ingham public:
1387b9c1b51eSKate Stone   typedef enum BreakpointClearType {
13885a988416SJim Ingham     eClearTypeInvalid,
13895a988416SJim Ingham     eClearTypeFileAndLine
13905a988416SJim Ingham   } BreakpointClearType;
13915a988416SJim Ingham 
13927428a18cSKate Stone   CommandObjectBreakpointClear(CommandInterpreter &interpreter)
13937428a18cSKate Stone       : CommandObjectParsed(interpreter, "breakpoint clear",
1394b9c1b51eSKate Stone                             "Delete or disable breakpoints matching the "
1395b9c1b51eSKate Stone                             "specified source file and line.",
13965a988416SJim Ingham                             "breakpoint clear <cmd-options>"),
1397b9c1b51eSKate Stone         m_options() {}
13985a988416SJim Ingham 
13999e85e5a8SEugene Zelenko   ~CommandObjectBreakpointClear() override = default;
14005a988416SJim Ingham 
1401b9c1b51eSKate Stone   Options *GetOptions() override { return &m_options; }
14025a988416SJim Ingham 
1403b9c1b51eSKate Stone   class CommandOptions : public Options {
14045a988416SJim Ingham   public:
1405b9c1b51eSKate Stone     CommandOptions() : Options(), m_filename(), m_line_num(0) {}
14065a988416SJim Ingham 
14079e85e5a8SEugene Zelenko     ~CommandOptions() override = default;
14085a988416SJim Ingham 
140997206d57SZachary Turner     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1410b9c1b51eSKate Stone                           ExecutionContext *execution_context) override {
141197206d57SZachary Turner       Status error;
14123bcdfc0eSGreg Clayton       const int short_option = m_getopt_table[option_idx].val;
14135a988416SJim Ingham 
1414b9c1b51eSKate Stone       switch (short_option) {
14155a988416SJim Ingham       case 'f':
14165a988416SJim Ingham         m_filename.assign(option_arg);
14175a988416SJim Ingham         break;
14185a988416SJim Ingham 
14195a988416SJim Ingham       case 'l':
1420fe11483bSZachary Turner         option_arg.getAsInteger(0, m_line_num);
14215a988416SJim Ingham         break;
14225a988416SJim Ingham 
14235a988416SJim Ingham       default:
1424b9c1b51eSKate Stone         error.SetErrorStringWithFormat("unrecognized option '%c'",
1425b9c1b51eSKate Stone                                        short_option);
14265a988416SJim Ingham         break;
14275a988416SJim Ingham       }
14285a988416SJim Ingham 
14295a988416SJim Ingham       return error;
14305a988416SJim Ingham     }
14315a988416SJim Ingham 
1432b9c1b51eSKate Stone     void OptionParsingStarting(ExecutionContext *execution_context) override {
14335a988416SJim Ingham       m_filename.clear();
14345a988416SJim Ingham       m_line_num = 0;
14355a988416SJim Ingham     }
14365a988416SJim Ingham 
14371f0f5b5bSZachary Turner     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
143870602439SZachary Turner       return llvm::makeArrayRef(g_breakpoint_clear_options);
14391f0f5b5bSZachary Turner     }
14405a988416SJim Ingham 
14415a988416SJim Ingham     // Instance variables to hold the values for command options.
14425a988416SJim Ingham 
14435a988416SJim Ingham     std::string m_filename;
14445a988416SJim Ingham     uint32_t m_line_num;
14455a988416SJim Ingham   };
14465a988416SJim Ingham 
14475a988416SJim Ingham protected:
1448b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
1449893c932aSJim Ingham     Target *target = GetSelectedOrDummyTarget();
1450b9c1b51eSKate Stone     if (target == nullptr) {
14515a988416SJim Ingham       result.AppendError("Invalid target. No existing target or breakpoints.");
14525a988416SJim Ingham       result.SetStatus(eReturnStatusFailed);
14535a988416SJim Ingham       return false;
14545a988416SJim Ingham     }
14555a988416SJim Ingham 
145605097246SAdrian Prantl     // The following are the various types of breakpoints that could be
145705097246SAdrian Prantl     // cleared:
14585a988416SJim Ingham     //   1). -f -l (clearing breakpoint by source location)
14595a988416SJim Ingham 
14605a988416SJim Ingham     BreakpointClearType break_type = eClearTypeInvalid;
14615a988416SJim Ingham 
14625a988416SJim Ingham     if (m_options.m_line_num != 0)
14635a988416SJim Ingham       break_type = eClearTypeFileAndLine;
14645a988416SJim Ingham 
1465bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
1466bb19a13cSSaleem Abdulrasool     target->GetBreakpointList().GetListMutex(lock);
14675a988416SJim Ingham 
14685a988416SJim Ingham     BreakpointList &breakpoints = target->GetBreakpointList();
14695a988416SJim Ingham     size_t num_breakpoints = breakpoints.GetSize();
14705a988416SJim Ingham 
14715a988416SJim Ingham     // Early return if there's no breakpoint at all.
1472b9c1b51eSKate Stone     if (num_breakpoints == 0) {
14735a988416SJim Ingham       result.AppendError("Breakpoint clear: No breakpoint cleared.");
14745a988416SJim Ingham       result.SetStatus(eReturnStatusFailed);
14755a988416SJim Ingham       return result.Succeeded();
14765a988416SJim Ingham     }
14775a988416SJim Ingham 
14785a988416SJim Ingham     // Find matching breakpoints and delete them.
14795a988416SJim Ingham 
14805a988416SJim Ingham     // First create a copy of all the IDs.
14815a988416SJim Ingham     std::vector<break_id_t> BreakIDs;
14825a988416SJim Ingham     for (size_t i = 0; i < num_breakpoints; ++i)
14839e85e5a8SEugene Zelenko       BreakIDs.push_back(breakpoints.GetBreakpointAtIndex(i)->GetID());
14845a988416SJim Ingham 
14855a988416SJim Ingham     int num_cleared = 0;
14865a988416SJim Ingham     StreamString ss;
1487b9c1b51eSKate Stone     switch (break_type) {
14885a988416SJim Ingham     case eClearTypeFileAndLine: // Breakpoint by source position
14895a988416SJim Ingham     {
14905a988416SJim Ingham       const ConstString filename(m_options.m_filename.c_str());
14915a988416SJim Ingham       BreakpointLocationCollection loc_coll;
14925a988416SJim Ingham 
1493b9c1b51eSKate Stone       for (size_t i = 0; i < num_breakpoints; ++i) {
14945a988416SJim Ingham         Breakpoint *bp = breakpoints.FindBreakpointByID(BreakIDs[i]).get();
14955a988416SJim Ingham 
1496b9c1b51eSKate Stone         if (bp->GetMatchingFileLine(filename, m_options.m_line_num, loc_coll)) {
1497b9c1b51eSKate Stone           // If the collection size is 0, it's a full match and we can just
1498b9c1b51eSKate Stone           // remove the breakpoint.
1499b9c1b51eSKate Stone           if (loc_coll.GetSize() == 0) {
15005a988416SJim Ingham             bp->GetDescription(&ss, lldb::eDescriptionLevelBrief);
15015a988416SJim Ingham             ss.EOL();
15025a988416SJim Ingham             target->RemoveBreakpointByID(bp->GetID());
15035a988416SJim Ingham             ++num_cleared;
15045a988416SJim Ingham           }
15055a988416SJim Ingham         }
15065a988416SJim Ingham       }
1507b9c1b51eSKate Stone     } break;
15085a988416SJim Ingham 
15095a988416SJim Ingham     default:
15105a988416SJim Ingham       break;
15115a988416SJim Ingham     }
15125a988416SJim Ingham 
1513b9c1b51eSKate Stone     if (num_cleared > 0) {
15145a988416SJim Ingham       Stream &output_stream = result.GetOutputStream();
15155a988416SJim Ingham       output_stream.Printf("%d breakpoints cleared:\n", num_cleared);
1516c156427dSZachary Turner       output_stream << ss.GetString();
15175a988416SJim Ingham       output_stream.EOL();
15185a988416SJim Ingham       result.SetStatus(eReturnStatusSuccessFinishNoResult);
1519b9c1b51eSKate Stone     } else {
15205a988416SJim Ingham       result.AppendError("Breakpoint clear: No breakpoint cleared.");
15215a988416SJim Ingham       result.SetStatus(eReturnStatusFailed);
15225a988416SJim Ingham     }
15235a988416SJim Ingham 
15245a988416SJim Ingham     return result.Succeeded();
15255a988416SJim Ingham   }
15265a988416SJim Ingham 
15275a988416SJim Ingham private:
15285a988416SJim Ingham   CommandOptions m_options;
15295a988416SJim Ingham };
15305a988416SJim Ingham 
15315a988416SJim Ingham //-------------------------------------------------------------------------
15325a988416SJim Ingham // CommandObjectBreakpointDelete
15335a988416SJim Ingham //-------------------------------------------------------------------------
15341f0f5b5bSZachary Turner static OptionDefinition g_breakpoint_delete_options[] = {
15351f0f5b5bSZachary Turner     // clang-format off
15361f0f5b5bSZachary Turner   { LLDB_OPT_SET_1, false, "force",             'f', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Delete all breakpoints without querying for confirmation." },
15371f0f5b5bSZachary Turner   { LLDB_OPT_SET_1, false, "dummy-breakpoints", 'D', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Delete Dummy breakpoints - i.e. breakpoints set before a file is provided, which prime new targets." },
15381f0f5b5bSZachary Turner     // clang-format on
15391f0f5b5bSZachary Turner };
15401f0f5b5bSZachary Turner 
15415a988416SJim Ingham #pragma mark Delete
15425a988416SJim Ingham 
1543b9c1b51eSKate Stone class CommandObjectBreakpointDelete : public CommandObjectParsed {
15445a988416SJim Ingham public:
1545b9c1b51eSKate Stone   CommandObjectBreakpointDelete(CommandInterpreter &interpreter)
1546b9c1b51eSKate Stone       : CommandObjectParsed(interpreter, "breakpoint delete",
1547b9c1b51eSKate Stone                             "Delete the specified breakpoint(s).  If no "
1548b9c1b51eSKate Stone                             "breakpoints are specified, delete them all.",
15499e85e5a8SEugene Zelenko                             nullptr),
1550b9c1b51eSKate Stone         m_options() {
15515a988416SJim Ingham     CommandArgumentEntry arg;
1552b9c1b51eSKate Stone     CommandObject::AddIDsArgumentData(arg, eArgTypeBreakpointID,
1553b9c1b51eSKate Stone                                       eArgTypeBreakpointIDRange);
1554b9c1b51eSKate Stone     // Add the entry for the first argument for this command to the object's
1555b9c1b51eSKate Stone     // arguments vector.
15565a988416SJim Ingham     m_arguments.push_back(arg);
15575a988416SJim Ingham   }
15585a988416SJim Ingham 
15599e85e5a8SEugene Zelenko   ~CommandObjectBreakpointDelete() override = default;
15605a988416SJim Ingham 
1561b9c1b51eSKate Stone   Options *GetOptions() override { return &m_options; }
156233df7cd3SJim Ingham 
1563b9c1b51eSKate Stone   class CommandOptions : public Options {
156433df7cd3SJim Ingham   public:
1565b9c1b51eSKate Stone     CommandOptions() : Options(), m_use_dummy(false), m_force(false) {}
156633df7cd3SJim Ingham 
15679e85e5a8SEugene Zelenko     ~CommandOptions() override = default;
156833df7cd3SJim Ingham 
156997206d57SZachary Turner     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1570b9c1b51eSKate Stone                           ExecutionContext *execution_context) override {
157197206d57SZachary Turner       Status error;
157233df7cd3SJim Ingham       const int short_option = m_getopt_table[option_idx].val;
157333df7cd3SJim Ingham 
1574b9c1b51eSKate Stone       switch (short_option) {
157533df7cd3SJim Ingham       case 'f':
157633df7cd3SJim Ingham         m_force = true;
157733df7cd3SJim Ingham         break;
157833df7cd3SJim Ingham 
157933df7cd3SJim Ingham       case 'D':
158033df7cd3SJim Ingham         m_use_dummy = true;
158133df7cd3SJim Ingham         break;
158233df7cd3SJim Ingham 
158333df7cd3SJim Ingham       default:
1584b9c1b51eSKate Stone         error.SetErrorStringWithFormat("unrecognized option '%c'",
1585b9c1b51eSKate Stone                                        short_option);
158633df7cd3SJim Ingham         break;
158733df7cd3SJim Ingham       }
158833df7cd3SJim Ingham 
158933df7cd3SJim Ingham       return error;
159033df7cd3SJim Ingham     }
159133df7cd3SJim Ingham 
1592b9c1b51eSKate Stone     void OptionParsingStarting(ExecutionContext *execution_context) override {
159333df7cd3SJim Ingham       m_use_dummy = false;
159433df7cd3SJim Ingham       m_force = false;
159533df7cd3SJim Ingham     }
159633df7cd3SJim Ingham 
15971f0f5b5bSZachary Turner     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
159870602439SZachary Turner       return llvm::makeArrayRef(g_breakpoint_delete_options);
15991f0f5b5bSZachary Turner     }
160033df7cd3SJim Ingham 
160133df7cd3SJim Ingham     // Instance variables to hold the values for command options.
160233df7cd3SJim Ingham     bool m_use_dummy;
160333df7cd3SJim Ingham     bool m_force;
160433df7cd3SJim Ingham   };
160533df7cd3SJim Ingham 
16065a988416SJim Ingham protected:
1607b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
160833df7cd3SJim Ingham     Target *target = GetSelectedOrDummyTarget(m_options.m_use_dummy);
160933df7cd3SJim Ingham 
1610b9c1b51eSKate Stone     if (target == nullptr) {
16115a988416SJim Ingham       result.AppendError("Invalid target. No existing target or breakpoints.");
16125a988416SJim Ingham       result.SetStatus(eReturnStatusFailed);
16135a988416SJim Ingham       return false;
16145a988416SJim Ingham     }
16155a988416SJim Ingham 
1616bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
1617bb19a13cSSaleem Abdulrasool     target->GetBreakpointList().GetListMutex(lock);
16185a988416SJim Ingham 
16195a988416SJim Ingham     const BreakpointList &breakpoints = target->GetBreakpointList();
16205a988416SJim Ingham 
16215a988416SJim Ingham     size_t num_breakpoints = breakpoints.GetSize();
16225a988416SJim Ingham 
1623b9c1b51eSKate Stone     if (num_breakpoints == 0) {
16245a988416SJim Ingham       result.AppendError("No breakpoints exist to be deleted.");
16255a988416SJim Ingham       result.SetStatus(eReturnStatusFailed);
16265a988416SJim Ingham       return false;
16275a988416SJim Ingham     }
16285a988416SJim Ingham 
162911eb9c64SZachary Turner     if (command.empty()) {
1630b9c1b51eSKate Stone       if (!m_options.m_force &&
1631b9c1b51eSKate Stone           !m_interpreter.Confirm(
1632b9c1b51eSKate Stone               "About to delete all breakpoints, do you want to do that?",
1633b9c1b51eSKate Stone               true)) {
16345a988416SJim Ingham         result.AppendMessage("Operation cancelled...");
1635b9c1b51eSKate Stone       } else {
1636b842f2ecSJim Ingham         target->RemoveAllowedBreakpoints();
1637b9c1b51eSKate Stone         result.AppendMessageWithFormat(
1638b9c1b51eSKate Stone             "All breakpoints removed. (%" PRIu64 " breakpoint%s)\n",
1639b9c1b51eSKate Stone             (uint64_t)num_breakpoints, num_breakpoints > 1 ? "s" : "");
16405a988416SJim Ingham       }
16415a988416SJim Ingham       result.SetStatus(eReturnStatusSuccessFinishNoResult);
1642b9c1b51eSKate Stone     } else {
16435a988416SJim Ingham       // Particular breakpoint selected; disable that breakpoint.
16445a988416SJim Ingham       BreakpointIDList valid_bp_ids;
1645b9c1b51eSKate Stone       CommandObjectMultiwordBreakpoint::VerifyBreakpointOrLocationIDs(
1646b842f2ecSJim Ingham           command, target, result, &valid_bp_ids,
1647b842f2ecSJim Ingham           BreakpointName::Permissions::PermissionKinds::deletePerm);
16485a988416SJim Ingham 
1649b9c1b51eSKate Stone       if (result.Succeeded()) {
16505a988416SJim Ingham         int delete_count = 0;
16515a988416SJim Ingham         int disable_count = 0;
16525a988416SJim Ingham         const size_t count = valid_bp_ids.GetSize();
1653b9c1b51eSKate Stone         for (size_t i = 0; i < count; ++i) {
16545a988416SJim Ingham           BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex(i);
16555a988416SJim Ingham 
1656b9c1b51eSKate Stone           if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID) {
1657b9c1b51eSKate Stone             if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID) {
1658b9c1b51eSKate Stone               Breakpoint *breakpoint =
1659b9c1b51eSKate Stone                   target->GetBreakpointByID(cur_bp_id.GetBreakpointID()).get();
1660b9c1b51eSKate Stone               BreakpointLocation *location =
1661b9c1b51eSKate Stone                   breakpoint->FindLocationByID(cur_bp_id.GetLocationID()).get();
1662b9c1b51eSKate Stone               // It makes no sense to try to delete individual locations, so we
1663b9c1b51eSKate Stone               // disable them instead.
1664b9c1b51eSKate Stone               if (location) {
16655a988416SJim Ingham                 location->SetEnabled(false);
16665a988416SJim Ingham                 ++disable_count;
16675a988416SJim Ingham               }
1668b9c1b51eSKate Stone             } else {
16695a988416SJim Ingham               target->RemoveBreakpointByID(cur_bp_id.GetBreakpointID());
16705a988416SJim Ingham               ++delete_count;
16715a988416SJim Ingham             }
16725a988416SJim Ingham           }
16735a988416SJim Ingham         }
1674b9c1b51eSKate Stone         result.AppendMessageWithFormat(
1675b9c1b51eSKate Stone             "%d breakpoints deleted; %d breakpoint locations disabled.\n",
16765a988416SJim Ingham             delete_count, disable_count);
16775a988416SJim Ingham         result.SetStatus(eReturnStatusSuccessFinishNoResult);
16785a988416SJim Ingham       }
16795a988416SJim Ingham     }
16805a988416SJim Ingham     return result.Succeeded();
16815a988416SJim Ingham   }
16829e85e5a8SEugene Zelenko 
168333df7cd3SJim Ingham private:
168433df7cd3SJim Ingham   CommandOptions m_options;
168533df7cd3SJim Ingham };
168633df7cd3SJim Ingham 
168730fdc8d8SChris Lattner //-------------------------------------------------------------------------
16885e09c8c3SJim Ingham // CommandObjectBreakpointName
16895e09c8c3SJim Ingham //-------------------------------------------------------------------------
16905e09c8c3SJim Ingham 
16917428a18cSKate Stone static OptionDefinition g_breakpoint_name_options[] = {
1692ac9c3a62SKate Stone     // clang-format off
1693ac9c3a62SKate Stone   {LLDB_OPT_SET_1, false, "name",              'N', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBreakpointName, "Specifies a breakpoint name to use."},
1694ac9c3a62SKate Stone   {LLDB_OPT_SET_2, false, "breakpoint-id",     'B', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBreakpointID,   "Specify a breakpoint ID to use."},
1695b842f2ecSJim Ingham   {LLDB_OPT_SET_3, false, "dummy-breakpoints", 'D', OptionParser::eNoArgument,       nullptr, nullptr, 0, eArgTypeNone,           "Operate on Dummy breakpoints - i.e. breakpoints set before a file is provided, which prime new targets."},
1696e9632ebaSJim Ingham   {LLDB_OPT_SET_4, false, "help-string",  'H', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeNone, "A help string describing the purpose of this name."},
1697ac9c3a62SKate Stone     // clang-format on
16985e09c8c3SJim Ingham };
1699b9c1b51eSKate Stone class BreakpointNameOptionGroup : public OptionGroup {
17005e09c8c3SJim Ingham public:
1701b9c1b51eSKate Stone   BreakpointNameOptionGroup()
1702b9c1b51eSKate Stone       : OptionGroup(), m_breakpoint(LLDB_INVALID_BREAK_ID), m_use_dummy(false) {
17035e09c8c3SJim Ingham   }
17045e09c8c3SJim Ingham 
17059e85e5a8SEugene Zelenko   ~BreakpointNameOptionGroup() override = default;
17065e09c8c3SJim Ingham 
17071f0f5b5bSZachary Turner   llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
170870602439SZachary Turner     return llvm::makeArrayRef(g_breakpoint_name_options);
17095e09c8c3SJim Ingham   }
17105e09c8c3SJim Ingham 
171197206d57SZachary Turner   Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1712b9c1b51eSKate Stone                         ExecutionContext *execution_context) override {
171397206d57SZachary Turner     Status error;
17145e09c8c3SJim Ingham     const int short_option = g_breakpoint_name_options[option_idx].short_option;
17155e09c8c3SJim Ingham 
1716b9c1b51eSKate Stone     switch (short_option) {
17175e09c8c3SJim Ingham     case 'N':
1718fe11483bSZachary Turner       if (BreakpointID::StringIsBreakpointName(option_arg, error) &&
1719b9c1b51eSKate Stone           error.Success())
1720fe11483bSZachary Turner         m_name.SetValueFromString(option_arg);
17215e09c8c3SJim Ingham       break;
17225e09c8c3SJim Ingham     case 'B':
1723fe11483bSZachary Turner       if (m_breakpoint.SetValueFromString(option_arg).Fail())
1724b9c1b51eSKate Stone         error.SetErrorStringWithFormat(
17258cef4b0bSZachary Turner             "unrecognized value \"%s\" for breakpoint",
1726fe11483bSZachary Turner             option_arg.str().c_str());
17275e09c8c3SJim Ingham       break;
17285e09c8c3SJim Ingham     case 'D':
1729fe11483bSZachary Turner       if (m_use_dummy.SetValueFromString(option_arg).Fail())
1730b9c1b51eSKate Stone         error.SetErrorStringWithFormat(
17318cef4b0bSZachary Turner             "unrecognized value \"%s\" for use-dummy",
1732fe11483bSZachary Turner             option_arg.str().c_str());
17335e09c8c3SJim Ingham       break;
1734e9632ebaSJim Ingham     case 'H':
1735e9632ebaSJim Ingham       m_help_string.SetValueFromString(option_arg);
1736e9632ebaSJim Ingham       break;
17375e09c8c3SJim Ingham 
17385e09c8c3SJim Ingham     default:
1739b9c1b51eSKate Stone       error.SetErrorStringWithFormat("unrecognized short option '%c'",
1740b9c1b51eSKate Stone                                      short_option);
17415e09c8c3SJim Ingham       break;
17425e09c8c3SJim Ingham     }
17435e09c8c3SJim Ingham     return error;
17445e09c8c3SJim Ingham   }
17455e09c8c3SJim Ingham 
1746b9c1b51eSKate Stone   void OptionParsingStarting(ExecutionContext *execution_context) override {
17475e09c8c3SJim Ingham     m_name.Clear();
17485e09c8c3SJim Ingham     m_breakpoint.Clear();
17495e09c8c3SJim Ingham     m_use_dummy.Clear();
17505e09c8c3SJim Ingham     m_use_dummy.SetDefaultValue(false);
1751e9632ebaSJim Ingham     m_help_string.Clear();
17525e09c8c3SJim Ingham   }
17535e09c8c3SJim Ingham 
17545e09c8c3SJim Ingham   OptionValueString m_name;
17555e09c8c3SJim Ingham   OptionValueUInt64 m_breakpoint;
17565e09c8c3SJim Ingham   OptionValueBoolean m_use_dummy;
1757e9632ebaSJim Ingham   OptionValueString m_help_string;
17585e09c8c3SJim Ingham };
17595e09c8c3SJim Ingham 
1760b842f2ecSJim Ingham static OptionDefinition g_breakpoint_access_options[] = {
1761b842f2ecSJim Ingham     // clang-format off
1762b842f2ecSJim Ingham   {LLDB_OPT_SET_1,   false, "allow-list",    'L', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "Determines whether the breakpoint will show up in break list if not referred to explicitly."},
1763b842f2ecSJim Ingham   {LLDB_OPT_SET_2,   false, "allow-disable", 'A', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "Determines whether the breakpoint can be disabled by name or when all breakpoints are disabled."},
1764b842f2ecSJim Ingham   {LLDB_OPT_SET_3,   false, "allow-delete",  'D', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "Determines whether the breakpoint can be deleted by name or when all breakpoints are deleted."},
1765b842f2ecSJim Ingham     // clang-format on
1766b842f2ecSJim Ingham };
1767b842f2ecSJim Ingham 
1768b842f2ecSJim Ingham class BreakpointAccessOptionGroup : public OptionGroup
1769b842f2ecSJim Ingham {
1770b842f2ecSJim Ingham public:
1771b842f2ecSJim Ingham   BreakpointAccessOptionGroup() :
1772b842f2ecSJim Ingham           OptionGroup()
1773b842f2ecSJim Ingham    {}
1774b842f2ecSJim Ingham 
1775b842f2ecSJim Ingham   ~BreakpointAccessOptionGroup() override = default;
1776b842f2ecSJim Ingham 
1777b842f2ecSJim Ingham   llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
1778b842f2ecSJim Ingham     return llvm::makeArrayRef(g_breakpoint_access_options);
1779b842f2ecSJim Ingham   }
1780b842f2ecSJim Ingham   Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1781b842f2ecSJim Ingham                         ExecutionContext *execution_context) override {
1782b842f2ecSJim Ingham     Status error;
1783b842f2ecSJim Ingham     const int short_option
1784b842f2ecSJim Ingham         = g_breakpoint_access_options[option_idx].short_option;
1785b842f2ecSJim Ingham 
1786b842f2ecSJim Ingham     switch (short_option) {
1787b842f2ecSJim Ingham       case 'L': {
1788b842f2ecSJim Ingham         bool value, success;
178947cbf4a0SPavel Labath         value = OptionArgParser::ToBoolean(option_arg, false, &success);
1790b842f2ecSJim Ingham         if (success) {
1791b842f2ecSJim Ingham           m_permissions.SetAllowList(value);
1792b842f2ecSJim Ingham         } else
1793b842f2ecSJim Ingham           error.SetErrorStringWithFormat(
1794b842f2ecSJim Ingham               "invalid boolean value '%s' passed for -L option",
1795b842f2ecSJim Ingham               option_arg.str().c_str());
1796b842f2ecSJim Ingham       } break;
1797b842f2ecSJim Ingham       case 'A': {
1798b842f2ecSJim Ingham         bool value, success;
179947cbf4a0SPavel Labath         value = OptionArgParser::ToBoolean(option_arg, false, &success);
1800b842f2ecSJim Ingham         if (success) {
1801b842f2ecSJim Ingham           m_permissions.SetAllowDisable(value);
1802b842f2ecSJim Ingham         } else
1803b842f2ecSJim Ingham           error.SetErrorStringWithFormat(
1804b842f2ecSJim Ingham               "invalid boolean value '%s' passed for -L option",
1805b842f2ecSJim Ingham               option_arg.str().c_str());
1806b842f2ecSJim Ingham       } break;
1807b842f2ecSJim Ingham       case 'D': {
1808b842f2ecSJim Ingham         bool value, success;
180947cbf4a0SPavel Labath         value = OptionArgParser::ToBoolean(option_arg, false, &success);
1810b842f2ecSJim Ingham         if (success) {
1811b842f2ecSJim Ingham           m_permissions.SetAllowDelete(value);
1812b842f2ecSJim Ingham         } else
1813b842f2ecSJim Ingham           error.SetErrorStringWithFormat(
1814b842f2ecSJim Ingham               "invalid boolean value '%s' passed for -L option",
1815b842f2ecSJim Ingham               option_arg.str().c_str());
1816b842f2ecSJim Ingham       } break;
1817b842f2ecSJim Ingham 
1818b842f2ecSJim Ingham     }
1819b842f2ecSJim Ingham 
1820b842f2ecSJim Ingham     return error;
1821b842f2ecSJim Ingham   }
1822b842f2ecSJim Ingham 
1823b842f2ecSJim Ingham   void OptionParsingStarting(ExecutionContext *execution_context) override {
1824b842f2ecSJim Ingham   }
1825b842f2ecSJim Ingham 
1826b842f2ecSJim Ingham   const BreakpointName::Permissions &GetPermissions() const
1827b842f2ecSJim Ingham   {
1828b842f2ecSJim Ingham     return m_permissions;
1829b842f2ecSJim Ingham   }
1830b842f2ecSJim Ingham   BreakpointName::Permissions m_permissions;
1831b842f2ecSJim Ingham };
1832b842f2ecSJim Ingham 
1833b842f2ecSJim Ingham class CommandObjectBreakpointNameConfigure : public CommandObjectParsed {
1834b842f2ecSJim Ingham public:
1835b842f2ecSJim Ingham   CommandObjectBreakpointNameConfigure(CommandInterpreter &interpreter)
1836b842f2ecSJim Ingham       : CommandObjectParsed(
1837b842f2ecSJim Ingham             interpreter, "configure", "Configure the options for the breakpoint"
1838b842f2ecSJim Ingham             " name provided.  "
1839b842f2ecSJim Ingham             "If you provide a breakpoint id, the options will be copied from "
1840b842f2ecSJim Ingham             "the breakpoint, otherwise only the options specified will be set "
1841b842f2ecSJim Ingham             "on the name.",
1842b842f2ecSJim Ingham             "breakpoint name configure <command-options> "
1843b842f2ecSJim Ingham             "<breakpoint-name-list>"),
1844b842f2ecSJim Ingham         m_bp_opts(), m_option_group() {
1845b842f2ecSJim Ingham     // Create the first variant for the first (and only) argument for this
1846b842f2ecSJim Ingham     // command.
1847b842f2ecSJim Ingham     CommandArgumentEntry arg1;
1848b842f2ecSJim Ingham     CommandArgumentData id_arg;
1849b842f2ecSJim Ingham     id_arg.arg_type = eArgTypeBreakpointName;
1850b842f2ecSJim Ingham     id_arg.arg_repetition = eArgRepeatOptional;
1851b842f2ecSJim Ingham     arg1.push_back(id_arg);
1852b842f2ecSJim Ingham     m_arguments.push_back(arg1);
1853b842f2ecSJim Ingham 
1854b842f2ecSJim Ingham     m_option_group.Append(&m_bp_opts,
1855b842f2ecSJim Ingham                           LLDB_OPT_SET_ALL,
1856b842f2ecSJim Ingham                           LLDB_OPT_SET_1);
1857b842f2ecSJim Ingham     m_option_group.Append(&m_access_options,
1858b842f2ecSJim Ingham                           LLDB_OPT_SET_ALL,
1859b842f2ecSJim Ingham                           LLDB_OPT_SET_ALL);
1860e9632ebaSJim Ingham     m_option_group.Append(&m_bp_id,
1861e9632ebaSJim Ingham                           LLDB_OPT_SET_2|LLDB_OPT_SET_4,
1862e9632ebaSJim Ingham                           LLDB_OPT_SET_ALL);
1863b842f2ecSJim Ingham     m_option_group.Finalize();
1864b842f2ecSJim Ingham   }
1865b842f2ecSJim Ingham 
1866b842f2ecSJim Ingham   ~CommandObjectBreakpointNameConfigure() override = default;
1867b842f2ecSJim Ingham 
1868b842f2ecSJim Ingham   Options *GetOptions() override { return &m_option_group; }
1869b842f2ecSJim Ingham 
1870b842f2ecSJim Ingham protected:
1871b842f2ecSJim Ingham   bool DoExecute(Args &command, CommandReturnObject &result) override {
1872b842f2ecSJim Ingham 
1873b842f2ecSJim Ingham     const size_t argc = command.GetArgumentCount();
1874b842f2ecSJim Ingham     if (argc == 0) {
1875b842f2ecSJim Ingham       result.AppendError("No names provided.");
1876b842f2ecSJim Ingham       result.SetStatus(eReturnStatusFailed);
1877b842f2ecSJim Ingham       return false;
1878b842f2ecSJim Ingham     }
1879b842f2ecSJim Ingham 
1880b842f2ecSJim Ingham     Target *target =
1881b842f2ecSJim Ingham         GetSelectedOrDummyTarget(false);
1882b842f2ecSJim Ingham 
1883b842f2ecSJim Ingham     if (target == nullptr) {
1884b842f2ecSJim Ingham       result.AppendError("Invalid target. No existing target or breakpoints.");
1885b842f2ecSJim Ingham       result.SetStatus(eReturnStatusFailed);
1886b842f2ecSJim Ingham       return false;
1887b842f2ecSJim Ingham     }
1888b842f2ecSJim Ingham 
1889b842f2ecSJim Ingham     std::unique_lock<std::recursive_mutex> lock;
1890b842f2ecSJim Ingham     target->GetBreakpointList().GetListMutex(lock);
1891b842f2ecSJim Ingham 
1892b842f2ecSJim Ingham     // Make a pass through first to see that all the names are legal.
1893b842f2ecSJim Ingham     for (auto &entry : command.entries()) {
1894b842f2ecSJim Ingham       Status error;
1895b842f2ecSJim Ingham       if (!BreakpointID::StringIsBreakpointName(entry.ref, error))
1896b842f2ecSJim Ingham       {
1897b842f2ecSJim Ingham         result.AppendErrorWithFormat("Invalid breakpoint name: %s - %s",
1898b842f2ecSJim Ingham                                      entry.c_str(), error.AsCString());
1899b842f2ecSJim Ingham         result.SetStatus(eReturnStatusFailed);
1900b842f2ecSJim Ingham         return false;
1901b842f2ecSJim Ingham       }
1902b842f2ecSJim Ingham     }
190305097246SAdrian Prantl     // Now configure them, we already pre-checked the names so we don't need to
190405097246SAdrian Prantl     // check the error:
1905b842f2ecSJim Ingham     BreakpointSP bp_sp;
1906b842f2ecSJim Ingham     if (m_bp_id.m_breakpoint.OptionWasSet())
1907b842f2ecSJim Ingham     {
1908b842f2ecSJim Ingham       lldb::break_id_t bp_id = m_bp_id.m_breakpoint.GetUInt64Value();
1909b842f2ecSJim Ingham       bp_sp = target->GetBreakpointByID(bp_id);
1910b842f2ecSJim Ingham       if (!bp_sp)
1911b842f2ecSJim Ingham       {
1912b842f2ecSJim Ingham         result.AppendErrorWithFormatv("Could not find specified breakpoint {0}",
1913b842f2ecSJim Ingham                            bp_id);
1914b842f2ecSJim Ingham         result.SetStatus(eReturnStatusFailed);
1915b842f2ecSJim Ingham         return false;
1916b842f2ecSJim Ingham       }
1917b842f2ecSJim Ingham     }
1918b842f2ecSJim Ingham 
1919b842f2ecSJim Ingham     Status error;
1920b842f2ecSJim Ingham     for (auto &entry : command.entries()) {
1921b842f2ecSJim Ingham       ConstString name(entry.c_str());
1922b842f2ecSJim Ingham       BreakpointName *bp_name = target->FindBreakpointName(name, true, error);
1923b842f2ecSJim Ingham       if (!bp_name)
1924b842f2ecSJim Ingham         continue;
1925e9632ebaSJim Ingham       if (m_bp_id.m_help_string.OptionWasSet())
1926e9632ebaSJim Ingham         bp_name->SetHelp(m_bp_id.m_help_string.GetStringValue().str().c_str());
1927e9632ebaSJim Ingham 
1928b842f2ecSJim Ingham       if (bp_sp)
1929b842f2ecSJim Ingham         target->ConfigureBreakpointName(*bp_name,
1930b842f2ecSJim Ingham                                        *bp_sp->GetOptions(),
1931b842f2ecSJim Ingham                                        m_access_options.GetPermissions());
1932b842f2ecSJim Ingham       else
1933b842f2ecSJim Ingham         target->ConfigureBreakpointName(*bp_name,
1934b842f2ecSJim Ingham                                        m_bp_opts.GetBreakpointOptions(),
1935b842f2ecSJim Ingham                                        m_access_options.GetPermissions());
1936b842f2ecSJim Ingham     }
1937b842f2ecSJim Ingham     return true;
1938b842f2ecSJim Ingham   }
1939b842f2ecSJim Ingham 
1940b842f2ecSJim Ingham private:
1941b842f2ecSJim Ingham   BreakpointNameOptionGroup m_bp_id; // Only using the id part of this.
1942b842f2ecSJim Ingham   BreakpointOptionGroup m_bp_opts;
1943b842f2ecSJim Ingham   BreakpointAccessOptionGroup m_access_options;
1944b842f2ecSJim Ingham   OptionGroupOptions m_option_group;
1945b842f2ecSJim Ingham };
1946b842f2ecSJim Ingham 
1947b9c1b51eSKate Stone class CommandObjectBreakpointNameAdd : public CommandObjectParsed {
19485e09c8c3SJim Ingham public:
1949b9c1b51eSKate Stone   CommandObjectBreakpointNameAdd(CommandInterpreter &interpreter)
1950b9c1b51eSKate Stone       : CommandObjectParsed(
1951b9c1b51eSKate Stone             interpreter, "add", "Add a name to the breakpoints provided.",
19525e09c8c3SJim Ingham             "breakpoint name add <command-options> <breakpoint-id-list>"),
1953b9c1b51eSKate Stone         m_name_options(), m_option_group() {
1954b9c1b51eSKate Stone     // Create the first variant for the first (and only) argument for this
1955b9c1b51eSKate Stone     // command.
19565e09c8c3SJim Ingham     CommandArgumentEntry arg1;
19575e09c8c3SJim Ingham     CommandArgumentData id_arg;
19585e09c8c3SJim Ingham     id_arg.arg_type = eArgTypeBreakpointID;
19595e09c8c3SJim Ingham     id_arg.arg_repetition = eArgRepeatOptional;
19605e09c8c3SJim Ingham     arg1.push_back(id_arg);
19615e09c8c3SJim Ingham     m_arguments.push_back(arg1);
19625e09c8c3SJim Ingham 
19635e09c8c3SJim Ingham     m_option_group.Append(&m_name_options, LLDB_OPT_SET_1, LLDB_OPT_SET_ALL);
19645e09c8c3SJim Ingham     m_option_group.Finalize();
19655e09c8c3SJim Ingham   }
19665e09c8c3SJim Ingham 
19679e85e5a8SEugene Zelenko   ~CommandObjectBreakpointNameAdd() override = default;
19685e09c8c3SJim Ingham 
1969b9c1b51eSKate Stone   Options *GetOptions() override { return &m_option_group; }
19705e09c8c3SJim Ingham 
19715e09c8c3SJim Ingham protected:
1972b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
1973b9c1b51eSKate Stone     if (!m_name_options.m_name.OptionWasSet()) {
19745e09c8c3SJim Ingham       result.SetError("No name option provided.");
19755e09c8c3SJim Ingham       return false;
19765e09c8c3SJim Ingham     }
19775e09c8c3SJim Ingham 
1978b9c1b51eSKate Stone     Target *target =
1979b9c1b51eSKate Stone         GetSelectedOrDummyTarget(m_name_options.m_use_dummy.GetCurrentValue());
19805e09c8c3SJim Ingham 
1981b9c1b51eSKate Stone     if (target == nullptr) {
19825e09c8c3SJim Ingham       result.AppendError("Invalid target. No existing target or breakpoints.");
19835e09c8c3SJim Ingham       result.SetStatus(eReturnStatusFailed);
19845e09c8c3SJim Ingham       return false;
19855e09c8c3SJim Ingham     }
19865e09c8c3SJim Ingham 
1987bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
1988bb19a13cSSaleem Abdulrasool     target->GetBreakpointList().GetListMutex(lock);
19895e09c8c3SJim Ingham 
19905e09c8c3SJim Ingham     const BreakpointList &breakpoints = target->GetBreakpointList();
19915e09c8c3SJim Ingham 
19925e09c8c3SJim Ingham     size_t num_breakpoints = breakpoints.GetSize();
1993b9c1b51eSKate Stone     if (num_breakpoints == 0) {
19945e09c8c3SJim Ingham       result.SetError("No breakpoints, cannot add names.");
19955e09c8c3SJim Ingham       result.SetStatus(eReturnStatusFailed);
19965e09c8c3SJim Ingham       return false;
19975e09c8c3SJim Ingham     }
19985e09c8c3SJim Ingham 
19995e09c8c3SJim Ingham     // Particular breakpoint selected; disable that breakpoint.
20005e09c8c3SJim Ingham     BreakpointIDList valid_bp_ids;
2001b9c1b51eSKate Stone     CommandObjectMultiwordBreakpoint::VerifyBreakpointIDs(
2002b842f2ecSJim Ingham         command, target, result, &valid_bp_ids,
2003b842f2ecSJim Ingham         BreakpointName::Permissions::PermissionKinds::listPerm);
20045e09c8c3SJim Ingham 
2005b9c1b51eSKate Stone     if (result.Succeeded()) {
2006b9c1b51eSKate Stone       if (valid_bp_ids.GetSize() == 0) {
20075e09c8c3SJim Ingham         result.SetError("No breakpoints specified, cannot add names.");
20085e09c8c3SJim Ingham         result.SetStatus(eReturnStatusFailed);
20095e09c8c3SJim Ingham         return false;
20105e09c8c3SJim Ingham       }
20115e09c8c3SJim Ingham       size_t num_valid_ids = valid_bp_ids.GetSize();
2012b842f2ecSJim Ingham       const char *bp_name = m_name_options.m_name.GetCurrentValue();
2013b842f2ecSJim Ingham       Status error; // This error reports illegal names, but we've already
2014b842f2ecSJim Ingham                     // checked that, so we don't need to check it again here.
2015b9c1b51eSKate Stone       for (size_t index = 0; index < num_valid_ids; index++) {
2016b9c1b51eSKate Stone         lldb::break_id_t bp_id =
2017b9c1b51eSKate Stone             valid_bp_ids.GetBreakpointIDAtIndex(index).GetBreakpointID();
20185e09c8c3SJim Ingham         BreakpointSP bp_sp = breakpoints.FindBreakpointByID(bp_id);
2019b842f2ecSJim Ingham         target->AddNameToBreakpoint(bp_sp, bp_name, error);
20205e09c8c3SJim Ingham       }
20215e09c8c3SJim Ingham     }
20225e09c8c3SJim Ingham 
20235e09c8c3SJim Ingham     return true;
20245e09c8c3SJim Ingham   }
20255e09c8c3SJim Ingham 
20265e09c8c3SJim Ingham private:
20275e09c8c3SJim Ingham   BreakpointNameOptionGroup m_name_options;
20285e09c8c3SJim Ingham   OptionGroupOptions m_option_group;
20295e09c8c3SJim Ingham };
20305e09c8c3SJim Ingham 
2031b9c1b51eSKate Stone class CommandObjectBreakpointNameDelete : public CommandObjectParsed {
20325e09c8c3SJim Ingham public:
2033b9c1b51eSKate Stone   CommandObjectBreakpointNameDelete(CommandInterpreter &interpreter)
2034b9c1b51eSKate Stone       : CommandObjectParsed(
2035b9c1b51eSKate Stone             interpreter, "delete",
20365e09c8c3SJim Ingham             "Delete a name from the breakpoints provided.",
20375e09c8c3SJim Ingham             "breakpoint name delete <command-options> <breakpoint-id-list>"),
2038b9c1b51eSKate Stone         m_name_options(), m_option_group() {
2039b9c1b51eSKate Stone     // Create the first variant for the first (and only) argument for this
2040b9c1b51eSKate Stone     // command.
20415e09c8c3SJim Ingham     CommandArgumentEntry arg1;
20425e09c8c3SJim Ingham     CommandArgumentData id_arg;
20435e09c8c3SJim Ingham     id_arg.arg_type = eArgTypeBreakpointID;
20445e09c8c3SJim Ingham     id_arg.arg_repetition = eArgRepeatOptional;
20455e09c8c3SJim Ingham     arg1.push_back(id_arg);
20465e09c8c3SJim Ingham     m_arguments.push_back(arg1);
20475e09c8c3SJim Ingham 
20485e09c8c3SJim Ingham     m_option_group.Append(&m_name_options, LLDB_OPT_SET_1, LLDB_OPT_SET_ALL);
20495e09c8c3SJim Ingham     m_option_group.Finalize();
20505e09c8c3SJim Ingham   }
20515e09c8c3SJim Ingham 
20529e85e5a8SEugene Zelenko   ~CommandObjectBreakpointNameDelete() override = default;
20535e09c8c3SJim Ingham 
2054b9c1b51eSKate Stone   Options *GetOptions() override { return &m_option_group; }
20555e09c8c3SJim Ingham 
20565e09c8c3SJim Ingham protected:
2057b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
2058b9c1b51eSKate Stone     if (!m_name_options.m_name.OptionWasSet()) {
20595e09c8c3SJim Ingham       result.SetError("No name option provided.");
20605e09c8c3SJim Ingham       return false;
20615e09c8c3SJim Ingham     }
20625e09c8c3SJim Ingham 
2063b9c1b51eSKate Stone     Target *target =
2064b9c1b51eSKate Stone         GetSelectedOrDummyTarget(m_name_options.m_use_dummy.GetCurrentValue());
20655e09c8c3SJim Ingham 
2066b9c1b51eSKate Stone     if (target == nullptr) {
20675e09c8c3SJim Ingham       result.AppendError("Invalid target. No existing target or breakpoints.");
20685e09c8c3SJim Ingham       result.SetStatus(eReturnStatusFailed);
20695e09c8c3SJim Ingham       return false;
20705e09c8c3SJim Ingham     }
20715e09c8c3SJim Ingham 
2072bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock;
2073bb19a13cSSaleem Abdulrasool     target->GetBreakpointList().GetListMutex(lock);
20745e09c8c3SJim Ingham 
20755e09c8c3SJim Ingham     const BreakpointList &breakpoints = target->GetBreakpointList();
20765e09c8c3SJim Ingham 
20775e09c8c3SJim Ingham     size_t num_breakpoints = breakpoints.GetSize();
2078b9c1b51eSKate Stone     if (num_breakpoints == 0) {
20795e09c8c3SJim Ingham       result.SetError("No breakpoints, cannot delete names.");
20805e09c8c3SJim Ingham       result.SetStatus(eReturnStatusFailed);
20815e09c8c3SJim Ingham       return false;
20825e09c8c3SJim Ingham     }
20835e09c8c3SJim Ingham 
20845e09c8c3SJim Ingham     // Particular breakpoint selected; disable that breakpoint.
20855e09c8c3SJim Ingham     BreakpointIDList valid_bp_ids;
2086b9c1b51eSKate Stone     CommandObjectMultiwordBreakpoint::VerifyBreakpointIDs(
2087b842f2ecSJim Ingham         command, target, result, &valid_bp_ids,
2088b842f2ecSJim Ingham         BreakpointName::Permissions::PermissionKinds::deletePerm);
20895e09c8c3SJim Ingham 
2090b9c1b51eSKate Stone     if (result.Succeeded()) {
2091b9c1b51eSKate Stone       if (valid_bp_ids.GetSize() == 0) {
20925e09c8c3SJim Ingham         result.SetError("No breakpoints specified, cannot delete names.");
20935e09c8c3SJim Ingham         result.SetStatus(eReturnStatusFailed);
20945e09c8c3SJim Ingham         return false;
20955e09c8c3SJim Ingham       }
2096b842f2ecSJim Ingham       ConstString bp_name(m_name_options.m_name.GetCurrentValue());
20975e09c8c3SJim Ingham       size_t num_valid_ids = valid_bp_ids.GetSize();
2098b9c1b51eSKate Stone       for (size_t index = 0; index < num_valid_ids; index++) {
2099b9c1b51eSKate Stone         lldb::break_id_t bp_id =
2100b9c1b51eSKate Stone             valid_bp_ids.GetBreakpointIDAtIndex(index).GetBreakpointID();
21015e09c8c3SJim Ingham         BreakpointSP bp_sp = breakpoints.FindBreakpointByID(bp_id);
2102b842f2ecSJim Ingham         target->RemoveNameFromBreakpoint(bp_sp, bp_name);
21035e09c8c3SJim Ingham       }
21045e09c8c3SJim Ingham     }
21055e09c8c3SJim Ingham 
21065e09c8c3SJim Ingham     return true;
21075e09c8c3SJim Ingham   }
21085e09c8c3SJim Ingham 
21095e09c8c3SJim Ingham private:
21105e09c8c3SJim Ingham   BreakpointNameOptionGroup m_name_options;
21115e09c8c3SJim Ingham   OptionGroupOptions m_option_group;
21125e09c8c3SJim Ingham };
21135e09c8c3SJim Ingham 
2114b9c1b51eSKate Stone class CommandObjectBreakpointNameList : public CommandObjectParsed {
21155e09c8c3SJim Ingham public:
2116b9c1b51eSKate Stone   CommandObjectBreakpointNameList(CommandInterpreter &interpreter)
2117b9c1b51eSKate Stone       : CommandObjectParsed(interpreter, "list",
2118b842f2ecSJim Ingham                             "List either the names for a breakpoint or info "
2119b842f2ecSJim Ingham                             "about a given name.  With no arguments, lists all "
2120b842f2ecSJim Ingham                             "names",
21215e09c8c3SJim Ingham                             "breakpoint name list <command-options>"),
2122b9c1b51eSKate Stone         m_name_options(), m_option_group() {
2123b842f2ecSJim Ingham     m_option_group.Append(&m_name_options, LLDB_OPT_SET_3, LLDB_OPT_SET_ALL);
21245e09c8c3SJim Ingham     m_option_group.Finalize();
21255e09c8c3SJim Ingham   }
21265e09c8c3SJim Ingham 
21279e85e5a8SEugene Zelenko   ~CommandObjectBreakpointNameList() override = default;
21285e09c8c3SJim Ingham 
2129b9c1b51eSKate Stone   Options *GetOptions() override { return &m_option_group; }
21305e09c8c3SJim Ingham 
21315e09c8c3SJim Ingham protected:
2132b9c1b51eSKate Stone   bool DoExecute(Args &command, CommandReturnObject &result) override {
2133b9c1b51eSKate Stone     Target *target =
2134b9c1b51eSKate Stone         GetSelectedOrDummyTarget(m_name_options.m_use_dummy.GetCurrentValue());
21355e09c8c3SJim Ingham 
2136b9c1b51eSKate Stone     if (target == nullptr) {
21375e09c8c3SJim Ingham       result.AppendError("Invalid target. No existing target or breakpoints.");
21385e09c8c3SJim Ingham       result.SetStatus(eReturnStatusFailed);
21395e09c8c3SJim Ingham       return false;
21405e09c8c3SJim Ingham     }
21415e09c8c3SJim Ingham 
2142b842f2ecSJim Ingham 
2143b842f2ecSJim Ingham     std::vector<std::string> name_list;
2144b842f2ecSJim Ingham     if (command.empty()) {
2145b842f2ecSJim Ingham       target->GetBreakpointNames(name_list);
2146b842f2ecSJim Ingham     } else {
2147b842f2ecSJim Ingham       for (const Args::ArgEntry &arg : command)
2148b842f2ecSJim Ingham       {
2149b842f2ecSJim Ingham         name_list.push_back(arg.c_str());
2150b842f2ecSJim Ingham       }
2151b842f2ecSJim Ingham     }
2152b842f2ecSJim Ingham 
2153b842f2ecSJim Ingham     if (name_list.empty()) {
2154b842f2ecSJim Ingham       result.AppendMessage("No breakpoint names found.");
2155b842f2ecSJim Ingham     } else {
2156b842f2ecSJim Ingham       for (const std::string &name_str : name_list) {
2157b842f2ecSJim Ingham         const char *name = name_str.c_str();
2158b842f2ecSJim Ingham         // First print out the options for the name:
2159b842f2ecSJim Ingham         Status error;
2160b842f2ecSJim Ingham         BreakpointName *bp_name = target->FindBreakpointName(ConstString(name),
2161b842f2ecSJim Ingham                                                              false,
2162b842f2ecSJim Ingham                                                              error);
2163b842f2ecSJim Ingham         if (bp_name)
2164b842f2ecSJim Ingham         {
2165b842f2ecSJim Ingham           StreamString s;
2166b842f2ecSJim Ingham           result.AppendMessageWithFormat("Name: %s\n", name);
2167b842f2ecSJim Ingham           if (bp_name->GetDescription(&s, eDescriptionLevelFull))
2168b842f2ecSJim Ingham           {
2169b842f2ecSJim Ingham             result.AppendMessage(s.GetString());
2170b842f2ecSJim Ingham           }
2171b842f2ecSJim Ingham 
2172bb19a13cSSaleem Abdulrasool           std::unique_lock<std::recursive_mutex> lock;
2173bb19a13cSSaleem Abdulrasool           target->GetBreakpointList().GetListMutex(lock);
21745e09c8c3SJim Ingham 
21755e09c8c3SJim Ingham           BreakpointList &breakpoints = target->GetBreakpointList();
2176b842f2ecSJim Ingham           bool any_set = false;
2177b9c1b51eSKate Stone           for (BreakpointSP bp_sp : breakpoints.Breakpoints()) {
2178b9c1b51eSKate Stone             if (bp_sp->MatchesName(name)) {
21795e09c8c3SJim Ingham               StreamString s;
2180b842f2ecSJim Ingham               any_set = true;
21815e09c8c3SJim Ingham               bp_sp->GetDescription(&s, eDescriptionLevelBrief);
21825e09c8c3SJim Ingham               s.EOL();
2183c156427dSZachary Turner               result.AppendMessage(s.GetString());
21845e09c8c3SJim Ingham             }
21855e09c8c3SJim Ingham           }
2186b842f2ecSJim Ingham           if (!any_set)
2187b842f2ecSJim Ingham             result.AppendMessage("No breakpoints using this name.");
2188b9c1b51eSKate Stone         } else {
2189b842f2ecSJim Ingham           result.AppendMessageWithFormat("Name: %s not found.\n", name);
21905e09c8c3SJim Ingham         }
2191b842f2ecSJim Ingham       }
21925e09c8c3SJim Ingham     }
21935e09c8c3SJim Ingham     return true;
21945e09c8c3SJim Ingham   }
21955e09c8c3SJim Ingham 
21965e09c8c3SJim Ingham private:
21975e09c8c3SJim Ingham   BreakpointNameOptionGroup m_name_options;
21985e09c8c3SJim Ingham   OptionGroupOptions m_option_group;
21995e09c8c3SJim Ingham };
22005e09c8c3SJim Ingham 
22015e09c8c3SJim Ingham //-------------------------------------------------------------------------
2202e14dc268SJim Ingham // CommandObjectBreakpointName
22035e09c8c3SJim Ingham //-------------------------------------------------------------------------
2204b9c1b51eSKate Stone class CommandObjectBreakpointName : public CommandObjectMultiword {
22055e09c8c3SJim Ingham public:
22067428a18cSKate Stone   CommandObjectBreakpointName(CommandInterpreter &interpreter)
2207b9c1b51eSKate Stone       : CommandObjectMultiword(
2208b9c1b51eSKate Stone             interpreter, "name", "Commands to manage name tags for breakpoints",
2209b9c1b51eSKate Stone             "breakpoint name <subcommand> [<command-options>]") {
2210b9c1b51eSKate Stone     CommandObjectSP add_command_object(
2211b9c1b51eSKate Stone         new CommandObjectBreakpointNameAdd(interpreter));
2212b9c1b51eSKate Stone     CommandObjectSP delete_command_object(
2213b9c1b51eSKate Stone         new CommandObjectBreakpointNameDelete(interpreter));
2214b9c1b51eSKate Stone     CommandObjectSP list_command_object(
2215b9c1b51eSKate Stone         new CommandObjectBreakpointNameList(interpreter));
2216b842f2ecSJim Ingham     CommandObjectSP configure_command_object(
2217b842f2ecSJim Ingham         new CommandObjectBreakpointNameConfigure(interpreter));
22185e09c8c3SJim Ingham 
22195e09c8c3SJim Ingham     LoadSubCommand("add", add_command_object);
22205e09c8c3SJim Ingham     LoadSubCommand("delete", delete_command_object);
22215e09c8c3SJim Ingham     LoadSubCommand("list", list_command_object);
2222b842f2ecSJim Ingham     LoadSubCommand("configure", configure_command_object);
22235e09c8c3SJim Ingham   }
22245e09c8c3SJim Ingham 
22259e85e5a8SEugene Zelenko   ~CommandObjectBreakpointName() override = default;
22265e09c8c3SJim Ingham };
22275e09c8c3SJim Ingham 
22285e09c8c3SJim Ingham //-------------------------------------------------------------------------
2229e14dc268SJim Ingham // CommandObjectBreakpointRead
2230e14dc268SJim Ingham //-------------------------------------------------------------------------
22313acdf385SJim Ingham #pragma mark Read::CommandOptions
22321f0f5b5bSZachary Turner static OptionDefinition g_breakpoint_read_options[] = {
22331f0f5b5bSZachary Turner     // clang-format off
22341f0f5b5bSZachary Turner   { LLDB_OPT_SET_ALL, true, "file",                   'f', OptionParser::eRequiredArgument, nullptr, nullptr, CommandCompletions::eDiskFileCompletion, eArgTypeFilename,       "The file from which to read the breakpoints." },
22353acdf385SJim Ingham   {LLDB_OPT_SET_ALL, false, "breakpoint-name",        'N', OptionParser::eRequiredArgument, nullptr, nullptr, 0,                                       eArgTypeBreakpointName, "Only read in breakpoints with this name."},
22361f0f5b5bSZachary Turner     // clang-format on
22371f0f5b5bSZachary Turner };
22381f0f5b5bSZachary Turner 
22391f0f5b5bSZachary Turner #pragma mark Read
2240e14dc268SJim Ingham 
2241e14dc268SJim Ingham class CommandObjectBreakpointRead : public CommandObjectParsed {
2242e14dc268SJim Ingham public:
2243e14dc268SJim Ingham   CommandObjectBreakpointRead(CommandInterpreter &interpreter)
2244e14dc268SJim Ingham       : CommandObjectParsed(interpreter, "breakpoint read",
2245e14dc268SJim Ingham                             "Read and set the breakpoints previously saved to "
2246e14dc268SJim Ingham                             "a file with \"breakpoint write\".  ",
2247e14dc268SJim Ingham                             nullptr),
2248e14dc268SJim Ingham         m_options() {
2249e14dc268SJim Ingham     CommandArgumentEntry arg;
2250e14dc268SJim Ingham     CommandObject::AddIDsArgumentData(arg, eArgTypeBreakpointID,
2251e14dc268SJim Ingham                                       eArgTypeBreakpointIDRange);
2252e14dc268SJim Ingham     // Add the entry for the first argument for this command to the object's
2253e14dc268SJim Ingham     // arguments vector.
2254e14dc268SJim Ingham     m_arguments.push_back(arg);
2255e14dc268SJim Ingham   }
2256e14dc268SJim Ingham 
2257e14dc268SJim Ingham   ~CommandObjectBreakpointRead() override = default;
2258e14dc268SJim Ingham 
2259e14dc268SJim Ingham   Options *GetOptions() override { return &m_options; }
2260e14dc268SJim Ingham 
2261e14dc268SJim Ingham   class CommandOptions : public Options {
2262e14dc268SJim Ingham   public:
2263e14dc268SJim Ingham     CommandOptions() : Options() {}
2264e14dc268SJim Ingham 
2265e14dc268SJim Ingham     ~CommandOptions() override = default;
2266e14dc268SJim Ingham 
226797206d57SZachary Turner     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
2268e14dc268SJim Ingham                           ExecutionContext *execution_context) override {
226997206d57SZachary Turner       Status error;
2270e14dc268SJim Ingham       const int short_option = m_getopt_table[option_idx].val;
2271e14dc268SJim Ingham 
2272e14dc268SJim Ingham       switch (short_option) {
2273e14dc268SJim Ingham       case 'f':
2274e14dc268SJim Ingham         m_filename.assign(option_arg);
2275e14dc268SJim Ingham         break;
22763acdf385SJim Ingham       case 'N': {
227797206d57SZachary Turner         Status name_error;
22783acdf385SJim Ingham         if (!BreakpointID::StringIsBreakpointName(llvm::StringRef(option_arg),
22793acdf385SJim Ingham                                                   name_error)) {
22803acdf385SJim Ingham           error.SetErrorStringWithFormat("Invalid breakpoint name: %s",
22813acdf385SJim Ingham                                          name_error.AsCString());
22823acdf385SJim Ingham         }
22833acdf385SJim Ingham         m_names.push_back(option_arg);
22843acdf385SJim Ingham         break;
22853acdf385SJim Ingham       }
2286e14dc268SJim Ingham       default:
2287e14dc268SJim Ingham         error.SetErrorStringWithFormat("unrecognized option '%c'",
2288e14dc268SJim Ingham                                        short_option);
2289e14dc268SJim Ingham         break;
2290e14dc268SJim Ingham       }
2291e14dc268SJim Ingham 
2292e14dc268SJim Ingham       return error;
2293e14dc268SJim Ingham     }
2294e14dc268SJim Ingham 
2295e14dc268SJim Ingham     void OptionParsingStarting(ExecutionContext *execution_context) override {
2296e14dc268SJim Ingham       m_filename.clear();
22973acdf385SJim Ingham       m_names.clear();
2298e14dc268SJim Ingham     }
2299e14dc268SJim Ingham 
23001f0f5b5bSZachary Turner     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
230170602439SZachary Turner       return llvm::makeArrayRef(g_breakpoint_read_options);
23021f0f5b5bSZachary Turner     }
2303e14dc268SJim Ingham 
2304e14dc268SJim Ingham     // Instance variables to hold the values for command options.
2305e14dc268SJim Ingham 
2306e14dc268SJim Ingham     std::string m_filename;
23073acdf385SJim Ingham     std::vector<std::string> m_names;
2308e14dc268SJim Ingham   };
2309e14dc268SJim Ingham 
2310e14dc268SJim Ingham protected:
2311e14dc268SJim Ingham   bool DoExecute(Args &command, CommandReturnObject &result) override {
2312e14dc268SJim Ingham     Target *target = GetSelectedOrDummyTarget();
2313e14dc268SJim Ingham     if (target == nullptr) {
2314e14dc268SJim Ingham       result.AppendError("Invalid target.  No existing target or breakpoints.");
2315e14dc268SJim Ingham       result.SetStatus(eReturnStatusFailed);
2316e14dc268SJim Ingham       return false;
2317e14dc268SJim Ingham     }
2318e14dc268SJim Ingham 
23193acdf385SJim Ingham     std::unique_lock<std::recursive_mutex> lock;
23203acdf385SJim Ingham     target->GetBreakpointList().GetListMutex(lock);
23213acdf385SJim Ingham 
2322e14dc268SJim Ingham     FileSpec input_spec(m_options.m_filename, true);
232301f16664SJim Ingham     BreakpointIDList new_bps;
232497206d57SZachary Turner     Status error = target->CreateBreakpointsFromFile(
232597206d57SZachary Turner         input_spec, m_options.m_names, new_bps);
2326e14dc268SJim Ingham 
2327e14dc268SJim Ingham     if (!error.Success()) {
232801f16664SJim Ingham       result.AppendError(error.AsCString());
2329e14dc268SJim Ingham       result.SetStatus(eReturnStatusFailed);
233001f16664SJim Ingham       return false;
2331e14dc268SJim Ingham     }
23323acdf385SJim Ingham 
23333acdf385SJim Ingham     Stream &output_stream = result.GetOutputStream();
23343acdf385SJim Ingham 
23353acdf385SJim Ingham     size_t num_breakpoints = new_bps.GetSize();
23363acdf385SJim Ingham     if (num_breakpoints == 0) {
23373acdf385SJim Ingham       result.AppendMessage("No breakpoints added.");
23383acdf385SJim Ingham     } else {
23393acdf385SJim Ingham       // No breakpoint selected; show info about all currently set breakpoints.
23403acdf385SJim Ingham       result.AppendMessage("New breakpoints:");
23413acdf385SJim Ingham       for (size_t i = 0; i < num_breakpoints; ++i) {
23423acdf385SJim Ingham         BreakpointID bp_id = new_bps.GetBreakpointIDAtIndex(i);
23433acdf385SJim Ingham         Breakpoint *bp = target->GetBreakpointList()
23443acdf385SJim Ingham                              .FindBreakpointByID(bp_id.GetBreakpointID())
23453acdf385SJim Ingham                              .get();
23463acdf385SJim Ingham         if (bp)
23473acdf385SJim Ingham           bp->GetDescription(&output_stream, lldb::eDescriptionLevelInitial,
23483acdf385SJim Ingham                              false);
23493acdf385SJim Ingham       }
23503acdf385SJim Ingham     }
2351e14dc268SJim Ingham     return result.Succeeded();
2352e14dc268SJim Ingham   }
2353e14dc268SJim Ingham 
2354e14dc268SJim Ingham private:
2355e14dc268SJim Ingham   CommandOptions m_options;
2356e14dc268SJim Ingham };
2357e14dc268SJim Ingham 
2358e14dc268SJim Ingham //-------------------------------------------------------------------------
2359e14dc268SJim Ingham // CommandObjectBreakpointWrite
2360e14dc268SJim Ingham //-------------------------------------------------------------------------
23611f0f5b5bSZachary Turner #pragma mark Write::CommandOptions
23621f0f5b5bSZachary Turner static OptionDefinition g_breakpoint_write_options[] = {
23631f0f5b5bSZachary Turner     // clang-format off
23641f0f5b5bSZachary Turner   { LLDB_OPT_SET_ALL, true,  "file",  'f', OptionParser::eRequiredArgument, nullptr, nullptr, CommandCompletions::eDiskFileCompletion, eArgTypeFilename,    "The file into which to write the breakpoints." },
23652d3628e1SJim Ingham   { LLDB_OPT_SET_ALL, false, "append",'a', OptionParser::eNoArgument,       nullptr, nullptr, 0,                                       eArgTypeNone,        "Append to saved breakpoints file if it exists."},
23661f0f5b5bSZachary Turner     // clang-format on
23671f0f5b5bSZachary Turner };
23681f0f5b5bSZachary Turner 
23691f0f5b5bSZachary Turner #pragma mark Write
2370e14dc268SJim Ingham class CommandObjectBreakpointWrite : public CommandObjectParsed {
2371e14dc268SJim Ingham public:
2372e14dc268SJim Ingham   CommandObjectBreakpointWrite(CommandInterpreter &interpreter)
2373e14dc268SJim Ingham       : CommandObjectParsed(interpreter, "breakpoint write",
2374e14dc268SJim Ingham                             "Write the breakpoints listed to a file that can "
2375e14dc268SJim Ingham                             "be read in with \"breakpoint read\".  "
2376e14dc268SJim Ingham                             "If given no arguments, writes all breakpoints.",
2377e14dc268SJim Ingham                             nullptr),
2378e14dc268SJim Ingham         m_options() {
2379e14dc268SJim Ingham     CommandArgumentEntry arg;
2380e14dc268SJim Ingham     CommandObject::AddIDsArgumentData(arg, eArgTypeBreakpointID,
2381e14dc268SJim Ingham                                       eArgTypeBreakpointIDRange);
2382e14dc268SJim Ingham     // Add the entry for the first argument for this command to the object's
2383e14dc268SJim Ingham     // arguments vector.
2384e14dc268SJim Ingham     m_arguments.push_back(arg);
2385e14dc268SJim Ingham   }
2386e14dc268SJim Ingham 
2387e14dc268SJim Ingham   ~CommandObjectBreakpointWrite() override = default;
2388e14dc268SJim Ingham 
2389e14dc268SJim Ingham   Options *GetOptions() override { return &m_options; }
2390e14dc268SJim Ingham 
2391e14dc268SJim Ingham   class CommandOptions : public Options {
2392e14dc268SJim Ingham   public:
2393e14dc268SJim Ingham     CommandOptions() : Options() {}
2394e14dc268SJim Ingham 
2395e14dc268SJim Ingham     ~CommandOptions() override = default;
2396e14dc268SJim Ingham 
239797206d57SZachary Turner     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
2398e14dc268SJim Ingham                           ExecutionContext *execution_context) override {
239997206d57SZachary Turner       Status error;
2400e14dc268SJim Ingham       const int short_option = m_getopt_table[option_idx].val;
2401e14dc268SJim Ingham 
2402e14dc268SJim Ingham       switch (short_option) {
2403e14dc268SJim Ingham       case 'f':
2404e14dc268SJim Ingham         m_filename.assign(option_arg);
2405e14dc268SJim Ingham         break;
24062d3628e1SJim Ingham       case 'a':
24072d3628e1SJim Ingham         m_append = true;
24082d3628e1SJim Ingham         break;
2409e14dc268SJim Ingham       default:
2410e14dc268SJim Ingham         error.SetErrorStringWithFormat("unrecognized option '%c'",
2411e14dc268SJim Ingham                                        short_option);
2412e14dc268SJim Ingham         break;
2413e14dc268SJim Ingham       }
2414e14dc268SJim Ingham 
2415e14dc268SJim Ingham       return error;
2416e14dc268SJim Ingham     }
2417e14dc268SJim Ingham 
2418e14dc268SJim Ingham     void OptionParsingStarting(ExecutionContext *execution_context) override {
2419e14dc268SJim Ingham       m_filename.clear();
24202d3628e1SJim Ingham       m_append = false;
2421e14dc268SJim Ingham     }
2422e14dc268SJim Ingham 
24231f0f5b5bSZachary Turner     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
242470602439SZachary Turner       return llvm::makeArrayRef(g_breakpoint_write_options);
24251f0f5b5bSZachary Turner     }
2426e14dc268SJim Ingham 
2427e14dc268SJim Ingham     // Instance variables to hold the values for command options.
2428e14dc268SJim Ingham 
2429e14dc268SJim Ingham     std::string m_filename;
24302d3628e1SJim Ingham     bool m_append = false;
2431e14dc268SJim Ingham   };
2432e14dc268SJim Ingham 
2433e14dc268SJim Ingham protected:
2434e14dc268SJim Ingham   bool DoExecute(Args &command, CommandReturnObject &result) override {
2435e14dc268SJim Ingham     Target *target = GetSelectedOrDummyTarget();
2436e14dc268SJim Ingham     if (target == nullptr) {
2437e14dc268SJim Ingham       result.AppendError("Invalid target.  No existing target or breakpoints.");
2438e14dc268SJim Ingham       result.SetStatus(eReturnStatusFailed);
2439e14dc268SJim Ingham       return false;
2440e14dc268SJim Ingham     }
2441e14dc268SJim Ingham 
2442e14dc268SJim Ingham     std::unique_lock<std::recursive_mutex> lock;
2443e14dc268SJim Ingham     target->GetBreakpointList().GetListMutex(lock);
2444e14dc268SJim Ingham 
2445e14dc268SJim Ingham     BreakpointIDList valid_bp_ids;
244611eb9c64SZachary Turner     if (!command.empty()) {
2447e14dc268SJim Ingham       CommandObjectMultiwordBreakpoint::VerifyBreakpointIDs(
2448b842f2ecSJim Ingham           command, target, result, &valid_bp_ids,
2449b842f2ecSJim Ingham           BreakpointName::Permissions::PermissionKinds::listPerm);
2450e14dc268SJim Ingham 
245101f16664SJim Ingham       if (!result.Succeeded()) {
2452e14dc268SJim Ingham         result.SetStatus(eReturnStatusFailed);
2453e14dc268SJim Ingham         return false;
2454e14dc268SJim Ingham       }
2455e14dc268SJim Ingham     }
245697206d57SZachary Turner     Status error = target->SerializeBreakpointsToFile(
2457771ef6d4SMalcolm Parsons         FileSpec(m_options.m_filename, true), valid_bp_ids, m_options.m_append);
245801f16664SJim Ingham     if (!error.Success()) {
245901f16664SJim Ingham       result.AppendErrorWithFormat("error serializing breakpoints: %s.",
246001f16664SJim Ingham                                    error.AsCString());
246101f16664SJim Ingham       result.SetStatus(eReturnStatusFailed);
2462e14dc268SJim Ingham     }
2463e14dc268SJim Ingham     return result.Succeeded();
2464e14dc268SJim Ingham   }
2465e14dc268SJim Ingham 
2466e14dc268SJim Ingham private:
2467e14dc268SJim Ingham   CommandOptions m_options;
2468e14dc268SJim Ingham };
2469e14dc268SJim Ingham 
2470e14dc268SJim Ingham //-------------------------------------------------------------------------
247130fdc8d8SChris Lattner // CommandObjectMultiwordBreakpoint
247230fdc8d8SChris Lattner //-------------------------------------------------------------------------
2473ae1c4cf5SJim Ingham #pragma mark MultiwordBreakpoint
247430fdc8d8SChris Lattner 
2475b9c1b51eSKate Stone CommandObjectMultiwordBreakpoint::CommandObjectMultiwordBreakpoint(
2476b9c1b51eSKate Stone     CommandInterpreter &interpreter)
2477b9c1b51eSKate Stone     : CommandObjectMultiword(
2478b9c1b51eSKate Stone           interpreter, "breakpoint",
24797428a18cSKate Stone           "Commands for operating on breakpoints (see 'help b' for shorthand.)",
2480b9c1b51eSKate Stone           "breakpoint <subcommand> [<command-options>]") {
2481b9c1b51eSKate Stone   CommandObjectSP list_command_object(
2482b9c1b51eSKate Stone       new CommandObjectBreakpointList(interpreter));
2483b9c1b51eSKate Stone   CommandObjectSP enable_command_object(
2484b9c1b51eSKate Stone       new CommandObjectBreakpointEnable(interpreter));
2485b9c1b51eSKate Stone   CommandObjectSP disable_command_object(
2486b9c1b51eSKate Stone       new CommandObjectBreakpointDisable(interpreter));
2487b9c1b51eSKate Stone   CommandObjectSP clear_command_object(
2488b9c1b51eSKate Stone       new CommandObjectBreakpointClear(interpreter));
2489b9c1b51eSKate Stone   CommandObjectSP delete_command_object(
2490b9c1b51eSKate Stone       new CommandObjectBreakpointDelete(interpreter));
2491b9c1b51eSKate Stone   CommandObjectSP set_command_object(
2492b9c1b51eSKate Stone       new CommandObjectBreakpointSet(interpreter));
2493b9c1b51eSKate Stone   CommandObjectSP command_command_object(
2494b9c1b51eSKate Stone       new CommandObjectBreakpointCommand(interpreter));
2495b9c1b51eSKate Stone   CommandObjectSP modify_command_object(
2496b9c1b51eSKate Stone       new CommandObjectBreakpointModify(interpreter));
2497b9c1b51eSKate Stone   CommandObjectSP name_command_object(
2498b9c1b51eSKate Stone       new CommandObjectBreakpointName(interpreter));
2499e14dc268SJim Ingham   CommandObjectSP write_command_object(
2500e14dc268SJim Ingham       new CommandObjectBreakpointWrite(interpreter));
2501e14dc268SJim Ingham   CommandObjectSP read_command_object(
2502e14dc268SJim Ingham       new CommandObjectBreakpointRead(interpreter));
250330fdc8d8SChris Lattner 
2504b7234e40SJohnny Chen   list_command_object->SetCommandName("breakpoint list");
250530fdc8d8SChris Lattner   enable_command_object->SetCommandName("breakpoint enable");
250630fdc8d8SChris Lattner   disable_command_object->SetCommandName("breakpoint disable");
2507b7234e40SJohnny Chen   clear_command_object->SetCommandName("breakpoint clear");
2508b7234e40SJohnny Chen   delete_command_object->SetCommandName("breakpoint delete");
2509ae1c4cf5SJim Ingham   set_command_object->SetCommandName("breakpoint set");
2510b7234e40SJohnny Chen   command_command_object->SetCommandName("breakpoint command");
2511b7234e40SJohnny Chen   modify_command_object->SetCommandName("breakpoint modify");
25125e09c8c3SJim Ingham   name_command_object->SetCommandName("breakpoint name");
2513e14dc268SJim Ingham   write_command_object->SetCommandName("breakpoint write");
2514e14dc268SJim Ingham   read_command_object->SetCommandName("breakpoint read");
251530fdc8d8SChris Lattner 
251623f59509SGreg Clayton   LoadSubCommand("list", list_command_object);
251723f59509SGreg Clayton   LoadSubCommand("enable", enable_command_object);
251823f59509SGreg Clayton   LoadSubCommand("disable", disable_command_object);
251923f59509SGreg Clayton   LoadSubCommand("clear", clear_command_object);
252023f59509SGreg Clayton   LoadSubCommand("delete", delete_command_object);
252123f59509SGreg Clayton   LoadSubCommand("set", set_command_object);
252223f59509SGreg Clayton   LoadSubCommand("command", command_command_object);
252323f59509SGreg Clayton   LoadSubCommand("modify", modify_command_object);
25245e09c8c3SJim Ingham   LoadSubCommand("name", name_command_object);
2525e14dc268SJim Ingham   LoadSubCommand("write", write_command_object);
2526e14dc268SJim Ingham   LoadSubCommand("read", read_command_object);
252730fdc8d8SChris Lattner }
252830fdc8d8SChris Lattner 
25299e85e5a8SEugene Zelenko CommandObjectMultiwordBreakpoint::~CommandObjectMultiwordBreakpoint() = default;
253030fdc8d8SChris Lattner 
2531b9c1b51eSKate Stone void CommandObjectMultiwordBreakpoint::VerifyIDs(Args &args, Target *target,
25325e09c8c3SJim Ingham                                                  bool allow_locations,
25335e09c8c3SJim Ingham                                                  CommandReturnObject &result,
2534b842f2ecSJim Ingham                                                  BreakpointIDList *valid_ids,
2535b842f2ecSJim Ingham                                                  BreakpointName::Permissions
2536b842f2ecSJim Ingham                                                      ::PermissionKinds
2537b842f2ecSJim Ingham                                                      purpose) {
253830fdc8d8SChris Lattner   // args can be strings representing 1). integers (for breakpoint ids)
2539b9c1b51eSKate Stone   //                                  2). the full breakpoint & location
2540b9c1b51eSKate Stone   //                                  canonical representation
2541b9c1b51eSKate Stone   //                                  3). the word "to" or a hyphen,
2542b9c1b51eSKate Stone   //                                  representing a range (in which case there
2543b9c1b51eSKate Stone   //                                      had *better* be an entry both before &
2544b9c1b51eSKate Stone   //                                      after of one of the first two types.
25455e09c8c3SJim Ingham   //                                  4). A breakpoint name
2546b9c1b51eSKate Stone   // If args is empty, we will use the last created breakpoint (if there is
2547b9c1b51eSKate Stone   // one.)
254830fdc8d8SChris Lattner 
254930fdc8d8SChris Lattner   Args temp_args;
255030fdc8d8SChris Lattner 
255111eb9c64SZachary Turner   if (args.empty()) {
2552b9c1b51eSKate Stone     if (target->GetLastCreatedBreakpoint()) {
2553b9c1b51eSKate Stone       valid_ids->AddBreakpointID(BreakpointID(
2554b9c1b51eSKate Stone           target->GetLastCreatedBreakpoint()->GetID(), LLDB_INVALID_BREAK_ID));
255536f3b369SJim Ingham       result.SetStatus(eReturnStatusSuccessFinishNoResult);
2556b9c1b51eSKate Stone     } else {
2557b9c1b51eSKate Stone       result.AppendError(
2558b9c1b51eSKate Stone           "No breakpoint specified and no last created breakpoint.");
255936f3b369SJim Ingham       result.SetStatus(eReturnStatusFailed);
256036f3b369SJim Ingham     }
256136f3b369SJim Ingham     return;
256236f3b369SJim Ingham   }
256336f3b369SJim Ingham 
2564b9c1b51eSKate Stone   // Create a new Args variable to use; copy any non-breakpoint-id-ranges stuff
256505097246SAdrian Prantl   // directly from the old ARGS to the new TEMP_ARGS.  Do not copy breakpoint
256605097246SAdrian Prantl   // id range strings over; instead generate a list of strings for all the
256705097246SAdrian Prantl   // breakpoint ids in the range, and shove all of those breakpoint id strings
256805097246SAdrian Prantl   // into TEMP_ARGS.
256930fdc8d8SChris Lattner 
2570b9c1b51eSKate Stone   BreakpointIDList::FindAndReplaceIDRanges(args, target, allow_locations,
2571b842f2ecSJim Ingham                                            purpose, result, temp_args);
257230fdc8d8SChris Lattner 
2573b9c1b51eSKate Stone   // NOW, convert the list of breakpoint id strings in TEMP_ARGS into an actual
2574b9c1b51eSKate Stone   // BreakpointIDList:
257530fdc8d8SChris Lattner 
2576b9c1b51eSKate Stone   valid_ids->InsertStringArray(temp_args.GetConstArgumentVector(),
2577b9c1b51eSKate Stone                                temp_args.GetArgumentCount(), result);
257830fdc8d8SChris Lattner 
257905097246SAdrian Prantl   // At this point,  all of the breakpoint ids that the user passed in have
258005097246SAdrian Prantl   // been converted to breakpoint IDs and put into valid_ids.
258130fdc8d8SChris Lattner 
2582b9c1b51eSKate Stone   if (result.Succeeded()) {
2583b9c1b51eSKate Stone     // Now that we've converted everything from args into a list of breakpoint
258405097246SAdrian Prantl     // ids, go through our tentative list of breakpoint id's and verify that
258505097246SAdrian Prantl     // they correspond to valid/currently set breakpoints.
258630fdc8d8SChris Lattner 
2587c982c768SGreg Clayton     const size_t count = valid_ids->GetSize();
2588b9c1b51eSKate Stone     for (size_t i = 0; i < count; ++i) {
258930fdc8d8SChris Lattner       BreakpointID cur_bp_id = valid_ids->GetBreakpointIDAtIndex(i);
2590b9c1b51eSKate Stone       Breakpoint *breakpoint =
2591b9c1b51eSKate Stone           target->GetBreakpointByID(cur_bp_id.GetBreakpointID()).get();
2592b9c1b51eSKate Stone       if (breakpoint != nullptr) {
2593c7bece56SGreg Clayton         const size_t num_locations = breakpoint->GetNumLocations();
2594b9c1b51eSKate Stone         if (static_cast<size_t>(cur_bp_id.GetLocationID()) > num_locations) {
259530fdc8d8SChris Lattner           StreamString id_str;
2596b9c1b51eSKate Stone           BreakpointID::GetCanonicalReference(
2597b9c1b51eSKate Stone               &id_str, cur_bp_id.GetBreakpointID(), cur_bp_id.GetLocationID());
2598c982c768SGreg Clayton           i = valid_ids->GetSize() + 1;
2599b9c1b51eSKate Stone           result.AppendErrorWithFormat(
2600b9c1b51eSKate Stone               "'%s' is not a currently valid breakpoint/location id.\n",
260130fdc8d8SChris Lattner               id_str.GetData());
260230fdc8d8SChris Lattner           result.SetStatus(eReturnStatusFailed);
260330fdc8d8SChris Lattner         }
2604b9c1b51eSKate Stone       } else {
2605c982c768SGreg Clayton         i = valid_ids->GetSize() + 1;
2606b9c1b51eSKate Stone         result.AppendErrorWithFormat(
2607b9c1b51eSKate Stone             "'%d' is not a currently valid breakpoint ID.\n",
26087428a18cSKate Stone             cur_bp_id.GetBreakpointID());
260930fdc8d8SChris Lattner         result.SetStatus(eReturnStatusFailed);
261030fdc8d8SChris Lattner       }
261130fdc8d8SChris Lattner     }
261230fdc8d8SChris Lattner   }
261330fdc8d8SChris Lattner }
2614