LLDB  mainline
BreakpointID.cpp
Go to the documentation of this file.
1 //===-- BreakpointID.cpp --------------------------------------------------===//
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 <cstdio>
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  return llvm::is_contained(g_range_specifiers, str);
33 }
34 
35 bool BreakpointID::IsValidIDExpression(llvm::StringRef str) {
36  return BreakpointID::ParseCanonicalReference(str).has_value();
37 }
38 
39 llvm::ArrayRef<llvm::StringRef> BreakpointID::GetRangeSpecifiers() {
40  return llvm::makeArrayRef(g_range_specifiers);
41 }
42 
44  if (level == eDescriptionLevelVerbose)
45  s->Printf("%p BreakpointID:", static_cast<void *>(this));
46 
48  s->PutCString("<invalid>");
50  s->Printf("%i", m_break_id);
51  else
52  s->Printf("%i.%i", m_break_id, m_location_id);
53 }
54 
56  break_id_t loc_id) {
57  if (bp_id == LLDB_INVALID_BREAK_ID)
58  s->PutCString("<invalid>");
59  else if (loc_id == LLDB_INVALID_BREAK_ID)
60  s->Printf("%i", bp_id);
61  else
62  s->Printf("%i.%i", bp_id, loc_id);
63 }
64 
65 llvm::Optional<BreakpointID>
66 BreakpointID::ParseCanonicalReference(llvm::StringRef input) {
67  break_id_t bp_id;
69 
70  if (input.empty())
71  return llvm::None;
72 
73  // If it doesn't start with an integer, it's not valid.
74  if (input.consumeInteger(0, bp_id))
75  return llvm::None;
76 
77  // period is optional, but if it exists, it must be followed by a number.
78  if (input.consume_front(".")) {
79  if (input.consumeInteger(0, loc_id))
80  return llvm::None;
81  }
82 
83  // And at the end, the entire string must have been consumed.
84  if (!input.empty())
85  return llvm::None;
86 
87  return BreakpointID(bp_id, loc_id);
88 }
89 
90 bool BreakpointID::StringIsBreakpointName(llvm::StringRef str, Status &error) {
91  error.Clear();
92  if (str.empty())
93  {
94  error.SetErrorString("Empty breakpoint names are not allowed");
95  return false;
96  }
97 
98  // First character must be a letter or _
99  if (!isalpha(str[0]) && str[0] != '_')
100  {
101  error.SetErrorStringWithFormat("Breakpoint names must start with a "
102  "character or underscore: %s",
103  str.str().c_str());
104  return false;
105  }
106 
107  // Cannot contain ., -, or space.
108  if (str.find_first_of(".- ") != llvm::StringRef::npos) {
109  error.SetErrorStringWithFormat("Breakpoint names cannot contain "
110  "'.' or '-' or spaces: \"%s\"",
111  str.str().c_str());
112  return false;
113  }
114 
115  return true;
116 }
lldb_private::Stream
Definition: Stream.h:28
lldb_private::BreakpointID::IsRangeIdentifier
static bool IsRangeIdentifier(llvm::StringRef str)
Definition: BreakpointID.cpp:31
lldb_private::BreakpointID::m_break_id
lldb::break_id_t m_break_id
Definition: BreakpointID.h:91
lldb_private::BreakpointID::m_location_id
lldb::break_id_t m_location_id
Definition: BreakpointID.h:92
lldb_private::BreakpointID::~BreakpointID
virtual ~BreakpointID()
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::BreakpointID::BreakpointID
BreakpointID(lldb::break_id_t bp_id=LLDB_INVALID_BREAK_ID, lldb::break_id_t loc_id=LLDB_INVALID_BREAK_ID)
Definition: BreakpointID.cpp:19
lldb_private::BreakpointID::GetDescription
void GetDescription(Stream *s, lldb::DescriptionLevel level)
Definition: BreakpointID.cpp:43
lldb_private::BreakpointID::GetRangeSpecifiers
static llvm::ArrayRef< llvm::StringRef > GetRangeSpecifiers()
Definition: BreakpointID.cpp:39
lldb_private::BreakpointID::GetCanonicalReference
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...
Definition: BreakpointID.cpp:55
lldb_private::BreakpointID::ParseCanonicalReference
static llvm::Optional< BreakpointID > ParseCanonicalReference(llvm::StringRef input)
Takes an input string containing the description of a breakpoint or breakpoint and location and retur...
Definition: BreakpointID.cpp:66
lldb_private::BreakpointID::StringIsBreakpointName
static bool StringIsBreakpointName(llvm::StringRef str, Status &error)
Takes an input string and checks to see whether it is a breakpoint name.
Definition: BreakpointID.cpp:90
lldb::break_id_t
int32_t break_id_t
Definition: lldb-types.h:88
g_range_specifiers
static llvm::StringRef g_range_specifiers[]
Definition: BreakpointID.cpp:24
LLDB_INVALID_BREAK_ID
#define LLDB_INVALID_BREAK_ID
Definition: lldb-defines.h:37
lldb_private::Status
Definition: Status.h:44
BreakpointID.h
lldb_private::Stream::Printf
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
Status.h
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb::eDescriptionLevelVerbose
@ eDescriptionLevelVerbose
Definition: lldb-enumerations.h:210
Breakpoint.h
Stream.h
lldb_private::BreakpointID::IsValidIDExpression
static bool IsValidIDExpression(llvm::StringRef str)
Definition: BreakpointID.cpp:35
lldb_private::Stream::PutCString
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:63
lldb
Definition: SBAddress.h:15
lldb::DescriptionLevel
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
Definition: lldb-enumerations.h:207