xref: /freebsd-src/contrib/llvm-project/lldb/source/Breakpoint/BreakpointID.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
15ffd83dbSDimitry Andric //===-- BreakpointID.cpp --------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
9fe6060f1SDimitry Andric #include <cstdio>
10*bdd1243dSDimitry Andric #include <optional>
110b57cec5SDimitry Andric 
120b57cec5SDimitry Andric #include "lldb/Breakpoint/Breakpoint.h"
130b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointID.h"
140b57cec5SDimitry Andric #include "lldb/Utility/Status.h"
150b57cec5SDimitry Andric #include "lldb/Utility/Stream.h"
160b57cec5SDimitry Andric 
170b57cec5SDimitry Andric using namespace lldb;
180b57cec5SDimitry Andric using namespace lldb_private;
190b57cec5SDimitry Andric 
BreakpointID(break_id_t bp_id,break_id_t loc_id)200b57cec5SDimitry Andric BreakpointID::BreakpointID(break_id_t bp_id, break_id_t loc_id)
210b57cec5SDimitry Andric     : m_break_id(bp_id), m_location_id(loc_id) {}
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric BreakpointID::~BreakpointID() = default;
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric static llvm::StringRef g_range_specifiers[] = {"-", "to", "To", "TO"};
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric // Tells whether or not STR is valid to use between two strings representing
280b57cec5SDimitry Andric // breakpoint IDs, to indicate a range of breakpoint IDs.  This is broken out
290b57cec5SDimitry Andric // into a separate function so that we can easily change or add to the format
300b57cec5SDimitry Andric // for specifying ID ranges at a later date.
310b57cec5SDimitry Andric 
IsRangeIdentifier(llvm::StringRef str)320b57cec5SDimitry Andric bool BreakpointID::IsRangeIdentifier(llvm::StringRef str) {
33349cc55cSDimitry Andric   return llvm::is_contained(g_range_specifiers, str);
340b57cec5SDimitry Andric }
350b57cec5SDimitry Andric 
IsValidIDExpression(llvm::StringRef str)360b57cec5SDimitry Andric bool BreakpointID::IsValidIDExpression(llvm::StringRef str) {
3781ad6265SDimitry Andric   return BreakpointID::ParseCanonicalReference(str).has_value();
380b57cec5SDimitry Andric }
390b57cec5SDimitry Andric 
GetRangeSpecifiers()400b57cec5SDimitry Andric llvm::ArrayRef<llvm::StringRef> BreakpointID::GetRangeSpecifiers() {
41*bdd1243dSDimitry Andric   return llvm::ArrayRef(g_range_specifiers);
420b57cec5SDimitry Andric }
430b57cec5SDimitry Andric 
GetDescription(Stream * s,lldb::DescriptionLevel level)440b57cec5SDimitry Andric void BreakpointID::GetDescription(Stream *s, lldb::DescriptionLevel level) {
450b57cec5SDimitry Andric   if (level == eDescriptionLevelVerbose)
460b57cec5SDimitry Andric     s->Printf("%p BreakpointID:", static_cast<void *>(this));
470b57cec5SDimitry Andric 
480b57cec5SDimitry Andric   if (m_break_id == LLDB_INVALID_BREAK_ID)
490b57cec5SDimitry Andric     s->PutCString("<invalid>");
500b57cec5SDimitry Andric   else if (m_location_id == LLDB_INVALID_BREAK_ID)
510b57cec5SDimitry Andric     s->Printf("%i", m_break_id);
520b57cec5SDimitry Andric   else
530b57cec5SDimitry Andric     s->Printf("%i.%i", m_break_id, m_location_id);
540b57cec5SDimitry Andric }
550b57cec5SDimitry Andric 
GetCanonicalReference(Stream * s,break_id_t bp_id,break_id_t loc_id)560b57cec5SDimitry Andric void BreakpointID::GetCanonicalReference(Stream *s, break_id_t bp_id,
570b57cec5SDimitry Andric                                          break_id_t loc_id) {
580b57cec5SDimitry Andric   if (bp_id == LLDB_INVALID_BREAK_ID)
590b57cec5SDimitry Andric     s->PutCString("<invalid>");
600b57cec5SDimitry Andric   else if (loc_id == LLDB_INVALID_BREAK_ID)
610b57cec5SDimitry Andric     s->Printf("%i", bp_id);
620b57cec5SDimitry Andric   else
630b57cec5SDimitry Andric     s->Printf("%i.%i", bp_id, loc_id);
640b57cec5SDimitry Andric }
650b57cec5SDimitry Andric 
66*bdd1243dSDimitry Andric std::optional<BreakpointID>
ParseCanonicalReference(llvm::StringRef input)670b57cec5SDimitry Andric BreakpointID::ParseCanonicalReference(llvm::StringRef input) {
680b57cec5SDimitry Andric   break_id_t bp_id;
690b57cec5SDimitry Andric   break_id_t loc_id = LLDB_INVALID_BREAK_ID;
700b57cec5SDimitry Andric 
710b57cec5SDimitry Andric   if (input.empty())
72*bdd1243dSDimitry Andric     return std::nullopt;
730b57cec5SDimitry Andric 
740b57cec5SDimitry Andric   // If it doesn't start with an integer, it's not valid.
750b57cec5SDimitry Andric   if (input.consumeInteger(0, bp_id))
76*bdd1243dSDimitry Andric     return std::nullopt;
770b57cec5SDimitry Andric 
780b57cec5SDimitry Andric   // period is optional, but if it exists, it must be followed by a number.
790b57cec5SDimitry Andric   if (input.consume_front(".")) {
800b57cec5SDimitry Andric     if (input.consumeInteger(0, loc_id))
81*bdd1243dSDimitry Andric       return std::nullopt;
820b57cec5SDimitry Andric   }
830b57cec5SDimitry Andric 
840b57cec5SDimitry Andric   // And at the end, the entire string must have been consumed.
850b57cec5SDimitry Andric   if (!input.empty())
86*bdd1243dSDimitry Andric     return std::nullopt;
870b57cec5SDimitry Andric 
880b57cec5SDimitry Andric   return BreakpointID(bp_id, loc_id);
890b57cec5SDimitry Andric }
900b57cec5SDimitry Andric 
StringIsBreakpointName(llvm::StringRef str,Status & error)910b57cec5SDimitry Andric bool BreakpointID::StringIsBreakpointName(llvm::StringRef str, Status &error) {
920b57cec5SDimitry Andric   error.Clear();
930b57cec5SDimitry Andric   if (str.empty())
940b57cec5SDimitry Andric   {
95e8d8bef9SDimitry Andric     error.SetErrorString("Empty breakpoint names are not allowed");
960b57cec5SDimitry Andric     return false;
970b57cec5SDimitry Andric   }
980b57cec5SDimitry Andric 
990b57cec5SDimitry Andric   // First character must be a letter or _
1000b57cec5SDimitry Andric   if (!isalpha(str[0]) && str[0] != '_')
1010b57cec5SDimitry Andric   {
1020b57cec5SDimitry Andric     error.SetErrorStringWithFormat("Breakpoint names must start with a "
1030b57cec5SDimitry Andric                                    "character or underscore: %s",
1040b57cec5SDimitry Andric                                    str.str().c_str());
1050b57cec5SDimitry Andric     return false;
1060b57cec5SDimitry Andric   }
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric   // Cannot contain ., -, or space.
1090b57cec5SDimitry Andric   if (str.find_first_of(".- ") != llvm::StringRef::npos) {
1100b57cec5SDimitry Andric     error.SetErrorStringWithFormat("Breakpoint names cannot contain "
1115ffd83dbSDimitry Andric                                    "'.' or '-' or spaces: \"%s\"",
1120b57cec5SDimitry Andric                                    str.str().c_str());
1130b57cec5SDimitry Andric     return false;
1140b57cec5SDimitry Andric   }
1150b57cec5SDimitry Andric 
1160b57cec5SDimitry Andric   return true;
1170b57cec5SDimitry Andric }
118