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