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