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