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