1 //===-- BreakpointID.cpp ----------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // C Includes
11 #include <stdio.h>
12 
13 // C++ Includes
14 // Other libraries and framework includes
15 // Project includes
16 #include "lldb/Breakpoint/BreakpointID.h"
17 #include "lldb/Breakpoint/Breakpoint.h"
18 #include "lldb/Core/Stream.h"
19 #include "lldb/Core/Error.h"
20 
21 using namespace lldb;
22 using namespace lldb_private;
23 
24 BreakpointID::BreakpointID (break_id_t bp_id, break_id_t loc_id) :
25     m_break_id (bp_id),
26     m_location_id (loc_id)
27 {
28 }
29 
30 BreakpointID::~BreakpointID() = default;
31 
32 const char *BreakpointID::g_range_specifiers[] = { "-", "to", "To", "TO", nullptr };
33 
34 // Tells whether or not STR is valid to use between two strings representing breakpoint IDs, to
35 // indicate a range of breakpoint IDs.  This is broken out into a separate function so that we can
36 // easily change or add to the format for specifying ID ranges at a later date.
37 
38 bool
39 BreakpointID::IsRangeIdentifier (const char *str)
40 {
41     int specifier_count = 0;
42     for (int i = 0; g_range_specifiers[i] != nullptr; ++i)
43       ++specifier_count;
44 
45     for (int i = 0; i < specifier_count; ++i)
46     {
47       if (strcmp (g_range_specifiers[i], str) == 0)
48             return true;
49     }
50 
51   return false;
52 }
53 
54 bool
55 BreakpointID::IsValidIDExpression (const char *str)
56 {
57     break_id_t bp_id;
58     break_id_t loc_id;
59     BreakpointID::ParseCanonicalReference (str, &bp_id, &loc_id);
60 
61     return (bp_id != LLDB_INVALID_BREAK_ID);
62 }
63 
64 void
65 BreakpointID::GetDescription (Stream *s, lldb::DescriptionLevel level)
66 {
67     if (level == eDescriptionLevelVerbose)
68         s->Printf("%p BreakpointID:", static_cast<void*>(this));
69 
70     if (m_break_id == LLDB_INVALID_BREAK_ID)
71         s->PutCString ("<invalid>");
72     else if (m_location_id == LLDB_INVALID_BREAK_ID)
73         s->Printf("%i", m_break_id);
74     else
75         s->Printf("%i.%i", m_break_id, m_location_id);
76 }
77 
78 void
79 BreakpointID::GetCanonicalReference (Stream *s, break_id_t bp_id, break_id_t loc_id)
80 {
81     if (bp_id == LLDB_INVALID_BREAK_ID)
82         s->PutCString ("<invalid>");
83     else if (loc_id == LLDB_INVALID_BREAK_ID)
84         s->Printf("%i", bp_id);
85     else
86         s->Printf("%i.%i", bp_id, loc_id);
87 }
88 
89 bool
90 BreakpointID::ParseCanonicalReference (const char *input, break_id_t *break_id_ptr, break_id_t *break_loc_id_ptr)
91 {
92     *break_id_ptr = LLDB_INVALID_BREAK_ID;
93     *break_loc_id_ptr = LLDB_INVALID_BREAK_ID;
94 
95     if (input == nullptr || *input == '\0')
96         return false;
97 
98     const char *format = "%i%n.%i%n";
99     int chars_consumed_1 = 0;
100     int chars_consumed_2 = 0;
101     int n_items_parsed = ::sscanf (input,
102                                    format,
103                                    break_id_ptr,        // %i   parse the breakpoint ID
104                                    &chars_consumed_1,   // %n   gets the number of characters parsed so far
105                                    break_loc_id_ptr,    // %i   parse the breakpoint location ID
106                                    &chars_consumed_2);  // %n   gets the number of characters parsed so far
107 
108     if ((n_items_parsed == 1 && input[chars_consumed_1] == '\0') ||
109         (n_items_parsed == 2 && input[chars_consumed_2] == '\0'))
110         return true;
111 
112     // Badly formatted canonical reference.
113     *break_id_ptr = LLDB_INVALID_BREAK_ID;
114     *break_loc_id_ptr = LLDB_INVALID_BREAK_ID;
115     return false;
116 }
117 
118 bool
119 BreakpointID::StringIsBreakpointName(const char *name, Error &error)
120 {
121     error.Clear();
122 
123     if (name && (name[0] >= 'A' && name[0] <= 'z'))
124     {
125         if (strcspn(name, ".- ") != strlen(name))
126         {
127             error.SetErrorStringWithFormat("invalid breakpoint name: \"%s\"", name);
128         }
129         return true;
130     }
131     else
132         return false;
133 }
134