LLDB  mainline
BreakpointID.cpp
Go to the documentation of this file.
1 //===-- BreakpointID.cpp ----------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include <stdio.h>
10 
13 #include "lldb/Utility/Status.h"
14 #include "lldb/Utility/Stream.h"
15 
16 using namespace lldb;
17 using namespace lldb_private;
18 
19 BreakpointID::BreakpointID(break_id_t bp_id, break_id_t loc_id)
20  : m_break_id(bp_id), m_location_id(loc_id) {}
21 
22 BreakpointID::~BreakpointID() = default;
23 
24 static llvm::StringRef g_range_specifiers[] = {"-", "to", "To", "TO"};
25 
26 // Tells whether or not STR is valid to use between two strings representing
27 // breakpoint IDs, to indicate a range of breakpoint IDs. This is broken out
28 // into a separate function so that we can easily change or add to the format
29 // for specifying ID ranges at a later date.
30 
31 bool BreakpointID::IsRangeIdentifier(llvm::StringRef str) {
32  for (auto spec : g_range_specifiers) {
33  if (spec == str)
34  return true;
35  }
36 
37  return false;
38 }
39 
40 bool BreakpointID::IsValidIDExpression(llvm::StringRef str) {
41  return BreakpointID::ParseCanonicalReference(str).hasValue();
42 }
43 
44 llvm::ArrayRef<llvm::StringRef> BreakpointID::GetRangeSpecifiers() {
45  return llvm::makeArrayRef(g_range_specifiers);
46 }
47 
49  if (level == eDescriptionLevelVerbose)
50  s->Printf("%p BreakpointID:", static_cast<void *>(this));
51 
53  s->PutCString("<invalid>");
55  s->Printf("%i", m_break_id);
56  else
57  s->Printf("%i.%i", m_break_id, m_location_id);
58 }
59 
61  break_id_t loc_id) {
62  if (bp_id == LLDB_INVALID_BREAK_ID)
63  s->PutCString("<invalid>");
64  else if (loc_id == LLDB_INVALID_BREAK_ID)
65  s->Printf("%i", bp_id);
66  else
67  s->Printf("%i.%i", bp_id, loc_id);
68 }
69 
70 llvm::Optional<BreakpointID>
71 BreakpointID::ParseCanonicalReference(llvm::StringRef input) {
72  break_id_t bp_id;
74 
75  if (input.empty())
76  return llvm::None;
77 
78  // If it doesn't start with an integer, it's not valid.
79  if (input.consumeInteger(0, bp_id))
80  return llvm::None;
81 
82  // period is optional, but if it exists, it must be followed by a number.
83  if (input.consume_front(".")) {
84  if (input.consumeInteger(0, loc_id))
85  return llvm::None;
86  }
87 
88  // And at the end, the entire string must have been consumed.
89  if (!input.empty())
90  return llvm::None;
91 
92  return BreakpointID(bp_id, loc_id);
93 }
94 
95 bool BreakpointID::StringIsBreakpointName(llvm::StringRef str, Status &error) {
96  error.Clear();
97  if (str.empty())
98  {
99  error.SetErrorStringWithFormat("Empty breakpoint names are not allowed");
100  return false;
101  }
102 
103  // First character must be a letter or _
104  if (!isalpha(str[0]) && str[0] != '_')
105  {
106  error.SetErrorStringWithFormat("Breakpoint names must start with a "
107  "character or underscore: %s",
108  str.str().c_str());
109  return false;
110  }
111 
112  // Cannot contain ., -, or space.
113  if (str.find_first_of(".- ") != llvm::StringRef::npos) {
114  error.SetErrorStringWithFormat("Breakpoint names cannot contain "
115  "'.' or '-': \"%s\"",
116  str.str().c_str());
117  return false;
118  }
119 
120  return true;
121 }
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:61
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
int32_t break_id_t
Definition: lldb-types.h:88
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
static bool IsRangeIdentifier(llvm::StringRef str)
static bool IsValidIDExpression(llvm::StringRef str)
static void GetCanonicalReference(Stream *s, lldb::break_id_t break_id, lldb::break_id_t break_loc_id)
Takes a breakpoint ID and the breakpoint location id and returns a string containing the canonical de...
lldb::break_id_t m_break_id
Definition: BreakpointID.h:92
void Clear()
Clear the object state.
Definition: Status.cpp:167
static llvm::Optional< BreakpointID > ParseCanonicalReference(llvm::StringRef input)
Takes an input string containing the description of a breakpoint or breakpoint and location and retur...
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:106
void GetDescription(Stream *s, lldb::DescriptionLevel level)
static llvm::ArrayRef< llvm::StringRef > GetRangeSpecifiers()
lldb::break_id_t m_location_id
Definition: BreakpointID.h:93
Definition: SBAddress.h:15
BreakpointID(lldb::break_id_t bp_id=LLDB_INVALID_BREAK_ID, lldb::break_id_t loc_id=LLDB_INVALID_BREAK_ID)
static llvm::StringRef g_range_specifiers[]
#define LLDB_INVALID_BREAK_ID
Definition: lldb-defines.h:49
int SetErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Set the current error string to a formatted error string.
Definition: Status.cpp:255
static bool StringIsBreakpointName(llvm::StringRef str, Status &error)
Takes an input string and checks to see whether it is a breakpoint name.
An error handling class.
Definition: Status.h:44