180814287SRaphael Isemann //===-- BreakpointID.cpp --------------------------------------------------===//
230fdc8d8SChris Lattner //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
630fdc8d8SChris Lattner //
730fdc8d8SChris Lattner //===----------------------------------------------------------------------===//
830fdc8d8SChris Lattner
976e47d48SRaphael Isemann #include <cstdio>
10dcbfd19eSGreg Clayton
1130fdc8d8SChris Lattner #include "lldb/Breakpoint/Breakpoint.h"
12b9c1b51eSKate Stone #include "lldb/Breakpoint/BreakpointID.h"
1397206d57SZachary Turner #include "lldb/Utility/Status.h"
14bf9a7730SZachary Turner #include "lldb/Utility/Stream.h"
1530fdc8d8SChris Lattner
1630fdc8d8SChris Lattner using namespace lldb;
1730fdc8d8SChris Lattner using namespace lldb_private;
1830fdc8d8SChris Lattner
BreakpointID(break_id_t bp_id,break_id_t loc_id)19b9c1b51eSKate Stone BreakpointID::BreakpointID(break_id_t bp_id, break_id_t loc_id)
20b9c1b51eSKate Stone : m_break_id(bp_id), m_location_id(loc_id) {}
2130fdc8d8SChris Lattner
2216fd7511SEugene Zelenko BreakpointID::~BreakpointID() = default;
2330fdc8d8SChris Lattner
24401f55dfSZachary Turner static llvm::StringRef g_range_specifiers[] = {"-", "to", "To", "TO"};
2530fdc8d8SChris Lattner
26b9c1b51eSKate Stone // Tells whether or not STR is valid to use between two strings representing
2705097246SAdrian Prantl // breakpoint IDs, to indicate a range of breakpoint IDs. This is broken out
2805097246SAdrian Prantl // into a separate function so that we can easily change or add to the format
2905097246SAdrian Prantl // for specifying ID ranges at a later date.
3030fdc8d8SChris Lattner
IsRangeIdentifier(llvm::StringRef str)31401f55dfSZachary Turner bool BreakpointID::IsRangeIdentifier(llvm::StringRef str) {
327cc8fa2dSKazu Hirata return llvm::is_contained(g_range_specifiers, str);
3330fdc8d8SChris Lattner }
3430fdc8d8SChris Lattner
IsValidIDExpression(llvm::StringRef str)35401f55dfSZachary Turner bool BreakpointID::IsValidIDExpression(llvm::StringRef str) {
36*064a08cdSKazu Hirata return BreakpointID::ParseCanonicalReference(str).has_value();
37401f55dfSZachary Turner }
3830fdc8d8SChris Lattner
GetRangeSpecifiers()39401f55dfSZachary Turner llvm::ArrayRef<llvm::StringRef> BreakpointID::GetRangeSpecifiers() {
400debabb3SZachary Turner return llvm::makeArrayRef(g_range_specifiers);
4130fdc8d8SChris Lattner }
4230fdc8d8SChris Lattner
GetDescription(Stream * s,lldb::DescriptionLevel level)43b9c1b51eSKate Stone void BreakpointID::GetDescription(Stream *s, lldb::DescriptionLevel level) {
4430fdc8d8SChris Lattner if (level == eDescriptionLevelVerbose)
45324a1036SSaleem Abdulrasool s->Printf("%p BreakpointID:", static_cast<void *>(this));
4630fdc8d8SChris Lattner
4730fdc8d8SChris Lattner if (m_break_id == LLDB_INVALID_BREAK_ID)
4830fdc8d8SChris Lattner s->PutCString("<invalid>");
4930fdc8d8SChris Lattner else if (m_location_id == LLDB_INVALID_BREAK_ID)
5030fdc8d8SChris Lattner s->Printf("%i", m_break_id);
5130fdc8d8SChris Lattner else
5230fdc8d8SChris Lattner s->Printf("%i.%i", m_break_id, m_location_id);
5330fdc8d8SChris Lattner }
5430fdc8d8SChris Lattner
GetCanonicalReference(Stream * s,break_id_t bp_id,break_id_t loc_id)55b9c1b51eSKate Stone void BreakpointID::GetCanonicalReference(Stream *s, break_id_t bp_id,
56b9c1b51eSKate Stone break_id_t loc_id) {
5730fdc8d8SChris Lattner if (bp_id == LLDB_INVALID_BREAK_ID)
5830fdc8d8SChris Lattner s->PutCString("<invalid>");
5930fdc8d8SChris Lattner else if (loc_id == LLDB_INVALID_BREAK_ID)
6030fdc8d8SChris Lattner s->Printf("%i", bp_id);
6130fdc8d8SChris Lattner else
6230fdc8d8SChris Lattner s->Printf("%i.%i", bp_id, loc_id);
6330fdc8d8SChris Lattner }
6430fdc8d8SChris Lattner
65401f55dfSZachary Turner llvm::Optional<BreakpointID>
ParseCanonicalReference(llvm::StringRef input)66401f55dfSZachary Turner BreakpointID::ParseCanonicalReference(llvm::StringRef input) {
67401f55dfSZachary Turner break_id_t bp_id;
68401f55dfSZachary Turner break_id_t loc_id = LLDB_INVALID_BREAK_ID;
6930fdc8d8SChris Lattner
70401f55dfSZachary Turner if (input.empty())
71401f55dfSZachary Turner return llvm::None;
7230fdc8d8SChris Lattner
73401f55dfSZachary Turner // If it doesn't start with an integer, it's not valid.
74401f55dfSZachary Turner if (input.consumeInteger(0, bp_id))
75401f55dfSZachary Turner return llvm::None;
7630fdc8d8SChris Lattner
77401f55dfSZachary Turner // period is optional, but if it exists, it must be followed by a number.
78401f55dfSZachary Turner if (input.consume_front(".")) {
79401f55dfSZachary Turner if (input.consumeInteger(0, loc_id))
80401f55dfSZachary Turner return llvm::None;
81401f55dfSZachary Turner }
8230fdc8d8SChris Lattner
83401f55dfSZachary Turner // And at the end, the entire string must have been consumed.
84401f55dfSZachary Turner if (!input.empty())
85401f55dfSZachary Turner return llvm::None;
86401f55dfSZachary Turner
87401f55dfSZachary Turner return BreakpointID(bp_id, loc_id);
8830fdc8d8SChris Lattner }
8930fdc8d8SChris Lattner
StringIsBreakpointName(llvm::StringRef str,Status & error)9097206d57SZachary Turner bool BreakpointID::StringIsBreakpointName(llvm::StringRef str, Status &error) {
915e09c8c3SJim Ingham error.Clear();
926fa7681bSZachary Turner if (str.empty())
93b842f2ecSJim Ingham {
9489533764SJonas Devlieghere error.SetErrorString("Empty breakpoint names are not allowed");
955e09c8c3SJim Ingham return false;
96b842f2ecSJim Ingham }
976fa7681bSZachary Turner
986fa7681bSZachary Turner // First character must be a letter or _
990ab39359SZachary Turner if (!isalpha(str[0]) && str[0] != '_')
100b842f2ecSJim Ingham {
101b842f2ecSJim Ingham error.SetErrorStringWithFormat("Breakpoint names must start with a "
102b842f2ecSJim Ingham "character or underscore: %s",
103b842f2ecSJim Ingham str.str().c_str());
1046fa7681bSZachary Turner return false;
105b842f2ecSJim Ingham }
1066fa7681bSZachary Turner
1076fa7681bSZachary Turner // Cannot contain ., -, or space.
1086fa7681bSZachary Turner if (str.find_first_of(".- ") != llvm::StringRef::npos) {
109b842f2ecSJim Ingham error.SetErrorStringWithFormat("Breakpoint names cannot contain "
11077943269SRaphael Isemann "'.' or '-' or spaces: \"%s\"",
1116fa7681bSZachary Turner str.str().c_str());
1126fa7681bSZachary Turner return false;
1136fa7681bSZachary Turner }
1146fa7681bSZachary Turner
1156fa7681bSZachary Turner return true;
1165e09c8c3SJim Ingham }
117