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 #include <stdio.h>
11
12 #include "lldb/Breakpoint/Breakpoint.h"
13 #include "lldb/Breakpoint/BreakpointID.h"
14 #include "lldb/Utility/Status.h"
15 #include "lldb/Utility/Stream.h"
16
17 using namespace lldb;
18 using namespace lldb_private;
19
BreakpointID(break_id_t bp_id,break_id_t loc_id)20 BreakpointID::BreakpointID(break_id_t bp_id, break_id_t loc_id)
21 : m_break_id(bp_id), m_location_id(loc_id) {}
22
23 BreakpointID::~BreakpointID() = default;
24
25 static llvm::StringRef g_range_specifiers[] = {"-", "to", "To", "TO"};
26
27 // Tells whether or not STR is valid to use between two strings representing
28 // breakpoint IDs, to indicate a range of breakpoint IDs. This is broken out
29 // into a separate function so that we can easily change or add to the format
30 // for specifying ID ranges at a later date.
31
IsRangeIdentifier(llvm::StringRef str)32 bool BreakpointID::IsRangeIdentifier(llvm::StringRef str) {
33 for (auto spec : g_range_specifiers) {
34 if (spec == str)
35 return true;
36 }
37
38 return false;
39 }
40
IsValidIDExpression(llvm::StringRef str)41 bool BreakpointID::IsValidIDExpression(llvm::StringRef str) {
42 return BreakpointID::ParseCanonicalReference(str).hasValue();
43 }
44
GetRangeSpecifiers()45 llvm::ArrayRef<llvm::StringRef> BreakpointID::GetRangeSpecifiers() {
46 return llvm::makeArrayRef(g_range_specifiers);
47 }
48
GetDescription(Stream * s,lldb::DescriptionLevel level)49 void BreakpointID::GetDescription(Stream *s, lldb::DescriptionLevel level) {
50 if (level == eDescriptionLevelVerbose)
51 s->Printf("%p BreakpointID:", static_cast<void *>(this));
52
53 if (m_break_id == LLDB_INVALID_BREAK_ID)
54 s->PutCString("<invalid>");
55 else if (m_location_id == LLDB_INVALID_BREAK_ID)
56 s->Printf("%i", m_break_id);
57 else
58 s->Printf("%i.%i", m_break_id, m_location_id);
59 }
60
GetCanonicalReference(Stream * s,break_id_t bp_id,break_id_t loc_id)61 void BreakpointID::GetCanonicalReference(Stream *s, break_id_t bp_id,
62 break_id_t loc_id) {
63 if (bp_id == LLDB_INVALID_BREAK_ID)
64 s->PutCString("<invalid>");
65 else if (loc_id == LLDB_INVALID_BREAK_ID)
66 s->Printf("%i", bp_id);
67 else
68 s->Printf("%i.%i", bp_id, loc_id);
69 }
70
71 llvm::Optional<BreakpointID>
ParseCanonicalReference(llvm::StringRef input)72 BreakpointID::ParseCanonicalReference(llvm::StringRef input) {
73 break_id_t bp_id;
74 break_id_t loc_id = LLDB_INVALID_BREAK_ID;
75
76 if (input.empty())
77 return llvm::None;
78
79 // If it doesn't start with an integer, it's not valid.
80 if (input.consumeInteger(0, bp_id))
81 return llvm::None;
82
83 // period is optional, but if it exists, it must be followed by a number.
84 if (input.consume_front(".")) {
85 if (input.consumeInteger(0, loc_id))
86 return llvm::None;
87 }
88
89 // And at the end, the entire string must have been consumed.
90 if (!input.empty())
91 return llvm::None;
92
93 return BreakpointID(bp_id, loc_id);
94 }
95
StringIsBreakpointName(llvm::StringRef str,Status & error)96 bool BreakpointID::StringIsBreakpointName(llvm::StringRef str, Status &error) {
97 error.Clear();
98 if (str.empty())
99 {
100 error.SetErrorStringWithFormat("Empty breakpoint names are not allowed");
101 return false;
102 }
103
104 // First character must be a letter or _
105 if (!isalpha(str[0]) && str[0] != '_')
106 {
107 error.SetErrorStringWithFormat("Breakpoint names must start with a "
108 "character or underscore: %s",
109 str.str().c_str());
110 return false;
111 }
112
113 // Cannot contain ., -, or space.
114 if (str.find_first_of(".- ") != llvm::StringRef::npos) {
115 error.SetErrorStringWithFormat("Breakpoint names cannot contain "
116 "'.' or '-': \"%s\"",
117 str.str().c_str());
118 return false;
119 }
120
121 return true;
122 }
123