cli-cmds.c revision 1.6 1 1.1 christos /* GDB CLI commands.
2 1.1 christos
3 1.6 christos Copyright (C) 2000-2016 Free Software Foundation, Inc.
4 1.1 christos
5 1.1 christos This file is part of GDB.
6 1.1 christos
7 1.1 christos This program is free software; you can redistribute it and/or modify
8 1.1 christos it under the terms of the GNU General Public License as published by
9 1.1 christos the Free Software Foundation; either version 3 of the License, or
10 1.1 christos (at your option) any later version.
11 1.1 christos
12 1.1 christos This program is distributed in the hope that it will be useful,
13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 1.1 christos GNU General Public License for more details.
16 1.1 christos
17 1.1 christos You should have received a copy of the GNU General Public License
18 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 1.1 christos
20 1.1 christos #include "defs.h"
21 1.1 christos #include "arch-utils.h"
22 1.1 christos #include "dyn-string.h"
23 1.1 christos #include "readline/readline.h"
24 1.1 christos #include "readline/tilde.h"
25 1.1 christos #include "completer.h"
26 1.1 christos #include "target.h" /* For baud_rate, remote_debug and remote_timeout. */
27 1.1 christos #include "gdb_wait.h" /* For shell escape implementation. */
28 1.1 christos #include "gdb_regex.h" /* Used by apropos_command. */
29 1.1 christos #include "gdb_vfork.h"
30 1.1 christos #include "linespec.h"
31 1.1 christos #include "expression.h"
32 1.1 christos #include "frame.h"
33 1.1 christos #include "value.h"
34 1.1 christos #include "language.h"
35 1.1 christos #include "filenames.h" /* For DOSish file names. */
36 1.1 christos #include "objfiles.h"
37 1.1 christos #include "source.h"
38 1.1 christos #include "disasm.h"
39 1.1 christos #include "tracepoint.h"
40 1.1 christos #include "filestuff.h"
41 1.6 christos #include "location.h"
42 1.1 christos
43 1.1 christos #include "ui-out.h"
44 1.1 christos
45 1.1 christos #include "top.h"
46 1.1 christos #include "cli/cli-decode.h"
47 1.1 christos #include "cli/cli-script.h"
48 1.1 christos #include "cli/cli-setshow.h"
49 1.1 christos #include "cli/cli-cmds.h"
50 1.1 christos #include "cli/cli-utils.h"
51 1.1 christos
52 1.3 christos #include "extension.h"
53 1.1 christos
54 1.1 christos #ifdef TUI
55 1.1 christos #include "tui/tui.h" /* For tui_active et.al. */
56 1.1 christos #endif
57 1.1 christos
58 1.1 christos #include <fcntl.h>
59 1.1 christos
60 1.1 christos /* Prototypes for local command functions */
61 1.1 christos
62 1.1 christos static void complete_command (char *, int);
63 1.1 christos
64 1.1 christos static void echo_command (char *, int);
65 1.1 christos
66 1.1 christos static void pwd_command (char *, int);
67 1.1 christos
68 1.1 christos static void show_version (char *, int);
69 1.1 christos
70 1.1 christos static void help_command (char *, int);
71 1.1 christos
72 1.1 christos static void show_command (char *, int);
73 1.1 christos
74 1.1 christos static void info_command (char *, int);
75 1.1 christos
76 1.1 christos static void show_debug (char *, int);
77 1.1 christos
78 1.1 christos static void set_debug (char *, int);
79 1.1 christos
80 1.1 christos static void show_user (char *, int);
81 1.1 christos
82 1.1 christos static void make_command (char *, int);
83 1.1 christos
84 1.1 christos static void shell_escape (char *, int);
85 1.1 christos
86 1.1 christos static void edit_command (char *, int);
87 1.1 christos
88 1.1 christos static void list_command (char *, int);
89 1.1 christos
90 1.1 christos /* Prototypes for local utility functions */
91 1.1 christos
92 1.1 christos static void ambiguous_line_spec (struct symtabs_and_lines *);
93 1.1 christos
94 1.1 christos static void filter_sals (struct symtabs_and_lines *);
95 1.1 christos
96 1.1 christos
97 1.1 christos /* Limit the call depth of user-defined commands */
99 1.1 christos unsigned int max_user_call_depth;
100 1.1 christos
101 1.1 christos /* Define all cmd_list_elements. */
102 1.1 christos
103 1.1 christos /* Chain containing all defined commands. */
104 1.1 christos
105 1.1 christos struct cmd_list_element *cmdlist;
106 1.1 christos
107 1.1 christos /* Chain containing all defined info subcommands. */
108 1.1 christos
109 1.1 christos struct cmd_list_element *infolist;
110 1.1 christos
111 1.1 christos /* Chain containing all defined enable subcommands. */
112 1.1 christos
113 1.1 christos struct cmd_list_element *enablelist;
114 1.1 christos
115 1.1 christos /* Chain containing all defined disable subcommands. */
116 1.1 christos
117 1.1 christos struct cmd_list_element *disablelist;
118 1.1 christos
119 1.1 christos /* Chain containing all defined stop subcommands. */
120 1.1 christos
121 1.1 christos struct cmd_list_element *stoplist;
122 1.1 christos
123 1.1 christos /* Chain containing all defined delete subcommands. */
124 1.1 christos
125 1.1 christos struct cmd_list_element *deletelist;
126 1.1 christos
127 1.1 christos /* Chain containing all defined detach subcommands. */
128 1.1 christos
129 1.1 christos struct cmd_list_element *detachlist;
130 1.1 christos
131 1.1 christos /* Chain containing all defined kill subcommands. */
132 1.1 christos
133 1.1 christos struct cmd_list_element *killlist;
134 1.1 christos
135 1.1 christos /* Chain containing all defined set subcommands */
136 1.1 christos
137 1.1 christos struct cmd_list_element *setlist;
138 1.1 christos
139 1.1 christos /* Chain containing all defined unset subcommands */
140 1.1 christos
141 1.1 christos struct cmd_list_element *unsetlist;
142 1.1 christos
143 1.1 christos /* Chain containing all defined show subcommands. */
144 1.1 christos
145 1.1 christos struct cmd_list_element *showlist;
146 1.1 christos
147 1.1 christos /* Chain containing all defined \"set history\". */
148 1.1 christos
149 1.1 christos struct cmd_list_element *sethistlist;
150 1.1 christos
151 1.1 christos /* Chain containing all defined \"show history\". */
152 1.1 christos
153 1.1 christos struct cmd_list_element *showhistlist;
154 1.1 christos
155 1.1 christos /* Chain containing all defined \"unset history\". */
156 1.1 christos
157 1.1 christos struct cmd_list_element *unsethistlist;
158 1.1 christos
159 1.1 christos /* Chain containing all defined maintenance subcommands. */
160 1.1 christos
161 1.1 christos struct cmd_list_element *maintenancelist;
162 1.1 christos
163 1.1 christos /* Chain containing all defined "maintenance info" subcommands. */
164 1.1 christos
165 1.1 christos struct cmd_list_element *maintenanceinfolist;
166 1.1 christos
167 1.1 christos /* Chain containing all defined "maintenance print" subcommands. */
168 1.1 christos
169 1.1 christos struct cmd_list_element *maintenanceprintlist;
170 1.1 christos
171 1.1 christos struct cmd_list_element *setprintlist;
172 1.1 christos
173 1.1 christos struct cmd_list_element *showprintlist;
174 1.1 christos
175 1.1 christos struct cmd_list_element *setdebuglist;
176 1.1 christos
177 1.1 christos struct cmd_list_element *showdebuglist;
178 1.1 christos
179 1.1 christos struct cmd_list_element *setchecklist;
180 1.1 christos
181 1.1 christos struct cmd_list_element *showchecklist;
182 1.1 christos
183 1.1 christos /* Command tracing state. */
184 1.1 christos
185 1.1 christos int source_verbose = 0;
186 1.1 christos int trace_commands = 0;
187 1.1 christos
188 1.1 christos /* 'script-extension' option support. */
190 1.1 christos
191 1.1 christos static const char script_ext_off[] = "off";
192 1.1 christos static const char script_ext_soft[] = "soft";
193 1.1 christos static const char script_ext_strict[] = "strict";
194 1.1 christos
195 1.1 christos static const char *const script_ext_enums[] = {
196 1.1 christos script_ext_off,
197 1.1 christos script_ext_soft,
198 1.1 christos script_ext_strict,
199 1.1 christos NULL
200 1.1 christos };
201 1.1 christos
202 1.1 christos static const char *script_ext_mode = script_ext_soft;
203 1.1 christos
204 1.1 christos /* Utility used everywhere when at least one argument is needed and
206 1.3 christos none is supplied. */
207 1.1 christos
208 1.1 christos void
209 1.1 christos error_no_arg (const char *why)
210 1.1 christos {
211 1.1 christos error (_("Argument required (%s)."), why);
212 1.1 christos }
213 1.1 christos
214 1.1 christos /* The "info" command is defined as a prefix, with allow_unknown = 0.
215 1.1 christos Therefore, its own definition is called only for "info" with no
216 1.1 christos args. */
217 1.1 christos
218 1.1 christos static void
219 1.1 christos info_command (char *arg, int from_tty)
220 1.3 christos {
221 1.1 christos printf_unfiltered (_("\"info\" must be followed by "
222 1.1 christos "the name of an info command.\n"));
223 1.1 christos help_list (infolist, "info ", all_commands, gdb_stdout);
224 1.1 christos }
225 1.1 christos
226 1.1 christos /* The "show" command with no arguments shows all the settings. */
227 1.1 christos
228 1.1 christos static void
229 1.1 christos show_command (char *arg, int from_tty)
230 1.1 christos {
231 1.1 christos cmd_show_list (showlist, from_tty, "");
232 1.1 christos }
233 1.1 christos
234 1.1 christos /* Provide documentation on command or list given by COMMAND. FROM_TTY
236 1.1 christos is ignored. */
237 1.1 christos
238 1.1 christos static void
239 1.1 christos help_command (char *command, int from_tty)
240 1.5 christos {
241 1.5 christos help_cmd (command, gdb_stdout);
242 1.1 christos }
243 1.1 christos
244 1.1 christos /* Note: The "complete" command is used by Emacs to implement completion.
246 1.1 christos [Is that why this function writes output with *_unfiltered?] */
247 1.1 christos
248 1.1 christos static void
249 1.1 christos complete_command (char *arg, int from_tty)
250 1.1 christos {
251 1.1 christos int argpoint;
252 1.5 christos char *point, *arg_prefix;
253 1.5 christos VEC (char_ptr) *completions;
254 1.5 christos
255 1.5 christos dont_repeat ();
256 1.5 christos
257 1.5 christos if (max_completions == 0)
258 1.5 christos {
259 1.5 christos /* Only print this for non-mi frontends. An MI frontend may not
260 1.5 christos be able to handle this. */
261 1.5 christos if (!ui_out_is_mi_like_p (current_uiout))
262 1.5 christos {
263 1.5 christos printf_unfiltered (_("max-completions is zero,"
264 1.1 christos " completion is disabled.\n"));
265 1.1 christos }
266 1.1 christos return;
267 1.1 christos }
268 1.1 christos
269 1.1 christos if (arg == NULL)
270 1.1 christos arg = "";
271 1.1 christos argpoint = strlen (arg);
272 1.1 christos
273 1.1 christos /* complete_line assumes that its first argument is somewhere
274 1.1 christos within, and except for filenames at the beginning of, the word to
275 1.1 christos be completed. The following crude imitation of readline's
276 1.1 christos word-breaking tries to accomodate this. */
277 1.1 christos point = arg + argpoint;
278 1.1 christos while (point > arg)
279 1.1 christos {
280 1.6 christos if (strchr (rl_completer_word_break_characters, point[-1]) != 0)
281 1.1 christos break;
282 1.1 christos point--;
283 1.1 christos }
284 1.1 christos
285 1.1 christos arg_prefix = (char *) alloca (point - arg + 1);
286 1.1 christos memcpy (arg_prefix, arg, point - arg);
287 1.1 christos arg_prefix[point - arg] = 0;
288 1.1 christos
289 1.1 christos completions = complete_line (point, arg, argpoint);
290 1.1 christos
291 1.1 christos if (completions)
292 1.1 christos {
293 1.1 christos int ix, size = VEC_length (char_ptr, completions);
294 1.1 christos char *item, *prev = NULL;
295 1.1 christos
296 1.1 christos qsort (VEC_address (char_ptr, completions), size,
297 1.1 christos sizeof (char *), compare_strings);
298 1.1 christos
299 1.1 christos /* We do extra processing here since we only want to print each
300 1.1 christos unique item once. */
301 1.1 christos for (ix = 0; VEC_iterate (char_ptr, completions, ix, item); ++ix)
302 1.1 christos {
303 1.1 christos if (prev == NULL || strcmp (item, prev) != 0)
304 1.1 christos {
305 1.1 christos printf_unfiltered ("%s%s\n", arg_prefix, item);
306 1.1 christos xfree (prev);
307 1.1 christos prev = item;
308 1.1 christos }
309 1.1 christos else
310 1.5 christos xfree (item);
311 1.5 christos }
312 1.5 christos
313 1.5 christos xfree (prev);
314 1.5 christos VEC_free (char_ptr, completions);
315 1.5 christos
316 1.5 christos if (size == max_completions)
317 1.5 christos {
318 1.5 christos /* ARG_PREFIX and POINT are included in the output so that emacs
319 1.1 christos will include the message in the output. */
320 1.1 christos printf_unfiltered (_("%s%s %s\n"),
321 1.1 christos arg_prefix, point,
322 1.1 christos get_max_completions_reached_message ());
323 1.1 christos }
324 1.1 christos }
325 1.1 christos }
326 1.1 christos
327 1.1 christos int
328 1.1 christos is_complete_command (struct cmd_list_element *c)
329 1.1 christos {
330 1.1 christos return cmd_cfunc_eq (c, complete_command);
331 1.1 christos }
332 1.1 christos
333 1.1 christos static void
334 1.1 christos show_version (char *args, int from_tty)
335 1.1 christos {
336 1.1 christos print_gdb_version (gdb_stdout);
337 1.1 christos printf_filtered ("\n");
338 1.1 christos }
339 1.1 christos
340 1.1 christos static void
341 1.1 christos show_configuration (char *args, int from_tty)
342 1.1 christos {
343 1.1 christos print_gdb_configuration (gdb_stdout);
344 1.1 christos }
345 1.1 christos
346 1.1 christos /* Handle the quit command. */
347 1.1 christos
348 1.1 christos void
349 1.1 christos quit_command (char *args, int from_tty)
350 1.1 christos {
351 1.1 christos if (!quit_confirm ())
352 1.1 christos error (_("Not confirmed."));
353 1.1 christos
354 1.1 christos query_if_trace_running (from_tty);
355 1.1 christos
356 1.1 christos quit_force (args, from_tty);
357 1.1 christos }
358 1.1 christos
359 1.1 christos static void
360 1.1 christos pwd_command (char *args, int from_tty)
361 1.1 christos {
362 1.1 christos if (args)
363 1.1 christos error (_("The \"pwd\" command does not take an argument: %s"), args);
364 1.1 christos if (! getcwd (gdb_dirbuf, sizeof (gdb_dirbuf)))
365 1.1 christos error (_("Error finding name of working directory: %s"),
366 1.1 christos safe_strerror (errno));
367 1.1 christos
368 1.1 christos if (strcmp (gdb_dirbuf, current_directory) != 0)
369 1.1 christos printf_unfiltered (_("Working directory %s\n (canonically %s).\n"),
370 1.1 christos current_directory, gdb_dirbuf);
371 1.1 christos else
372 1.1 christos printf_unfiltered (_("Working directory %s.\n"), current_directory);
373 1.1 christos }
374 1.1 christos
375 1.1 christos void
376 1.1 christos cd_command (char *dir, int from_tty)
377 1.1 christos {
378 1.1 christos int len;
379 1.1 christos /* Found something other than leading repetitions of "/..". */
380 1.1 christos int found_real_path;
381 1.1 christos char *p;
382 1.1 christos struct cleanup *cleanup;
383 1.1 christos
384 1.1 christos /* If the new directory is absolute, repeat is a no-op; if relative,
385 1.1 christos repeat might be useful but is more likely to be a mistake. */
386 1.1 christos dont_repeat ();
387 1.1 christos
388 1.1 christos if (dir == 0)
389 1.1 christos dir = "~";
390 1.1 christos
391 1.1 christos dir = tilde_expand (dir);
392 1.1 christos cleanup = make_cleanup (xfree, dir);
393 1.1 christos
394 1.1 christos if (chdir (dir) < 0)
395 1.1 christos perror_with_name (dir);
396 1.1 christos
397 1.1 christos #ifdef HAVE_DOS_BASED_FILE_SYSTEM
398 1.1 christos /* There's too much mess with DOSish names like "d:", "d:.",
399 1.1 christos "d:./foo" etc. Instead of having lots of special #ifdef'ed code,
400 1.1 christos simply get the canonicalized name of the current directory. */
401 1.1 christos dir = getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
402 1.1 christos #endif
403 1.1 christos
404 1.1 christos len = strlen (dir);
405 1.1 christos if (IS_DIR_SEPARATOR (dir[len - 1]))
406 1.1 christos {
407 1.1 christos /* Remove the trailing slash unless this is a root directory
408 1.1 christos (including a drive letter on non-Unix systems). */
409 1.1 christos if (!(len == 1) /* "/" */
410 1.1 christos #ifdef HAVE_DOS_BASED_FILE_SYSTEM
411 1.1 christos && !(len == 3 && dir[1] == ':') /* "d:/" */
412 1.1 christos #endif
413 1.1 christos )
414 1.1 christos len--;
415 1.1 christos }
416 1.1 christos
417 1.1 christos dir = savestring (dir, len);
418 1.1 christos if (IS_ABSOLUTE_PATH (dir))
419 1.1 christos current_directory = dir;
420 1.1 christos else
421 1.1 christos {
422 1.1 christos if (IS_DIR_SEPARATOR (current_directory[strlen (current_directory) - 1]))
423 1.1 christos current_directory = concat (current_directory, dir, (char *)NULL);
424 1.1 christos else
425 1.1 christos current_directory = concat (current_directory, SLASH_STRING,
426 1.1 christos dir, (char *)NULL);
427 1.1 christos xfree (dir);
428 1.1 christos }
429 1.1 christos
430 1.1 christos /* Now simplify any occurrences of `.' and `..' in the pathname. */
431 1.1 christos
432 1.1 christos found_real_path = 0;
433 1.1 christos for (p = current_directory; *p;)
434 1.1 christos {
435 1.1 christos if (IS_DIR_SEPARATOR (p[0]) && p[1] == '.'
436 1.1 christos && (p[2] == 0 || IS_DIR_SEPARATOR (p[2])))
437 1.1 christos memmove (p, p + 2, strlen (p + 2) + 1);
438 1.1 christos else if (IS_DIR_SEPARATOR (p[0]) && p[1] == '.' && p[2] == '.'
439 1.1 christos && (p[3] == 0 || IS_DIR_SEPARATOR (p[3])))
440 1.1 christos {
441 1.1 christos if (found_real_path)
442 1.1 christos {
443 1.1 christos /* Search backwards for the directory just before the "/.."
444 1.1 christos and obliterate it and the "/..". */
445 1.1 christos char *q = p;
446 1.1 christos
447 1.1 christos while (q != current_directory && !IS_DIR_SEPARATOR (q[-1]))
448 1.1 christos --q;
449 1.1 christos
450 1.1 christos if (q == current_directory)
451 1.1 christos /* current_directory is
452 1.1 christos a relative pathname ("can't happen"--leave it alone). */
453 1.1 christos ++p;
454 1.1 christos else
455 1.1 christos {
456 1.1 christos memmove (q - 1, p + 3, strlen (p + 3) + 1);
457 1.1 christos p = q - 1;
458 1.1 christos }
459 1.1 christos }
460 1.1 christos else
461 1.1 christos /* We are dealing with leading repetitions of "/..", for
462 1.1 christos example "/../..", which is the Mach super-root. */
463 1.1 christos p += 3;
464 1.1 christos }
465 1.1 christos else
466 1.1 christos {
467 1.1 christos found_real_path = 1;
468 1.1 christos ++p;
469 1.1 christos }
470 1.1 christos }
471 1.1 christos
472 1.1 christos forget_cached_source_info ();
473 1.1 christos
474 1.1 christos if (from_tty)
475 1.1 christos pwd_command ((char *) 0, 1);
476 1.1 christos
477 1.1 christos do_cleanups (cleanup);
478 1.1 christos }
479 1.1 christos
480 1.1 christos /* Show the current value of the 'script-extension' option. */
482 1.1 christos
483 1.1 christos static void
484 1.1 christos show_script_ext_mode (struct ui_file *file, int from_tty,
485 1.1 christos struct cmd_list_element *c, const char *value)
486 1.1 christos {
487 1.1 christos fprintf_filtered (file,
488 1.1 christos _("Script filename extension recognition is \"%s\".\n"),
489 1.1 christos value);
490 1.1 christos }
491 1.1 christos
492 1.1 christos /* Try to open SCRIPT_FILE.
493 1.1 christos If successful, the full path name is stored in *FULL_PATHP,
494 1.1 christos the stream is stored in *STREAMP, and return 1.
495 1.1 christos The caller is responsible for freeing *FULL_PATHP.
496 1.1 christos If not successful, return 0; errno is set for the last file
497 1.1 christos we tried to open.
498 1.1 christos
499 1.1 christos If SEARCH_PATH is non-zero, and the file isn't found in cwd,
500 1.1 christos search for it in the source search path. */
501 1.1 christos
502 1.1 christos int
503 1.1 christos find_and_open_script (const char *script_file, int search_path,
504 1.1 christos FILE **streamp, char **full_pathp)
505 1.1 christos {
506 1.1 christos char *file;
507 1.1 christos int fd;
508 1.1 christos struct cleanup *old_cleanups;
509 1.1 christos int search_flags = OPF_TRY_CWD_FIRST | OPF_RETURN_REALPATH;
510 1.1 christos
511 1.1 christos file = tilde_expand (script_file);
512 1.1 christos old_cleanups = make_cleanup (xfree, file);
513 1.1 christos
514 1.1 christos if (search_path)
515 1.1 christos search_flags |= OPF_SEARCH_IN_PATH;
516 1.1 christos
517 1.1 christos /* Search for and open 'file' on the search path used for source
518 1.1 christos files. Put the full location in *FULL_PATHP. */
519 1.1 christos fd = openp (source_path, search_flags,
520 1.1 christos file, O_RDONLY, full_pathp);
521 1.1 christos
522 1.1 christos if (fd == -1)
523 1.1 christos {
524 1.1 christos int save_errno = errno;
525 1.1 christos do_cleanups (old_cleanups);
526 1.1 christos errno = save_errno;
527 1.1 christos return 0;
528 1.1 christos }
529 1.1 christos
530 1.1 christos do_cleanups (old_cleanups);
531 1.1 christos
532 1.1 christos *streamp = fdopen (fd, FOPEN_RT);
533 1.1 christos if (*streamp == NULL)
534 1.1 christos {
535 1.1 christos int save_errno = errno;
536 1.1 christos
537 1.1 christos close (fd);
538 1.1 christos if (full_pathp)
539 1.1 christos xfree (*full_pathp);
540 1.1 christos errno = save_errno;
541 1.6 christos return 0;
542 1.6 christos }
543 1.6 christos
544 1.6 christos return 1;
545 1.6 christos }
546 1.6 christos
547 1.1 christos /* Load script FILE, which has already been opened as STREAM.
548 1.1 christos FILE_TO_OPEN is the form of FILE to use if one needs to open the file.
549 1.6 christos This is provided as FILE may have been found via the source search path.
550 1.6 christos An important thing to note here is that FILE may be a symlink to a file
551 1.1 christos with a different or non-existing suffix, and thus one cannot infer the
552 1.3 christos extension language from FILE_TO_OPEN. */
553 1.1 christos
554 1.3 christos static void
555 1.3 christos source_script_from_stream (FILE *stream, const char *file,
556 1.3 christos const char *file_to_open)
557 1.3 christos {
558 1.1 christos if (script_ext_mode != script_ext_off)
559 1.3 christos {
560 1.3 christos const struct extension_language_defn *extlang
561 1.3 christos = get_ext_lang_of_file (file);
562 1.3 christos
563 1.3 christos if (extlang != NULL)
564 1.3 christos {
565 1.6 christos if (ext_lang_present_p (extlang))
566 1.3 christos {
567 1.3 christos script_sourcer_func *sourcer
568 1.3 christos = ext_lang_script_sourcer (extlang);
569 1.3 christos
570 1.3 christos gdb_assert (sourcer != NULL);
571 1.3 christos sourcer (extlang, stream, file_to_open);
572 1.3 christos return;
573 1.3 christos }
574 1.3 christos else if (script_ext_mode == script_ext_soft)
575 1.1 christos {
576 1.1 christos /* Assume the file is a gdb script.
577 1.3 christos This is handled below. */
578 1.3 christos }
579 1.1 christos else
580 1.1 christos throw_ext_lang_unsupported (extlang);
581 1.1 christos }
582 1.1 christos }
583 1.1 christos
584 1.1 christos script_from_file (stream, file);
585 1.1 christos }
586 1.1 christos
587 1.1 christos /* Worker to perform the "source" command.
588 1.1 christos Load script FILE.
589 1.1 christos If SEARCH_PATH is non-zero, and the file isn't found in cwd,
590 1.1 christos search for it in the source search path. */
591 1.1 christos
592 1.1 christos static void
593 1.1 christos source_script_with_search (const char *file, int from_tty, int search_path)
594 1.1 christos {
595 1.1 christos FILE *stream;
596 1.1 christos char *full_path;
597 1.1 christos struct cleanup *old_cleanups;
598 1.1 christos
599 1.1 christos if (file == NULL || *file == 0)
600 1.1 christos error (_("source command requires file name of file to source."));
601 1.1 christos
602 1.1 christos if (!find_and_open_script (file, search_path, &stream, &full_path))
603 1.1 christos {
604 1.1 christos /* The script wasn't found, or was otherwise inaccessible.
605 1.1 christos If the source command was invoked interactively, throw an
606 1.1 christos error. Otherwise (e.g. if it was invoked by a script),
607 1.1 christos just emit a warning, rather than cause an error. */
608 1.1 christos if (from_tty)
609 1.1 christos perror_with_name (file);
610 1.1 christos else
611 1.1 christos {
612 1.1 christos perror_warning_with_name (file);
613 1.1 christos return;
614 1.1 christos }
615 1.1 christos }
616 1.1 christos
617 1.1 christos old_cleanups = make_cleanup (xfree, full_path);
618 1.6 christos make_cleanup_fclose (stream);
619 1.1 christos /* The python support reopens the file, so we need to pass full_path here
620 1.1 christos in case the file was found on the search path. It's useful to do this
621 1.1 christos anyway so that error messages show the actual file used. But only do
622 1.1 christos this if we (may have) used search_path, as printing the full path in
623 1.1 christos errors for the non-search case can be more noise than signal. */
624 1.1 christos source_script_from_stream (stream, file, search_path ? full_path : file);
625 1.1 christos do_cleanups (old_cleanups);
626 1.1 christos }
627 1.1 christos
628 1.1 christos /* Wrapper around source_script_with_search to export it to main.c
629 1.1 christos for use in loading .gdbinit scripts. */
630 1.1 christos
631 1.1 christos void
632 1.1 christos source_script (const char *file, int from_tty)
633 1.1 christos {
634 1.1 christos source_script_with_search (file, from_tty, 0);
635 1.1 christos }
636 1.1 christos
637 1.1 christos /* Return the source_verbose global variable to its previous state
638 1.1 christos on exit from the source command, by whatever means. */
639 1.1 christos static void
640 1.1 christos source_verbose_cleanup (void *old_value)
641 1.1 christos {
642 1.1 christos source_verbose = *(int *)old_value;
643 1.1 christos xfree (old_value);
644 1.1 christos }
645 1.6 christos
646 1.1 christos static void
647 1.1 christos source_command (char *args, int from_tty)
648 1.1 christos {
649 1.1 christos struct cleanup *old_cleanups;
650 1.1 christos char *file = args;
651 1.1 christos int *old_source_verbose = XNEW (int);
652 1.1 christos int search_path = 0;
653 1.1 christos
654 1.1 christos *old_source_verbose = source_verbose;
655 1.1 christos old_cleanups = make_cleanup (source_verbose_cleanup,
656 1.1 christos old_source_verbose);
657 1.1 christos
658 1.1 christos /* -v causes the source command to run in verbose mode.
659 1.1 christos -s causes the file to be searched in the source search path,
660 1.1 christos even if the file name contains a '/'.
661 1.1 christos We still have to be able to handle filenames with spaces in a
662 1.1 christos backward compatible way, so buildargv is not appropriate. */
663 1.1 christos
664 1.1 christos if (args)
665 1.1 christos {
666 1.1 christos while (args[0] != '\0')
667 1.1 christos {
668 1.1 christos /* Make sure leading white space does not break the
669 1.1 christos comparisons. */
670 1.1 christos args = skip_spaces (args);
671 1.1 christos
672 1.1 christos if (args[0] != '-')
673 1.1 christos break;
674 1.1 christos
675 1.1 christos if (args[1] == 'v' && isspace (args[2]))
676 1.1 christos {
677 1.1 christos source_verbose = 1;
678 1.1 christos
679 1.1 christos /* Skip passed -v. */
680 1.1 christos args = &args[3];
681 1.1 christos }
682 1.1 christos else if (args[1] == 's' && isspace (args[2]))
683 1.1 christos {
684 1.1 christos search_path = 1;
685 1.1 christos
686 1.1 christos /* Skip passed -s. */
687 1.1 christos args = &args[3];
688 1.1 christos }
689 1.1 christos else
690 1.1 christos break;
691 1.1 christos }
692 1.1 christos
693 1.1 christos file = skip_spaces (args);
694 1.1 christos }
695 1.1 christos
696 1.1 christos source_script_with_search (file, from_tty, search_path);
697 1.1 christos
698 1.1 christos do_cleanups (old_cleanups);
699 1.1 christos }
700 1.1 christos
701 1.1 christos
702 1.1 christos static void
703 1.1 christos echo_command (char *text, int from_tty)
704 1.1 christos {
705 1.1 christos const char *p = text;
706 1.1 christos int c;
707 1.1 christos
708 1.1 christos if (text)
709 1.1 christos while ((c = *p++) != '\0')
710 1.1 christos {
711 1.1 christos if (c == '\\')
712 1.1 christos {
713 1.1 christos /* \ at end of argument is used after spaces
714 1.1 christos so they won't be lost. */
715 1.1 christos if (*p == 0)
716 1.1 christos return;
717 1.1 christos
718 1.1 christos c = parse_escape (get_current_arch (), &p);
719 1.1 christos if (c >= 0)
720 1.1 christos printf_filtered ("%c", c);
721 1.1 christos }
722 1.1 christos else
723 1.1 christos printf_filtered ("%c", c);
724 1.1 christos }
725 1.1 christos
726 1.1 christos /* Force this output to appear now. */
727 1.1 christos wrap_here ("");
728 1.1 christos gdb_flush (gdb_stdout);
729 1.1 christos }
730 1.1 christos
731 1.1 christos static void
732 1.1 christos shell_escape (char *arg, int from_tty)
733 1.1 christos {
734 1.1 christos #if defined(CANT_FORK) || \
735 1.1 christos (!defined(HAVE_WORKING_VFORK) && !defined(HAVE_WORKING_FORK))
736 1.1 christos /* If ARG is NULL, they want an inferior shell, but `system' just
737 1.1 christos reports if the shell is available when passed a NULL arg. */
738 1.1 christos int rc = system (arg ? arg : "");
739 1.1 christos
740 1.1 christos if (!arg)
741 1.1 christos arg = "inferior shell";
742 1.1 christos
743 1.1 christos if (rc == -1)
744 1.1 christos {
745 1.1 christos fprintf_unfiltered (gdb_stderr, "Cannot execute %s: %s\n", arg,
746 1.1 christos safe_strerror (errno));
747 1.1 christos gdb_flush (gdb_stderr);
748 1.1 christos }
749 1.1 christos else if (rc)
750 1.1 christos {
751 1.1 christos fprintf_unfiltered (gdb_stderr, "%s exited with status %d\n", arg, rc);
752 1.1 christos gdb_flush (gdb_stderr);
753 1.1 christos }
754 1.1 christos #ifdef GLOBAL_CURDIR
755 1.1 christos /* Make sure to return to the directory GDB thinks it is, in case
756 1.1 christos the shell command we just ran changed it. */
757 1.1 christos chdir (current_directory);
758 1.1 christos #endif
759 1.1 christos #else /* Can fork. */
760 1.1 christos int status, pid;
761 1.1 christos
762 1.1 christos if ((pid = vfork ()) == 0)
763 1.1 christos {
764 1.1 christos const char *p, *user_shell;
765 1.1 christos
766 1.1 christos close_most_fds ();
767 1.1 christos
768 1.1 christos if ((user_shell = (char *) getenv ("SHELL")) == NULL)
769 1.1 christos user_shell = "/bin/sh";
770 1.1 christos
771 1.1 christos /* Get the name of the shell for arg0. */
772 1.1 christos p = lbasename (user_shell);
773 1.1 christos
774 1.1 christos if (!arg)
775 1.1 christos execl (user_shell, p, (char *) 0);
776 1.1 christos else
777 1.1 christos execl (user_shell, p, "-c", arg, (char *) 0);
778 1.1 christos
779 1.1 christos fprintf_unfiltered (gdb_stderr, "Cannot execute %s: %s\n", user_shell,
780 1.1 christos safe_strerror (errno));
781 1.1 christos gdb_flush (gdb_stderr);
782 1.1 christos _exit (0177);
783 1.1 christos }
784 1.1 christos
785 1.1 christos if (pid != -1)
786 1.1 christos waitpid (pid, &status, 0);
787 1.1 christos else
788 1.1 christos error (_("Fork failed"));
789 1.1 christos #endif /* Can fork. */
790 1.1 christos }
791 1.1 christos
792 1.1 christos static void
793 1.1 christos edit_command (char *arg, int from_tty)
794 1.1 christos {
795 1.1 christos struct symtabs_and_lines sals;
796 1.1 christos struct symtab_and_line sal;
797 1.1 christos struct symbol *sym;
798 1.1 christos char *editor;
799 1.1 christos char *p;
800 1.1 christos const char *fn;
801 1.1 christos
802 1.1 christos /* Pull in the current default source line if necessary. */
803 1.1 christos if (arg == 0)
804 1.1 christos {
805 1.1 christos set_default_source_symtab_and_line ();
806 1.1 christos sal = get_current_source_symtab_and_line ();
807 1.1 christos }
808 1.1 christos
809 1.1 christos /* Bare "edit" edits file with present line. */
810 1.1 christos
811 1.1 christos if (arg == 0)
812 1.1 christos {
813 1.6 christos if (sal.symtab == 0)
814 1.6 christos error (_("No default source file yet."));
815 1.6 christos sal.line += get_lines_to_list () / 2;
816 1.6 christos }
817 1.1 christos else
818 1.1 christos {
819 1.6 christos struct cleanup *cleanup;
820 1.6 christos struct event_location *location;
821 1.6 christos char *arg1;
822 1.1 christos
823 1.1 christos /* Now should only be one argument -- decode it in SAL. */
824 1.1 christos arg1 = arg;
825 1.1 christos location = string_to_event_location (&arg1, current_language);
826 1.1 christos cleanup = make_cleanup_delete_event_location (location);
827 1.6 christos sals = decode_line_1 (location, DECODE_LINE_LIST_MODE, NULL, NULL, 0);
828 1.1 christos
829 1.1 christos filter_sals (&sals);
830 1.1 christos if (! sals.nelts)
831 1.1 christos {
832 1.1 christos /* C++ */
833 1.1 christos do_cleanups (cleanup);
834 1.6 christos return;
835 1.1 christos }
836 1.1 christos if (sals.nelts > 1)
837 1.1 christos {
838 1.1 christos ambiguous_line_spec (&sals);
839 1.1 christos xfree (sals.sals);
840 1.1 christos do_cleanups (cleanup);
841 1.1 christos return;
842 1.1 christos }
843 1.1 christos
844 1.1 christos sal = sals.sals[0];
845 1.1 christos xfree (sals.sals);
846 1.1 christos
847 1.1 christos if (*arg1)
848 1.1 christos error (_("Junk at end of line specification."));
849 1.1 christos
850 1.1 christos /* If line was specified by address, first print exactly which
851 1.1 christos line, and which file. In this case, sal.symtab == 0 means
852 1.1 christos address is outside of all known source files, not that user
853 1.1 christos failed to give a filename. */
854 1.1 christos if (*arg == '*')
855 1.1 christos {
856 1.3 christos struct gdbarch *gdbarch;
857 1.1 christos
858 1.1 christos if (sal.symtab == 0)
859 1.1 christos error (_("No source file for address %s."),
860 1.1 christos paddress (get_current_arch (), sal.pc));
861 1.1 christos
862 1.1 christos gdbarch = get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
863 1.1 christos sym = find_pc_function (sal.pc);
864 1.1 christos if (sym)
865 1.1 christos printf_filtered ("%s is in %s (%s:%d).\n",
866 1.1 christos paddress (gdbarch, sal.pc),
867 1.1 christos SYMBOL_PRINT_NAME (sym),
868 1.1 christos symtab_to_filename_for_display (sal.symtab),
869 1.1 christos sal.line);
870 1.1 christos else
871 1.1 christos printf_filtered ("%s is at %s:%d.\n",
872 1.1 christos paddress (gdbarch, sal.pc),
873 1.1 christos symtab_to_filename_for_display (sal.symtab),
874 1.1 christos sal.line);
875 1.1 christos }
876 1.6 christos
877 1.1 christos /* If what was given does not imply a symtab, it must be an
878 1.1 christos undebuggable symbol which means no source code. */
879 1.1 christos
880 1.1 christos if (sal.symtab == 0)
881 1.1 christos error (_("No line number known for %s."), arg);
882 1.1 christos do_cleanups (cleanup);
883 1.1 christos }
884 1.1 christos
885 1.1 christos if ((editor = (char *) getenv ("EDITOR")) == NULL)
886 1.1 christos editor = "/bin/ex";
887 1.1 christos
888 1.1 christos fn = symtab_to_fullname (sal.symtab);
889 1.1 christos
890 1.1 christos /* Quote the file name, in case it has whitespace or other special
891 1.1 christos characters. */
892 1.1 christos p = xstrprintf ("%s +%d \"%s\"", editor, sal.line, fn);
893 1.1 christos shell_escape (p, from_tty);
894 1.1 christos xfree (p);
895 1.1 christos }
896 1.1 christos
897 1.1 christos static void
898 1.1 christos list_command (char *arg, int from_tty)
899 1.1 christos {
900 1.1 christos struct symtabs_and_lines sals, sals_end;
901 1.1 christos struct symtab_and_line sal = { 0 };
902 1.1 christos struct symtab_and_line sal_end = { 0 };
903 1.1 christos struct symtab_and_line cursal = { 0 };
904 1.1 christos struct symbol *sym;
905 1.6 christos char *arg1;
906 1.6 christos int no_end = 1;
907 1.6 christos int dummy_end = 0;
908 1.1 christos int dummy_beg = 0;
909 1.1 christos int linenum_beg = 0;
910 1.6 christos char *p;
911 1.1 christos struct cleanup *cleanup;
912 1.1 christos
913 1.1 christos cleanup = make_cleanup (null_cleanup, NULL);
914 1.3 christos
915 1.3 christos /* Pull in the current default source line if necessary. */
916 1.3 christos if (arg == NULL || ((arg[0] == '+' || arg[0] == '-') && arg[1] == '\0'))
917 1.3 christos {
918 1.3 christos set_default_source_symtab_and_line ();
919 1.3 christos cursal = get_current_source_symtab_and_line ();
920 1.3 christos
921 1.3 christos /* If this is the first "list" since we've set the current
922 1.3 christos source line, center the listing around that line. */
923 1.3 christos if (get_first_line_listed () == 0)
924 1.3 christos {
925 1.3 christos int first;
926 1.3 christos
927 1.3 christos first = max (cursal.line - get_lines_to_list () / 2, 1);
928 1.3 christos
929 1.3 christos /* A small special case --- if listing backwards, and we
930 1.3 christos should list only one line, list the preceding line,
931 1.3 christos instead of the exact line we've just shown after e.g.,
932 1.3 christos stopping for a breakpoint. */
933 1.3 christos if (arg != NULL && arg[0] == '-'
934 1.1 christos && get_lines_to_list () == 1 && first > 1)
935 1.6 christos first -= 1;
936 1.6 christos
937 1.6 christos print_source_lines (cursal.symtab, first,
938 1.6 christos first + get_lines_to_list (), 0);
939 1.6 christos }
940 1.6 christos
941 1.6 christos /* "l" or "l +" lists next ten lines. */
942 1.6 christos else if (arg == NULL || arg[0] == '+')
943 1.6 christos print_source_lines (cursal.symtab, cursal.line,
944 1.6 christos cursal.line + get_lines_to_list (), 0);
945 1.6 christos
946 1.6 christos /* "l -" lists previous ten lines, the ones before the ten just
947 1.6 christos listed. */
948 1.6 christos else if (arg[0] == '-')
949 1.6 christos {
950 1.6 christos if (get_first_line_listed () == 1)
951 1.6 christos error (_("Already at the start of %s."),
952 1.1 christos symtab_to_filename_for_display (cursal.symtab));
953 1.1 christos print_source_lines (cursal.symtab,
954 1.1 christos max (get_first_line_listed ()
955 1.1 christos - get_lines_to_list (), 1),
956 1.1 christos get_first_line_listed (), 0);
957 1.1 christos }
958 1.1 christos
959 1.1 christos return;
960 1.1 christos }
961 1.1 christos
962 1.1 christos /* Now if there is only one argument, decode it in SAL
963 1.1 christos and set NO_END.
964 1.1 christos If there are two arguments, decode them in SAL and SAL_END
965 1.1 christos and clear NO_END; however, if one of the arguments is blank,
966 1.1 christos set DUMMY_BEG or DUMMY_END to record that fact. */
967 1.1 christos
968 1.1 christos if (!have_full_symbols () && !have_partial_symbols ())
969 1.1 christos error (_("No symbol table is loaded. Use the \"file\" command."));
970 1.6 christos
971 1.6 christos arg1 = arg;
972 1.6 christos if (*arg1 == ',')
973 1.6 christos dummy_beg = 1;
974 1.6 christos else
975 1.1 christos {
976 1.1 christos struct event_location *location;
977 1.1 christos
978 1.6 christos location = string_to_event_location (&arg1, current_language);
979 1.6 christos make_cleanup_delete_event_location (location);
980 1.6 christos sals = decode_line_1 (location, DECODE_LINE_LIST_MODE, NULL, NULL, 0);
981 1.6 christos
982 1.6 christos filter_sals (&sals);
983 1.1 christos if (!sals.nelts)
984 1.1 christos {
985 1.1 christos /* C++ */
986 1.1 christos do_cleanups (cleanup);
987 1.6 christos return;
988 1.1 christos }
989 1.1 christos if (sals.nelts > 1)
990 1.1 christos {
991 1.1 christos ambiguous_line_spec (&sals);
992 1.1 christos xfree (sals.sals);
993 1.1 christos do_cleanups (cleanup);
994 1.1 christos return;
995 1.1 christos }
996 1.1 christos
997 1.1 christos sal = sals.sals[0];
998 1.1 christos xfree (sals.sals);
999 1.1 christos }
1000 1.1 christos
1001 1.1 christos /* Record whether the BEG arg is all digits. */
1002 1.1 christos
1003 1.1 christos for (p = arg; p != arg1 && *p >= '0' && *p <= '9'; p++);
1004 1.1 christos linenum_beg = (p == arg1);
1005 1.1 christos
1006 1.1 christos while (*arg1 == ' ' || *arg1 == '\t')
1007 1.1 christos arg1++;
1008 1.1 christos if (*arg1 == ',')
1009 1.1 christos {
1010 1.1 christos no_end = 0;
1011 1.1 christos arg1++;
1012 1.6 christos while (*arg1 == ' ' || *arg1 == '\t')
1013 1.6 christos arg1++;
1014 1.6 christos if (*arg1 == 0)
1015 1.6 christos dummy_end = 1;
1016 1.1 christos else
1017 1.6 christos {
1018 1.6 christos struct event_location *location;
1019 1.1 christos
1020 1.6 christos location = string_to_event_location (&arg1, current_language);
1021 1.6 christos make_cleanup_delete_event_location (location);
1022 1.6 christos if (dummy_beg)
1023 1.1 christos sals_end = decode_line_1 (location,
1024 1.1 christos DECODE_LINE_LIST_MODE, NULL, NULL, 0);
1025 1.6 christos else
1026 1.6 christos sals_end = decode_line_1 (location, DECODE_LINE_LIST_MODE,
1027 1.6 christos NULL, sal.symtab, sal.line);
1028 1.6 christos
1029 1.1 christos filter_sals (&sals_end);
1030 1.1 christos if (sals_end.nelts == 0)
1031 1.1 christos {
1032 1.1 christos do_cleanups (cleanup);
1033 1.6 christos return;
1034 1.1 christos }
1035 1.1 christos if (sals_end.nelts > 1)
1036 1.1 christos {
1037 1.1 christos ambiguous_line_spec (&sals_end);
1038 1.1 christos xfree (sals_end.sals);
1039 1.1 christos do_cleanups (cleanup);
1040 1.1 christos return;
1041 1.1 christos }
1042 1.1 christos sal_end = sals_end.sals[0];
1043 1.1 christos xfree (sals_end.sals);
1044 1.1 christos }
1045 1.1 christos }
1046 1.1 christos
1047 1.1 christos if (*arg1)
1048 1.1 christos error (_("Junk at end of line specification."));
1049 1.1 christos
1050 1.1 christos if (!no_end && !dummy_beg && !dummy_end
1051 1.1 christos && sal.symtab != sal_end.symtab)
1052 1.1 christos error (_("Specified start and end are in different files."));
1053 1.1 christos if (dummy_beg && dummy_end)
1054 1.1 christos error (_("Two empty args do not say what lines to list."));
1055 1.1 christos
1056 1.1 christos /* If line was specified by address,
1057 1.1 christos first print exactly which line, and which file.
1058 1.1 christos
1059 1.1 christos In this case, sal.symtab == 0 means address is outside of all
1060 1.1 christos known source files, not that user failed to give a filename. */
1061 1.1 christos if (*arg == '*')
1062 1.1 christos {
1063 1.3 christos struct gdbarch *gdbarch;
1064 1.1 christos
1065 1.1 christos if (sal.symtab == 0)
1066 1.1 christos error (_("No source file for address %s."),
1067 1.1 christos paddress (get_current_arch (), sal.pc));
1068 1.1 christos
1069 1.1 christos gdbarch = get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
1070 1.1 christos sym = find_pc_function (sal.pc);
1071 1.1 christos if (sym)
1072 1.1 christos printf_filtered ("%s is in %s (%s:%d).\n",
1073 1.1 christos paddress (gdbarch, sal.pc),
1074 1.1 christos SYMBOL_PRINT_NAME (sym),
1075 1.1 christos symtab_to_filename_for_display (sal.symtab), sal.line);
1076 1.1 christos else
1077 1.1 christos printf_filtered ("%s is at %s:%d.\n",
1078 1.1 christos paddress (gdbarch, sal.pc),
1079 1.1 christos symtab_to_filename_for_display (sal.symtab), sal.line);
1080 1.1 christos }
1081 1.1 christos
1082 1.1 christos /* If line was not specified by just a line number, and it does not
1083 1.1 christos imply a symtab, it must be an undebuggable symbol which means no
1084 1.1 christos source code. */
1085 1.1 christos
1086 1.1 christos if (!linenum_beg && sal.symtab == 0)
1087 1.1 christos error (_("No line number known for %s."), arg);
1088 1.1 christos
1089 1.1 christos /* If this command is repeated with RET,
1090 1.1 christos turn it into the no-arg variant. */
1091 1.1 christos
1092 1.1 christos if (from_tty)
1093 1.1 christos *arg = 0;
1094 1.1 christos
1095 1.1 christos if (dummy_beg && sal_end.symtab == 0)
1096 1.1 christos error (_("No default source file yet. Do \"help list\"."));
1097 1.1 christos if (dummy_beg)
1098 1.1 christos print_source_lines (sal_end.symtab,
1099 1.1 christos max (sal_end.line - (get_lines_to_list () - 1), 1),
1100 1.1 christos sal_end.line + 1, 0);
1101 1.1 christos else if (sal.symtab == 0)
1102 1.1 christos error (_("No default source file yet. Do \"help list\"."));
1103 1.1 christos else if (no_end)
1104 1.1 christos {
1105 1.1 christos int first_line = sal.line - get_lines_to_list () / 2;
1106 1.1 christos
1107 1.1 christos if (first_line < 1) first_line = 1;
1108 1.1 christos
1109 1.1 christos print_source_lines (sal.symtab,
1110 1.1 christos first_line,
1111 1.1 christos first_line + get_lines_to_list (),
1112 1.1 christos 0);
1113 1.1 christos }
1114 1.6 christos else
1115 1.1 christos print_source_lines (sal.symtab, sal.line,
1116 1.1 christos (dummy_end
1117 1.1 christos ? sal.line + get_lines_to_list ()
1118 1.1 christos : sal_end.line + 1),
1119 1.1 christos 0);
1120 1.1 christos do_cleanups (cleanup);
1121 1.1 christos }
1122 1.1 christos
1123 1.1 christos /* Subroutine of disassemble_command to simplify it.
1124 1.1 christos Perform the disassembly.
1125 1.1 christos NAME is the name of the function if known, or NULL.
1126 1.1 christos [LOW,HIGH) are the range of addresses to disassemble.
1127 1.1 christos MIXED is non-zero to print source with the assembler. */
1128 1.1 christos
1129 1.1 christos static void
1130 1.1 christos print_disassembly (struct gdbarch *gdbarch, const char *name,
1131 1.1 christos CORE_ADDR low, CORE_ADDR high, int flags)
1132 1.1 christos {
1133 1.1 christos #if defined(TUI)
1134 1.1 christos if (!tui_is_window_visible (DISASSEM_WIN))
1135 1.1 christos #endif
1136 1.1 christos {
1137 1.1 christos printf_filtered ("Dump of assembler code ");
1138 1.1 christos if (name != NULL)
1139 1.1 christos printf_filtered ("for function %s:\n", name);
1140 1.1 christos else
1141 1.1 christos printf_filtered ("from %s to %s:\n",
1142 1.1 christos paddress (gdbarch, low), paddress (gdbarch, high));
1143 1.1 christos
1144 1.1 christos /* Dump the specified range. */
1145 1.1 christos gdb_disassembly (gdbarch, current_uiout, 0, flags, -1, low, high);
1146 1.1 christos
1147 1.1 christos printf_filtered ("End of assembler dump.\n");
1148 1.1 christos gdb_flush (gdb_stdout);
1149 1.1 christos }
1150 1.1 christos #if defined(TUI)
1151 1.1 christos else
1152 1.1 christos {
1153 1.1 christos tui_show_assembly (gdbarch, low);
1154 1.1 christos }
1155 1.1 christos #endif
1156 1.1 christos }
1157 1.1 christos
1158 1.1 christos /* Subroutine of disassemble_command to simplify it.
1159 1.1 christos Print a disassembly of the current function according to FLAGS. */
1160 1.1 christos
1161 1.1 christos static void
1162 1.1 christos disassemble_current_function (int flags)
1163 1.1 christos {
1164 1.1 christos struct frame_info *frame;
1165 1.1 christos struct gdbarch *gdbarch;
1166 1.1 christos CORE_ADDR low, high, pc;
1167 1.1 christos const char *name;
1168 1.1 christos
1169 1.1 christos frame = get_selected_frame (_("No frame selected."));
1170 1.1 christos gdbarch = get_frame_arch (frame);
1171 1.1 christos pc = get_frame_address_in_block (frame);
1172 1.1 christos if (find_pc_partial_function (pc, &name, &low, &high) == 0)
1173 1.1 christos error (_("No function contains program counter for selected frame."));
1174 1.1 christos #if defined(TUI)
1175 1.1 christos /* NOTE: cagney/2003-02-13 The `tui_active' was previously
1176 1.1 christos `tui_version'. */
1177 1.1 christos if (tui_active)
1178 1.1 christos /* FIXME: cagney/2004-02-07: This should be an observer. */
1179 1.1 christos low = tui_get_low_disassembly_address (gdbarch, low, pc);
1180 1.1 christos #endif
1181 1.1 christos low += gdbarch_deprecated_function_start_offset (gdbarch);
1182 1.1 christos
1183 1.6 christos print_disassembly (gdbarch, name, low, high, flags);
1184 1.1 christos }
1185 1.6 christos
1186 1.1 christos /* Dump a specified section of assembly code.
1187 1.6 christos
1188 1.6 christos Usage:
1189 1.1 christos disassemble [/mrs]
1190 1.1 christos - dump the assembly code for the function of the current pc
1191 1.6 christos disassemble [/mrs] addr
1192 1.6 christos - dump the assembly code for the function at ADDR
1193 1.6 christos disassemble [/mrs] low,high
1194 1.6 christos disassemble [/mrs] low,+length
1195 1.6 christos - dump the assembly code in the range [LOW,HIGH), or [LOW,LOW+length)
1196 1.6 christos
1197 1.6 christos A /m modifier will include source code with the assembly in a
1198 1.6 christos "source centric" view. This view lists only the file of the first insn,
1199 1.6 christos even if other source files are involved (e.g., inlined functions), and
1200 1.6 christos the output is in source order, even with optimized code. This view is
1201 1.6 christos considered deprecated as it hasn't been useful in practice.
1202 1.6 christos
1203 1.1 christos A /r modifier will include raw instructions in hex with the assembly.
1204 1.1 christos
1205 1.1 christos A /s modifier will include source code with the assembly, like /m, with
1206 1.1 christos two important differences:
1207 1.1 christos 1) The output is still in pc address order.
1208 1.1 christos 2) File names and contents for all relevant source files are displayed. */
1209 1.1 christos
1210 1.1 christos static void
1211 1.1 christos disassemble_command (char *arg, int from_tty)
1212 1.1 christos {
1213 1.1 christos struct gdbarch *gdbarch = get_current_arch ();
1214 1.1 christos CORE_ADDR low, high;
1215 1.1 christos const char *name;
1216 1.1 christos CORE_ADDR pc;
1217 1.1 christos int flags;
1218 1.1 christos const char *p;
1219 1.1 christos
1220 1.1 christos p = arg;
1221 1.1 christos name = NULL;
1222 1.1 christos flags = 0;
1223 1.1 christos
1224 1.1 christos if (p && *p == '/')
1225 1.1 christos {
1226 1.1 christos ++p;
1227 1.1 christos
1228 1.1 christos if (*p == '\0')
1229 1.1 christos error (_("Missing modifier."));
1230 1.6 christos
1231 1.1 christos while (*p && ! isspace (*p))
1232 1.1 christos {
1233 1.1 christos switch (*p++)
1234 1.1 christos {
1235 1.6 christos case 'm':
1236 1.6 christos flags |= DISASSEMBLY_SOURCE_DEPRECATED;
1237 1.6 christos break;
1238 1.1 christos case 'r':
1239 1.1 christos flags |= DISASSEMBLY_RAW_INSN;
1240 1.1 christos break;
1241 1.1 christos case 's':
1242 1.1 christos flags |= DISASSEMBLY_SOURCE;
1243 1.1 christos break;
1244 1.1 christos default:
1245 1.1 christos error (_("Invalid disassembly modifier."));
1246 1.6 christos }
1247 1.6 christos }
1248 1.6 christos
1249 1.6 christos p = skip_spaces_const (p);
1250 1.1 christos }
1251 1.1 christos
1252 1.1 christos if ((flags & (DISASSEMBLY_SOURCE_DEPRECATED | DISASSEMBLY_SOURCE))
1253 1.1 christos == (DISASSEMBLY_SOURCE_DEPRECATED | DISASSEMBLY_SOURCE))
1254 1.1 christos error (_("Cannot specify both /m and /s."));
1255 1.1 christos
1256 1.1 christos if (! p || ! *p)
1257 1.1 christos {
1258 1.1 christos flags |= DISASSEMBLY_OMIT_FNAME;
1259 1.1 christos disassemble_current_function (flags);
1260 1.1 christos return;
1261 1.1 christos }
1262 1.1 christos
1263 1.1 christos pc = value_as_address (parse_to_comma_and_eval (&p));
1264 1.1 christos if (p[0] == ',')
1265 1.1 christos ++p;
1266 1.1 christos if (p[0] == '\0')
1267 1.1 christos {
1268 1.1 christos /* One argument. */
1269 1.1 christos if (find_pc_partial_function (pc, &name, &low, &high) == 0)
1270 1.1 christos error (_("No function contains specified address."));
1271 1.1 christos #if defined(TUI)
1272 1.1 christos /* NOTE: cagney/2003-02-13 The `tui_active' was previously
1273 1.1 christos `tui_version'. */
1274 1.1 christos if (tui_active)
1275 1.1 christos /* FIXME: cagney/2004-02-07: This should be an observer. */
1276 1.1 christos low = tui_get_low_disassembly_address (gdbarch, low, pc);
1277 1.1 christos #endif
1278 1.1 christos low += gdbarch_deprecated_function_start_offset (gdbarch);
1279 1.1 christos flags |= DISASSEMBLY_OMIT_FNAME;
1280 1.1 christos }
1281 1.1 christos else
1282 1.1 christos {
1283 1.1 christos /* Two arguments. */
1284 1.1 christos int incl_flag = 0;
1285 1.1 christos low = pc;
1286 1.1 christos p = skip_spaces_const (p);
1287 1.1 christos if (p[0] == '+')
1288 1.1 christos {
1289 1.1 christos ++p;
1290 1.1 christos incl_flag = 1;
1291 1.1 christos }
1292 1.1 christos high = parse_and_eval_address (p);
1293 1.1 christos if (incl_flag)
1294 1.1 christos high += low;
1295 1.1 christos }
1296 1.1 christos
1297 1.1 christos print_disassembly (gdbarch, name, low, high, flags);
1298 1.1 christos }
1299 1.1 christos
1300 1.1 christos static void
1301 1.1 christos make_command (char *arg, int from_tty)
1302 1.1 christos {
1303 1.6 christos char *p;
1304 1.1 christos
1305 1.1 christos if (arg == 0)
1306 1.1 christos p = "make";
1307 1.1 christos else
1308 1.1 christos {
1309 1.1 christos p = (char *) xmalloc (sizeof ("make ") + strlen (arg));
1310 1.1 christos strcpy (p, "make ");
1311 1.1 christos strcpy (p + sizeof ("make ") - 1, arg);
1312 1.1 christos }
1313 1.1 christos
1314 1.1 christos shell_escape (p, from_tty);
1315 1.1 christos }
1316 1.1 christos
1317 1.1 christos static void
1318 1.1 christos show_user (char *args, int from_tty)
1319 1.1 christos {
1320 1.1 christos struct cmd_list_element *c;
1321 1.1 christos extern struct cmd_list_element *cmdlist;
1322 1.3 christos
1323 1.1 christos if (args)
1324 1.1 christos {
1325 1.1 christos const char *comname = args;
1326 1.1 christos
1327 1.1 christos c = lookup_cmd (&comname, cmdlist, "", 0, 1);
1328 1.1 christos if (!cli_user_command_p (c))
1329 1.1 christos error (_("Not a user command."));
1330 1.3 christos show_user_1 (c, "", args, gdb_stdout);
1331 1.1 christos }
1332 1.1 christos else
1333 1.1 christos {
1334 1.1 christos for (c = cmdlist; c; c = c->next)
1335 1.1 christos {
1336 1.1 christos if (cli_user_command_p (c) || c->prefixlist != NULL)
1337 1.1 christos show_user_1 (c, "", c->name, gdb_stdout);
1338 1.1 christos }
1339 1.1 christos }
1340 1.1 christos }
1341 1.1 christos
1342 1.1 christos /* Search through names of commands and documentations for a certain
1343 1.1 christos regular expression. */
1344 1.1 christos
1345 1.1 christos static void
1346 1.1 christos apropos_command (char *searchstr, int from_tty)
1347 1.1 christos {
1348 1.1 christos regex_t pattern;
1349 1.1 christos int code;
1350 1.1 christos
1351 1.1 christos if (searchstr == NULL)
1352 1.1 christos error (_("REGEXP string is empty"));
1353 1.1 christos
1354 1.1 christos code = regcomp (&pattern, searchstr, REG_ICASE);
1355 1.1 christos if (code == 0)
1356 1.1 christos {
1357 1.1 christos struct cleanup *cleanups;
1358 1.1 christos
1359 1.1 christos cleanups = make_regfree_cleanup (&pattern);
1360 1.1 christos apropos_cmd (gdb_stdout, cmdlist, &pattern, "");
1361 1.1 christos do_cleanups (cleanups);
1362 1.1 christos }
1363 1.1 christos else
1364 1.1 christos {
1365 1.1 christos char *err = get_regcomp_error (code, &pattern);
1366 1.1 christos
1367 1.1 christos make_cleanup (xfree, err);
1368 1.1 christos error (_("Error in regular expression: %s"), err);
1369 1.1 christos }
1370 1.1 christos }
1371 1.1 christos
1372 1.1 christos /* Subroutine of alias_command to simplify it.
1373 1.1 christos Return the first N elements of ARGV flattened back to a string
1374 1.1 christos with a space separating each element.
1375 1.1 christos ARGV may not be NULL.
1376 1.1 christos This does not take care of quoting elements in case they contain spaces
1377 1.1 christos on purpose. */
1378 1.1 christos
1379 1.1 christos static dyn_string_t
1380 1.1 christos argv_to_dyn_string (char **argv, int n)
1381 1.1 christos {
1382 1.1 christos int i;
1383 1.1 christos dyn_string_t result = dyn_string_new (10);
1384 1.1 christos
1385 1.1 christos gdb_assert (argv != NULL);
1386 1.1 christos gdb_assert (n >= 0 && n <= countargv (argv));
1387 1.1 christos
1388 1.1 christos for (i = 0; i < n; ++i)
1389 1.1 christos {
1390 1.1 christos if (i > 0)
1391 1.1 christos dyn_string_append_char (result, ' ');
1392 1.1 christos dyn_string_append_cstr (result, argv[i]);
1393 1.1 christos }
1394 1.1 christos
1395 1.1 christos return result;
1396 1.1 christos }
1397 1.1 christos
1398 1.1 christos /* Subroutine of alias_command to simplify it.
1399 1.1 christos Return TRUE if COMMAND exists, unambiguously. Otherwise FALSE. */
1400 1.1 christos
1401 1.1 christos static int
1402 1.1 christos valid_command_p (const char *command)
1403 1.1 christos {
1404 1.1 christos struct cmd_list_element *c;
1405 1.1 christos
1406 1.1 christos c = lookup_cmd_1 (& command, cmdlist, NULL, 1);
1407 1.1 christos
1408 1.1 christos if (c == NULL || c == (struct cmd_list_element *) -1)
1409 1.1 christos return FALSE;
1410 1.1 christos
1411 1.1 christos /* This is the slightly tricky part.
1412 1.1 christos lookup_cmd_1 will return a pointer to the last part of COMMAND
1413 1.6 christos to match, leaving COMMAND pointing at the remainder. */
1414 1.6 christos while (*command == ' ' || *command == '\t')
1415 1.6 christos ++command;
1416 1.6 christos return *command == '\0';
1417 1.6 christos }
1418 1.6 christos
1419 1.6 christos /* Called when "alias" was incorrectly used. */
1420 1.6 christos
1421 1.1 christos static void
1422 1.1 christos alias_usage_error (void)
1423 1.1 christos {
1424 1.1 christos error (_("Usage: alias [-a] [--] ALIAS = COMMAND"));
1425 1.1 christos }
1426 1.1 christos
1427 1.1 christos /* Make an alias of an existing command. */
1428 1.1 christos
1429 1.1 christos static void
1430 1.1 christos alias_command (char *args, int from_tty)
1431 1.1 christos {
1432 1.1 christos int i, alias_argc, command_argc;
1433 1.1 christos int abbrev_flag = 0;
1434 1.6 christos char *args2, *equals, *alias, *command;
1435 1.1 christos char **alias_argv, **command_argv;
1436 1.1 christos dyn_string_t alias_dyn_string, command_dyn_string;
1437 1.1 christos struct cleanup *cleanup;
1438 1.1 christos
1439 1.1 christos if (args == NULL || strchr (args, '=') == NULL)
1440 1.1 christos alias_usage_error ();
1441 1.1 christos
1442 1.1 christos args2 = xstrdup (args);
1443 1.1 christos cleanup = make_cleanup (xfree, args2);
1444 1.1 christos equals = strchr (args2, '=');
1445 1.1 christos *equals = '\0';
1446 1.1 christos alias_argv = gdb_buildargv (args2);
1447 1.1 christos make_cleanup_freeargv (alias_argv);
1448 1.1 christos command_argv = gdb_buildargv (equals + 1);
1449 1.1 christos make_cleanup_freeargv (command_argv);
1450 1.1 christos
1451 1.1 christos for (i = 0; alias_argv[i] != NULL; )
1452 1.1 christos {
1453 1.1 christos if (strcmp (alias_argv[i], "-a") == 0)
1454 1.1 christos {
1455 1.1 christos ++alias_argv;
1456 1.1 christos abbrev_flag = 1;
1457 1.1 christos }
1458 1.1 christos else if (strcmp (alias_argv[i], "--") == 0)
1459 1.1 christos {
1460 1.1 christos ++alias_argv;
1461 1.1 christos break;
1462 1.1 christos }
1463 1.6 christos else
1464 1.1 christos break;
1465 1.1 christos }
1466 1.1 christos
1467 1.1 christos if (alias_argv[0] == NULL || command_argv[0] == NULL
1468 1.1 christos || *alias_argv[0] == '\0' || *command_argv[0] == '\0')
1469 1.1 christos alias_usage_error ();
1470 1.1 christos
1471 1.1 christos for (i = 0; alias_argv[i] != NULL; ++i)
1472 1.1 christos {
1473 1.1 christos if (! valid_user_defined_cmd_name_p (alias_argv[i]))
1474 1.1 christos {
1475 1.1 christos if (i == 0)
1476 1.1 christos error (_("Invalid command name: %s"), alias_argv[i]);
1477 1.1 christos else
1478 1.1 christos error (_("Invalid command element name: %s"), alias_argv[i]);
1479 1.1 christos }
1480 1.1 christos }
1481 1.1 christos
1482 1.1 christos alias_argc = countargv (alias_argv);
1483 1.1 christos command_argc = countargv (command_argv);
1484 1.1 christos
1485 1.1 christos /* COMMAND must exist.
1486 1.1 christos Reconstruct the command to remove any extraneous spaces,
1487 1.1 christos for better error messages. */
1488 1.1 christos command_dyn_string = argv_to_dyn_string (command_argv, command_argc);
1489 1.1 christos make_cleanup_dyn_string_delete (command_dyn_string);
1490 1.1 christos command = dyn_string_buf (command_dyn_string);
1491 1.1 christos if (! valid_command_p (command))
1492 1.1 christos error (_("Invalid command to alias to: %s"), command);
1493 1.1 christos
1494 1.1 christos /* ALIAS must not exist. */
1495 1.1 christos alias_dyn_string = argv_to_dyn_string (alias_argv, alias_argc);
1496 1.1 christos make_cleanup_dyn_string_delete (alias_dyn_string);
1497 1.1 christos alias = dyn_string_buf (alias_dyn_string);
1498 1.1 christos if (valid_command_p (alias))
1499 1.1 christos error (_("Alias already exists: %s"), alias);
1500 1.1 christos
1501 1.1 christos /* If ALIAS is one word, it is an alias for the entire COMMAND.
1502 1.1 christos Example: alias spe = set print elements
1503 1.1 christos
1504 1.1 christos Otherwise ALIAS and COMMAND must have the same number of words,
1505 1.1 christos and every word except the last must match; and the last word of
1506 1.1 christos ALIAS is made an alias of the last word of COMMAND.
1507 1.1 christos Example: alias set print elms = set pr elem
1508 1.1 christos Note that unambiguous abbreviations are allowed. */
1509 1.1 christos
1510 1.1 christos if (alias_argc == 1)
1511 1.1 christos {
1512 1.1 christos /* add_cmd requires *we* allocate space for name, hence the xstrdup. */
1513 1.1 christos add_com_alias (xstrdup (alias_argv[0]), command, class_alias,
1514 1.1 christos abbrev_flag);
1515 1.1 christos }
1516 1.1 christos else
1517 1.1 christos {
1518 1.1 christos dyn_string_t alias_prefix_dyn_string, command_prefix_dyn_string;
1519 1.1 christos const char *alias_prefix, *command_prefix;
1520 1.1 christos struct cmd_list_element *c_alias, *c_command;
1521 1.1 christos
1522 1.1 christos if (alias_argc != command_argc)
1523 1.1 christos error (_("Mismatched command length between ALIAS and COMMAND."));
1524 1.1 christos
1525 1.1 christos /* Create copies of ALIAS and COMMAND without the last word,
1526 1.1 christos and use that to verify the leading elements match. */
1527 1.1 christos alias_prefix_dyn_string =
1528 1.1 christos argv_to_dyn_string (alias_argv, alias_argc - 1);
1529 1.1 christos make_cleanup_dyn_string_delete (alias_prefix_dyn_string);
1530 1.1 christos command_prefix_dyn_string =
1531 1.1 christos argv_to_dyn_string (alias_argv, command_argc - 1);
1532 1.1 christos make_cleanup_dyn_string_delete (command_prefix_dyn_string);
1533 1.1 christos alias_prefix = dyn_string_buf (alias_prefix_dyn_string);
1534 1.1 christos command_prefix = dyn_string_buf (command_prefix_dyn_string);
1535 1.1 christos
1536 1.1 christos c_command = lookup_cmd_1 (& command_prefix, cmdlist, NULL, 1);
1537 1.1 christos /* We've already tried to look up COMMAND. */
1538 1.1 christos gdb_assert (c_command != NULL
1539 1.1 christos && c_command != (struct cmd_list_element *) -1);
1540 1.1 christos gdb_assert (c_command->prefixlist != NULL);
1541 1.1 christos c_alias = lookup_cmd_1 (& alias_prefix, cmdlist, NULL, 1);
1542 1.1 christos if (c_alias != c_command)
1543 1.1 christos error (_("ALIAS and COMMAND prefixes do not match."));
1544 1.1 christos
1545 1.1 christos /* add_cmd requires *we* allocate space for name, hence the xstrdup. */
1546 1.1 christos add_alias_cmd (xstrdup (alias_argv[alias_argc - 1]),
1547 1.1 christos command_argv[command_argc - 1],
1548 1.1 christos class_alias, abbrev_flag, c_command->prefixlist);
1549 1.1 christos }
1550 1.1 christos
1551 1.1 christos do_cleanups (cleanup);
1552 1.1 christos }
1553 1.1 christos
1554 1.1 christos /* Print a list of files and line numbers which a user may choose from
1556 1.1 christos in order to list a function which was specified ambiguously (as
1557 1.1 christos with `list classname::overloadedfuncname', for example). The
1558 1.1 christos vector in SALS provides the filenames and line numbers. */
1559 1.1 christos
1560 1.1 christos static void
1561 1.1 christos ambiguous_line_spec (struct symtabs_and_lines *sals)
1562 1.1 christos {
1563 1.1 christos int i;
1564 1.1 christos
1565 1.1 christos for (i = 0; i < sals->nelts; ++i)
1566 1.1 christos printf_filtered (_("file: \"%s\", line number: %d\n"),
1567 1.1 christos symtab_to_filename_for_display (sals->sals[i].symtab),
1568 1.1 christos sals->sals[i].line);
1569 1.6 christos }
1570 1.6 christos
1571 1.3 christos /* Sort function for filter_sals. */
1572 1.3 christos
1573 1.1 christos static int
1574 1.1 christos compare_symtabs (const void *a, const void *b)
1575 1.3 christos {
1576 1.1 christos const struct symtab_and_line *sala = (const struct symtab_and_line *) a;
1577 1.3 christos const struct symtab_and_line *salb = (const struct symtab_and_line *) b;
1578 1.1 christos const char *dira = SYMTAB_DIRNAME (sala->symtab);
1579 1.1 christos const char *dirb = SYMTAB_DIRNAME (salb->symtab);
1580 1.3 christos int r;
1581 1.1 christos
1582 1.3 christos if (dira == NULL)
1583 1.1 christos {
1584 1.1 christos if (dirb != NULL)
1585 1.1 christos return -1;
1586 1.1 christos }
1587 1.3 christos else if (dirb == NULL)
1588 1.1 christos {
1589 1.1 christos if (dira != NULL)
1590 1.1 christos return 1;
1591 1.1 christos }
1592 1.1 christos else
1593 1.1 christos {
1594 1.1 christos r = filename_cmp (dira, dirb);
1595 1.1 christos if (r)
1596 1.1 christos return r;
1597 1.1 christos }
1598 1.1 christos
1599 1.1 christos r = filename_cmp (sala->symtab->filename, salb->symtab->filename);
1600 1.1 christos if (r)
1601 1.1 christos return r;
1602 1.1 christos
1603 1.1 christos if (sala->line < salb->line)
1604 1.1 christos return -1;
1605 1.1 christos return sala->line == salb->line ? 0 : 1;
1606 1.1 christos }
1607 1.1 christos
1608 1.1 christos /* Remove any SALs that do not match the current program space, or
1609 1.1 christos which appear to be "file:line" duplicates. */
1610 1.1 christos
1611 1.1 christos static void
1612 1.1 christos filter_sals (struct symtabs_and_lines *sals)
1613 1.1 christos {
1614 1.1 christos int i, out, prev;
1615 1.1 christos
1616 1.1 christos out = 0;
1617 1.1 christos for (i = 0; i < sals->nelts; ++i)
1618 1.1 christos {
1619 1.1 christos if (sals->sals[i].pspace == current_program_space
1620 1.1 christos && sals->sals[i].symtab != NULL)
1621 1.1 christos {
1622 1.1 christos sals->sals[out] = sals->sals[i];
1623 1.1 christos ++out;
1624 1.1 christos }
1625 1.1 christos }
1626 1.1 christos sals->nelts = out;
1627 1.1 christos
1628 1.1 christos qsort (sals->sals, sals->nelts, sizeof (struct symtab_and_line),
1629 1.1 christos compare_symtabs);
1630 1.1 christos
1631 1.1 christos out = 1;
1632 1.1 christos prev = 0;
1633 1.1 christos for (i = 1; i < sals->nelts; ++i)
1634 1.1 christos {
1635 1.1 christos if (compare_symtabs (&sals->sals[prev], &sals->sals[i]))
1636 1.1 christos {
1637 1.1 christos /* Symtabs differ. */
1638 1.1 christos sals->sals[out] = sals->sals[i];
1639 1.1 christos prev = out;
1640 1.1 christos ++out;
1641 1.1 christos }
1642 1.1 christos }
1643 1.1 christos
1644 1.1 christos if (sals->nelts == 0)
1645 1.1 christos {
1646 1.1 christos xfree (sals->sals);
1647 1.1 christos sals->sals = NULL;
1648 1.1 christos }
1649 1.1 christos else
1650 1.1 christos sals->nelts = out;
1651 1.3 christos }
1652 1.1 christos
1653 1.1 christos static void
1654 1.1 christos set_debug (char *arg, int from_tty)
1655 1.1 christos {
1656 1.1 christos printf_unfiltered (_("\"set debug\" must be followed by "
1657 1.1 christos "the name of a debug subcommand.\n"));
1658 1.1 christos help_list (setdebuglist, "set debug ", all_commands, gdb_stdout);
1659 1.1 christos }
1660 1.1 christos
1661 1.1 christos static void
1662 1.1 christos show_debug (char *args, int from_tty)
1663 1.1 christos {
1664 1.1 christos cmd_show_list (showdebuglist, from_tty, "");
1665 1.1 christos }
1666 1.1 christos
1667 1.1 christos void
1668 1.1 christos init_cmd_lists (void)
1669 1.1 christos {
1670 1.1 christos max_user_call_depth = 1024;
1671 1.1 christos }
1672 1.1 christos
1673 1.1 christos static void
1674 1.1 christos show_info_verbose (struct ui_file *file, int from_tty,
1675 1.1 christos struct cmd_list_element *c,
1676 1.1 christos const char *value)
1677 1.1 christos {
1678 1.1 christos if (info_verbose)
1679 1.1 christos fprintf_filtered (file,
1680 1.1 christos _("Verbose printing of informational messages is %s.\n"),
1681 1.1 christos value);
1682 1.1 christos else
1683 1.1 christos fprintf_filtered (file, _("Verbosity is %s.\n"), value);
1684 1.1 christos }
1685 1.1 christos
1686 1.1 christos static void
1687 1.1 christos show_history_expansion_p (struct ui_file *file, int from_tty,
1688 1.1 christos struct cmd_list_element *c, const char *value)
1689 1.1 christos {
1690 1.1 christos fprintf_filtered (file, _("History expansion on command input is %s.\n"),
1691 1.1 christos value);
1692 1.1 christos }
1693 1.1 christos
1694 1.1 christos static void
1695 1.1 christos show_remote_debug (struct ui_file *file, int from_tty,
1696 1.1 christos struct cmd_list_element *c, const char *value)
1697 1.1 christos {
1698 1.1 christos fprintf_filtered (file, _("Debugging of remote protocol is %s.\n"),
1699 1.1 christos value);
1700 1.1 christos }
1701 1.1 christos
1702 1.1 christos static void
1703 1.1 christos show_remote_timeout (struct ui_file *file, int from_tty,
1704 1.1 christos struct cmd_list_element *c, const char *value)
1705 1.1 christos {
1706 1.1 christos fprintf_filtered (file,
1707 1.1 christos _("Timeout limit to wait for target to respond is %s.\n"),
1708 1.1 christos value);
1709 1.1 christos }
1710 1.1 christos
1711 1.1 christos static void
1712 1.1 christos show_max_user_call_depth (struct ui_file *file, int from_tty,
1713 1.1 christos struct cmd_list_element *c, const char *value)
1714 1.1 christos {
1715 1.1 christos fprintf_filtered (file,
1716 1.1 christos _("The max call depth for user-defined commands is %s.\n"),
1717 1.1 christos value);
1718 1.1 christos }
1719 1.1 christos
1720 1.1 christos
1721 1.1 christos
1723 1.1 christos initialize_file_ftype _initialize_cli_cmds;
1724 1.1 christos
1725 1.1 christos void
1726 1.1 christos _initialize_cli_cmds (void)
1727 1.1 christos {
1728 1.1 christos struct cmd_list_element *c;
1729 1.1 christos
1730 1.1 christos /* Define the classes of commands.
1731 1.1 christos They will appear in the help list in alphabetical order. */
1732 1.1 christos
1733 1.1 christos add_cmd ("internals", class_maintenance, NULL, _("\
1734 1.1 christos Maintenance commands.\n\
1735 1.1 christos Some gdb commands are provided just for use by gdb maintainers.\n\
1736 1.1 christos These commands are subject to frequent change, and may not be as\n\
1737 1.1 christos well documented as user commands."),
1738 1.1 christos &cmdlist);
1739 1.1 christos add_cmd ("obscure", class_obscure, NULL, _("Obscure features."), &cmdlist);
1740 1.1 christos add_cmd ("aliases", class_alias, NULL,
1741 1.1 christos _("Aliases of other commands."), &cmdlist);
1742 1.1 christos add_cmd ("user-defined", class_user, NULL, _("\
1743 1.1 christos User-defined commands.\n\
1744 1.1 christos The commands in this class are those defined by the user.\n\
1745 1.1 christos Use the \"define\" command to define a command."), &cmdlist);
1746 1.1 christos add_cmd ("support", class_support, NULL, _("Support facilities."), &cmdlist);
1747 1.1 christos if (!dbx_commands)
1748 1.1 christos add_cmd ("status", class_info, NULL, _("Status inquiries."), &cmdlist);
1749 1.1 christos add_cmd ("files", class_files, NULL, _("Specifying and examining files."),
1750 1.1 christos &cmdlist);
1751 1.1 christos add_cmd ("breakpoints", class_breakpoint, NULL,
1752 1.1 christos _("Making program stop at certain points."), &cmdlist);
1753 1.1 christos add_cmd ("data", class_vars, NULL, _("Examining data."), &cmdlist);
1754 1.1 christos add_cmd ("stack", class_stack, NULL, _("\
1755 1.1 christos Examining the stack.\n\
1756 1.1 christos The stack is made up of stack frames. Gdb assigns numbers to stack frames\n\
1757 1.1 christos counting from zero for the innermost (currently executing) frame.\n\n\
1758 1.1 christos At any time gdb identifies one frame as the \"selected\" frame.\n\
1759 1.1 christos Variable lookups are done with respect to the selected frame.\n\
1760 1.1 christos When the program being debugged stops, gdb selects the innermost frame.\n\
1761 1.1 christos The commands below can be used to select other frames by number or address."),
1762 1.1 christos &cmdlist);
1763 1.1 christos add_cmd ("running", class_run, NULL, _("Running the program."), &cmdlist);
1764 1.1 christos
1765 1.1 christos /* Define general commands. */
1766 1.1 christos
1767 1.1 christos add_com ("pwd", class_files, pwd_command, _("\
1768 1.1 christos Print working directory. This is used for your program as well."));
1769 1.1 christos
1770 1.1 christos c = add_cmd ("cd", class_files, cd_command, _("\
1771 1.1 christos Set working directory to DIR for debugger and program being debugged.\n\
1772 1.1 christos The change does not take effect for the program being debugged\n\
1773 1.1 christos until the next time it is started."), &cmdlist);
1774 1.1 christos set_cmd_completer (c, filename_completer);
1775 1.1 christos
1776 1.1 christos add_com ("echo", class_support, echo_command, _("\
1777 1.1 christos Print a constant string. Give string as argument.\n\
1778 1.1 christos C escape sequences may be used in the argument.\n\
1779 1.1 christos No newline is added at the end of the argument;\n\
1780 1.1 christos use \"\\n\" if you want a newline to be printed.\n\
1781 1.1 christos Since leading and trailing whitespace are ignored in command arguments,\n\
1782 1.1 christos if you want to print some you must use \"\\\" before leading whitespace\n\
1783 1.1 christos to be printed or after trailing whitespace."));
1784 1.1 christos
1785 1.1 christos add_setshow_enum_cmd ("script-extension", class_support,
1786 1.1 christos script_ext_enums, &script_ext_mode, _("\
1787 1.1 christos Set mode for script filename extension recognition."), _("\
1788 1.1 christos Show mode for script filename extension recognition."), _("\
1789 1.1 christos off == no filename extension recognition (all sourced files are GDB scripts)\n\
1790 1.3 christos soft == evaluate script according to filename extension, fallback to GDB script"
1791 1.3 christos "\n\
1792 1.3 christos strict == evaluate script according to filename extension, error if not supported"
1793 1.3 christos ),
1794 1.3 christos NULL,
1795 1.1 christos show_script_ext_mode,
1796 1.1 christos &setlist, &showlist);
1797 1.1 christos
1798 1.1 christos add_com ("quit", class_support, quit_command, _("\
1799 1.1 christos Exit gdb.\n\
1800 1.1 christos Usage: quit [EXPR]\n\
1801 1.1 christos The optional expression EXPR, if present, is evaluated and the result\n\
1802 1.1 christos used as GDB's exit code. The default is zero."));
1803 1.1 christos c = add_com ("help", class_support, help_command,
1804 1.1 christos _("Print list of commands."));
1805 1.1 christos set_cmd_completer (c, command_completer);
1806 1.1 christos add_com_alias ("q", "quit", class_support, 1);
1807 1.1 christos add_com_alias ("h", "help", class_support, 1);
1808 1.1 christos
1809 1.1 christos add_setshow_boolean_cmd ("verbose", class_support, &info_verbose, _("\
1810 1.1 christos Set verbosity."), _("\
1811 1.1 christos Show verbosity."), NULL,
1812 1.1 christos set_verbose,
1813 1.1 christos show_info_verbose,
1814 1.1 christos &setlist, &showlist);
1815 1.1 christos
1816 1.1 christos add_prefix_cmd ("history", class_support, set_history,
1817 1.1 christos _("Generic command for setting command history parameters."),
1818 1.1 christos &sethistlist, "set history ", 0, &setlist);
1819 1.1 christos add_prefix_cmd ("history", class_support, show_history,
1820 1.1 christos _("Generic command for showing command history parameters."),
1821 1.1 christos &showhistlist, "show history ", 0, &showlist);
1822 1.1 christos
1823 1.1 christos add_setshow_boolean_cmd ("expansion", no_class, &history_expansion_p, _("\
1824 1.1 christos Set history expansion on command input."), _("\
1825 1.1 christos Show history expansion on command input."), _("\
1826 1.1 christos Without an argument, history expansion is enabled."),
1827 1.1 christos NULL,
1828 1.1 christos show_history_expansion_p,
1829 1.1 christos &sethistlist, &showhistlist);
1830 1.1 christos
1831 1.1 christos add_prefix_cmd ("info", class_info, info_command, _("\
1832 1.1 christos Generic command for showing things about the program being debugged."),
1833 1.1 christos &infolist, "info ", 0, &cmdlist);
1834 1.1 christos add_com_alias ("i", "info", class_info, 1);
1835 1.1 christos add_com_alias ("inf", "info", class_info, 1);
1836 1.1 christos
1837 1.1 christos add_com ("complete", class_obscure, complete_command,
1838 1.1 christos _("List the completions for the rest of the line as a command."));
1839 1.1 christos
1840 1.1 christos add_prefix_cmd ("show", class_info, show_command, _("\
1841 1.1 christos Generic command for showing things about the debugger."),
1842 1.1 christos &showlist, "show ", 0, &cmdlist);
1843 1.1 christos /* Another way to get at the same thing. */
1844 1.1 christos add_info ("set", show_command, _("Show all GDB settings."));
1845 1.1 christos
1846 1.1 christos add_cmd ("commands", no_set_class, show_commands, _("\
1847 1.1 christos Show the history of commands you typed.\n\
1848 1.1 christos You can supply a command number to start with, or a `+' to start after\n\
1849 1.1 christos the previous command number shown."),
1850 1.1 christos &showlist);
1851 1.1 christos
1852 1.1 christos add_cmd ("version", no_set_class, show_version,
1853 1.1 christos _("Show what version of GDB this is."), &showlist);
1854 1.1 christos
1855 1.1 christos add_cmd ("configuration", no_set_class, show_configuration,
1856 1.1 christos _("Show how GDB was configured at build time."), &showlist);
1857 1.1 christos
1858 1.1 christos add_setshow_zinteger_cmd ("remote", no_class, &remote_debug, _("\
1859 1.1 christos Set debugging of remote protocol."), _("\
1860 1.1 christos Show debugging of remote protocol."), _("\
1861 1.1 christos When enabled, each packet sent or received with the remote target\n\
1862 1.1 christos is displayed."),
1863 1.1 christos NULL,
1864 1.1 christos show_remote_debug,
1865 1.1 christos &setdebuglist, &showdebuglist);
1866 1.1 christos
1867 1.1 christos add_setshow_zuinteger_unlimited_cmd ("remotetimeout", no_class,
1868 1.1 christos &remote_timeout, _("\
1869 1.1 christos Set timeout limit to wait for target to respond."), _("\
1870 1.1 christos Show timeout limit to wait for target to respond."), _("\
1871 1.1 christos This value is used to set the time limit for gdb to wait for a response\n\
1872 1.1 christos from the target."),
1873 1.1 christos NULL,
1874 1.1 christos show_remote_timeout,
1875 1.1 christos &setlist, &showlist);
1876 1.1 christos
1877 1.1 christos add_prefix_cmd ("debug", no_class, set_debug,
1878 1.1 christos _("Generic command for setting gdb debugging flags"),
1879 1.1 christos &setdebuglist, "set debug ", 0, &setlist);
1880 1.1 christos
1881 1.1 christos add_prefix_cmd ("debug", no_class, show_debug,
1882 1.1 christos _("Generic command for showing gdb debugging flags"),
1883 1.1 christos &showdebuglist, "show debug ", 0, &showlist);
1884 1.1 christos
1885 1.1 christos c = add_com ("shell", class_support, shell_escape, _("\
1886 1.1 christos Execute the rest of the line as a shell command.\n\
1887 1.1 christos With no arguments, run an inferior shell."));
1888 1.1 christos set_cmd_completer (c, filename_completer);
1889 1.1 christos
1890 1.1 christos c = add_com ("edit", class_files, edit_command, _("\
1891 1.1 christos Edit specified file or function.\n\
1892 1.1 christos With no argument, edits file containing most recent line listed.\n\
1893 1.1 christos Editing targets can be specified in these ways:\n\
1894 1.1 christos FILE:LINENUM, to edit at that line in that file,\n\
1895 1.1 christos FUNCTION, to edit at the beginning of that function,\n\
1896 1.1 christos FILE:FUNCTION, to distinguish among like-named static functions.\n\
1897 1.1 christos *ADDRESS, to edit at the line containing that address.\n\
1898 1.1 christos Uses EDITOR environment variable contents as editor (or ex as default)."));
1899 1.1 christos
1900 1.1 christos c->completer = location_completer;
1901 1.1 christos
1902 1.1 christos add_com ("list", class_files, list_command, _("\
1903 1.1 christos List specified function or line.\n\
1904 1.1 christos With no argument, lists ten more lines after or around previous listing.\n\
1905 1.1 christos \"list -\" lists the ten lines before a previous ten-line listing.\n\
1906 1.6 christos One argument specifies a line, and ten lines are listed around that line.\n\
1907 1.6 christos Two arguments with comma between specify starting and ending lines to list.\n\
1908 1.6 christos Lines can be specified in these ways:\n\
1909 1.6 christos LINENUM, to list around that line in current file,\n\
1910 1.6 christos FILE:LINENUM, to list around that line in that file,\n\
1911 1.6 christos FUNCTION, to list around beginning of that function,\n\
1912 1.1 christos FILE:FUNCTION, to distinguish among like-named static functions.\n\
1913 1.5 christos *ADDRESS, to list around the line containing that address.\n\
1914 1.1 christos With two args, if one is empty, it stands for ten lines away from\n\
1915 1.1 christos the other arg.\n\
1916 1.1 christos \n\
1917 1.1 christos By default, when a single location is given, display ten lines.\n\
1918 1.1 christos This can be changed using \"set listsize\", and the current value\n\
1919 1.1 christos can be shown using \"show listsize\"."));
1920 1.1 christos
1921 1.6 christos add_com_alias ("l", "list", class_files, 1);
1922 1.1 christos
1923 1.6 christos if (dbx_commands)
1924 1.6 christos add_com_alias ("file", "list", class_files, 1);
1925 1.6 christos
1926 1.6 christos c = add_com ("disassemble", class_vars, disassemble_command, _("\
1927 1.6 christos Disassemble a specified section of memory.\n\
1928 1.6 christos Default is the function surrounding the pc of the selected frame.\n\
1929 1.6 christos \n\
1930 1.6 christos With a /m modifier, source lines are included (if available).\n\
1931 1.6 christos This view is \"source centric\": the output is in source line order,\n\
1932 1.6 christos regardless of any optimization that is present. Only the main source file\n\
1933 1.6 christos is displayed, not those of, e.g., any inlined functions.\n\
1934 1.1 christos This modifier hasn't proved useful in practice and is deprecated\n\
1935 1.6 christos in favor of /s.\n\
1936 1.1 christos \n\
1937 1.1 christos With a /s modifier, source lines are included (if available).\n\
1938 1.1 christos This differs from /m in two important respects:\n\
1939 1.1 christos - the output is still in pc address order, and\n\
1940 1.1 christos - file names and contents for all relevant source files are displayed.\n\
1941 1.1 christos \n\
1942 1.1 christos With a /r modifier, raw instructions in hex are included.\n\
1943 1.1 christos \n\
1944 1.1 christos With a single argument, the function surrounding that address is dumped.\n\
1945 1.1 christos Two arguments (separated by a comma) are taken as a range of memory to dump,\n\
1946 1.1 christos in the form of \"start,end\", or \"start,+length\".\n\
1947 1.1 christos \n\
1948 1.1 christos Note that the address is interpreted as an expression, not as a location\n\
1949 1.1 christos like in the \"break\" command.\n\
1950 1.1 christos So, for example, if you want to disassemble function bar in file foo.c\n\
1951 1.1 christos you must type \"disassemble 'foo.c'::bar\" and not \"disassemble foo.c:bar\"."));
1952 1.3 christos set_cmd_completer (c, location_completer);
1953 1.1 christos
1954 1.1 christos add_com_alias ("!", "shell", class_support, 0);
1955 1.1 christos
1956 1.1 christos c = add_com ("make", class_support, make_command, _("\
1957 1.1 christos Run the ``make'' program using the rest of the line as arguments."));
1958 1.1 christos set_cmd_completer (c, filename_completer);
1959 1.1 christos add_cmd ("user", no_class, show_user, _("\
1960 1.3 christos Show definitions of non-python/scheme user defined commands.\n\
1961 1.3 christos Argument is the name of the user defined command.\n\
1962 1.1 christos With no argument, show definitions of all user defined commands."), &showlist);
1963 1.1 christos add_com ("apropos", class_support, apropos_command,
1964 1.1 christos _("Search for commands matching a REGEXP"));
1965 1.1 christos
1966 1.1 christos add_setshow_uinteger_cmd ("max-user-call-depth", no_class,
1967 1.1 christos &max_user_call_depth, _("\
1968 1.1 christos Set the max call depth for non-python/scheme user-defined commands."), _("\
1969 1.1 christos Show the max call depth for non-python/scheme user-defined commands."), NULL,
1970 1.1 christos NULL,
1971 1.1 christos show_max_user_call_depth,
1972 1.1 christos &setlist, &showlist);
1973 1.1 christos
1974 1.1 christos add_setshow_boolean_cmd ("trace-commands", no_class, &trace_commands, _("\
1975 1.1 christos Set tracing of GDB CLI commands."), _("\
1976 1.1 christos Show state of GDB CLI command tracing."), _("\
1977 1.1 christos When 'on', each command is displayed as it is executed."),
1978 1.1 christos NULL,
1979 1.1 christos NULL,
1980 1.1 christos &setlist, &showlist);
1981 1.1 christos
1982 1.1 christos c = add_com ("alias", class_support, alias_command, _("\
1983 1.1 christos Define a new command that is an alias of an existing command.\n\
1984 1.1 christos Usage: alias [-a] [--] ALIAS = COMMAND\n\
1985 1.1 christos ALIAS is the name of the alias command to create.\n\
1986 1.1 christos COMMAND is the command being aliased to.\n\
1987 1.1 christos If \"-a\" is specified, the command is an abbreviation,\n\
1988 1.1 christos and will not appear in help command list output.\n\
1989 1.1 christos \n\
1990 1.1 christos Examples:\n\
1991 1.1 christos Make \"spe\" an alias of \"set print elements\":\n\
1992 1.1 christos alias spe = set print elements\n\
1993 1.1 christos Make \"elms\" an alias of \"elements\" in the \"set print\" command:\n\
1994 1.1 christos alias -a set print elms = set print elements"));
1995 1.1 christos }
1996 1.1 christos
1997 1.1 christos void
1998 1.1 christos init_cli_cmds (void)
1999 1.1 christos {
2000 1.1 christos struct cmd_list_element *c;
2001 1.1 christos char *source_help_text;
2002 1.1 christos
2003 1.1 christos source_help_text = xstrprintf (_("\
2004 1.1 christos Read commands from a file named FILE.\n\
2005 1.1 christos \n\
2006 1.1 christos Usage: source [-s] [-v] FILE\n\
2007 1.1 christos -s: search for the script in the source search path,\n\
2008 1.1 christos even if FILE contains directories.\n\
2009 -v: each command in FILE is echoed as it is executed.\n\
2010 \n\
2011 Note that the file \"%s\" is read automatically in this way\n\
2012 when GDB is started."), gdbinit);
2013 c = add_cmd ("source", class_support, source_command,
2014 source_help_text, &cmdlist);
2015 set_cmd_completer (c, filename_completer);
2016 }
2017