]>
Commit | Line | Data |
---|---|---|
1 | /* General utility routines for GDB, the GNU debugger. | |
2 | Copyright 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, | |
3 | 1996, 1997, 1998, 1999, 2000, 2001, 2002 | |
4 | Free Software Foundation, Inc. | |
5 | ||
6 | This file is part of GDB. | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program; if not, write to the Free Software | |
20 | Foundation, Inc., 59 Temple Place - Suite 330, | |
21 | Boston, MA 02111-1307, USA. */ | |
22 | ||
23 | #include "config.h" | |
24 | ||
25 | /* Include before "bfd.h" so that we get stdbool.h in time, if <curses.h> | |
26 | brings it in. */ | |
27 | #ifdef HAVE_CURSES_H | |
28 | #include <curses.h> | |
29 | #endif | |
30 | #ifdef HAVE_TERM_H | |
31 | #include <term.h> | |
32 | #endif | |
33 | ||
34 | #include "defs.h" | |
35 | #include "gdb_assert.h" | |
36 | #include <ctype.h> | |
37 | #include "gdb_string.h" | |
38 | #include "event-top.h" | |
39 | ||
40 | #ifdef __GO32__ | |
41 | #include <pc.h> | |
42 | #endif | |
43 | ||
44 | /* SunOS's curses.h has a '#define reg register' in it. Thank you Sun. */ | |
45 | #ifdef reg | |
46 | #undef reg | |
47 | #endif | |
48 | ||
49 | #include <signal.h> | |
50 | #include "gdbcmd.h" | |
51 | #include "serial.h" | |
52 | #include "bfd.h" | |
53 | #include "target.h" | |
54 | #include "demangle.h" | |
55 | #include "expression.h" | |
56 | #include "language.h" | |
57 | #include "annotate.h" | |
58 | ||
59 | #include "inferior.h" /* for signed_pointer_to_address */ | |
60 | ||
61 | #include <sys/param.h> /* For MAXPATHLEN */ | |
62 | ||
63 | #include <readline/readline.h> | |
64 | ||
65 | #ifdef USE_MMALLOC | |
66 | #include "mmalloc.h" | |
67 | #endif | |
68 | ||
69 | #ifdef NEED_DECLARATION_MALLOC | |
70 | extern PTR malloc (); | |
71 | #endif | |
72 | #ifdef NEED_DECLARATION_REALLOC | |
73 | extern PTR realloc (); | |
74 | #endif | |
75 | #ifdef NEED_DECLARATION_FREE | |
76 | extern void free (); | |
77 | #endif | |
78 | ||
79 | #undef XMALLOC | |
80 | #define XMALLOC(TYPE) ((TYPE*) xmalloc (sizeof (TYPE))) | |
81 | ||
82 | /* readline defines this. */ | |
83 | #undef savestring | |
84 | ||
85 | void (*error_begin_hook) (void); | |
86 | ||
87 | /* Holds the last error message issued by gdb */ | |
88 | ||
89 | static struct ui_file *gdb_lasterr; | |
90 | ||
91 | /* Prototypes for local functions */ | |
92 | ||
93 | static void vfprintf_maybe_filtered (struct ui_file *, const char *, | |
94 | va_list, int); | |
95 | ||
96 | static void fputs_maybe_filtered (const char *, struct ui_file *, int); | |
97 | ||
98 | #if defined (USE_MMALLOC) && !defined (NO_MMCHECK) | |
99 | static void malloc_botch (void); | |
100 | #endif | |
101 | ||
102 | static void prompt_for_continue (void); | |
103 | ||
104 | static void set_width_command (char *, int, struct cmd_list_element *); | |
105 | ||
106 | static void set_width (void); | |
107 | ||
108 | /* Chain of cleanup actions established with make_cleanup, | |
109 | to be executed if an error happens. */ | |
110 | ||
111 | static struct cleanup *cleanup_chain; /* cleaned up after a failed command */ | |
112 | static struct cleanup *final_cleanup_chain; /* cleaned up when gdb exits */ | |
113 | static struct cleanup *run_cleanup_chain; /* cleaned up on each 'run' */ | |
114 | static struct cleanup *exec_cleanup_chain; /* cleaned up on each execution command */ | |
115 | /* cleaned up on each error from within an execution command */ | |
116 | static struct cleanup *exec_error_cleanup_chain; | |
117 | ||
118 | /* Pointer to what is left to do for an execution command after the | |
119 | target stops. Used only in asynchronous mode, by targets that | |
120 | support async execution. The finish and until commands use it. So | |
121 | does the target extended-remote command. */ | |
122 | struct continuation *cmd_continuation; | |
123 | struct continuation *intermediate_continuation; | |
124 | ||
125 | /* Nonzero if we have job control. */ | |
126 | ||
127 | int job_control; | |
128 | ||
129 | /* Nonzero means a quit has been requested. */ | |
130 | ||
131 | int quit_flag; | |
132 | ||
133 | /* Nonzero means quit immediately if Control-C is typed now, rather | |
134 | than waiting until QUIT is executed. Be careful in setting this; | |
135 | code which executes with immediate_quit set has to be very careful | |
136 | about being able to deal with being interrupted at any time. It is | |
137 | almost always better to use QUIT; the only exception I can think of | |
138 | is being able to quit out of a system call (using EINTR loses if | |
139 | the SIGINT happens between the previous QUIT and the system call). | |
140 | To immediately quit in the case in which a SIGINT happens between | |
141 | the previous QUIT and setting immediate_quit (desirable anytime we | |
142 | expect to block), call QUIT after setting immediate_quit. */ | |
143 | ||
144 | int immediate_quit; | |
145 | ||
146 | /* Nonzero means that encoded C++ names should be printed out in their | |
147 | C++ form rather than raw. */ | |
148 | ||
149 | int demangle = 1; | |
150 | ||
151 | /* Nonzero means that encoded C++ names should be printed out in their | |
152 | C++ form even in assembler language displays. If this is set, but | |
153 | DEMANGLE is zero, names are printed raw, i.e. DEMANGLE controls. */ | |
154 | ||
155 | int asm_demangle = 0; | |
156 | ||
157 | /* Nonzero means that strings with character values >0x7F should be printed | |
158 | as octal escapes. Zero means just print the value (e.g. it's an | |
159 | international character, and the terminal or window can cope.) */ | |
160 | ||
161 | int sevenbit_strings = 0; | |
162 | ||
163 | /* String to be printed before error messages, if any. */ | |
164 | ||
165 | char *error_pre_print; | |
166 | ||
167 | /* String to be printed before quit messages, if any. */ | |
168 | ||
169 | char *quit_pre_print; | |
170 | ||
171 | /* String to be printed before warning messages, if any. */ | |
172 | ||
173 | char *warning_pre_print = "\nwarning: "; | |
174 | ||
175 | int pagination_enabled = 1; | |
176 | \f | |
177 | ||
178 | /* Add a new cleanup to the cleanup_chain, | |
179 | and return the previous chain pointer | |
180 | to be passed later to do_cleanups or discard_cleanups. | |
181 | Args are FUNCTION to clean up with, and ARG to pass to it. */ | |
182 | ||
183 | struct cleanup * | |
184 | make_cleanup (make_cleanup_ftype *function, void *arg) | |
185 | { | |
186 | return make_my_cleanup (&cleanup_chain, function, arg); | |
187 | } | |
188 | ||
189 | struct cleanup * | |
190 | make_final_cleanup (make_cleanup_ftype *function, void *arg) | |
191 | { | |
192 | return make_my_cleanup (&final_cleanup_chain, function, arg); | |
193 | } | |
194 | ||
195 | struct cleanup * | |
196 | make_run_cleanup (make_cleanup_ftype *function, void *arg) | |
197 | { | |
198 | return make_my_cleanup (&run_cleanup_chain, function, arg); | |
199 | } | |
200 | ||
201 | struct cleanup * | |
202 | make_exec_cleanup (make_cleanup_ftype *function, void *arg) | |
203 | { | |
204 | return make_my_cleanup (&exec_cleanup_chain, function, arg); | |
205 | } | |
206 | ||
207 | struct cleanup * | |
208 | make_exec_error_cleanup (make_cleanup_ftype *function, void *arg) | |
209 | { | |
210 | return make_my_cleanup (&exec_error_cleanup_chain, function, arg); | |
211 | } | |
212 | ||
213 | static void | |
214 | do_freeargv (void *arg) | |
215 | { | |
216 | freeargv ((char **) arg); | |
217 | } | |
218 | ||
219 | struct cleanup * | |
220 | make_cleanup_freeargv (char **arg) | |
221 | { | |
222 | return make_my_cleanup (&cleanup_chain, do_freeargv, arg); | |
223 | } | |
224 | ||
225 | static void | |
226 | do_bfd_close_cleanup (void *arg) | |
227 | { | |
228 | bfd_close (arg); | |
229 | } | |
230 | ||
231 | struct cleanup * | |
232 | make_cleanup_bfd_close (bfd *abfd) | |
233 | { | |
234 | return make_cleanup (do_bfd_close_cleanup, abfd); | |
235 | } | |
236 | ||
237 | static void | |
238 | do_close_cleanup (void *arg) | |
239 | { | |
240 | int *fd = arg; | |
241 | close (*fd); | |
242 | xfree (fd); | |
243 | } | |
244 | ||
245 | struct cleanup * | |
246 | make_cleanup_close (int fd) | |
247 | { | |
248 | int *saved_fd = xmalloc (sizeof (fd)); | |
249 | *saved_fd = fd; | |
250 | return make_cleanup (do_close_cleanup, saved_fd); | |
251 | } | |
252 | ||
253 | static void | |
254 | do_ui_file_delete (void *arg) | |
255 | { | |
256 | ui_file_delete (arg); | |
257 | } | |
258 | ||
259 | struct cleanup * | |
260 | make_cleanup_ui_file_delete (struct ui_file *arg) | |
261 | { | |
262 | return make_my_cleanup (&cleanup_chain, do_ui_file_delete, arg); | |
263 | } | |
264 | ||
265 | struct cleanup * | |
266 | make_my_cleanup (struct cleanup **pmy_chain, make_cleanup_ftype *function, | |
267 | void *arg) | |
268 | { | |
269 | register struct cleanup *new | |
270 | = (struct cleanup *) xmalloc (sizeof (struct cleanup)); | |
271 | register struct cleanup *old_chain = *pmy_chain; | |
272 | ||
273 | new->next = *pmy_chain; | |
274 | new->function = function; | |
275 | new->arg = arg; | |
276 | *pmy_chain = new; | |
277 | ||
278 | return old_chain; | |
279 | } | |
280 | ||
281 | /* Discard cleanups and do the actions they describe | |
282 | until we get back to the point OLD_CHAIN in the cleanup_chain. */ | |
283 | ||
284 | void | |
285 | do_cleanups (register struct cleanup *old_chain) | |
286 | { | |
287 | do_my_cleanups (&cleanup_chain, old_chain); | |
288 | } | |
289 | ||
290 | void | |
291 | do_final_cleanups (register struct cleanup *old_chain) | |
292 | { | |
293 | do_my_cleanups (&final_cleanup_chain, old_chain); | |
294 | } | |
295 | ||
296 | void | |
297 | do_run_cleanups (register struct cleanup *old_chain) | |
298 | { | |
299 | do_my_cleanups (&run_cleanup_chain, old_chain); | |
300 | } | |
301 | ||
302 | void | |
303 | do_exec_cleanups (register struct cleanup *old_chain) | |
304 | { | |
305 | do_my_cleanups (&exec_cleanup_chain, old_chain); | |
306 | } | |
307 | ||
308 | void | |
309 | do_exec_error_cleanups (register struct cleanup *old_chain) | |
310 | { | |
311 | do_my_cleanups (&exec_error_cleanup_chain, old_chain); | |
312 | } | |
313 | ||
314 | void | |
315 | do_my_cleanups (register struct cleanup **pmy_chain, | |
316 | register struct cleanup *old_chain) | |
317 | { | |
318 | register struct cleanup *ptr; | |
319 | while ((ptr = *pmy_chain) != old_chain) | |
320 | { | |
321 | *pmy_chain = ptr->next; /* Do this first incase recursion */ | |
322 | (*ptr->function) (ptr->arg); | |
323 | xfree (ptr); | |
324 | } | |
325 | } | |
326 | ||
327 | /* Discard cleanups, not doing the actions they describe, | |
328 | until we get back to the point OLD_CHAIN in the cleanup_chain. */ | |
329 | ||
330 | void | |
331 | discard_cleanups (register struct cleanup *old_chain) | |
332 | { | |
333 | discard_my_cleanups (&cleanup_chain, old_chain); | |
334 | } | |
335 | ||
336 | void | |
337 | discard_final_cleanups (register struct cleanup *old_chain) | |
338 | { | |
339 | discard_my_cleanups (&final_cleanup_chain, old_chain); | |
340 | } | |
341 | ||
342 | void | |
343 | discard_exec_error_cleanups (register struct cleanup *old_chain) | |
344 | { | |
345 | discard_my_cleanups (&exec_error_cleanup_chain, old_chain); | |
346 | } | |
347 | ||
348 | void | |
349 | discard_my_cleanups (register struct cleanup **pmy_chain, | |
350 | register struct cleanup *old_chain) | |
351 | { | |
352 | register struct cleanup *ptr; | |
353 | while ((ptr = *pmy_chain) != old_chain) | |
354 | { | |
355 | *pmy_chain = ptr->next; | |
356 | xfree (ptr); | |
357 | } | |
358 | } | |
359 | ||
360 | /* Set the cleanup_chain to 0, and return the old cleanup chain. */ | |
361 | struct cleanup * | |
362 | save_cleanups (void) | |
363 | { | |
364 | return save_my_cleanups (&cleanup_chain); | |
365 | } | |
366 | ||
367 | struct cleanup * | |
368 | save_final_cleanups (void) | |
369 | { | |
370 | return save_my_cleanups (&final_cleanup_chain); | |
371 | } | |
372 | ||
373 | struct cleanup * | |
374 | save_my_cleanups (struct cleanup **pmy_chain) | |
375 | { | |
376 | struct cleanup *old_chain = *pmy_chain; | |
377 | ||
378 | *pmy_chain = 0; | |
379 | return old_chain; | |
380 | } | |
381 | ||
382 | /* Restore the cleanup chain from a previously saved chain. */ | |
383 | void | |
384 | restore_cleanups (struct cleanup *chain) | |
385 | { | |
386 | restore_my_cleanups (&cleanup_chain, chain); | |
387 | } | |
388 | ||
389 | void | |
390 | restore_final_cleanups (struct cleanup *chain) | |
391 | { | |
392 | restore_my_cleanups (&final_cleanup_chain, chain); | |
393 | } | |
394 | ||
395 | void | |
396 | restore_my_cleanups (struct cleanup **pmy_chain, struct cleanup *chain) | |
397 | { | |
398 | *pmy_chain = chain; | |
399 | } | |
400 | ||
401 | /* This function is useful for cleanups. | |
402 | Do | |
403 | ||
404 | foo = xmalloc (...); | |
405 | old_chain = make_cleanup (free_current_contents, &foo); | |
406 | ||
407 | to arrange to free the object thus allocated. */ | |
408 | ||
409 | void | |
410 | free_current_contents (void *ptr) | |
411 | { | |
412 | void **location = ptr; | |
413 | if (location == NULL) | |
414 | internal_error (__FILE__, __LINE__, | |
415 | "free_current_contents: NULL pointer"); | |
416 | if (*location != NULL) | |
417 | { | |
418 | xfree (*location); | |
419 | *location = NULL; | |
420 | } | |
421 | } | |
422 | ||
423 | /* Provide a known function that does nothing, to use as a base for | |
424 | for a possibly long chain of cleanups. This is useful where we | |
425 | use the cleanup chain for handling normal cleanups as well as dealing | |
426 | with cleanups that need to be done as a result of a call to error(). | |
427 | In such cases, we may not be certain where the first cleanup is, unless | |
428 | we have a do-nothing one to always use as the base. */ | |
429 | ||
430 | /* ARGSUSED */ | |
431 | void | |
432 | null_cleanup (void *arg) | |
433 | { | |
434 | } | |
435 | ||
436 | /* Add a continuation to the continuation list, the global list | |
437 | cmd_continuation. The new continuation will be added at the front.*/ | |
438 | void | |
439 | add_continuation (void (*continuation_hook) (struct continuation_arg *), | |
440 | struct continuation_arg *arg_list) | |
441 | { | |
442 | struct continuation *continuation_ptr; | |
443 | ||
444 | continuation_ptr = (struct continuation *) xmalloc (sizeof (struct continuation)); | |
445 | continuation_ptr->continuation_hook = continuation_hook; | |
446 | continuation_ptr->arg_list = arg_list; | |
447 | continuation_ptr->next = cmd_continuation; | |
448 | cmd_continuation = continuation_ptr; | |
449 | } | |
450 | ||
451 | /* Walk down the cmd_continuation list, and execute all the | |
452 | continuations. There is a problem though. In some cases new | |
453 | continuations may be added while we are in the middle of this | |
454 | loop. If this happens they will be added in the front, and done | |
455 | before we have a chance of exhausting those that were already | |
456 | there. We need to then save the beginning of the list in a pointer | |
457 | and do the continuations from there on, instead of using the | |
458 | global beginning of list as our iteration pointer.*/ | |
459 | void | |
460 | do_all_continuations (void) | |
461 | { | |
462 | struct continuation *continuation_ptr; | |
463 | struct continuation *saved_continuation; | |
464 | ||
465 | /* Copy the list header into another pointer, and set the global | |
466 | list header to null, so that the global list can change as a side | |
467 | effect of invoking the continuations and the processing of | |
468 | the preexisting continuations will not be affected. */ | |
469 | continuation_ptr = cmd_continuation; | |
470 | cmd_continuation = NULL; | |
471 | ||
472 | /* Work now on the list we have set aside. */ | |
473 | while (continuation_ptr) | |
474 | { | |
475 | (continuation_ptr->continuation_hook) (continuation_ptr->arg_list); | |
476 | saved_continuation = continuation_ptr; | |
477 | continuation_ptr = continuation_ptr->next; | |
478 | xfree (saved_continuation); | |
479 | } | |
480 | } | |
481 | ||
482 | /* Walk down the cmd_continuation list, and get rid of all the | |
483 | continuations. */ | |
484 | void | |
485 | discard_all_continuations (void) | |
486 | { | |
487 | struct continuation *continuation_ptr; | |
488 | ||
489 | while (cmd_continuation) | |
490 | { | |
491 | continuation_ptr = cmd_continuation; | |
492 | cmd_continuation = continuation_ptr->next; | |
493 | xfree (continuation_ptr); | |
494 | } | |
495 | } | |
496 | ||
497 | /* Add a continuation to the continuation list, the global list | |
498 | intermediate_continuation. The new continuation will be added at the front.*/ | |
499 | void | |
500 | add_intermediate_continuation (void (*continuation_hook) | |
501 | (struct continuation_arg *), | |
502 | struct continuation_arg *arg_list) | |
503 | { | |
504 | struct continuation *continuation_ptr; | |
505 | ||
506 | continuation_ptr = (struct continuation *) xmalloc (sizeof (struct continuation)); | |
507 | continuation_ptr->continuation_hook = continuation_hook; | |
508 | continuation_ptr->arg_list = arg_list; | |
509 | continuation_ptr->next = intermediate_continuation; | |
510 | intermediate_continuation = continuation_ptr; | |
511 | } | |
512 | ||
513 | /* Walk down the cmd_continuation list, and execute all the | |
514 | continuations. There is a problem though. In some cases new | |
515 | continuations may be added while we are in the middle of this | |
516 | loop. If this happens they will be added in the front, and done | |
517 | before we have a chance of exhausting those that were already | |
518 | there. We need to then save the beginning of the list in a pointer | |
519 | and do the continuations from there on, instead of using the | |
520 | global beginning of list as our iteration pointer.*/ | |
521 | void | |
522 | do_all_intermediate_continuations (void) | |
523 | { | |
524 | struct continuation *continuation_ptr; | |
525 | struct continuation *saved_continuation; | |
526 | ||
527 | /* Copy the list header into another pointer, and set the global | |
528 | list header to null, so that the global list can change as a side | |
529 | effect of invoking the continuations and the processing of | |
530 | the preexisting continuations will not be affected. */ | |
531 | continuation_ptr = intermediate_continuation; | |
532 | intermediate_continuation = NULL; | |
533 | ||
534 | /* Work now on the list we have set aside. */ | |
535 | while (continuation_ptr) | |
536 | { | |
537 | (continuation_ptr->continuation_hook) (continuation_ptr->arg_list); | |
538 | saved_continuation = continuation_ptr; | |
539 | continuation_ptr = continuation_ptr->next; | |
540 | xfree (saved_continuation); | |
541 | } | |
542 | } | |
543 | ||
544 | /* Walk down the cmd_continuation list, and get rid of all the | |
545 | continuations. */ | |
546 | void | |
547 | discard_all_intermediate_continuations (void) | |
548 | { | |
549 | struct continuation *continuation_ptr; | |
550 | ||
551 | while (intermediate_continuation) | |
552 | { | |
553 | continuation_ptr = intermediate_continuation; | |
554 | intermediate_continuation = continuation_ptr->next; | |
555 | xfree (continuation_ptr); | |
556 | } | |
557 | } | |
558 | ||
559 | \f | |
560 | ||
561 | /* Print a warning message. The first argument STRING is the warning | |
562 | message, used as an fprintf format string, the second is the | |
563 | va_list of arguments for that string. A warning is unfiltered (not | |
564 | paginated) so that the user does not need to page through each | |
565 | screen full of warnings when there are lots of them. */ | |
566 | ||
567 | void | |
568 | vwarning (const char *string, va_list args) | |
569 | { | |
570 | if (warning_hook) | |
571 | (*warning_hook) (string, args); | |
572 | else | |
573 | { | |
574 | target_terminal_ours (); | |
575 | wrap_here (""); /* Force out any buffered output */ | |
576 | gdb_flush (gdb_stdout); | |
577 | if (warning_pre_print) | |
578 | fprintf_unfiltered (gdb_stderr, warning_pre_print); | |
579 | vfprintf_unfiltered (gdb_stderr, string, args); | |
580 | fprintf_unfiltered (gdb_stderr, "\n"); | |
581 | va_end (args); | |
582 | } | |
583 | } | |
584 | ||
585 | /* Print a warning message. | |
586 | The first argument STRING is the warning message, used as a fprintf string, | |
587 | and the remaining args are passed as arguments to it. | |
588 | The primary difference between warnings and errors is that a warning | |
589 | does not force the return to command level. */ | |
590 | ||
591 | void | |
592 | warning (const char *string,...) | |
593 | { | |
594 | va_list args; | |
595 | va_start (args, string); | |
596 | vwarning (string, args); | |
597 | va_end (args); | |
598 | } | |
599 | ||
600 | /* Print an error message and return to command level. | |
601 | The first argument STRING is the error message, used as a fprintf string, | |
602 | and the remaining args are passed as arguments to it. */ | |
603 | ||
604 | NORETURN void | |
605 | verror (const char *string, va_list args) | |
606 | { | |
607 | struct ui_file *tmp_stream = mem_fileopen (); | |
608 | make_cleanup_ui_file_delete (tmp_stream); | |
609 | vfprintf_unfiltered (tmp_stream, string, args); | |
610 | error_stream (tmp_stream); | |
611 | } | |
612 | ||
613 | NORETURN void | |
614 | error (const char *string,...) | |
615 | { | |
616 | va_list args; | |
617 | va_start (args, string); | |
618 | verror (string, args); | |
619 | va_end (args); | |
620 | } | |
621 | ||
622 | static void | |
623 | do_write (void *data, const char *buffer, long length_buffer) | |
624 | { | |
625 | ui_file_write (data, buffer, length_buffer); | |
626 | } | |
627 | ||
628 | NORETURN void | |
629 | error_stream (struct ui_file *stream) | |
630 | { | |
631 | if (error_begin_hook) | |
632 | error_begin_hook (); | |
633 | ||
634 | /* Copy the stream into the GDB_LASTERR buffer. */ | |
635 | ui_file_rewind (gdb_lasterr); | |
636 | ui_file_put (stream, do_write, gdb_lasterr); | |
637 | ||
638 | /* Write the message plus any error_pre_print to gdb_stderr. */ | |
639 | target_terminal_ours (); | |
640 | wrap_here (""); /* Force out any buffered output */ | |
641 | gdb_flush (gdb_stdout); | |
642 | annotate_error_begin (); | |
643 | if (error_pre_print) | |
644 | fprintf_filtered (gdb_stderr, error_pre_print); | |
645 | ui_file_put (stream, do_write, gdb_stderr); | |
646 | fprintf_filtered (gdb_stderr, "\n"); | |
647 | ||
648 | return_to_top_level (RETURN_ERROR); | |
649 | } | |
650 | ||
651 | /* Get the last error message issued by gdb */ | |
652 | ||
653 | char * | |
654 | error_last_message (void) | |
655 | { | |
656 | long len; | |
657 | return ui_file_xstrdup (gdb_lasterr, &len); | |
658 | } | |
659 | ||
660 | /* This is to be called by main() at the very beginning */ | |
661 | ||
662 | void | |
663 | error_init (void) | |
664 | { | |
665 | gdb_lasterr = mem_fileopen (); | |
666 | } | |
667 | ||
668 | /* Print a message reporting an internal error. Ask the user if they | |
669 | want to continue, dump core, or just exit. */ | |
670 | ||
671 | NORETURN void | |
672 | internal_verror (const char *file, int line, | |
673 | const char *fmt, va_list ap) | |
674 | { | |
675 | static char msg[] = "Internal GDB error: recursive internal error.\n"; | |
676 | static int dejavu = 0; | |
677 | int quit_p; | |
678 | int dump_core_p; | |
679 | ||
680 | /* don't allow infinite error recursion. */ | |
681 | switch (dejavu) | |
682 | { | |
683 | case 0: | |
684 | dejavu = 1; | |
685 | break; | |
686 | case 1: | |
687 | dejavu = 2; | |
688 | fputs_unfiltered (msg, gdb_stderr); | |
689 | abort (); /* NOTE: GDB has only three calls to abort(). */ | |
690 | default: | |
691 | dejavu = 3; | |
692 | write (STDERR_FILENO, msg, sizeof (msg)); | |
693 | exit (1); | |
694 | } | |
695 | ||
696 | /* Try to get the message out */ | |
697 | target_terminal_ours (); | |
698 | fprintf_unfiltered (gdb_stderr, "%s:%d: gdb-internal-error: ", file, line); | |
699 | vfprintf_unfiltered (gdb_stderr, fmt, ap); | |
700 | fputs_unfiltered ("\n", gdb_stderr); | |
701 | ||
702 | /* Default (yes/batch case) is to quit GDB. When in batch mode this | |
703 | lessens the likelhood of GDB going into an infinate loop. */ | |
704 | quit_p = query ("\ | |
705 | An internal GDB error was detected. This may make further\n\ | |
706 | debugging unreliable. Quit this debugging session? "); | |
707 | ||
708 | /* Default (yes/batch case) is to dump core. This leaves a GDB | |
709 | dropping so that it is easier to see that something went wrong to | |
710 | GDB. */ | |
711 | dump_core_p = query ("\ | |
712 | Create a core file containing the current state of GDB? "); | |
713 | ||
714 | if (quit_p) | |
715 | { | |
716 | if (dump_core_p) | |
717 | abort (); /* NOTE: GDB has only three calls to abort(). */ | |
718 | else | |
719 | exit (1); | |
720 | } | |
721 | else | |
722 | { | |
723 | if (dump_core_p) | |
724 | { | |
725 | if (fork () == 0) | |
726 | abort (); /* NOTE: GDB has only three calls to abort(). */ | |
727 | } | |
728 | } | |
729 | ||
730 | dejavu = 0; | |
731 | return_to_top_level (RETURN_ERROR); | |
732 | } | |
733 | ||
734 | NORETURN void | |
735 | internal_error (const char *file, int line, const char *string, ...) | |
736 | { | |
737 | va_list ap; | |
738 | va_start (ap, string); | |
739 | ||
740 | internal_verror (file, line, string, ap); | |
741 | va_end (ap); | |
742 | } | |
743 | ||
744 | /* The strerror() function can return NULL for errno values that are | |
745 | out of range. Provide a "safe" version that always returns a | |
746 | printable string. */ | |
747 | ||
748 | char * | |
749 | safe_strerror (int errnum) | |
750 | { | |
751 | char *msg; | |
752 | static char buf[32]; | |
753 | ||
754 | if ((msg = strerror (errnum)) == NULL) | |
755 | { | |
756 | sprintf (buf, "(undocumented errno %d)", errnum); | |
757 | msg = buf; | |
758 | } | |
759 | return (msg); | |
760 | } | |
761 | ||
762 | /* Print the system error message for errno, and also mention STRING | |
763 | as the file name for which the error was encountered. | |
764 | Then return to command level. */ | |
765 | ||
766 | NORETURN void | |
767 | perror_with_name (char *string) | |
768 | { | |
769 | char *err; | |
770 | char *combined; | |
771 | ||
772 | err = safe_strerror (errno); | |
773 | combined = (char *) alloca (strlen (err) + strlen (string) + 3); | |
774 | strcpy (combined, string); | |
775 | strcat (combined, ": "); | |
776 | strcat (combined, err); | |
777 | ||
778 | /* I understand setting these is a matter of taste. Still, some people | |
779 | may clear errno but not know about bfd_error. Doing this here is not | |
780 | unreasonable. */ | |
781 | bfd_set_error (bfd_error_no_error); | |
782 | errno = 0; | |
783 | ||
784 | error ("%s.", combined); | |
785 | } | |
786 | ||
787 | /* Print the system error message for ERRCODE, and also mention STRING | |
788 | as the file name for which the error was encountered. */ | |
789 | ||
790 | void | |
791 | print_sys_errmsg (char *string, int errcode) | |
792 | { | |
793 | char *err; | |
794 | char *combined; | |
795 | ||
796 | err = safe_strerror (errcode); | |
797 | combined = (char *) alloca (strlen (err) + strlen (string) + 3); | |
798 | strcpy (combined, string); | |
799 | strcat (combined, ": "); | |
800 | strcat (combined, err); | |
801 | ||
802 | /* We want anything which was printed on stdout to come out first, before | |
803 | this message. */ | |
804 | gdb_flush (gdb_stdout); | |
805 | fprintf_unfiltered (gdb_stderr, "%s.\n", combined); | |
806 | } | |
807 | ||
808 | /* Control C eventually causes this to be called, at a convenient time. */ | |
809 | ||
810 | void | |
811 | quit (void) | |
812 | { | |
813 | struct serial *gdb_stdout_serial = serial_fdopen (1); | |
814 | ||
815 | target_terminal_ours (); | |
816 | ||
817 | /* We want all output to appear now, before we print "Quit". We | |
818 | have 3 levels of buffering we have to flush (it's possible that | |
819 | some of these should be changed to flush the lower-level ones | |
820 | too): */ | |
821 | ||
822 | /* 1. The _filtered buffer. */ | |
823 | wrap_here ((char *) 0); | |
824 | ||
825 | /* 2. The stdio buffer. */ | |
826 | gdb_flush (gdb_stdout); | |
827 | gdb_flush (gdb_stderr); | |
828 | ||
829 | /* 3. The system-level buffer. */ | |
830 | serial_drain_output (gdb_stdout_serial); | |
831 | serial_un_fdopen (gdb_stdout_serial); | |
832 | ||
833 | annotate_error_begin (); | |
834 | ||
835 | /* Don't use *_filtered; we don't want to prompt the user to continue. */ | |
836 | if (quit_pre_print) | |
837 | fprintf_unfiltered (gdb_stderr, quit_pre_print); | |
838 | ||
839 | #ifdef __MSDOS__ | |
840 | /* No steenking SIGINT will ever be coming our way when the | |
841 | program is resumed. Don't lie. */ | |
842 | fprintf_unfiltered (gdb_stderr, "Quit\n"); | |
843 | #else | |
844 | if (job_control | |
845 | /* If there is no terminal switching for this target, then we can't | |
846 | possibly get screwed by the lack of job control. */ | |
847 | || current_target.to_terminal_ours == NULL) | |
848 | fprintf_unfiltered (gdb_stderr, "Quit\n"); | |
849 | else | |
850 | fprintf_unfiltered (gdb_stderr, | |
851 | "Quit (expect signal SIGINT when the program is resumed)\n"); | |
852 | #endif | |
853 | return_to_top_level (RETURN_QUIT); | |
854 | } | |
855 | ||
856 | /* Control C comes here */ | |
857 | void | |
858 | request_quit (int signo) | |
859 | { | |
860 | quit_flag = 1; | |
861 | /* Restore the signal handler. Harmless with BSD-style signals, needed | |
862 | for System V-style signals. So just always do it, rather than worrying | |
863 | about USG defines and stuff like that. */ | |
864 | signal (signo, request_quit); | |
865 | ||
866 | #ifdef REQUEST_QUIT | |
867 | REQUEST_QUIT; | |
868 | #else | |
869 | if (immediate_quit) | |
870 | quit (); | |
871 | #endif | |
872 | } | |
873 | \f | |
874 | /* Memory management stuff (malloc friends). */ | |
875 | ||
876 | #if !defined (USE_MMALLOC) | |
877 | ||
878 | /* NOTE: These must use PTR so that their definition matches the | |
879 | declaration found in "mmalloc.h". */ | |
880 | ||
881 | static void * | |
882 | mmalloc (void *md, size_t size) | |
883 | { | |
884 | return malloc (size); /* NOTE: GDB's only call to malloc() */ | |
885 | } | |
886 | ||
887 | static void * | |
888 | mrealloc (void *md, void *ptr, size_t size) | |
889 | { | |
890 | if (ptr == 0) /* Guard against old realloc's */ | |
891 | return mmalloc (md, size); | |
892 | else | |
893 | return realloc (ptr, size); /* NOTE: GDB's only call to ralloc() */ | |
894 | } | |
895 | ||
896 | static void * | |
897 | mcalloc (void *md, size_t number, size_t size) | |
898 | { | |
899 | return calloc (number, size); /* NOTE: GDB's only call to calloc() */ | |
900 | } | |
901 | ||
902 | static void | |
903 | mfree (void *md, void *ptr) | |
904 | { | |
905 | free (ptr); /* NOTE: GDB's only call to free() */ | |
906 | } | |
907 | ||
908 | #endif /* USE_MMALLOC */ | |
909 | ||
910 | #if !defined (USE_MMALLOC) || defined (NO_MMCHECK) | |
911 | ||
912 | void | |
913 | init_malloc (void *md) | |
914 | { | |
915 | } | |
916 | ||
917 | #else /* Have mmalloc and want corruption checking */ | |
918 | ||
919 | static void | |
920 | malloc_botch (void) | |
921 | { | |
922 | fprintf_unfiltered (gdb_stderr, "Memory corruption\n"); | |
923 | internal_error (__FILE__, __LINE__, "failed internal consistency check"); | |
924 | } | |
925 | ||
926 | /* Attempt to install hooks in mmalloc/mrealloc/mfree for the heap specified | |
927 | by MD, to detect memory corruption. Note that MD may be NULL to specify | |
928 | the default heap that grows via sbrk. | |
929 | ||
930 | Note that for freshly created regions, we must call mmcheckf prior to any | |
931 | mallocs in the region. Otherwise, any region which was allocated prior to | |
932 | installing the checking hooks, which is later reallocated or freed, will | |
933 | fail the checks! The mmcheck function only allows initial hooks to be | |
934 | installed before the first mmalloc. However, anytime after we have called | |
935 | mmcheck the first time to install the checking hooks, we can call it again | |
936 | to update the function pointer to the memory corruption handler. | |
937 | ||
938 | Returns zero on failure, non-zero on success. */ | |
939 | ||
940 | #ifndef MMCHECK_FORCE | |
941 | #define MMCHECK_FORCE 0 | |
942 | #endif | |
943 | ||
944 | void | |
945 | init_malloc (void *md) | |
946 | { | |
947 | if (!mmcheckf (md, malloc_botch, MMCHECK_FORCE)) | |
948 | { | |
949 | /* Don't use warning(), which relies on current_target being set | |
950 | to something other than dummy_target, until after | |
951 | initialize_all_files(). */ | |
952 | ||
953 | fprintf_unfiltered | |
954 | (gdb_stderr, "warning: failed to install memory consistency checks; "); | |
955 | fprintf_unfiltered | |
956 | (gdb_stderr, "configuration should define NO_MMCHECK or MMCHECK_FORCE\n"); | |
957 | } | |
958 | ||
959 | mmtrace (); | |
960 | } | |
961 | ||
962 | #endif /* Have mmalloc and want corruption checking */ | |
963 | ||
964 | /* Called when a memory allocation fails, with the number of bytes of | |
965 | memory requested in SIZE. */ | |
966 | ||
967 | NORETURN void | |
968 | nomem (long size) | |
969 | { | |
970 | if (size > 0) | |
971 | { | |
972 | internal_error (__FILE__, __LINE__, | |
973 | "virtual memory exhausted: can't allocate %ld bytes.", size); | |
974 | } | |
975 | else | |
976 | { | |
977 | internal_error (__FILE__, __LINE__, | |
978 | "virtual memory exhausted."); | |
979 | } | |
980 | } | |
981 | ||
982 | /* The xmmalloc() family of memory management routines. | |
983 | ||
984 | These are are like the mmalloc() family except that they implement | |
985 | consistent semantics and guard against typical memory management | |
986 | problems: if a malloc fails, an internal error is thrown; if | |
987 | free(NULL) is called, it is ignored; if *alloc(0) is called, NULL | |
988 | is returned. | |
989 | ||
990 | All these routines are implemented using the mmalloc() family. */ | |
991 | ||
992 | void * | |
993 | xmmalloc (void *md, size_t size) | |
994 | { | |
995 | void *val; | |
996 | ||
997 | if (size == 0) | |
998 | { | |
999 | val = NULL; | |
1000 | } | |
1001 | else | |
1002 | { | |
1003 | val = mmalloc (md, size); | |
1004 | if (val == NULL) | |
1005 | nomem (size); | |
1006 | } | |
1007 | return (val); | |
1008 | } | |
1009 | ||
1010 | void * | |
1011 | xmrealloc (void *md, void *ptr, size_t size) | |
1012 | { | |
1013 | void *val; | |
1014 | ||
1015 | if (size == 0) | |
1016 | { | |
1017 | if (ptr != NULL) | |
1018 | mfree (md, ptr); | |
1019 | val = NULL; | |
1020 | } | |
1021 | else | |
1022 | { | |
1023 | if (ptr != NULL) | |
1024 | { | |
1025 | val = mrealloc (md, ptr, size); | |
1026 | } | |
1027 | else | |
1028 | { | |
1029 | val = mmalloc (md, size); | |
1030 | } | |
1031 | if (val == NULL) | |
1032 | { | |
1033 | nomem (size); | |
1034 | } | |
1035 | } | |
1036 | return (val); | |
1037 | } | |
1038 | ||
1039 | void * | |
1040 | xmcalloc (void *md, size_t number, size_t size) | |
1041 | { | |
1042 | void *mem; | |
1043 | if (number == 0 || size == 0) | |
1044 | mem = NULL; | |
1045 | else | |
1046 | { | |
1047 | mem = mcalloc (md, number, size); | |
1048 | if (mem == NULL) | |
1049 | nomem (number * size); | |
1050 | } | |
1051 | return mem; | |
1052 | } | |
1053 | ||
1054 | void | |
1055 | xmfree (void *md, void *ptr) | |
1056 | { | |
1057 | if (ptr != NULL) | |
1058 | mfree (md, ptr); | |
1059 | } | |
1060 | ||
1061 | /* The xmalloc() (libiberty.h) family of memory management routines. | |
1062 | ||
1063 | These are like the ISO-C malloc() family except that they implement | |
1064 | consistent semantics and guard against typical memory management | |
1065 | problems. See xmmalloc() above for further information. | |
1066 | ||
1067 | All these routines are wrappers to the xmmalloc() family. */ | |
1068 | ||
1069 | /* NOTE: These are declared using PTR to ensure consistency with | |
1070 | "libiberty.h". xfree() is GDB local. */ | |
1071 | ||
1072 | PTR | |
1073 | xmalloc (size_t size) | |
1074 | { | |
1075 | return xmmalloc (NULL, size); | |
1076 | } | |
1077 | ||
1078 | PTR | |
1079 | xrealloc (PTR ptr, size_t size) | |
1080 | { | |
1081 | return xmrealloc (NULL, ptr, size); | |
1082 | } | |
1083 | ||
1084 | PTR | |
1085 | xcalloc (size_t number, size_t size) | |
1086 | { | |
1087 | return xmcalloc (NULL, number, size); | |
1088 | } | |
1089 | ||
1090 | void | |
1091 | xfree (void *ptr) | |
1092 | { | |
1093 | xmfree (NULL, ptr); | |
1094 | } | |
1095 | \f | |
1096 | ||
1097 | /* Like asprintf/vasprintf but get an internal_error if the call | |
1098 | fails. */ | |
1099 | ||
1100 | void | |
1101 | xasprintf (char **ret, const char *format, ...) | |
1102 | { | |
1103 | va_list args; | |
1104 | va_start (args, format); | |
1105 | xvasprintf (ret, format, args); | |
1106 | va_end (args); | |
1107 | } | |
1108 | ||
1109 | void | |
1110 | xvasprintf (char **ret, const char *format, va_list ap) | |
1111 | { | |
1112 | int status = vasprintf (ret, format, ap); | |
1113 | /* NULL could be returned due to a memory allocation problem; a | |
1114 | badly format string; or something else. */ | |
1115 | if ((*ret) == NULL) | |
1116 | internal_error (__FILE__, __LINE__, | |
1117 | "vasprintf returned NULL buffer (errno %d)", | |
1118 | errno); | |
1119 | /* A negative status with a non-NULL buffer shouldn't never | |
1120 | happen. But to be sure. */ | |
1121 | if (status < 0) | |
1122 | internal_error (__FILE__, __LINE__, | |
1123 | "vasprintf call failed (errno %d)", | |
1124 | errno); | |
1125 | } | |
1126 | ||
1127 | ||
1128 | /* My replacement for the read system call. | |
1129 | Used like `read' but keeps going if `read' returns too soon. */ | |
1130 | ||
1131 | int | |
1132 | myread (int desc, char *addr, int len) | |
1133 | { | |
1134 | register int val; | |
1135 | int orglen = len; | |
1136 | ||
1137 | while (len > 0) | |
1138 | { | |
1139 | val = read (desc, addr, len); | |
1140 | if (val < 0) | |
1141 | return val; | |
1142 | if (val == 0) | |
1143 | return orglen - len; | |
1144 | len -= val; | |
1145 | addr += val; | |
1146 | } | |
1147 | return orglen; | |
1148 | } | |
1149 | \f | |
1150 | /* Make a copy of the string at PTR with SIZE characters | |
1151 | (and add a null character at the end in the copy). | |
1152 | Uses malloc to get the space. Returns the address of the copy. */ | |
1153 | ||
1154 | char * | |
1155 | savestring (const char *ptr, size_t size) | |
1156 | { | |
1157 | register char *p = (char *) xmalloc (size + 1); | |
1158 | memcpy (p, ptr, size); | |
1159 | p[size] = 0; | |
1160 | return p; | |
1161 | } | |
1162 | ||
1163 | char * | |
1164 | msavestring (void *md, const char *ptr, size_t size) | |
1165 | { | |
1166 | register char *p = (char *) xmmalloc (md, size + 1); | |
1167 | memcpy (p, ptr, size); | |
1168 | p[size] = 0; | |
1169 | return p; | |
1170 | } | |
1171 | ||
1172 | char * | |
1173 | mstrsave (void *md, const char *ptr) | |
1174 | { | |
1175 | return (msavestring (md, ptr, strlen (ptr))); | |
1176 | } | |
1177 | ||
1178 | void | |
1179 | print_spaces (register int n, register struct ui_file *file) | |
1180 | { | |
1181 | fputs_unfiltered (n_spaces (n), file); | |
1182 | } | |
1183 | ||
1184 | /* Print a host address. */ | |
1185 | ||
1186 | void | |
1187 | gdb_print_host_address (void *addr, struct ui_file *stream) | |
1188 | { | |
1189 | ||
1190 | /* We could use the %p conversion specifier to fprintf if we had any | |
1191 | way of knowing whether this host supports it. But the following | |
1192 | should work on the Alpha and on 32 bit machines. */ | |
1193 | ||
1194 | fprintf_filtered (stream, "0x%lx", (unsigned long) addr); | |
1195 | } | |
1196 | ||
1197 | /* Ask user a y-or-n question and return 1 iff answer is yes. | |
1198 | Takes three args which are given to printf to print the question. | |
1199 | The first, a control string, should end in "? ". | |
1200 | It should not say how to answer, because we do that. */ | |
1201 | ||
1202 | /* VARARGS */ | |
1203 | int | |
1204 | query (char *ctlstr,...) | |
1205 | { | |
1206 | va_list args; | |
1207 | register int answer; | |
1208 | register int ans2; | |
1209 | int retval; | |
1210 | ||
1211 | va_start (args, ctlstr); | |
1212 | ||
1213 | if (query_hook) | |
1214 | { | |
1215 | return query_hook (ctlstr, args); | |
1216 | } | |
1217 | ||
1218 | /* Automatically answer "yes" if input is not from a terminal. */ | |
1219 | if (!input_from_terminal_p ()) | |
1220 | return 1; | |
1221 | ||
1222 | while (1) | |
1223 | { | |
1224 | wrap_here (""); /* Flush any buffered output */ | |
1225 | gdb_flush (gdb_stdout); | |
1226 | ||
1227 | if (annotation_level > 1) | |
1228 | printf_filtered ("\n\032\032pre-query\n"); | |
1229 | ||
1230 | vfprintf_filtered (gdb_stdout, ctlstr, args); | |
1231 | printf_filtered ("(y or n) "); | |
1232 | ||
1233 | if (annotation_level > 1) | |
1234 | printf_filtered ("\n\032\032query\n"); | |
1235 | ||
1236 | wrap_here (""); | |
1237 | gdb_flush (gdb_stdout); | |
1238 | ||
1239 | answer = fgetc (stdin); | |
1240 | clearerr (stdin); /* in case of C-d */ | |
1241 | if (answer == EOF) /* C-d */ | |
1242 | { | |
1243 | retval = 1; | |
1244 | break; | |
1245 | } | |
1246 | /* Eat rest of input line, to EOF or newline */ | |
1247 | if (answer != '\n') | |
1248 | do | |
1249 | { | |
1250 | ans2 = fgetc (stdin); | |
1251 | clearerr (stdin); | |
1252 | } | |
1253 | while (ans2 != EOF && ans2 != '\n' && ans2 != '\r'); | |
1254 | ||
1255 | if (answer >= 'a') | |
1256 | answer -= 040; | |
1257 | if (answer == 'Y') | |
1258 | { | |
1259 | retval = 1; | |
1260 | break; | |
1261 | } | |
1262 | if (answer == 'N') | |
1263 | { | |
1264 | retval = 0; | |
1265 | break; | |
1266 | } | |
1267 | printf_filtered ("Please answer y or n.\n"); | |
1268 | } | |
1269 | ||
1270 | if (annotation_level > 1) | |
1271 | printf_filtered ("\n\032\032post-query\n"); | |
1272 | return retval; | |
1273 | } | |
1274 | \f | |
1275 | ||
1276 | /* Parse a C escape sequence. STRING_PTR points to a variable | |
1277 | containing a pointer to the string to parse. That pointer | |
1278 | should point to the character after the \. That pointer | |
1279 | is updated past the characters we use. The value of the | |
1280 | escape sequence is returned. | |
1281 | ||
1282 | A negative value means the sequence \ newline was seen, | |
1283 | which is supposed to be equivalent to nothing at all. | |
1284 | ||
1285 | If \ is followed by a null character, we return a negative | |
1286 | value and leave the string pointer pointing at the null character. | |
1287 | ||
1288 | If \ is followed by 000, we return 0 and leave the string pointer | |
1289 | after the zeros. A value of 0 does not mean end of string. */ | |
1290 | ||
1291 | int | |
1292 | parse_escape (char **string_ptr) | |
1293 | { | |
1294 | register int c = *(*string_ptr)++; | |
1295 | switch (c) | |
1296 | { | |
1297 | case 'a': | |
1298 | return 007; /* Bell (alert) char */ | |
1299 | case 'b': | |
1300 | return '\b'; | |
1301 | case 'e': /* Escape character */ | |
1302 | return 033; | |
1303 | case 'f': | |
1304 | return '\f'; | |
1305 | case 'n': | |
1306 | return '\n'; | |
1307 | case 'r': | |
1308 | return '\r'; | |
1309 | case 't': | |
1310 | return '\t'; | |
1311 | case 'v': | |
1312 | return '\v'; | |
1313 | case '\n': | |
1314 | return -2; | |
1315 | case 0: | |
1316 | (*string_ptr)--; | |
1317 | return 0; | |
1318 | case '^': | |
1319 | c = *(*string_ptr)++; | |
1320 | if (c == '\\') | |
1321 | c = parse_escape (string_ptr); | |
1322 | if (c == '?') | |
1323 | return 0177; | |
1324 | return (c & 0200) | (c & 037); | |
1325 | ||
1326 | case '0': | |
1327 | case '1': | |
1328 | case '2': | |
1329 | case '3': | |
1330 | case '4': | |
1331 | case '5': | |
1332 | case '6': | |
1333 | case '7': | |
1334 | { | |
1335 | register int i = c - '0'; | |
1336 | register int count = 0; | |
1337 | while (++count < 3) | |
1338 | { | |
1339 | if ((c = *(*string_ptr)++) >= '0' && c <= '7') | |
1340 | { | |
1341 | i *= 8; | |
1342 | i += c - '0'; | |
1343 | } | |
1344 | else | |
1345 | { | |
1346 | (*string_ptr)--; | |
1347 | break; | |
1348 | } | |
1349 | } | |
1350 | return i; | |
1351 | } | |
1352 | default: | |
1353 | return c; | |
1354 | } | |
1355 | } | |
1356 | \f | |
1357 | /* Print the character C on STREAM as part of the contents of a literal | |
1358 | string whose delimiter is QUOTER. Note that this routine should only | |
1359 | be call for printing things which are independent of the language | |
1360 | of the program being debugged. */ | |
1361 | ||
1362 | static void | |
1363 | printchar (int c, void (*do_fputs) (const char *, struct ui_file *), | |
1364 | void (*do_fprintf) (struct ui_file *, const char *, ...), | |
1365 | struct ui_file *stream, int quoter) | |
1366 | { | |
1367 | ||
1368 | c &= 0xFF; /* Avoid sign bit follies */ | |
1369 | ||
1370 | if (c < 0x20 || /* Low control chars */ | |
1371 | (c >= 0x7F && c < 0xA0) || /* DEL, High controls */ | |
1372 | (sevenbit_strings && c >= 0x80)) | |
1373 | { /* high order bit set */ | |
1374 | switch (c) | |
1375 | { | |
1376 | case '\n': | |
1377 | do_fputs ("\\n", stream); | |
1378 | break; | |
1379 | case '\b': | |
1380 | do_fputs ("\\b", stream); | |
1381 | break; | |
1382 | case '\t': | |
1383 | do_fputs ("\\t", stream); | |
1384 | break; | |
1385 | case '\f': | |
1386 | do_fputs ("\\f", stream); | |
1387 | break; | |
1388 | case '\r': | |
1389 | do_fputs ("\\r", stream); | |
1390 | break; | |
1391 | case '\033': | |
1392 | do_fputs ("\\e", stream); | |
1393 | break; | |
1394 | case '\007': | |
1395 | do_fputs ("\\a", stream); | |
1396 | break; | |
1397 | default: | |
1398 | do_fprintf (stream, "\\%.3o", (unsigned int) c); | |
1399 | break; | |
1400 | } | |
1401 | } | |
1402 | else | |
1403 | { | |
1404 | if (c == '\\' || c == quoter) | |
1405 | do_fputs ("\\", stream); | |
1406 | do_fprintf (stream, "%c", c); | |
1407 | } | |
1408 | } | |
1409 | ||
1410 | /* Print the character C on STREAM as part of the contents of a | |
1411 | literal string whose delimiter is QUOTER. Note that these routines | |
1412 | should only be call for printing things which are independent of | |
1413 | the language of the program being debugged. */ | |
1414 | ||
1415 | void | |
1416 | fputstr_filtered (const char *str, int quoter, struct ui_file *stream) | |
1417 | { | |
1418 | while (*str) | |
1419 | printchar (*str++, fputs_filtered, fprintf_filtered, stream, quoter); | |
1420 | } | |
1421 | ||
1422 | void | |
1423 | fputstr_unfiltered (const char *str, int quoter, struct ui_file *stream) | |
1424 | { | |
1425 | while (*str) | |
1426 | printchar (*str++, fputs_unfiltered, fprintf_unfiltered, stream, quoter); | |
1427 | } | |
1428 | ||
1429 | void | |
1430 | fputstrn_unfiltered (const char *str, int n, int quoter, struct ui_file *stream) | |
1431 | { | |
1432 | int i; | |
1433 | for (i = 0; i < n; i++) | |
1434 | printchar (str[i], fputs_unfiltered, fprintf_unfiltered, stream, quoter); | |
1435 | } | |
1436 | ||
1437 | \f | |
1438 | ||
1439 | /* Number of lines per page or UINT_MAX if paging is disabled. */ | |
1440 | static unsigned int lines_per_page; | |
1441 | /* Number of chars per line or UINT_MAX if line folding is disabled. */ | |
1442 | static unsigned int chars_per_line; | |
1443 | /* Current count of lines printed on this page, chars on this line. */ | |
1444 | static unsigned int lines_printed, chars_printed; | |
1445 | ||
1446 | /* Buffer and start column of buffered text, for doing smarter word- | |
1447 | wrapping. When someone calls wrap_here(), we start buffering output | |
1448 | that comes through fputs_filtered(). If we see a newline, we just | |
1449 | spit it out and forget about the wrap_here(). If we see another | |
1450 | wrap_here(), we spit it out and remember the newer one. If we see | |
1451 | the end of the line, we spit out a newline, the indent, and then | |
1452 | the buffered output. */ | |
1453 | ||
1454 | /* Malloc'd buffer with chars_per_line+2 bytes. Contains characters which | |
1455 | are waiting to be output (they have already been counted in chars_printed). | |
1456 | When wrap_buffer[0] is null, the buffer is empty. */ | |
1457 | static char *wrap_buffer; | |
1458 | ||
1459 | /* Pointer in wrap_buffer to the next character to fill. */ | |
1460 | static char *wrap_pointer; | |
1461 | ||
1462 | /* String to indent by if the wrap occurs. Must not be NULL if wrap_column | |
1463 | is non-zero. */ | |
1464 | static char *wrap_indent; | |
1465 | ||
1466 | /* Column number on the screen where wrap_buffer begins, or 0 if wrapping | |
1467 | is not in effect. */ | |
1468 | static int wrap_column; | |
1469 | \f | |
1470 | ||
1471 | /* Inialize the lines and chars per page */ | |
1472 | void | |
1473 | init_page_info (void) | |
1474 | { | |
1475 | #if defined(TUI) | |
1476 | if (!tui_get_command_dimension (&chars_per_line, &lines_per_page)) | |
1477 | #endif | |
1478 | { | |
1479 | /* These defaults will be used if we are unable to get the correct | |
1480 | values from termcap. */ | |
1481 | #if defined(__GO32__) | |
1482 | lines_per_page = ScreenRows (); | |
1483 | chars_per_line = ScreenCols (); | |
1484 | #else | |
1485 | lines_per_page = 24; | |
1486 | chars_per_line = 80; | |
1487 | ||
1488 | #if !defined (_WIN32) | |
1489 | /* No termcap under MPW, although might be cool to do something | |
1490 | by looking at worksheet or console window sizes. */ | |
1491 | /* Initialize the screen height and width from termcap. */ | |
1492 | { | |
1493 | char *termtype = getenv ("TERM"); | |
1494 | ||
1495 | /* Positive means success, nonpositive means failure. */ | |
1496 | int status; | |
1497 | ||
1498 | /* 2048 is large enough for all known terminals, according to the | |
1499 | GNU termcap manual. */ | |
1500 | char term_buffer[2048]; | |
1501 | ||
1502 | if (termtype) | |
1503 | { | |
1504 | status = tgetent (term_buffer, termtype); | |
1505 | if (status > 0) | |
1506 | { | |
1507 | int val; | |
1508 | int running_in_emacs = getenv ("EMACS") != NULL; | |
1509 | ||
1510 | val = tgetnum ("li"); | |
1511 | if (val >= 0 && !running_in_emacs) | |
1512 | lines_per_page = val; | |
1513 | else | |
1514 | /* The number of lines per page is not mentioned | |
1515 | in the terminal description. This probably means | |
1516 | that paging is not useful (e.g. emacs shell window), | |
1517 | so disable paging. */ | |
1518 | lines_per_page = UINT_MAX; | |
1519 | ||
1520 | val = tgetnum ("co"); | |
1521 | if (val >= 0) | |
1522 | chars_per_line = val; | |
1523 | } | |
1524 | } | |
1525 | } | |
1526 | #endif /* MPW */ | |
1527 | ||
1528 | #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER) | |
1529 | ||
1530 | /* If there is a better way to determine the window size, use it. */ | |
1531 | SIGWINCH_HANDLER (SIGWINCH); | |
1532 | #endif | |
1533 | #endif | |
1534 | /* If the output is not a terminal, don't paginate it. */ | |
1535 | if (!ui_file_isatty (gdb_stdout)) | |
1536 | lines_per_page = UINT_MAX; | |
1537 | } /* the command_line_version */ | |
1538 | set_width (); | |
1539 | } | |
1540 | ||
1541 | static void | |
1542 | set_width (void) | |
1543 | { | |
1544 | if (chars_per_line == 0) | |
1545 | init_page_info (); | |
1546 | ||
1547 | if (!wrap_buffer) | |
1548 | { | |
1549 | wrap_buffer = (char *) xmalloc (chars_per_line + 2); | |
1550 | wrap_buffer[0] = '\0'; | |
1551 | } | |
1552 | else | |
1553 | wrap_buffer = (char *) xrealloc (wrap_buffer, chars_per_line + 2); | |
1554 | wrap_pointer = wrap_buffer; /* Start it at the beginning */ | |
1555 | } | |
1556 | ||
1557 | /* ARGSUSED */ | |
1558 | static void | |
1559 | set_width_command (char *args, int from_tty, struct cmd_list_element *c) | |
1560 | { | |
1561 | set_width (); | |
1562 | } | |
1563 | ||
1564 | /* Wait, so the user can read what's on the screen. Prompt the user | |
1565 | to continue by pressing RETURN. */ | |
1566 | ||
1567 | static void | |
1568 | prompt_for_continue (void) | |
1569 | { | |
1570 | char *ignore; | |
1571 | char cont_prompt[120]; | |
1572 | ||
1573 | if (annotation_level > 1) | |
1574 | printf_unfiltered ("\n\032\032pre-prompt-for-continue\n"); | |
1575 | ||
1576 | strcpy (cont_prompt, | |
1577 | "---Type <return> to continue, or q <return> to quit---"); | |
1578 | if (annotation_level > 1) | |
1579 | strcat (cont_prompt, "\n\032\032prompt-for-continue\n"); | |
1580 | ||
1581 | /* We must do this *before* we call gdb_readline, else it will eventually | |
1582 | call us -- thinking that we're trying to print beyond the end of the | |
1583 | screen. */ | |
1584 | reinitialize_more_filter (); | |
1585 | ||
1586 | immediate_quit++; | |
1587 | /* On a real operating system, the user can quit with SIGINT. | |
1588 | But not on GO32. | |
1589 | ||
1590 | 'q' is provided on all systems so users don't have to change habits | |
1591 | from system to system, and because telling them what to do in | |
1592 | the prompt is more user-friendly than expecting them to think of | |
1593 | SIGINT. */ | |
1594 | /* Call readline, not gdb_readline, because GO32 readline handles control-C | |
1595 | whereas control-C to gdb_readline will cause the user to get dumped | |
1596 | out to DOS. */ | |
1597 | ignore = readline (cont_prompt); | |
1598 | ||
1599 | if (annotation_level > 1) | |
1600 | printf_unfiltered ("\n\032\032post-prompt-for-continue\n"); | |
1601 | ||
1602 | if (ignore) | |
1603 | { | |
1604 | char *p = ignore; | |
1605 | while (*p == ' ' || *p == '\t') | |
1606 | ++p; | |
1607 | if (p[0] == 'q') | |
1608 | { | |
1609 | if (!event_loop_p) | |
1610 | request_quit (SIGINT); | |
1611 | else | |
1612 | async_request_quit (0); | |
1613 | } | |
1614 | xfree (ignore); | |
1615 | } | |
1616 | immediate_quit--; | |
1617 | ||
1618 | /* Now we have to do this again, so that GDB will know that it doesn't | |
1619 | need to save the ---Type <return>--- line at the top of the screen. */ | |
1620 | reinitialize_more_filter (); | |
1621 | ||
1622 | dont_repeat (); /* Forget prev cmd -- CR won't repeat it. */ | |
1623 | } | |
1624 | ||
1625 | /* Reinitialize filter; ie. tell it to reset to original values. */ | |
1626 | ||
1627 | void | |
1628 | reinitialize_more_filter (void) | |
1629 | { | |
1630 | lines_printed = 0; | |
1631 | chars_printed = 0; | |
1632 | } | |
1633 | ||
1634 | /* Indicate that if the next sequence of characters overflows the line, | |
1635 | a newline should be inserted here rather than when it hits the end. | |
1636 | If INDENT is non-null, it is a string to be printed to indent the | |
1637 | wrapped part on the next line. INDENT must remain accessible until | |
1638 | the next call to wrap_here() or until a newline is printed through | |
1639 | fputs_filtered(). | |
1640 | ||
1641 | If the line is already overfull, we immediately print a newline and | |
1642 | the indentation, and disable further wrapping. | |
1643 | ||
1644 | If we don't know the width of lines, but we know the page height, | |
1645 | we must not wrap words, but should still keep track of newlines | |
1646 | that were explicitly printed. | |
1647 | ||
1648 | INDENT should not contain tabs, as that will mess up the char count | |
1649 | on the next line. FIXME. | |
1650 | ||
1651 | This routine is guaranteed to force out any output which has been | |
1652 | squirreled away in the wrap_buffer, so wrap_here ((char *)0) can be | |
1653 | used to force out output from the wrap_buffer. */ | |
1654 | ||
1655 | void | |
1656 | wrap_here (char *indent) | |
1657 | { | |
1658 | /* This should have been allocated, but be paranoid anyway. */ | |
1659 | if (!wrap_buffer) | |
1660 | internal_error (__FILE__, __LINE__, "failed internal consistency check"); | |
1661 | ||
1662 | if (wrap_buffer[0]) | |
1663 | { | |
1664 | *wrap_pointer = '\0'; | |
1665 | fputs_unfiltered (wrap_buffer, gdb_stdout); | |
1666 | } | |
1667 | wrap_pointer = wrap_buffer; | |
1668 | wrap_buffer[0] = '\0'; | |
1669 | if (chars_per_line == UINT_MAX) /* No line overflow checking */ | |
1670 | { | |
1671 | wrap_column = 0; | |
1672 | } | |
1673 | else if (chars_printed >= chars_per_line) | |
1674 | { | |
1675 | puts_filtered ("\n"); | |
1676 | if (indent != NULL) | |
1677 | puts_filtered (indent); | |
1678 | wrap_column = 0; | |
1679 | } | |
1680 | else | |
1681 | { | |
1682 | wrap_column = chars_printed; | |
1683 | if (indent == NULL) | |
1684 | wrap_indent = ""; | |
1685 | else | |
1686 | wrap_indent = indent; | |
1687 | } | |
1688 | } | |
1689 | ||
1690 | /* Ensure that whatever gets printed next, using the filtered output | |
1691 | commands, starts at the beginning of the line. I.E. if there is | |
1692 | any pending output for the current line, flush it and start a new | |
1693 | line. Otherwise do nothing. */ | |
1694 | ||
1695 | void | |
1696 | begin_line (void) | |
1697 | { | |
1698 | if (chars_printed > 0) | |
1699 | { | |
1700 | puts_filtered ("\n"); | |
1701 | } | |
1702 | } | |
1703 | ||
1704 | ||
1705 | /* Like fputs but if FILTER is true, pause after every screenful. | |
1706 | ||
1707 | Regardless of FILTER can wrap at points other than the final | |
1708 | character of a line. | |
1709 | ||
1710 | Unlike fputs, fputs_maybe_filtered does not return a value. | |
1711 | It is OK for LINEBUFFER to be NULL, in which case just don't print | |
1712 | anything. | |
1713 | ||
1714 | Note that a longjmp to top level may occur in this routine (only if | |
1715 | FILTER is true) (since prompt_for_continue may do so) so this | |
1716 | routine should not be called when cleanups are not in place. */ | |
1717 | ||
1718 | static void | |
1719 | fputs_maybe_filtered (const char *linebuffer, struct ui_file *stream, | |
1720 | int filter) | |
1721 | { | |
1722 | const char *lineptr; | |
1723 | ||
1724 | if (linebuffer == 0) | |
1725 | return; | |
1726 | ||
1727 | /* Don't do any filtering if it is disabled. */ | |
1728 | if ((stream != gdb_stdout) || !pagination_enabled | |
1729 | || (lines_per_page == UINT_MAX && chars_per_line == UINT_MAX)) | |
1730 | { | |
1731 | fputs_unfiltered (linebuffer, stream); | |
1732 | return; | |
1733 | } | |
1734 | ||
1735 | /* Go through and output each character. Show line extension | |
1736 | when this is necessary; prompt user for new page when this is | |
1737 | necessary. */ | |
1738 | ||
1739 | lineptr = linebuffer; | |
1740 | while (*lineptr) | |
1741 | { | |
1742 | /* Possible new page. */ | |
1743 | if (filter && | |
1744 | (lines_printed >= lines_per_page - 1)) | |
1745 | prompt_for_continue (); | |
1746 | ||
1747 | while (*lineptr && *lineptr != '\n') | |
1748 | { | |
1749 | /* Print a single line. */ | |
1750 | if (*lineptr == '\t') | |
1751 | { | |
1752 | if (wrap_column) | |
1753 | *wrap_pointer++ = '\t'; | |
1754 | else | |
1755 | fputc_unfiltered ('\t', stream); | |
1756 | /* Shifting right by 3 produces the number of tab stops | |
1757 | we have already passed, and then adding one and | |
1758 | shifting left 3 advances to the next tab stop. */ | |
1759 | chars_printed = ((chars_printed >> 3) + 1) << 3; | |
1760 | lineptr++; | |
1761 | } | |
1762 | else | |
1763 | { | |
1764 | if (wrap_column) | |
1765 | *wrap_pointer++ = *lineptr; | |
1766 | else | |
1767 | fputc_unfiltered (*lineptr, stream); | |
1768 | chars_printed++; | |
1769 | lineptr++; | |
1770 | } | |
1771 | ||
1772 | if (chars_printed >= chars_per_line) | |
1773 | { | |
1774 | unsigned int save_chars = chars_printed; | |
1775 | ||
1776 | chars_printed = 0; | |
1777 | lines_printed++; | |
1778 | /* If we aren't actually wrapping, don't output newline -- | |
1779 | if chars_per_line is right, we probably just overflowed | |
1780 | anyway; if it's wrong, let us keep going. */ | |
1781 | if (wrap_column) | |
1782 | fputc_unfiltered ('\n', stream); | |
1783 | ||
1784 | /* Possible new page. */ | |
1785 | if (lines_printed >= lines_per_page - 1) | |
1786 | prompt_for_continue (); | |
1787 | ||
1788 | /* Now output indentation and wrapped string */ | |
1789 | if (wrap_column) | |
1790 | { | |
1791 | fputs_unfiltered (wrap_indent, stream); | |
1792 | *wrap_pointer = '\0'; /* Null-terminate saved stuff */ | |
1793 | fputs_unfiltered (wrap_buffer, stream); /* and eject it */ | |
1794 | /* FIXME, this strlen is what prevents wrap_indent from | |
1795 | containing tabs. However, if we recurse to print it | |
1796 | and count its chars, we risk trouble if wrap_indent is | |
1797 | longer than (the user settable) chars_per_line. | |
1798 | Note also that this can set chars_printed > chars_per_line | |
1799 | if we are printing a long string. */ | |
1800 | chars_printed = strlen (wrap_indent) | |
1801 | + (save_chars - wrap_column); | |
1802 | wrap_pointer = wrap_buffer; /* Reset buffer */ | |
1803 | wrap_buffer[0] = '\0'; | |
1804 | wrap_column = 0; /* And disable fancy wrap */ | |
1805 | } | |
1806 | } | |
1807 | } | |
1808 | ||
1809 | if (*lineptr == '\n') | |
1810 | { | |
1811 | chars_printed = 0; | |
1812 | wrap_here ((char *) 0); /* Spit out chars, cancel further wraps */ | |
1813 | lines_printed++; | |
1814 | fputc_unfiltered ('\n', stream); | |
1815 | lineptr++; | |
1816 | } | |
1817 | } | |
1818 | } | |
1819 | ||
1820 | void | |
1821 | fputs_filtered (const char *linebuffer, struct ui_file *stream) | |
1822 | { | |
1823 | fputs_maybe_filtered (linebuffer, stream, 1); | |
1824 | } | |
1825 | ||
1826 | int | |
1827 | putchar_unfiltered (int c) | |
1828 | { | |
1829 | char buf = c; | |
1830 | ui_file_write (gdb_stdout, &buf, 1); | |
1831 | return c; | |
1832 | } | |
1833 | ||
1834 | /* Write character C to gdb_stdout using GDB's paging mechanism and return C. | |
1835 | May return nonlocally. */ | |
1836 | ||
1837 | int | |
1838 | putchar_filtered (int c) | |
1839 | { | |
1840 | return fputc_filtered (c, gdb_stdout); | |
1841 | } | |
1842 | ||
1843 | int | |
1844 | fputc_unfiltered (int c, struct ui_file *stream) | |
1845 | { | |
1846 | char buf = c; | |
1847 | ui_file_write (stream, &buf, 1); | |
1848 | return c; | |
1849 | } | |
1850 | ||
1851 | int | |
1852 | fputc_filtered (int c, struct ui_file *stream) | |
1853 | { | |
1854 | char buf[2]; | |
1855 | ||
1856 | buf[0] = c; | |
1857 | buf[1] = 0; | |
1858 | fputs_filtered (buf, stream); | |
1859 | return c; | |
1860 | } | |
1861 | ||
1862 | /* puts_debug is like fputs_unfiltered, except it prints special | |
1863 | characters in printable fashion. */ | |
1864 | ||
1865 | void | |
1866 | puts_debug (char *prefix, char *string, char *suffix) | |
1867 | { | |
1868 | int ch; | |
1869 | ||
1870 | /* Print prefix and suffix after each line. */ | |
1871 | static int new_line = 1; | |
1872 | static int return_p = 0; | |
1873 | static char *prev_prefix = ""; | |
1874 | static char *prev_suffix = ""; | |
1875 | ||
1876 | if (*string == '\n') | |
1877 | return_p = 0; | |
1878 | ||
1879 | /* If the prefix is changing, print the previous suffix, a new line, | |
1880 | and the new prefix. */ | |
1881 | if ((return_p || (strcmp (prev_prefix, prefix) != 0)) && !new_line) | |
1882 | { | |
1883 | fputs_unfiltered (prev_suffix, gdb_stdlog); | |
1884 | fputs_unfiltered ("\n", gdb_stdlog); | |
1885 | fputs_unfiltered (prefix, gdb_stdlog); | |
1886 | } | |
1887 | ||
1888 | /* Print prefix if we printed a newline during the previous call. */ | |
1889 | if (new_line) | |
1890 | { | |
1891 | new_line = 0; | |
1892 | fputs_unfiltered (prefix, gdb_stdlog); | |
1893 | } | |
1894 | ||
1895 | prev_prefix = prefix; | |
1896 | prev_suffix = suffix; | |
1897 | ||
1898 | /* Output characters in a printable format. */ | |
1899 | while ((ch = *string++) != '\0') | |
1900 | { | |
1901 | switch (ch) | |
1902 | { | |
1903 | default: | |
1904 | if (isprint (ch)) | |
1905 | fputc_unfiltered (ch, gdb_stdlog); | |
1906 | ||
1907 | else | |
1908 | fprintf_unfiltered (gdb_stdlog, "\\x%02x", ch & 0xff); | |
1909 | break; | |
1910 | ||
1911 | case '\\': | |
1912 | fputs_unfiltered ("\\\\", gdb_stdlog); | |
1913 | break; | |
1914 | case '\b': | |
1915 | fputs_unfiltered ("\\b", gdb_stdlog); | |
1916 | break; | |
1917 | case '\f': | |
1918 | fputs_unfiltered ("\\f", gdb_stdlog); | |
1919 | break; | |
1920 | case '\n': | |
1921 | new_line = 1; | |
1922 | fputs_unfiltered ("\\n", gdb_stdlog); | |
1923 | break; | |
1924 | case '\r': | |
1925 | fputs_unfiltered ("\\r", gdb_stdlog); | |
1926 | break; | |
1927 | case '\t': | |
1928 | fputs_unfiltered ("\\t", gdb_stdlog); | |
1929 | break; | |
1930 | case '\v': | |
1931 | fputs_unfiltered ("\\v", gdb_stdlog); | |
1932 | break; | |
1933 | } | |
1934 | ||
1935 | return_p = ch == '\r'; | |
1936 | } | |
1937 | ||
1938 | /* Print suffix if we printed a newline. */ | |
1939 | if (new_line) | |
1940 | { | |
1941 | fputs_unfiltered (suffix, gdb_stdlog); | |
1942 | fputs_unfiltered ("\n", gdb_stdlog); | |
1943 | } | |
1944 | } | |
1945 | ||
1946 | ||
1947 | /* Print a variable number of ARGS using format FORMAT. If this | |
1948 | information is going to put the amount written (since the last call | |
1949 | to REINITIALIZE_MORE_FILTER or the last page break) over the page size, | |
1950 | call prompt_for_continue to get the users permision to continue. | |
1951 | ||
1952 | Unlike fprintf, this function does not return a value. | |
1953 | ||
1954 | We implement three variants, vfprintf (takes a vararg list and stream), | |
1955 | fprintf (takes a stream to write on), and printf (the usual). | |
1956 | ||
1957 | Note also that a longjmp to top level may occur in this routine | |
1958 | (since prompt_for_continue may do so) so this routine should not be | |
1959 | called when cleanups are not in place. */ | |
1960 | ||
1961 | static void | |
1962 | vfprintf_maybe_filtered (struct ui_file *stream, const char *format, | |
1963 | va_list args, int filter) | |
1964 | { | |
1965 | char *linebuffer; | |
1966 | struct cleanup *old_cleanups; | |
1967 | ||
1968 | xvasprintf (&linebuffer, format, args); | |
1969 | old_cleanups = make_cleanup (xfree, linebuffer); | |
1970 | fputs_maybe_filtered (linebuffer, stream, filter); | |
1971 | do_cleanups (old_cleanups); | |
1972 | } | |
1973 | ||
1974 | ||
1975 | void | |
1976 | vfprintf_filtered (struct ui_file *stream, const char *format, va_list args) | |
1977 | { | |
1978 | vfprintf_maybe_filtered (stream, format, args, 1); | |
1979 | } | |
1980 | ||
1981 | void | |
1982 | vfprintf_unfiltered (struct ui_file *stream, const char *format, va_list args) | |
1983 | { | |
1984 | char *linebuffer; | |
1985 | struct cleanup *old_cleanups; | |
1986 | ||
1987 | xvasprintf (&linebuffer, format, args); | |
1988 | old_cleanups = make_cleanup (xfree, linebuffer); | |
1989 | fputs_unfiltered (linebuffer, stream); | |
1990 | do_cleanups (old_cleanups); | |
1991 | } | |
1992 | ||
1993 | void | |
1994 | vprintf_filtered (const char *format, va_list args) | |
1995 | { | |
1996 | vfprintf_maybe_filtered (gdb_stdout, format, args, 1); | |
1997 | } | |
1998 | ||
1999 | void | |
2000 | vprintf_unfiltered (const char *format, va_list args) | |
2001 | { | |
2002 | vfprintf_unfiltered (gdb_stdout, format, args); | |
2003 | } | |
2004 | ||
2005 | void | |
2006 | fprintf_filtered (struct ui_file * stream, const char *format,...) | |
2007 | { | |
2008 | va_list args; | |
2009 | va_start (args, format); | |
2010 | vfprintf_filtered (stream, format, args); | |
2011 | va_end (args); | |
2012 | } | |
2013 | ||
2014 | void | |
2015 | fprintf_unfiltered (struct ui_file * stream, const char *format,...) | |
2016 | { | |
2017 | va_list args; | |
2018 | va_start (args, format); | |
2019 | vfprintf_unfiltered (stream, format, args); | |
2020 | va_end (args); | |
2021 | } | |
2022 | ||
2023 | /* Like fprintf_filtered, but prints its result indented. | |
2024 | Called as fprintfi_filtered (spaces, stream, format, ...); */ | |
2025 | ||
2026 | void | |
2027 | fprintfi_filtered (int spaces, struct ui_file * stream, const char *format,...) | |
2028 | { | |
2029 | va_list args; | |
2030 | va_start (args, format); | |
2031 | print_spaces_filtered (spaces, stream); | |
2032 | ||
2033 | vfprintf_filtered (stream, format, args); | |
2034 | va_end (args); | |
2035 | } | |
2036 | ||
2037 | ||
2038 | void | |
2039 | printf_filtered (const char *format,...) | |
2040 | { | |
2041 | va_list args; | |
2042 | va_start (args, format); | |
2043 | vfprintf_filtered (gdb_stdout, format, args); | |
2044 | va_end (args); | |
2045 | } | |
2046 | ||
2047 | ||
2048 | void | |
2049 | printf_unfiltered (const char *format,...) | |
2050 | { | |
2051 | va_list args; | |
2052 | va_start (args, format); | |
2053 | vfprintf_unfiltered (gdb_stdout, format, args); | |
2054 | va_end (args); | |
2055 | } | |
2056 | ||
2057 | /* Like printf_filtered, but prints it's result indented. | |
2058 | Called as printfi_filtered (spaces, format, ...); */ | |
2059 | ||
2060 | void | |
2061 | printfi_filtered (int spaces, const char *format,...) | |
2062 | { | |
2063 | va_list args; | |
2064 | va_start (args, format); | |
2065 | print_spaces_filtered (spaces, gdb_stdout); | |
2066 | vfprintf_filtered (gdb_stdout, format, args); | |
2067 | va_end (args); | |
2068 | } | |
2069 | ||
2070 | /* Easy -- but watch out! | |
2071 | ||
2072 | This routine is *not* a replacement for puts()! puts() appends a newline. | |
2073 | This one doesn't, and had better not! */ | |
2074 | ||
2075 | void | |
2076 | puts_filtered (const char *string) | |
2077 | { | |
2078 | fputs_filtered (string, gdb_stdout); | |
2079 | } | |
2080 | ||
2081 | void | |
2082 | puts_unfiltered (const char *string) | |
2083 | { | |
2084 | fputs_unfiltered (string, gdb_stdout); | |
2085 | } | |
2086 | ||
2087 | /* Return a pointer to N spaces and a null. The pointer is good | |
2088 | until the next call to here. */ | |
2089 | char * | |
2090 | n_spaces (int n) | |
2091 | { | |
2092 | char *t; | |
2093 | static char *spaces = 0; | |
2094 | static int max_spaces = -1; | |
2095 | ||
2096 | if (n > max_spaces) | |
2097 | { | |
2098 | if (spaces) | |
2099 | xfree (spaces); | |
2100 | spaces = (char *) xmalloc (n + 1); | |
2101 | for (t = spaces + n; t != spaces;) | |
2102 | *--t = ' '; | |
2103 | spaces[n] = '\0'; | |
2104 | max_spaces = n; | |
2105 | } | |
2106 | ||
2107 | return spaces + max_spaces - n; | |
2108 | } | |
2109 | ||
2110 | /* Print N spaces. */ | |
2111 | void | |
2112 | print_spaces_filtered (int n, struct ui_file *stream) | |
2113 | { | |
2114 | fputs_filtered (n_spaces (n), stream); | |
2115 | } | |
2116 | \f | |
2117 | /* C++ demangler stuff. */ | |
2118 | ||
2119 | /* fprintf_symbol_filtered attempts to demangle NAME, a symbol in language | |
2120 | LANG, using demangling args ARG_MODE, and print it filtered to STREAM. | |
2121 | If the name is not mangled, or the language for the name is unknown, or | |
2122 | demangling is off, the name is printed in its "raw" form. */ | |
2123 | ||
2124 | void | |
2125 | fprintf_symbol_filtered (struct ui_file *stream, char *name, enum language lang, | |
2126 | int arg_mode) | |
2127 | { | |
2128 | char *demangled; | |
2129 | ||
2130 | if (name != NULL) | |
2131 | { | |
2132 | /* If user wants to see raw output, no problem. */ | |
2133 | if (!demangle) | |
2134 | { | |
2135 | fputs_filtered (name, stream); | |
2136 | } | |
2137 | else | |
2138 | { | |
2139 | switch (lang) | |
2140 | { | |
2141 | case language_cplus: | |
2142 | demangled = cplus_demangle (name, arg_mode); | |
2143 | break; | |
2144 | case language_java: | |
2145 | demangled = cplus_demangle (name, arg_mode | DMGL_JAVA); | |
2146 | break; | |
2147 | case language_chill: | |
2148 | demangled = chill_demangle (name); | |
2149 | break; | |
2150 | default: | |
2151 | demangled = NULL; | |
2152 | break; | |
2153 | } | |
2154 | fputs_filtered (demangled ? demangled : name, stream); | |
2155 | if (demangled != NULL) | |
2156 | { | |
2157 | xfree (demangled); | |
2158 | } | |
2159 | } | |
2160 | } | |
2161 | } | |
2162 | ||
2163 | /* Do a strcmp() type operation on STRING1 and STRING2, ignoring any | |
2164 | differences in whitespace. Returns 0 if they match, non-zero if they | |
2165 | don't (slightly different than strcmp()'s range of return values). | |
2166 | ||
2167 | As an extra hack, string1=="FOO(ARGS)" matches string2=="FOO". | |
2168 | This "feature" is useful when searching for matching C++ function names | |
2169 | (such as if the user types 'break FOO', where FOO is a mangled C++ | |
2170 | function). */ | |
2171 | ||
2172 | int | |
2173 | strcmp_iw (const char *string1, const char *string2) | |
2174 | { | |
2175 | while ((*string1 != '\0') && (*string2 != '\0')) | |
2176 | { | |
2177 | while (isspace (*string1)) | |
2178 | { | |
2179 | string1++; | |
2180 | } | |
2181 | while (isspace (*string2)) | |
2182 | { | |
2183 | string2++; | |
2184 | } | |
2185 | if (*string1 != *string2) | |
2186 | { | |
2187 | break; | |
2188 | } | |
2189 | if (*string1 != '\0') | |
2190 | { | |
2191 | string1++; | |
2192 | string2++; | |
2193 | } | |
2194 | } | |
2195 | return (*string1 != '\0' && *string1 != '(') || (*string2 != '\0'); | |
2196 | } | |
2197 | \f | |
2198 | ||
2199 | /* | |
2200 | ** subset_compare() | |
2201 | ** Answer whether string_to_compare is a full or partial match to | |
2202 | ** template_string. The partial match must be in sequence starting | |
2203 | ** at index 0. | |
2204 | */ | |
2205 | int | |
2206 | subset_compare (char *string_to_compare, char *template_string) | |
2207 | { | |
2208 | int match; | |
2209 | if (template_string != (char *) NULL && string_to_compare != (char *) NULL && | |
2210 | strlen (string_to_compare) <= strlen (template_string)) | |
2211 | match = (strncmp (template_string, | |
2212 | string_to_compare, | |
2213 | strlen (string_to_compare)) == 0); | |
2214 | else | |
2215 | match = 0; | |
2216 | return match; | |
2217 | } | |
2218 | ||
2219 | ||
2220 | static void pagination_on_command (char *arg, int from_tty); | |
2221 | static void | |
2222 | pagination_on_command (char *arg, int from_tty) | |
2223 | { | |
2224 | pagination_enabled = 1; | |
2225 | } | |
2226 | ||
2227 | static void pagination_on_command (char *arg, int from_tty); | |
2228 | static void | |
2229 | pagination_off_command (char *arg, int from_tty) | |
2230 | { | |
2231 | pagination_enabled = 0; | |
2232 | } | |
2233 | \f | |
2234 | ||
2235 | void | |
2236 | initialize_utils (void) | |
2237 | { | |
2238 | struct cmd_list_element *c; | |
2239 | ||
2240 | c = add_set_cmd ("width", class_support, var_uinteger, | |
2241 | (char *) &chars_per_line, | |
2242 | "Set number of characters gdb thinks are in a line.", | |
2243 | &setlist); | |
2244 | add_show_from_set (c, &showlist); | |
2245 | set_cmd_sfunc (c, set_width_command); | |
2246 | ||
2247 | add_show_from_set | |
2248 | (add_set_cmd ("height", class_support, | |
2249 | var_uinteger, (char *) &lines_per_page, | |
2250 | "Set number of lines gdb thinks are in a page.", &setlist), | |
2251 | &showlist); | |
2252 | ||
2253 | init_page_info (); | |
2254 | ||
2255 | /* If the output is not a terminal, don't paginate it. */ | |
2256 | if (!ui_file_isatty (gdb_stdout)) | |
2257 | lines_per_page = UINT_MAX; | |
2258 | ||
2259 | set_width_command ((char *) NULL, 0, c); | |
2260 | ||
2261 | add_show_from_set | |
2262 | (add_set_cmd ("demangle", class_support, var_boolean, | |
2263 | (char *) &demangle, | |
2264 | "Set demangling of encoded C++ names when displaying symbols.", | |
2265 | &setprintlist), | |
2266 | &showprintlist); | |
2267 | ||
2268 | add_show_from_set | |
2269 | (add_set_cmd ("pagination", class_support, | |
2270 | var_boolean, (char *) &pagination_enabled, | |
2271 | "Set state of pagination.", &setlist), | |
2272 | &showlist); | |
2273 | ||
2274 | if (xdb_commands) | |
2275 | { | |
2276 | add_com ("am", class_support, pagination_on_command, | |
2277 | "Enable pagination"); | |
2278 | add_com ("sm", class_support, pagination_off_command, | |
2279 | "Disable pagination"); | |
2280 | } | |
2281 | ||
2282 | add_show_from_set | |
2283 | (add_set_cmd ("sevenbit-strings", class_support, var_boolean, | |
2284 | (char *) &sevenbit_strings, | |
2285 | "Set printing of 8-bit characters in strings as \\nnn.", | |
2286 | &setprintlist), | |
2287 | &showprintlist); | |
2288 | ||
2289 | add_show_from_set | |
2290 | (add_set_cmd ("asm-demangle", class_support, var_boolean, | |
2291 | (char *) &asm_demangle, | |
2292 | "Set demangling of C++ names in disassembly listings.", | |
2293 | &setprintlist), | |
2294 | &showprintlist); | |
2295 | } | |
2296 | ||
2297 | /* Machine specific function to handle SIGWINCH signal. */ | |
2298 | ||
2299 | #ifdef SIGWINCH_HANDLER_BODY | |
2300 | SIGWINCH_HANDLER_BODY | |
2301 | #endif | |
2302 | ||
2303 | /* print routines to handle variable size regs, etc. */ | |
2304 | ||
2305 | /* temporary storage using circular buffer */ | |
2306 | #define NUMCELLS 16 | |
2307 | #define CELLSIZE 32 | |
2308 | static char * | |
2309 | get_cell (void) | |
2310 | { | |
2311 | static char buf[NUMCELLS][CELLSIZE]; | |
2312 | static int cell = 0; | |
2313 | if (++cell >= NUMCELLS) | |
2314 | cell = 0; | |
2315 | return buf[cell]; | |
2316 | } | |
2317 | ||
2318 | int | |
2319 | strlen_paddr (void) | |
2320 | { | |
2321 | return (TARGET_ADDR_BIT / 8 * 2); | |
2322 | } | |
2323 | ||
2324 | char * | |
2325 | paddr (CORE_ADDR addr) | |
2326 | { | |
2327 | return phex (addr, TARGET_ADDR_BIT / 8); | |
2328 | } | |
2329 | ||
2330 | char * | |
2331 | paddr_nz (CORE_ADDR addr) | |
2332 | { | |
2333 | return phex_nz (addr, TARGET_ADDR_BIT / 8); | |
2334 | } | |
2335 | ||
2336 | static void | |
2337 | decimal2str (char *paddr_str, char *sign, ULONGEST addr) | |
2338 | { | |
2339 | /* steal code from valprint.c:print_decimal(). Should this worry | |
2340 | about the real size of addr as the above does? */ | |
2341 | unsigned long temp[3]; | |
2342 | int i = 0; | |
2343 | do | |
2344 | { | |
2345 | temp[i] = addr % (1000 * 1000 * 1000); | |
2346 | addr /= (1000 * 1000 * 1000); | |
2347 | i++; | |
2348 | } | |
2349 | while (addr != 0 && i < (sizeof (temp) / sizeof (temp[0]))); | |
2350 | switch (i) | |
2351 | { | |
2352 | case 1: | |
2353 | sprintf (paddr_str, "%s%lu", | |
2354 | sign, temp[0]); | |
2355 | break; | |
2356 | case 2: | |
2357 | sprintf (paddr_str, "%s%lu%09lu", | |
2358 | sign, temp[1], temp[0]); | |
2359 | break; | |
2360 | case 3: | |
2361 | sprintf (paddr_str, "%s%lu%09lu%09lu", | |
2362 | sign, temp[2], temp[1], temp[0]); | |
2363 | break; | |
2364 | default: | |
2365 | internal_error (__FILE__, __LINE__, "failed internal consistency check"); | |
2366 | } | |
2367 | } | |
2368 | ||
2369 | char * | |
2370 | paddr_u (CORE_ADDR addr) | |
2371 | { | |
2372 | char *paddr_str = get_cell (); | |
2373 | decimal2str (paddr_str, "", addr); | |
2374 | return paddr_str; | |
2375 | } | |
2376 | ||
2377 | char * | |
2378 | paddr_d (LONGEST addr) | |
2379 | { | |
2380 | char *paddr_str = get_cell (); | |
2381 | if (addr < 0) | |
2382 | decimal2str (paddr_str, "-", -addr); | |
2383 | else | |
2384 | decimal2str (paddr_str, "", addr); | |
2385 | return paddr_str; | |
2386 | } | |
2387 | ||
2388 | /* eliminate warning from compiler on 32-bit systems */ | |
2389 | static int thirty_two = 32; | |
2390 | ||
2391 | char * | |
2392 | phex (ULONGEST l, int sizeof_l) | |
2393 | { | |
2394 | char *str; | |
2395 | switch (sizeof_l) | |
2396 | { | |
2397 | case 8: | |
2398 | str = get_cell (); | |
2399 | sprintf (str, "%08lx%08lx", | |
2400 | (unsigned long) (l >> thirty_two), | |
2401 | (unsigned long) (l & 0xffffffff)); | |
2402 | break; | |
2403 | case 4: | |
2404 | str = get_cell (); | |
2405 | sprintf (str, "%08lx", (unsigned long) l); | |
2406 | break; | |
2407 | case 2: | |
2408 | str = get_cell (); | |
2409 | sprintf (str, "%04x", (unsigned short) (l & 0xffff)); | |
2410 | break; | |
2411 | default: | |
2412 | str = phex (l, sizeof (l)); | |
2413 | break; | |
2414 | } | |
2415 | return str; | |
2416 | } | |
2417 | ||
2418 | char * | |
2419 | phex_nz (ULONGEST l, int sizeof_l) | |
2420 | { | |
2421 | char *str; | |
2422 | switch (sizeof_l) | |
2423 | { | |
2424 | case 8: | |
2425 | { | |
2426 | unsigned long high = (unsigned long) (l >> thirty_two); | |
2427 | str = get_cell (); | |
2428 | if (high == 0) | |
2429 | sprintf (str, "%lx", (unsigned long) (l & 0xffffffff)); | |
2430 | else | |
2431 | sprintf (str, "%lx%08lx", | |
2432 | high, (unsigned long) (l & 0xffffffff)); | |
2433 | break; | |
2434 | } | |
2435 | case 4: | |
2436 | str = get_cell (); | |
2437 | sprintf (str, "%lx", (unsigned long) l); | |
2438 | break; | |
2439 | case 2: | |
2440 | str = get_cell (); | |
2441 | sprintf (str, "%x", (unsigned short) (l & 0xffff)); | |
2442 | break; | |
2443 | default: | |
2444 | str = phex_nz (l, sizeof (l)); | |
2445 | break; | |
2446 | } | |
2447 | return str; | |
2448 | } | |
2449 | ||
2450 | ||
2451 | /* Convert to / from the hosts pointer to GDB's internal CORE_ADDR | |
2452 | using the target's conversion routines. */ | |
2453 | CORE_ADDR | |
2454 | host_pointer_to_address (void *ptr) | |
2455 | { | |
2456 | if (sizeof (ptr) != TYPE_LENGTH (builtin_type_void_data_ptr)) | |
2457 | internal_error (__FILE__, __LINE__, | |
2458 | "core_addr_to_void_ptr: bad cast"); | |
2459 | return POINTER_TO_ADDRESS (builtin_type_void_data_ptr, &ptr); | |
2460 | } | |
2461 | ||
2462 | void * | |
2463 | address_to_host_pointer (CORE_ADDR addr) | |
2464 | { | |
2465 | void *ptr; | |
2466 | if (sizeof (ptr) != TYPE_LENGTH (builtin_type_void_data_ptr)) | |
2467 | internal_error (__FILE__, __LINE__, | |
2468 | "core_addr_to_void_ptr: bad cast"); | |
2469 | ADDRESS_TO_POINTER (builtin_type_void_data_ptr, &ptr, addr); | |
2470 | return ptr; | |
2471 | } | |
2472 | ||
2473 | /* Convert a CORE_ADDR into a string. */ | |
2474 | const char * | |
2475 | core_addr_to_string (const CORE_ADDR addr) | |
2476 | { | |
2477 | char *str = get_cell (); | |
2478 | strcpy (str, "0x"); | |
2479 | strcat (str, phex_nz (addr, sizeof (addr))); | |
2480 | return str; | |
2481 | } | |
2482 | ||
2483 | /* Convert a string back into a CORE_ADDR. */ | |
2484 | CORE_ADDR | |
2485 | string_to_core_addr (const char *my_string) | |
2486 | { | |
2487 | CORE_ADDR addr = 0; | |
2488 | if (my_string[0] == '0' && tolower (my_string[1]) == 'x') | |
2489 | { | |
2490 | /* Assume that it is in decimal. */ | |
2491 | int i; | |
2492 | for (i = 2; my_string[i] != '\0'; i++) | |
2493 | { | |
2494 | if (isdigit (my_string[i])) | |
2495 | addr = (my_string[i] - '0') + (addr * 16); | |
2496 | else if (isxdigit (my_string[i])) | |
2497 | addr = (tolower (my_string[i]) - 'a' + 0xa) + (addr * 16); | |
2498 | else | |
2499 | internal_error (__FILE__, __LINE__, "invalid hex"); | |
2500 | } | |
2501 | } | |
2502 | else | |
2503 | { | |
2504 | /* Assume that it is in decimal. */ | |
2505 | int i; | |
2506 | for (i = 0; my_string[i] != '\0'; i++) | |
2507 | { | |
2508 | if (isdigit (my_string[i])) | |
2509 | addr = (my_string[i] - '0') + (addr * 10); | |
2510 | else | |
2511 | internal_error (__FILE__, __LINE__, "invalid decimal"); | |
2512 | } | |
2513 | } | |
2514 | return addr; | |
2515 | } | |
2516 | ||
2517 | char * | |
2518 | gdb_realpath (const char *filename) | |
2519 | { | |
2520 | #ifdef HAVE_REALPATH | |
2521 | #if defined (PATH_MAX) | |
2522 | char buf[PATH_MAX]; | |
2523 | #elif defined (MAXPATHLEN) | |
2524 | char buf[MAXPATHLEN]; | |
2525 | #else | |
2526 | #error "Neither PATH_MAX nor MAXPATHLEN defined" | |
2527 | #endif | |
2528 | char *rp = realpath (filename, buf); | |
2529 | return xstrdup (rp ? rp : filename); | |
2530 | #else | |
2531 | return xstrdup (filename); | |
2532 | #endif | |
2533 | } |