CommandObjectBreakpoint.cpp revision 254721
1217309Snwhitehorn//===-- CommandObjectBreakpoint.cpp -----------------------------*- C++ -*-===//
2251843Sbapt//
3217309Snwhitehorn//                     The LLVM Compiler Infrastructure
4217309Snwhitehorn//
5217309Snwhitehorn// This file is distributed under the University of Illinois Open Source
6251843Sbapt// License. See LICENSE.TXT for details.
7217309Snwhitehorn//
8217309Snwhitehorn//===----------------------------------------------------------------------===//
9217309Snwhitehorn
10217309Snwhitehorn#include "lldb/lldb-python.h"
11217309Snwhitehorn
12217309Snwhitehorn#include "CommandObjectBreakpoint.h"
13217309Snwhitehorn#include "CommandObjectBreakpointCommand.h"
14217309Snwhitehorn
15217309Snwhitehorn// C Includes
16217309Snwhitehorn// C++ Includes
17217309Snwhitehorn// Other libraries and framework includes
18217309Snwhitehorn// Project includes
19217309Snwhitehorn#include "lldb/Breakpoint/Breakpoint.h"
20217309Snwhitehorn#include "lldb/Breakpoint/BreakpointIDList.h"
21217309Snwhitehorn#include "lldb/Breakpoint/BreakpointLocation.h"
22217309Snwhitehorn#include "lldb/Interpreter/Options.h"
23217309Snwhitehorn#include "lldb/Core/RegularExpression.h"
24217309Snwhitehorn#include "lldb/Core/StreamString.h"
25217309Snwhitehorn#include "lldb/Interpreter/CommandInterpreter.h"
26217309Snwhitehorn#include "lldb/Interpreter/CommandReturnObject.h"
27217309Snwhitehorn#include "lldb/Target/Target.h"
28217309Snwhitehorn#include "lldb/Interpreter/CommandCompletions.h"
29217309Snwhitehorn#include "lldb/Target/StackFrame.h"
30217309Snwhitehorn#include "lldb/Target/Thread.h"
31217309Snwhitehorn#include "lldb/Target/ThreadSpec.h"
32217309Snwhitehorn
33217309Snwhitehorn#include <vector>
34217309Snwhitehorn
35217309Snwhitehornusing namespace lldb;
36217309Snwhitehornusing namespace lldb_private;
37217309Snwhitehorn
38217309Snwhitehornstatic void
39217309SnwhitehornAddBreakpointDescription (Stream *s, Breakpoint *bp, lldb::DescriptionLevel level)
40217309Snwhitehorn{
41217309Snwhitehorn    s->IndentMore();
42217309Snwhitehorn    bp->GetDescription (s, level, true);
43217309Snwhitehorn    s->IndentLess();
44217309Snwhitehorn    s->EOL();
45217309Snwhitehorn}
46217309Snwhitehorn
47217309Snwhitehorn//-------------------------------------------------------------------------
48217309Snwhitehorn// CommandObjectBreakpointSet
49217309Snwhitehorn//-------------------------------------------------------------------------
50217309Snwhitehorn
51217309Snwhitehorn
52217309Snwhitehornclass CommandObjectBreakpointSet : public CommandObjectParsed
53217309Snwhitehorn{
54217309Snwhitehornpublic:
55217309Snwhitehorn
56217309Snwhitehorn    typedef enum BreakpointSetType
57217309Snwhitehorn    {
58217309Snwhitehorn        eSetTypeInvalid,
59217309Snwhitehorn        eSetTypeFileAndLine,
60217309Snwhitehorn        eSetTypeAddress,
61217309Snwhitehorn        eSetTypeFunctionName,
62217309Snwhitehorn        eSetTypeFunctionRegexp,
63217309Snwhitehorn        eSetTypeSourceRegexp,
64217309Snwhitehorn        eSetTypeException
65217309Snwhitehorn    } BreakpointSetType;
66217309Snwhitehorn
67217309Snwhitehorn    CommandObjectBreakpointSet (CommandInterpreter &interpreter) :
68217309Snwhitehorn        CommandObjectParsed (interpreter,
69217309Snwhitehorn                             "breakpoint set",
70217309Snwhitehorn                             "Sets a breakpoint or set of breakpoints in the executable.",
71217309Snwhitehorn                             "breakpoint set <cmd-options>"),
72217309Snwhitehorn        m_options (interpreter)
73217309Snwhitehorn    {
74217309Snwhitehorn    }
75217309Snwhitehorn
76217309Snwhitehorn
77217309Snwhitehorn    virtual
78217309Snwhitehorn    ~CommandObjectBreakpointSet () {}
79217309Snwhitehorn
80217309Snwhitehorn    virtual Options *
81217309Snwhitehorn    GetOptions ()
82217309Snwhitehorn    {
83217309Snwhitehorn        return &m_options;
84217309Snwhitehorn    }
85217309Snwhitehorn
86217309Snwhitehorn    class CommandOptions : public Options
87217309Snwhitehorn    {
88217309Snwhitehorn    public:
89217309Snwhitehorn
90217309Snwhitehorn        CommandOptions (CommandInterpreter &interpreter) :
91217309Snwhitehorn            Options (interpreter),
92217309Snwhitehorn            m_condition (),
93217309Snwhitehorn            m_filenames (),
94217309Snwhitehorn            m_line_num (0),
95217309Snwhitehorn            m_column (0),
96217309Snwhitehorn            m_func_names (),
97217309Snwhitehorn            m_func_name_type_mask (eFunctionNameTypeNone),
98217309Snwhitehorn            m_func_regexp (),
99217309Snwhitehorn            m_source_text_regexp(),
100217309Snwhitehorn            m_modules (),
101217309Snwhitehorn            m_load_addr(),
102217309Snwhitehorn            m_ignore_count (0),
103217309Snwhitehorn            m_thread_id(LLDB_INVALID_THREAD_ID),
104217309Snwhitehorn            m_thread_index (UINT32_MAX),
105217309Snwhitehorn            m_thread_name(),
106217309Snwhitehorn            m_queue_name(),
107217309Snwhitehorn            m_catch_bp (false),
108217309Snwhitehorn            m_throw_bp (true),
109217309Snwhitehorn            m_language (eLanguageTypeUnknown),
110217309Snwhitehorn            m_skip_prologue (eLazyBoolCalculate),
111217309Snwhitehorn            m_one_shot (false)
112217309Snwhitehorn        {
113217309Snwhitehorn        }
114217309Snwhitehorn
115217309Snwhitehorn
116217309Snwhitehorn        virtual
117217309Snwhitehorn        ~CommandOptions () {}
118217309Snwhitehorn
119217309Snwhitehorn        virtual Error
120217309Snwhitehorn        SetOptionValue (uint32_t option_idx, const char *option_arg)
121217309Snwhitehorn        {
122217309Snwhitehorn            Error error;
123217309Snwhitehorn            const int short_option = m_getopt_table[option_idx].val;
124217309Snwhitehorn
125217309Snwhitehorn            switch (short_option)
126217309Snwhitehorn            {
127217309Snwhitehorn                case 'a':
128217309Snwhitehorn                    {
129217309Snwhitehorn                        ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
130217309Snwhitehorn                        m_load_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
131217309Snwhitehorn                    }
132217309Snwhitehorn                    break;
133217309Snwhitehorn
134217309Snwhitehorn                case 'b':
135217309Snwhitehorn                    m_func_names.push_back (option_arg);
136217309Snwhitehorn                    m_func_name_type_mask |= eFunctionNameTypeBase;
137217309Snwhitehorn                    break;
138217309Snwhitehorn
139217309Snwhitehorn                case 'C':
140217309Snwhitehorn                    m_column = Args::StringToUInt32 (option_arg, 0);
141217309Snwhitehorn                    break;
142217309Snwhitehorn
143217309Snwhitehorn                case 'c':
144217309Snwhitehorn                    m_condition.assign(option_arg);
145217309Snwhitehorn                    break;
146217309Snwhitehorn
147217309Snwhitehorn                case 'E':
148217309Snwhitehorn                {
149217309Snwhitehorn                    LanguageType language = LanguageRuntime::GetLanguageTypeFromString (option_arg);
150217309Snwhitehorn
151217309Snwhitehorn                    switch (language)
152217309Snwhitehorn                    {
153217309Snwhitehorn                        case eLanguageTypeC89:
154217309Snwhitehorn                        case eLanguageTypeC:
155217309Snwhitehorn                        case eLanguageTypeC99:
156217309Snwhitehorn                            m_language = eLanguageTypeC;
157217309Snwhitehorn                            break;
158217309Snwhitehorn                        case eLanguageTypeC_plus_plus:
159217309Snwhitehorn                            m_language = eLanguageTypeC_plus_plus;
160217309Snwhitehorn                            break;
161217309Snwhitehorn                        case eLanguageTypeObjC:
162217309Snwhitehorn                            m_language = eLanguageTypeObjC;
163217309Snwhitehorn                            break;
164217309Snwhitehorn                        case eLanguageTypeObjC_plus_plus:
165217309Snwhitehorn                            error.SetErrorStringWithFormat ("Set exception breakpoints separately for c++ and objective-c");
166217309Snwhitehorn                            break;
167217309Snwhitehorn                        case eLanguageTypeUnknown:
168217309Snwhitehorn                            error.SetErrorStringWithFormat ("Unknown language type: '%s' for exception breakpoint", option_arg);
169217309Snwhitehorn                            break;
170217309Snwhitehorn                        default:
171217309Snwhitehorn                            error.SetErrorStringWithFormat ("Unsupported language type: '%s' for exception breakpoint", option_arg);
172217309Snwhitehorn                    }
173217309Snwhitehorn                }
174217309Snwhitehorn                break;
175217309Snwhitehorn
176217309Snwhitehorn                case 'f':
177217309Snwhitehorn                    m_filenames.AppendIfUnique (FileSpec(option_arg, false));
178217309Snwhitehorn                    break;
179217309Snwhitehorn
180217309Snwhitehorn                case 'F':
181217309Snwhitehorn                    m_func_names.push_back (option_arg);
182217309Snwhitehorn                    m_func_name_type_mask |= eFunctionNameTypeFull;
183217309Snwhitehorn                    break;
184217309Snwhitehorn
185217309Snwhitehorn                case 'h':
186217309Snwhitehorn                {
187217309Snwhitehorn                    bool success;
188217309Snwhitehorn                    m_catch_bp = Args::StringToBoolean (option_arg, true, &success);
189217309Snwhitehorn                    if (!success)
190217309Snwhitehorn                        error.SetErrorStringWithFormat ("Invalid boolean value for on-catch option: '%s'", option_arg);
191217309Snwhitehorn                }
192217309Snwhitehorn                break;
193217309Snwhitehorn                case 'i':
194217309Snwhitehorn                {
195217309Snwhitehorn                    m_ignore_count = Args::StringToUInt32(option_arg, UINT32_MAX, 0);
196217309Snwhitehorn                    if (m_ignore_count == UINT32_MAX)
197217309Snwhitehorn                       error.SetErrorStringWithFormat ("invalid ignore count '%s'", option_arg);
198217309Snwhitehorn                    break;
199217309Snwhitehorn                }
200217309Snwhitehorn
201217309Snwhitehorn                case 'K':
202217309Snwhitehorn                {
203217309Snwhitehorn                    bool success;
204217309Snwhitehorn                    bool value;
205217309Snwhitehorn                    value = Args::StringToBoolean (option_arg, true, &success);
206217309Snwhitehorn                    if (value)
207217309Snwhitehorn                        m_skip_prologue = eLazyBoolYes;
208217309Snwhitehorn                    else
209217309Snwhitehorn                        m_skip_prologue = eLazyBoolNo;
210217309Snwhitehorn
211217309Snwhitehorn                    if (!success)
212217309Snwhitehorn                        error.SetErrorStringWithFormat ("Invalid boolean value for skip prologue option: '%s'", option_arg);
213217309Snwhitehorn                }
214217309Snwhitehorn                break;
215217309Snwhitehorn
216217309Snwhitehorn                case 'l':
217217309Snwhitehorn                    m_line_num = Args::StringToUInt32 (option_arg, 0);
218217309Snwhitehorn                    break;
219217309Snwhitehorn
220217309Snwhitehorn                case 'M':
221217309Snwhitehorn                    m_func_names.push_back (option_arg);
222217309Snwhitehorn                    m_func_name_type_mask |= eFunctionNameTypeMethod;
223217309Snwhitehorn                    break;
224217309Snwhitehorn
225217309Snwhitehorn                case 'n':
226217309Snwhitehorn                    m_func_names.push_back (option_arg);
227217309Snwhitehorn                    m_func_name_type_mask |= eFunctionNameTypeAuto;
228251843Sbapt                    break;
229251843Sbapt
230251843Sbapt                case 'o':
231251843Sbapt                    m_one_shot = true;
232251843Sbapt                    break;
233251843Sbapt
234217309Snwhitehorn                case 'p':
235251843Sbapt                    m_source_text_regexp.assign (option_arg);
236217309Snwhitehorn                    break;
237217309Snwhitehorn
238217309Snwhitehorn                case 'q':
239217309Snwhitehorn                    m_queue_name.assign (option_arg);
240217309Snwhitehorn                    break;
241217309Snwhitehorn
242217309Snwhitehorn                case 'r':
243217309Snwhitehorn                    m_func_regexp.assign (option_arg);
244217309Snwhitehorn                    break;
245217309Snwhitehorn
246217309Snwhitehorn                case 's':
247217309Snwhitehorn                {
248217309Snwhitehorn                    m_modules.AppendIfUnique (FileSpec (option_arg, false));
249217309Snwhitehorn                    break;
250217309Snwhitehorn                }
251251843Sbapt
252217309Snwhitehorn                case 'S':
253217309Snwhitehorn                    m_func_names.push_back (option_arg);
254217309Snwhitehorn                    m_func_name_type_mask |= eFunctionNameTypeSelector;
255217309Snwhitehorn                    break;
256217309Snwhitehorn
257217309Snwhitehorn                case 't' :
258217309Snwhitehorn                {
259217309Snwhitehorn                    m_thread_id = Args::StringToUInt64(option_arg, LLDB_INVALID_THREAD_ID, 0);
260217309Snwhitehorn                    if (m_thread_id == LLDB_INVALID_THREAD_ID)
261217309Snwhitehorn                       error.SetErrorStringWithFormat ("invalid thread id string '%s'", option_arg);
262251843Sbapt                }
263217309Snwhitehorn                break;
264251843Sbapt
265217309Snwhitehorn                case 'T':
266217309Snwhitehorn                    m_thread_name.assign (option_arg);
267217309Snwhitehorn                    break;
268217309Snwhitehorn
269217309Snwhitehorn                case 'w':
270217309Snwhitehorn                {
271217309Snwhitehorn                    bool success;
272217309Snwhitehorn                    m_throw_bp = Args::StringToBoolean (option_arg, true, &success);
273217309Snwhitehorn                    if (!success)
274217309Snwhitehorn                        error.SetErrorStringWithFormat ("Invalid boolean value for on-throw option: '%s'", option_arg);
275217309Snwhitehorn                }
276217309Snwhitehorn                break;
277217309Snwhitehorn
278217309Snwhitehorn                case 'x':
279217309Snwhitehorn                {
280217309Snwhitehorn                    m_thread_index = Args::StringToUInt32(option_arg, UINT32_MAX, 0);
281217309Snwhitehorn                    if (m_thread_id == UINT32_MAX)
282217309Snwhitehorn                       error.SetErrorStringWithFormat ("invalid thread index string '%s'", option_arg);
283217309Snwhitehorn
284217309Snwhitehorn                }
285217309Snwhitehorn                break;
286217309Snwhitehorn
287217309Snwhitehorn                default:
288217309Snwhitehorn                    error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
289217309Snwhitehorn                    break;
290217309Snwhitehorn            }
291217309Snwhitehorn
292217309Snwhitehorn            return error;
293217309Snwhitehorn        }
294217309Snwhitehorn        void
295217309Snwhitehorn        OptionParsingStarting ()
296217309Snwhitehorn        {
297217309Snwhitehorn            m_condition.clear();
298217309Snwhitehorn            m_filenames.Clear();
299251843Sbapt            m_line_num = 0;
300217309Snwhitehorn            m_column = 0;
301251843Sbapt            m_func_names.clear();
302251843Sbapt            m_func_name_type_mask = eFunctionNameTypeNone;
303251843Sbapt            m_func_regexp.clear();
304251843Sbapt            m_source_text_regexp.clear();
305251843Sbapt            m_modules.Clear();
306251843Sbapt            m_load_addr = LLDB_INVALID_ADDRESS;
307251843Sbapt            m_ignore_count = 0;
308217309Snwhitehorn            m_thread_id = LLDB_INVALID_THREAD_ID;
309217309Snwhitehorn            m_thread_index = UINT32_MAX;
310217309Snwhitehorn            m_thread_name.clear();
311217309Snwhitehorn            m_queue_name.clear();
312217309Snwhitehorn            m_catch_bp = false;
313217309Snwhitehorn            m_throw_bp = true;
314217309Snwhitehorn            m_language = eLanguageTypeUnknown;
315217309Snwhitehorn            m_skip_prologue = eLazyBoolCalculate;
316217309Snwhitehorn            m_one_shot = false;
317217309Snwhitehorn        }
318217309Snwhitehorn
319217309Snwhitehorn        const OptionDefinition*
320217309Snwhitehorn        GetDefinitions ()
321251843Sbapt        {
322217309Snwhitehorn            return g_option_table;
323251843Sbapt        }
324251843Sbapt
325251843Sbapt        // Options table: Required for subclasses of Options.
326251843Sbapt
327251843Sbapt        static OptionDefinition g_option_table[];
328251843Sbapt
329251843Sbapt        // Instance variables to hold the values for command options.
330217309Snwhitehorn
331217309Snwhitehorn        std::string m_condition;
332217309Snwhitehorn        FileSpecList m_filenames;
333217309Snwhitehorn        uint32_t m_line_num;
334217309Snwhitehorn        uint32_t m_column;
335217309Snwhitehorn        std::vector<std::string> m_func_names;
336217309Snwhitehorn        uint32_t m_func_name_type_mask;
337217309Snwhitehorn        std::string m_func_regexp;
338217309Snwhitehorn        std::string m_source_text_regexp;
339217309Snwhitehorn        FileSpecList m_modules;
340217309Snwhitehorn        lldb::addr_t m_load_addr;
341217309Snwhitehorn        uint32_t m_ignore_count;
342217309Snwhitehorn        lldb::tid_t m_thread_id;
343217309Snwhitehorn        uint32_t m_thread_index;
344217309Snwhitehorn        std::string m_thread_name;
345217309Snwhitehorn        std::string m_queue_name;
346217309Snwhitehorn        bool m_catch_bp;
347217309Snwhitehorn        bool m_throw_bp;
348217309Snwhitehorn        lldb::LanguageType m_language;
349217309Snwhitehorn        LazyBool m_skip_prologue;
350217309Snwhitehorn        bool m_one_shot;
351217309Snwhitehorn
352217309Snwhitehorn    };
353217309Snwhitehorn
354217309Snwhitehornprotected:
355217309Snwhitehorn    virtual bool
356217309Snwhitehorn    DoExecute (Args& command,
357217309Snwhitehorn             CommandReturnObject &result)
358217309Snwhitehorn    {
359217309Snwhitehorn        Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
360217309Snwhitehorn        if (target == NULL)
361217309Snwhitehorn        {
362217309Snwhitehorn            result.AppendError ("Invalid target.  Must set target before setting breakpoints (see 'target create' command).");
363217309Snwhitehorn            result.SetStatus (eReturnStatusFailed);
364217309Snwhitehorn            return false;
365217309Snwhitehorn        }
366217309Snwhitehorn
367217309Snwhitehorn        // The following are the various types of breakpoints that could be set:
368217309Snwhitehorn        //   1).  -f -l -p  [-s -g]   (setting breakpoint by source location)
369217309Snwhitehorn        //   2).  -a  [-s -g]         (setting breakpoint by address)
370217309Snwhitehorn        //   3).  -n  [-s -g]         (setting breakpoint by function name)
371217309Snwhitehorn        //   4).  -r  [-s -g]         (setting breakpoint by function name regular expression)
372217309Snwhitehorn        //   5).  -p -f               (setting a breakpoint by comparing a reg-exp to source text)
373217309Snwhitehorn        //   6).  -E [-w -h]          (setting a breakpoint for exceptions for a given language.)
374217309Snwhitehorn
375217309Snwhitehorn        BreakpointSetType break_type = eSetTypeInvalid;
376217309Snwhitehorn
377217309Snwhitehorn        if (m_options.m_line_num != 0)
378217309Snwhitehorn            break_type = eSetTypeFileAndLine;
379217309Snwhitehorn        else if (m_options.m_load_addr != LLDB_INVALID_ADDRESS)
380217309Snwhitehorn            break_type = eSetTypeAddress;
381217309Snwhitehorn        else if (!m_options.m_func_names.empty())
382217309Snwhitehorn            break_type = eSetTypeFunctionName;
383217309Snwhitehorn        else if  (!m_options.m_func_regexp.empty())
384217309Snwhitehorn            break_type = eSetTypeFunctionRegexp;
385217309Snwhitehorn        else if (!m_options.m_source_text_regexp.empty())
386217309Snwhitehorn            break_type = eSetTypeSourceRegexp;
387217309Snwhitehorn        else if (m_options.m_language != eLanguageTypeUnknown)
388217309Snwhitehorn            break_type = eSetTypeException;
389217309Snwhitehorn
390217309Snwhitehorn        Breakpoint *bp = NULL;
391217309Snwhitehorn        FileSpec module_spec;
392217309Snwhitehorn        const bool internal = false;
393217309Snwhitehorn
394217309Snwhitehorn        switch (break_type)
395217309Snwhitehorn        {
396217309Snwhitehorn            case eSetTypeFileAndLine: // Breakpoint by source position
397224014Snwhitehorn                {
398217309Snwhitehorn                    FileSpec file;
399217309Snwhitehorn                    const size_t num_files = m_options.m_filenames.GetSize();
400217309Snwhitehorn                    if (num_files == 0)
401217309Snwhitehorn                    {
402217309Snwhitehorn                        if (!GetDefaultFile (target, file, result))
403217309Snwhitehorn                        {
404217309Snwhitehorn                            result.AppendError("No file supplied and no default file available.");
405217309Snwhitehorn                            result.SetStatus (eReturnStatusFailed);
406217309Snwhitehorn                            return false;
407217309Snwhitehorn                        }
408217309Snwhitehorn                    }
409217309Snwhitehorn                    else if (num_files > 1)
410217309Snwhitehorn                    {
411217309Snwhitehorn                        result.AppendError("Only one file at a time is allowed for file and line breakpoints.");
412217309Snwhitehorn                        result.SetStatus (eReturnStatusFailed);
413217309Snwhitehorn                        return false;
414217309Snwhitehorn                    }
415217309Snwhitehorn                    else
416217309Snwhitehorn                        file = m_options.m_filenames.GetFileSpecAtIndex(0);
417217309Snwhitehorn
418217309Snwhitehorn                    // Only check for inline functions if
419217309Snwhitehorn                    LazyBool check_inlines = eLazyBoolCalculate;
420217309Snwhitehorn
421217309Snwhitehorn                    bp = target->CreateBreakpoint (&(m_options.m_modules),
422217309Snwhitehorn                                                   file,
423217309Snwhitehorn                                                   m_options.m_line_num,
424217309Snwhitehorn                                                   check_inlines,
425217309Snwhitehorn                                                   m_options.m_skip_prologue,
426217309Snwhitehorn                                                   internal).get();
427217309Snwhitehorn                }
428217309Snwhitehorn                break;
429217309Snwhitehorn
430217309Snwhitehorn            case eSetTypeAddress: // Breakpoint by address
431217309Snwhitehorn                bp = target->CreateBreakpoint (m_options.m_load_addr, false).get();
432217309Snwhitehorn                break;
433217309Snwhitehorn
434217309Snwhitehorn            case eSetTypeFunctionName: // Breakpoint by function name
435217309Snwhitehorn                {
436217309Snwhitehorn                    uint32_t name_type_mask = m_options.m_func_name_type_mask;
437217309Snwhitehorn
438217309Snwhitehorn                    if (name_type_mask == 0)
439251843Sbapt                        name_type_mask = eFunctionNameTypeAuto;
440217309Snwhitehorn
441217309Snwhitehorn                    bp = target->CreateBreakpoint (&(m_options.m_modules),
442217309Snwhitehorn                                                   &(m_options.m_filenames),
443217309Snwhitehorn                                                   m_options.m_func_names,
444217309Snwhitehorn                                                   name_type_mask,
445217309Snwhitehorn                                                   m_options.m_skip_prologue,
446217309Snwhitehorn                                                   internal).get();
447217309Snwhitehorn                }
448217309Snwhitehorn                break;
449217309Snwhitehorn
450217309Snwhitehorn            case eSetTypeFunctionRegexp: // Breakpoint by regular expression function name
451217309Snwhitehorn                {
452217309Snwhitehorn                    RegularExpression regexp(m_options.m_func_regexp.c_str());
453217309Snwhitehorn                    if (!regexp.IsValid())
454217309Snwhitehorn                    {
455217309Snwhitehorn                        char err_str[1024];
456217309Snwhitehorn                        regexp.GetErrorAsCString(err_str, sizeof(err_str));
457217309Snwhitehorn                        result.AppendErrorWithFormat("Function name regular expression could not be compiled: \"%s\"",
458217309Snwhitehorn                                                     err_str);
459217309Snwhitehorn                        result.SetStatus (eReturnStatusFailed);
460217309Snwhitehorn                        return false;
461217309Snwhitehorn                    }
462217309Snwhitehorn
463217309Snwhitehorn                    bp = target->CreateFuncRegexBreakpoint (&(m_options.m_modules),
464217309Snwhitehorn                                                            &(m_options.m_filenames),
465217309Snwhitehorn                                                            regexp,
466217309Snwhitehorn                                                            m_options.m_skip_prologue,
467217309Snwhitehorn                                                            internal).get();
468217309Snwhitehorn                }
469217309Snwhitehorn                break;
470217309Snwhitehorn            case eSetTypeSourceRegexp: // Breakpoint by regexp on source text.
471217309Snwhitehorn                {
472217309Snwhitehorn                    const size_t num_files = m_options.m_filenames.GetSize();
473217309Snwhitehorn
474217309Snwhitehorn                    if (num_files == 0)
475217309Snwhitehorn                    {
476217309Snwhitehorn                        FileSpec file;
477217309Snwhitehorn                        if (!GetDefaultFile (target, file, result))
478217309Snwhitehorn                        {
479217309Snwhitehorn                            result.AppendError ("No files provided and could not find default file.");
480217309Snwhitehorn                            result.SetStatus (eReturnStatusFailed);
481217309Snwhitehorn                            return false;
482217309Snwhitehorn                        }
483217309Snwhitehorn                        else
484217309Snwhitehorn                        {
485217309Snwhitehorn                            m_options.m_filenames.Append (file);
486217309Snwhitehorn                        }
487217309Snwhitehorn                    }
488217309Snwhitehorn
489217309Snwhitehorn                    RegularExpression regexp(m_options.m_source_text_regexp.c_str());
490217309Snwhitehorn                    if (!regexp.IsValid())
491217309Snwhitehorn                    {
492217309Snwhitehorn                        char err_str[1024];
493217309Snwhitehorn                        regexp.GetErrorAsCString(err_str, sizeof(err_str));
494217309Snwhitehorn                        result.AppendErrorWithFormat("Source text regular expression could not be compiled: \"%s\"",
495217309Snwhitehorn                                                     err_str);
496217309Snwhitehorn                        result.SetStatus (eReturnStatusFailed);
497217309Snwhitehorn                        return false;
498217309Snwhitehorn                    }
499217309Snwhitehorn                    bp = target->CreateSourceRegexBreakpoint (&(m_options.m_modules), &(m_options.m_filenames), regexp).get();
500251843Sbapt                }
501251843Sbapt                break;
502217309Snwhitehorn            case eSetTypeException:
503217309Snwhitehorn                {
504251843Sbapt                    bp = target->CreateExceptionBreakpoint (m_options.m_language, m_options.m_catch_bp, m_options.m_throw_bp).get();
505217309Snwhitehorn                }
506217309Snwhitehorn                break;
507217309Snwhitehorn            default:
508217309Snwhitehorn                break;
509217309Snwhitehorn        }
510217309Snwhitehorn
511217309Snwhitehorn        // Now set the various options that were passed in:
512217309Snwhitehorn        if (bp)
513217309Snwhitehorn        {
514217309Snwhitehorn            if (m_options.m_thread_id != LLDB_INVALID_THREAD_ID)
515217309Snwhitehorn                bp->SetThreadID (m_options.m_thread_id);
516217309Snwhitehorn
517217309Snwhitehorn            if (m_options.m_thread_index != UINT32_MAX)
518217309Snwhitehorn                bp->GetOptions()->GetThreadSpec()->SetIndex(m_options.m_thread_index);
519217309Snwhitehorn
520217309Snwhitehorn            if (!m_options.m_thread_name.empty())
521217309Snwhitehorn                bp->GetOptions()->GetThreadSpec()->SetName(m_options.m_thread_name.c_str());
522217309Snwhitehorn
523217309Snwhitehorn            if (!m_options.m_queue_name.empty())
524217309Snwhitehorn                bp->GetOptions()->GetThreadSpec()->SetQueueName(m_options.m_queue_name.c_str());
525217309Snwhitehorn
526217309Snwhitehorn            if (m_options.m_ignore_count != 0)
527217309Snwhitehorn                bp->GetOptions()->SetIgnoreCount(m_options.m_ignore_count);
528217309Snwhitehorn
529217309Snwhitehorn            if (!m_options.m_condition.empty())
530217309Snwhitehorn                bp->GetOptions()->SetCondition(m_options.m_condition.c_str());
531217309Snwhitehorn
532217309Snwhitehorn            bp->SetOneShot (m_options.m_one_shot);
533217309Snwhitehorn        }
534217309Snwhitehorn
535217309Snwhitehorn        if (bp)
536217309Snwhitehorn        {
537217309Snwhitehorn            Stream &output_stream = result.GetOutputStream();
538217309Snwhitehorn            const bool show_locations = false;
539217309Snwhitehorn            bp->GetDescription(&output_stream, lldb::eDescriptionLevelInitial, show_locations);
540217309Snwhitehorn            // Don't print out this warning for exception breakpoints.  They can get set before the target
541217309Snwhitehorn            // is set, but we won't know how to actually set the breakpoint till we run.
542217309Snwhitehorn            if (bp->GetNumLocations() == 0 && break_type != eSetTypeException)
543217309Snwhitehorn                output_stream.Printf ("WARNING:  Unable to resolve breakpoint to any actual locations.\n");
544217309Snwhitehorn            result.SetStatus (eReturnStatusSuccessFinishResult);
545217309Snwhitehorn        }
546217309Snwhitehorn        else if (!bp)
547217309Snwhitehorn        {
548251843Sbapt            result.AppendError ("Breakpoint creation failed: No breakpoint created.");
549217309Snwhitehorn            result.SetStatus (eReturnStatusFailed);
550217309Snwhitehorn        }
551217309Snwhitehorn
552217309Snwhitehorn        return result.Succeeded();
553217309Snwhitehorn    }
554217309Snwhitehorn
555217309Snwhitehornprivate:
556217309Snwhitehorn    bool
557217309Snwhitehorn    GetDefaultFile (Target *target, FileSpec &file, CommandReturnObject &result)
558217309Snwhitehorn    {
559217309Snwhitehorn        uint32_t default_line;
560217309Snwhitehorn        // First use the Source Manager's default file.
561217309Snwhitehorn        // Then use the current stack frame's file.
562217309Snwhitehorn        if (!target->GetSourceManager().GetDefaultFileAndLine(file, default_line))
563217309Snwhitehorn        {
564217309Snwhitehorn            StackFrame *cur_frame = m_exe_ctx.GetFramePtr();
565217309Snwhitehorn            if (cur_frame == NULL)
566217309Snwhitehorn            {
567217309Snwhitehorn                result.AppendError ("No selected frame to use to find the default file.");
568217309Snwhitehorn                result.SetStatus (eReturnStatusFailed);
569217309Snwhitehorn                return false;
570217309Snwhitehorn            }
571217309Snwhitehorn            else if (!cur_frame->HasDebugInformation())
572217309Snwhitehorn            {
573217309Snwhitehorn                result.AppendError ("Cannot use the selected frame to find the default file, it has no debug info.");
574217309Snwhitehorn                result.SetStatus (eReturnStatusFailed);
575217309Snwhitehorn                return false;
576217309Snwhitehorn            }
577217309Snwhitehorn            else
578217309Snwhitehorn            {
579217309Snwhitehorn                const SymbolContext &sc = cur_frame->GetSymbolContext (eSymbolContextLineEntry);
580217309Snwhitehorn                if (sc.line_entry.file)
581217309Snwhitehorn                {
582224014Snwhitehorn                    file = sc.line_entry.file;
583217309Snwhitehorn                }
584217309Snwhitehorn                else
585217309Snwhitehorn                {
586217309Snwhitehorn                    result.AppendError ("Can't find the file for the selected frame to use as the default file.");
587217309Snwhitehorn                    result.SetStatus (eReturnStatusFailed);
588217309Snwhitehorn                    return false;
589217309Snwhitehorn                }
590217309Snwhitehorn            }
591217309Snwhitehorn        }
592217309Snwhitehorn        return true;
593217309Snwhitehorn    }
594217309Snwhitehorn
595217309Snwhitehorn    CommandOptions m_options;
596217309Snwhitehorn};
597217309Snwhitehorn// If an additional option set beyond LLDB_OPTION_SET_10 is added, make sure to
598217309Snwhitehorn// update the numbers passed to LLDB_OPT_SET_FROM_TO(...) appropriately.
599217309Snwhitehorn#define LLDB_OPT_FILE ( LLDB_OPT_SET_FROM_TO(1, 9) & ~LLDB_OPT_SET_2 )
600217309Snwhitehorn#define LLDB_OPT_NOT_10 ( LLDB_OPT_SET_FROM_TO(1, 10) & ~LLDB_OPT_SET_10 )
601217309Snwhitehorn#define LLDB_OPT_SKIP_PROLOGUE ( LLDB_OPT_SET_1 | LLDB_OPT_SET_FROM_TO(3,8) )
602217309Snwhitehorn
603217309SnwhitehornOptionDefinition
604217309SnwhitehornCommandObjectBreakpointSet::CommandOptions::g_option_table[] =
605217309Snwhitehorn{
606217309Snwhitehorn    { LLDB_OPT_NOT_10, false, "shlib", 's', required_argument, NULL, CommandCompletions::eModuleCompletion, eArgTypeShlibName,
607217309Snwhitehorn        "Set the breakpoint only in this shared library.  "
608217309Snwhitehorn        "Can repeat this option multiple times to specify multiple shared libraries."},
609217309Snwhitehorn
610217309Snwhitehorn    { LLDB_OPT_SET_ALL, false, "ignore-count", 'i', required_argument,   NULL, 0, eArgTypeCount,
611217309Snwhitehorn        "Set the number of times this breakpoint is skipped before stopping." },
612217309Snwhitehorn
613217309Snwhitehorn    { LLDB_OPT_SET_ALL, false, "one-shot", 'o', no_argument,   NULL, 0, eArgTypeNone,
614217309Snwhitehorn        "The breakpoint is deleted the first time it causes a stop." },
615217309Snwhitehorn
616217309Snwhitehorn    { LLDB_OPT_SET_ALL, false, "condition",    'c', required_argument, NULL, 0, eArgTypeExpression,
617217309Snwhitehorn        "The breakpoint stops only if this condition expression evaluates to true."},
618217309Snwhitehorn
619217309Snwhitehorn    { LLDB_OPT_SET_ALL, false, "thread-index", 'x', required_argument, NULL, 0, eArgTypeThreadIndex,
620217309Snwhitehorn        "The breakpoint stops only for the thread whose indeX matches this argument."},
621217309Snwhitehorn
622217309Snwhitehorn    { LLDB_OPT_SET_ALL, false, "thread-id", 't', required_argument, NULL, 0, eArgTypeThreadID,
623217309Snwhitehorn        "The breakpoint stops only for the thread whose TID matches this argument."},
624217309Snwhitehorn
625217309Snwhitehorn    { LLDB_OPT_SET_ALL, false, "thread-name", 'T', required_argument, NULL, 0, eArgTypeThreadName,
626217309Snwhitehorn        "The breakpoint stops only for the thread whose thread name matches this argument."},
627217309Snwhitehorn
628217309Snwhitehorn    { LLDB_OPT_SET_ALL, false, "queue-name", 'q', required_argument, NULL, 0, eArgTypeQueueName,
629217309Snwhitehorn        "The breakpoint stops only for threads in the queue whose name is given by this argument."},
630217309Snwhitehorn
631217309Snwhitehorn    { LLDB_OPT_FILE, false, "file", 'f', required_argument, NULL, CommandCompletions::eSourceFileCompletion, eArgTypeFilename,
632217309Snwhitehorn        "Specifies the source file in which to set this breakpoint.  "
633217309Snwhitehorn        "Note, by default lldb only looks for files that are #included if they use the standard include file extensions.  "
634217309Snwhitehorn        "To set breakpoints on .c/.cpp/.m/.mm files that are #included, set target.inline-breakpoint-strategy"
635217309Snwhitehorn        " to \"always\"."},
636217309Snwhitehorn
637217309Snwhitehorn    { LLDB_OPT_SET_1, true, "line", 'l', required_argument, NULL, 0, eArgTypeLineNum,
638217309Snwhitehorn        "Specifies the line number on which to set this breakpoint."},
639217309Snwhitehorn
640217309Snwhitehorn    // Comment out this option for the moment, as we don't actually use it, but will in the future.
641217309Snwhitehorn    // This way users won't see it, but the infrastructure is left in place.
642217309Snwhitehorn    //    { 0, false, "column",     'C', required_argument, NULL, "<column>",
643217309Snwhitehorn    //    "Set the breakpoint by source location at this particular column."},
644217309Snwhitehorn
645217309Snwhitehorn    { LLDB_OPT_SET_2, true, "address", 'a', required_argument, NULL, 0, eArgTypeAddressOrExpression,
646217309Snwhitehorn        "Set the breakpoint by address, at the specified address."},
647217309Snwhitehorn
648217309Snwhitehorn    { LLDB_OPT_SET_3, true, "name", 'n', required_argument, NULL, CommandCompletions::eSymbolCompletion, eArgTypeFunctionName,
649217309Snwhitehorn        "Set the breakpoint by function name.  Can be repeated multiple times to make one breakpoint for multiple names" },
650217309Snwhitehorn
651217309Snwhitehorn    { LLDB_OPT_SET_4, true, "fullname", 'F', required_argument, NULL, CommandCompletions::eSymbolCompletion, eArgTypeFullName,
652217309Snwhitehorn        "Set the breakpoint by fully qualified function names. For C++ this means namespaces and all arguments, and "
653217309Snwhitehorn        "for Objective C this means a full function prototype with class and selector.   "
654217309Snwhitehorn        "Can be repeated multiple times to make one breakpoint for multiple names." },
655217309Snwhitehorn
656217309Snwhitehorn    { LLDB_OPT_SET_5, true, "selector", 'S', required_argument, NULL, 0, eArgTypeSelector,
657217309Snwhitehorn        "Set the breakpoint by ObjC selector name. Can be repeated multiple times to make one breakpoint for multiple Selectors." },
658217309Snwhitehorn
659217309Snwhitehorn    { LLDB_OPT_SET_6, true, "method", 'M', required_argument, NULL, 0, eArgTypeMethod,
660217309Snwhitehorn        "Set the breakpoint by C++ method names.  Can be repeated multiple times to make one breakpoint for multiple methods." },
661217309Snwhitehorn
662217309Snwhitehorn    { LLDB_OPT_SET_7, true, "func-regex", 'r', required_argument, NULL, 0, eArgTypeRegularExpression,
663217309Snwhitehorn        "Set the breakpoint by function name, evaluating a regular-expression to find the function name(s)." },
664217309Snwhitehorn
665217309Snwhitehorn    { LLDB_OPT_SET_8, true, "basename", 'b', required_argument, NULL, CommandCompletions::eSymbolCompletion, eArgTypeFunctionName,
666217309Snwhitehorn        "Set the breakpoint by function basename (C++ namespaces and arguments will be ignored). "
667217309Snwhitehorn        "Can be repeated multiple times to make one breakpoint for multiple symbols." },
668217309Snwhitehorn
669217309Snwhitehorn    { LLDB_OPT_SET_9, true, "source-pattern-regexp", 'p', required_argument, NULL, 0, eArgTypeRegularExpression,
670217309Snwhitehorn        "Set the breakpoint by specifying a regular expression which is matched against the source text in a source file or files "
671217309Snwhitehorn        "specified with the -f option.  The -f option can be specified more than once.  "
672220749Snwhitehorn        "If no source files are specified, uses the current \"default source file\"" },
673220749Snwhitehorn
674220749Snwhitehorn    { LLDB_OPT_SET_10, true, "language-exception", 'E', required_argument, NULL, 0, eArgTypeLanguage,
675220749Snwhitehorn        "Set the breakpoint on exceptions thrown by the specified language (without options, on throw but not catch.)" },
676217309Snwhitehorn
677217309Snwhitehorn    { LLDB_OPT_SET_10, false, "on-throw", 'w', required_argument, NULL, 0, eArgTypeBoolean,
678217309Snwhitehorn        "Set the breakpoint on exception throW." },
679217309Snwhitehorn
680217309Snwhitehorn    { LLDB_OPT_SET_10, false, "on-catch", 'h', required_argument, NULL, 0, eArgTypeBoolean,
681217309Snwhitehorn        "Set the breakpoint on exception catcH." },
682217309Snwhitehorn
683217309Snwhitehorn    { LLDB_OPT_SKIP_PROLOGUE, false, "skip-prologue", 'K', required_argument, NULL, 0, eArgTypeBoolean,
684251843Sbapt        "sKip the prologue if the breakpoint is at the beginning of a function.  If not set the target.skip-prologue setting is used." },
685217309Snwhitehorn
686217309Snwhitehorn    { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
687217309Snwhitehorn};
688
689//-------------------------------------------------------------------------
690// CommandObjectBreakpointModify
691//-------------------------------------------------------------------------
692#pragma mark Modify
693
694class CommandObjectBreakpointModify : public CommandObjectParsed
695{
696public:
697
698    CommandObjectBreakpointModify (CommandInterpreter &interpreter) :
699        CommandObjectParsed (interpreter,
700                             "breakpoint modify",
701                             "Modify the options on a breakpoint or set of breakpoints in the executable.  "
702                             "If no breakpoint is specified, acts on the last created breakpoint.  "
703                             "With the exception of -e, -d and -i, passing an empty argument clears the modification.",
704                             NULL),
705        m_options (interpreter)
706    {
707        CommandArgumentEntry arg;
708        CommandObject::AddIDsArgumentData(arg, eArgTypeBreakpointID, eArgTypeBreakpointIDRange);
709        // Add the entry for the first argument for this command to the object's arguments vector.
710        m_arguments.push_back (arg);
711    }
712
713
714    virtual
715    ~CommandObjectBreakpointModify () {}
716
717    virtual Options *
718    GetOptions ()
719    {
720        return &m_options;
721    }
722
723    class CommandOptions : public Options
724    {
725    public:
726
727        CommandOptions (CommandInterpreter &interpreter) :
728            Options (interpreter),
729            m_ignore_count (0),
730            m_thread_id(LLDB_INVALID_THREAD_ID),
731            m_thread_id_passed(false),
732            m_thread_index (UINT32_MAX),
733            m_thread_index_passed(false),
734            m_thread_name(),
735            m_queue_name(),
736            m_condition (),
737            m_one_shot (false),
738            m_enable_passed (false),
739            m_enable_value (false),
740            m_name_passed (false),
741            m_queue_passed (false),
742            m_condition_passed (false),
743            m_one_shot_passed (false)
744        {
745        }
746
747        virtual
748        ~CommandOptions () {}
749
750        virtual Error
751        SetOptionValue (uint32_t option_idx, const char *option_arg)
752        {
753            Error error;
754            const int short_option = m_getopt_table[option_idx].val;
755
756            switch (short_option)
757            {
758                case 'c':
759                    if (option_arg != NULL)
760                        m_condition.assign (option_arg);
761                    else
762                        m_condition.clear();
763                    m_condition_passed = true;
764                    break;
765                case 'd':
766                    m_enable_passed = true;
767                    m_enable_value = false;
768                    break;
769                case 'e':
770                    m_enable_passed = true;
771                    m_enable_value = true;
772                    break;
773                case 'i':
774                {
775                    m_ignore_count = Args::StringToUInt32(option_arg, UINT32_MAX, 0);
776                    if (m_ignore_count == UINT32_MAX)
777                       error.SetErrorStringWithFormat ("invalid ignore count '%s'", option_arg);
778                }
779                break;
780                case 'o':
781                {
782                    bool value, success;
783                    value = Args::StringToBoolean(option_arg, false, &success);
784                    if (success)
785                    {
786                        m_one_shot_passed = true;
787                        m_one_shot = value;
788                    }
789                    else
790                        error.SetErrorStringWithFormat("invalid boolean value '%s' passed for -o option", option_arg);
791                }
792                break;
793                case 't' :
794                {
795                    if (option_arg[0] == '\0')
796                    {
797                        m_thread_id = LLDB_INVALID_THREAD_ID;
798                        m_thread_id_passed = true;
799                    }
800                    else
801                    {
802                        m_thread_id = Args::StringToUInt64(option_arg, LLDB_INVALID_THREAD_ID, 0);
803                        if (m_thread_id == LLDB_INVALID_THREAD_ID)
804                           error.SetErrorStringWithFormat ("invalid thread id string '%s'", option_arg);
805                        else
806                            m_thread_id_passed = true;
807                    }
808                }
809                break;
810                case 'T':
811                    if (option_arg != NULL)
812                        m_thread_name.assign (option_arg);
813                    else
814                        m_thread_name.clear();
815                    m_name_passed = true;
816                    break;
817                case 'q':
818                    if (option_arg != NULL)
819                        m_queue_name.assign (option_arg);
820                    else
821                        m_queue_name.clear();
822                    m_queue_passed = true;
823                    break;
824                case 'x':
825                {
826                    if (option_arg[0] == '\n')
827                    {
828                        m_thread_index = UINT32_MAX;
829                        m_thread_index_passed = true;
830                    }
831                    else
832                    {
833                        m_thread_index = Args::StringToUInt32 (option_arg, UINT32_MAX, 0);
834                        if (m_thread_id == UINT32_MAX)
835                           error.SetErrorStringWithFormat ("invalid thread index string '%s'", option_arg);
836                        else
837                            m_thread_index_passed = true;
838                    }
839                }
840                break;
841                default:
842                    error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
843                    break;
844            }
845
846            return error;
847        }
848        void
849        OptionParsingStarting ()
850        {
851            m_ignore_count = 0;
852            m_thread_id = LLDB_INVALID_THREAD_ID;
853            m_thread_id_passed = false;
854            m_thread_index = UINT32_MAX;
855            m_thread_index_passed = false;
856            m_thread_name.clear();
857            m_queue_name.clear();
858            m_condition.clear();
859            m_one_shot = false;
860            m_enable_passed = false;
861            m_queue_passed = false;
862            m_name_passed = false;
863            m_condition_passed = false;
864            m_one_shot_passed = false;
865        }
866
867        const OptionDefinition*
868        GetDefinitions ()
869        {
870            return g_option_table;
871        }
872
873
874        // Options table: Required for subclasses of Options.
875
876        static OptionDefinition g_option_table[];
877
878        // Instance variables to hold the values for command options.
879
880        uint32_t m_ignore_count;
881        lldb::tid_t m_thread_id;
882        bool m_thread_id_passed;
883        uint32_t m_thread_index;
884        bool m_thread_index_passed;
885        std::string m_thread_name;
886        std::string m_queue_name;
887        std::string m_condition;
888        bool m_one_shot;
889        bool m_enable_passed;
890        bool m_enable_value;
891        bool m_name_passed;
892        bool m_queue_passed;
893        bool m_condition_passed;
894        bool m_one_shot_passed;
895
896    };
897
898protected:
899    virtual bool
900    DoExecute (Args& command, CommandReturnObject &result)
901    {
902        Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
903        if (target == NULL)
904        {
905            result.AppendError ("Invalid target.  No existing target or breakpoints.");
906            result.SetStatus (eReturnStatusFailed);
907            return false;
908        }
909
910        Mutex::Locker locker;
911        target->GetBreakpointList().GetListMutex(locker);
912
913        BreakpointIDList valid_bp_ids;
914
915        CommandObjectMultiwordBreakpoint::VerifyBreakpointIDs (command, target, result, &valid_bp_ids);
916
917        if (result.Succeeded())
918        {
919            const size_t count = valid_bp_ids.GetSize();
920            for (size_t i = 0; i < count; ++i)
921            {
922                BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex (i);
923
924                if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID)
925                {
926                    Breakpoint *bp = target->GetBreakpointByID (cur_bp_id.GetBreakpointID()).get();
927                    if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID)
928                    {
929                        BreakpointLocation *location = bp->FindLocationByID (cur_bp_id.GetLocationID()).get();
930                        if (location)
931                        {
932                            if (m_options.m_thread_id_passed)
933                                location->SetThreadID (m_options.m_thread_id);
934
935                            if (m_options.m_thread_index_passed)
936                                location->SetThreadIndex(m_options.m_thread_index);
937
938                            if (m_options.m_name_passed)
939                                location->SetThreadName(m_options.m_thread_name.c_str());
940
941                            if (m_options.m_queue_passed)
942                                location->SetQueueName(m_options.m_queue_name.c_str());
943
944                            if (m_options.m_ignore_count != 0)
945                                location->SetIgnoreCount(m_options.m_ignore_count);
946
947                            if (m_options.m_enable_passed)
948                                location->SetEnabled (m_options.m_enable_value);
949
950                            if (m_options.m_condition_passed)
951                                location->SetCondition (m_options.m_condition.c_str());
952                        }
953                    }
954                    else
955                    {
956                        if (m_options.m_thread_id_passed)
957                            bp->SetThreadID (m_options.m_thread_id);
958
959                        if (m_options.m_thread_index_passed)
960                            bp->SetThreadIndex(m_options.m_thread_index);
961
962                        if (m_options.m_name_passed)
963                            bp->SetThreadName(m_options.m_thread_name.c_str());
964
965                        if (m_options.m_queue_passed)
966                            bp->SetQueueName(m_options.m_queue_name.c_str());
967
968                        if (m_options.m_ignore_count != 0)
969                            bp->SetIgnoreCount(m_options.m_ignore_count);
970
971                        if (m_options.m_enable_passed)
972                            bp->SetEnabled (m_options.m_enable_value);
973
974                        if (m_options.m_condition_passed)
975                            bp->SetCondition (m_options.m_condition.c_str());
976                    }
977                }
978            }
979        }
980
981        return result.Succeeded();
982    }
983
984private:
985    CommandOptions m_options;
986};
987
988#pragma mark Modify::CommandOptions
989OptionDefinition
990CommandObjectBreakpointModify::CommandOptions::g_option_table[] =
991{
992{ LLDB_OPT_SET_ALL, false, "ignore-count", 'i', required_argument, NULL, 0, eArgTypeCount, "Set the number of times this breakpoint is skipped before stopping." },
993{ LLDB_OPT_SET_ALL, false, "one-shot",     'o', required_argument, NULL, 0, eArgTypeBoolean, "The breakpoint is deleted the first time it stop causes a stop." },
994{ LLDB_OPT_SET_ALL, false, "thread-index", 'x', required_argument, NULL, 0, eArgTypeThreadIndex, "The breakpoint stops only for the thread whose index matches this argument."},
995{ LLDB_OPT_SET_ALL, false, "thread-id",    't', required_argument, NULL, 0, eArgTypeThreadID, "The breakpoint stops only for the thread whose TID matches this argument."},
996{ LLDB_OPT_SET_ALL, false, "thread-name",  'T', required_argument, NULL, 0, eArgTypeThreadName, "The breakpoint stops only for the thread whose thread name matches this argument."},
997{ LLDB_OPT_SET_ALL, false, "queue-name",   'q', required_argument, NULL, 0, eArgTypeQueueName, "The breakpoint stops only for threads in the queue whose name is given by this argument."},
998{ LLDB_OPT_SET_ALL, false, "condition",    'c', required_argument, NULL, 0, eArgTypeExpression, "The breakpoint stops only if this condition expression evaluates to true."},
999{ LLDB_OPT_SET_1,   false, "enable",       'e', no_argument,       NULL, 0, eArgTypeNone, "Enable the breakpoint."},
1000{ LLDB_OPT_SET_2,   false, "disable",      'd', no_argument,       NULL, 0, eArgTypeNone, "Disable the breakpoint."},
1001{ 0,                false, NULL,            0 , 0,                 NULL, 0, eArgTypeNone, NULL }
1002};
1003
1004//-------------------------------------------------------------------------
1005// CommandObjectBreakpointEnable
1006//-------------------------------------------------------------------------
1007#pragma mark Enable
1008
1009class CommandObjectBreakpointEnable : public CommandObjectParsed
1010{
1011public:
1012    CommandObjectBreakpointEnable (CommandInterpreter &interpreter) :
1013        CommandObjectParsed (interpreter,
1014                             "enable",
1015                             "Enable the specified disabled breakpoint(s). If no breakpoints are specified, enable all of them.",
1016                             NULL)
1017    {
1018        CommandArgumentEntry arg;
1019        CommandObject::AddIDsArgumentData(arg, eArgTypeBreakpointID, eArgTypeBreakpointIDRange);
1020        // Add the entry for the first argument for this command to the object's arguments vector.
1021        m_arguments.push_back (arg);
1022    }
1023
1024
1025    virtual
1026    ~CommandObjectBreakpointEnable () {}
1027
1028protected:
1029    virtual bool
1030    DoExecute (Args& command, CommandReturnObject &result)
1031    {
1032        Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
1033        if (target == NULL)
1034        {
1035            result.AppendError ("Invalid target.  No existing target or breakpoints.");
1036            result.SetStatus (eReturnStatusFailed);
1037            return false;
1038        }
1039
1040        Mutex::Locker locker;
1041        target->GetBreakpointList().GetListMutex(locker);
1042
1043        const BreakpointList &breakpoints = target->GetBreakpointList();
1044
1045        size_t num_breakpoints = breakpoints.GetSize();
1046
1047        if (num_breakpoints == 0)
1048        {
1049            result.AppendError ("No breakpoints exist to be enabled.");
1050            result.SetStatus (eReturnStatusFailed);
1051            return false;
1052        }
1053
1054        if (command.GetArgumentCount() == 0)
1055        {
1056            // No breakpoint selected; enable all currently set breakpoints.
1057            target->EnableAllBreakpoints ();
1058            result.AppendMessageWithFormat ("All breakpoints enabled. (%lu breakpoints)\n", num_breakpoints);
1059            result.SetStatus (eReturnStatusSuccessFinishNoResult);
1060        }
1061        else
1062        {
1063            // Particular breakpoint selected; enable that breakpoint.
1064            BreakpointIDList valid_bp_ids;
1065            CommandObjectMultiwordBreakpoint::VerifyBreakpointIDs (command, target, result, &valid_bp_ids);
1066
1067            if (result.Succeeded())
1068            {
1069                int enable_count = 0;
1070                int loc_count = 0;
1071                const size_t count = valid_bp_ids.GetSize();
1072                for (size_t i = 0; i < count; ++i)
1073                {
1074                    BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex (i);
1075
1076                    if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID)
1077                    {
1078                        Breakpoint *breakpoint = target->GetBreakpointByID (cur_bp_id.GetBreakpointID()).get();
1079                        if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID)
1080                        {
1081                            BreakpointLocation *location = breakpoint->FindLocationByID (cur_bp_id.GetLocationID()).get();
1082                            if (location)
1083                            {
1084                                location->SetEnabled (true);
1085                                ++loc_count;
1086                            }
1087                        }
1088                        else
1089                        {
1090                            breakpoint->SetEnabled (true);
1091                            ++enable_count;
1092                        }
1093                    }
1094                }
1095                result.AppendMessageWithFormat ("%d breakpoints enabled.\n", enable_count + loc_count);
1096                result.SetStatus (eReturnStatusSuccessFinishNoResult);
1097            }
1098        }
1099
1100        return result.Succeeded();
1101    }
1102};
1103
1104//-------------------------------------------------------------------------
1105// CommandObjectBreakpointDisable
1106//-------------------------------------------------------------------------
1107#pragma mark Disable
1108
1109class CommandObjectBreakpointDisable : public CommandObjectParsed
1110{
1111public:
1112    CommandObjectBreakpointDisable (CommandInterpreter &interpreter) :
1113        CommandObjectParsed (interpreter,
1114                             "breakpoint disable",
1115                             "Disable the specified breakpoint(s) without removing it/them.  If no breakpoints are specified, disable them all.",
1116                             NULL)
1117    {
1118        SetHelpLong(
1119"Disable the specified breakpoint(s) without removing it/them.  \n\
1120If no breakpoints are specified, disable them all.\n\
1121\n\
1122Note: disabling a breakpoint will cause none of its locations to be hit\n\
1123regardless of whether they are enabled or disabled.  So the sequence: \n\
1124\n\
1125    (lldb) break disable 1\n\
1126    (lldb) break enable 1.1\n\
1127\n\
1128will NOT cause location 1.1 to get hit.  To achieve that, do:\n\
1129\n\
1130    (lldb) break disable 1.*\n\
1131    (lldb) break enable 1.1\n\
1132\n\
1133The first command disables all the locations of breakpoint 1, \n\
1134the second re-enables the first location."
1135                    );
1136
1137        CommandArgumentEntry arg;
1138        CommandObject::AddIDsArgumentData(arg, eArgTypeBreakpointID, eArgTypeBreakpointIDRange);
1139        // Add the entry for the first argument for this command to the object's arguments vector.
1140        m_arguments.push_back (arg);
1141
1142    }
1143
1144
1145    virtual
1146    ~CommandObjectBreakpointDisable () {}
1147
1148protected:
1149    virtual bool
1150    DoExecute (Args& command, CommandReturnObject &result)
1151    {
1152        Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
1153        if (target == NULL)
1154        {
1155            result.AppendError ("Invalid target.  No existing target or breakpoints.");
1156            result.SetStatus (eReturnStatusFailed);
1157            return false;
1158        }
1159
1160        Mutex::Locker locker;
1161        target->GetBreakpointList().GetListMutex(locker);
1162
1163        const BreakpointList &breakpoints = target->GetBreakpointList();
1164        size_t num_breakpoints = breakpoints.GetSize();
1165
1166        if (num_breakpoints == 0)
1167        {
1168            result.AppendError ("No breakpoints exist to be disabled.");
1169            result.SetStatus (eReturnStatusFailed);
1170            return false;
1171        }
1172
1173        if (command.GetArgumentCount() == 0)
1174        {
1175            // No breakpoint selected; disable all currently set breakpoints.
1176            target->DisableAllBreakpoints ();
1177            result.AppendMessageWithFormat ("All breakpoints disabled. (%lu breakpoints)\n", num_breakpoints);
1178            result.SetStatus (eReturnStatusSuccessFinishNoResult);
1179        }
1180        else
1181        {
1182            // Particular breakpoint selected; disable that breakpoint.
1183            BreakpointIDList valid_bp_ids;
1184
1185            CommandObjectMultiwordBreakpoint::VerifyBreakpointIDs (command, target, result, &valid_bp_ids);
1186
1187            if (result.Succeeded())
1188            {
1189                int disable_count = 0;
1190                int loc_count = 0;
1191                const size_t count = valid_bp_ids.GetSize();
1192                for (size_t i = 0; i < count; ++i)
1193                {
1194                    BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex (i);
1195
1196                    if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID)
1197                    {
1198                        Breakpoint *breakpoint = target->GetBreakpointByID (cur_bp_id.GetBreakpointID()).get();
1199                        if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID)
1200                        {
1201                            BreakpointLocation *location = breakpoint->FindLocationByID (cur_bp_id.GetLocationID()).get();
1202                            if (location)
1203                            {
1204                                location->SetEnabled (false);
1205                                ++loc_count;
1206                            }
1207                        }
1208                        else
1209                        {
1210                            breakpoint->SetEnabled (false);
1211                            ++disable_count;
1212                        }
1213                    }
1214                }
1215                result.AppendMessageWithFormat ("%d breakpoints disabled.\n", disable_count + loc_count);
1216                result.SetStatus (eReturnStatusSuccessFinishNoResult);
1217            }
1218        }
1219
1220        return result.Succeeded();
1221    }
1222
1223};
1224
1225//-------------------------------------------------------------------------
1226// CommandObjectBreakpointList
1227//-------------------------------------------------------------------------
1228#pragma mark List
1229
1230class CommandObjectBreakpointList : public CommandObjectParsed
1231{
1232public:
1233    CommandObjectBreakpointList (CommandInterpreter &interpreter) :
1234        CommandObjectParsed (interpreter,
1235                             "breakpoint list",
1236                             "List some or all breakpoints at configurable levels of detail.",
1237                             NULL),
1238        m_options (interpreter)
1239    {
1240        CommandArgumentEntry arg;
1241        CommandArgumentData bp_id_arg;
1242
1243        // Define the first (and only) variant of this arg.
1244        bp_id_arg.arg_type = eArgTypeBreakpointID;
1245        bp_id_arg.arg_repetition = eArgRepeatOptional;
1246
1247        // There is only one variant this argument could be; put it into the argument entry.
1248        arg.push_back (bp_id_arg);
1249
1250        // Push the data for the first argument into the m_arguments vector.
1251        m_arguments.push_back (arg);
1252    }
1253
1254
1255    virtual
1256    ~CommandObjectBreakpointList () {}
1257
1258    virtual Options *
1259    GetOptions ()
1260    {
1261        return &m_options;
1262    }
1263
1264    class CommandOptions : public Options
1265    {
1266    public:
1267
1268        CommandOptions (CommandInterpreter &interpreter) :
1269            Options (interpreter),
1270            m_level (lldb::eDescriptionLevelBrief)  // Breakpoint List defaults to brief descriptions
1271        {
1272        }
1273
1274        virtual
1275        ~CommandOptions () {}
1276
1277        virtual Error
1278        SetOptionValue (uint32_t option_idx, const char *option_arg)
1279        {
1280            Error error;
1281            const int short_option = m_getopt_table[option_idx].val;
1282
1283            switch (short_option)
1284            {
1285                case 'b':
1286                    m_level = lldb::eDescriptionLevelBrief;
1287                    break;
1288                case 'f':
1289                    m_level = lldb::eDescriptionLevelFull;
1290                    break;
1291                case 'v':
1292                    m_level = lldb::eDescriptionLevelVerbose;
1293                    break;
1294                case 'i':
1295                    m_internal = true;
1296                    break;
1297                default:
1298                    error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
1299                    break;
1300            }
1301
1302            return error;
1303        }
1304
1305        void
1306        OptionParsingStarting ()
1307        {
1308            m_level = lldb::eDescriptionLevelFull;
1309            m_internal = false;
1310        }
1311
1312        const OptionDefinition *
1313        GetDefinitions ()
1314        {
1315            return g_option_table;
1316        }
1317
1318        // Options table: Required for subclasses of Options.
1319
1320        static OptionDefinition g_option_table[];
1321
1322        // Instance variables to hold the values for command options.
1323
1324        lldb::DescriptionLevel m_level;
1325
1326        bool m_internal;
1327    };
1328
1329protected:
1330    virtual bool
1331    DoExecute (Args& command, CommandReturnObject &result)
1332    {
1333        Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
1334        if (target == NULL)
1335        {
1336            result.AppendError ("Invalid target. No current target or breakpoints.");
1337            result.SetStatus (eReturnStatusSuccessFinishNoResult);
1338            return true;
1339        }
1340
1341        const BreakpointList &breakpoints = target->GetBreakpointList(m_options.m_internal);
1342        Mutex::Locker locker;
1343        target->GetBreakpointList(m_options.m_internal).GetListMutex(locker);
1344
1345        size_t num_breakpoints = breakpoints.GetSize();
1346
1347        if (num_breakpoints == 0)
1348        {
1349            result.AppendMessage ("No breakpoints currently set.");
1350            result.SetStatus (eReturnStatusSuccessFinishNoResult);
1351            return true;
1352        }
1353
1354        Stream &output_stream = result.GetOutputStream();
1355
1356        if (command.GetArgumentCount() == 0)
1357        {
1358            // No breakpoint selected; show info about all currently set breakpoints.
1359            result.AppendMessage ("Current breakpoints:");
1360            for (size_t i = 0; i < num_breakpoints; ++i)
1361            {
1362                Breakpoint *breakpoint = breakpoints.GetBreakpointAtIndex (i).get();
1363                AddBreakpointDescription (&output_stream, breakpoint, m_options.m_level);
1364            }
1365            result.SetStatus (eReturnStatusSuccessFinishNoResult);
1366        }
1367        else
1368        {
1369            // Particular breakpoints selected; show info about that breakpoint.
1370            BreakpointIDList valid_bp_ids;
1371            CommandObjectMultiwordBreakpoint::VerifyBreakpointIDs (command, target, result, &valid_bp_ids);
1372
1373            if (result.Succeeded())
1374            {
1375                for (size_t i = 0; i < valid_bp_ids.GetSize(); ++i)
1376                {
1377                    BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex (i);
1378                    Breakpoint *breakpoint = target->GetBreakpointByID (cur_bp_id.GetBreakpointID()).get();
1379                    AddBreakpointDescription (&output_stream, breakpoint, m_options.m_level);
1380                }
1381                result.SetStatus (eReturnStatusSuccessFinishNoResult);
1382            }
1383            else
1384            {
1385                result.AppendError ("Invalid breakpoint id.");
1386                result.SetStatus (eReturnStatusFailed);
1387            }
1388        }
1389
1390        return result.Succeeded();
1391    }
1392
1393private:
1394    CommandOptions m_options;
1395};
1396
1397#pragma mark List::CommandOptions
1398OptionDefinition
1399CommandObjectBreakpointList::CommandOptions::g_option_table[] =
1400{
1401    { LLDB_OPT_SET_ALL, false, "internal", 'i', no_argument, NULL, 0, eArgTypeNone,
1402        "Show debugger internal breakpoints" },
1403
1404    { LLDB_OPT_SET_1, false, "brief",    'b', no_argument, NULL, 0, eArgTypeNone,
1405        "Give a brief description of the breakpoint (no location info)."},
1406
1407    // FIXME: We need to add an "internal" command, and then add this sort of thing to it.
1408    // But I need to see it for now, and don't want to wait.
1409    { LLDB_OPT_SET_2, false, "full",    'f', no_argument, NULL, 0, eArgTypeNone,
1410        "Give a full description of the breakpoint and its locations."},
1411
1412    { LLDB_OPT_SET_3, false, "verbose", 'v', no_argument, NULL, 0, eArgTypeNone,
1413        "Explain everything we know about the breakpoint (for debugging debugger bugs)." },
1414
1415    { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
1416};
1417
1418//-------------------------------------------------------------------------
1419// CommandObjectBreakpointClear
1420//-------------------------------------------------------------------------
1421#pragma mark Clear
1422
1423class CommandObjectBreakpointClear : public CommandObjectParsed
1424{
1425public:
1426
1427    typedef enum BreakpointClearType
1428    {
1429        eClearTypeInvalid,
1430        eClearTypeFileAndLine
1431    } BreakpointClearType;
1432
1433    CommandObjectBreakpointClear (CommandInterpreter &interpreter) :
1434        CommandObjectParsed (interpreter,
1435                             "breakpoint clear",
1436                             "Clears a breakpoint or set of breakpoints in the executable.",
1437                             "breakpoint clear <cmd-options>"),
1438        m_options (interpreter)
1439    {
1440    }
1441
1442    virtual
1443    ~CommandObjectBreakpointClear () {}
1444
1445    virtual Options *
1446    GetOptions ()
1447    {
1448        return &m_options;
1449    }
1450
1451    class CommandOptions : public Options
1452    {
1453    public:
1454
1455        CommandOptions (CommandInterpreter &interpreter) :
1456            Options (interpreter),
1457            m_filename (),
1458            m_line_num (0)
1459        {
1460        }
1461
1462        virtual
1463        ~CommandOptions () {}
1464
1465        virtual Error
1466        SetOptionValue (uint32_t option_idx, const char *option_arg)
1467        {
1468            Error error;
1469            const int short_option = m_getopt_table[option_idx].val;
1470
1471            switch (short_option)
1472            {
1473                case 'f':
1474                    m_filename.assign (option_arg);
1475                    break;
1476
1477                case 'l':
1478                    m_line_num = Args::StringToUInt32 (option_arg, 0);
1479                    break;
1480
1481                default:
1482                    error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
1483                    break;
1484            }
1485
1486            return error;
1487        }
1488
1489        void
1490        OptionParsingStarting ()
1491        {
1492            m_filename.clear();
1493            m_line_num = 0;
1494        }
1495
1496        const OptionDefinition*
1497        GetDefinitions ()
1498        {
1499            return g_option_table;
1500        }
1501
1502        // Options table: Required for subclasses of Options.
1503
1504        static OptionDefinition g_option_table[];
1505
1506        // Instance variables to hold the values for command options.
1507
1508        std::string m_filename;
1509        uint32_t m_line_num;
1510
1511    };
1512
1513protected:
1514    virtual bool
1515    DoExecute (Args& command, CommandReturnObject &result)
1516    {
1517        Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
1518        if (target == NULL)
1519        {
1520            result.AppendError ("Invalid target. No existing target or breakpoints.");
1521            result.SetStatus (eReturnStatusFailed);
1522            return false;
1523        }
1524
1525        // The following are the various types of breakpoints that could be cleared:
1526        //   1). -f -l (clearing breakpoint by source location)
1527
1528        BreakpointClearType break_type = eClearTypeInvalid;
1529
1530        if (m_options.m_line_num != 0)
1531            break_type = eClearTypeFileAndLine;
1532
1533        Mutex::Locker locker;
1534        target->GetBreakpointList().GetListMutex(locker);
1535
1536        BreakpointList &breakpoints = target->GetBreakpointList();
1537        size_t num_breakpoints = breakpoints.GetSize();
1538
1539        // Early return if there's no breakpoint at all.
1540        if (num_breakpoints == 0)
1541        {
1542            result.AppendError ("Breakpoint clear: No breakpoint cleared.");
1543            result.SetStatus (eReturnStatusFailed);
1544            return result.Succeeded();
1545        }
1546
1547        // Find matching breakpoints and delete them.
1548
1549        // First create a copy of all the IDs.
1550        std::vector<break_id_t> BreakIDs;
1551        for (size_t i = 0; i < num_breakpoints; ++i)
1552            BreakIDs.push_back(breakpoints.GetBreakpointAtIndex(i).get()->GetID());
1553
1554        int num_cleared = 0;
1555        StreamString ss;
1556        switch (break_type)
1557        {
1558            case eClearTypeFileAndLine: // Breakpoint by source position
1559                {
1560                    const ConstString filename(m_options.m_filename.c_str());
1561                    BreakpointLocationCollection loc_coll;
1562
1563                    for (size_t i = 0; i < num_breakpoints; ++i)
1564                    {
1565                        Breakpoint *bp = breakpoints.FindBreakpointByID(BreakIDs[i]).get();
1566
1567                        if (bp->GetMatchingFileLine(filename, m_options.m_line_num, loc_coll))
1568                        {
1569                            // If the collection size is 0, it's a full match and we can just remove the breakpoint.
1570                            if (loc_coll.GetSize() == 0)
1571                            {
1572                                bp->GetDescription(&ss, lldb::eDescriptionLevelBrief);
1573                                ss.EOL();
1574                                target->RemoveBreakpointByID (bp->GetID());
1575                                ++num_cleared;
1576                            }
1577                        }
1578                    }
1579                }
1580                break;
1581
1582            default:
1583                break;
1584        }
1585
1586        if (num_cleared > 0)
1587        {
1588            Stream &output_stream = result.GetOutputStream();
1589            output_stream.Printf ("%d breakpoints cleared:\n", num_cleared);
1590            output_stream << ss.GetData();
1591            output_stream.EOL();
1592            result.SetStatus (eReturnStatusSuccessFinishNoResult);
1593        }
1594        else
1595        {
1596            result.AppendError ("Breakpoint clear: No breakpoint cleared.");
1597            result.SetStatus (eReturnStatusFailed);
1598        }
1599
1600        return result.Succeeded();
1601    }
1602
1603private:
1604    CommandOptions m_options;
1605};
1606
1607#pragma mark Clear::CommandOptions
1608
1609OptionDefinition
1610CommandObjectBreakpointClear::CommandOptions::g_option_table[] =
1611{
1612    { LLDB_OPT_SET_1, false, "file", 'f', required_argument, NULL, CommandCompletions::eSourceFileCompletion, eArgTypeFilename,
1613        "Specify the breakpoint by source location in this particular file."},
1614
1615    { LLDB_OPT_SET_1, true, "line", 'l', required_argument, NULL, 0, eArgTypeLineNum,
1616        "Specify the breakpoint by source location at this particular line."},
1617
1618    { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
1619};
1620
1621//-------------------------------------------------------------------------
1622// CommandObjectBreakpointDelete
1623//-------------------------------------------------------------------------
1624#pragma mark Delete
1625
1626class CommandObjectBreakpointDelete : public CommandObjectParsed
1627{
1628public:
1629    CommandObjectBreakpointDelete (CommandInterpreter &interpreter) :
1630        CommandObjectParsed (interpreter,
1631                             "breakpoint delete",
1632                             "Delete the specified breakpoint(s).  If no breakpoints are specified, delete them all.",
1633                             NULL)
1634    {
1635        CommandArgumentEntry arg;
1636        CommandObject::AddIDsArgumentData(arg, eArgTypeBreakpointID, eArgTypeBreakpointIDRange);
1637        // Add the entry for the first argument for this command to the object's arguments vector.
1638        m_arguments.push_back (arg);
1639    }
1640
1641    virtual
1642    ~CommandObjectBreakpointDelete () {}
1643
1644protected:
1645    virtual bool
1646    DoExecute (Args& command, CommandReturnObject &result)
1647    {
1648        Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
1649        if (target == NULL)
1650        {
1651            result.AppendError ("Invalid target. No existing target or breakpoints.");
1652            result.SetStatus (eReturnStatusFailed);
1653            return false;
1654        }
1655
1656        Mutex::Locker locker;
1657        target->GetBreakpointList().GetListMutex(locker);
1658
1659        const BreakpointList &breakpoints = target->GetBreakpointList();
1660
1661        size_t num_breakpoints = breakpoints.GetSize();
1662
1663        if (num_breakpoints == 0)
1664        {
1665            result.AppendError ("No breakpoints exist to be deleted.");
1666            result.SetStatus (eReturnStatusFailed);
1667            return false;
1668        }
1669
1670        if (command.GetArgumentCount() == 0)
1671        {
1672            if (!m_interpreter.Confirm ("About to delete all breakpoints, do you want to do that?", true))
1673            {
1674                result.AppendMessage("Operation cancelled...");
1675            }
1676            else
1677            {
1678                target->RemoveAllBreakpoints ();
1679                result.AppendMessageWithFormat ("All breakpoints removed. (%lu %s)\n", num_breakpoints, num_breakpoints > 1 ? "breakpoints" : "breakpoint");
1680            }
1681            result.SetStatus (eReturnStatusSuccessFinishNoResult);
1682        }
1683        else
1684        {
1685            // Particular breakpoint selected; disable that breakpoint.
1686            BreakpointIDList valid_bp_ids;
1687            CommandObjectMultiwordBreakpoint::VerifyBreakpointIDs (command, target, result, &valid_bp_ids);
1688
1689            if (result.Succeeded())
1690            {
1691                int delete_count = 0;
1692                int disable_count = 0;
1693                const size_t count = valid_bp_ids.GetSize();
1694                for (size_t i = 0; i < count; ++i)
1695                {
1696                    BreakpointID cur_bp_id = valid_bp_ids.GetBreakpointIDAtIndex (i);
1697
1698                    if (cur_bp_id.GetBreakpointID() != LLDB_INVALID_BREAK_ID)
1699                    {
1700                        if (cur_bp_id.GetLocationID() != LLDB_INVALID_BREAK_ID)
1701                        {
1702                            Breakpoint *breakpoint = target->GetBreakpointByID (cur_bp_id.GetBreakpointID()).get();
1703                            BreakpointLocation *location = breakpoint->FindLocationByID (cur_bp_id.GetLocationID()).get();
1704                            // It makes no sense to try to delete individual locations, so we disable them instead.
1705                            if (location)
1706                            {
1707                                location->SetEnabled (false);
1708                                ++disable_count;
1709                            }
1710                        }
1711                        else
1712                        {
1713                            target->RemoveBreakpointByID (cur_bp_id.GetBreakpointID());
1714                            ++delete_count;
1715                        }
1716                    }
1717                }
1718                result.AppendMessageWithFormat ("%d breakpoints deleted; %d breakpoint locations disabled.\n",
1719                                               delete_count, disable_count);
1720                result.SetStatus (eReturnStatusSuccessFinishNoResult);
1721            }
1722        }
1723        return result.Succeeded();
1724    }
1725};
1726
1727//-------------------------------------------------------------------------
1728// CommandObjectMultiwordBreakpoint
1729//-------------------------------------------------------------------------
1730#pragma mark MultiwordBreakpoint
1731
1732CommandObjectMultiwordBreakpoint::CommandObjectMultiwordBreakpoint (CommandInterpreter &interpreter) :
1733    CommandObjectMultiword (interpreter,
1734                            "breakpoint",
1735                            "A set of commands for operating on breakpoints. Also see _regexp-break.",
1736                            "breakpoint <command> [<command-options>]")
1737{
1738    CommandObjectSP list_command_object (new CommandObjectBreakpointList (interpreter));
1739    CommandObjectSP enable_command_object (new CommandObjectBreakpointEnable (interpreter));
1740    CommandObjectSP disable_command_object (new CommandObjectBreakpointDisable (interpreter));
1741    CommandObjectSP clear_command_object (new CommandObjectBreakpointClear (interpreter));
1742    CommandObjectSP delete_command_object (new CommandObjectBreakpointDelete (interpreter));
1743    CommandObjectSP set_command_object (new CommandObjectBreakpointSet (interpreter));
1744    CommandObjectSP command_command_object (new CommandObjectBreakpointCommand (interpreter));
1745    CommandObjectSP modify_command_object (new CommandObjectBreakpointModify(interpreter));
1746
1747    list_command_object->SetCommandName ("breakpoint list");
1748    enable_command_object->SetCommandName("breakpoint enable");
1749    disable_command_object->SetCommandName("breakpoint disable");
1750    clear_command_object->SetCommandName("breakpoint clear");
1751    delete_command_object->SetCommandName("breakpoint delete");
1752    set_command_object->SetCommandName("breakpoint set");
1753    command_command_object->SetCommandName ("breakpoint command");
1754    modify_command_object->SetCommandName ("breakpoint modify");
1755
1756    LoadSubCommand ("list",       list_command_object);
1757    LoadSubCommand ("enable",     enable_command_object);
1758    LoadSubCommand ("disable",    disable_command_object);
1759    LoadSubCommand ("clear",      clear_command_object);
1760    LoadSubCommand ("delete",     delete_command_object);
1761    LoadSubCommand ("set",        set_command_object);
1762    LoadSubCommand ("command",    command_command_object);
1763    LoadSubCommand ("modify",     modify_command_object);
1764}
1765
1766CommandObjectMultiwordBreakpoint::~CommandObjectMultiwordBreakpoint ()
1767{
1768}
1769
1770void
1771CommandObjectMultiwordBreakpoint::VerifyBreakpointIDs (Args &args, Target *target, CommandReturnObject &result,
1772                                                         BreakpointIDList *valid_ids)
1773{
1774    // args can be strings representing 1). integers (for breakpoint ids)
1775    //                                  2). the full breakpoint & location canonical representation
1776    //                                  3). the word "to" or a hyphen, representing a range (in which case there
1777    //                                      had *better* be an entry both before & after of one of the first two types.
1778    // If args is empty, we will use the last created breakpoint (if there is one.)
1779
1780    Args temp_args;
1781
1782    if (args.GetArgumentCount() == 0)
1783    {
1784        if (target->GetLastCreatedBreakpoint())
1785        {
1786            valid_ids->AddBreakpointID (BreakpointID(target->GetLastCreatedBreakpoint()->GetID(), LLDB_INVALID_BREAK_ID));
1787            result.SetStatus (eReturnStatusSuccessFinishNoResult);
1788        }
1789        else
1790        {
1791            result.AppendError("No breakpoint specified and no last created breakpoint.");
1792            result.SetStatus (eReturnStatusFailed);
1793        }
1794        return;
1795    }
1796
1797    // Create a new Args variable to use; copy any non-breakpoint-id-ranges stuff directly from the old ARGS to
1798    // the new TEMP_ARGS.  Do not copy breakpoint id range strings over; instead generate a list of strings for
1799    // all the breakpoint ids in the range, and shove all of those breakpoint id strings into TEMP_ARGS.
1800
1801    BreakpointIDList::FindAndReplaceIDRanges (args, target, result, temp_args);
1802
1803    // NOW, convert the list of breakpoint id strings in TEMP_ARGS into an actual BreakpointIDList:
1804
1805    valid_ids->InsertStringArray (temp_args.GetConstArgumentVector(), temp_args.GetArgumentCount(), result);
1806
1807    // At this point,  all of the breakpoint ids that the user passed in have been converted to breakpoint IDs
1808    // and put into valid_ids.
1809
1810    if (result.Succeeded())
1811    {
1812        // Now that we've converted everything from args into a list of breakpoint ids, go through our tentative list
1813        // of breakpoint id's and verify that they correspond to valid/currently set breakpoints.
1814
1815        const size_t count = valid_ids->GetSize();
1816        for (size_t i = 0; i < count; ++i)
1817        {
1818            BreakpointID cur_bp_id = valid_ids->GetBreakpointIDAtIndex (i);
1819            Breakpoint *breakpoint = target->GetBreakpointByID (cur_bp_id.GetBreakpointID()).get();
1820            if (breakpoint != NULL)
1821            {
1822                const size_t num_locations = breakpoint->GetNumLocations();
1823                if (cur_bp_id.GetLocationID() > num_locations)
1824                {
1825                    StreamString id_str;
1826                    BreakpointID::GetCanonicalReference (&id_str,
1827                                                         cur_bp_id.GetBreakpointID(),
1828                                                         cur_bp_id.GetLocationID());
1829                    i = valid_ids->GetSize() + 1;
1830                    result.AppendErrorWithFormat ("'%s' is not a currently valid breakpoint/location id.\n",
1831                                                 id_str.GetData());
1832                    result.SetStatus (eReturnStatusFailed);
1833                }
1834            }
1835            else
1836            {
1837                i = valid_ids->GetSize() + 1;
1838                result.AppendErrorWithFormat ("'%d' is not a currently valid breakpoint id.\n", cur_bp_id.GetBreakpointID());
1839                result.SetStatus (eReturnStatusFailed);
1840            }
1841        }
1842    }
1843}
1844