forked from gbenson/binutils-gdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtop.c
2086 lines (1711 loc) · 59.2 KB
/
top.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Top level stuff for GDB, the GNU debugger.
Copyright (C) 1986-2015 Free Software Foundation, Inc.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "defs.h"
#include "gdbcmd.h"
#include "cli/cli-cmds.h"
#include "cli/cli-script.h"
#include "cli/cli-setshow.h"
#include "cli/cli-decode.h"
#include "symtab.h"
#include "inferior.h"
#include "infrun.h"
#include <signal.h>
#include "target.h"
#include "target-dcache.h"
#include "breakpoint.h"
#include "gdbtypes.h"
#include "expression.h"
#include "value.h"
#include "language.h"
#include "terminal.h" /* For job_control. */
#include "annotate.h"
#include "completer.h"
#include "top.h"
#include "version.h"
#include "serial.h"
#include "doublest.h"
#include "main.h"
#include "event-loop.h"
#include "gdbthread.h"
#include "extension.h"
#include "interps.h"
#include "observer.h"
#include "maint.h"
#include "filenames.h"
#include "frame.h"
/* readline include files. */
#include "readline/readline.h"
#include "readline/history.h"
/* readline defines this. */
#undef savestring
#include <sys/types.h>
#include "event-top.h"
#include <sys/stat.h>
#include <ctype.h>
#include "ui-out.h"
#include "cli-out.h"
#include "tracepoint.h"
#include "inf-loop.h"
#if defined(TUI)
# include "tui/tui.h"
#endif
extern void initialize_all_files (void);
#define PROMPT(X) the_prompts.prompt_stack[the_prompts.top + X].prompt
#define PREFIX(X) the_prompts.prompt_stack[the_prompts.top + X].prefix
#define SUFFIX(X) the_prompts.prompt_stack[the_prompts.top + X].suffix
/* Default command line prompt. This is overriden in some configs. */
#ifndef DEFAULT_PROMPT
#define DEFAULT_PROMPT "(gdb) "
#endif
/* Initialization file name for gdb. This is host-dependent. */
const char gdbinit[] = GDBINIT;
int inhibit_gdbinit = 0;
extern char lang_frame_mismatch_warn[]; /* language.c */
/* Flag for whether we want to confirm potentially dangerous
operations. Default is yes. */
int confirm = 1;
static void
show_confirm (struct ui_file *file, int from_tty,
struct cmd_list_element *c, const char *value)
{
fprintf_filtered (file, _("Whether to confirm potentially "
"dangerous operations is %s.\n"),
value);
}
/* stdio stream that command input is being read from. Set to stdin
normally. Set by source_command to the file we are sourcing. Set
to NULL if we are executing a user-defined command or interacting
via a GUI. */
FILE *instream;
/* Flag to indicate whether a user defined command is currently running. */
int in_user_command;
/* Current working directory. */
char *current_directory;
/* The directory name is actually stored here (usually). */
char gdb_dirbuf[1024];
/* Function to call before reading a command, if nonzero.
The function receives two args: an input stream,
and a prompt string. */
void (*window_hook) (FILE *, char *);
/* Buffer used for reading command lines, and the size
allocated for it so far. */
char *saved_command_line;
int saved_command_line_size = 100;
/* Nonzero if the current command is modified by "server ". This
affects things like recording into the command history, commands
repeating on RETURN, etc. This is so a user interface (emacs, GUI,
whatever) can issue its own commands and also send along commands
from the user, and have the user not notice that the user interface
is issuing commands too. */
int server_command;
/* Timeout limit for response from target. */
/* The default value has been changed many times over the years. It
was originally 5 seconds. But that was thought to be a long time
to sit and wait, so it was changed to 2 seconds. That was thought
to be plenty unless the connection was going through some terminal
server or multiplexer or other form of hairy serial connection.
In mid-1996, remote_timeout was moved from remote.c to top.c and
it began being used in other remote-* targets. It appears that the
default was changed to 20 seconds at that time, perhaps because the
Renesas E7000 ICE didn't always respond in a timely manner.
But if 5 seconds is a long time to sit and wait for retransmissions,
20 seconds is far worse. This demonstrates the difficulty of using
a single variable for all protocol timeouts.
As remote.c is used much more than remote-e7000.c, it was changed
back to 2 seconds in 1999. */
int remote_timeout = 2;
/* Non-zero tells remote* modules to output debugging info. */
int remote_debug = 0;
/* Sbrk location on entry to main. Used for statistics only. */
#ifdef HAVE_SBRK
char *lim_at_start;
#endif
/* Hooks for alternate command interfaces. */
/* This hook is called from within gdb's many mini-event loops which
could steal control from a real user interface's event loop. It
returns non-zero if the user is requesting a detach, zero
otherwise. */
int (*deprecated_ui_loop_hook) (int);
/* Called from print_frame_info to list the line we stopped in. */
void (*deprecated_print_frame_info_listing_hook) (struct symtab * s,
int line,
int stopline,
int noerror);
/* Replaces most of query. */
int (*deprecated_query_hook) (const char *, va_list);
/* Replaces most of warning. */
void (*deprecated_warning_hook) (const char *, va_list);
/* These three functions support getting lines of text from the user.
They are used in sequence. First deprecated_readline_begin_hook is
called with a text string that might be (for example) a message for
the user to type in a sequence of commands to be executed at a
breakpoint. If this function calls back to a GUI, it might take
this opportunity to pop up a text interaction window with this
message. Next, deprecated_readline_hook is called with a prompt
that is emitted prior to collecting the user input. It can be
called multiple times. Finally, deprecated_readline_end_hook is
called to notify the GUI that we are done with the interaction
window and it can close it. */
void (*deprecated_readline_begin_hook) (char *, ...);
char *(*deprecated_readline_hook) (const char *);
void (*deprecated_readline_end_hook) (void);
/* Called as appropriate to notify the interface that we have attached
to or detached from an already running process. */
void (*deprecated_attach_hook) (void);
void (*deprecated_detach_hook) (void);
/* Called during long calculations to allow GUI to repair window
damage, and to check for stop buttons, etc... */
void (*deprecated_interactive_hook) (void);
/* Called when going to wait for the target. Usually allows the GUI
to run while waiting for target events. */
ptid_t (*deprecated_target_wait_hook) (ptid_t ptid,
struct target_waitstatus *status,
int options);
/* Used by UI as a wrapper around command execution. May do various
things like enabling/disabling buttons, etc... */
void (*deprecated_call_command_hook) (struct cmd_list_element * c,
char *cmd, int from_tty);
/* Called when the current thread changes. Argument is thread id. */
void (*deprecated_context_hook) (int id);
/* Handler for SIGHUP. */
#ifdef SIGHUP
/* NOTE 1999-04-29: This function will be static again, once we modify
gdb to use the event loop as the default command loop and we merge
event-top.c into this file, top.c. */
/* static */ void
quit_cover (void)
{
/* Stop asking user for confirmation --- we're exiting. This
prevents asking the user dumb questions. */
confirm = 0;
quit_command ((char *) 0, 0);
}
#endif /* defined SIGHUP */
/* Line number we are currently in, in a file which is being sourced. */
/* NOTE 1999-04-29: This variable will be static again, once we modify
gdb to use the event loop as the default command loop and we merge
event-top.c into this file, top.c. */
/* static */ int source_line_number;
/* Name of the file we are sourcing. */
/* NOTE 1999-04-29: This variable will be static again, once we modify
gdb to use the event loop as the default command loop and we merge
event-top.c into this file, top.c. */
/* static */ const char *source_file_name;
/* Clean up on error during a "source" command (or execution of a
user-defined command). */
void
do_restore_instream_cleanup (void *stream)
{
/* Restore the previous input stream. */
instream = stream;
}
/* Read commands from STREAM. */
void
read_command_file (FILE *stream)
{
struct cleanup *cleanups;
cleanups = make_cleanup (do_restore_instream_cleanup, instream);
instream = stream;
command_loop ();
do_cleanups (cleanups);
}
void (*pre_init_ui_hook) (void);
#ifdef __MSDOS__
static void
do_chdir_cleanup (void *old_dir)
{
chdir (old_dir);
xfree (old_dir);
}
#endif
struct cleanup *
prepare_execute_command (void)
{
struct value *mark;
struct cleanup *cleanup;
mark = value_mark ();
cleanup = make_cleanup_value_free_to_mark (mark);
/* With multiple threads running while the one we're examining is
stopped, the dcache can get stale without us being able to detect
it. For the duration of the command, though, use the dcache to
help things like backtrace. */
if (non_stop)
target_dcache_invalidate ();
return cleanup;
}
/* Tell the user if the language has changed (except first time) after
executing a command. */
void
check_frame_language_change (void)
{
static int warned = 0;
struct frame_info *frame;
/* First make sure that a new frame has been selected, in case the
command or the hooks changed the program state. */
frame = deprecated_safe_get_selected_frame ();
if (current_language != expected_language)
{
if (language_mode == language_mode_auto && info_verbose)
{
language_info (1); /* Print what changed. */
}
warned = 0;
}
/* Warn the user if the working language does not match the language
of the current frame. Only warn the user if we are actually
running the program, i.e. there is a stack. */
/* FIXME: This should be cacheing the frame and only running when
the frame changes. */
if (has_stack_frames ())
{
enum language flang;
flang = get_frame_language (frame);
if (!warned
&& flang != language_unknown
&& flang != current_language->la_language)
{
printf_filtered ("%s\n", lang_frame_mismatch_warn);
warned = 1;
}
}
}
/* See top.h. */
void
wait_sync_command_done (void)
{
while (gdb_do_one_event () >= 0)
if (!sync_execution)
break;
}
/* See top.h. */
void
maybe_wait_sync_command_done (int was_sync)
{
/* If the interpreter is in sync mode (we're running a user
command's list, running command hooks or similars), and we
just ran a synchronous command that started the target, wait
for that command to end. */
if (!interpreter_async && !was_sync && sync_execution)
wait_sync_command_done ();
}
/* Execute the line P as a command, in the current user context.
Pass FROM_TTY as second argument to the defining function. */
void
execute_command (char *p, int from_tty)
{
struct cleanup *cleanup_if_error, *cleanup;
struct cmd_list_element *c;
char *line;
cleanup_if_error = make_bpstat_clear_actions_cleanup ();
cleanup = prepare_execute_command ();
/* Force cleanup of any alloca areas if using C alloca instead of
a builtin alloca. */
alloca (0);
/* This can happen when command_line_input hits end of file. */
if (p == NULL)
{
do_cleanups (cleanup);
discard_cleanups (cleanup_if_error);
return;
}
target_log_command (p);
while (*p == ' ' || *p == '\t')
p++;
if (*p)
{
const char *cmd = p;
char *arg;
int was_sync = sync_execution;
line = p;
/* If trace-commands is set then this will print this command. */
print_command_trace (p);
c = lookup_cmd (&cmd, cmdlist, "", 0, 1);
p = (char *) cmd;
/* Pass null arg rather than an empty one. */
arg = *p ? p : 0;
/* FIXME: cagney/2002-02-02: The c->type test is pretty dodgy
while the is_complete_command(cfunc) test is just plain
bogus. They should both be replaced by a test of the form
c->strip_trailing_white_space_p. */
/* NOTE: cagney/2002-02-02: The function.cfunc in the below
can't be replaced with func. This is because it is the
cfunc, and not the func, that has the value that the
is_complete_command hack is testing for. */
/* Clear off trailing whitespace, except for set and complete
command. */
if (arg
&& c->type != set_cmd
&& !is_complete_command (c))
{
p = arg + strlen (arg) - 1;
while (p >= arg && (*p == ' ' || *p == '\t'))
p--;
*(p + 1) = '\0';
}
/* If this command has been pre-hooked, run the hook first. */
execute_cmd_pre_hook (c);
if (c->deprecated_warn_user)
deprecated_cmd_warning (line);
/* c->user_commands would be NULL in the case of a python command. */
if (c->theclass == class_user && c->user_commands)
execute_user_command (c, arg);
else if (c->type == set_cmd)
do_set_command (arg, from_tty, c);
else if (c->type == show_cmd)
do_show_command (arg, from_tty, c);
else if (!cmd_func_p (c))
error (_("That is not a command, just a help topic."));
else if (deprecated_call_command_hook)
deprecated_call_command_hook (c, arg, from_tty);
else
cmd_func (c, arg, from_tty);
maybe_wait_sync_command_done (was_sync);
/* If this command has been post-hooked, run the hook last. */
execute_cmd_post_hook (c);
}
check_frame_language_change ();
do_cleanups (cleanup);
discard_cleanups (cleanup_if_error);
}
/* Run execute_command for P and FROM_TTY. Capture its output into the
returned string, do not display it to the screen. BATCH_FLAG will be
temporarily set to true. */
char *
execute_command_to_string (char *p, int from_tty)
{
struct ui_file *str_file;
struct cleanup *cleanup;
char *retval;
/* GDB_STDOUT should be better already restored during these
restoration callbacks. */
cleanup = set_batch_flag_and_make_cleanup_restore_page_info ();
make_cleanup_restore_integer (&interpreter_async);
interpreter_async = 0;
str_file = mem_fileopen ();
make_cleanup_ui_file_delete (str_file);
make_cleanup_restore_ui_file (&gdb_stdout);
make_cleanup_restore_ui_file (&gdb_stderr);
make_cleanup_restore_ui_file (&gdb_stdlog);
make_cleanup_restore_ui_file (&gdb_stdtarg);
make_cleanup_restore_ui_file (&gdb_stdtargerr);
if (ui_out_redirect (current_uiout, str_file) < 0)
warning (_("Current output protocol does not support redirection"));
else
make_cleanup_ui_out_redirect_pop (current_uiout);
gdb_stdout = str_file;
gdb_stderr = str_file;
gdb_stdlog = str_file;
gdb_stdtarg = str_file;
gdb_stdtargerr = str_file;
execute_command (p, from_tty);
retval = ui_file_xstrdup (str_file, NULL);
do_cleanups (cleanup);
return retval;
}
/* Read commands from `instream' and execute them
until end of file or error reading instream. */
void
command_loop (void)
{
struct cleanup *old_chain;
char *command;
int stdin_is_tty = ISATTY (stdin);
while (instream && !feof (instream))
{
if (window_hook && instream == stdin)
(*window_hook) (instream, get_prompt ());
clear_quit_flag ();
if (instream == stdin && stdin_is_tty)
reinitialize_more_filter ();
old_chain = make_cleanup (null_cleanup, 0);
/* Get a command-line. This calls the readline package. */
command = command_line_input (instream == stdin ?
get_prompt () : (char *) NULL,
instream == stdin, "prompt");
if (command == 0)
{
do_cleanups (old_chain);
return;
}
make_command_stats_cleanup (1);
/* Do not execute commented lines. */
if (command[0] != '#')
{
execute_command (command, instream == stdin);
/* Do any commands attached to breakpoint we are stopped at. */
bpstat_do_actions ();
}
do_cleanups (old_chain);
}
}
/* When nonzero, cause dont_repeat to do nothing. This should only be
set via prevent_dont_repeat. */
static int suppress_dont_repeat = 0;
/* Commands call this if they do not want to be repeated by null lines. */
void
dont_repeat (void)
{
if (suppress_dont_repeat || server_command)
return;
/* If we aren't reading from standard input, we are saving the last
thing read from stdin in line and don't want to delete it. Null
lines won't repeat here in any case. */
if (instream == stdin)
*saved_command_line = 0;
}
/* Prevent dont_repeat from working, and return a cleanup that
restores the previous state. */
struct cleanup *
prevent_dont_repeat (void)
{
struct cleanup *result = make_cleanup_restore_integer (&suppress_dont_repeat);
suppress_dont_repeat = 1;
return result;
}
/* Read a line from the stream "instream" without command line editing.
It prints PROMPT_ARG once at the start.
Action is compatible with "readline", e.g. space for the result is
malloc'd and should be freed by the caller.
A NULL return means end of file. */
char *
gdb_readline (const char *prompt_arg)
{
int c;
char *result;
int input_index = 0;
int result_size = 80;
if (prompt_arg)
{
/* Don't use a _filtered function here. It causes the assumed
character position to be off, since the newline we read from
the user is not accounted for. */
fputs_unfiltered (prompt_arg, gdb_stdout);
gdb_flush (gdb_stdout);
}
result = (char *) xmalloc (result_size);
while (1)
{
/* Read from stdin if we are executing a user defined command.
This is the right thing for prompt_for_continue, at least. */
c = fgetc (instream ? instream : stdin);
if (c == EOF)
{
if (input_index > 0)
/* The last line does not end with a newline. Return it, and
if we are called again fgetc will still return EOF and
we'll return NULL then. */
break;
xfree (result);
return NULL;
}
if (c == '\n')
{
if (input_index > 0 && result[input_index - 1] == '\r')
input_index--;
break;
}
result[input_index++] = c;
while (input_index >= result_size)
{
result_size *= 2;
result = (char *) xrealloc (result, result_size);
}
}
result[input_index++] = '\0';
return result;
}
/* Variables which control command line editing and history
substitution. These variables are given default values at the end
of this file. */
static int command_editing_p;
/* NOTE 1999-04-29: This variable will be static again, once we modify
gdb to use the event loop as the default command loop and we merge
event-top.c into this file, top.c. */
/* static */ int history_expansion_p;
static int write_history_p;
static void
show_write_history_p (struct ui_file *file, int from_tty,
struct cmd_list_element *c, const char *value)
{
fprintf_filtered (file, _("Saving of the history record on exit is %s.\n"),
value);
}
/* The variable associated with the "set/show history size"
command. The value -1 means unlimited, and -2 means undefined. */
static int history_size_setshow_var = -2;
static void
show_history_size (struct ui_file *file, int from_tty,
struct cmd_list_element *c, const char *value)
{
fprintf_filtered (file, _("The size of the command history is %s.\n"),
value);
}
/* Variable associated with the "history remove-duplicates" option.
The value -1 means unlimited. */
static int history_remove_duplicates = 0;
static void
show_history_remove_duplicates (struct ui_file *file, int from_tty,
struct cmd_list_element *c, const char *value)
{
fprintf_filtered (file,
_("The number of history entries to look back at for "
"duplicates is %s.\n"),
value);
}
static char *history_filename;
static void
show_history_filename (struct ui_file *file, int from_tty,
struct cmd_list_element *c, const char *value)
{
fprintf_filtered (file, _("The filename in which to record "
"the command history is \"%s\".\n"),
value);
}
/* This is like readline(), but it has some gdb-specific behavior.
gdb may want readline in both the synchronous and async modes during
a single gdb invocation. At the ordinary top-level prompt we might
be using the async readline. That means we can't use
rl_pre_input_hook, since it doesn't work properly in async mode.
However, for a secondary prompt (" >", such as occurs during a
`define'), gdb wants a synchronous response.
We used to call readline() directly, running it in synchronous
mode. But mixing modes this way is not supported, and as of
readline 5.x it no longer works; the arrow keys come unbound during
the synchronous call. So we make a nested call into the event
loop. That's what gdb_readline_wrapper is for. */
/* A flag set as soon as gdb_readline_wrapper_line is called; we can't
rely on gdb_readline_wrapper_result, which might still be NULL if
the user types Control-D for EOF. */
static int gdb_readline_wrapper_done;
/* The result of the current call to gdb_readline_wrapper, once a newline
is seen. */
static char *gdb_readline_wrapper_result;
/* Any intercepted hook. Operate-and-get-next sets this, expecting it
to be called after the newline is processed (which will redisplay
the prompt). But in gdb_readline_wrapper we will not get a new
prompt until the next call, or until we return to the event loop.
So we disable this hook around the newline and restore it before we
return. */
static void (*saved_after_char_processing_hook) (void);
/* The number of nested readline secondary prompts that are currently
active. */
static int gdb_secondary_prompt_depth = 0;
/* See top.h. */
int
gdb_in_secondary_prompt_p (void)
{
return gdb_secondary_prompt_depth > 0;
}
/* This function is called when readline has seen a complete line of
text. */
static void
gdb_readline_wrapper_line (char *line)
{
gdb_assert (!gdb_readline_wrapper_done);
gdb_readline_wrapper_result = line;
gdb_readline_wrapper_done = 1;
/* Prevent operate-and-get-next from acting too early. */
saved_after_char_processing_hook = after_char_processing_hook;
after_char_processing_hook = NULL;
/* Prevent parts of the prompt from being redisplayed if annotations
are enabled, and readline's state getting out of sync. We'll
reinstall the callback handler, which puts the terminal in raw
mode (or in readline lingo, in prepped state), when we're next
ready to process user input, either in display_gdb_prompt, or if
we're handling an asynchronous target event and running in the
background, just before returning to the event loop to process
further input (or more target events). */
if (async_command_editing_p)
gdb_rl_callback_handler_remove ();
}
struct gdb_readline_wrapper_cleanup
{
void (*handler_orig) (char *);
int already_prompted_orig;
/* Whether the target was async. */
int target_is_async_orig;
};
static void
gdb_readline_wrapper_cleanup (void *arg)
{
struct gdb_readline_wrapper_cleanup *cleanup = arg;
rl_already_prompted = cleanup->already_prompted_orig;
gdb_assert (input_handler == gdb_readline_wrapper_line);
input_handler = cleanup->handler_orig;
/* Don't restore our input handler in readline yet. That would make
readline prep the terminal (putting it in raw mode), while the
line we just read may trigger execution of a command that expects
the terminal in the default cooked/canonical mode, such as e.g.,
running Python's interactive online help utility. See
gdb_readline_wrapper_line for when we'll reinstall it. */
gdb_readline_wrapper_result = NULL;
gdb_readline_wrapper_done = 0;
gdb_secondary_prompt_depth--;
gdb_assert (gdb_secondary_prompt_depth >= 0);
after_char_processing_hook = saved_after_char_processing_hook;
saved_after_char_processing_hook = NULL;
if (cleanup->target_is_async_orig)
target_async (1);
xfree (cleanup);
}
char *
gdb_readline_wrapper (const char *prompt)
{
struct cleanup *back_to;
struct gdb_readline_wrapper_cleanup *cleanup;
char *retval;
cleanup = XNEW (struct gdb_readline_wrapper_cleanup);
cleanup->handler_orig = input_handler;
input_handler = gdb_readline_wrapper_line;
cleanup->already_prompted_orig = rl_already_prompted;
cleanup->target_is_async_orig = target_is_async_p ();
gdb_secondary_prompt_depth++;
back_to = make_cleanup (gdb_readline_wrapper_cleanup, cleanup);
if (cleanup->target_is_async_orig)
target_async (0);
/* Display our prompt and prevent double prompt display. */
display_gdb_prompt (prompt);
rl_already_prompted = 1;
if (after_char_processing_hook)
(*after_char_processing_hook) ();
gdb_assert (after_char_processing_hook == NULL);
while (gdb_do_one_event () >= 0)
if (gdb_readline_wrapper_done)
break;
retval = gdb_readline_wrapper_result;
do_cleanups (back_to);
return retval;
}
/* The current saved history number from operate-and-get-next.
This is -1 if not valid. */
static int operate_saved_history = -1;
/* This is put on the appropriate hook and helps operate-and-get-next
do its work. */
static void
gdb_rl_operate_and_get_next_completion (void)
{
int delta = where_history () - operate_saved_history;
/* The `key' argument to rl_get_previous_history is ignored. */
rl_get_previous_history (delta, 0);
operate_saved_history = -1;
/* readline doesn't automatically update the display for us. */
rl_redisplay ();
after_char_processing_hook = NULL;
rl_pre_input_hook = NULL;
}
/* This is a gdb-local readline command handler. It accepts the
current command line (like RET does) and, if this command was taken
from the history, arranges for the next command in the history to
appear on the command line when the prompt returns.
We ignore the arguments. */
static int
gdb_rl_operate_and_get_next (int count, int key)
{
int where;
/* Use the async hook. */
after_char_processing_hook = gdb_rl_operate_and_get_next_completion;
/* Find the current line, and find the next line to use. */
where = where_history();
if ((history_is_stifled () && (history_length >= history_max_entries))
|| (where >= history_length - 1))
operate_saved_history = where;
else
operate_saved_history = where + 1;
return rl_newline (1, key);
}
/* Number of user commands executed during this session. */
static int command_count = 0;
/* Add the user command COMMAND to the input history list. */
void
gdb_add_history (const char *command)
{
command_count++;
if (history_remove_duplicates != 0)
{
int lookbehind;
int lookbehind_threshold;
/* The lookbehind threshold for finding a duplicate history entry is
bounded by command_count because we can't meaningfully delete
history entries that are already stored in the history file since
the history file is appended to. */
if (history_remove_duplicates == -1
|| history_remove_duplicates > command_count)
lookbehind_threshold = command_count;
else
lookbehind_threshold = history_remove_duplicates;
using_history ();
for (lookbehind = 0; lookbehind < lookbehind_threshold; lookbehind++)
{
HIST_ENTRY *temp = previous_history ();
if (temp == NULL)
break;
if (strcmp (temp->line, command) == 0)
{
HIST_ENTRY *prev = remove_history (where_history ());
command_count--;
free_history_entry (prev);
break;
}
}
using_history ();
}
add_history (command);
}
/* Safely append new history entries to the history file in a corruption-free
way using an intermediate local history file. */
static void
gdb_safe_append_history (void)
{
int ret, saved_errno;
char *local_history_filename;
struct cleanup *old_chain;
local_history_filename
= xstrprintf ("%s-gdb%d~", history_filename, getpid ());
old_chain = make_cleanup (xfree, local_history_filename);
ret = rename (history_filename, local_history_filename);
saved_errno = errno;
if (ret < 0 && saved_errno != ENOENT)
{
warning (_("Could not rename %s to %s: %s"),
history_filename, local_history_filename,
safe_strerror (saved_errno));
}
else