xref: /openbsd-src/gnu/llvm/lldb/source/Commands/CommandOptionArgumentTable.cpp (revision f6aab3d83b51b91c24247ad2c2573574de475a82)
1*f6aab3d8Srobert //===-- CommandOptionArgumentTable.cpp ------------------------------------===//
2*f6aab3d8Srobert //
3*f6aab3d8Srobert // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*f6aab3d8Srobert // See https://llvm.org/LICENSE.txt for license information.
5*f6aab3d8Srobert // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*f6aab3d8Srobert //
7*f6aab3d8Srobert //===----------------------------------------------------------------------===//
8*f6aab3d8Srobert 
9*f6aab3d8Srobert #include "lldb/Interpreter/CommandOptionArgumentTable.h"
10*f6aab3d8Srobert #include "lldb/DataFormatters/FormatManager.h"
11*f6aab3d8Srobert #include "lldb/Target/Language.h"
12*f6aab3d8Srobert #include "lldb/Utility/StreamString.h"
13*f6aab3d8Srobert 
14*f6aab3d8Srobert using namespace lldb;
15*f6aab3d8Srobert using namespace lldb_private;
16*f6aab3d8Srobert 
17*f6aab3d8Srobert namespace lldb_private {
RegisterNameHelpTextCallback()18*f6aab3d8Srobert llvm::StringRef RegisterNameHelpTextCallback() {
19*f6aab3d8Srobert   return "Register names can be specified using the architecture specific "
20*f6aab3d8Srobert          "names.  "
21*f6aab3d8Srobert          "They can also be specified using generic names.  Not all generic "
22*f6aab3d8Srobert          "entities have "
23*f6aab3d8Srobert          "registers backing them on all architectures.  When they don't the "
24*f6aab3d8Srobert          "generic name "
25*f6aab3d8Srobert          "will return an error.\n"
26*f6aab3d8Srobert          "The generic names defined in lldb are:\n"
27*f6aab3d8Srobert          "\n"
28*f6aab3d8Srobert          "pc       - program counter register\n"
29*f6aab3d8Srobert          "ra       - return address register\n"
30*f6aab3d8Srobert          "fp       - frame pointer register\n"
31*f6aab3d8Srobert          "sp       - stack pointer register\n"
32*f6aab3d8Srobert          "flags    - the flags register\n"
33*f6aab3d8Srobert          "arg{1-6} - integer argument passing registers.\n";
34*f6aab3d8Srobert }
35*f6aab3d8Srobert 
BreakpointIDHelpTextCallback()36*f6aab3d8Srobert llvm::StringRef BreakpointIDHelpTextCallback() {
37*f6aab3d8Srobert   return "Breakpoints are identified using major and minor numbers; the major "
38*f6aab3d8Srobert          "number corresponds to the single entity that was created with a "
39*f6aab3d8Srobert          "'breakpoint "
40*f6aab3d8Srobert          "set' command; the minor numbers correspond to all the locations that "
41*f6aab3d8Srobert          "were "
42*f6aab3d8Srobert          "actually found/set based on the major breakpoint.  A full breakpoint "
43*f6aab3d8Srobert          "ID might "
44*f6aab3d8Srobert          "look like 3.14, meaning the 14th location set for the 3rd "
45*f6aab3d8Srobert          "breakpoint.  You "
46*f6aab3d8Srobert          "can specify all the locations of a breakpoint by just indicating the "
47*f6aab3d8Srobert          "major "
48*f6aab3d8Srobert          "breakpoint number. A valid breakpoint ID consists either of just the "
49*f6aab3d8Srobert          "major "
50*f6aab3d8Srobert          "number, or the major number followed by a dot and the location "
51*f6aab3d8Srobert          "number (e.g. "
52*f6aab3d8Srobert          "3 or 3.2 could both be valid breakpoint IDs.)";
53*f6aab3d8Srobert }
54*f6aab3d8Srobert 
BreakpointIDRangeHelpTextCallback()55*f6aab3d8Srobert llvm::StringRef BreakpointIDRangeHelpTextCallback() {
56*f6aab3d8Srobert   return "A 'breakpoint ID list' is a manner of specifying multiple "
57*f6aab3d8Srobert          "breakpoints. "
58*f6aab3d8Srobert          "This can be done through several mechanisms.  The easiest way is to "
59*f6aab3d8Srobert          "just "
60*f6aab3d8Srobert          "enter a space-separated list of breakpoint IDs.  To specify all the "
61*f6aab3d8Srobert          "breakpoint locations under a major breakpoint, you can use the major "
62*f6aab3d8Srobert          "breakpoint number followed by '.*', eg. '5.*' means all the "
63*f6aab3d8Srobert          "locations under "
64*f6aab3d8Srobert          "breakpoint 5.  You can also indicate a range of breakpoints by using "
65*f6aab3d8Srobert          "<start-bp-id> - <end-bp-id>.  The start-bp-id and end-bp-id for a "
66*f6aab3d8Srobert          "range can "
67*f6aab3d8Srobert          "be any valid breakpoint IDs.  It is not legal, however, to specify a "
68*f6aab3d8Srobert          "range "
69*f6aab3d8Srobert          "using specific locations that cross major breakpoint numbers.  I.e. "
70*f6aab3d8Srobert          "3.2 - 3.7"
71*f6aab3d8Srobert          " is legal; 2 - 5 is legal; but 3.2 - 4.4 is not legal.";
72*f6aab3d8Srobert }
73*f6aab3d8Srobert 
BreakpointNameHelpTextCallback()74*f6aab3d8Srobert llvm::StringRef BreakpointNameHelpTextCallback() {
75*f6aab3d8Srobert   return "A name that can be added to a breakpoint when it is created, or "
76*f6aab3d8Srobert          "later "
77*f6aab3d8Srobert          "on with the \"breakpoint name add\" command.  "
78*f6aab3d8Srobert          "Breakpoint names can be used to specify breakpoints in all the "
79*f6aab3d8Srobert          "places breakpoint IDs "
80*f6aab3d8Srobert          "and breakpoint ID ranges can be used.  As such they provide a "
81*f6aab3d8Srobert          "convenient way to group breakpoints, "
82*f6aab3d8Srobert          "and to operate on breakpoints you create without having to track the "
83*f6aab3d8Srobert          "breakpoint number.  "
84*f6aab3d8Srobert          "Note, the attributes you set when using a breakpoint name in a "
85*f6aab3d8Srobert          "breakpoint command don't "
86*f6aab3d8Srobert          "adhere to the name, but instead are set individually on all the "
87*f6aab3d8Srobert          "breakpoints currently tagged with that "
88*f6aab3d8Srobert          "name.  Future breakpoints "
89*f6aab3d8Srobert          "tagged with that name will not pick up the attributes previously "
90*f6aab3d8Srobert          "given using that name.  "
91*f6aab3d8Srobert          "In order to distinguish breakpoint names from breakpoint IDs and "
92*f6aab3d8Srobert          "ranges, "
93*f6aab3d8Srobert          "names must start with a letter from a-z or A-Z and cannot contain "
94*f6aab3d8Srobert          "spaces, \".\" or \"-\".  "
95*f6aab3d8Srobert          "Also, breakpoint names can only be applied to breakpoints, not to "
96*f6aab3d8Srobert          "breakpoint locations.";
97*f6aab3d8Srobert }
98*f6aab3d8Srobert 
GDBFormatHelpTextCallback()99*f6aab3d8Srobert llvm::StringRef GDBFormatHelpTextCallback() {
100*f6aab3d8Srobert   return "A GDB format consists of a repeat count, a format letter and a size "
101*f6aab3d8Srobert          "letter. "
102*f6aab3d8Srobert          "The repeat count is optional and defaults to 1. The format letter is "
103*f6aab3d8Srobert          "optional "
104*f6aab3d8Srobert          "and defaults to the previous format that was used. The size letter "
105*f6aab3d8Srobert          "is optional "
106*f6aab3d8Srobert          "and defaults to the previous size that was used.\n"
107*f6aab3d8Srobert          "\n"
108*f6aab3d8Srobert          "Format letters include:\n"
109*f6aab3d8Srobert          "o - octal\n"
110*f6aab3d8Srobert          "x - hexadecimal\n"
111*f6aab3d8Srobert          "d - decimal\n"
112*f6aab3d8Srobert          "u - unsigned decimal\n"
113*f6aab3d8Srobert          "t - binary\n"
114*f6aab3d8Srobert          "f - float\n"
115*f6aab3d8Srobert          "a - address\n"
116*f6aab3d8Srobert          "i - instruction\n"
117*f6aab3d8Srobert          "c - char\n"
118*f6aab3d8Srobert          "s - string\n"
119*f6aab3d8Srobert          "T - OSType\n"
120*f6aab3d8Srobert          "A - float as hex\n"
121*f6aab3d8Srobert          "\n"
122*f6aab3d8Srobert          "Size letters include:\n"
123*f6aab3d8Srobert          "b - 1 byte  (byte)\n"
124*f6aab3d8Srobert          "h - 2 bytes (halfword)\n"
125*f6aab3d8Srobert          "w - 4 bytes (word)\n"
126*f6aab3d8Srobert          "g - 8 bytes (giant)\n"
127*f6aab3d8Srobert          "\n"
128*f6aab3d8Srobert          "Example formats:\n"
129*f6aab3d8Srobert          "32xb - show 32 1 byte hexadecimal integer values\n"
130*f6aab3d8Srobert          "16xh - show 16 2 byte hexadecimal integer values\n"
131*f6aab3d8Srobert          "64   - show 64 2 byte hexadecimal integer values (format and size "
132*f6aab3d8Srobert          "from the last format)\n"
133*f6aab3d8Srobert          "dw   - show 1 4 byte decimal integer value\n";
134*f6aab3d8Srobert }
135*f6aab3d8Srobert 
FormatHelpTextCallback()136*f6aab3d8Srobert llvm::StringRef FormatHelpTextCallback() {
137*f6aab3d8Srobert   static std::string help_text;
138*f6aab3d8Srobert 
139*f6aab3d8Srobert   if (!help_text.empty())
140*f6aab3d8Srobert     return help_text;
141*f6aab3d8Srobert 
142*f6aab3d8Srobert   StreamString sstr;
143*f6aab3d8Srobert   sstr << "One of the format names (or one-character names) that can be used "
144*f6aab3d8Srobert           "to show a variable's value:\n";
145*f6aab3d8Srobert   for (Format f = eFormatDefault; f < kNumFormats; f = Format(f + 1)) {
146*f6aab3d8Srobert     if (f != eFormatDefault)
147*f6aab3d8Srobert       sstr.PutChar('\n');
148*f6aab3d8Srobert 
149*f6aab3d8Srobert     char format_char = FormatManager::GetFormatAsFormatChar(f);
150*f6aab3d8Srobert     if (format_char)
151*f6aab3d8Srobert       sstr.Printf("'%c' or ", format_char);
152*f6aab3d8Srobert 
153*f6aab3d8Srobert     sstr.Printf("\"%s\"", FormatManager::GetFormatAsCString(f));
154*f6aab3d8Srobert   }
155*f6aab3d8Srobert 
156*f6aab3d8Srobert   sstr.Flush();
157*f6aab3d8Srobert 
158*f6aab3d8Srobert   help_text = std::string(sstr.GetString());
159*f6aab3d8Srobert 
160*f6aab3d8Srobert   return help_text;
161*f6aab3d8Srobert }
162*f6aab3d8Srobert 
LanguageTypeHelpTextCallback()163*f6aab3d8Srobert llvm::StringRef LanguageTypeHelpTextCallback() {
164*f6aab3d8Srobert   static std::string help_text;
165*f6aab3d8Srobert 
166*f6aab3d8Srobert   if (!help_text.empty())
167*f6aab3d8Srobert     return help_text;
168*f6aab3d8Srobert 
169*f6aab3d8Srobert   StreamString sstr;
170*f6aab3d8Srobert   sstr << "One of the following languages:\n";
171*f6aab3d8Srobert 
172*f6aab3d8Srobert   Language::PrintAllLanguages(sstr, "  ", "\n");
173*f6aab3d8Srobert 
174*f6aab3d8Srobert   sstr.Flush();
175*f6aab3d8Srobert 
176*f6aab3d8Srobert   help_text = std::string(sstr.GetString());
177*f6aab3d8Srobert 
178*f6aab3d8Srobert   return help_text;
179*f6aab3d8Srobert }
180*f6aab3d8Srobert 
SummaryStringHelpTextCallback()181*f6aab3d8Srobert llvm::StringRef SummaryStringHelpTextCallback() {
182*f6aab3d8Srobert   return "A summary string is a way to extract information from variables in "
183*f6aab3d8Srobert          "order to present them using a summary.\n"
184*f6aab3d8Srobert          "Summary strings contain static text, variables, scopes and control "
185*f6aab3d8Srobert          "sequences:\n"
186*f6aab3d8Srobert          "  - Static text can be any sequence of non-special characters, i.e. "
187*f6aab3d8Srobert          "anything but '{', '}', '$', or '\\'.\n"
188*f6aab3d8Srobert          "  - Variables are sequences of characters beginning with ${, ending "
189*f6aab3d8Srobert          "with } and that contain symbols in the format described below.\n"
190*f6aab3d8Srobert          "  - Scopes are any sequence of text between { and }. Anything "
191*f6aab3d8Srobert          "included in a scope will only appear in the output summary if there "
192*f6aab3d8Srobert          "were no errors.\n"
193*f6aab3d8Srobert          "  - Control sequences are the usual C/C++ '\\a', '\\n', ..., plus "
194*f6aab3d8Srobert          "'\\$', '\\{' and '\\}'.\n"
195*f6aab3d8Srobert          "A summary string works by copying static text verbatim, turning "
196*f6aab3d8Srobert          "control sequences into their character counterpart, expanding "
197*f6aab3d8Srobert          "variables and trying to expand scopes.\n"
198*f6aab3d8Srobert          "A variable is expanded by giving it a value other than its textual "
199*f6aab3d8Srobert          "representation, and the way this is done depends on what comes after "
200*f6aab3d8Srobert          "the ${ marker.\n"
201*f6aab3d8Srobert          "The most common sequence if ${var followed by an expression path, "
202*f6aab3d8Srobert          "which is the text one would type to access a member of an aggregate "
203*f6aab3d8Srobert          "types, given a variable of that type"
204*f6aab3d8Srobert          " (e.g. if type T has a member named x, which has a member named y, "
205*f6aab3d8Srobert          "and if t is of type T, the expression path would be .x.y and the way "
206*f6aab3d8Srobert          "to fit that into a summary string would be"
207*f6aab3d8Srobert          " ${var.x.y}). You can also use ${*var followed by an expression path "
208*f6aab3d8Srobert          "and in that case the object referred by the path will be "
209*f6aab3d8Srobert          "dereferenced before being displayed."
210*f6aab3d8Srobert          " If the object is not a pointer, doing so will cause an error. For "
211*f6aab3d8Srobert          "additional details on expression paths, you can type 'help "
212*f6aab3d8Srobert          "expr-path'. \n"
213*f6aab3d8Srobert          "By default, summary strings attempt to display the summary for any "
214*f6aab3d8Srobert          "variable they reference, and if that fails the value. If neither can "
215*f6aab3d8Srobert          "be shown, nothing is displayed."
216*f6aab3d8Srobert          "In a summary string, you can also use an array index [n], or a "
217*f6aab3d8Srobert          "slice-like range [n-m]. This can have two different meanings "
218*f6aab3d8Srobert          "depending on what kind of object the expression"
219*f6aab3d8Srobert          " path refers to:\n"
220*f6aab3d8Srobert          "  - if it is a scalar type (any basic type like int, float, ...) the "
221*f6aab3d8Srobert          "expression is a bitfield, i.e. the bits indicated by the indexing "
222*f6aab3d8Srobert          "operator are extracted out of the number"
223*f6aab3d8Srobert          " and displayed as an individual variable\n"
224*f6aab3d8Srobert          "  - if it is an array or pointer the array items indicated by the "
225*f6aab3d8Srobert          "indexing operator are shown as the result of the variable. if the "
226*f6aab3d8Srobert          "expression is an array, real array items are"
227*f6aab3d8Srobert          " printed; if it is a pointer, the pointer-as-array syntax is used to "
228*f6aab3d8Srobert          "obtain the values (this means, the latter case can have no range "
229*f6aab3d8Srobert          "checking)\n"
230*f6aab3d8Srobert          "If you are trying to display an array for which the size is known, "
231*f6aab3d8Srobert          "you can also use [] instead of giving an exact range. This has the "
232*f6aab3d8Srobert          "effect of showing items 0 thru size - 1.\n"
233*f6aab3d8Srobert          "Additionally, a variable can contain an (optional) format code, as "
234*f6aab3d8Srobert          "in ${var.x.y%code}, where code can be any of the valid formats "
235*f6aab3d8Srobert          "described in 'help format', or one of the"
236*f6aab3d8Srobert          " special symbols only allowed as part of a variable:\n"
237*f6aab3d8Srobert          "    %V: show the value of the object by default\n"
238*f6aab3d8Srobert          "    %S: show the summary of the object by default\n"
239*f6aab3d8Srobert          "    %@: show the runtime-provided object description (for "
240*f6aab3d8Srobert          "Objective-C, it calls NSPrintForDebugger; for C/C++ it does "
241*f6aab3d8Srobert          "nothing)\n"
242*f6aab3d8Srobert          "    %L: show the location of the object (memory address or a "
243*f6aab3d8Srobert          "register name)\n"
244*f6aab3d8Srobert          "    %#: show the number of children of the object\n"
245*f6aab3d8Srobert          "    %T: show the type of the object\n"
246*f6aab3d8Srobert          "Another variable that you can use in summary strings is ${svar . "
247*f6aab3d8Srobert          "This sequence works exactly like ${var, including the fact that "
248*f6aab3d8Srobert          "${*svar is an allowed sequence, but uses"
249*f6aab3d8Srobert          " the object's synthetic children provider instead of the actual "
250*f6aab3d8Srobert          "objects. For instance, if you are using STL synthetic children "
251*f6aab3d8Srobert          "providers, the following summary string would"
252*f6aab3d8Srobert          " count the number of actual elements stored in an std::list:\n"
253*f6aab3d8Srobert          "type summary add -s \"${svar%#}\" -x \"std::list<\"";
254*f6aab3d8Srobert }
255*f6aab3d8Srobert 
ExprPathHelpTextCallback()256*f6aab3d8Srobert llvm::StringRef ExprPathHelpTextCallback() {
257*f6aab3d8Srobert   return "An expression path is the sequence of symbols that is used in C/C++ "
258*f6aab3d8Srobert          "to access a member variable of an aggregate object (class).\n"
259*f6aab3d8Srobert          "For instance, given a class:\n"
260*f6aab3d8Srobert          "  class foo {\n"
261*f6aab3d8Srobert          "      int a;\n"
262*f6aab3d8Srobert          "      int b; .\n"
263*f6aab3d8Srobert          "      foo* next;\n"
264*f6aab3d8Srobert          "  };\n"
265*f6aab3d8Srobert          "the expression to read item b in the item pointed to by next for foo "
266*f6aab3d8Srobert          "aFoo would be aFoo.next->b.\n"
267*f6aab3d8Srobert          "Given that aFoo could just be any object of type foo, the string "
268*f6aab3d8Srobert          "'.next->b' is the expression path, because it can be attached to any "
269*f6aab3d8Srobert          "foo instance to achieve the effect.\n"
270*f6aab3d8Srobert          "Expression paths in LLDB include dot (.) and arrow (->) operators, "
271*f6aab3d8Srobert          "and most commands using expression paths have ways to also accept "
272*f6aab3d8Srobert          "the star (*) operator.\n"
273*f6aab3d8Srobert          "The meaning of these operators is the same as the usual one given to "
274*f6aab3d8Srobert          "them by the C/C++ standards.\n"
275*f6aab3d8Srobert          "LLDB also has support for indexing ([ ]) in expression paths, and "
276*f6aab3d8Srobert          "extends the traditional meaning of the square brackets operator to "
277*f6aab3d8Srobert          "allow bitfield extraction:\n"
278*f6aab3d8Srobert          "for objects of native types (int, float, char, ...) saying '[n-m]' "
279*f6aab3d8Srobert          "as an expression path (where n and m are any positive integers, e.g. "
280*f6aab3d8Srobert          "[3-5]) causes LLDB to extract"
281*f6aab3d8Srobert          " bits n thru m from the value of the variable. If n == m, [n] is "
282*f6aab3d8Srobert          "also allowed as a shortcut syntax. For arrays and pointers, "
283*f6aab3d8Srobert          "expression paths can only contain one index"
284*f6aab3d8Srobert          " and the meaning of the operation is the same as the one defined by "
285*f6aab3d8Srobert          "C/C++ (item extraction). Some commands extend bitfield-like syntax "
286*f6aab3d8Srobert          "for arrays and pointers with the"
287*f6aab3d8Srobert          " meaning of array slicing (taking elements n thru m inside the array "
288*f6aab3d8Srobert          "or pointed-to memory).";
289*f6aab3d8Srobert }
290*f6aab3d8Srobert 
arch_helper()291*f6aab3d8Srobert llvm::StringRef arch_helper() {
292*f6aab3d8Srobert   static StreamString g_archs_help;
293*f6aab3d8Srobert   if (g_archs_help.Empty()) {
294*f6aab3d8Srobert     StringList archs;
295*f6aab3d8Srobert 
296*f6aab3d8Srobert     ArchSpec::ListSupportedArchNames(archs);
297*f6aab3d8Srobert     g_archs_help.Printf("These are the supported architecture names:\n");
298*f6aab3d8Srobert     archs.Join("\n", g_archs_help);
299*f6aab3d8Srobert   }
300*f6aab3d8Srobert   return g_archs_help.GetString();
301*f6aab3d8Srobert }
302*f6aab3d8Srobert 
303*f6aab3d8Srobert template <int I> struct TableValidator : TableValidator<I + 1> {
304*f6aab3d8Srobert   static_assert(
305*f6aab3d8Srobert       g_argument_table[I].arg_type == I,
306*f6aab3d8Srobert       "g_argument_table order doesn't match CommandArgumentType enumeration");
307*f6aab3d8Srobert };
308*f6aab3d8Srobert 
309*f6aab3d8Srobert template <> struct TableValidator<eArgTypeLastArg> {};
310*f6aab3d8Srobert 
311*f6aab3d8Srobert TableValidator<0> validator;
312*f6aab3d8Srobert 
313*f6aab3d8Srobert } // namespace lldb_private
314