Commit | Line | Data |
---|---|---|
bd5635a1 | 1 | /* General utility routines for GDB, the GNU debugger. |
7919c3ed | 2 | Copyright 1986, 1989, 1990, 1991, 1992 Free Software Foundation, Inc. |
bd5635a1 RP |
3 | |
4 | This file is part of GDB. | |
5 | ||
351b221d | 6 | This program is free software; you can redistribute it and/or modify |
bd5635a1 | 7 | it under the terms of the GNU General Public License as published by |
351b221d JG |
8 | the Free Software Foundation; either version 2 of the License, or |
9 | (at your option) any later version. | |
bd5635a1 | 10 | |
351b221d | 11 | This program is distributed in the hope that it will be useful, |
bd5635a1 RP |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
351b221d JG |
17 | along with this program; if not, write to the Free Software |
18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
bd5635a1 | 19 | |
d747e0af | 20 | #include "defs.h" |
51b57ded | 21 | #if !defined(__GO32__) |
bd5635a1 RP |
22 | #include <sys/ioctl.h> |
23 | #include <sys/param.h> | |
24 | #include <pwd.h> | |
51b57ded | 25 | #endif |
2bc2e684 FF |
26 | #include <varargs.h> |
27 | #include <ctype.h> | |
28 | #include <string.h> | |
29 | ||
bd5635a1 RP |
30 | #include "signals.h" |
31 | #include "gdbcmd.h" | |
159dd2aa | 32 | #include "serial.h" |
199b2450 | 33 | #include "terminal.h" /* For job_control */ |
bd5635a1 RP |
34 | #include "bfd.h" |
35 | #include "target.h" | |
bcf2e6ab | 36 | #include "demangle.h" |
bd5d07d9 FF |
37 | #include "expression.h" |
38 | #include "language.h" | |
bd5635a1 | 39 | |
d8742f46 JK |
40 | #include "readline.h" |
41 | ||
42 | /* readline defines this. */ | |
43 | #undef savestring | |
44 | ||
7919c3ed JG |
45 | /* Prototypes for local functions */ |
46 | ||
65ce5df4 JG |
47 | #if defined (NO_MMALLOC) || defined (NO_MMALLOC_CHECK) |
48 | #else | |
3624c875 | 49 | |
7919c3ed JG |
50 | static void |
51 | malloc_botch PARAMS ((void)); | |
3624c875 | 52 | |
65ce5df4 | 53 | #endif /* NO_MMALLOC, etc */ |
7919c3ed JG |
54 | |
55 | static void | |
56 | fatal_dump_core (); /* Can't prototype with <varargs.h> usage... */ | |
57 | ||
58 | static void | |
59 | prompt_for_continue PARAMS ((void)); | |
60 | ||
61 | static void | |
62 | set_width_command PARAMS ((char *, int, struct cmd_list_element *)); | |
63 | ||
bd5635a1 RP |
64 | /* If this definition isn't overridden by the header files, assume |
65 | that isatty and fileno exist on this system. */ | |
66 | #ifndef ISATTY | |
67 | #define ISATTY(FP) (isatty (fileno (FP))) | |
68 | #endif | |
69 | ||
bd5635a1 RP |
70 | /* Chain of cleanup actions established with make_cleanup, |
71 | to be executed if an error happens. */ | |
72 | ||
73 | static struct cleanup *cleanup_chain; | |
74 | ||
75 | /* Nonzero means a quit has been requested. */ | |
76 | ||
77 | int quit_flag; | |
78 | ||
159dd2aa JK |
79 | /* Nonzero means quit immediately if Control-C is typed now, rather |
80 | than waiting until QUIT is executed. Be careful in setting this; | |
81 | code which executes with immediate_quit set has to be very careful | |
82 | about being able to deal with being interrupted at any time. It is | |
83 | almost always better to use QUIT; the only exception I can think of | |
84 | is being able to quit out of a system call (using EINTR loses if | |
85 | the SIGINT happens between the previous QUIT and the system call). | |
86 | To immediately quit in the case in which a SIGINT happens between | |
87 | the previous QUIT and setting immediate_quit (desirable anytime we | |
88 | expect to block), call QUIT after setting immediate_quit. */ | |
bd5635a1 RP |
89 | |
90 | int immediate_quit; | |
91 | ||
92 | /* Nonzero means that encoded C++ names should be printed out in their | |
93 | C++ form rather than raw. */ | |
94 | ||
95 | int demangle = 1; | |
96 | ||
97 | /* Nonzero means that encoded C++ names should be printed out in their | |
98 | C++ form even in assembler language displays. If this is set, but | |
99 | DEMANGLE is zero, names are printed raw, i.e. DEMANGLE controls. */ | |
100 | ||
101 | int asm_demangle = 0; | |
102 | ||
103 | /* Nonzero means that strings with character values >0x7F should be printed | |
104 | as octal escapes. Zero means just print the value (e.g. it's an | |
105 | international character, and the terminal or window can cope.) */ | |
106 | ||
107 | int sevenbit_strings = 0; | |
81066208 JG |
108 | |
109 | /* String to be printed before error messages, if any. */ | |
110 | ||
111 | char *error_pre_print; | |
3624c875 | 112 | char *warning_pre_print = "\nwarning: "; |
bd5635a1 RP |
113 | \f |
114 | /* Add a new cleanup to the cleanup_chain, | |
115 | and return the previous chain pointer | |
116 | to be passed later to do_cleanups or discard_cleanups. | |
117 | Args are FUNCTION to clean up with, and ARG to pass to it. */ | |
118 | ||
119 | struct cleanup * | |
120 | make_cleanup (function, arg) | |
7919c3ed JG |
121 | void (*function) PARAMS ((PTR)); |
122 | PTR arg; | |
bd5635a1 RP |
123 | { |
124 | register struct cleanup *new | |
125 | = (struct cleanup *) xmalloc (sizeof (struct cleanup)); | |
126 | register struct cleanup *old_chain = cleanup_chain; | |
127 | ||
128 | new->next = cleanup_chain; | |
129 | new->function = function; | |
130 | new->arg = arg; | |
131 | cleanup_chain = new; | |
132 | ||
133 | return old_chain; | |
134 | } | |
135 | ||
136 | /* Discard cleanups and do the actions they describe | |
137 | until we get back to the point OLD_CHAIN in the cleanup_chain. */ | |
138 | ||
139 | void | |
140 | do_cleanups (old_chain) | |
141 | register struct cleanup *old_chain; | |
142 | { | |
143 | register struct cleanup *ptr; | |
144 | while ((ptr = cleanup_chain) != old_chain) | |
145 | { | |
5e5215eb | 146 | cleanup_chain = ptr->next; /* Do this first incase recursion */ |
bd5635a1 | 147 | (*ptr->function) (ptr->arg); |
bd5635a1 RP |
148 | free (ptr); |
149 | } | |
150 | } | |
151 | ||
152 | /* Discard cleanups, not doing the actions they describe, | |
153 | until we get back to the point OLD_CHAIN in the cleanup_chain. */ | |
154 | ||
155 | void | |
156 | discard_cleanups (old_chain) | |
157 | register struct cleanup *old_chain; | |
158 | { | |
159 | register struct cleanup *ptr; | |
160 | while ((ptr = cleanup_chain) != old_chain) | |
161 | { | |
162 | cleanup_chain = ptr->next; | |
be772100 | 163 | free ((PTR)ptr); |
bd5635a1 RP |
164 | } |
165 | } | |
166 | ||
167 | /* Set the cleanup_chain to 0, and return the old cleanup chain. */ | |
168 | struct cleanup * | |
169 | save_cleanups () | |
170 | { | |
171 | struct cleanup *old_chain = cleanup_chain; | |
172 | ||
173 | cleanup_chain = 0; | |
174 | return old_chain; | |
175 | } | |
176 | ||
177 | /* Restore the cleanup chain from a previously saved chain. */ | |
178 | void | |
179 | restore_cleanups (chain) | |
180 | struct cleanup *chain; | |
181 | { | |
182 | cleanup_chain = chain; | |
183 | } | |
184 | ||
185 | /* This function is useful for cleanups. | |
186 | Do | |
187 | ||
188 | foo = xmalloc (...); | |
189 | old_chain = make_cleanup (free_current_contents, &foo); | |
190 | ||
191 | to arrange to free the object thus allocated. */ | |
192 | ||
193 | void | |
194 | free_current_contents (location) | |
195 | char **location; | |
196 | { | |
197 | free (*location); | |
198 | } | |
088c3a0b JG |
199 | |
200 | /* Provide a known function that does nothing, to use as a base for | |
201 | for a possibly long chain of cleanups. This is useful where we | |
202 | use the cleanup chain for handling normal cleanups as well as dealing | |
203 | with cleanups that need to be done as a result of a call to error(). | |
204 | In such cases, we may not be certain where the first cleanup is, unless | |
205 | we have a do-nothing one to always use as the base. */ | |
206 | ||
207 | /* ARGSUSED */ | |
208 | void | |
209 | null_cleanup (arg) | |
210 | char **arg; | |
211 | { | |
212 | } | |
213 | ||
bd5635a1 | 214 | \f |
2bc2e684 FF |
215 | /* Provide a hook for modules wishing to print their own warning messages |
216 | to set up the terminal state in a compatible way, without them having | |
217 | to import all the target_<...> macros. */ | |
218 | ||
219 | void | |
220 | warning_setup () | |
221 | { | |
222 | target_terminal_ours (); | |
223 | wrap_here(""); /* Force out any buffered output */ | |
199b2450 | 224 | gdb_flush (gdb_stdout); |
2bc2e684 FF |
225 | } |
226 | ||
227 | /* Print a warning message. | |
228 | The first argument STRING is the warning message, used as a fprintf string, | |
229 | and the remaining args are passed as arguments to it. | |
230 | The primary difference between warnings and errors is that a warning | |
231 | does not force the return to command level. */ | |
232 | ||
233 | /* VARARGS */ | |
234 | void | |
235 | warning (va_alist) | |
236 | va_dcl | |
237 | { | |
238 | va_list args; | |
239 | char *string; | |
240 | ||
241 | va_start (args); | |
242 | target_terminal_ours (); | |
243 | wrap_here(""); /* Force out any buffered output */ | |
199b2450 | 244 | gdb_flush (gdb_stdout); |
2bc2e684 | 245 | if (warning_pre_print) |
199b2450 | 246 | fprintf_unfiltered (gdb_stderr, warning_pre_print); |
2bc2e684 | 247 | string = va_arg (args, char *); |
199b2450 TL |
248 | vfprintf_unfiltered (gdb_stderr, string, args); |
249 | fprintf_unfiltered (gdb_stderr, "\n"); | |
2bc2e684 FF |
250 | va_end (args); |
251 | } | |
252 | ||
a0cf4681 JK |
253 | /* Start the printing of an error message. Way to use this is to call |
254 | this, output the error message, and then call | |
255 | return_to_top_level (RETURN_ERROR). error() provides a convenient way to | |
256 | do this for the special case that the error message can be formatted with | |
257 | a single printf call, but this is more general. */ | |
258 | void | |
259 | error_begin () | |
260 | { | |
261 | target_terminal_ours (); | |
262 | wrap_here (""); /* Force out any buffered output */ | |
263 | gdb_flush (gdb_stdout); | |
264 | ||
265 | if (annotation_level > 1) | |
266 | fprintf_filtered (gdb_stderr, "\n\032\032error-begin\n"); | |
267 | ||
268 | if (error_pre_print) | |
269 | fprintf_filtered (gdb_stderr, error_pre_print); | |
270 | } | |
271 | ||
bd5635a1 RP |
272 | /* Print an error message and return to command level. |
273 | The first argument STRING is the error message, used as a fprintf string, | |
274 | and the remaining args are passed as arguments to it. */ | |
275 | ||
276 | /* VARARGS */ | |
7919c3ed | 277 | NORETURN void |
bd5635a1 RP |
278 | error (va_alist) |
279 | va_dcl | |
280 | { | |
281 | va_list args; | |
282 | char *string; | |
283 | ||
a0cf4681 | 284 | error_begin (); |
bd5635a1 | 285 | va_start (args); |
bd5635a1 | 286 | string = va_arg (args, char *); |
199b2450 TL |
287 | vfprintf_filtered (gdb_stderr, string, args); |
288 | fprintf_filtered (gdb_stderr, "\n"); | |
bd5635a1 | 289 | va_end (args); |
159dd2aa | 290 | return_to_top_level (RETURN_ERROR); |
bd5635a1 RP |
291 | } |
292 | ||
293 | /* Print an error message and exit reporting failure. | |
294 | This is for a error that we cannot continue from. | |
7919c3ed JG |
295 | The arguments are printed a la printf. |
296 | ||
297 | This function cannot be declared volatile (NORETURN) in an | |
298 | ANSI environment because exit() is not declared volatile. */ | |
bd5635a1 RP |
299 | |
300 | /* VARARGS */ | |
7919c3ed | 301 | NORETURN void |
bd5635a1 RP |
302 | fatal (va_alist) |
303 | va_dcl | |
304 | { | |
305 | va_list args; | |
306 | char *string; | |
307 | ||
308 | va_start (args); | |
309 | string = va_arg (args, char *); | |
199b2450 TL |
310 | fprintf_unfiltered (gdb_stderr, "\ngdb: "); |
311 | vfprintf_unfiltered (gdb_stderr, string, args); | |
312 | fprintf_unfiltered (gdb_stderr, "\n"); | |
bd5635a1 RP |
313 | va_end (args); |
314 | exit (1); | |
315 | } | |
316 | ||
317 | /* Print an error message and exit, dumping core. | |
318 | The arguments are printed a la printf (). */ | |
7919c3ed | 319 | |
bd5635a1 | 320 | /* VARARGS */ |
7919c3ed | 321 | static void |
bd5635a1 RP |
322 | fatal_dump_core (va_alist) |
323 | va_dcl | |
324 | { | |
325 | va_list args; | |
326 | char *string; | |
327 | ||
328 | va_start (args); | |
329 | string = va_arg (args, char *); | |
330 | /* "internal error" is always correct, since GDB should never dump | |
331 | core, no matter what the input. */ | |
199b2450 TL |
332 | fprintf_unfiltered (gdb_stderr, "\ngdb internal error: "); |
333 | vfprintf_unfiltered (gdb_stderr, string, args); | |
334 | fprintf_unfiltered (gdb_stderr, "\n"); | |
bd5635a1 RP |
335 | va_end (args); |
336 | ||
337 | signal (SIGQUIT, SIG_DFL); | |
338 | kill (getpid (), SIGQUIT); | |
339 | /* We should never get here, but just in case... */ | |
340 | exit (1); | |
341 | } | |
7919c3ed | 342 | |
4ace50a5 FF |
343 | /* The strerror() function can return NULL for errno values that are |
344 | out of range. Provide a "safe" version that always returns a | |
345 | printable string. */ | |
346 | ||
347 | char * | |
348 | safe_strerror (errnum) | |
349 | int errnum; | |
350 | { | |
351 | char *msg; | |
352 | static char buf[32]; | |
353 | ||
354 | if ((msg = strerror (errnum)) == NULL) | |
355 | { | |
356 | sprintf (buf, "(undocumented errno %d)", errnum); | |
357 | msg = buf; | |
358 | } | |
359 | return (msg); | |
360 | } | |
361 | ||
362 | /* The strsignal() function can return NULL for signal values that are | |
363 | out of range. Provide a "safe" version that always returns a | |
364 | printable string. */ | |
365 | ||
366 | char * | |
367 | safe_strsignal (signo) | |
368 | int signo; | |
369 | { | |
370 | char *msg; | |
371 | static char buf[32]; | |
372 | ||
373 | if ((msg = strsignal (signo)) == NULL) | |
374 | { | |
375 | sprintf (buf, "(undocumented signal %d)", signo); | |
376 | msg = buf; | |
377 | } | |
378 | return (msg); | |
379 | } | |
380 | ||
381 | ||
bd5635a1 RP |
382 | /* Print the system error message for errno, and also mention STRING |
383 | as the file name for which the error was encountered. | |
384 | Then return to command level. */ | |
385 | ||
386 | void | |
387 | perror_with_name (string) | |
388 | char *string; | |
389 | { | |
bd5635a1 RP |
390 | char *err; |
391 | char *combined; | |
392 | ||
4ace50a5 | 393 | err = safe_strerror (errno); |
bd5635a1 RP |
394 | combined = (char *) alloca (strlen (err) + strlen (string) + 3); |
395 | strcpy (combined, string); | |
396 | strcat (combined, ": "); | |
397 | strcat (combined, err); | |
398 | ||
399 | /* I understand setting these is a matter of taste. Still, some people | |
400 | may clear errno but not know about bfd_error. Doing this here is not | |
401 | unreasonable. */ | |
8eec3310 | 402 | bfd_set_error (bfd_error_no_error); |
bd5635a1 RP |
403 | errno = 0; |
404 | ||
405 | error ("%s.", combined); | |
406 | } | |
407 | ||
408 | /* Print the system error message for ERRCODE, and also mention STRING | |
409 | as the file name for which the error was encountered. */ | |
410 | ||
411 | void | |
412 | print_sys_errmsg (string, errcode) | |
413 | char *string; | |
414 | int errcode; | |
415 | { | |
bd5635a1 RP |
416 | char *err; |
417 | char *combined; | |
418 | ||
4ace50a5 | 419 | err = safe_strerror (errcode); |
bd5635a1 RP |
420 | combined = (char *) alloca (strlen (err) + strlen (string) + 3); |
421 | strcpy (combined, string); | |
422 | strcat (combined, ": "); | |
423 | strcat (combined, err); | |
424 | ||
44a09a68 JK |
425 | /* We want anything which was printed on stdout to come out first, before |
426 | this message. */ | |
427 | gdb_flush (gdb_stdout); | |
199b2450 | 428 | fprintf_unfiltered (gdb_stderr, "%s.\n", combined); |
bd5635a1 RP |
429 | } |
430 | ||
431 | /* Control C eventually causes this to be called, at a convenient time. */ | |
432 | ||
433 | void | |
434 | quit () | |
435 | { | |
199b2450 | 436 | serial_t gdb_stdout_serial = serial_fdopen (1); |
159dd2aa | 437 | |
bd5635a1 | 438 | target_terminal_ours (); |
159dd2aa | 439 | |
44a09a68 JK |
440 | /* We want all output to appear now, before we print "Quit". We |
441 | have 3 levels of buffering we have to flush (it's possible that | |
442 | some of these should be changed to flush the lower-level ones | |
443 | too): */ | |
444 | ||
445 | /* 1. The _filtered buffer. */ | |
446 | wrap_here ((char *)0); | |
447 | ||
448 | /* 2. The stdio buffer. */ | |
449 | gdb_flush (gdb_stdout); | |
450 | gdb_flush (gdb_stderr); | |
159dd2aa | 451 | |
44a09a68 JK |
452 | /* 3. The system-level buffer. */ |
453 | SERIAL_FLUSH_OUTPUT (gdb_stdout_serial); | |
199b2450 | 454 | SERIAL_UN_FDOPEN (gdb_stdout_serial); |
159dd2aa | 455 | |
a0cf4681 JK |
456 | if (annotation_level > 1) |
457 | fprintf_filtered (gdb_stderr, "\n\032\032error-begin\n"); | |
458 | ||
159dd2aa JK |
459 | /* Don't use *_filtered; we don't want to prompt the user to continue. */ |
460 | if (error_pre_print) | |
199b2450 | 461 | fprintf_unfiltered (gdb_stderr, error_pre_print); |
159dd2aa JK |
462 | |
463 | if (job_control | |
464 | /* If there is no terminal switching for this target, then we can't | |
465 | possibly get screwed by the lack of job control. */ | |
466 | || current_target->to_terminal_ours == NULL) | |
199b2450 | 467 | fprintf_unfiltered (gdb_stderr, "Quit\n"); |
159dd2aa | 468 | else |
199b2450 | 469 | fprintf_unfiltered (gdb_stderr, |
159dd2aa JK |
470 | "Quit (expect signal SIGINT when the program is resumed)\n"); |
471 | return_to_top_level (RETURN_QUIT); | |
bd5635a1 RP |
472 | } |
473 | ||
bd5d07d9 FF |
474 | |
475 | #ifdef __GO32__ | |
476 | ||
477 | /* In the absence of signals, poll keyboard for a quit. | |
478 | Called from #define QUIT pollquit() in xm-go32.h. */ | |
479 | ||
480 | void | |
481 | pollquit() | |
482 | { | |
483 | if (kbhit ()) | |
484 | { | |
485 | int k = getkey (); | |
44a09a68 | 486 | if (k == 1) { |
bd5d07d9 | 487 | quit_flag = 1; |
44a09a68 JK |
488 | quit(); |
489 | } | |
490 | else if (k == 2) { | |
bd5d07d9 | 491 | immediate_quit = 1; |
44a09a68 JK |
492 | quit (); |
493 | } | |
494 | else | |
495 | { | |
496 | /* We just ignore it */ | |
497 | fprintf_unfiltered (gdb_stderr, "CTRL-A to quit, CTRL-B to quit harder\n"); | |
498 | } | |
bd5d07d9 FF |
499 | } |
500 | } | |
501 | ||
bd5d07d9 | 502 | |
44a09a68 JK |
503 | #endif |
504 | #ifdef __GO32__ | |
505 | void notice_quit() | |
506 | { | |
507 | if (kbhit ()) | |
508 | { | |
509 | int k = getkey (); | |
510 | if (k == 1) { | |
511 | quit_flag = 1; | |
512 | } | |
513 | else if (k == 2) | |
514 | { | |
515 | immediate_quit = 1; | |
516 | } | |
517 | else | |
518 | { | |
519 | fprintf_unfiltered (gdb_stderr, "CTRL-A to quit, CTRL-B to quit harder\n"); | |
520 | } | |
521 | } | |
522 | } | |
523 | #else | |
524 | void notice_quit() | |
525 | { | |
526 | /* Done by signals */ | |
527 | } | |
528 | #endif | |
bd5635a1 RP |
529 | /* Control C comes here */ |
530 | ||
531 | void | |
088c3a0b JG |
532 | request_quit (signo) |
533 | int signo; | |
bd5635a1 RP |
534 | { |
535 | quit_flag = 1; | |
536 | ||
44a09a68 JK |
537 | /* Restore the signal handler. Harmless with BSD-style signals, needed |
538 | for System V-style signals. So just always do it, rather than worrying | |
539 | about USG defines and stuff like that. */ | |
088c3a0b | 540 | signal (signo, request_quit); |
bd5635a1 RP |
541 | |
542 | if (immediate_quit) | |
543 | quit (); | |
544 | } | |
3624c875 FF |
545 | |
546 | \f | |
547 | /* Memory management stuff (malloc friends). */ | |
548 | ||
549 | #if defined (NO_MMALLOC) | |
550 | ||
551 | PTR | |
552 | mmalloc (md, size) | |
553 | PTR md; | |
554 | long size; | |
555 | { | |
556 | return (malloc (size)); | |
557 | } | |
558 | ||
559 | PTR | |
560 | mrealloc (md, ptr, size) | |
561 | PTR md; | |
562 | PTR ptr; | |
563 | long size; | |
564 | { | |
4ace50a5 FF |
565 | if (ptr == 0) /* Guard against old realloc's */ |
566 | return malloc (size); | |
567 | else | |
568 | return realloc (ptr, size); | |
3624c875 FF |
569 | } |
570 | ||
571 | void | |
572 | mfree (md, ptr) | |
573 | PTR md; | |
574 | PTR ptr; | |
575 | { | |
576 | free (ptr); | |
577 | } | |
578 | ||
579 | #endif /* NO_MMALLOC */ | |
580 | ||
581 | #if defined (NO_MMALLOC) || defined (NO_MMALLOC_CHECK) | |
582 | ||
583 | void | |
584 | init_malloc (md) | |
585 | PTR md; | |
586 | { | |
587 | } | |
588 | ||
589 | #else /* have mmalloc and want corruption checking */ | |
590 | ||
591 | static void | |
592 | malloc_botch () | |
593 | { | |
594 | fatal_dump_core ("Memory corruption"); | |
595 | } | |
596 | ||
597 | /* Attempt to install hooks in mmalloc/mrealloc/mfree for the heap specified | |
598 | by MD, to detect memory corruption. Note that MD may be NULL to specify | |
599 | the default heap that grows via sbrk. | |
600 | ||
601 | Note that for freshly created regions, we must call mmcheck prior to any | |
602 | mallocs in the region. Otherwise, any region which was allocated prior to | |
603 | installing the checking hooks, which is later reallocated or freed, will | |
604 | fail the checks! The mmcheck function only allows initial hooks to be | |
605 | installed before the first mmalloc. However, anytime after we have called | |
606 | mmcheck the first time to install the checking hooks, we can call it again | |
607 | to update the function pointer to the memory corruption handler. | |
608 | ||
609 | Returns zero on failure, non-zero on success. */ | |
610 | ||
611 | void | |
612 | init_malloc (md) | |
613 | PTR md; | |
614 | { | |
615 | if (!mmcheck (md, malloc_botch)) | |
616 | { | |
617 | warning ("internal error: failed to install memory consistency checks"); | |
618 | } | |
619 | ||
4ed3a9ea | 620 | mmtrace (); |
3624c875 FF |
621 | } |
622 | ||
623 | #endif /* Have mmalloc and want corruption checking */ | |
624 | ||
625 | /* Called when a memory allocation fails, with the number of bytes of | |
626 | memory requested in SIZE. */ | |
627 | ||
628 | NORETURN void | |
629 | nomem (size) | |
630 | long size; | |
631 | { | |
632 | if (size > 0) | |
633 | { | |
634 | fatal ("virtual memory exhausted: can't allocate %ld bytes.", size); | |
635 | } | |
636 | else | |
637 | { | |
638 | fatal ("virtual memory exhausted."); | |
639 | } | |
640 | } | |
641 | ||
642 | /* Like mmalloc but get error if no storage available, and protect against | |
643 | the caller wanting to allocate zero bytes. Whether to return NULL for | |
644 | a zero byte request, or translate the request into a request for one | |
645 | byte of zero'd storage, is a religious issue. */ | |
646 | ||
647 | PTR | |
648 | xmmalloc (md, size) | |
649 | PTR md; | |
650 | long size; | |
651 | { | |
652 | register PTR val; | |
653 | ||
654 | if (size == 0) | |
655 | { | |
656 | val = NULL; | |
657 | } | |
658 | else if ((val = mmalloc (md, size)) == NULL) | |
659 | { | |
660 | nomem (size); | |
661 | } | |
662 | return (val); | |
663 | } | |
664 | ||
665 | /* Like mrealloc but get error if no storage available. */ | |
666 | ||
667 | PTR | |
668 | xmrealloc (md, ptr, size) | |
669 | PTR md; | |
670 | PTR ptr; | |
671 | long size; | |
672 | { | |
673 | register PTR val; | |
674 | ||
675 | if (ptr != NULL) | |
676 | { | |
677 | val = mrealloc (md, ptr, size); | |
678 | } | |
679 | else | |
680 | { | |
681 | val = mmalloc (md, size); | |
682 | } | |
683 | if (val == NULL) | |
684 | { | |
685 | nomem (size); | |
686 | } | |
687 | return (val); | |
688 | } | |
689 | ||
690 | /* Like malloc but get error if no storage available, and protect against | |
691 | the caller wanting to allocate zero bytes. */ | |
692 | ||
693 | PTR | |
694 | xmalloc (size) | |
695 | long size; | |
696 | { | |
199b2450 | 697 | return (xmmalloc ((PTR) NULL, size)); |
3624c875 FF |
698 | } |
699 | ||
700 | /* Like mrealloc but get error if no storage available. */ | |
701 | ||
702 | PTR | |
703 | xrealloc (ptr, size) | |
704 | PTR ptr; | |
705 | long size; | |
706 | { | |
199b2450 | 707 | return (xmrealloc ((PTR) NULL, ptr, size)); |
3624c875 FF |
708 | } |
709 | ||
bd5635a1 RP |
710 | \f |
711 | /* My replacement for the read system call. | |
712 | Used like `read' but keeps going if `read' returns too soon. */ | |
713 | ||
714 | int | |
715 | myread (desc, addr, len) | |
716 | int desc; | |
717 | char *addr; | |
718 | int len; | |
719 | { | |
720 | register int val; | |
721 | int orglen = len; | |
722 | ||
723 | while (len > 0) | |
724 | { | |
725 | val = read (desc, addr, len); | |
726 | if (val < 0) | |
727 | return val; | |
728 | if (val == 0) | |
729 | return orglen - len; | |
730 | len -= val; | |
731 | addr += val; | |
732 | } | |
733 | return orglen; | |
734 | } | |
735 | \f | |
736 | /* Make a copy of the string at PTR with SIZE characters | |
737 | (and add a null character at the end in the copy). | |
738 | Uses malloc to get the space. Returns the address of the copy. */ | |
739 | ||
740 | char * | |
741 | savestring (ptr, size) | |
088c3a0b | 742 | const char *ptr; |
bd5635a1 RP |
743 | int size; |
744 | { | |
745 | register char *p = (char *) xmalloc (size + 1); | |
4ed3a9ea | 746 | memcpy (p, ptr, size); |
bd5635a1 RP |
747 | p[size] = 0; |
748 | return p; | |
749 | } | |
750 | ||
3624c875 FF |
751 | char * |
752 | msavestring (md, ptr, size) | |
199b2450 | 753 | PTR md; |
3624c875 FF |
754 | const char *ptr; |
755 | int size; | |
756 | { | |
757 | register char *p = (char *) xmmalloc (md, size + 1); | |
4ed3a9ea | 758 | memcpy (p, ptr, size); |
3624c875 FF |
759 | p[size] = 0; |
760 | return p; | |
761 | } | |
762 | ||
8aa13b87 JK |
763 | /* The "const" is so it compiles under DGUX (which prototypes strsave |
764 | in <string.h>. FIXME: This should be named "xstrsave", shouldn't it? | |
765 | Doesn't real strsave return NULL if out of memory? */ | |
bd5635a1 RP |
766 | char * |
767 | strsave (ptr) | |
8aa13b87 | 768 | const char *ptr; |
bd5635a1 RP |
769 | { |
770 | return savestring (ptr, strlen (ptr)); | |
771 | } | |
772 | ||
3624c875 FF |
773 | char * |
774 | mstrsave (md, ptr) | |
199b2450 | 775 | PTR md; |
3624c875 FF |
776 | const char *ptr; |
777 | { | |
778 | return (msavestring (md, ptr, strlen (ptr))); | |
779 | } | |
780 | ||
bd5635a1 RP |
781 | void |
782 | print_spaces (n, file) | |
783 | register int n; | |
784 | register FILE *file; | |
785 | { | |
786 | while (n-- > 0) | |
787 | fputc (' ', file); | |
788 | } | |
789 | ||
8eec3310 SC |
790 | /* Print a host address. */ |
791 | ||
792 | void | |
793 | gdb_print_address (addr, stream) | |
794 | PTR addr; | |
795 | GDB_FILE *stream; | |
796 | { | |
797 | ||
798 | /* We could use the %p conversion specifier to fprintf if we had any | |
799 | way of knowing whether this host supports it. But the following | |
800 | should work on the Alpha and on 32 bit machines. */ | |
801 | ||
802 | fprintf_filtered (stream, "0x%lx", (unsigned long)addr); | |
803 | } | |
804 | ||
bd5635a1 RP |
805 | /* Ask user a y-or-n question and return 1 iff answer is yes. |
806 | Takes three args which are given to printf to print the question. | |
807 | The first, a control string, should end in "? ". | |
808 | It should not say how to answer, because we do that. */ | |
809 | ||
810 | /* VARARGS */ | |
811 | int | |
812 | query (va_alist) | |
813 | va_dcl | |
814 | { | |
815 | va_list args; | |
816 | char *ctlstr; | |
817 | register int answer; | |
818 | register int ans2; | |
d8742f46 | 819 | int retval; |
bd5635a1 RP |
820 | |
821 | /* Automatically answer "yes" if input is not from a terminal. */ | |
822 | if (!input_from_terminal_p ()) | |
823 | return 1; | |
824 | ||
825 | while (1) | |
826 | { | |
546014f7 | 827 | wrap_here (""); /* Flush any buffered output */ |
199b2450 | 828 | gdb_flush (gdb_stdout); |
d8742f46 JK |
829 | |
830 | if (annotation_level > 1) | |
831 | printf_filtered ("\n\032\032pre-query\n"); | |
832 | ||
bd5635a1 RP |
833 | va_start (args); |
834 | ctlstr = va_arg (args, char *); | |
199b2450 | 835 | vfprintf_filtered (gdb_stdout, ctlstr, args); |
b36e3a9b | 836 | va_end (args); |
bcf2e6ab | 837 | printf_filtered ("(y or n) "); |
d8742f46 JK |
838 | |
839 | if (annotation_level > 1) | |
840 | printf_filtered ("\n\032\032query\n"); | |
841 | ||
199b2450 | 842 | gdb_flush (gdb_stdout); |
b36e3a9b SG |
843 | answer = fgetc (stdin); |
844 | clearerr (stdin); /* in case of C-d */ | |
845 | if (answer == EOF) /* C-d */ | |
d8742f46 JK |
846 | { |
847 | retval = 1; | |
848 | break; | |
849 | } | |
b36e3a9b SG |
850 | if (answer != '\n') /* Eat rest of input line, to EOF or newline */ |
851 | do | |
852 | { | |
853 | ans2 = fgetc (stdin); | |
854 | clearerr (stdin); | |
855 | } | |
856 | while (ans2 != EOF && ans2 != '\n'); | |
bd5635a1 RP |
857 | if (answer >= 'a') |
858 | answer -= 040; | |
859 | if (answer == 'Y') | |
d8742f46 JK |
860 | { |
861 | retval = 1; | |
862 | break; | |
863 | } | |
bd5635a1 | 864 | if (answer == 'N') |
d8742f46 JK |
865 | { |
866 | retval = 0; | |
867 | break; | |
868 | } | |
bcf2e6ab | 869 | printf_filtered ("Please answer y or n.\n"); |
bd5635a1 | 870 | } |
d8742f46 JK |
871 | |
872 | if (annotation_level > 1) | |
873 | printf_filtered ("\n\032\032post-query\n"); | |
874 | return retval; | |
bd5635a1 | 875 | } |
7919c3ed | 876 | |
bd5635a1 RP |
877 | \f |
878 | /* Parse a C escape sequence. STRING_PTR points to a variable | |
879 | containing a pointer to the string to parse. That pointer | |
880 | should point to the character after the \. That pointer | |
881 | is updated past the characters we use. The value of the | |
882 | escape sequence is returned. | |
883 | ||
884 | A negative value means the sequence \ newline was seen, | |
885 | which is supposed to be equivalent to nothing at all. | |
886 | ||
887 | If \ is followed by a null character, we return a negative | |
888 | value and leave the string pointer pointing at the null character. | |
889 | ||
890 | If \ is followed by 000, we return 0 and leave the string pointer | |
891 | after the zeros. A value of 0 does not mean end of string. */ | |
892 | ||
893 | int | |
894 | parse_escape (string_ptr) | |
895 | char **string_ptr; | |
896 | { | |
897 | register int c = *(*string_ptr)++; | |
898 | switch (c) | |
899 | { | |
900 | case 'a': | |
2bc2e684 | 901 | return 007; /* Bell (alert) char */ |
bd5635a1 RP |
902 | case 'b': |
903 | return '\b'; | |
2bc2e684 | 904 | case 'e': /* Escape character */ |
bd5635a1 RP |
905 | return 033; |
906 | case 'f': | |
907 | return '\f'; | |
908 | case 'n': | |
909 | return '\n'; | |
910 | case 'r': | |
911 | return '\r'; | |
912 | case 't': | |
913 | return '\t'; | |
914 | case 'v': | |
915 | return '\v'; | |
916 | case '\n': | |
917 | return -2; | |
918 | case 0: | |
919 | (*string_ptr)--; | |
920 | return 0; | |
921 | case '^': | |
922 | c = *(*string_ptr)++; | |
923 | if (c == '\\') | |
924 | c = parse_escape (string_ptr); | |
925 | if (c == '?') | |
926 | return 0177; | |
927 | return (c & 0200) | (c & 037); | |
928 | ||
929 | case '0': | |
930 | case '1': | |
931 | case '2': | |
932 | case '3': | |
933 | case '4': | |
934 | case '5': | |
935 | case '6': | |
936 | case '7': | |
937 | { | |
938 | register int i = c - '0'; | |
939 | register int count = 0; | |
940 | while (++count < 3) | |
941 | { | |
942 | if ((c = *(*string_ptr)++) >= '0' && c <= '7') | |
943 | { | |
944 | i *= 8; | |
945 | i += c - '0'; | |
946 | } | |
947 | else | |
948 | { | |
949 | (*string_ptr)--; | |
950 | break; | |
951 | } | |
952 | } | |
953 | return i; | |
954 | } | |
955 | default: | |
956 | return c; | |
957 | } | |
958 | } | |
959 | \f | |
51b80b00 FF |
960 | /* Print the character C on STREAM as part of the contents of a literal |
961 | string whose delimiter is QUOTER. Note that this routine should only | |
962 | be call for printing things which are independent of the language | |
963 | of the program being debugged. */ | |
bd5635a1 RP |
964 | |
965 | void | |
51b80b00 | 966 | gdb_printchar (c, stream, quoter) |
088c3a0b | 967 | register int c; |
bd5635a1 RP |
968 | FILE *stream; |
969 | int quoter; | |
970 | { | |
bd5635a1 | 971 | |
7e7e2d40 JG |
972 | c &= 0xFF; /* Avoid sign bit follies */ |
973 | ||
fcdb113e JG |
974 | if ( c < 0x20 || /* Low control chars */ |
975 | (c >= 0x7F && c < 0xA0) || /* DEL, High controls */ | |
976 | (sevenbit_strings && c >= 0x80)) { /* high order bit set */ | |
bd5635a1 RP |
977 | switch (c) |
978 | { | |
979 | case '\n': | |
980 | fputs_filtered ("\\n", stream); | |
981 | break; | |
982 | case '\b': | |
983 | fputs_filtered ("\\b", stream); | |
984 | break; | |
985 | case '\t': | |
986 | fputs_filtered ("\\t", stream); | |
987 | break; | |
988 | case '\f': | |
989 | fputs_filtered ("\\f", stream); | |
990 | break; | |
991 | case '\r': | |
992 | fputs_filtered ("\\r", stream); | |
993 | break; | |
994 | case '\033': | |
995 | fputs_filtered ("\\e", stream); | |
996 | break; | |
997 | case '\007': | |
998 | fputs_filtered ("\\a", stream); | |
999 | break; | |
1000 | default: | |
1001 | fprintf_filtered (stream, "\\%.3o", (unsigned int) c); | |
1002 | break; | |
1003 | } | |
2bc2e684 FF |
1004 | } else { |
1005 | if (c == '\\' || c == quoter) | |
1006 | fputs_filtered ("\\", stream); | |
1007 | fprintf_filtered (stream, "%c", c); | |
1008 | } | |
bd5635a1 RP |
1009 | } |
1010 | \f | |
1011 | /* Number of lines per page or UINT_MAX if paging is disabled. */ | |
1012 | static unsigned int lines_per_page; | |
1013 | /* Number of chars per line or UNIT_MAX is line folding is disabled. */ | |
1014 | static unsigned int chars_per_line; | |
1015 | /* Current count of lines printed on this page, chars on this line. */ | |
1016 | static unsigned int lines_printed, chars_printed; | |
1017 | ||
1018 | /* Buffer and start column of buffered text, for doing smarter word- | |
1019 | wrapping. When someone calls wrap_here(), we start buffering output | |
1020 | that comes through fputs_filtered(). If we see a newline, we just | |
1021 | spit it out and forget about the wrap_here(). If we see another | |
1022 | wrap_here(), we spit it out and remember the newer one. If we see | |
1023 | the end of the line, we spit out a newline, the indent, and then | |
159dd2aa JK |
1024 | the buffered output. */ |
1025 | ||
1026 | /* Malloc'd buffer with chars_per_line+2 bytes. Contains characters which | |
1027 | are waiting to be output (they have already been counted in chars_printed). | |
1028 | When wrap_buffer[0] is null, the buffer is empty. */ | |
1029 | static char *wrap_buffer; | |
bd5635a1 | 1030 | |
159dd2aa JK |
1031 | /* Pointer in wrap_buffer to the next character to fill. */ |
1032 | static char *wrap_pointer; | |
bd5635a1 | 1033 | |
159dd2aa JK |
1034 | /* String to indent by if the wrap occurs. Must not be NULL if wrap_column |
1035 | is non-zero. */ | |
1036 | static char *wrap_indent; | |
1037 | ||
1038 | /* Column number on the screen where wrap_buffer begins, or 0 if wrapping | |
1039 | is not in effect. */ | |
bd5635a1 RP |
1040 | static int wrap_column; |
1041 | ||
e1ce8aa5 | 1042 | /* ARGSUSED */ |
bd5635a1 RP |
1043 | static void |
1044 | set_width_command (args, from_tty, c) | |
1045 | char *args; | |
1046 | int from_tty; | |
1047 | struct cmd_list_element *c; | |
1048 | { | |
1049 | if (!wrap_buffer) | |
1050 | { | |
1051 | wrap_buffer = (char *) xmalloc (chars_per_line + 2); | |
1052 | wrap_buffer[0] = '\0'; | |
1053 | } | |
1054 | else | |
1055 | wrap_buffer = (char *) xrealloc (wrap_buffer, chars_per_line + 2); | |
1056 | wrap_pointer = wrap_buffer; /* Start it at the beginning */ | |
1057 | } | |
1058 | ||
d974236f JG |
1059 | /* Wait, so the user can read what's on the screen. Prompt the user |
1060 | to continue by pressing RETURN. */ | |
1061 | ||
bd5635a1 RP |
1062 | static void |
1063 | prompt_for_continue () | |
1064 | { | |
351b221d | 1065 | char *ignore; |
d8742f46 JK |
1066 | char cont_prompt[120]; |
1067 | ||
4dd876ac JK |
1068 | if (annotation_level > 1) |
1069 | printf_unfiltered ("\n\032\032pre-prompt-for-continue\n"); | |
1070 | ||
d8742f46 JK |
1071 | strcpy (cont_prompt, |
1072 | "---Type <return> to continue, or q <return> to quit---"); | |
1073 | if (annotation_level > 1) | |
1074 | strcat (cont_prompt, "\n\032\032prompt-for-continue\n"); | |
351b221d | 1075 | |
d974236f JG |
1076 | /* We must do this *before* we call gdb_readline, else it will eventually |
1077 | call us -- thinking that we're trying to print beyond the end of the | |
1078 | screen. */ | |
1079 | reinitialize_more_filter (); | |
1080 | ||
bd5635a1 | 1081 | immediate_quit++; |
159dd2aa JK |
1082 | /* On a real operating system, the user can quit with SIGINT. |
1083 | But not on GO32. | |
1084 | ||
1085 | 'q' is provided on all systems so users don't have to change habits | |
1086 | from system to system, and because telling them what to do in | |
1087 | the prompt is more user-friendly than expecting them to think of | |
1088 | SIGINT. */ | |
a94100d1 JK |
1089 | /* Call readline, not gdb_readline, because GO32 readline handles control-C |
1090 | whereas control-C to gdb_readline will cause the user to get dumped | |
1091 | out to DOS. */ | |
d8742f46 | 1092 | ignore = readline (cont_prompt); |
4dd876ac JK |
1093 | |
1094 | if (annotation_level > 1) | |
1095 | printf_unfiltered ("\n\032\032post-prompt-for-continue\n"); | |
1096 | ||
351b221d | 1097 | if (ignore) |
159dd2aa JK |
1098 | { |
1099 | char *p = ignore; | |
1100 | while (*p == ' ' || *p == '\t') | |
1101 | ++p; | |
1102 | if (p[0] == 'q') | |
1103 | request_quit (SIGINT); | |
1104 | free (ignore); | |
1105 | } | |
bd5635a1 | 1106 | immediate_quit--; |
d974236f JG |
1107 | |
1108 | /* Now we have to do this again, so that GDB will know that it doesn't | |
1109 | need to save the ---Type <return>--- line at the top of the screen. */ | |
1110 | reinitialize_more_filter (); | |
1111 | ||
351b221d | 1112 | dont_repeat (); /* Forget prev cmd -- CR won't repeat it. */ |
bd5635a1 RP |
1113 | } |
1114 | ||
1115 | /* Reinitialize filter; ie. tell it to reset to original values. */ | |
1116 | ||
1117 | void | |
1118 | reinitialize_more_filter () | |
1119 | { | |
1120 | lines_printed = 0; | |
1121 | chars_printed = 0; | |
1122 | } | |
1123 | ||
1124 | /* Indicate that if the next sequence of characters overflows the line, | |
1125 | a newline should be inserted here rather than when it hits the end. | |
159dd2aa | 1126 | If INDENT is non-null, it is a string to be printed to indent the |
bd5635a1 RP |
1127 | wrapped part on the next line. INDENT must remain accessible until |
1128 | the next call to wrap_here() or until a newline is printed through | |
1129 | fputs_filtered(). | |
1130 | ||
1131 | If the line is already overfull, we immediately print a newline and | |
1132 | the indentation, and disable further wrapping. | |
1133 | ||
2bc2e684 FF |
1134 | If we don't know the width of lines, but we know the page height, |
1135 | we must not wrap words, but should still keep track of newlines | |
1136 | that were explicitly printed. | |
1137 | ||
159dd2aa JK |
1138 | INDENT should not contain tabs, as that will mess up the char count |
1139 | on the next line. FIXME. | |
1140 | ||
1141 | This routine is guaranteed to force out any output which has been | |
1142 | squirreled away in the wrap_buffer, so wrap_here ((char *)0) can be | |
1143 | used to force out output from the wrap_buffer. */ | |
bd5635a1 RP |
1144 | |
1145 | void | |
1146 | wrap_here(indent) | |
159dd2aa | 1147 | char *indent; |
bd5635a1 RP |
1148 | { |
1149 | if (wrap_buffer[0]) | |
1150 | { | |
1151 | *wrap_pointer = '\0'; | |
199b2450 | 1152 | fputs (wrap_buffer, gdb_stdout); |
bd5635a1 RP |
1153 | } |
1154 | wrap_pointer = wrap_buffer; | |
1155 | wrap_buffer[0] = '\0'; | |
2bc2e684 FF |
1156 | if (chars_per_line == UINT_MAX) /* No line overflow checking */ |
1157 | { | |
1158 | wrap_column = 0; | |
1159 | } | |
1160 | else if (chars_printed >= chars_per_line) | |
bd5635a1 RP |
1161 | { |
1162 | puts_filtered ("\n"); | |
159dd2aa JK |
1163 | if (indent != NULL) |
1164 | puts_filtered (indent); | |
bd5635a1 RP |
1165 | wrap_column = 0; |
1166 | } | |
1167 | else | |
1168 | { | |
1169 | wrap_column = chars_printed; | |
159dd2aa JK |
1170 | if (indent == NULL) |
1171 | wrap_indent = ""; | |
1172 | else | |
1173 | wrap_indent = indent; | |
bd5635a1 RP |
1174 | } |
1175 | } | |
1176 | ||
51b80b00 FF |
1177 | /* Ensure that whatever gets printed next, using the filtered output |
1178 | commands, starts at the beginning of the line. I.E. if there is | |
1179 | any pending output for the current line, flush it and start a new | |
1180 | line. Otherwise do nothing. */ | |
1181 | ||
1182 | void | |
1183 | begin_line () | |
1184 | { | |
1185 | if (chars_printed > 0) | |
1186 | { | |
1187 | puts_filtered ("\n"); | |
1188 | } | |
1189 | } | |
1190 | ||
199b2450 TL |
1191 | |
1192 | GDB_FILE * | |
1193 | gdb_fopen (name, mode) | |
1194 | char * name; | |
1195 | char * mode; | |
1196 | { | |
1197 | return fopen (name, mode); | |
1198 | } | |
1199 | ||
bd5635a1 | 1200 | void |
199b2450 TL |
1201 | gdb_flush (stream) |
1202 | FILE *stream; | |
1203 | { | |
1204 | fflush (stream); | |
1205 | } | |
1206 | ||
44a09a68 JK |
1207 | /* Like fputs but if FILTER is true, pause after every screenful. |
1208 | ||
1209 | Regardless of FILTER can wrap at points other than the final | |
1210 | character of a line. | |
1211 | ||
1212 | Unlike fputs, fputs_maybe_filtered does not return a value. | |
1213 | It is OK for LINEBUFFER to be NULL, in which case just don't print | |
1214 | anything. | |
1215 | ||
1216 | Note that a longjmp to top level may occur in this routine (only if | |
1217 | FILTER is true) (since prompt_for_continue may do so) so this | |
1218 | routine should not be called when cleanups are not in place. */ | |
1219 | ||
199b2450 TL |
1220 | static void |
1221 | fputs_maybe_filtered (linebuffer, stream, filter) | |
088c3a0b | 1222 | const char *linebuffer; |
bd5635a1 | 1223 | FILE *stream; |
199b2450 | 1224 | int filter; |
bd5635a1 | 1225 | { |
7919c3ed | 1226 | const char *lineptr; |
bd5635a1 RP |
1227 | |
1228 | if (linebuffer == 0) | |
1229 | return; | |
1230 | ||
1231 | /* Don't do any filtering if it is disabled. */ | |
199b2450 | 1232 | if (stream != gdb_stdout |
bd5635a1 RP |
1233 | || (lines_per_page == UINT_MAX && chars_per_line == UINT_MAX)) |
1234 | { | |
1235 | fputs (linebuffer, stream); | |
1236 | return; | |
1237 | } | |
1238 | ||
1239 | /* Go through and output each character. Show line extension | |
1240 | when this is necessary; prompt user for new page when this is | |
1241 | necessary. */ | |
1242 | ||
1243 | lineptr = linebuffer; | |
1244 | while (*lineptr) | |
1245 | { | |
1246 | /* Possible new page. */ | |
199b2450 TL |
1247 | if (filter && |
1248 | (lines_printed >= lines_per_page - 1)) | |
bd5635a1 RP |
1249 | prompt_for_continue (); |
1250 | ||
1251 | while (*lineptr && *lineptr != '\n') | |
1252 | { | |
1253 | /* Print a single line. */ | |
1254 | if (*lineptr == '\t') | |
1255 | { | |
1256 | if (wrap_column) | |
1257 | *wrap_pointer++ = '\t'; | |
1258 | else | |
1259 | putc ('\t', stream); | |
1260 | /* Shifting right by 3 produces the number of tab stops | |
1261 | we have already passed, and then adding one and | |
1262 | shifting left 3 advances to the next tab stop. */ | |
1263 | chars_printed = ((chars_printed >> 3) + 1) << 3; | |
1264 | lineptr++; | |
1265 | } | |
1266 | else | |
1267 | { | |
1268 | if (wrap_column) | |
1269 | *wrap_pointer++ = *lineptr; | |
1270 | else | |
1271 | putc (*lineptr, stream); | |
1272 | chars_printed++; | |
1273 | lineptr++; | |
1274 | } | |
1275 | ||
1276 | if (chars_printed >= chars_per_line) | |
1277 | { | |
1278 | unsigned int save_chars = chars_printed; | |
1279 | ||
1280 | chars_printed = 0; | |
1281 | lines_printed++; | |
1282 | /* If we aren't actually wrapping, don't output newline -- | |
1283 | if chars_per_line is right, we probably just overflowed | |
1284 | anyway; if it's wrong, let us keep going. */ | |
1285 | if (wrap_column) | |
1286 | putc ('\n', stream); | |
1287 | ||
1288 | /* Possible new page. */ | |
1289 | if (lines_printed >= lines_per_page - 1) | |
1290 | prompt_for_continue (); | |
1291 | ||
1292 | /* Now output indentation and wrapped string */ | |
1293 | if (wrap_column) | |
1294 | { | |
159dd2aa | 1295 | fputs (wrap_indent, stream); |
bd5635a1 RP |
1296 | *wrap_pointer = '\0'; /* Null-terminate saved stuff */ |
1297 | fputs (wrap_buffer, stream); /* and eject it */ | |
1298 | /* FIXME, this strlen is what prevents wrap_indent from | |
1299 | containing tabs. However, if we recurse to print it | |
1300 | and count its chars, we risk trouble if wrap_indent is | |
1301 | longer than (the user settable) chars_per_line. | |
1302 | Note also that this can set chars_printed > chars_per_line | |
1303 | if we are printing a long string. */ | |
1304 | chars_printed = strlen (wrap_indent) | |
1305 | + (save_chars - wrap_column); | |
1306 | wrap_pointer = wrap_buffer; /* Reset buffer */ | |
1307 | wrap_buffer[0] = '\0'; | |
1308 | wrap_column = 0; /* And disable fancy wrap */ | |
1309 | } | |
1310 | } | |
1311 | } | |
1312 | ||
1313 | if (*lineptr == '\n') | |
1314 | { | |
1315 | chars_printed = 0; | |
d11c44f1 | 1316 | wrap_here ((char *)0); /* Spit out chars, cancel further wraps */ |
bd5635a1 RP |
1317 | lines_printed++; |
1318 | putc ('\n', stream); | |
1319 | lineptr++; | |
1320 | } | |
1321 | } | |
1322 | } | |
1323 | ||
199b2450 TL |
1324 | void |
1325 | fputs_filtered (linebuffer, stream) | |
1326 | const char *linebuffer; | |
1327 | FILE *stream; | |
1328 | { | |
1329 | fputs_maybe_filtered (linebuffer, stream, 1); | |
1330 | } | |
1331 | ||
1332 | void | |
1333 | fputs_unfiltered (linebuffer, stream) | |
1334 | const char *linebuffer; | |
1335 | FILE *stream; | |
1336 | { | |
f29351d9 JK |
1337 | #if 0 |
1338 | ||
1339 | /* This gets the wrap_buffer buffering wrong when called from | |
1340 | gdb_readline (GDB was sometimes failing to print the prompt | |
1341 | before reading input). Even at other times, it seems kind of | |
1342 | misguided, especially now that printf_unfiltered doesn't use | |
1343 | printf_maybe_filtered. */ | |
1344 | ||
199b2450 | 1345 | fputs_maybe_filtered (linebuffer, stream, 0); |
f29351d9 JK |
1346 | #else |
1347 | fputs (linebuffer, stream); | |
1348 | #endif | |
199b2450 TL |
1349 | } |
1350 | ||
1351 | void | |
1352 | putc_unfiltered (c) | |
1353 | int c; | |
1354 | { | |
1355 | char buf[2]; | |
1356 | buf[0] = c; | |
1357 | buf[1] = 0; | |
1358 | fputs_unfiltered (buf, gdb_stdout); | |
1359 | } | |
1360 | ||
1361 | void | |
1362 | fputc_unfiltered (c, stream) | |
1363 | int c; | |
1364 | FILE * stream; | |
1365 | { | |
1366 | char buf[2]; | |
1367 | buf[0] = c; | |
1368 | buf[1] = 0; | |
1369 | fputs_unfiltered (buf, stream); | |
1370 | } | |
1371 | ||
1372 | ||
bd5635a1 RP |
1373 | /* Print a variable number of ARGS using format FORMAT. If this |
1374 | information is going to put the amount written (since the last call | |
d974236f | 1375 | to REINITIALIZE_MORE_FILTER or the last page break) over the page size, |
bd5635a1 RP |
1376 | print out a pause message and do a gdb_readline to get the users |
1377 | permision to continue. | |
1378 | ||
1379 | Unlike fprintf, this function does not return a value. | |
1380 | ||
1381 | We implement three variants, vfprintf (takes a vararg list and stream), | |
1382 | fprintf (takes a stream to write on), and printf (the usual). | |
1383 | ||
1384 | Note that this routine has a restriction that the length of the | |
1385 | final output line must be less than 255 characters *or* it must be | |
1386 | less than twice the size of the format string. This is a very | |
1387 | arbitrary restriction, but it is an internal restriction, so I'll | |
1388 | put it in. This means that the %s format specifier is almost | |
1389 | useless; unless the caller can GUARANTEE that the string is short | |
1390 | enough, fputs_filtered should be used instead. | |
1391 | ||
1392 | Note also that a longjmp to top level may occur in this routine | |
1393 | (since prompt_for_continue may do so) so this routine should not be | |
1394 | called when cleanups are not in place. */ | |
1395 | ||
d974236f JG |
1396 | #define MIN_LINEBUF 255 |
1397 | ||
199b2450 TL |
1398 | static void |
1399 | vfprintf_maybe_filtered (stream, format, args, filter) | |
bd5635a1 RP |
1400 | FILE *stream; |
1401 | char *format; | |
7919c3ed | 1402 | va_list args; |
199b2450 | 1403 | int filter; |
bd5635a1 | 1404 | { |
d974236f JG |
1405 | char line_buf[MIN_LINEBUF+10]; |
1406 | char *linebuffer = line_buf; | |
bd5635a1 RP |
1407 | int format_length; |
1408 | ||
1409 | format_length = strlen (format); | |
1410 | ||
bd5635a1 | 1411 | /* Reallocate buffer to a larger size if this is necessary. */ |
d974236f | 1412 | if (format_length * 2 > MIN_LINEBUF) |
bd5635a1 | 1413 | { |
d974236f | 1414 | linebuffer = alloca (10 + format_length * 2); |
bd5635a1 RP |
1415 | } |
1416 | ||
bd5635a1 RP |
1417 | /* This won't blow up if the restrictions described above are |
1418 | followed. */ | |
4ed3a9ea | 1419 | vsprintf (linebuffer, format, args); |
bd5635a1 | 1420 | |
199b2450 TL |
1421 | fputs_maybe_filtered (linebuffer, stream, filter); |
1422 | } | |
1423 | ||
1424 | ||
1425 | void | |
1426 | vfprintf_filtered (stream, format, args) | |
1427 | FILE *stream; | |
1428 | char *format; | |
1429 | va_list args; | |
1430 | { | |
1431 | vfprintf_maybe_filtered (stream, format, args, 1); | |
1432 | } | |
1433 | ||
1434 | void | |
1435 | vfprintf_unfiltered (stream, format, args) | |
1436 | FILE *stream; | |
1437 | char *format; | |
1438 | va_list args; | |
1439 | { | |
69fb299e | 1440 | vfprintf (stream, format, args); |
bd5635a1 RP |
1441 | } |
1442 | ||
51b80b00 FF |
1443 | void |
1444 | vprintf_filtered (format, args) | |
1445 | char *format; | |
1446 | va_list args; | |
1447 | { | |
199b2450 TL |
1448 | vfprintf_maybe_filtered (gdb_stdout, format, args, 1); |
1449 | } | |
1450 | ||
1451 | void | |
1452 | vprintf_unfiltered (format, args) | |
1453 | char *format; | |
1454 | va_list args; | |
1455 | { | |
69fb299e | 1456 | vfprintf (gdb_stdout, format, args); |
51b80b00 FF |
1457 | } |
1458 | ||
bd5635a1 RP |
1459 | /* VARARGS */ |
1460 | void | |
1461 | fprintf_filtered (va_alist) | |
1462 | va_dcl | |
1463 | { | |
546014f7 | 1464 | va_list args; |
bd5635a1 RP |
1465 | FILE *stream; |
1466 | char *format; | |
546014f7 PB |
1467 | |
1468 | va_start (args); | |
1469 | stream = va_arg (args, FILE *); | |
1470 | format = va_arg (args, char *); | |
1471 | ||
1472 | /* This won't blow up if the restrictions described above are | |
1473 | followed. */ | |
1474 | vfprintf_filtered (stream, format, args); | |
1475 | va_end (args); | |
1476 | } | |
1477 | ||
199b2450 TL |
1478 | /* VARARGS */ |
1479 | void | |
1480 | fprintf_unfiltered (va_alist) | |
1481 | va_dcl | |
1482 | { | |
1483 | va_list args; | |
1484 | FILE *stream; | |
1485 | char *format; | |
1486 | ||
1487 | va_start (args); | |
1488 | stream = va_arg (args, FILE *); | |
1489 | format = va_arg (args, char *); | |
1490 | ||
1491 | /* This won't blow up if the restrictions described above are | |
1492 | followed. */ | |
1493 | vfprintf_unfiltered (stream, format, args); | |
1494 | va_end (args); | |
1495 | } | |
1496 | ||
546014f7 | 1497 | /* Like fprintf_filtered, but prints it's result indent. |
199b2450 | 1498 | Called as fprintfi_filtered (spaces, stream, format, ...); */ |
546014f7 PB |
1499 | |
1500 | /* VARARGS */ | |
1501 | void | |
1502 | fprintfi_filtered (va_alist) | |
1503 | va_dcl | |
1504 | { | |
7919c3ed | 1505 | va_list args; |
546014f7 PB |
1506 | int spaces; |
1507 | FILE *stream; | |
1508 | char *format; | |
bd5635a1 RP |
1509 | |
1510 | va_start (args); | |
546014f7 | 1511 | spaces = va_arg (args, int); |
bd5635a1 RP |
1512 | stream = va_arg (args, FILE *); |
1513 | format = va_arg (args, char *); | |
546014f7 | 1514 | print_spaces_filtered (spaces, stream); |
bd5635a1 RP |
1515 | |
1516 | /* This won't blow up if the restrictions described above are | |
1517 | followed. */ | |
7919c3ed | 1518 | vfprintf_filtered (stream, format, args); |
bd5635a1 RP |
1519 | va_end (args); |
1520 | } | |
1521 | ||
199b2450 | 1522 | |
bd5635a1 RP |
1523 | /* VARARGS */ |
1524 | void | |
1525 | printf_filtered (va_alist) | |
1526 | va_dcl | |
1527 | { | |
1528 | va_list args; | |
1529 | char *format; | |
1530 | ||
1531 | va_start (args); | |
1532 | format = va_arg (args, char *); | |
1533 | ||
199b2450 TL |
1534 | vfprintf_filtered (gdb_stdout, format, args); |
1535 | va_end (args); | |
1536 | } | |
1537 | ||
1538 | ||
1539 | /* VARARGS */ | |
1540 | void | |
1541 | printf_unfiltered (va_alist) | |
1542 | va_dcl | |
1543 | { | |
1544 | va_list args; | |
1545 | char *format; | |
1546 | ||
1547 | va_start (args); | |
1548 | format = va_arg (args, char *); | |
1549 | ||
1550 | vfprintf_unfiltered (gdb_stdout, format, args); | |
bd5635a1 RP |
1551 | va_end (args); |
1552 | } | |
bd5635a1 | 1553 | |
546014f7 | 1554 | /* Like printf_filtered, but prints it's result indented. |
199b2450 | 1555 | Called as printfi_filtered (spaces, format, ...); */ |
546014f7 PB |
1556 | |
1557 | /* VARARGS */ | |
1558 | void | |
1559 | printfi_filtered (va_alist) | |
1560 | va_dcl | |
1561 | { | |
1562 | va_list args; | |
1563 | int spaces; | |
1564 | char *format; | |
1565 | ||
1566 | va_start (args); | |
1567 | spaces = va_arg (args, int); | |
1568 | format = va_arg (args, char *); | |
199b2450 TL |
1569 | print_spaces_filtered (spaces, gdb_stdout); |
1570 | vfprintf_filtered (gdb_stdout, format, args); | |
546014f7 PB |
1571 | va_end (args); |
1572 | } | |
1573 | ||
51b80b00 FF |
1574 | /* Easy -- but watch out! |
1575 | ||
1576 | This routine is *not* a replacement for puts()! puts() appends a newline. | |
1577 | This one doesn't, and had better not! */ | |
bd5635a1 RP |
1578 | |
1579 | void | |
1580 | puts_filtered (string) | |
1581 | char *string; | |
1582 | { | |
199b2450 TL |
1583 | fputs_filtered (string, gdb_stdout); |
1584 | } | |
1585 | ||
1586 | void | |
1587 | puts_unfiltered (string) | |
1588 | char *string; | |
1589 | { | |
1590 | fputs_unfiltered (string, gdb_stdout); | |
bd5635a1 RP |
1591 | } |
1592 | ||
1593 | /* Return a pointer to N spaces and a null. The pointer is good | |
1594 | until the next call to here. */ | |
1595 | char * | |
1596 | n_spaces (n) | |
1597 | int n; | |
1598 | { | |
1599 | register char *t; | |
1600 | static char *spaces; | |
1601 | static int max_spaces; | |
1602 | ||
1603 | if (n > max_spaces) | |
1604 | { | |
1605 | if (spaces) | |
1606 | free (spaces); | |
3624c875 | 1607 | spaces = (char *) xmalloc (n+1); |
bd5635a1 RP |
1608 | for (t = spaces+n; t != spaces;) |
1609 | *--t = ' '; | |
1610 | spaces[n] = '\0'; | |
1611 | max_spaces = n; | |
1612 | } | |
1613 | ||
1614 | return spaces + max_spaces - n; | |
1615 | } | |
1616 | ||
1617 | /* Print N spaces. */ | |
1618 | void | |
1619 | print_spaces_filtered (n, stream) | |
1620 | int n; | |
1621 | FILE *stream; | |
1622 | { | |
1623 | fputs_filtered (n_spaces (n), stream); | |
1624 | } | |
1625 | \f | |
1626 | /* C++ demangler stuff. */ | |
bd5635a1 | 1627 | |
65ce5df4 JG |
1628 | /* fprintf_symbol_filtered attempts to demangle NAME, a symbol in language |
1629 | LANG, using demangling args ARG_MODE, and print it filtered to STREAM. | |
1630 | If the name is not mangled, or the language for the name is unknown, or | |
1631 | demangling is off, the name is printed in its "raw" form. */ | |
1632 | ||
bd5635a1 | 1633 | void |
65ce5df4 | 1634 | fprintf_symbol_filtered (stream, name, lang, arg_mode) |
bd5635a1 RP |
1635 | FILE *stream; |
1636 | char *name; | |
65ce5df4 JG |
1637 | enum language lang; |
1638 | int arg_mode; | |
bd5635a1 | 1639 | { |
65ce5df4 | 1640 | char *demangled; |
bd5d07d9 | 1641 | |
65ce5df4 | 1642 | if (name != NULL) |
bd5d07d9 | 1643 | { |
65ce5df4 JG |
1644 | /* If user wants to see raw output, no problem. */ |
1645 | if (!demangle) | |
bd5d07d9 | 1646 | { |
65ce5df4 JG |
1647 | fputs_filtered (name, stream); |
1648 | } | |
1649 | else | |
1650 | { | |
1651 | switch (lang) | |
1652 | { | |
1653 | case language_cplus: | |
1654 | demangled = cplus_demangle (name, arg_mode); | |
1655 | break; | |
65ce5df4 JG |
1656 | case language_chill: |
1657 | demangled = chill_demangle (name); | |
1658 | break; | |
65ce5df4 JG |
1659 | default: |
1660 | demangled = NULL; | |
1661 | break; | |
1662 | } | |
1663 | fputs_filtered (demangled ? demangled : name, stream); | |
1664 | if (demangled != NULL) | |
1665 | { | |
1666 | free (demangled); | |
1667 | } | |
bd5d07d9 | 1668 | } |
bd5635a1 RP |
1669 | } |
1670 | } | |
51b57ded FF |
1671 | |
1672 | /* Do a strcmp() type operation on STRING1 and STRING2, ignoring any | |
1673 | differences in whitespace. Returns 0 if they match, non-zero if they | |
546014f7 PB |
1674 | don't (slightly different than strcmp()'s range of return values). |
1675 | ||
1676 | As an extra hack, string1=="FOO(ARGS)" matches string2=="FOO". | |
2e4964ad FF |
1677 | This "feature" is useful when searching for matching C++ function names |
1678 | (such as if the user types 'break FOO', where FOO is a mangled C++ | |
1679 | function). */ | |
51b57ded | 1680 | |
51b80b00 | 1681 | int |
51b57ded FF |
1682 | strcmp_iw (string1, string2) |
1683 | const char *string1; | |
1684 | const char *string2; | |
1685 | { | |
1686 | while ((*string1 != '\0') && (*string2 != '\0')) | |
1687 | { | |
1688 | while (isspace (*string1)) | |
1689 | { | |
1690 | string1++; | |
1691 | } | |
1692 | while (isspace (*string2)) | |
1693 | { | |
1694 | string2++; | |
1695 | } | |
1696 | if (*string1 != *string2) | |
1697 | { | |
1698 | break; | |
1699 | } | |
1700 | if (*string1 != '\0') | |
1701 | { | |
1702 | string1++; | |
1703 | string2++; | |
1704 | } | |
1705 | } | |
546014f7 | 1706 | return (*string1 != '\0' && *string1 != '(') || (*string2 != '\0'); |
51b57ded FF |
1707 | } |
1708 | ||
bd5635a1 | 1709 | \f |
bd5635a1 RP |
1710 | void |
1711 | _initialize_utils () | |
1712 | { | |
1713 | struct cmd_list_element *c; | |
1714 | ||
1715 | c = add_set_cmd ("width", class_support, var_uinteger, | |
1716 | (char *)&chars_per_line, | |
1717 | "Set number of characters gdb thinks are in a line.", | |
1718 | &setlist); | |
1719 | add_show_from_set (c, &showlist); | |
d747e0af | 1720 | c->function.sfunc = set_width_command; |
bd5635a1 RP |
1721 | |
1722 | add_show_from_set | |
1723 | (add_set_cmd ("height", class_support, | |
1724 | var_uinteger, (char *)&lines_per_page, | |
1725 | "Set number of lines gdb thinks are in a page.", &setlist), | |
1726 | &showlist); | |
1727 | ||
1728 | /* These defaults will be used if we are unable to get the correct | |
1729 | values from termcap. */ | |
51b57ded FF |
1730 | #if defined(__GO32__) |
1731 | lines_per_page = ScreenRows(); | |
1732 | chars_per_line = ScreenCols(); | |
1733 | #else | |
bd5635a1 RP |
1734 | lines_per_page = 24; |
1735 | chars_per_line = 80; | |
1736 | /* Initialize the screen height and width from termcap. */ | |
1737 | { | |
1738 | char *termtype = getenv ("TERM"); | |
1739 | ||
1740 | /* Positive means success, nonpositive means failure. */ | |
1741 | int status; | |
1742 | ||
1743 | /* 2048 is large enough for all known terminals, according to the | |
1744 | GNU termcap manual. */ | |
1745 | char term_buffer[2048]; | |
1746 | ||
1747 | if (termtype) | |
1748 | { | |
1749 | status = tgetent (term_buffer, termtype); | |
1750 | if (status > 0) | |
1751 | { | |
1752 | int val; | |
1753 | ||
1754 | val = tgetnum ("li"); | |
1755 | if (val >= 0) | |
1756 | lines_per_page = val; | |
1757 | else | |
1758 | /* The number of lines per page is not mentioned | |
1759 | in the terminal description. This probably means | |
1760 | that paging is not useful (e.g. emacs shell window), | |
1761 | so disable paging. */ | |
1762 | lines_per_page = UINT_MAX; | |
1763 | ||
1764 | val = tgetnum ("co"); | |
1765 | if (val >= 0) | |
1766 | chars_per_line = val; | |
1767 | } | |
1768 | } | |
1769 | } | |
1770 | ||
1eeba686 PB |
1771 | #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER) |
1772 | ||
4ace50a5 | 1773 | /* If there is a better way to determine the window size, use it. */ |
1eeba686 PB |
1774 | SIGWINCH_HANDLER (); |
1775 | #endif | |
51b57ded | 1776 | #endif |
2bc2e684 | 1777 | /* If the output is not a terminal, don't paginate it. */ |
199b2450 | 1778 | if (!ISATTY (gdb_stdout)) |
2bc2e684 FF |
1779 | lines_per_page = UINT_MAX; |
1780 | ||
bd5635a1 RP |
1781 | set_width_command ((char *)NULL, 0, c); |
1782 | ||
1783 | add_show_from_set | |
1784 | (add_set_cmd ("demangle", class_support, var_boolean, | |
1785 | (char *)&demangle, | |
1786 | "Set demangling of encoded C++ names when displaying symbols.", | |
f266e564 JK |
1787 | &setprintlist), |
1788 | &showprintlist); | |
bd5635a1 RP |
1789 | |
1790 | add_show_from_set | |
1791 | (add_set_cmd ("sevenbit-strings", class_support, var_boolean, | |
1792 | (char *)&sevenbit_strings, | |
1793 | "Set printing of 8-bit characters in strings as \\nnn.", | |
f266e564 JK |
1794 | &setprintlist), |
1795 | &showprintlist); | |
bd5635a1 RP |
1796 | |
1797 | add_show_from_set | |
1798 | (add_set_cmd ("asm-demangle", class_support, var_boolean, | |
1799 | (char *)&asm_demangle, | |
1800 | "Set demangling of C++ names in disassembly listings.", | |
f266e564 JK |
1801 | &setprintlist), |
1802 | &showprintlist); | |
bd5635a1 | 1803 | } |
1eeba686 PB |
1804 | |
1805 | /* Machine specific function to handle SIGWINCH signal. */ | |
1806 | ||
1807 | #ifdef SIGWINCH_HANDLER_BODY | |
1808 | SIGWINCH_HANDLER_BODY | |
1809 | #endif | |
bd5d07d9 | 1810 |