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