xref: /src/contrib/llvm-project/lldb/source/Commands/CommandOptionArgumentTable.cpp (revision fcaf7f8644a9988098ac6be2165bce3ea4786e91)
14b4fe385SDimitry Andric //===-- CommandOptionArgumentTable.cpp ------------------------------------===//
24b4fe385SDimitry Andric //
34b4fe385SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44b4fe385SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
54b4fe385SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
64b4fe385SDimitry Andric //
74b4fe385SDimitry Andric //===----------------------------------------------------------------------===//
84b4fe385SDimitry Andric 
94b4fe385SDimitry Andric #include "lldb/Interpreter/CommandOptionArgumentTable.h"
104b4fe385SDimitry Andric #include "lldb/DataFormatters/FormatManager.h"
114b4fe385SDimitry Andric #include "lldb/Target/Language.h"
124b4fe385SDimitry Andric #include "lldb/Utility/StreamString.h"
134b4fe385SDimitry Andric 
144b4fe385SDimitry Andric using namespace lldb;
154b4fe385SDimitry Andric using namespace lldb_private;
164b4fe385SDimitry Andric 
174b4fe385SDimitry Andric namespace lldb_private {
RegisterNameHelpTextCallback()184b4fe385SDimitry Andric llvm::StringRef RegisterNameHelpTextCallback() {
194b4fe385SDimitry Andric   return "Register names can be specified using the architecture specific "
204b4fe385SDimitry Andric          "names.  "
214b4fe385SDimitry Andric          "They can also be specified using generic names.  Not all generic "
224b4fe385SDimitry Andric          "entities have "
234b4fe385SDimitry Andric          "registers backing them on all architectures.  When they don't the "
244b4fe385SDimitry Andric          "generic name "
254b4fe385SDimitry Andric          "will return an error.\n"
264b4fe385SDimitry Andric          "The generic names defined in lldb are:\n"
274b4fe385SDimitry Andric          "\n"
284b4fe385SDimitry Andric          "pc       - program counter register\n"
294b4fe385SDimitry Andric          "ra       - return address register\n"
304b4fe385SDimitry Andric          "fp       - frame pointer register\n"
314b4fe385SDimitry Andric          "sp       - stack pointer register\n"
324b4fe385SDimitry Andric          "flags    - the flags register\n"
334b4fe385SDimitry Andric          "arg{1-6} - integer argument passing registers.\n";
344b4fe385SDimitry Andric }
354b4fe385SDimitry Andric 
BreakpointIDHelpTextCallback()364b4fe385SDimitry Andric llvm::StringRef BreakpointIDHelpTextCallback() {
374b4fe385SDimitry Andric   return "Breakpoints are identified using major and minor numbers; the major "
384b4fe385SDimitry Andric          "number corresponds to the single entity that was created with a "
394b4fe385SDimitry Andric          "'breakpoint "
404b4fe385SDimitry Andric          "set' command; the minor numbers correspond to all the locations that "
414b4fe385SDimitry Andric          "were "
424b4fe385SDimitry Andric          "actually found/set based on the major breakpoint.  A full breakpoint "
434b4fe385SDimitry Andric          "ID might "
444b4fe385SDimitry Andric          "look like 3.14, meaning the 14th location set for the 3rd "
454b4fe385SDimitry Andric          "breakpoint.  You "
464b4fe385SDimitry Andric          "can specify all the locations of a breakpoint by just indicating the "
474b4fe385SDimitry Andric          "major "
484b4fe385SDimitry Andric          "breakpoint number. A valid breakpoint ID consists either of just the "
494b4fe385SDimitry Andric          "major "
504b4fe385SDimitry Andric          "number, or the major number followed by a dot and the location "
514b4fe385SDimitry Andric          "number (e.g. "
524b4fe385SDimitry Andric          "3 or 3.2 could both be valid breakpoint IDs.)";
534b4fe385SDimitry Andric }
544b4fe385SDimitry Andric 
BreakpointIDRangeHelpTextCallback()554b4fe385SDimitry Andric llvm::StringRef BreakpointIDRangeHelpTextCallback() {
564b4fe385SDimitry Andric   return "A 'breakpoint ID list' is a manner of specifying multiple "
574b4fe385SDimitry Andric          "breakpoints. "
584b4fe385SDimitry Andric          "This can be done through several mechanisms.  The easiest way is to "
594b4fe385SDimitry Andric          "just "
604b4fe385SDimitry Andric          "enter a space-separated list of breakpoint IDs.  To specify all the "
614b4fe385SDimitry Andric          "breakpoint locations under a major breakpoint, you can use the major "
624b4fe385SDimitry Andric          "breakpoint number followed by '.*', eg. '5.*' means all the "
634b4fe385SDimitry Andric          "locations under "
644b4fe385SDimitry Andric          "breakpoint 5.  You can also indicate a range of breakpoints by using "
654b4fe385SDimitry Andric          "<start-bp-id> - <end-bp-id>.  The start-bp-id and end-bp-id for a "
664b4fe385SDimitry Andric          "range can "
674b4fe385SDimitry Andric          "be any valid breakpoint IDs.  It is not legal, however, to specify a "
684b4fe385SDimitry Andric          "range "
694b4fe385SDimitry Andric          "using specific locations that cross major breakpoint numbers.  I.e. "
704b4fe385SDimitry Andric          "3.2 - 3.7"
714b4fe385SDimitry Andric          " is legal; 2 - 5 is legal; but 3.2 - 4.4 is not legal.";
724b4fe385SDimitry Andric }
734b4fe385SDimitry Andric 
BreakpointNameHelpTextCallback()744b4fe385SDimitry Andric llvm::StringRef BreakpointNameHelpTextCallback() {
754b4fe385SDimitry Andric   return "A name that can be added to a breakpoint when it is created, or "
764b4fe385SDimitry Andric          "later "
774b4fe385SDimitry Andric          "on with the \"breakpoint name add\" command.  "
784b4fe385SDimitry Andric          "Breakpoint names can be used to specify breakpoints in all the "
794b4fe385SDimitry Andric          "places breakpoint IDs "
804b4fe385SDimitry Andric          "and breakpoint ID ranges can be used.  As such they provide a "
814b4fe385SDimitry Andric          "convenient way to group breakpoints, "
824b4fe385SDimitry Andric          "and to operate on breakpoints you create without having to track the "
834b4fe385SDimitry Andric          "breakpoint number.  "
844b4fe385SDimitry Andric          "Note, the attributes you set when using a breakpoint name in a "
854b4fe385SDimitry Andric          "breakpoint command don't "
864b4fe385SDimitry Andric          "adhere to the name, but instead are set individually on all the "
874b4fe385SDimitry Andric          "breakpoints currently tagged with that "
884b4fe385SDimitry Andric          "name.  Future breakpoints "
894b4fe385SDimitry Andric          "tagged with that name will not pick up the attributes previously "
904b4fe385SDimitry Andric          "given using that name.  "
914b4fe385SDimitry Andric          "In order to distinguish breakpoint names from breakpoint IDs and "
924b4fe385SDimitry Andric          "ranges, "
934b4fe385SDimitry Andric          "names must start with a letter from a-z or A-Z and cannot contain "
944b4fe385SDimitry Andric          "spaces, \".\" or \"-\".  "
954b4fe385SDimitry Andric          "Also, breakpoint names can only be applied to breakpoints, not to "
964b4fe385SDimitry Andric          "breakpoint locations.";
974b4fe385SDimitry Andric }
984b4fe385SDimitry Andric 
GDBFormatHelpTextCallback()994b4fe385SDimitry Andric llvm::StringRef GDBFormatHelpTextCallback() {
1004b4fe385SDimitry Andric   return "A GDB format consists of a repeat count, a format letter and a size "
1014b4fe385SDimitry Andric          "letter. "
1024b4fe385SDimitry Andric          "The repeat count is optional and defaults to 1. The format letter is "
1034b4fe385SDimitry Andric          "optional "
1044b4fe385SDimitry Andric          "and defaults to the previous format that was used. The size letter "
1054b4fe385SDimitry Andric          "is optional "
1064b4fe385SDimitry Andric          "and defaults to the previous size that was used.\n"
1074b4fe385SDimitry Andric          "\n"
1084b4fe385SDimitry Andric          "Format letters include:\n"
1094b4fe385SDimitry Andric          "o - octal\n"
1104b4fe385SDimitry Andric          "x - hexadecimal\n"
1114b4fe385SDimitry Andric          "d - decimal\n"
1124b4fe385SDimitry Andric          "u - unsigned decimal\n"
1134b4fe385SDimitry Andric          "t - binary\n"
1144b4fe385SDimitry Andric          "f - float\n"
1154b4fe385SDimitry Andric          "a - address\n"
1164b4fe385SDimitry Andric          "i - instruction\n"
1174b4fe385SDimitry Andric          "c - char\n"
1184b4fe385SDimitry Andric          "s - string\n"
1194b4fe385SDimitry Andric          "T - OSType\n"
1204b4fe385SDimitry Andric          "A - float as hex\n"
1214b4fe385SDimitry Andric          "\n"
1224b4fe385SDimitry Andric          "Size letters include:\n"
1234b4fe385SDimitry Andric          "b - 1 byte  (byte)\n"
1244b4fe385SDimitry Andric          "h - 2 bytes (halfword)\n"
1254b4fe385SDimitry Andric          "w - 4 bytes (word)\n"
1264b4fe385SDimitry Andric          "g - 8 bytes (giant)\n"
1274b4fe385SDimitry Andric          "\n"
1284b4fe385SDimitry Andric          "Example formats:\n"
1294b4fe385SDimitry Andric          "32xb - show 32 1 byte hexadecimal integer values\n"
1304b4fe385SDimitry Andric          "16xh - show 16 2 byte hexadecimal integer values\n"
1314b4fe385SDimitry Andric          "64   - show 64 2 byte hexadecimal integer values (format and size "
1324b4fe385SDimitry Andric          "from the last format)\n"
1334b4fe385SDimitry Andric          "dw   - show 1 4 byte decimal integer value\n";
1344b4fe385SDimitry Andric }
1354b4fe385SDimitry Andric 
FormatHelpTextCallback()1364b4fe385SDimitry Andric llvm::StringRef FormatHelpTextCallback() {
1374b4fe385SDimitry Andric   static std::string help_text;
1384b4fe385SDimitry Andric 
1394b4fe385SDimitry Andric   if (!help_text.empty())
1404b4fe385SDimitry Andric     return help_text;
1414b4fe385SDimitry Andric 
1424b4fe385SDimitry Andric   StreamString sstr;
1434b4fe385SDimitry Andric   sstr << "One of the format names (or one-character names) that can be used "
1444b4fe385SDimitry Andric           "to show a variable's value:\n";
1454b4fe385SDimitry Andric   for (Format f = eFormatDefault; f < kNumFormats; f = Format(f + 1)) {
1464b4fe385SDimitry Andric     if (f != eFormatDefault)
1474b4fe385SDimitry Andric       sstr.PutChar('\n');
1484b4fe385SDimitry Andric 
1494b4fe385SDimitry Andric     char format_char = FormatManager::GetFormatAsFormatChar(f);
1504b4fe385SDimitry Andric     if (format_char)
1514b4fe385SDimitry Andric       sstr.Printf("'%c' or ", format_char);
1524b4fe385SDimitry Andric 
1534b4fe385SDimitry Andric     sstr.Printf("\"%s\"", FormatManager::GetFormatAsCString(f));
1544b4fe385SDimitry Andric   }
1554b4fe385SDimitry Andric 
1564b4fe385SDimitry Andric   sstr.Flush();
1574b4fe385SDimitry Andric 
1584b4fe385SDimitry Andric   help_text = std::string(sstr.GetString());
1594b4fe385SDimitry Andric 
1604b4fe385SDimitry Andric   return help_text;
1614b4fe385SDimitry Andric }
1624b4fe385SDimitry Andric 
LanguageTypeHelpTextCallback()1634b4fe385SDimitry Andric llvm::StringRef LanguageTypeHelpTextCallback() {
1644b4fe385SDimitry Andric   static std::string help_text;
1654b4fe385SDimitry Andric 
1664b4fe385SDimitry Andric   if (!help_text.empty())
1674b4fe385SDimitry Andric     return help_text;
1684b4fe385SDimitry Andric 
1694b4fe385SDimitry Andric   StreamString sstr;
1704b4fe385SDimitry Andric   sstr << "One of the following languages:\n";
1714b4fe385SDimitry Andric 
1724b4fe385SDimitry Andric   Language::PrintAllLanguages(sstr, "  ", "\n");
1734b4fe385SDimitry Andric 
1744b4fe385SDimitry Andric   sstr.Flush();
1754b4fe385SDimitry Andric 
1764b4fe385SDimitry Andric   help_text = std::string(sstr.GetString());
1774b4fe385SDimitry Andric 
1784b4fe385SDimitry Andric   return help_text;
1794b4fe385SDimitry Andric }
1804b4fe385SDimitry Andric 
SummaryStringHelpTextCallback()1814b4fe385SDimitry Andric llvm::StringRef SummaryStringHelpTextCallback() {
1824b4fe385SDimitry Andric   return "A summary string is a way to extract information from variables in "
1834b4fe385SDimitry Andric          "order to present them using a summary.\n"
1844b4fe385SDimitry Andric          "Summary strings contain static text, variables, scopes and control "
1854b4fe385SDimitry Andric          "sequences:\n"
1864b4fe385SDimitry Andric          "  - Static text can be any sequence of non-special characters, i.e. "
1874b4fe385SDimitry Andric          "anything but '{', '}', '$', or '\\'.\n"
1884b4fe385SDimitry Andric          "  - Variables are sequences of characters beginning with ${, ending "
1894b4fe385SDimitry Andric          "with } and that contain symbols in the format described below.\n"
1904b4fe385SDimitry Andric          "  - Scopes are any sequence of text between { and }. Anything "
1914b4fe385SDimitry Andric          "included in a scope will only appear in the output summary if there "
1924b4fe385SDimitry Andric          "were no errors.\n"
1934b4fe385SDimitry Andric          "  - Control sequences are the usual C/C++ '\\a', '\\n', ..., plus "
1944b4fe385SDimitry Andric          "'\\$', '\\{' and '\\}'.\n"
1954b4fe385SDimitry Andric          "A summary string works by copying static text verbatim, turning "
1964b4fe385SDimitry Andric          "control sequences into their character counterpart, expanding "
1974b4fe385SDimitry Andric          "variables and trying to expand scopes.\n"
1984b4fe385SDimitry Andric          "A variable is expanded by giving it a value other than its textual "
1994b4fe385SDimitry Andric          "representation, and the way this is done depends on what comes after "
2004b4fe385SDimitry Andric          "the ${ marker.\n"
2014b4fe385SDimitry Andric          "The most common sequence if ${var followed by an expression path, "
2024b4fe385SDimitry Andric          "which is the text one would type to access a member of an aggregate "
2034b4fe385SDimitry Andric          "types, given a variable of that type"
2044b4fe385SDimitry Andric          " (e.g. if type T has a member named x, which has a member named y, "
2054b4fe385SDimitry Andric          "and if t is of type T, the expression path would be .x.y and the way "
2064b4fe385SDimitry Andric          "to fit that into a summary string would be"
2074b4fe385SDimitry Andric          " ${var.x.y}). You can also use ${*var followed by an expression path "
2084b4fe385SDimitry Andric          "and in that case the object referred by the path will be "
2094b4fe385SDimitry Andric          "dereferenced before being displayed."
2104b4fe385SDimitry Andric          " If the object is not a pointer, doing so will cause an error. For "
2114b4fe385SDimitry Andric          "additional details on expression paths, you can type 'help "
2124b4fe385SDimitry Andric          "expr-path'. \n"
2134b4fe385SDimitry Andric          "By default, summary strings attempt to display the summary for any "
2144b4fe385SDimitry Andric          "variable they reference, and if that fails the value. If neither can "
2154b4fe385SDimitry Andric          "be shown, nothing is displayed."
2164b4fe385SDimitry Andric          "In a summary string, you can also use an array index [n], or a "
2174b4fe385SDimitry Andric          "slice-like range [n-m]. This can have two different meanings "
2184b4fe385SDimitry Andric          "depending on what kind of object the expression"
2194b4fe385SDimitry Andric          " path refers to:\n"
2204b4fe385SDimitry Andric          "  - if it is a scalar type (any basic type like int, float, ...) the "
2214b4fe385SDimitry Andric          "expression is a bitfield, i.e. the bits indicated by the indexing "
2224b4fe385SDimitry Andric          "operator are extracted out of the number"
2234b4fe385SDimitry Andric          " and displayed as an individual variable\n"
2244b4fe385SDimitry Andric          "  - if it is an array or pointer the array items indicated by the "
2254b4fe385SDimitry Andric          "indexing operator are shown as the result of the variable. if the "
2264b4fe385SDimitry Andric          "expression is an array, real array items are"
2274b4fe385SDimitry Andric          " printed; if it is a pointer, the pointer-as-array syntax is used to "
2284b4fe385SDimitry Andric          "obtain the values (this means, the latter case can have no range "
2294b4fe385SDimitry Andric          "checking)\n"
2304b4fe385SDimitry Andric          "If you are trying to display an array for which the size is known, "
2314b4fe385SDimitry Andric          "you can also use [] instead of giving an exact range. This has the "
2324b4fe385SDimitry Andric          "effect of showing items 0 thru size - 1.\n"
2334b4fe385SDimitry Andric          "Additionally, a variable can contain an (optional) format code, as "
2344b4fe385SDimitry Andric          "in ${var.x.y%code}, where code can be any of the valid formats "
2354b4fe385SDimitry Andric          "described in 'help format', or one of the"
2364b4fe385SDimitry Andric          " special symbols only allowed as part of a variable:\n"
2374b4fe385SDimitry Andric          "    %V: show the value of the object by default\n"
2384b4fe385SDimitry Andric          "    %S: show the summary of the object by default\n"
2394b4fe385SDimitry Andric          "    %@: show the runtime-provided object description (for "
2404b4fe385SDimitry Andric          "Objective-C, it calls NSPrintForDebugger; for C/C++ it does "
2414b4fe385SDimitry Andric          "nothing)\n"
2424b4fe385SDimitry Andric          "    %L: show the location of the object (memory address or a "
2434b4fe385SDimitry Andric          "register name)\n"
2444b4fe385SDimitry Andric          "    %#: show the number of children of the object\n"
2454b4fe385SDimitry Andric          "    %T: show the type of the object\n"
2464b4fe385SDimitry Andric          "Another variable that you can use in summary strings is ${svar . "
2474b4fe385SDimitry Andric          "This sequence works exactly like ${var, including the fact that "
2484b4fe385SDimitry Andric          "${*svar is an allowed sequence, but uses"
2494b4fe385SDimitry Andric          " the object's synthetic children provider instead of the actual "
2504b4fe385SDimitry Andric          "objects. For instance, if you are using STL synthetic children "
2514b4fe385SDimitry Andric          "providers, the following summary string would"
2524b4fe385SDimitry Andric          " count the number of actual elements stored in an std::list:\n"
2534b4fe385SDimitry Andric          "type summary add -s \"${svar%#}\" -x \"std::list<\"";
2544b4fe385SDimitry Andric }
2554b4fe385SDimitry Andric 
ExprPathHelpTextCallback()2564b4fe385SDimitry Andric llvm::StringRef ExprPathHelpTextCallback() {
2574b4fe385SDimitry Andric   return "An expression path is the sequence of symbols that is used in C/C++ "
2584b4fe385SDimitry Andric          "to access a member variable of an aggregate object (class).\n"
2594b4fe385SDimitry Andric          "For instance, given a class:\n"
2604b4fe385SDimitry Andric          "  class foo {\n"
2614b4fe385SDimitry Andric          "      int a;\n"
2624b4fe385SDimitry Andric          "      int b; .\n"
2634b4fe385SDimitry Andric          "      foo* next;\n"
2644b4fe385SDimitry Andric          "  };\n"
2654b4fe385SDimitry Andric          "the expression to read item b in the item pointed to by next for foo "
2664b4fe385SDimitry Andric          "aFoo would be aFoo.next->b.\n"
2674b4fe385SDimitry Andric          "Given that aFoo could just be any object of type foo, the string "
2684b4fe385SDimitry Andric          "'.next->b' is the expression path, because it can be attached to any "
2694b4fe385SDimitry Andric          "foo instance to achieve the effect.\n"
2704b4fe385SDimitry Andric          "Expression paths in LLDB include dot (.) and arrow (->) operators, "
2714b4fe385SDimitry Andric          "and most commands using expression paths have ways to also accept "
2724b4fe385SDimitry Andric          "the star (*) operator.\n"
2734b4fe385SDimitry Andric          "The meaning of these operators is the same as the usual one given to "
2744b4fe385SDimitry Andric          "them by the C/C++ standards.\n"
2754b4fe385SDimitry Andric          "LLDB also has support for indexing ([ ]) in expression paths, and "
2764b4fe385SDimitry Andric          "extends the traditional meaning of the square brackets operator to "
2774b4fe385SDimitry Andric          "allow bitfield extraction:\n"
2784b4fe385SDimitry Andric          "for objects of native types (int, float, char, ...) saying '[n-m]' "
2794b4fe385SDimitry Andric          "as an expression path (where n and m are any positive integers, e.g. "
2804b4fe385SDimitry Andric          "[3-5]) causes LLDB to extract"
2814b4fe385SDimitry Andric          " bits n thru m from the value of the variable. If n == m, [n] is "
2824b4fe385SDimitry Andric          "also allowed as a shortcut syntax. For arrays and pointers, "
2834b4fe385SDimitry Andric          "expression paths can only contain one index"
2844b4fe385SDimitry Andric          " and the meaning of the operation is the same as the one defined by "
2854b4fe385SDimitry Andric          "C/C++ (item extraction). Some commands extend bitfield-like syntax "
2864b4fe385SDimitry Andric          "for arrays and pointers with the"
2874b4fe385SDimitry Andric          " meaning of array slicing (taking elements n thru m inside the array "
2884b4fe385SDimitry Andric          "or pointed-to memory).";
2894b4fe385SDimitry Andric }
2904b4fe385SDimitry Andric 
arch_helper()2914b4fe385SDimitry Andric llvm::StringRef arch_helper() {
2924b4fe385SDimitry Andric   static StreamString g_archs_help;
2934b4fe385SDimitry Andric   if (g_archs_help.Empty()) {
2944b4fe385SDimitry Andric     StringList archs;
2954b4fe385SDimitry Andric 
2964b4fe385SDimitry Andric     ArchSpec::ListSupportedArchNames(archs);
2974b4fe385SDimitry Andric     g_archs_help.Printf("These are the supported architecture names:\n");
2984b4fe385SDimitry Andric     archs.Join("\n", g_archs_help);
2994b4fe385SDimitry Andric   }
3004b4fe385SDimitry Andric   return g_archs_help.GetString();
3014b4fe385SDimitry Andric }
3024b4fe385SDimitry Andric 
3034b4fe385SDimitry Andric template <int I> struct TableValidator : TableValidator<I + 1> {
3044b4fe385SDimitry Andric   static_assert(
3054b4fe385SDimitry Andric       g_argument_table[I].arg_type == I,
3064b4fe385SDimitry Andric       "g_argument_table order doesn't match CommandArgumentType enumeration");
3074b4fe385SDimitry Andric };
3084b4fe385SDimitry Andric 
3094b4fe385SDimitry Andric template <> struct TableValidator<eArgTypeLastArg> {};
3104b4fe385SDimitry Andric 
3114b4fe385SDimitry Andric TableValidator<0> validator;
3124b4fe385SDimitry Andric 
3134b4fe385SDimitry Andric } // namespace lldb_private
314