Commit | Line | Data |
---|---|---|
c906108c | 1 | /* General utility routines for GDB, the GNU debugger. |
1bac305b | 2 | |
c5a57081 | 3 | Copyright (C) 1986, 1988-2012 Free Software Foundation, Inc. |
c906108c | 4 | |
c5aa993b | 5 | This file is part of GDB. |
c906108c | 6 | |
c5aa993b JM |
7 | This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 9 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 10 | (at your option) any later version. |
c906108c | 11 | |
c5aa993b JM |
12 | This program is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
c906108c | 16 | |
c5aa993b | 17 | You should have received a copy of the GNU General Public License |
a9762ec7 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c | 19 | |
4e8f7a8b | 20 | #include "defs.h" |
5a56e9c5 | 21 | #include "dyn-string.h" |
4e8f7a8b DJ |
22 | #include "gdb_assert.h" |
23 | #include <ctype.h> | |
24 | #include "gdb_string.h" | |
0b6cb71e | 25 | #include "gdb_wait.h" |
4e8f7a8b | 26 | #include "event-top.h" |
60250e8b | 27 | #include "exceptions.h" |
95e54da7 | 28 | #include "gdbthread.h" |
202cbf1c | 29 | #include "fnmatch.h" |
7991dee7 JK |
30 | #ifdef HAVE_SYS_RESOURCE_H |
31 | #include <sys/resource.h> | |
32 | #endif /* HAVE_SYS_RESOURCE_H */ | |
4e8f7a8b | 33 | |
6a83354a AC |
34 | #ifdef TUI |
35 | #include "tui/tui.h" /* For tui_get_command_dimension. */ | |
36 | #endif | |
37 | ||
9d271fd8 AC |
38 | #ifdef __GO32__ |
39 | #include <pc.h> | |
40 | #endif | |
41 | ||
581e13c1 | 42 | /* SunOS's curses.h has a '#define reg register' in it. Thank you Sun. */ |
c906108c SS |
43 | #ifdef reg |
44 | #undef reg | |
45 | #endif | |
46 | ||
042be3a9 | 47 | #include <signal.h> |
0a1c4d10 | 48 | #include "timeval-utils.h" |
c906108c SS |
49 | #include "gdbcmd.h" |
50 | #include "serial.h" | |
51 | #include "bfd.h" | |
52 | #include "target.h" | |
50f182aa | 53 | #include "gdb-demangle.h" |
c906108c SS |
54 | #include "expression.h" |
55 | #include "language.h" | |
234b45d4 | 56 | #include "charset.h" |
c906108c | 57 | #include "annotate.h" |
303c8ebd | 58 | #include "filenames.h" |
7b90c3f9 | 59 | #include "symfile.h" |
ae5a43e0 | 60 | #include "gdb_obstack.h" |
9544c605 | 61 | #include "gdbcore.h" |
698ba934 | 62 | #include "top.h" |
7c953934 | 63 | #include "main.h" |
cb08cc53 | 64 | #include "solist.h" |
c906108c | 65 | |
8731e58e | 66 | #include "inferior.h" /* for signed_pointer_to_address */ |
ac2e2ef7 | 67 | |
2d1b2124 AC |
68 | #include <sys/param.h> /* For MAXPATHLEN */ |
69 | ||
3b78cdbb | 70 | #include "gdb_curses.h" |
020cc13c | 71 | |
dbda9972 | 72 | #include "readline/readline.h" |
c906108c | 73 | |
75feb17d DJ |
74 | #include <sys/time.h> |
75 | #include <time.h> | |
76 | ||
8626589c | 77 | #include "gdb_usleep.h" |
390a8aca | 78 | #include "interps.h" |
dc92e161 | 79 | #include "gdb_regex.h" |
8626589c | 80 | |
a3828db0 | 81 | #if !HAVE_DECL_MALLOC |
5ac79d78 | 82 | extern PTR malloc (); /* ARI: PTR */ |
3c37485b | 83 | #endif |
a3828db0 | 84 | #if !HAVE_DECL_REALLOC |
5ac79d78 | 85 | extern PTR realloc (); /* ARI: PTR */ |
0e52036f | 86 | #endif |
a3828db0 | 87 | #if !HAVE_DECL_FREE |
81b8eb80 AC |
88 | extern void free (); |
89 | #endif | |
81b8eb80 | 90 | |
c906108c SS |
91 | /* readline defines this. */ |
92 | #undef savestring | |
93 | ||
9a4105ab | 94 | void (*deprecated_error_begin_hook) (void); |
c906108c SS |
95 | |
96 | /* Prototypes for local functions */ | |
97 | ||
d9fcf2fb | 98 | static void vfprintf_maybe_filtered (struct ui_file *, const char *, |
a0b31db1 | 99 | va_list, int) ATTRIBUTE_PRINTF (2, 0); |
c906108c | 100 | |
d9fcf2fb | 101 | static void fputs_maybe_filtered (const char *, struct ui_file *, int); |
c906108c | 102 | |
a14ed312 | 103 | static void prompt_for_continue (void); |
c906108c | 104 | |
eb0d3137 | 105 | static void set_screen_size (void); |
a14ed312 | 106 | static void set_width (void); |
c906108c | 107 | |
75feb17d DJ |
108 | /* A flag indicating whether to timestamp debugging messages. */ |
109 | ||
110 | static int debug_timestamp = 0; | |
111 | ||
581e13c1 | 112 | /* Nonzero if we have job control. */ |
c906108c SS |
113 | |
114 | int job_control; | |
115 | ||
116 | /* Nonzero means a quit has been requested. */ | |
117 | ||
118 | int quit_flag; | |
119 | ||
120 | /* Nonzero means quit immediately if Control-C is typed now, rather | |
121 | than waiting until QUIT is executed. Be careful in setting this; | |
122 | code which executes with immediate_quit set has to be very careful | |
123 | about being able to deal with being interrupted at any time. It is | |
124 | almost always better to use QUIT; the only exception I can think of | |
125 | is being able to quit out of a system call (using EINTR loses if | |
126 | the SIGINT happens between the previous QUIT and the system call). | |
127 | To immediately quit in the case in which a SIGINT happens between | |
128 | the previous QUIT and setting immediate_quit (desirable anytime we | |
129 | expect to block), call QUIT after setting immediate_quit. */ | |
130 | ||
131 | int immediate_quit; | |
132 | ||
c906108c SS |
133 | /* Nonzero means that strings with character values >0x7F should be printed |
134 | as octal escapes. Zero means just print the value (e.g. it's an | |
135 | international character, and the terminal or window can cope.) */ | |
136 | ||
137 | int sevenbit_strings = 0; | |
920d2a44 AC |
138 | static void |
139 | show_sevenbit_strings (struct ui_file *file, int from_tty, | |
140 | struct cmd_list_element *c, const char *value) | |
141 | { | |
3e43a32a MS |
142 | fprintf_filtered (file, _("Printing of 8-bit characters " |
143 | "in strings as \\nnn is %s.\n"), | |
920d2a44 AC |
144 | value); |
145 | } | |
c906108c SS |
146 | |
147 | /* String to be printed before error messages, if any. */ | |
148 | ||
149 | char *error_pre_print; | |
150 | ||
151 | /* String to be printed before quit messages, if any. */ | |
152 | ||
153 | char *quit_pre_print; | |
154 | ||
155 | /* String to be printed before warning messages, if any. */ | |
156 | ||
157 | char *warning_pre_print = "\nwarning: "; | |
158 | ||
159 | int pagination_enabled = 1; | |
920d2a44 AC |
160 | static void |
161 | show_pagination_enabled (struct ui_file *file, int from_tty, | |
162 | struct cmd_list_element *c, const char *value) | |
163 | { | |
164 | fprintf_filtered (file, _("State of pagination is %s.\n"), value); | |
165 | } | |
166 | ||
c906108c | 167 | \f |
c27f5738 | 168 | /* Cleanup utilities. |
c5aa993b | 169 | |
c27f5738 DE |
170 | These are not defined in cleanups.c (nor declared in cleanups.h) |
171 | because while they use the "cleanup API" they are not part of the | |
172 | "cleanup API". */ | |
7a292a7a | 173 | |
7a292a7a | 174 | static void |
fba45db2 | 175 | do_freeargv (void *arg) |
7a292a7a | 176 | { |
c5aa993b | 177 | freeargv ((char **) arg); |
7a292a7a SS |
178 | } |
179 | ||
180 | struct cleanup * | |
fba45db2 | 181 | make_cleanup_freeargv (char **arg) |
7a292a7a | 182 | { |
e0088cfd | 183 | return make_cleanup (do_freeargv, arg); |
7a292a7a SS |
184 | } |
185 | ||
5a56e9c5 DE |
186 | static void |
187 | do_dyn_string_delete (void *arg) | |
188 | { | |
189 | dyn_string_delete ((dyn_string_t) arg); | |
190 | } | |
191 | ||
192 | struct cleanup * | |
193 | make_cleanup_dyn_string_delete (dyn_string_t arg) | |
194 | { | |
e0088cfd | 195 | return make_cleanup (do_dyn_string_delete, arg); |
5a56e9c5 DE |
196 | } |
197 | ||
5c65bbb6 AC |
198 | static void |
199 | do_bfd_close_cleanup (void *arg) | |
200 | { | |
201 | bfd_close (arg); | |
202 | } | |
203 | ||
204 | struct cleanup * | |
205 | make_cleanup_bfd_close (bfd *abfd) | |
206 | { | |
207 | return make_cleanup (do_bfd_close_cleanup, abfd); | |
208 | } | |
209 | ||
f5ff8c83 AC |
210 | static void |
211 | do_close_cleanup (void *arg) | |
212 | { | |
f042532c | 213 | int *fd = arg; |
e0627e85 | 214 | |
f042532c | 215 | close (*fd); |
f5ff8c83 AC |
216 | } |
217 | ||
218 | struct cleanup * | |
219 | make_cleanup_close (int fd) | |
220 | { | |
f042532c | 221 | int *saved_fd = xmalloc (sizeof (fd)); |
e0627e85 | 222 | |
f042532c | 223 | *saved_fd = fd; |
a05016c0 | 224 | return make_cleanup_dtor (do_close_cleanup, saved_fd, xfree); |
f5ff8c83 AC |
225 | } |
226 | ||
7c8a8b04 TT |
227 | /* Helper function which does the work for make_cleanup_fclose. */ |
228 | ||
229 | static void | |
230 | do_fclose_cleanup (void *arg) | |
231 | { | |
c02866a0 | 232 | FILE *file = arg; |
e0627e85 | 233 | |
c02866a0 | 234 | fclose (file); |
7c8a8b04 TT |
235 | } |
236 | ||
237 | /* Return a new cleanup that closes FILE. */ | |
238 | ||
239 | struct cleanup * | |
240 | make_cleanup_fclose (FILE *file) | |
241 | { | |
242 | return make_cleanup (do_fclose_cleanup, file); | |
243 | } | |
244 | ||
16ad9370 TT |
245 | /* Helper function which does the work for make_cleanup_obstack_free. */ |
246 | ||
247 | static void | |
248 | do_obstack_free (void *arg) | |
249 | { | |
250 | struct obstack *ob = arg; | |
e0627e85 | 251 | |
16ad9370 TT |
252 | obstack_free (ob, NULL); |
253 | } | |
254 | ||
255 | /* Return a new cleanup that frees OBSTACK. */ | |
256 | ||
257 | struct cleanup * | |
258 | make_cleanup_obstack_free (struct obstack *obstack) | |
259 | { | |
260 | return make_cleanup (do_obstack_free, obstack); | |
261 | } | |
262 | ||
11cf8741 | 263 | static void |
d9fcf2fb | 264 | do_ui_file_delete (void *arg) |
11cf8741 | 265 | { |
d9fcf2fb | 266 | ui_file_delete (arg); |
11cf8741 JM |
267 | } |
268 | ||
269 | struct cleanup * | |
d9fcf2fb | 270 | make_cleanup_ui_file_delete (struct ui_file *arg) |
11cf8741 | 271 | { |
e0088cfd | 272 | return make_cleanup (do_ui_file_delete, arg); |
11cf8741 JM |
273 | } |
274 | ||
8d4d924b JK |
275 | /* Helper function for make_cleanup_ui_out_redirect_pop. */ |
276 | ||
277 | static void | |
278 | do_ui_out_redirect_pop (void *arg) | |
279 | { | |
280 | struct ui_out *uiout = arg; | |
281 | ||
282 | if (ui_out_redirect (uiout, NULL) < 0) | |
283 | warning (_("Cannot restore redirection of the current output protocol")); | |
284 | } | |
285 | ||
286 | /* Return a new cleanup that pops the last redirection by ui_out_redirect | |
287 | with NULL parameter. */ | |
288 | ||
289 | struct cleanup * | |
290 | make_cleanup_ui_out_redirect_pop (struct ui_out *uiout) | |
291 | { | |
e0088cfd | 292 | return make_cleanup (do_ui_out_redirect_pop, uiout); |
8d4d924b JK |
293 | } |
294 | ||
7b90c3f9 JB |
295 | static void |
296 | do_free_section_addr_info (void *arg) | |
297 | { | |
298 | free_section_addr_info (arg); | |
299 | } | |
300 | ||
301 | struct cleanup * | |
302 | make_cleanup_free_section_addr_info (struct section_addr_info *addrs) | |
303 | { | |
e0088cfd | 304 | return make_cleanup (do_free_section_addr_info, addrs); |
7b90c3f9 JB |
305 | } |
306 | ||
0b080f59 VP |
307 | struct restore_integer_closure |
308 | { | |
309 | int *variable; | |
310 | int value; | |
311 | }; | |
312 | ||
313 | static void | |
314 | restore_integer (void *p) | |
315 | { | |
316 | struct restore_integer_closure *closure = p; | |
e0627e85 | 317 | |
0b080f59 VP |
318 | *(closure->variable) = closure->value; |
319 | } | |
7b90c3f9 | 320 | |
3e43a32a MS |
321 | /* Remember the current value of *VARIABLE and make it restored when |
322 | the cleanup is run. */ | |
5da1313b | 323 | |
c906108c | 324 | struct cleanup * |
0b080f59 VP |
325 | make_cleanup_restore_integer (int *variable) |
326 | { | |
327 | struct restore_integer_closure *c = | |
328 | xmalloc (sizeof (struct restore_integer_closure)); | |
e0627e85 | 329 | |
0b080f59 VP |
330 | c->variable = variable; |
331 | c->value = *variable; | |
332 | ||
e0088cfd | 333 | return make_cleanup_dtor (restore_integer, (void *) c, xfree); |
0b080f59 VP |
334 | } |
335 | ||
3e43a32a MS |
336 | /* Remember the current value of *VARIABLE and make it restored when |
337 | the cleanup is run. */ | |
5da1313b JK |
338 | |
339 | struct cleanup * | |
340 | make_cleanup_restore_uinteger (unsigned int *variable) | |
341 | { | |
342 | return make_cleanup_restore_integer ((int *) variable); | |
343 | } | |
344 | ||
c0edd9ed JK |
345 | /* Helper for make_cleanup_unpush_target. */ |
346 | ||
347 | static void | |
348 | do_unpush_target (void *arg) | |
349 | { | |
350 | struct target_ops *ops = arg; | |
351 | ||
352 | unpush_target (ops); | |
353 | } | |
354 | ||
355 | /* Return a new cleanup that unpushes OPS. */ | |
356 | ||
357 | struct cleanup * | |
358 | make_cleanup_unpush_target (struct target_ops *ops) | |
359 | { | |
e0088cfd | 360 | return make_cleanup (do_unpush_target, ops); |
c0edd9ed JK |
361 | } |
362 | ||
8e3b41a9 JK |
363 | /* Helper for make_cleanup_htab_delete compile time checking the types. */ |
364 | ||
365 | static void | |
366 | do_htab_delete_cleanup (void *htab_voidp) | |
367 | { | |
368 | htab_t htab = htab_voidp; | |
369 | ||
370 | htab_delete (htab); | |
371 | } | |
372 | ||
373 | /* Return a new cleanup that deletes HTAB. */ | |
374 | ||
375 | struct cleanup * | |
376 | make_cleanup_htab_delete (htab_t htab) | |
377 | { | |
378 | return make_cleanup (do_htab_delete_cleanup, htab); | |
379 | } | |
380 | ||
5da1313b JK |
381 | struct restore_ui_file_closure |
382 | { | |
383 | struct ui_file **variable; | |
384 | struct ui_file *value; | |
385 | }; | |
386 | ||
387 | static void | |
388 | do_restore_ui_file (void *p) | |
389 | { | |
390 | struct restore_ui_file_closure *closure = p; | |
391 | ||
392 | *(closure->variable) = closure->value; | |
393 | } | |
394 | ||
395 | /* Remember the current value of *VARIABLE and make it restored when | |
396 | the cleanup is run. */ | |
397 | ||
398 | struct cleanup * | |
399 | make_cleanup_restore_ui_file (struct ui_file **variable) | |
400 | { | |
401 | struct restore_ui_file_closure *c = XNEW (struct restore_ui_file_closure); | |
402 | ||
403 | c->variable = variable; | |
404 | c->value = *variable; | |
405 | ||
406 | return make_cleanup_dtor (do_restore_ui_file, (void *) c, xfree); | |
407 | } | |
408 | ||
028d0ed5 TJB |
409 | /* Helper for make_cleanup_value_free_to_mark. */ |
410 | ||
411 | static void | |
412 | do_value_free_to_mark (void *value) | |
413 | { | |
414 | value_free_to_mark ((struct value *) value); | |
415 | } | |
416 | ||
417 | /* Free all values allocated since MARK was obtained by value_mark | |
418 | (except for those released) when the cleanup is run. */ | |
419 | ||
420 | struct cleanup * | |
421 | make_cleanup_value_free_to_mark (struct value *mark) | |
422 | { | |
e0088cfd | 423 | return make_cleanup (do_value_free_to_mark, mark); |
028d0ed5 TJB |
424 | } |
425 | ||
72fc29ff TT |
426 | /* Helper for make_cleanup_value_free. */ |
427 | ||
428 | static void | |
429 | do_value_free (void *value) | |
430 | { | |
431 | value_free (value); | |
432 | } | |
433 | ||
434 | /* Free VALUE. */ | |
435 | ||
436 | struct cleanup * | |
437 | make_cleanup_value_free (struct value *value) | |
438 | { | |
e0088cfd | 439 | return make_cleanup (do_value_free, value); |
72fc29ff TT |
440 | } |
441 | ||
cb08cc53 JK |
442 | /* Helper for make_cleanup_free_so. */ |
443 | ||
444 | static void | |
445 | do_free_so (void *arg) | |
446 | { | |
447 | struct so_list *so = arg; | |
448 | ||
449 | free_so (so); | |
450 | } | |
451 | ||
452 | /* Make cleanup handler calling free_so for SO. */ | |
453 | ||
454 | struct cleanup * | |
455 | make_cleanup_free_so (struct so_list *so) | |
456 | { | |
e0088cfd | 457 | return make_cleanup (do_free_so, so); |
cb08cc53 JK |
458 | } |
459 | ||
c906108c SS |
460 | /* This function is useful for cleanups. |
461 | Do | |
462 | ||
c5aa993b JM |
463 | foo = xmalloc (...); |
464 | old_chain = make_cleanup (free_current_contents, &foo); | |
c906108c SS |
465 | |
466 | to arrange to free the object thus allocated. */ | |
467 | ||
468 | void | |
2f9429ae | 469 | free_current_contents (void *ptr) |
c906108c | 470 | { |
2f9429ae | 471 | void **location = ptr; |
e0627e85 | 472 | |
e2f9c474 | 473 | if (location == NULL) |
8e65ff28 | 474 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 475 | _("free_current_contents: NULL pointer")); |
2f9429ae | 476 | if (*location != NULL) |
e2f9c474 | 477 | { |
b8c9b27d | 478 | xfree (*location); |
e2f9c474 AC |
479 | *location = NULL; |
480 | } | |
c906108c SS |
481 | } |
482 | ||
0f3bb72e PH |
483 | /* If nonzero, display time usage both at startup and for each command. */ |
484 | ||
485 | static int display_time; | |
486 | ||
487 | /* If nonzero, display space usage both at startup and for each command. */ | |
488 | ||
489 | static int display_space; | |
490 | ||
491 | /* Records a run time and space usage to be used as a base for | |
492 | reporting elapsed time or change in space. In addition, | |
493 | the msg_type field indicates whether the saved time is from the | |
494 | beginning of GDB execution (0) or the beginning of an individual | |
495 | command execution (1). */ | |
496 | struct cmd_stats | |
497 | { | |
498 | int msg_type; | |
0a1c4d10 DE |
499 | long start_cpu_time; |
500 | struct timeval start_wall_time; | |
0f3bb72e PH |
501 | long start_space; |
502 | }; | |
503 | ||
504 | /* Set whether to display time statistics to NEW_VALUE (non-zero | |
505 | means true). */ | |
506 | void | |
507 | set_display_time (int new_value) | |
508 | { | |
509 | display_time = new_value; | |
510 | } | |
511 | ||
512 | /* Set whether to display space statistics to NEW_VALUE (non-zero | |
513 | means true). */ | |
514 | void | |
515 | set_display_space (int new_value) | |
516 | { | |
517 | display_space = new_value; | |
518 | } | |
519 | ||
520 | /* As indicated by display_time and display_space, report GDB's elapsed time | |
521 | and space usage from the base time and space provided in ARG, which | |
581e13c1 MS |
522 | must be a pointer to a struct cmd_stat. This function is intended |
523 | to be called as a cleanup. */ | |
0f3bb72e PH |
524 | static void |
525 | report_command_stats (void *arg) | |
526 | { | |
527 | struct cmd_stats *start_stats = (struct cmd_stats *) arg; | |
528 | int msg_type = start_stats->msg_type; | |
529 | ||
530 | if (display_time) | |
531 | { | |
0a1c4d10 DE |
532 | long cmd_time = get_run_time () - start_stats->start_cpu_time; |
533 | struct timeval now_wall_time, delta_wall_time; | |
534 | ||
535 | gettimeofday (&now_wall_time, NULL); | |
536 | timeval_sub (&delta_wall_time, | |
537 | &now_wall_time, &start_stats->start_wall_time); | |
0f3bb72e PH |
538 | |
539 | printf_unfiltered (msg_type == 0 | |
0a1c4d10 DE |
540 | ? _("Startup time: %ld.%06ld (cpu), %ld.%06ld (wall)\n") |
541 | : _("Command execution time: %ld.%06ld (cpu), %ld.%06ld (wall)\n"), | |
542 | cmd_time / 1000000, cmd_time % 1000000, | |
2b54dda2 DM |
543 | (long) delta_wall_time.tv_sec, |
544 | (long) delta_wall_time.tv_usec); | |
0f3bb72e PH |
545 | } |
546 | ||
547 | if (display_space) | |
548 | { | |
549 | #ifdef HAVE_SBRK | |
550 | char *lim = (char *) sbrk (0); | |
551 | ||
552 | long space_now = lim - lim_at_start; | |
553 | long space_diff = space_now - start_stats->start_space; | |
554 | ||
555 | printf_unfiltered (msg_type == 0 | |
5d901a73 TT |
556 | ? _("Space used: %ld (%s%ld during startup)\n") |
557 | : _("Space used: %ld (%s%ld for this command)\n"), | |
0f3bb72e | 558 | space_now, |
5d901a73 | 559 | (space_diff >= 0 ? "+" : ""), |
0f3bb72e PH |
560 | space_diff); |
561 | #endif | |
562 | } | |
563 | } | |
564 | ||
565 | /* Create a cleanup that reports time and space used since its | |
566 | creation. Precise messages depend on MSG_TYPE: | |
567 | 0: Initial time/space | |
568 | 1: Individual command time/space. */ | |
569 | struct cleanup * | |
570 | make_command_stats_cleanup (int msg_type) | |
571 | { | |
572 | struct cmd_stats *new_stat = XMALLOC (struct cmd_stats); | |
573 | ||
574 | #ifdef HAVE_SBRK | |
575 | char *lim = (char *) sbrk (0); | |
576 | new_stat->start_space = lim - lim_at_start; | |
577 | #endif | |
578 | ||
579 | new_stat->msg_type = msg_type; | |
0a1c4d10 DE |
580 | new_stat->start_cpu_time = get_run_time (); |
581 | gettimeofday (&new_stat->start_wall_time, NULL); | |
0f3bb72e PH |
582 | |
583 | return make_cleanup_dtor (report_command_stats, new_stat, xfree); | |
584 | } | |
c906108c | 585 | \f |
c5aa993b | 586 | |
8731e58e | 587 | |
f5a96129 AC |
588 | /* Print a warning message. The first argument STRING is the warning |
589 | message, used as an fprintf format string, the second is the | |
590 | va_list of arguments for that string. A warning is unfiltered (not | |
591 | paginated) so that the user does not need to page through each | |
592 | screen full of warnings when there are lots of them. */ | |
c906108c SS |
593 | |
594 | void | |
f5a96129 | 595 | vwarning (const char *string, va_list args) |
c906108c | 596 | { |
9a4105ab AC |
597 | if (deprecated_warning_hook) |
598 | (*deprecated_warning_hook) (string, args); | |
f5a96129 AC |
599 | else |
600 | { | |
601 | target_terminal_ours (); | |
581e13c1 | 602 | wrap_here (""); /* Force out any buffered output. */ |
f5a96129 AC |
603 | gdb_flush (gdb_stdout); |
604 | if (warning_pre_print) | |
306d9ac5 | 605 | fputs_unfiltered (warning_pre_print, gdb_stderr); |
f5a96129 AC |
606 | vfprintf_unfiltered (gdb_stderr, string, args); |
607 | fprintf_unfiltered (gdb_stderr, "\n"); | |
608 | va_end (args); | |
609 | } | |
c906108c SS |
610 | } |
611 | ||
612 | /* Print a warning message. | |
613 | The first argument STRING is the warning message, used as a fprintf string, | |
614 | and the remaining args are passed as arguments to it. | |
615 | The primary difference between warnings and errors is that a warning | |
616 | does not force the return to command level. */ | |
617 | ||
c906108c | 618 | void |
8731e58e | 619 | warning (const char *string, ...) |
c906108c SS |
620 | { |
621 | va_list args; | |
e0627e85 | 622 | |
c906108c | 623 | va_start (args, string); |
f5a96129 AC |
624 | vwarning (string, args); |
625 | va_end (args); | |
c906108c SS |
626 | } |
627 | ||
c906108c SS |
628 | /* Print an error message and return to command level. |
629 | The first argument STRING is the error message, used as a fprintf string, | |
630 | and the remaining args are passed as arguments to it. */ | |
631 | ||
c25c4a8b | 632 | void |
4ce44c66 JM |
633 | verror (const char *string, va_list args) |
634 | { | |
6b1b7650 | 635 | throw_verror (GENERIC_ERROR, string, args); |
4ce44c66 JM |
636 | } |
637 | ||
c25c4a8b | 638 | void |
8731e58e | 639 | error (const char *string, ...) |
c906108c SS |
640 | { |
641 | va_list args; | |
e0627e85 | 642 | |
c906108c | 643 | va_start (args, string); |
6b1b7650 | 644 | throw_verror (GENERIC_ERROR, string, args); |
4ce44c66 | 645 | va_end (args); |
c906108c SS |
646 | } |
647 | ||
d75e3c94 JJ |
648 | /* Print an error message and quit. |
649 | The first argument STRING is the error message, used as a fprintf string, | |
650 | and the remaining args are passed as arguments to it. */ | |
651 | ||
c25c4a8b | 652 | void |
d75e3c94 JJ |
653 | vfatal (const char *string, va_list args) |
654 | { | |
6b1b7650 | 655 | throw_vfatal (string, args); |
d75e3c94 JJ |
656 | } |
657 | ||
c25c4a8b | 658 | void |
d75e3c94 JJ |
659 | fatal (const char *string, ...) |
660 | { | |
661 | va_list args; | |
e0627e85 | 662 | |
d75e3c94 | 663 | va_start (args, string); |
6b1b7650 | 664 | throw_vfatal (string, args); |
d75e3c94 JJ |
665 | va_end (args); |
666 | } | |
667 | ||
c25c4a8b | 668 | void |
d75e3c94 | 669 | error_stream (struct ui_file *stream) |
2acceee2 | 670 | { |
759ef836 | 671 | char *message = ui_file_xstrdup (stream, NULL); |
e0627e85 | 672 | |
6b1b7650 | 673 | make_cleanup (xfree, message); |
8a3fe4f8 | 674 | error (("%s"), message); |
2acceee2 | 675 | } |
c906108c | 676 | |
7991dee7 JK |
677 | /* Dump core trying to increase the core soft limit to hard limit first. */ |
678 | ||
679 | static void | |
680 | dump_core (void) | |
681 | { | |
682 | #ifdef HAVE_SETRLIMIT | |
683 | struct rlimit rlim = { RLIM_INFINITY, RLIM_INFINITY }; | |
684 | ||
685 | setrlimit (RLIMIT_CORE, &rlim); | |
686 | #endif /* HAVE_SETRLIMIT */ | |
687 | ||
688 | abort (); /* NOTE: GDB has only three calls to abort(). */ | |
689 | } | |
690 | ||
3e43a32a MS |
691 | /* Check whether GDB will be able to dump core using the dump_core |
692 | function. */ | |
7991dee7 JK |
693 | |
694 | static int | |
695 | can_dump_core (const char *reason) | |
696 | { | |
697 | #ifdef HAVE_GETRLIMIT | |
698 | struct rlimit rlim; | |
699 | ||
700 | /* Be quiet and assume we can dump if an error is returned. */ | |
701 | if (getrlimit (RLIMIT_CORE, &rlim) != 0) | |
702 | return 1; | |
703 | ||
704 | if (rlim.rlim_max == 0) | |
705 | { | |
706 | fprintf_unfiltered (gdb_stderr, | |
3e43a32a MS |
707 | _("%s\nUnable to dump core, use `ulimit -c" |
708 | " unlimited' before executing GDB next time.\n"), | |
709 | reason); | |
7991dee7 JK |
710 | return 0; |
711 | } | |
712 | #endif /* HAVE_GETRLIMIT */ | |
713 | ||
714 | return 1; | |
715 | } | |
716 | ||
3c16cced PA |
717 | /* Allow the user to configure the debugger behavior with respect to |
718 | what to do when an internal problem is detected. */ | |
719 | ||
720 | const char internal_problem_ask[] = "ask"; | |
721 | const char internal_problem_yes[] = "yes"; | |
722 | const char internal_problem_no[] = "no"; | |
40478521 | 723 | static const char *const internal_problem_modes[] = |
3c16cced PA |
724 | { |
725 | internal_problem_ask, | |
726 | internal_problem_yes, | |
727 | internal_problem_no, | |
728 | NULL | |
729 | }; | |
3c16cced | 730 | |
581e13c1 | 731 | /* Print a message reporting an internal error/warning. Ask the user |
dec43320 AC |
732 | if they want to continue, dump core, or just exit. Return |
733 | something to indicate a quit. */ | |
c906108c | 734 | |
dec43320 | 735 | struct internal_problem |
c906108c | 736 | { |
dec43320 | 737 | const char *name; |
3c16cced PA |
738 | const char *should_quit; |
739 | const char *should_dump_core; | |
dec43320 AC |
740 | }; |
741 | ||
742 | /* Report a problem, internal to GDB, to the user. Once the problem | |
743 | has been reported, and assuming GDB didn't quit, the caller can | |
744 | either allow execution to resume or throw an error. */ | |
745 | ||
a0b31db1 | 746 | static void ATTRIBUTE_PRINTF (4, 0) |
dec43320 | 747 | internal_vproblem (struct internal_problem *problem, |
8731e58e | 748 | const char *file, int line, const char *fmt, va_list ap) |
dec43320 | 749 | { |
dec43320 | 750 | static int dejavu; |
375fc983 | 751 | int quit_p; |
7be570e7 | 752 | int dump_core_p; |
714b1282 | 753 | char *reason; |
c906108c | 754 | |
dec43320 | 755 | /* Don't allow infinite error/warning recursion. */ |
714b1282 AC |
756 | { |
757 | static char msg[] = "Recursive internal problem.\n"; | |
5d502164 | 758 | |
714b1282 AC |
759 | switch (dejavu) |
760 | { | |
761 | case 0: | |
762 | dejavu = 1; | |
763 | break; | |
764 | case 1: | |
765 | dejavu = 2; | |
766 | fputs_unfiltered (msg, gdb_stderr); | |
7991dee7 | 767 | abort (); /* NOTE: GDB has only three calls to abort(). */ |
714b1282 AC |
768 | default: |
769 | dejavu = 3; | |
bf1d7d9c JB |
770 | /* Newer GLIBC versions put the warn_unused_result attribute |
771 | on write, but this is one of those rare cases where | |
772 | ignoring the return value is correct. Casting to (void) | |
773 | does not fix this problem. This is the solution suggested | |
774 | at http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25509. */ | |
775 | if (write (STDERR_FILENO, msg, sizeof (msg)) != sizeof (msg)) | |
7991dee7 | 776 | abort (); /* NOTE: GDB has only three calls to abort(). */ |
714b1282 AC |
777 | exit (1); |
778 | } | |
779 | } | |
c906108c | 780 | |
dec43320 | 781 | /* Try to get the message out and at the start of a new line. */ |
4261bedc | 782 | target_terminal_ours (); |
dec43320 AC |
783 | begin_line (); |
784 | ||
714b1282 AC |
785 | /* Create a string containing the full error/warning message. Need |
786 | to call query with this full string, as otherwize the reason | |
787 | (error/warning) and question become separated. Format using a | |
788 | style similar to a compiler error message. Include extra detail | |
789 | so that the user knows that they are living on the edge. */ | |
790 | { | |
791 | char *msg; | |
5d502164 | 792 | |
e623b504 | 793 | msg = xstrvprintf (fmt, ap); |
3e43a32a MS |
794 | reason = xstrprintf ("%s:%d: %s: %s\n" |
795 | "A problem internal to GDB has been detected,\n" | |
796 | "further debugging may prove unreliable.", | |
797 | file, line, problem->name, msg); | |
714b1282 AC |
798 | xfree (msg); |
799 | make_cleanup (xfree, reason); | |
800 | } | |
7be570e7 | 801 | |
3c16cced | 802 | if (problem->should_quit == internal_problem_ask) |
dec43320 | 803 | { |
dec43320 | 804 | /* Default (yes/batch case) is to quit GDB. When in batch mode |
3c16cced PA |
805 | this lessens the likelihood of GDB going into an infinite |
806 | loop. */ | |
e360902b | 807 | if (!confirm) |
26bb68be PP |
808 | { |
809 | /* Emit the message and quit. */ | |
810 | fputs_unfiltered (reason, gdb_stderr); | |
811 | fputs_unfiltered ("\n", gdb_stderr); | |
812 | quit_p = 1; | |
813 | } | |
814 | else | |
815 | quit_p = query (_("%s\nQuit this debugging session? "), reason); | |
dec43320 | 816 | } |
3c16cced PA |
817 | else if (problem->should_quit == internal_problem_yes) |
818 | quit_p = 1; | |
819 | else if (problem->should_quit == internal_problem_no) | |
820 | quit_p = 0; | |
821 | else | |
822 | internal_error (__FILE__, __LINE__, _("bad switch")); | |
dec43320 | 823 | |
3c16cced | 824 | if (problem->should_dump_core == internal_problem_ask) |
dec43320 | 825 | { |
7991dee7 JK |
826 | if (!can_dump_core (reason)) |
827 | dump_core_p = 0; | |
828 | else | |
829 | { | |
830 | /* Default (yes/batch case) is to dump core. This leaves a GDB | |
831 | `dropping' so that it is easier to see that something went | |
832 | wrong in GDB. */ | |
833 | dump_core_p = query (_("%s\nCreate a core file of GDB? "), reason); | |
834 | } | |
dec43320 | 835 | } |
3c16cced | 836 | else if (problem->should_dump_core == internal_problem_yes) |
7991dee7 | 837 | dump_core_p = can_dump_core (reason); |
3c16cced PA |
838 | else if (problem->should_dump_core == internal_problem_no) |
839 | dump_core_p = 0; | |
840 | else | |
841 | internal_error (__FILE__, __LINE__, _("bad switch")); | |
7be570e7 | 842 | |
375fc983 | 843 | if (quit_p) |
7be570e7 JM |
844 | { |
845 | if (dump_core_p) | |
7991dee7 | 846 | dump_core (); |
375fc983 AC |
847 | else |
848 | exit (1); | |
7be570e7 JM |
849 | } |
850 | else | |
851 | { | |
852 | if (dump_core_p) | |
375fc983 | 853 | { |
9b265ec2 | 854 | #ifdef HAVE_WORKING_FORK |
375fc983 | 855 | if (fork () == 0) |
7991dee7 | 856 | dump_core (); |
9b265ec2 | 857 | #endif |
375fc983 | 858 | } |
7be570e7 | 859 | } |
96baa820 JM |
860 | |
861 | dejavu = 0; | |
dec43320 AC |
862 | } |
863 | ||
864 | static struct internal_problem internal_error_problem = { | |
3c16cced | 865 | "internal-error", internal_problem_ask, internal_problem_ask |
dec43320 AC |
866 | }; |
867 | ||
c25c4a8b | 868 | void |
8731e58e | 869 | internal_verror (const char *file, int line, const char *fmt, va_list ap) |
dec43320 AC |
870 | { |
871 | internal_vproblem (&internal_error_problem, file, line, fmt, ap); | |
315a522e | 872 | deprecated_throw_reason (RETURN_ERROR); |
c906108c SS |
873 | } |
874 | ||
c25c4a8b | 875 | void |
8e65ff28 | 876 | internal_error (const char *file, int line, const char *string, ...) |
4ce44c66 JM |
877 | { |
878 | va_list ap; | |
e0627e85 | 879 | |
4ce44c66 | 880 | va_start (ap, string); |
8e65ff28 | 881 | internal_verror (file, line, string, ap); |
4ce44c66 JM |
882 | va_end (ap); |
883 | } | |
884 | ||
dec43320 | 885 | static struct internal_problem internal_warning_problem = { |
3c16cced | 886 | "internal-warning", internal_problem_ask, internal_problem_ask |
dec43320 AC |
887 | }; |
888 | ||
889 | void | |
8731e58e | 890 | internal_vwarning (const char *file, int line, const char *fmt, va_list ap) |
dec43320 AC |
891 | { |
892 | internal_vproblem (&internal_warning_problem, file, line, fmt, ap); | |
893 | } | |
894 | ||
895 | void | |
896 | internal_warning (const char *file, int line, const char *string, ...) | |
897 | { | |
898 | va_list ap; | |
e0627e85 | 899 | |
dec43320 AC |
900 | va_start (ap, string); |
901 | internal_vwarning (file, line, string, ap); | |
902 | va_end (ap); | |
903 | } | |
904 | ||
3c16cced PA |
905 | /* Dummy functions to keep add_prefix_cmd happy. */ |
906 | ||
907 | static void | |
908 | set_internal_problem_cmd (char *args, int from_tty) | |
909 | { | |
910 | } | |
911 | ||
912 | static void | |
913 | show_internal_problem_cmd (char *args, int from_tty) | |
914 | { | |
915 | } | |
916 | ||
917 | /* When GDB reports an internal problem (error or warning) it gives | |
918 | the user the opportunity to quit GDB and/or create a core file of | |
919 | the current debug session. This function registers a few commands | |
920 | that make it possible to specify that GDB should always or never | |
921 | quit or create a core file, without asking. The commands look | |
922 | like: | |
923 | ||
924 | maint set PROBLEM-NAME quit ask|yes|no | |
925 | maint show PROBLEM-NAME quit | |
926 | maint set PROBLEM-NAME corefile ask|yes|no | |
927 | maint show PROBLEM-NAME corefile | |
928 | ||
929 | Where PROBLEM-NAME is currently "internal-error" or | |
930 | "internal-warning". */ | |
931 | ||
932 | static void | |
933 | add_internal_problem_command (struct internal_problem *problem) | |
934 | { | |
935 | struct cmd_list_element **set_cmd_list; | |
936 | struct cmd_list_element **show_cmd_list; | |
937 | char *set_doc; | |
938 | char *show_doc; | |
939 | ||
940 | set_cmd_list = xmalloc (sizeof (*set_cmd_list)); | |
941 | show_cmd_list = xmalloc (sizeof (*set_cmd_list)); | |
942 | *set_cmd_list = NULL; | |
943 | *show_cmd_list = NULL; | |
944 | ||
945 | set_doc = xstrprintf (_("Configure what GDB does when %s is detected."), | |
946 | problem->name); | |
947 | ||
948 | show_doc = xstrprintf (_("Show what GDB does when %s is detected."), | |
949 | problem->name); | |
950 | ||
951 | add_prefix_cmd ((char*) problem->name, | |
952 | class_maintenance, set_internal_problem_cmd, set_doc, | |
953 | set_cmd_list, | |
c4f7c687 MK |
954 | concat ("maintenance set ", problem->name, " ", |
955 | (char *) NULL), | |
3c16cced PA |
956 | 0/*allow-unknown*/, &maintenance_set_cmdlist); |
957 | ||
958 | add_prefix_cmd ((char*) problem->name, | |
959 | class_maintenance, show_internal_problem_cmd, show_doc, | |
960 | show_cmd_list, | |
c4f7c687 MK |
961 | concat ("maintenance show ", problem->name, " ", |
962 | (char *) NULL), | |
3c16cced PA |
963 | 0/*allow-unknown*/, &maintenance_show_cmdlist); |
964 | ||
3e43a32a MS |
965 | set_doc = xstrprintf (_("Set whether GDB should quit " |
966 | "when an %s is detected"), | |
3c16cced | 967 | problem->name); |
3e43a32a MS |
968 | show_doc = xstrprintf (_("Show whether GDB will quit " |
969 | "when an %s is detected"), | |
3c16cced PA |
970 | problem->name); |
971 | add_setshow_enum_cmd ("quit", class_maintenance, | |
972 | internal_problem_modes, | |
973 | &problem->should_quit, | |
974 | set_doc, | |
975 | show_doc, | |
976 | NULL, /* help_doc */ | |
977 | NULL, /* setfunc */ | |
978 | NULL, /* showfunc */ | |
979 | set_cmd_list, | |
980 | show_cmd_list); | |
981 | ||
1eefb858 TT |
982 | xfree (set_doc); |
983 | xfree (show_doc); | |
984 | ||
3e43a32a MS |
985 | set_doc = xstrprintf (_("Set whether GDB should create a core " |
986 | "file of GDB when %s is detected"), | |
3c16cced | 987 | problem->name); |
3e43a32a MS |
988 | show_doc = xstrprintf (_("Show whether GDB will create a core " |
989 | "file of GDB when %s is detected"), | |
3c16cced PA |
990 | problem->name); |
991 | add_setshow_enum_cmd ("corefile", class_maintenance, | |
992 | internal_problem_modes, | |
993 | &problem->should_dump_core, | |
994 | set_doc, | |
995 | show_doc, | |
996 | NULL, /* help_doc */ | |
997 | NULL, /* setfunc */ | |
998 | NULL, /* showfunc */ | |
999 | set_cmd_list, | |
1000 | show_cmd_list); | |
1eefb858 TT |
1001 | |
1002 | xfree (set_doc); | |
1003 | xfree (show_doc); | |
3c16cced PA |
1004 | } |
1005 | ||
c906108c SS |
1006 | /* Print the system error message for errno, and also mention STRING |
1007 | as the file name for which the error was encountered. | |
1008 | Then return to command level. */ | |
1009 | ||
c25c4a8b | 1010 | void |
6972bc8b | 1011 | perror_with_name (const char *string) |
c906108c SS |
1012 | { |
1013 | char *err; | |
1014 | char *combined; | |
1015 | ||
1016 | err = safe_strerror (errno); | |
1017 | combined = (char *) alloca (strlen (err) + strlen (string) + 3); | |
1018 | strcpy (combined, string); | |
1019 | strcat (combined, ": "); | |
1020 | strcat (combined, err); | |
1021 | ||
1022 | /* I understand setting these is a matter of taste. Still, some people | |
1023 | may clear errno but not know about bfd_error. Doing this here is not | |
581e13c1 | 1024 | unreasonable. */ |
c906108c SS |
1025 | bfd_set_error (bfd_error_no_error); |
1026 | errno = 0; | |
1027 | ||
8a3fe4f8 | 1028 | error (_("%s."), combined); |
c906108c SS |
1029 | } |
1030 | ||
1031 | /* Print the system error message for ERRCODE, and also mention STRING | |
1032 | as the file name for which the error was encountered. */ | |
1033 | ||
1034 | void | |
6972bc8b | 1035 | print_sys_errmsg (const char *string, int errcode) |
c906108c SS |
1036 | { |
1037 | char *err; | |
1038 | char *combined; | |
1039 | ||
1040 | err = safe_strerror (errcode); | |
1041 | combined = (char *) alloca (strlen (err) + strlen (string) + 3); | |
1042 | strcpy (combined, string); | |
1043 | strcat (combined, ": "); | |
1044 | strcat (combined, err); | |
1045 | ||
1046 | /* We want anything which was printed on stdout to come out first, before | |
1047 | this message. */ | |
1048 | gdb_flush (gdb_stdout); | |
1049 | fprintf_unfiltered (gdb_stderr, "%s.\n", combined); | |
1050 | } | |
1051 | ||
1052 | /* Control C eventually causes this to be called, at a convenient time. */ | |
1053 | ||
1054 | void | |
fba45db2 | 1055 | quit (void) |
c906108c | 1056 | { |
7be570e7 JM |
1057 | #ifdef __MSDOS__ |
1058 | /* No steenking SIGINT will ever be coming our way when the | |
1059 | program is resumed. Don't lie. */ | |
e06e2353 | 1060 | fatal ("Quit"); |
7be570e7 | 1061 | #else |
c906108c | 1062 | if (job_control |
8731e58e AC |
1063 | /* If there is no terminal switching for this target, then we can't |
1064 | possibly get screwed by the lack of job control. */ | |
c906108c | 1065 | || current_target.to_terminal_ours == NULL) |
e06e2353 | 1066 | fatal ("Quit"); |
c906108c | 1067 | else |
e06e2353 | 1068 | fatal ("Quit (expect signal SIGINT when the program is resumed)"); |
7be570e7 | 1069 | #endif |
c906108c SS |
1070 | } |
1071 | ||
c906108c | 1072 | \f |
c906108c | 1073 | /* Called when a memory allocation fails, with the number of bytes of |
581e13c1 | 1074 | memory requested in SIZE. */ |
c906108c | 1075 | |
c25c4a8b | 1076 | void |
d26e3629 | 1077 | malloc_failure (long size) |
c906108c SS |
1078 | { |
1079 | if (size > 0) | |
1080 | { | |
8e65ff28 | 1081 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 1082 | _("virtual memory exhausted: can't allocate %ld bytes."), |
8731e58e | 1083 | size); |
c906108c SS |
1084 | } |
1085 | else | |
1086 | { | |
e2e0b3e5 | 1087 | internal_error (__FILE__, __LINE__, _("virtual memory exhausted.")); |
c906108c SS |
1088 | } |
1089 | } | |
1090 | ||
c906108c SS |
1091 | /* My replacement for the read system call. |
1092 | Used like `read' but keeps going if `read' returns too soon. */ | |
1093 | ||
1094 | int | |
fba45db2 | 1095 | myread (int desc, char *addr, int len) |
c906108c | 1096 | { |
52f0bd74 | 1097 | int val; |
c906108c SS |
1098 | int orglen = len; |
1099 | ||
1100 | while (len > 0) | |
1101 | { | |
1102 | val = read (desc, addr, len); | |
1103 | if (val < 0) | |
1104 | return val; | |
1105 | if (val == 0) | |
1106 | return orglen - len; | |
1107 | len -= val; | |
1108 | addr += val; | |
1109 | } | |
1110 | return orglen; | |
1111 | } | |
d26e3629 | 1112 | |
c906108c SS |
1113 | /* Make a copy of the string at PTR with SIZE characters |
1114 | (and add a null character at the end in the copy). | |
1115 | Uses malloc to get the space. Returns the address of the copy. */ | |
1116 | ||
1117 | char * | |
5565b556 | 1118 | savestring (const char *ptr, size_t size) |
c906108c | 1119 | { |
52f0bd74 | 1120 | char *p = (char *) xmalloc (size + 1); |
e0627e85 | 1121 | |
c906108c SS |
1122 | memcpy (p, ptr, size); |
1123 | p[size] = 0; | |
1124 | return p; | |
1125 | } | |
1126 | ||
c906108c | 1127 | void |
aa1ee363 | 1128 | print_spaces (int n, struct ui_file *file) |
c906108c | 1129 | { |
392a587b | 1130 | fputs_unfiltered (n_spaces (n), file); |
c906108c SS |
1131 | } |
1132 | ||
1133 | /* Print a host address. */ | |
1134 | ||
1135 | void | |
ac16bf07 | 1136 | gdb_print_host_address (const void *addr, struct ui_file *stream) |
c906108c | 1137 | { |
ea8992ce | 1138 | fprintf_filtered (stream, "%s", host_address_to_string (addr)); |
c906108c | 1139 | } |
c906108c | 1140 | \f |
c5aa993b | 1141 | |
dc92e161 TT |
1142 | /* A cleanup function that calls regfree. */ |
1143 | ||
1144 | static void | |
1145 | do_regfree_cleanup (void *r) | |
1146 | { | |
1147 | regfree (r); | |
1148 | } | |
1149 | ||
1150 | /* Create a new cleanup that frees the compiled regular expression R. */ | |
1151 | ||
1152 | struct cleanup * | |
1153 | make_regfree_cleanup (regex_t *r) | |
1154 | { | |
1155 | return make_cleanup (do_regfree_cleanup, r); | |
1156 | } | |
1157 | ||
1158 | /* Return an xmalloc'd error message resulting from a regular | |
1159 | expression compilation failure. */ | |
1160 | ||
1161 | char * | |
1162 | get_regcomp_error (int code, regex_t *rx) | |
1163 | { | |
1164 | size_t length = regerror (code, rx, NULL, 0); | |
1165 | char *result = xmalloc (length); | |
1166 | ||
1167 | regerror (code, rx, result, length); | |
1168 | return result; | |
1169 | } | |
1170 | ||
1171 | \f | |
1172 | ||
981c7f5a | 1173 | /* This function supports the query, nquery, and yquery functions. |
cbdeadca | 1174 | Ask user a y-or-n question and return 0 if answer is no, 1 if |
981c7f5a DJ |
1175 | answer is yes, or default the answer to the specified default |
1176 | (for yquery or nquery). DEFCHAR may be 'y' or 'n' to provide a | |
1177 | default answer, or '\0' for no default. | |
cbdeadca JJ |
1178 | CTLSTR is the control string and should end in "? ". It should |
1179 | not say how to answer, because we do that. | |
1180 | ARGS are the arguments passed along with the CTLSTR argument to | |
1181 | printf. */ | |
1182 | ||
a0b31db1 | 1183 | static int ATTRIBUTE_PRINTF (1, 0) |
cbdeadca JJ |
1184 | defaulted_query (const char *ctlstr, const char defchar, va_list args) |
1185 | { | |
1186 | int answer; | |
1187 | int ans2; | |
1188 | int retval; | |
1189 | int def_value; | |
1190 | char def_answer, not_def_answer; | |
981c7f5a | 1191 | char *y_string, *n_string, *question; |
cbdeadca JJ |
1192 | |
1193 | /* Set up according to which answer is the default. */ | |
981c7f5a DJ |
1194 | if (defchar == '\0') |
1195 | { | |
1196 | def_value = 1; | |
1197 | def_answer = 'Y'; | |
1198 | not_def_answer = 'N'; | |
1199 | y_string = "y"; | |
1200 | n_string = "n"; | |
1201 | } | |
1202 | else if (defchar == 'y') | |
cbdeadca JJ |
1203 | { |
1204 | def_value = 1; | |
1205 | def_answer = 'Y'; | |
1206 | not_def_answer = 'N'; | |
1207 | y_string = "[y]"; | |
1208 | n_string = "n"; | |
1209 | } | |
1210 | else | |
1211 | { | |
1212 | def_value = 0; | |
1213 | def_answer = 'N'; | |
1214 | not_def_answer = 'Y'; | |
1215 | y_string = "y"; | |
1216 | n_string = "[n]"; | |
1217 | } | |
1218 | ||
981c7f5a | 1219 | /* Automatically answer the default value if the user did not want |
a502cf95 | 1220 | prompts or the command was issued with the server prefix. */ |
e360902b | 1221 | if (!confirm || server_command) |
981c7f5a DJ |
1222 | return def_value; |
1223 | ||
1224 | /* If input isn't coming from the user directly, just say what | |
7a01c6e0 | 1225 | question we're asking, and then answer the default automatically. This |
981c7f5a DJ |
1226 | way, important error messages don't get lost when talking to GDB |
1227 | over a pipe. */ | |
c63a1f86 | 1228 | if (! input_from_terminal_p ()) |
981c7f5a DJ |
1229 | { |
1230 | wrap_here (""); | |
1231 | vfprintf_filtered (gdb_stdout, ctlstr, args); | |
1232 | ||
3e43a32a MS |
1233 | printf_filtered (_("(%s or %s) [answered %c; " |
1234 | "input not from terminal]\n"), | |
981c7f5a DJ |
1235 | y_string, n_string, def_answer); |
1236 | gdb_flush (gdb_stdout); | |
1237 | ||
1238 | return def_value; | |
1239 | } | |
1240 | ||
9a4105ab | 1241 | if (deprecated_query_hook) |
cbdeadca | 1242 | { |
9a4105ab | 1243 | return deprecated_query_hook (ctlstr, args); |
cbdeadca JJ |
1244 | } |
1245 | ||
981c7f5a DJ |
1246 | /* Format the question outside of the loop, to avoid reusing args. */ |
1247 | question = xstrvprintf (ctlstr, args); | |
1248 | ||
cbdeadca JJ |
1249 | while (1) |
1250 | { | |
581e13c1 | 1251 | wrap_here (""); /* Flush any buffered output. */ |
cbdeadca JJ |
1252 | gdb_flush (gdb_stdout); |
1253 | ||
1254 | if (annotation_level > 1) | |
a3f17187 | 1255 | printf_filtered (("\n\032\032pre-query\n")); |
cbdeadca | 1256 | |
981c7f5a | 1257 | fputs_filtered (question, gdb_stdout); |
a3f17187 | 1258 | printf_filtered (_("(%s or %s) "), y_string, n_string); |
cbdeadca JJ |
1259 | |
1260 | if (annotation_level > 1) | |
a3f17187 | 1261 | printf_filtered (("\n\032\032query\n")); |
cbdeadca JJ |
1262 | |
1263 | wrap_here (""); | |
1264 | gdb_flush (gdb_stdout); | |
1265 | ||
1266 | answer = fgetc (stdin); | |
8626589c JB |
1267 | |
1268 | /* We expect fgetc to block until a character is read. But | |
1269 | this may not be the case if the terminal was opened with | |
1270 | the NONBLOCK flag. In that case, if there is nothing to | |
1271 | read on stdin, fgetc returns EOF, but also sets the error | |
1272 | condition flag on stdin and errno to EAGAIN. With a true | |
1273 | EOF, stdin's error condition flag is not set. | |
1274 | ||
1275 | A situation where this behavior was observed is a pseudo | |
1276 | terminal on AIX. */ | |
1277 | while (answer == EOF && ferror (stdin) && errno == EAGAIN) | |
1278 | { | |
1279 | /* Not a real EOF. Wait a little while and try again until | |
1280 | we read something. */ | |
1281 | clearerr (stdin); | |
1282 | gdb_usleep (10000); | |
1283 | answer = fgetc (stdin); | |
1284 | } | |
1285 | ||
cbdeadca JJ |
1286 | clearerr (stdin); /* in case of C-d */ |
1287 | if (answer == EOF) /* C-d */ | |
1288 | { | |
fa3fd85b | 1289 | printf_filtered ("EOF [assumed %c]\n", def_answer); |
cbdeadca JJ |
1290 | retval = def_value; |
1291 | break; | |
1292 | } | |
581e13c1 | 1293 | /* Eat rest of input line, to EOF or newline. */ |
cbdeadca JJ |
1294 | if (answer != '\n') |
1295 | do | |
1296 | { | |
1297 | ans2 = fgetc (stdin); | |
1298 | clearerr (stdin); | |
1299 | } | |
1300 | while (ans2 != EOF && ans2 != '\n' && ans2 != '\r'); | |
1301 | ||
1302 | if (answer >= 'a') | |
1303 | answer -= 040; | |
1304 | /* Check answer. For the non-default, the user must specify | |
1305 | the non-default explicitly. */ | |
1306 | if (answer == not_def_answer) | |
1307 | { | |
1308 | retval = !def_value; | |
1309 | break; | |
1310 | } | |
981c7f5a DJ |
1311 | /* Otherwise, if a default was specified, the user may either |
1312 | specify the required input or have it default by entering | |
1313 | nothing. */ | |
1314 | if (answer == def_answer | |
1315 | || (defchar != '\0' && | |
1316 | (answer == '\n' || answer == '\r' || answer == EOF))) | |
cbdeadca JJ |
1317 | { |
1318 | retval = def_value; | |
1319 | break; | |
1320 | } | |
1321 | /* Invalid entries are not defaulted and require another selection. */ | |
a3f17187 | 1322 | printf_filtered (_("Please answer %s or %s.\n"), |
cbdeadca JJ |
1323 | y_string, n_string); |
1324 | } | |
1325 | ||
981c7f5a | 1326 | xfree (question); |
cbdeadca | 1327 | if (annotation_level > 1) |
a3f17187 | 1328 | printf_filtered (("\n\032\032post-query\n")); |
cbdeadca JJ |
1329 | return retval; |
1330 | } | |
1331 | \f | |
1332 | ||
1333 | /* Ask user a y-or-n question and return 0 if answer is no, 1 if | |
1334 | answer is yes, or 0 if answer is defaulted. | |
1335 | Takes three args which are given to printf to print the question. | |
1336 | The first, a control string, should end in "? ". | |
1337 | It should not say how to answer, because we do that. */ | |
1338 | ||
1339 | int | |
1340 | nquery (const char *ctlstr, ...) | |
1341 | { | |
1342 | va_list args; | |
899500d6 | 1343 | int ret; |
cbdeadca JJ |
1344 | |
1345 | va_start (args, ctlstr); | |
899500d6 | 1346 | ret = defaulted_query (ctlstr, 'n', args); |
cbdeadca | 1347 | va_end (args); |
899500d6 | 1348 | return ret; |
cbdeadca JJ |
1349 | } |
1350 | ||
1351 | /* Ask user a y-or-n question and return 0 if answer is no, 1 if | |
1352 | answer is yes, or 1 if answer is defaulted. | |
1353 | Takes three args which are given to printf to print the question. | |
1354 | The first, a control string, should end in "? ". | |
1355 | It should not say how to answer, because we do that. */ | |
1356 | ||
1357 | int | |
1358 | yquery (const char *ctlstr, ...) | |
1359 | { | |
1360 | va_list args; | |
899500d6 | 1361 | int ret; |
cbdeadca JJ |
1362 | |
1363 | va_start (args, ctlstr); | |
899500d6 | 1364 | ret = defaulted_query (ctlstr, 'y', args); |
cbdeadca | 1365 | va_end (args); |
899500d6 | 1366 | return ret; |
cbdeadca JJ |
1367 | } |
1368 | ||
981c7f5a DJ |
1369 | /* Ask user a y-or-n question and return 1 iff answer is yes. |
1370 | Takes three args which are given to printf to print the question. | |
1371 | The first, a control string, should end in "? ". | |
1372 | It should not say how to answer, because we do that. */ | |
1373 | ||
1374 | int | |
1375 | query (const char *ctlstr, ...) | |
1376 | { | |
1377 | va_list args; | |
899500d6 | 1378 | int ret; |
981c7f5a DJ |
1379 | |
1380 | va_start (args, ctlstr); | |
899500d6 | 1381 | ret = defaulted_query (ctlstr, '\0', args); |
981c7f5a | 1382 | va_end (args); |
899500d6 | 1383 | return ret; |
981c7f5a DJ |
1384 | } |
1385 | ||
6c7a06a3 TT |
1386 | /* A helper for parse_escape that converts a host character to a |
1387 | target character. C is the host character. If conversion is | |
1388 | possible, then the target character is stored in *TARGET_C and the | |
1389 | function returns 1. Otherwise, the function returns 0. */ | |
1390 | ||
1391 | static int | |
f870a310 | 1392 | host_char_to_target (struct gdbarch *gdbarch, int c, int *target_c) |
234b45d4 | 1393 | { |
6c7a06a3 TT |
1394 | struct obstack host_data; |
1395 | char the_char = c; | |
1396 | struct cleanup *cleanups; | |
1397 | int result = 0; | |
234b45d4 | 1398 | |
6c7a06a3 TT |
1399 | obstack_init (&host_data); |
1400 | cleanups = make_cleanup_obstack_free (&host_data); | |
234b45d4 | 1401 | |
f870a310 | 1402 | convert_between_encodings (target_charset (gdbarch), host_charset (), |
6c7a06a3 TT |
1403 | &the_char, 1, 1, &host_data, translit_none); |
1404 | ||
1405 | if (obstack_object_size (&host_data) == 1) | |
1406 | { | |
1407 | result = 1; | |
1408 | *target_c = *(char *) obstack_base (&host_data); | |
1409 | } | |
1410 | ||
1411 | do_cleanups (cleanups); | |
1412 | return result; | |
234b45d4 KB |
1413 | } |
1414 | ||
c906108c SS |
1415 | /* Parse a C escape sequence. STRING_PTR points to a variable |
1416 | containing a pointer to the string to parse. That pointer | |
1417 | should point to the character after the \. That pointer | |
1418 | is updated past the characters we use. The value of the | |
1419 | escape sequence is returned. | |
1420 | ||
1421 | A negative value means the sequence \ newline was seen, | |
1422 | which is supposed to be equivalent to nothing at all. | |
1423 | ||
1424 | If \ is followed by a null character, we return a negative | |
1425 | value and leave the string pointer pointing at the null character. | |
1426 | ||
1427 | If \ is followed by 000, we return 0 and leave the string pointer | |
1428 | after the zeros. A value of 0 does not mean end of string. */ | |
1429 | ||
1430 | int | |
f870a310 | 1431 | parse_escape (struct gdbarch *gdbarch, char **string_ptr) |
c906108c | 1432 | { |
581e13c1 | 1433 | int target_char = -2; /* Initialize to avoid GCC warnings. */ |
52f0bd74 | 1434 | int c = *(*string_ptr)++; |
e0627e85 | 1435 | |
6c7a06a3 TT |
1436 | switch (c) |
1437 | { | |
8731e58e AC |
1438 | case '\n': |
1439 | return -2; | |
1440 | case 0: | |
1441 | (*string_ptr)--; | |
1442 | return 0; | |
8731e58e AC |
1443 | |
1444 | case '0': | |
1445 | case '1': | |
1446 | case '2': | |
1447 | case '3': | |
1448 | case '4': | |
1449 | case '5': | |
1450 | case '6': | |
1451 | case '7': | |
1452 | { | |
6c7a06a3 | 1453 | int i = host_hex_value (c); |
aa1ee363 | 1454 | int count = 0; |
8731e58e AC |
1455 | while (++count < 3) |
1456 | { | |
5cb316ef | 1457 | c = (**string_ptr); |
6c7a06a3 | 1458 | if (isdigit (c) && c != '8' && c != '9') |
8731e58e | 1459 | { |
5cb316ef | 1460 | (*string_ptr)++; |
8731e58e | 1461 | i *= 8; |
6c7a06a3 | 1462 | i += host_hex_value (c); |
8731e58e AC |
1463 | } |
1464 | else | |
1465 | { | |
8731e58e AC |
1466 | break; |
1467 | } | |
1468 | } | |
1469 | return i; | |
1470 | } | |
6c7a06a3 TT |
1471 | |
1472 | case 'a': | |
1473 | c = '\a'; | |
1474 | break; | |
1475 | case 'b': | |
1476 | c = '\b'; | |
1477 | break; | |
1478 | case 'f': | |
1479 | c = '\f'; | |
1480 | break; | |
1481 | case 'n': | |
1482 | c = '\n'; | |
1483 | break; | |
1484 | case 'r': | |
1485 | c = '\r'; | |
1486 | break; | |
1487 | case 't': | |
1488 | c = '\t'; | |
1489 | break; | |
1490 | case 'v': | |
1491 | c = '\v'; | |
1492 | break; | |
1493 | ||
1494 | default: | |
1495 | break; | |
1496 | } | |
1497 | ||
f870a310 | 1498 | if (!host_char_to_target (gdbarch, c, &target_char)) |
3351ea09 JB |
1499 | error (_("The escape sequence `\\%c' is equivalent to plain `%c'," |
1500 | " which has no equivalent\nin the `%s' character set."), | |
905b671b | 1501 | c, c, target_charset (gdbarch)); |
6c7a06a3 | 1502 | return target_char; |
c906108c SS |
1503 | } |
1504 | \f | |
1505 | /* Print the character C on STREAM as part of the contents of a literal | |
1506 | string whose delimiter is QUOTER. Note that this routine should only | |
1507 | be call for printing things which are independent of the language | |
581e13c1 | 1508 | of the program being debugged. */ |
c906108c | 1509 | |
43e526b9 | 1510 | static void |
74f832da | 1511 | printchar (int c, void (*do_fputs) (const char *, struct ui_file *), |
bee0189a DJ |
1512 | void (*do_fprintf) (struct ui_file *, const char *, ...) |
1513 | ATTRIBUTE_FPTR_PRINTF_2, struct ui_file *stream, int quoter) | |
c906108c | 1514 | { |
c906108c SS |
1515 | c &= 0xFF; /* Avoid sign bit follies */ |
1516 | ||
c5aa993b JM |
1517 | if (c < 0x20 || /* Low control chars */ |
1518 | (c >= 0x7F && c < 0xA0) || /* DEL, High controls */ | |
1519 | (sevenbit_strings && c >= 0x80)) | |
1520 | { /* high order bit set */ | |
1521 | switch (c) | |
1522 | { | |
1523 | case '\n': | |
43e526b9 | 1524 | do_fputs ("\\n", stream); |
c5aa993b JM |
1525 | break; |
1526 | case '\b': | |
43e526b9 | 1527 | do_fputs ("\\b", stream); |
c5aa993b JM |
1528 | break; |
1529 | case '\t': | |
43e526b9 | 1530 | do_fputs ("\\t", stream); |
c5aa993b JM |
1531 | break; |
1532 | case '\f': | |
43e526b9 | 1533 | do_fputs ("\\f", stream); |
c5aa993b JM |
1534 | break; |
1535 | case '\r': | |
43e526b9 | 1536 | do_fputs ("\\r", stream); |
c5aa993b JM |
1537 | break; |
1538 | case '\033': | |
43e526b9 | 1539 | do_fputs ("\\e", stream); |
c5aa993b JM |
1540 | break; |
1541 | case '\007': | |
43e526b9 | 1542 | do_fputs ("\\a", stream); |
c5aa993b JM |
1543 | break; |
1544 | default: | |
43e526b9 | 1545 | do_fprintf (stream, "\\%.3o", (unsigned int) c); |
c5aa993b JM |
1546 | break; |
1547 | } | |
1548 | } | |
1549 | else | |
1550 | { | |
1551 | if (c == '\\' || c == quoter) | |
43e526b9 JM |
1552 | do_fputs ("\\", stream); |
1553 | do_fprintf (stream, "%c", c); | |
c5aa993b | 1554 | } |
c906108c | 1555 | } |
43e526b9 JM |
1556 | |
1557 | /* Print the character C on STREAM as part of the contents of a | |
1558 | literal string whose delimiter is QUOTER. Note that these routines | |
1559 | should only be call for printing things which are independent of | |
581e13c1 | 1560 | the language of the program being debugged. */ |
43e526b9 JM |
1561 | |
1562 | void | |
fba45db2 | 1563 | fputstr_filtered (const char *str, int quoter, struct ui_file *stream) |
43e526b9 JM |
1564 | { |
1565 | while (*str) | |
1566 | printchar (*str++, fputs_filtered, fprintf_filtered, stream, quoter); | |
1567 | } | |
1568 | ||
1569 | void | |
fba45db2 | 1570 | fputstr_unfiltered (const char *str, int quoter, struct ui_file *stream) |
43e526b9 JM |
1571 | { |
1572 | while (*str) | |
1573 | printchar (*str++, fputs_unfiltered, fprintf_unfiltered, stream, quoter); | |
1574 | } | |
1575 | ||
0876f84a DJ |
1576 | void |
1577 | fputstrn_filtered (const char *str, int n, int quoter, | |
1578 | struct ui_file *stream) | |
1579 | { | |
1580 | int i; | |
e0627e85 | 1581 | |
0876f84a DJ |
1582 | for (i = 0; i < n; i++) |
1583 | printchar (str[i], fputs_filtered, fprintf_filtered, stream, quoter); | |
1584 | } | |
1585 | ||
43e526b9 | 1586 | void |
8731e58e AC |
1587 | fputstrn_unfiltered (const char *str, int n, int quoter, |
1588 | struct ui_file *stream) | |
43e526b9 JM |
1589 | { |
1590 | int i; | |
5d502164 | 1591 | |
43e526b9 JM |
1592 | for (i = 0; i < n; i++) |
1593 | printchar (str[i], fputs_unfiltered, fprintf_unfiltered, stream, quoter); | |
1594 | } | |
c906108c | 1595 | \f |
c5aa993b | 1596 | |
c906108c SS |
1597 | /* Number of lines per page or UINT_MAX if paging is disabled. */ |
1598 | static unsigned int lines_per_page; | |
920d2a44 AC |
1599 | static void |
1600 | show_lines_per_page (struct ui_file *file, int from_tty, | |
1601 | struct cmd_list_element *c, const char *value) | |
1602 | { | |
3e43a32a MS |
1603 | fprintf_filtered (file, |
1604 | _("Number of lines gdb thinks are in a page is %s.\n"), | |
920d2a44 AC |
1605 | value); |
1606 | } | |
eb0d3137 | 1607 | |
cbfbd72a | 1608 | /* Number of chars per line or UINT_MAX if line folding is disabled. */ |
c906108c | 1609 | static unsigned int chars_per_line; |
920d2a44 AC |
1610 | static void |
1611 | show_chars_per_line (struct ui_file *file, int from_tty, | |
1612 | struct cmd_list_element *c, const char *value) | |
1613 | { | |
3e43a32a MS |
1614 | fprintf_filtered (file, |
1615 | _("Number of characters gdb thinks " | |
1616 | "are in a line is %s.\n"), | |
920d2a44 AC |
1617 | value); |
1618 | } | |
eb0d3137 | 1619 | |
c906108c SS |
1620 | /* Current count of lines printed on this page, chars on this line. */ |
1621 | static unsigned int lines_printed, chars_printed; | |
1622 | ||
1623 | /* Buffer and start column of buffered text, for doing smarter word- | |
1624 | wrapping. When someone calls wrap_here(), we start buffering output | |
1625 | that comes through fputs_filtered(). If we see a newline, we just | |
1626 | spit it out and forget about the wrap_here(). If we see another | |
1627 | wrap_here(), we spit it out and remember the newer one. If we see | |
1628 | the end of the line, we spit out a newline, the indent, and then | |
1629 | the buffered output. */ | |
1630 | ||
1631 | /* Malloc'd buffer with chars_per_line+2 bytes. Contains characters which | |
1632 | are waiting to be output (they have already been counted in chars_printed). | |
1633 | When wrap_buffer[0] is null, the buffer is empty. */ | |
1634 | static char *wrap_buffer; | |
1635 | ||
1636 | /* Pointer in wrap_buffer to the next character to fill. */ | |
1637 | static char *wrap_pointer; | |
1638 | ||
1639 | /* String to indent by if the wrap occurs. Must not be NULL if wrap_column | |
1640 | is non-zero. */ | |
1641 | static char *wrap_indent; | |
1642 | ||
1643 | /* Column number on the screen where wrap_buffer begins, or 0 if wrapping | |
1644 | is not in effect. */ | |
1645 | static int wrap_column; | |
c906108c | 1646 | \f |
c5aa993b | 1647 | |
eb0d3137 MK |
1648 | /* Inialize the number of lines per page and chars per line. */ |
1649 | ||
c906108c | 1650 | void |
fba45db2 | 1651 | init_page_info (void) |
c906108c | 1652 | { |
5da1313b JK |
1653 | if (batch_flag) |
1654 | { | |
1655 | lines_per_page = UINT_MAX; | |
1656 | chars_per_line = UINT_MAX; | |
1657 | } | |
1658 | else | |
c906108c | 1659 | #if defined(TUI) |
5ecb1806 | 1660 | if (!tui_get_command_dimension (&chars_per_line, &lines_per_page)) |
c906108c SS |
1661 | #endif |
1662 | { | |
eb0d3137 | 1663 | int rows, cols; |
c906108c | 1664 | |
ec145965 EZ |
1665 | #if defined(__GO32__) |
1666 | rows = ScreenRows (); | |
1667 | cols = ScreenCols (); | |
1668 | lines_per_page = rows; | |
1669 | chars_per_line = cols; | |
1670 | #else | |
eb0d3137 MK |
1671 | /* Make sure Readline has initialized its terminal settings. */ |
1672 | rl_reset_terminal (NULL); | |
c906108c | 1673 | |
eb0d3137 MK |
1674 | /* Get the screen size from Readline. */ |
1675 | rl_get_screen_size (&rows, &cols); | |
1676 | lines_per_page = rows; | |
1677 | chars_per_line = cols; | |
c906108c | 1678 | |
eb0d3137 MK |
1679 | /* Readline should have fetched the termcap entry for us. */ |
1680 | if (tgetnum ("li") < 0 || getenv ("EMACS")) | |
1681 | { | |
1682 | /* The number of lines per page is not mentioned in the | |
1683 | terminal description. This probably means that paging is | |
1684 | not useful (e.g. emacs shell window), so disable paging. */ | |
1685 | lines_per_page = UINT_MAX; | |
1686 | } | |
c906108c | 1687 | |
eb0d3137 | 1688 | /* FIXME: Get rid of this junk. */ |
c906108c | 1689 | #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER) |
c906108c SS |
1690 | SIGWINCH_HANDLER (SIGWINCH); |
1691 | #endif | |
eb0d3137 | 1692 | |
c906108c | 1693 | /* If the output is not a terminal, don't paginate it. */ |
d9fcf2fb | 1694 | if (!ui_file_isatty (gdb_stdout)) |
c5aa993b | 1695 | lines_per_page = UINT_MAX; |
eb0d3137 | 1696 | #endif |
ec145965 | 1697 | } |
eb0d3137 MK |
1698 | |
1699 | set_screen_size (); | |
c5aa993b | 1700 | set_width (); |
c906108c SS |
1701 | } |
1702 | ||
5da1313b JK |
1703 | /* Helper for make_cleanup_restore_page_info. */ |
1704 | ||
1705 | static void | |
1706 | do_restore_page_info_cleanup (void *arg) | |
1707 | { | |
1708 | set_screen_size (); | |
1709 | set_width (); | |
1710 | } | |
1711 | ||
1712 | /* Provide cleanup for restoring the terminal size. */ | |
1713 | ||
1714 | struct cleanup * | |
1715 | make_cleanup_restore_page_info (void) | |
1716 | { | |
1717 | struct cleanup *back_to; | |
1718 | ||
1719 | back_to = make_cleanup (do_restore_page_info_cleanup, NULL); | |
1720 | make_cleanup_restore_uinteger (&lines_per_page); | |
1721 | make_cleanup_restore_uinteger (&chars_per_line); | |
1722 | ||
1723 | return back_to; | |
1724 | } | |
1725 | ||
1726 | /* Temporarily set BATCH_FLAG and the associated unlimited terminal size. | |
1727 | Provide cleanup for restoring the original state. */ | |
1728 | ||
1729 | struct cleanup * | |
1730 | set_batch_flag_and_make_cleanup_restore_page_info (void) | |
1731 | { | |
1732 | struct cleanup *back_to = make_cleanup_restore_page_info (); | |
1733 | ||
1734 | make_cleanup_restore_integer (&batch_flag); | |
1735 | batch_flag = 1; | |
1736 | init_page_info (); | |
1737 | ||
1738 | return back_to; | |
1739 | } | |
1740 | ||
eb0d3137 MK |
1741 | /* Set the screen size based on LINES_PER_PAGE and CHARS_PER_LINE. */ |
1742 | ||
1743 | static void | |
1744 | set_screen_size (void) | |
1745 | { | |
1746 | int rows = lines_per_page; | |
1747 | int cols = chars_per_line; | |
1748 | ||
1749 | if (rows <= 0) | |
1750 | rows = INT_MAX; | |
1751 | ||
1752 | if (cols <= 0) | |
0caa462c | 1753 | cols = INT_MAX; |
eb0d3137 MK |
1754 | |
1755 | /* Update Readline's idea of the terminal size. */ | |
1756 | rl_set_screen_size (rows, cols); | |
1757 | } | |
1758 | ||
1759 | /* Reinitialize WRAP_BUFFER according to the current value of | |
1760 | CHARS_PER_LINE. */ | |
1761 | ||
c906108c | 1762 | static void |
fba45db2 | 1763 | set_width (void) |
c906108c SS |
1764 | { |
1765 | if (chars_per_line == 0) | |
c5aa993b | 1766 | init_page_info (); |
c906108c SS |
1767 | |
1768 | if (!wrap_buffer) | |
1769 | { | |
1770 | wrap_buffer = (char *) xmalloc (chars_per_line + 2); | |
1771 | wrap_buffer[0] = '\0'; | |
1772 | } | |
1773 | else | |
1774 | wrap_buffer = (char *) xrealloc (wrap_buffer, chars_per_line + 2); | |
eb0d3137 | 1775 | wrap_pointer = wrap_buffer; /* Start it at the beginning. */ |
c906108c SS |
1776 | } |
1777 | ||
c5aa993b | 1778 | static void |
fba45db2 | 1779 | set_width_command (char *args, int from_tty, struct cmd_list_element *c) |
c906108c | 1780 | { |
eb0d3137 | 1781 | set_screen_size (); |
c906108c SS |
1782 | set_width (); |
1783 | } | |
1784 | ||
eb0d3137 MK |
1785 | static void |
1786 | set_height_command (char *args, int from_tty, struct cmd_list_element *c) | |
1787 | { | |
1788 | set_screen_size (); | |
1789 | } | |
1790 | ||
c906108c SS |
1791 | /* Wait, so the user can read what's on the screen. Prompt the user |
1792 | to continue by pressing RETURN. */ | |
1793 | ||
1794 | static void | |
fba45db2 | 1795 | prompt_for_continue (void) |
c906108c SS |
1796 | { |
1797 | char *ignore; | |
1798 | char cont_prompt[120]; | |
1799 | ||
1800 | if (annotation_level > 1) | |
a3f17187 | 1801 | printf_unfiltered (("\n\032\032pre-prompt-for-continue\n")); |
c906108c SS |
1802 | |
1803 | strcpy (cont_prompt, | |
1804 | "---Type <return> to continue, or q <return> to quit---"); | |
1805 | if (annotation_level > 1) | |
1806 | strcat (cont_prompt, "\n\032\032prompt-for-continue\n"); | |
1807 | ||
1808 | /* We must do this *before* we call gdb_readline, else it will eventually | |
1809 | call us -- thinking that we're trying to print beyond the end of the | |
1810 | screen. */ | |
1811 | reinitialize_more_filter (); | |
1812 | ||
1813 | immediate_quit++; | |
1814 | /* On a real operating system, the user can quit with SIGINT. | |
1815 | But not on GO32. | |
1816 | ||
1817 | 'q' is provided on all systems so users don't have to change habits | |
1818 | from system to system, and because telling them what to do in | |
1819 | the prompt is more user-friendly than expecting them to think of | |
1820 | SIGINT. */ | |
1821 | /* Call readline, not gdb_readline, because GO32 readline handles control-C | |
1822 | whereas control-C to gdb_readline will cause the user to get dumped | |
1823 | out to DOS. */ | |
b4f5539f | 1824 | ignore = gdb_readline_wrapper (cont_prompt); |
c906108c SS |
1825 | |
1826 | if (annotation_level > 1) | |
a3f17187 | 1827 | printf_unfiltered (("\n\032\032post-prompt-for-continue\n")); |
c906108c SS |
1828 | |
1829 | if (ignore) | |
1830 | { | |
1831 | char *p = ignore; | |
5d502164 | 1832 | |
c906108c SS |
1833 | while (*p == ' ' || *p == '\t') |
1834 | ++p; | |
1835 | if (p[0] == 'q') | |
362646f5 | 1836 | async_request_quit (0); |
b8c9b27d | 1837 | xfree (ignore); |
c906108c SS |
1838 | } |
1839 | immediate_quit--; | |
1840 | ||
1841 | /* Now we have to do this again, so that GDB will know that it doesn't | |
1842 | need to save the ---Type <return>--- line at the top of the screen. */ | |
1843 | reinitialize_more_filter (); | |
1844 | ||
581e13c1 | 1845 | dont_repeat (); /* Forget prev cmd -- CR won't repeat it. */ |
c906108c SS |
1846 | } |
1847 | ||
1848 | /* Reinitialize filter; ie. tell it to reset to original values. */ | |
1849 | ||
1850 | void | |
fba45db2 | 1851 | reinitialize_more_filter (void) |
c906108c SS |
1852 | { |
1853 | lines_printed = 0; | |
1854 | chars_printed = 0; | |
1855 | } | |
1856 | ||
1857 | /* Indicate that if the next sequence of characters overflows the line, | |
581e13c1 | 1858 | a newline should be inserted here rather than when it hits the end. |
c906108c SS |
1859 | If INDENT is non-null, it is a string to be printed to indent the |
1860 | wrapped part on the next line. INDENT must remain accessible until | |
1861 | the next call to wrap_here() or until a newline is printed through | |
1862 | fputs_filtered(). | |
1863 | ||
1864 | If the line is already overfull, we immediately print a newline and | |
1865 | the indentation, and disable further wrapping. | |
1866 | ||
1867 | If we don't know the width of lines, but we know the page height, | |
1868 | we must not wrap words, but should still keep track of newlines | |
1869 | that were explicitly printed. | |
1870 | ||
1871 | INDENT should not contain tabs, as that will mess up the char count | |
1872 | on the next line. FIXME. | |
1873 | ||
1874 | This routine is guaranteed to force out any output which has been | |
1875 | squirreled away in the wrap_buffer, so wrap_here ((char *)0) can be | |
1876 | used to force out output from the wrap_buffer. */ | |
1877 | ||
1878 | void | |
fba45db2 | 1879 | wrap_here (char *indent) |
c906108c | 1880 | { |
581e13c1 | 1881 | /* This should have been allocated, but be paranoid anyway. */ |
c906108c | 1882 | if (!wrap_buffer) |
3e43a32a MS |
1883 | internal_error (__FILE__, __LINE__, |
1884 | _("failed internal consistency check")); | |
c906108c SS |
1885 | |
1886 | if (wrap_buffer[0]) | |
1887 | { | |
1888 | *wrap_pointer = '\0'; | |
1889 | fputs_unfiltered (wrap_buffer, gdb_stdout); | |
1890 | } | |
1891 | wrap_pointer = wrap_buffer; | |
1892 | wrap_buffer[0] = '\0'; | |
3e43a32a | 1893 | if (chars_per_line == UINT_MAX) /* No line overflow checking. */ |
c906108c SS |
1894 | { |
1895 | wrap_column = 0; | |
1896 | } | |
1897 | else if (chars_printed >= chars_per_line) | |
1898 | { | |
1899 | puts_filtered ("\n"); | |
1900 | if (indent != NULL) | |
1901 | puts_filtered (indent); | |
1902 | wrap_column = 0; | |
1903 | } | |
1904 | else | |
1905 | { | |
1906 | wrap_column = chars_printed; | |
1907 | if (indent == NULL) | |
1908 | wrap_indent = ""; | |
1909 | else | |
1910 | wrap_indent = indent; | |
1911 | } | |
1912 | } | |
1913 | ||
4a351cef | 1914 | /* Print input string to gdb_stdout, filtered, with wrap, |
581e13c1 | 1915 | arranging strings in columns of n chars. String can be |
4a351cef AF |
1916 | right or left justified in the column. Never prints |
1917 | trailing spaces. String should never be longer than | |
1918 | width. FIXME: this could be useful for the EXAMINE | |
581e13c1 | 1919 | command, which currently doesn't tabulate very well. */ |
4a351cef AF |
1920 | |
1921 | void | |
1922 | puts_filtered_tabular (char *string, int width, int right) | |
1923 | { | |
1924 | int spaces = 0; | |
1925 | int stringlen; | |
1926 | char *spacebuf; | |
1927 | ||
1928 | gdb_assert (chars_per_line > 0); | |
1929 | if (chars_per_line == UINT_MAX) | |
1930 | { | |
1931 | fputs_filtered (string, gdb_stdout); | |
1932 | fputs_filtered ("\n", gdb_stdout); | |
1933 | return; | |
1934 | } | |
1935 | ||
1936 | if (((chars_printed - 1) / width + 2) * width >= chars_per_line) | |
1937 | fputs_filtered ("\n", gdb_stdout); | |
1938 | ||
1939 | if (width >= chars_per_line) | |
1940 | width = chars_per_line - 1; | |
1941 | ||
1942 | stringlen = strlen (string); | |
1943 | ||
1944 | if (chars_printed > 0) | |
1945 | spaces = width - (chars_printed - 1) % width - 1; | |
1946 | if (right) | |
1947 | spaces += width - stringlen; | |
1948 | ||
1949 | spacebuf = alloca (spaces + 1); | |
1950 | spacebuf[spaces] = '\0'; | |
1951 | while (spaces--) | |
1952 | spacebuf[spaces] = ' '; | |
1953 | ||
1954 | fputs_filtered (spacebuf, gdb_stdout); | |
1955 | fputs_filtered (string, gdb_stdout); | |
1956 | } | |
1957 | ||
1958 | ||
c906108c | 1959 | /* Ensure that whatever gets printed next, using the filtered output |
581e13c1 | 1960 | commands, starts at the beginning of the line. I.e. if there is |
c906108c | 1961 | any pending output for the current line, flush it and start a new |
581e13c1 | 1962 | line. Otherwise do nothing. */ |
c906108c SS |
1963 | |
1964 | void | |
fba45db2 | 1965 | begin_line (void) |
c906108c SS |
1966 | { |
1967 | if (chars_printed > 0) | |
1968 | { | |
1969 | puts_filtered ("\n"); | |
1970 | } | |
1971 | } | |
1972 | ||
ac9a91a7 | 1973 | |
c906108c SS |
1974 | /* Like fputs but if FILTER is true, pause after every screenful. |
1975 | ||
1976 | Regardless of FILTER can wrap at points other than the final | |
1977 | character of a line. | |
1978 | ||
1979 | Unlike fputs, fputs_maybe_filtered does not return a value. | |
1980 | It is OK for LINEBUFFER to be NULL, in which case just don't print | |
1981 | anything. | |
1982 | ||
1983 | Note that a longjmp to top level may occur in this routine (only if | |
1984 | FILTER is true) (since prompt_for_continue may do so) so this | |
1985 | routine should not be called when cleanups are not in place. */ | |
1986 | ||
1987 | static void | |
fba45db2 KB |
1988 | fputs_maybe_filtered (const char *linebuffer, struct ui_file *stream, |
1989 | int filter) | |
c906108c SS |
1990 | { |
1991 | const char *lineptr; | |
1992 | ||
1993 | if (linebuffer == 0) | |
1994 | return; | |
1995 | ||
1996 | /* Don't do any filtering if it is disabled. */ | |
390a8aca | 1997 | if (stream != gdb_stdout |
b2e7f004 JK |
1998 | || !pagination_enabled |
1999 | || batch_flag | |
390a8aca | 2000 | || (lines_per_page == UINT_MAX && chars_per_line == UINT_MAX) |
58dadb1b | 2001 | || top_level_interpreter () == NULL |
390a8aca | 2002 | || ui_out_is_mi_like_p (interp_ui_out (top_level_interpreter ()))) |
c906108c SS |
2003 | { |
2004 | fputs_unfiltered (linebuffer, stream); | |
2005 | return; | |
2006 | } | |
2007 | ||
2008 | /* Go through and output each character. Show line extension | |
2009 | when this is necessary; prompt user for new page when this is | |
2010 | necessary. */ | |
c5aa993b | 2011 | |
c906108c SS |
2012 | lineptr = linebuffer; |
2013 | while (*lineptr) | |
2014 | { | |
2015 | /* Possible new page. */ | |
8731e58e | 2016 | if (filter && (lines_printed >= lines_per_page - 1)) |
c906108c SS |
2017 | prompt_for_continue (); |
2018 | ||
2019 | while (*lineptr && *lineptr != '\n') | |
2020 | { | |
2021 | /* Print a single line. */ | |
2022 | if (*lineptr == '\t') | |
2023 | { | |
2024 | if (wrap_column) | |
2025 | *wrap_pointer++ = '\t'; | |
2026 | else | |
2027 | fputc_unfiltered ('\t', stream); | |
2028 | /* Shifting right by 3 produces the number of tab stops | |
2029 | we have already passed, and then adding one and | |
c5aa993b | 2030 | shifting left 3 advances to the next tab stop. */ |
c906108c SS |
2031 | chars_printed = ((chars_printed >> 3) + 1) << 3; |
2032 | lineptr++; | |
2033 | } | |
2034 | else | |
2035 | { | |
2036 | if (wrap_column) | |
2037 | *wrap_pointer++ = *lineptr; | |
2038 | else | |
c5aa993b | 2039 | fputc_unfiltered (*lineptr, stream); |
c906108c SS |
2040 | chars_printed++; |
2041 | lineptr++; | |
2042 | } | |
c5aa993b | 2043 | |
c906108c SS |
2044 | if (chars_printed >= chars_per_line) |
2045 | { | |
2046 | unsigned int save_chars = chars_printed; | |
2047 | ||
2048 | chars_printed = 0; | |
2049 | lines_printed++; | |
2050 | /* If we aren't actually wrapping, don't output newline -- | |
c5aa993b JM |
2051 | if chars_per_line is right, we probably just overflowed |
2052 | anyway; if it's wrong, let us keep going. */ | |
c906108c SS |
2053 | if (wrap_column) |
2054 | fputc_unfiltered ('\n', stream); | |
2055 | ||
2056 | /* Possible new page. */ | |
2057 | if (lines_printed >= lines_per_page - 1) | |
2058 | prompt_for_continue (); | |
2059 | ||
581e13c1 | 2060 | /* Now output indentation and wrapped string. */ |
c906108c SS |
2061 | if (wrap_column) |
2062 | { | |
2063 | fputs_unfiltered (wrap_indent, stream); | |
581e13c1 MS |
2064 | *wrap_pointer = '\0'; /* Null-terminate saved stuff, */ |
2065 | fputs_unfiltered (wrap_buffer, stream); /* and eject it. */ | |
c906108c SS |
2066 | /* FIXME, this strlen is what prevents wrap_indent from |
2067 | containing tabs. However, if we recurse to print it | |
2068 | and count its chars, we risk trouble if wrap_indent is | |
581e13c1 | 2069 | longer than (the user settable) chars_per_line. |
c906108c SS |
2070 | Note also that this can set chars_printed > chars_per_line |
2071 | if we are printing a long string. */ | |
2072 | chars_printed = strlen (wrap_indent) | |
c5aa993b | 2073 | + (save_chars - wrap_column); |
c906108c SS |
2074 | wrap_pointer = wrap_buffer; /* Reset buffer */ |
2075 | wrap_buffer[0] = '\0'; | |
c5aa993b JM |
2076 | wrap_column = 0; /* And disable fancy wrap */ |
2077 | } | |
c906108c SS |
2078 | } |
2079 | } | |
2080 | ||
2081 | if (*lineptr == '\n') | |
2082 | { | |
2083 | chars_printed = 0; | |
3e43a32a MS |
2084 | wrap_here ((char *) 0); /* Spit out chars, cancel |
2085 | further wraps. */ | |
c906108c SS |
2086 | lines_printed++; |
2087 | fputc_unfiltered ('\n', stream); | |
2088 | lineptr++; | |
2089 | } | |
2090 | } | |
2091 | } | |
2092 | ||
2093 | void | |
fba45db2 | 2094 | fputs_filtered (const char *linebuffer, struct ui_file *stream) |
c906108c SS |
2095 | { |
2096 | fputs_maybe_filtered (linebuffer, stream, 1); | |
2097 | } | |
2098 | ||
2099 | int | |
fba45db2 | 2100 | putchar_unfiltered (int c) |
c906108c | 2101 | { |
11cf8741 | 2102 | char buf = c; |
e0627e85 | 2103 | |
d9fcf2fb | 2104 | ui_file_write (gdb_stdout, &buf, 1); |
c906108c SS |
2105 | return c; |
2106 | } | |
2107 | ||
d1f4cff8 AC |
2108 | /* Write character C to gdb_stdout using GDB's paging mechanism and return C. |
2109 | May return nonlocally. */ | |
2110 | ||
2111 | int | |
2112 | putchar_filtered (int c) | |
2113 | { | |
2114 | return fputc_filtered (c, gdb_stdout); | |
2115 | } | |
2116 | ||
c906108c | 2117 | int |
fba45db2 | 2118 | fputc_unfiltered (int c, struct ui_file *stream) |
c906108c | 2119 | { |
11cf8741 | 2120 | char buf = c; |
e0627e85 | 2121 | |
d9fcf2fb | 2122 | ui_file_write (stream, &buf, 1); |
c906108c SS |
2123 | return c; |
2124 | } | |
2125 | ||
2126 | int | |
fba45db2 | 2127 | fputc_filtered (int c, struct ui_file *stream) |
c906108c SS |
2128 | { |
2129 | char buf[2]; | |
2130 | ||
2131 | buf[0] = c; | |
2132 | buf[1] = 0; | |
2133 | fputs_filtered (buf, stream); | |
2134 | return c; | |
2135 | } | |
2136 | ||
2137 | /* puts_debug is like fputs_unfiltered, except it prints special | |
2138 | characters in printable fashion. */ | |
2139 | ||
2140 | void | |
fba45db2 | 2141 | puts_debug (char *prefix, char *string, char *suffix) |
c906108c SS |
2142 | { |
2143 | int ch; | |
2144 | ||
2145 | /* Print prefix and suffix after each line. */ | |
2146 | static int new_line = 1; | |
2147 | static int return_p = 0; | |
2148 | static char *prev_prefix = ""; | |
2149 | static char *prev_suffix = ""; | |
2150 | ||
2151 | if (*string == '\n') | |
2152 | return_p = 0; | |
2153 | ||
2154 | /* If the prefix is changing, print the previous suffix, a new line, | |
2155 | and the new prefix. */ | |
c5aa993b | 2156 | if ((return_p || (strcmp (prev_prefix, prefix) != 0)) && !new_line) |
c906108c | 2157 | { |
9846de1b JM |
2158 | fputs_unfiltered (prev_suffix, gdb_stdlog); |
2159 | fputs_unfiltered ("\n", gdb_stdlog); | |
2160 | fputs_unfiltered (prefix, gdb_stdlog); | |
c906108c SS |
2161 | } |
2162 | ||
2163 | /* Print prefix if we printed a newline during the previous call. */ | |
2164 | if (new_line) | |
2165 | { | |
2166 | new_line = 0; | |
9846de1b | 2167 | fputs_unfiltered (prefix, gdb_stdlog); |
c906108c SS |
2168 | } |
2169 | ||
2170 | prev_prefix = prefix; | |
2171 | prev_suffix = suffix; | |
2172 | ||
2173 | /* Output characters in a printable format. */ | |
2174 | while ((ch = *string++) != '\0') | |
2175 | { | |
2176 | switch (ch) | |
c5aa993b | 2177 | { |
c906108c SS |
2178 | default: |
2179 | if (isprint (ch)) | |
9846de1b | 2180 | fputc_unfiltered (ch, gdb_stdlog); |
c906108c SS |
2181 | |
2182 | else | |
9846de1b | 2183 | fprintf_unfiltered (gdb_stdlog, "\\x%02x", ch & 0xff); |
c906108c SS |
2184 | break; |
2185 | ||
c5aa993b JM |
2186 | case '\\': |
2187 | fputs_unfiltered ("\\\\", gdb_stdlog); | |
2188 | break; | |
2189 | case '\b': | |
2190 | fputs_unfiltered ("\\b", gdb_stdlog); | |
2191 | break; | |
2192 | case '\f': | |
2193 | fputs_unfiltered ("\\f", gdb_stdlog); | |
2194 | break; | |
2195 | case '\n': | |
2196 | new_line = 1; | |
2197 | fputs_unfiltered ("\\n", gdb_stdlog); | |
2198 | break; | |
2199 | case '\r': | |
2200 | fputs_unfiltered ("\\r", gdb_stdlog); | |
2201 | break; | |
2202 | case '\t': | |
2203 | fputs_unfiltered ("\\t", gdb_stdlog); | |
2204 | break; | |
2205 | case '\v': | |
2206 | fputs_unfiltered ("\\v", gdb_stdlog); | |
2207 | break; | |
2208 | } | |
c906108c SS |
2209 | |
2210 | return_p = ch == '\r'; | |
2211 | } | |
2212 | ||
2213 | /* Print suffix if we printed a newline. */ | |
2214 | if (new_line) | |
2215 | { | |
9846de1b JM |
2216 | fputs_unfiltered (suffix, gdb_stdlog); |
2217 | fputs_unfiltered ("\n", gdb_stdlog); | |
c906108c SS |
2218 | } |
2219 | } | |
2220 | ||
2221 | ||
2222 | /* Print a variable number of ARGS using format FORMAT. If this | |
2223 | information is going to put the amount written (since the last call | |
2224 | to REINITIALIZE_MORE_FILTER or the last page break) over the page size, | |
2225 | call prompt_for_continue to get the users permision to continue. | |
2226 | ||
2227 | Unlike fprintf, this function does not return a value. | |
2228 | ||
2229 | We implement three variants, vfprintf (takes a vararg list and stream), | |
2230 | fprintf (takes a stream to write on), and printf (the usual). | |
2231 | ||
2232 | Note also that a longjmp to top level may occur in this routine | |
2233 | (since prompt_for_continue may do so) so this routine should not be | |
2234 | called when cleanups are not in place. */ | |
2235 | ||
2236 | static void | |
fba45db2 KB |
2237 | vfprintf_maybe_filtered (struct ui_file *stream, const char *format, |
2238 | va_list args, int filter) | |
c906108c SS |
2239 | { |
2240 | char *linebuffer; | |
2241 | struct cleanup *old_cleanups; | |
2242 | ||
e623b504 | 2243 | linebuffer = xstrvprintf (format, args); |
b8c9b27d | 2244 | old_cleanups = make_cleanup (xfree, linebuffer); |
c906108c SS |
2245 | fputs_maybe_filtered (linebuffer, stream, filter); |
2246 | do_cleanups (old_cleanups); | |
2247 | } | |
2248 | ||
2249 | ||
2250 | void | |
fba45db2 | 2251 | vfprintf_filtered (struct ui_file *stream, const char *format, va_list args) |
c906108c SS |
2252 | { |
2253 | vfprintf_maybe_filtered (stream, format, args, 1); | |
2254 | } | |
2255 | ||
2256 | void | |
fba45db2 | 2257 | vfprintf_unfiltered (struct ui_file *stream, const char *format, va_list args) |
c906108c SS |
2258 | { |
2259 | char *linebuffer; | |
2260 | struct cleanup *old_cleanups; | |
2261 | ||
e623b504 | 2262 | linebuffer = xstrvprintf (format, args); |
b8c9b27d | 2263 | old_cleanups = make_cleanup (xfree, linebuffer); |
75feb17d DJ |
2264 | if (debug_timestamp && stream == gdb_stdlog) |
2265 | { | |
2266 | struct timeval tm; | |
2267 | char *timestamp; | |
6e5abd65 | 2268 | int len, need_nl; |
75feb17d DJ |
2269 | |
2270 | gettimeofday (&tm, NULL); | |
6e5abd65 PA |
2271 | |
2272 | len = strlen (linebuffer); | |
2273 | need_nl = (len > 0 && linebuffer[len - 1] != '\n'); | |
2274 | ||
2275 | timestamp = xstrprintf ("%ld:%ld %s%s", | |
2276 | (long) tm.tv_sec, (long) tm.tv_usec, | |
2277 | linebuffer, | |
2278 | need_nl ? "\n": ""); | |
75feb17d DJ |
2279 | make_cleanup (xfree, timestamp); |
2280 | fputs_unfiltered (timestamp, stream); | |
2281 | } | |
6e5abd65 PA |
2282 | else |
2283 | fputs_unfiltered (linebuffer, stream); | |
c906108c SS |
2284 | do_cleanups (old_cleanups); |
2285 | } | |
2286 | ||
2287 | void | |
fba45db2 | 2288 | vprintf_filtered (const char *format, va_list args) |
c906108c SS |
2289 | { |
2290 | vfprintf_maybe_filtered (gdb_stdout, format, args, 1); | |
2291 | } | |
2292 | ||
2293 | void | |
fba45db2 | 2294 | vprintf_unfiltered (const char *format, va_list args) |
c906108c SS |
2295 | { |
2296 | vfprintf_unfiltered (gdb_stdout, format, args); | |
2297 | } | |
2298 | ||
c906108c | 2299 | void |
8731e58e | 2300 | fprintf_filtered (struct ui_file *stream, const char *format, ...) |
c906108c SS |
2301 | { |
2302 | va_list args; | |
e0627e85 | 2303 | |
c906108c | 2304 | va_start (args, format); |
c906108c SS |
2305 | vfprintf_filtered (stream, format, args); |
2306 | va_end (args); | |
2307 | } | |
2308 | ||
c906108c | 2309 | void |
8731e58e | 2310 | fprintf_unfiltered (struct ui_file *stream, const char *format, ...) |
c906108c SS |
2311 | { |
2312 | va_list args; | |
e0627e85 | 2313 | |
c906108c | 2314 | va_start (args, format); |
c906108c SS |
2315 | vfprintf_unfiltered (stream, format, args); |
2316 | va_end (args); | |
2317 | } | |
2318 | ||
2319 | /* Like fprintf_filtered, but prints its result indented. | |
2320 | Called as fprintfi_filtered (spaces, stream, format, ...); */ | |
2321 | ||
c906108c | 2322 | void |
8731e58e AC |
2323 | fprintfi_filtered (int spaces, struct ui_file *stream, const char *format, |
2324 | ...) | |
c906108c SS |
2325 | { |
2326 | va_list args; | |
e0627e85 | 2327 | |
c906108c | 2328 | va_start (args, format); |
c906108c SS |
2329 | print_spaces_filtered (spaces, stream); |
2330 | ||
2331 | vfprintf_filtered (stream, format, args); | |
2332 | va_end (args); | |
2333 | } | |
2334 | ||
2335 | ||
c906108c | 2336 | void |
8731e58e | 2337 | printf_filtered (const char *format, ...) |
c906108c SS |
2338 | { |
2339 | va_list args; | |
e0627e85 | 2340 | |
c906108c | 2341 | va_start (args, format); |
c906108c SS |
2342 | vfprintf_filtered (gdb_stdout, format, args); |
2343 | va_end (args); | |
2344 | } | |
2345 | ||
2346 | ||
c906108c | 2347 | void |
8731e58e | 2348 | printf_unfiltered (const char *format, ...) |
c906108c SS |
2349 | { |
2350 | va_list args; | |
e0627e85 | 2351 | |
c906108c | 2352 | va_start (args, format); |
c906108c SS |
2353 | vfprintf_unfiltered (gdb_stdout, format, args); |
2354 | va_end (args); | |
2355 | } | |
2356 | ||
2357 | /* Like printf_filtered, but prints it's result indented. | |
2358 | Called as printfi_filtered (spaces, format, ...); */ | |
2359 | ||
c906108c | 2360 | void |
8731e58e | 2361 | printfi_filtered (int spaces, const char *format, ...) |
c906108c SS |
2362 | { |
2363 | va_list args; | |
e0627e85 | 2364 | |
c906108c | 2365 | va_start (args, format); |
c906108c SS |
2366 | print_spaces_filtered (spaces, gdb_stdout); |
2367 | vfprintf_filtered (gdb_stdout, format, args); | |
2368 | va_end (args); | |
2369 | } | |
2370 | ||
2371 | /* Easy -- but watch out! | |
2372 | ||
2373 | This routine is *not* a replacement for puts()! puts() appends a newline. | |
2374 | This one doesn't, and had better not! */ | |
2375 | ||
2376 | void | |
fba45db2 | 2377 | puts_filtered (const char *string) |
c906108c SS |
2378 | { |
2379 | fputs_filtered (string, gdb_stdout); | |
2380 | } | |
2381 | ||
2382 | void | |
fba45db2 | 2383 | puts_unfiltered (const char *string) |
c906108c SS |
2384 | { |
2385 | fputs_unfiltered (string, gdb_stdout); | |
2386 | } | |
2387 | ||
2388 | /* Return a pointer to N spaces and a null. The pointer is good | |
2389 | until the next call to here. */ | |
2390 | char * | |
fba45db2 | 2391 | n_spaces (int n) |
c906108c | 2392 | { |
392a587b JM |
2393 | char *t; |
2394 | static char *spaces = 0; | |
2395 | static int max_spaces = -1; | |
c906108c SS |
2396 | |
2397 | if (n > max_spaces) | |
2398 | { | |
2399 | if (spaces) | |
b8c9b27d | 2400 | xfree (spaces); |
c5aa993b JM |
2401 | spaces = (char *) xmalloc (n + 1); |
2402 | for (t = spaces + n; t != spaces;) | |
c906108c SS |
2403 | *--t = ' '; |
2404 | spaces[n] = '\0'; | |
2405 | max_spaces = n; | |
2406 | } | |
2407 | ||
2408 | return spaces + max_spaces - n; | |
2409 | } | |
2410 | ||
2411 | /* Print N spaces. */ | |
2412 | void | |
fba45db2 | 2413 | print_spaces_filtered (int n, struct ui_file *stream) |
c906108c SS |
2414 | { |
2415 | fputs_filtered (n_spaces (n), stream); | |
2416 | } | |
2417 | \f | |
4a351cef | 2418 | /* C++/ObjC demangler stuff. */ |
c906108c | 2419 | |
389e51db AC |
2420 | /* fprintf_symbol_filtered attempts to demangle NAME, a symbol in language |
2421 | LANG, using demangling args ARG_MODE, and print it filtered to STREAM. | |
2422 | If the name is not mangled, or the language for the name is unknown, or | |
581e13c1 | 2423 | demangling is off, the name is printed in its "raw" form. */ |
c906108c SS |
2424 | |
2425 | void | |
0d5cff50 | 2426 | fprintf_symbol_filtered (struct ui_file *stream, const char *name, |
8731e58e | 2427 | enum language lang, int arg_mode) |
c906108c SS |
2428 | { |
2429 | char *demangled; | |
2430 | ||
2431 | if (name != NULL) | |
2432 | { | |
2433 | /* If user wants to see raw output, no problem. */ | |
2434 | if (!demangle) | |
2435 | { | |
2436 | fputs_filtered (name, stream); | |
2437 | } | |
2438 | else | |
2439 | { | |
9a3d7dfd | 2440 | demangled = language_demangle (language_def (lang), name, arg_mode); |
c906108c SS |
2441 | fputs_filtered (demangled ? demangled : name, stream); |
2442 | if (demangled != NULL) | |
2443 | { | |
b8c9b27d | 2444 | xfree (demangled); |
c906108c SS |
2445 | } |
2446 | } | |
2447 | } | |
2448 | } | |
2449 | ||
2450 | /* Do a strcmp() type operation on STRING1 and STRING2, ignoring any | |
2451 | differences in whitespace. Returns 0 if they match, non-zero if they | |
2452 | don't (slightly different than strcmp()'s range of return values). | |
c5aa993b | 2453 | |
c906108c SS |
2454 | As an extra hack, string1=="FOO(ARGS)" matches string2=="FOO". |
2455 | This "feature" is useful when searching for matching C++ function names | |
2456 | (such as if the user types 'break FOO', where FOO is a mangled C++ | |
581e13c1 | 2457 | function). */ |
c906108c SS |
2458 | |
2459 | int | |
fba45db2 | 2460 | strcmp_iw (const char *string1, const char *string2) |
c906108c SS |
2461 | { |
2462 | while ((*string1 != '\0') && (*string2 != '\0')) | |
2463 | { | |
2464 | while (isspace (*string1)) | |
2465 | { | |
2466 | string1++; | |
2467 | } | |
2468 | while (isspace (*string2)) | |
2469 | { | |
2470 | string2++; | |
2471 | } | |
559a7a62 JK |
2472 | if (case_sensitivity == case_sensitive_on && *string1 != *string2) |
2473 | break; | |
2474 | if (case_sensitivity == case_sensitive_off | |
2475 | && (tolower ((unsigned char) *string1) | |
2476 | != tolower ((unsigned char) *string2))) | |
2477 | break; | |
c906108c SS |
2478 | if (*string1 != '\0') |
2479 | { | |
2480 | string1++; | |
2481 | string2++; | |
2482 | } | |
2483 | } | |
2484 | return (*string1 != '\0' && *string1 != '(') || (*string2 != '\0'); | |
2485 | } | |
2de7ced7 | 2486 | |
0fe19209 DC |
2487 | /* This is like strcmp except that it ignores whitespace and treats |
2488 | '(' as the first non-NULL character in terms of ordering. Like | |
2489 | strcmp (and unlike strcmp_iw), it returns negative if STRING1 < | |
2490 | STRING2, 0 if STRING2 = STRING2, and positive if STRING1 > STRING2 | |
2491 | according to that ordering. | |
2492 | ||
2493 | If a list is sorted according to this function and if you want to | |
2494 | find names in the list that match some fixed NAME according to | |
2495 | strcmp_iw(LIST_ELT, NAME), then the place to start looking is right | |
2496 | where this function would put NAME. | |
2497 | ||
559a7a62 JK |
2498 | This function must be neutral to the CASE_SENSITIVITY setting as the user |
2499 | may choose it during later lookup. Therefore this function always sorts | |
2500 | primarily case-insensitively and secondarily case-sensitively. | |
2501 | ||
0fe19209 DC |
2502 | Here are some examples of why using strcmp to sort is a bad idea: |
2503 | ||
2504 | Whitespace example: | |
2505 | ||
2506 | Say your partial symtab contains: "foo<char *>", "goo". Then, if | |
2507 | we try to do a search for "foo<char*>", strcmp will locate this | |
2508 | after "foo<char *>" and before "goo". Then lookup_partial_symbol | |
2509 | will start looking at strings beginning with "goo", and will never | |
2510 | see the correct match of "foo<char *>". | |
2511 | ||
2512 | Parenthesis example: | |
2513 | ||
2514 | In practice, this is less like to be an issue, but I'll give it a | |
2515 | shot. Let's assume that '$' is a legitimate character to occur in | |
2516 | symbols. (Which may well even be the case on some systems.) Then | |
2517 | say that the partial symbol table contains "foo$" and "foo(int)". | |
2518 | strcmp will put them in this order, since '$' < '('. Now, if the | |
2519 | user searches for "foo", then strcmp will sort "foo" before "foo$". | |
2520 | Then lookup_partial_symbol will notice that strcmp_iw("foo$", | |
2521 | "foo") is false, so it won't proceed to the actual match of | |
2522 | "foo(int)" with "foo". */ | |
2523 | ||
2524 | int | |
2525 | strcmp_iw_ordered (const char *string1, const char *string2) | |
2526 | { | |
559a7a62 JK |
2527 | const char *saved_string1 = string1, *saved_string2 = string2; |
2528 | enum case_sensitivity case_pass = case_sensitive_off; | |
2529 | ||
2530 | for (;;) | |
0fe19209 | 2531 | { |
b11b1f88 JK |
2532 | /* C1 and C2 are valid only if *string1 != '\0' && *string2 != '\0'. |
2533 | Provide stub characters if we are already at the end of one of the | |
2534 | strings. */ | |
2535 | char c1 = 'X', c2 = 'X'; | |
2536 | ||
2537 | while (*string1 != '\0' && *string2 != '\0') | |
0fe19209 | 2538 | { |
b11b1f88 JK |
2539 | while (isspace (*string1)) |
2540 | string1++; | |
2541 | while (isspace (*string2)) | |
2542 | string2++; | |
2543 | ||
559a7a62 JK |
2544 | switch (case_pass) |
2545 | { | |
2546 | case case_sensitive_off: | |
2547 | c1 = tolower ((unsigned char) *string1); | |
2548 | c2 = tolower ((unsigned char) *string2); | |
2549 | break; | |
2550 | case case_sensitive_on: | |
b11b1f88 JK |
2551 | c1 = *string1; |
2552 | c2 = *string2; | |
559a7a62 JK |
2553 | break; |
2554 | } | |
b11b1f88 JK |
2555 | if (c1 != c2) |
2556 | break; | |
2557 | ||
2558 | if (*string1 != '\0') | |
2559 | { | |
2560 | string1++; | |
2561 | string2++; | |
2562 | } | |
0fe19209 | 2563 | } |
b11b1f88 JK |
2564 | |
2565 | switch (*string1) | |
0fe19209 | 2566 | { |
b11b1f88 JK |
2567 | /* Characters are non-equal unless they're both '\0'; we want to |
2568 | make sure we get the comparison right according to our | |
2569 | comparison in the cases where one of them is '\0' or '('. */ | |
2570 | case '\0': | |
2571 | if (*string2 == '\0') | |
559a7a62 | 2572 | break; |
b11b1f88 JK |
2573 | else |
2574 | return -1; | |
2575 | case '(': | |
2576 | if (*string2 == '\0') | |
2577 | return 1; | |
2578 | else | |
2579 | return -1; | |
2580 | default: | |
2581 | if (*string2 == '\0' || *string2 == '(') | |
2582 | return 1; | |
559a7a62 JK |
2583 | else if (c1 > c2) |
2584 | return 1; | |
2585 | else if (c1 < c2) | |
2586 | return -1; | |
2587 | /* PASSTHRU */ | |
0fe19209 | 2588 | } |
559a7a62 JK |
2589 | |
2590 | if (case_pass == case_sensitive_on) | |
2591 | return 0; | |
2592 | ||
2593 | /* Otherwise the strings were equal in case insensitive way, make | |
2594 | a more fine grained comparison in a case sensitive way. */ | |
2595 | ||
2596 | case_pass = case_sensitive_on; | |
2597 | string1 = saved_string1; | |
2598 | string2 = saved_string2; | |
0fe19209 | 2599 | } |
0fe19209 DC |
2600 | } |
2601 | ||
2de7ced7 DJ |
2602 | /* A simple comparison function with opposite semantics to strcmp. */ |
2603 | ||
2604 | int | |
2605 | streq (const char *lhs, const char *rhs) | |
2606 | { | |
2607 | return !strcmp (lhs, rhs); | |
2608 | } | |
c906108c | 2609 | \f |
c5aa993b | 2610 | |
c906108c | 2611 | /* |
c5aa993b JM |
2612 | ** subset_compare() |
2613 | ** Answer whether string_to_compare is a full or partial match to | |
2614 | ** template_string. The partial match must be in sequence starting | |
2615 | ** at index 0. | |
2616 | */ | |
c906108c | 2617 | int |
fba45db2 | 2618 | subset_compare (char *string_to_compare, char *template_string) |
7a292a7a SS |
2619 | { |
2620 | int match; | |
e0627e85 | 2621 | |
8731e58e AC |
2622 | if (template_string != (char *) NULL && string_to_compare != (char *) NULL |
2623 | && strlen (string_to_compare) <= strlen (template_string)) | |
2624 | match = | |
2625 | (strncmp | |
2626 | (template_string, string_to_compare, strlen (string_to_compare)) == 0); | |
7a292a7a SS |
2627 | else |
2628 | match = 0; | |
2629 | return match; | |
2630 | } | |
c906108c | 2631 | |
7a292a7a | 2632 | static void |
fba45db2 | 2633 | pagination_on_command (char *arg, int from_tty) |
c906108c SS |
2634 | { |
2635 | pagination_enabled = 1; | |
2636 | } | |
2637 | ||
7a292a7a | 2638 | static void |
fba45db2 | 2639 | pagination_off_command (char *arg, int from_tty) |
c906108c SS |
2640 | { |
2641 | pagination_enabled = 0; | |
2642 | } | |
75feb17d DJ |
2643 | |
2644 | static void | |
2645 | show_debug_timestamp (struct ui_file *file, int from_tty, | |
2646 | struct cmd_list_element *c, const char *value) | |
2647 | { | |
3e43a32a MS |
2648 | fprintf_filtered (file, _("Timestamping debugging messages is %s.\n"), |
2649 | value); | |
75feb17d | 2650 | } |
c906108c | 2651 | \f |
c5aa993b | 2652 | |
c906108c | 2653 | void |
fba45db2 | 2654 | initialize_utils (void) |
c906108c | 2655 | { |
35096d9d AC |
2656 | add_setshow_uinteger_cmd ("width", class_support, &chars_per_line, _("\ |
2657 | Set number of characters gdb thinks are in a line."), _("\ | |
2658 | Show number of characters gdb thinks are in a line."), NULL, | |
2659 | set_width_command, | |
920d2a44 | 2660 | show_chars_per_line, |
35096d9d AC |
2661 | &setlist, &showlist); |
2662 | ||
2663 | add_setshow_uinteger_cmd ("height", class_support, &lines_per_page, _("\ | |
2664 | Set number of lines gdb thinks are in a page."), _("\ | |
2665 | Show number of lines gdb thinks are in a page."), NULL, | |
2666 | set_height_command, | |
920d2a44 | 2667 | show_lines_per_page, |
35096d9d | 2668 | &setlist, &showlist); |
c5aa993b | 2669 | |
c906108c SS |
2670 | init_page_info (); |
2671 | ||
5bf193a2 AC |
2672 | add_setshow_boolean_cmd ("pagination", class_support, |
2673 | &pagination_enabled, _("\ | |
2674 | Set state of pagination."), _("\ | |
2675 | Show state of pagination."), NULL, | |
2676 | NULL, | |
920d2a44 | 2677 | show_pagination_enabled, |
5bf193a2 | 2678 | &setlist, &showlist); |
4261bedc | 2679 | |
c906108c SS |
2680 | if (xdb_commands) |
2681 | { | |
c5aa993b | 2682 | add_com ("am", class_support, pagination_on_command, |
1bedd215 | 2683 | _("Enable pagination")); |
c5aa993b | 2684 | add_com ("sm", class_support, pagination_off_command, |
1bedd215 | 2685 | _("Disable pagination")); |
c906108c SS |
2686 | } |
2687 | ||
5bf193a2 AC |
2688 | add_setshow_boolean_cmd ("sevenbit-strings", class_support, |
2689 | &sevenbit_strings, _("\ | |
2690 | Set printing of 8-bit characters in strings as \\nnn."), _("\ | |
2691 | Show printing of 8-bit characters in strings as \\nnn."), NULL, | |
2692 | NULL, | |
920d2a44 | 2693 | show_sevenbit_strings, |
5bf193a2 AC |
2694 | &setprintlist, &showprintlist); |
2695 | ||
75feb17d DJ |
2696 | add_setshow_boolean_cmd ("timestamp", class_maintenance, |
2697 | &debug_timestamp, _("\ | |
2698 | Set timestamping of debugging messages."), _("\ | |
2699 | Show timestamping of debugging messages."), _("\ | |
2700 | When set, debugging messages will be marked with seconds and microseconds."), | |
2701 | NULL, | |
2702 | show_debug_timestamp, | |
2703 | &setdebuglist, &showdebuglist); | |
c906108c SS |
2704 | } |
2705 | ||
581e13c1 | 2706 | /* Machine specific function to handle SIGWINCH signal. */ |
c906108c SS |
2707 | |
2708 | #ifdef SIGWINCH_HANDLER_BODY | |
c5aa993b | 2709 | SIGWINCH_HANDLER_BODY |
c906108c | 2710 | #endif |
581e13c1 MS |
2711 | /* Print routines to handle variable size regs, etc. */ |
2712 | /* Temporary storage using circular buffer. */ | |
c906108c | 2713 | #define NUMCELLS 16 |
0759e0bf | 2714 | #define CELLSIZE 50 |
c5aa993b | 2715 | static char * |
fba45db2 | 2716 | get_cell (void) |
c906108c SS |
2717 | { |
2718 | static char buf[NUMCELLS][CELLSIZE]; | |
c5aa993b | 2719 | static int cell = 0; |
e0627e85 | 2720 | |
c5aa993b JM |
2721 | if (++cell >= NUMCELLS) |
2722 | cell = 0; | |
c906108c SS |
2723 | return buf[cell]; |
2724 | } | |
2725 | ||
66bf4b3a | 2726 | const char * |
5af949e3 | 2727 | paddress (struct gdbarch *gdbarch, CORE_ADDR addr) |
66bf4b3a AC |
2728 | { |
2729 | /* Truncate address to the size of a target address, avoiding shifts | |
2730 | larger or equal than the width of a CORE_ADDR. The local | |
2731 | variable ADDR_BIT stops the compiler reporting a shift overflow | |
581e13c1 | 2732 | when it won't occur. */ |
66bf4b3a AC |
2733 | /* NOTE: This assumes that the significant address information is |
2734 | kept in the least significant bits of ADDR - the upper bits were | |
76e71323 | 2735 | either zero or sign extended. Should gdbarch_address_to_pointer or |
66bf4b3a AC |
2736 | some ADDRESS_TO_PRINTABLE() be used to do the conversion? */ |
2737 | ||
5af949e3 | 2738 | int addr_bit = gdbarch_addr_bit (gdbarch); |
66bf4b3a AC |
2739 | |
2740 | if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT)) | |
2741 | addr &= ((CORE_ADDR) 1 << addr_bit) - 1; | |
2742 | return hex_string (addr); | |
2743 | } | |
2744 | ||
f1310107 TJB |
2745 | /* This function is described in "defs.h". */ |
2746 | ||
2747 | const char * | |
2748 | print_core_address (struct gdbarch *gdbarch, CORE_ADDR address) | |
2749 | { | |
2750 | int addr_bit = gdbarch_addr_bit (gdbarch); | |
2751 | ||
2752 | if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT)) | |
2753 | address &= ((CORE_ADDR) 1 << addr_bit) - 1; | |
2754 | ||
2755 | /* FIXME: cagney/2002-05-03: Need local_address_string() function | |
2756 | that returns the language localized string formatted to a width | |
2757 | based on gdbarch_addr_bit. */ | |
2758 | if (addr_bit <= 32) | |
2759 | return hex_string_custom (address, 8); | |
2760 | else | |
2761 | return hex_string_custom (address, 16); | |
2762 | } | |
2763 | ||
8e3b41a9 JK |
2764 | /* Callback hash_f for htab_create_alloc or htab_create_alloc_ex. */ |
2765 | ||
2766 | hashval_t | |
2767 | core_addr_hash (const void *ap) | |
2768 | { | |
2769 | const CORE_ADDR *addrp = ap; | |
2770 | ||
2771 | return *addrp; | |
2772 | } | |
2773 | ||
2774 | /* Callback eq_f for htab_create_alloc or htab_create_alloc_ex. */ | |
2775 | ||
2776 | int | |
2777 | core_addr_eq (const void *ap, const void *bp) | |
2778 | { | |
2779 | const CORE_ADDR *addr_ap = ap; | |
2780 | const CORE_ADDR *addr_bp = bp; | |
2781 | ||
2782 | return *addr_ap == *addr_bp; | |
2783 | } | |
2784 | ||
8cf46f62 MK |
2785 | static char * |
2786 | decimal2str (char *sign, ULONGEST addr, int width) | |
104c1213 | 2787 | { |
8cf46f62 | 2788 | /* Steal code from valprint.c:print_decimal(). Should this worry |
581e13c1 | 2789 | about the real size of addr as the above does? */ |
104c1213 | 2790 | unsigned long temp[3]; |
8cf46f62 | 2791 | char *str = get_cell (); |
104c1213 | 2792 | int i = 0; |
5d502164 | 2793 | |
104c1213 JM |
2794 | do |
2795 | { | |
2796 | temp[i] = addr % (1000 * 1000 * 1000); | |
2797 | addr /= (1000 * 1000 * 1000); | |
2798 | i++; | |
bb599908 | 2799 | width -= 9; |
104c1213 JM |
2800 | } |
2801 | while (addr != 0 && i < (sizeof (temp) / sizeof (temp[0]))); | |
8cf46f62 | 2802 | |
bb599908 PH |
2803 | width += 9; |
2804 | if (width < 0) | |
2805 | width = 0; | |
8cf46f62 | 2806 | |
104c1213 JM |
2807 | switch (i) |
2808 | { | |
2809 | case 1: | |
8cf46f62 | 2810 | xsnprintf (str, CELLSIZE, "%s%0*lu", sign, width, temp[0]); |
104c1213 JM |
2811 | break; |
2812 | case 2: | |
8cf46f62 MK |
2813 | xsnprintf (str, CELLSIZE, "%s%0*lu%09lu", sign, width, |
2814 | temp[1], temp[0]); | |
104c1213 JM |
2815 | break; |
2816 | case 3: | |
8cf46f62 MK |
2817 | xsnprintf (str, CELLSIZE, "%s%0*lu%09lu%09lu", sign, width, |
2818 | temp[2], temp[1], temp[0]); | |
bb599908 PH |
2819 | break; |
2820 | default: | |
2821 | internal_error (__FILE__, __LINE__, | |
e2e0b3e5 | 2822 | _("failed internal consistency check")); |
bb599908 | 2823 | } |
8cf46f62 MK |
2824 | |
2825 | return str; | |
bb599908 PH |
2826 | } |
2827 | ||
8cf46f62 MK |
2828 | static char * |
2829 | octal2str (ULONGEST addr, int width) | |
bb599908 PH |
2830 | { |
2831 | unsigned long temp[3]; | |
8cf46f62 | 2832 | char *str = get_cell (); |
bb599908 | 2833 | int i = 0; |
5d502164 | 2834 | |
bb599908 PH |
2835 | do |
2836 | { | |
2837 | temp[i] = addr % (0100000 * 0100000); | |
2838 | addr /= (0100000 * 0100000); | |
2839 | i++; | |
2840 | width -= 10; | |
2841 | } | |
2842 | while (addr != 0 && i < (sizeof (temp) / sizeof (temp[0]))); | |
8cf46f62 | 2843 | |
bb599908 PH |
2844 | width += 10; |
2845 | if (width < 0) | |
2846 | width = 0; | |
8cf46f62 | 2847 | |
bb599908 PH |
2848 | switch (i) |
2849 | { | |
2850 | case 1: | |
2851 | if (temp[0] == 0) | |
8cf46f62 | 2852 | xsnprintf (str, CELLSIZE, "%*o", width, 0); |
bb599908 | 2853 | else |
8cf46f62 | 2854 | xsnprintf (str, CELLSIZE, "0%0*lo", width, temp[0]); |
bb599908 PH |
2855 | break; |
2856 | case 2: | |
8cf46f62 | 2857 | xsnprintf (str, CELLSIZE, "0%0*lo%010lo", width, temp[1], temp[0]); |
bb599908 PH |
2858 | break; |
2859 | case 3: | |
8cf46f62 MK |
2860 | xsnprintf (str, CELLSIZE, "0%0*lo%010lo%010lo", width, |
2861 | temp[2], temp[1], temp[0]); | |
104c1213 JM |
2862 | break; |
2863 | default: | |
8731e58e | 2864 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 2865 | _("failed internal consistency check")); |
104c1213 | 2866 | } |
8cf46f62 MK |
2867 | |
2868 | return str; | |
104c1213 JM |
2869 | } |
2870 | ||
2871 | char * | |
623d3eb1 | 2872 | pulongest (ULONGEST u) |
104c1213 | 2873 | { |
623d3eb1 | 2874 | return decimal2str ("", u, 0); |
104c1213 JM |
2875 | } |
2876 | ||
2877 | char * | |
623d3eb1 | 2878 | plongest (LONGEST l) |
104c1213 | 2879 | { |
623d3eb1 DE |
2880 | if (l < 0) |
2881 | return decimal2str ("-", -l, 0); | |
104c1213 | 2882 | else |
623d3eb1 | 2883 | return decimal2str ("", l, 0); |
104c1213 JM |
2884 | } |
2885 | ||
8cf46f62 | 2886 | /* Eliminate warning from compiler on 32-bit systems. */ |
5683e87a AC |
2887 | static int thirty_two = 32; |
2888 | ||
104c1213 | 2889 | char * |
5683e87a | 2890 | phex (ULONGEST l, int sizeof_l) |
104c1213 | 2891 | { |
45a1e866 | 2892 | char *str; |
8cf46f62 | 2893 | |
5683e87a | 2894 | switch (sizeof_l) |
104c1213 JM |
2895 | { |
2896 | case 8: | |
45a1e866 | 2897 | str = get_cell (); |
8cf46f62 MK |
2898 | xsnprintf (str, CELLSIZE, "%08lx%08lx", |
2899 | (unsigned long) (l >> thirty_two), | |
2900 | (unsigned long) (l & 0xffffffff)); | |
104c1213 JM |
2901 | break; |
2902 | case 4: | |
45a1e866 | 2903 | str = get_cell (); |
8cf46f62 | 2904 | xsnprintf (str, CELLSIZE, "%08lx", (unsigned long) l); |
104c1213 JM |
2905 | break; |
2906 | case 2: | |
45a1e866 | 2907 | str = get_cell (); |
8cf46f62 | 2908 | xsnprintf (str, CELLSIZE, "%04x", (unsigned short) (l & 0xffff)); |
104c1213 JM |
2909 | break; |
2910 | default: | |
45a1e866 | 2911 | str = phex (l, sizeof (l)); |
5683e87a | 2912 | break; |
104c1213 | 2913 | } |
8cf46f62 | 2914 | |
5683e87a | 2915 | return str; |
104c1213 JM |
2916 | } |
2917 | ||
c5aa993b | 2918 | char * |
5683e87a | 2919 | phex_nz (ULONGEST l, int sizeof_l) |
c906108c | 2920 | { |
faf833ca | 2921 | char *str; |
8cf46f62 | 2922 | |
5683e87a | 2923 | switch (sizeof_l) |
c906108c | 2924 | { |
c5aa993b JM |
2925 | case 8: |
2926 | { | |
5683e87a | 2927 | unsigned long high = (unsigned long) (l >> thirty_two); |
5d502164 | 2928 | |
faf833ca | 2929 | str = get_cell (); |
c5aa993b | 2930 | if (high == 0) |
8cf46f62 MK |
2931 | xsnprintf (str, CELLSIZE, "%lx", |
2932 | (unsigned long) (l & 0xffffffff)); | |
c5aa993b | 2933 | else |
8cf46f62 MK |
2934 | xsnprintf (str, CELLSIZE, "%lx%08lx", high, |
2935 | (unsigned long) (l & 0xffffffff)); | |
c906108c | 2936 | break; |
c5aa993b JM |
2937 | } |
2938 | case 4: | |
faf833ca | 2939 | str = get_cell (); |
8cf46f62 | 2940 | xsnprintf (str, CELLSIZE, "%lx", (unsigned long) l); |
c5aa993b JM |
2941 | break; |
2942 | case 2: | |
faf833ca | 2943 | str = get_cell (); |
8cf46f62 | 2944 | xsnprintf (str, CELLSIZE, "%x", (unsigned short) (l & 0xffff)); |
c5aa993b JM |
2945 | break; |
2946 | default: | |
faf833ca | 2947 | str = phex_nz (l, sizeof (l)); |
5683e87a | 2948 | break; |
c906108c | 2949 | } |
8cf46f62 | 2950 | |
5683e87a | 2951 | return str; |
c906108c | 2952 | } |
ac2e2ef7 | 2953 | |
0759e0bf AC |
2954 | /* Converts a LONGEST to a C-format hexadecimal literal and stores it |
2955 | in a static string. Returns a pointer to this string. */ | |
2956 | char * | |
2957 | hex_string (LONGEST num) | |
2958 | { | |
2959 | char *result = get_cell (); | |
e0627e85 | 2960 | |
8cf46f62 | 2961 | xsnprintf (result, CELLSIZE, "0x%s", phex_nz (num, sizeof (num))); |
0759e0bf AC |
2962 | return result; |
2963 | } | |
2964 | ||
2965 | /* Converts a LONGEST number to a C-format hexadecimal literal and | |
2966 | stores it in a static string. Returns a pointer to this string | |
2967 | that is valid until the next call. The number is padded on the | |
2968 | left with 0s to at least WIDTH characters. */ | |
2969 | char * | |
2970 | hex_string_custom (LONGEST num, int width) | |
2971 | { | |
2972 | char *result = get_cell (); | |
2973 | char *result_end = result + CELLSIZE - 1; | |
2974 | const char *hex = phex_nz (num, sizeof (num)); | |
2975 | int hex_len = strlen (hex); | |
2976 | ||
2977 | if (hex_len > width) | |
2978 | width = hex_len; | |
2979 | if (width + 2 >= CELLSIZE) | |
3e43a32a MS |
2980 | internal_error (__FILE__, __LINE__, _("\ |
2981 | hex_string_custom: insufficient space to store result")); | |
0759e0bf AC |
2982 | |
2983 | strcpy (result_end - width - 2, "0x"); | |
2984 | memset (result_end - width, '0', width); | |
2985 | strcpy (result_end - hex_len, hex); | |
2986 | return result_end - width - 2; | |
2987 | } | |
ac2e2ef7 | 2988 | |
bb599908 PH |
2989 | /* Convert VAL to a numeral in the given radix. For |
2990 | * radix 10, IS_SIGNED may be true, indicating a signed quantity; | |
2991 | * otherwise VAL is interpreted as unsigned. If WIDTH is supplied, | |
2992 | * it is the minimum width (0-padded if needed). USE_C_FORMAT means | |
2993 | * to use C format in all cases. If it is false, then 'x' | |
581e13c1 | 2994 | * and 'o' formats do not include a prefix (0x or leading 0). */ |
bb599908 PH |
2995 | |
2996 | char * | |
2997 | int_string (LONGEST val, int radix, int is_signed, int width, | |
2998 | int use_c_format) | |
2999 | { | |
3000 | switch (radix) | |
3001 | { | |
3002 | case 16: | |
3003 | { | |
3004 | char *result; | |
5d502164 | 3005 | |
bb599908 PH |
3006 | if (width == 0) |
3007 | result = hex_string (val); | |
3008 | else | |
3009 | result = hex_string_custom (val, width); | |
3010 | if (! use_c_format) | |
3011 | result += 2; | |
3012 | return result; | |
3013 | } | |
3014 | case 10: | |
3015 | { | |
bb599908 | 3016 | if (is_signed && val < 0) |
8cf46f62 | 3017 | return decimal2str ("-", -val, width); |
bb599908 | 3018 | else |
8cf46f62 | 3019 | return decimal2str ("", val, width); |
bb599908 PH |
3020 | } |
3021 | case 8: | |
3022 | { | |
8cf46f62 | 3023 | char *result = octal2str (val, width); |
5d502164 | 3024 | |
bb599908 PH |
3025 | if (use_c_format || val == 0) |
3026 | return result; | |
3027 | else | |
3028 | return result + 1; | |
3029 | } | |
3030 | default: | |
3031 | internal_error (__FILE__, __LINE__, | |
e2e0b3e5 | 3032 | _("failed internal consistency check")); |
bb599908 PH |
3033 | } |
3034 | } | |
3035 | ||
03dd37c3 AC |
3036 | /* Convert a CORE_ADDR into a string. */ |
3037 | const char * | |
3038 | core_addr_to_string (const CORE_ADDR addr) | |
49b563f9 KS |
3039 | { |
3040 | char *str = get_cell (); | |
e0627e85 | 3041 | |
49b563f9 KS |
3042 | strcpy (str, "0x"); |
3043 | strcat (str, phex (addr, sizeof (addr))); | |
3044 | return str; | |
3045 | } | |
3046 | ||
3047 | const char * | |
3048 | core_addr_to_string_nz (const CORE_ADDR addr) | |
03dd37c3 AC |
3049 | { |
3050 | char *str = get_cell (); | |
e0627e85 | 3051 | |
03dd37c3 AC |
3052 | strcpy (str, "0x"); |
3053 | strcat (str, phex_nz (addr, sizeof (addr))); | |
3054 | return str; | |
3055 | } | |
3056 | ||
3057 | /* Convert a string back into a CORE_ADDR. */ | |
3058 | CORE_ADDR | |
3059 | string_to_core_addr (const char *my_string) | |
3060 | { | |
3061 | CORE_ADDR addr = 0; | |
9544c605 | 3062 | |
03dd37c3 AC |
3063 | if (my_string[0] == '0' && tolower (my_string[1]) == 'x') |
3064 | { | |
ced572fe | 3065 | /* Assume that it is in hex. */ |
03dd37c3 | 3066 | int i; |
5d502164 | 3067 | |
03dd37c3 AC |
3068 | for (i = 2; my_string[i] != '\0'; i++) |
3069 | { | |
3070 | if (isdigit (my_string[i])) | |
3071 | addr = (my_string[i] - '0') + (addr * 16); | |
8731e58e | 3072 | else if (isxdigit (my_string[i])) |
03dd37c3 AC |
3073 | addr = (tolower (my_string[i]) - 'a' + 0xa) + (addr * 16); |
3074 | else | |
63f06803 | 3075 | error (_("invalid hex \"%s\""), my_string); |
03dd37c3 AC |
3076 | } |
3077 | } | |
3078 | else | |
3079 | { | |
3080 | /* Assume that it is in decimal. */ | |
3081 | int i; | |
5d502164 | 3082 | |
03dd37c3 AC |
3083 | for (i = 0; my_string[i] != '\0'; i++) |
3084 | { | |
3085 | if (isdigit (my_string[i])) | |
3086 | addr = (my_string[i] - '0') + (addr * 10); | |
3087 | else | |
63f06803 | 3088 | error (_("invalid decimal \"%s\""), my_string); |
03dd37c3 AC |
3089 | } |
3090 | } | |
9544c605 | 3091 | |
03dd37c3 AC |
3092 | return addr; |
3093 | } | |
58d370e0 | 3094 | |
17ea7499 CES |
3095 | const char * |
3096 | host_address_to_string (const void *addr) | |
3097 | { | |
3098 | char *str = get_cell (); | |
ea8992ce | 3099 | |
773698b5 | 3100 | xsnprintf (str, CELLSIZE, "0x%s", phex_nz ((uintptr_t) addr, sizeof (addr))); |
17ea7499 CES |
3101 | return str; |
3102 | } | |
3103 | ||
58d370e0 TT |
3104 | char * |
3105 | gdb_realpath (const char *filename) | |
3106 | { | |
70d35819 AC |
3107 | /* Method 1: The system has a compile time upper bound on a filename |
3108 | path. Use that and realpath() to canonicalize the name. This is | |
3109 | the most common case. Note that, if there isn't a compile time | |
3110 | upper bound, you want to avoid realpath() at all costs. */ | |
a4db0f07 | 3111 | #if defined(HAVE_REALPATH) |
70d35819 | 3112 | { |
a4db0f07 | 3113 | # if defined (PATH_MAX) |
70d35819 | 3114 | char buf[PATH_MAX]; |
a4db0f07 RH |
3115 | # define USE_REALPATH |
3116 | # elif defined (MAXPATHLEN) | |
70d35819 | 3117 | char buf[MAXPATHLEN]; |
a4db0f07 RH |
3118 | # define USE_REALPATH |
3119 | # endif | |
70d35819 | 3120 | # if defined (USE_REALPATH) |
82c0260e | 3121 | const char *rp = realpath (filename, buf); |
5d502164 | 3122 | |
70d35819 AC |
3123 | if (rp == NULL) |
3124 | rp = filename; | |
3125 | return xstrdup (rp); | |
70d35819 | 3126 | # endif |
6f88d630 | 3127 | } |
a4db0f07 RH |
3128 | #endif /* HAVE_REALPATH */ |
3129 | ||
70d35819 AC |
3130 | /* Method 2: The host system (i.e., GNU) has the function |
3131 | canonicalize_file_name() which malloc's a chunk of memory and | |
3132 | returns that, use that. */ | |
3133 | #if defined(HAVE_CANONICALIZE_FILE_NAME) | |
3134 | { | |
3135 | char *rp = canonicalize_file_name (filename); | |
5d502164 | 3136 | |
70d35819 AC |
3137 | if (rp == NULL) |
3138 | return xstrdup (filename); | |
3139 | else | |
3140 | return rp; | |
3141 | } | |
58d370e0 | 3142 | #endif |
70d35819 | 3143 | |
6411e720 AC |
3144 | /* FIXME: cagney/2002-11-13: |
3145 | ||
3146 | Method 2a: Use realpath() with a NULL buffer. Some systems, due | |
7a9dd1b2 | 3147 | to the problems described in method 3, have modified their |
6411e720 AC |
3148 | realpath() implementation so that it will allocate a buffer when |
3149 | NULL is passed in. Before this can be used, though, some sort of | |
3150 | configure time test would need to be added. Otherwize the code | |
3151 | will likely core dump. */ | |
3152 | ||
70d35819 AC |
3153 | /* Method 3: Now we're getting desperate! The system doesn't have a |
3154 | compile time buffer size and no alternative function. Query the | |
3155 | OS, using pathconf(), for the buffer limit. Care is needed | |
3156 | though, some systems do not limit PATH_MAX (return -1 for | |
3157 | pathconf()) making it impossible to pass a correctly sized buffer | |
3158 | to realpath() (it could always overflow). On those systems, we | |
3159 | skip this. */ | |
3160 | #if defined (HAVE_REALPATH) && defined (HAVE_UNISTD_H) && defined(HAVE_ALLOCA) | |
3161 | { | |
3162 | /* Find out the max path size. */ | |
3163 | long path_max = pathconf ("/", _PC_PATH_MAX); | |
5d502164 | 3164 | |
70d35819 AC |
3165 | if (path_max > 0) |
3166 | { | |
3167 | /* PATH_MAX is bounded. */ | |
3168 | char *buf = alloca (path_max); | |
3169 | char *rp = realpath (filename, buf); | |
5d502164 | 3170 | |
70d35819 AC |
3171 | return xstrdup (rp ? rp : filename); |
3172 | } | |
3173 | } | |
3174 | #endif | |
3175 | ||
9c5e4386 JB |
3176 | /* The MS Windows method. If we don't have realpath, we assume we |
3177 | don't have symlinks and just canonicalize to a Windows absolute | |
3178 | path. GetFullPath converts ../ and ./ in relative paths to | |
3179 | absolute paths, filling in current drive if one is not given | |
3180 | or using the current directory of a specified drive (eg, "E:foo"). | |
3181 | It also converts all forward slashes to back slashes. */ | |
3182 | /* The file system is case-insensitive but case-preserving. | |
3183 | So we do not lowercase the path. Otherwise, we might not | |
3184 | be able to display the original casing in a given path. */ | |
3185 | #if defined (_WIN32) | |
3186 | { | |
3187 | char buf[MAX_PATH]; | |
3188 | DWORD len = GetFullPathName (filename, MAX_PATH, buf, NULL); | |
3189 | ||
3190 | if (len > 0 && len < MAX_PATH) | |
3191 | return xstrdup (buf); | |
3192 | } | |
3193 | #endif | |
3194 | ||
70d35819 AC |
3195 | /* This system is a lost cause, just dup the buffer. */ |
3196 | return xstrdup (filename); | |
58d370e0 | 3197 | } |
303c8ebd JB |
3198 | |
3199 | /* Return a copy of FILENAME, with its directory prefix canonicalized | |
3200 | by gdb_realpath. */ | |
3201 | ||
3202 | char * | |
3203 | xfullpath (const char *filename) | |
3204 | { | |
3205 | const char *base_name = lbasename (filename); | |
3206 | char *dir_name; | |
3207 | char *real_path; | |
3208 | char *result; | |
3209 | ||
3210 | /* Extract the basename of filename, and return immediately | |
581e13c1 | 3211 | a copy of filename if it does not contain any directory prefix. */ |
303c8ebd JB |
3212 | if (base_name == filename) |
3213 | return xstrdup (filename); | |
3214 | ||
3215 | dir_name = alloca ((size_t) (base_name - filename + 2)); | |
3216 | /* Allocate enough space to store the dir_name + plus one extra | |
3217 | character sometimes needed under Windows (see below), and | |
581e13c1 | 3218 | then the closing \000 character. */ |
303c8ebd JB |
3219 | strncpy (dir_name, filename, base_name - filename); |
3220 | dir_name[base_name - filename] = '\000'; | |
3221 | ||
3222 | #ifdef HAVE_DOS_BASED_FILE_SYSTEM | |
3223 | /* We need to be careful when filename is of the form 'd:foo', which | |
3224 | is equivalent of d:./foo, which is totally different from d:/foo. */ | |
8731e58e | 3225 | if (strlen (dir_name) == 2 && isalpha (dir_name[0]) && dir_name[1] == ':') |
303c8ebd JB |
3226 | { |
3227 | dir_name[2] = '.'; | |
3228 | dir_name[3] = '\000'; | |
3229 | } | |
3230 | #endif | |
3231 | ||
3232 | /* Canonicalize the directory prefix, and build the resulting | |
581e13c1 | 3233 | filename. If the dirname realpath already contains an ending |
303c8ebd JB |
3234 | directory separator, avoid doubling it. */ |
3235 | real_path = gdb_realpath (dir_name); | |
3236 | if (IS_DIR_SEPARATOR (real_path[strlen (real_path) - 1])) | |
c4f7c687 | 3237 | result = concat (real_path, base_name, (char *) NULL); |
303c8ebd | 3238 | else |
c4f7c687 | 3239 | result = concat (real_path, SLASH_STRING, base_name, (char *) NULL); |
303c8ebd JB |
3240 | |
3241 | xfree (real_path); | |
3242 | return result; | |
3243 | } | |
5b5d99cf JB |
3244 | |
3245 | ||
3246 | /* This is the 32-bit CRC function used by the GNU separate debug | |
3247 | facility. An executable may contain a section named | |
3248 | .gnu_debuglink, which holds the name of a separate executable file | |
3249 | containing its debug info, and a checksum of that file's contents, | |
3250 | computed using this function. */ | |
3251 | unsigned long | |
3252 | gnu_debuglink_crc32 (unsigned long crc, unsigned char *buf, size_t len) | |
3253 | { | |
6e0a4fbc | 3254 | static const unsigned int crc32_table[256] = { |
8731e58e AC |
3255 | 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, |
3256 | 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, | |
3257 | 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, | |
3258 | 0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, | |
3259 | 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856, | |
3260 | 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, | |
3261 | 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, | |
3262 | 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, | |
3263 | 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, | |
3264 | 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a, | |
3265 | 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599, | |
3266 | 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, | |
3267 | 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, | |
3268 | 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, | |
3269 | 0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0x0f00f934, 0x9609a88e, | |
3270 | 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01, | |
3271 | 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed, | |
3272 | 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, | |
3273 | 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, | |
3274 | 0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, | |
3275 | 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, | |
3276 | 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5, | |
3277 | 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010, | |
3278 | 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, | |
3279 | 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, | |
3280 | 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, | |
3281 | 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615, | |
3282 | 0x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8, | |
3283 | 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 0xf00f9344, | |
3284 | 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, | |
3285 | 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, | |
3286 | 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, | |
3287 | 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, | |
3288 | 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c, | |
3289 | 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef, | |
3290 | 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, | |
3291 | 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, | |
3292 | 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, | |
3293 | 0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c, | |
3294 | 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713, | |
3295 | 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b, | |
3296 | 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, | |
3297 | 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, | |
3298 | 0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, | |
3299 | 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278, | |
3300 | 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7, | |
3301 | 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66, | |
3302 | 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, | |
3303 | 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, | |
3304 | 0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, | |
3305 | 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, | |
3306 | 0x2d02ef8d | |
3307 | }; | |
5b5d99cf JB |
3308 | unsigned char *end; |
3309 | ||
3310 | crc = ~crc & 0xffffffff; | |
3311 | for (end = buf + len; buf < end; ++buf) | |
3312 | crc = crc32_table[(crc ^ *buf) & 0xff] ^ (crc >> 8); | |
82ae4854 | 3313 | return ~crc & 0xffffffff; |
5b5d99cf | 3314 | } |
5b03f266 AC |
3315 | |
3316 | ULONGEST | |
3317 | align_up (ULONGEST v, int n) | |
3318 | { | |
3319 | /* Check that N is really a power of two. */ | |
3320 | gdb_assert (n && (n & (n-1)) == 0); | |
3321 | return (v + n - 1) & -n; | |
3322 | } | |
3323 | ||
3324 | ULONGEST | |
3325 | align_down (ULONGEST v, int n) | |
3326 | { | |
3327 | /* Check that N is really a power of two. */ | |
3328 | gdb_assert (n && (n & (n-1)) == 0); | |
3329 | return (v & -n); | |
3330 | } | |
ae5a43e0 DJ |
3331 | |
3332 | /* Allocation function for the libiberty hash table which uses an | |
3333 | obstack. The obstack is passed as DATA. */ | |
3334 | ||
3335 | void * | |
3336 | hashtab_obstack_allocate (void *data, size_t size, size_t count) | |
3337 | { | |
3338 | unsigned int total = size * count; | |
3339 | void *ptr = obstack_alloc ((struct obstack *) data, total); | |
e0627e85 | 3340 | |
ae5a43e0 DJ |
3341 | memset (ptr, 0, total); |
3342 | return ptr; | |
3343 | } | |
3344 | ||
3345 | /* Trivial deallocation function for the libiberty splay tree and hash | |
3346 | table - don't deallocate anything. Rely on later deletion of the | |
3347 | obstack. DATA will be the obstack, although it is not needed | |
3348 | here. */ | |
3349 | ||
3350 | void | |
3351 | dummy_obstack_deallocate (void *object, void *data) | |
3352 | { | |
3353 | return; | |
3354 | } | |
253c8abb DJ |
3355 | |
3356 | /* The bit offset of the highest byte in a ULONGEST, for overflow | |
3357 | checking. */ | |
3358 | ||
3359 | #define HIGH_BYTE_POSN ((sizeof (ULONGEST) - 1) * HOST_CHAR_BIT) | |
3360 | ||
3361 | /* True (non-zero) iff DIGIT is a valid digit in radix BASE, | |
3362 | where 2 <= BASE <= 36. */ | |
3363 | ||
3364 | static int | |
3365 | is_digit_in_base (unsigned char digit, int base) | |
3366 | { | |
3367 | if (!isalnum (digit)) | |
3368 | return 0; | |
3369 | if (base <= 10) | |
3370 | return (isdigit (digit) && digit < base + '0'); | |
3371 | else | |
3372 | return (isdigit (digit) || tolower (digit) < base - 10 + 'a'); | |
3373 | } | |
3374 | ||
3375 | static int | |
3376 | digit_to_int (unsigned char c) | |
3377 | { | |
3378 | if (isdigit (c)) | |
3379 | return c - '0'; | |
3380 | else | |
3381 | return tolower (c) - 'a' + 10; | |
3382 | } | |
3383 | ||
3384 | /* As for strtoul, but for ULONGEST results. */ | |
3385 | ||
3386 | ULONGEST | |
3387 | strtoulst (const char *num, const char **trailer, int base) | |
3388 | { | |
3389 | unsigned int high_part; | |
3390 | ULONGEST result; | |
3391 | int minus = 0; | |
3392 | int i = 0; | |
3393 | ||
3394 | /* Skip leading whitespace. */ | |
3395 | while (isspace (num[i])) | |
3396 | i++; | |
3397 | ||
3398 | /* Handle prefixes. */ | |
3399 | if (num[i] == '+') | |
3400 | i++; | |
3401 | else if (num[i] == '-') | |
3402 | { | |
3403 | minus = 1; | |
3404 | i++; | |
3405 | } | |
3406 | ||
3407 | if (base == 0 || base == 16) | |
3408 | { | |
3409 | if (num[i] == '0' && (num[i + 1] == 'x' || num[i + 1] == 'X')) | |
3410 | { | |
3411 | i += 2; | |
3412 | if (base == 0) | |
3413 | base = 16; | |
3414 | } | |
3415 | } | |
3416 | ||
3417 | if (base == 0 && num[i] == '0') | |
3418 | base = 8; | |
3419 | ||
3420 | if (base == 0) | |
3421 | base = 10; | |
3422 | ||
3423 | if (base < 2 || base > 36) | |
3424 | { | |
3425 | errno = EINVAL; | |
3426 | return 0; | |
3427 | } | |
3428 | ||
3429 | result = high_part = 0; | |
3430 | for (; is_digit_in_base (num[i], base); i += 1) | |
3431 | { | |
3432 | result = result * base + digit_to_int (num[i]); | |
3433 | high_part = high_part * base + (unsigned int) (result >> HIGH_BYTE_POSN); | |
3434 | result &= ((ULONGEST) 1 << HIGH_BYTE_POSN) - 1; | |
3435 | if (high_part > 0xff) | |
3436 | { | |
3437 | errno = ERANGE; | |
3438 | result = ~ (ULONGEST) 0; | |
3439 | high_part = 0; | |
3440 | minus = 0; | |
3441 | break; | |
3442 | } | |
3443 | } | |
3444 | ||
3445 | if (trailer != NULL) | |
3446 | *trailer = &num[i]; | |
3447 | ||
3448 | result = result + ((ULONGEST) high_part << HIGH_BYTE_POSN); | |
3449 | if (minus) | |
3450 | return -result; | |
3451 | else | |
3452 | return result; | |
3453 | } | |
e1024ff1 DJ |
3454 | |
3455 | /* Simple, portable version of dirname that does not modify its | |
3456 | argument. */ | |
3457 | ||
3458 | char * | |
3459 | ldirname (const char *filename) | |
3460 | { | |
3461 | const char *base = lbasename (filename); | |
3462 | char *dirname; | |
3463 | ||
3464 | while (base > filename && IS_DIR_SEPARATOR (base[-1])) | |
3465 | --base; | |
3466 | ||
3467 | if (base == filename) | |
3468 | return NULL; | |
3469 | ||
3470 | dirname = xmalloc (base - filename + 2); | |
3471 | memcpy (dirname, filename, base - filename); | |
3472 | ||
3473 | /* On DOS based file systems, convert "d:foo" to "d:.", so that we | |
3474 | create "d:./bar" later instead of the (different) "d:/bar". */ | |
3475 | if (base - filename == 2 && IS_ABSOLUTE_PATH (base) | |
3476 | && !IS_DIR_SEPARATOR (filename[0])) | |
3477 | dirname[base++ - filename] = '.'; | |
3478 | ||
3479 | dirname[base - filename] = '\0'; | |
3480 | return dirname; | |
3481 | } | |
d1a41061 PP |
3482 | |
3483 | /* Call libiberty's buildargv, and return the result. | |
3484 | If buildargv fails due to out-of-memory, call nomem. | |
3485 | Therefore, the returned value is guaranteed to be non-NULL, | |
3486 | unless the parameter itself is NULL. */ | |
3487 | ||
3488 | char ** | |
3489 | gdb_buildargv (const char *s) | |
3490 | { | |
3491 | char **argv = buildargv (s); | |
e0627e85 | 3492 | |
d1a41061 | 3493 | if (s != NULL && argv == NULL) |
d26e3629 | 3494 | malloc_failure (0); |
d1a41061 PP |
3495 | return argv; |
3496 | } | |
3c16cced | 3497 | |
dc146f7c VP |
3498 | int |
3499 | compare_positive_ints (const void *ap, const void *bp) | |
3500 | { | |
3501 | /* Because we know we're comparing two ints which are positive, | |
3502 | there's no danger of overflow here. */ | |
3503 | return * (int *) ap - * (int *) bp; | |
3504 | } | |
3505 | ||
f8eba3c6 TT |
3506 | /* String compare function for qsort. */ |
3507 | ||
3508 | int | |
3509 | compare_strings (const void *arg1, const void *arg2) | |
3510 | { | |
3511 | const char **s1 = (const char **) arg1; | |
3512 | const char **s2 = (const char **) arg2; | |
3513 | ||
3514 | return strcmp (*s1, *s2); | |
3515 | } | |
3516 | ||
d18b8b7a | 3517 | #define AMBIGUOUS_MESS1 ".\nMatching formats:" |
3e43a32a MS |
3518 | #define AMBIGUOUS_MESS2 \ |
3519 | ".\nUse \"set gnutarget format-name\" to specify the format." | |
d18b8b7a HZ |
3520 | |
3521 | const char * | |
3522 | gdb_bfd_errmsg (bfd_error_type error_tag, char **matching) | |
3523 | { | |
3524 | char *ret, *retp; | |
3525 | int ret_len; | |
3526 | char **p; | |
3527 | ||
3528 | /* Check if errmsg just need simple return. */ | |
3529 | if (error_tag != bfd_error_file_ambiguously_recognized || matching == NULL) | |
3530 | return bfd_errmsg (error_tag); | |
3531 | ||
3532 | ret_len = strlen (bfd_errmsg (error_tag)) + strlen (AMBIGUOUS_MESS1) | |
3533 | + strlen (AMBIGUOUS_MESS2); | |
3534 | for (p = matching; *p; p++) | |
3535 | ret_len += strlen (*p) + 1; | |
3536 | ret = xmalloc (ret_len + 1); | |
3537 | retp = ret; | |
3538 | make_cleanup (xfree, ret); | |
3539 | ||
3540 | strcpy (retp, bfd_errmsg (error_tag)); | |
3541 | retp += strlen (retp); | |
3542 | ||
3543 | strcpy (retp, AMBIGUOUS_MESS1); | |
3544 | retp += strlen (retp); | |
3545 | ||
3546 | for (p = matching; *p; p++) | |
3547 | { | |
3548 | sprintf (retp, " %s", *p); | |
3549 | retp += strlen (retp); | |
3550 | } | |
3551 | xfree (matching); | |
3552 | ||
3553 | strcpy (retp, AMBIGUOUS_MESS2); | |
3554 | ||
3555 | return ret; | |
3556 | } | |
3557 | ||
74164c56 JK |
3558 | /* Return ARGS parsed as a valid pid, or throw an error. */ |
3559 | ||
3560 | int | |
3561 | parse_pid_to_attach (char *args) | |
3562 | { | |
3563 | unsigned long pid; | |
3564 | char *dummy; | |
3565 | ||
3566 | if (!args) | |
3567 | error_no_arg (_("process-id to attach")); | |
3568 | ||
3569 | dummy = args; | |
3570 | pid = strtoul (args, &dummy, 0); | |
3571 | /* Some targets don't set errno on errors, grrr! */ | |
3572 | if ((pid == 0 && dummy == args) || dummy != &args[strlen (args)]) | |
3573 | error (_("Illegal process-id: %s."), args); | |
3574 | ||
3575 | return pid; | |
3576 | } | |
3577 | ||
353d1d73 JK |
3578 | /* Helper for make_bpstat_clear_actions_cleanup. */ |
3579 | ||
3580 | static void | |
3581 | do_bpstat_clear_actions_cleanup (void *unused) | |
3582 | { | |
3583 | bpstat_clear_actions (); | |
3584 | } | |
3585 | ||
3586 | /* Call bpstat_clear_actions for the case an exception is throw. You should | |
3587 | discard_cleanups if no exception is caught. */ | |
3588 | ||
3589 | struct cleanup * | |
3590 | make_bpstat_clear_actions_cleanup (void) | |
3591 | { | |
3592 | return make_cleanup (do_bpstat_clear_actions_cleanup, NULL); | |
3593 | } | |
3594 | ||
df15bd07 JK |
3595 | /* Check for GCC >= 4.x according to the symtab->producer string. Return minor |
3596 | version (x) of 4.x in such case. If it is not GCC or it is GCC older than | |
3597 | 4.x return -1. If it is GCC 5.x or higher return INT_MAX. */ | |
3598 | ||
3599 | int | |
3600 | producer_is_gcc_ge_4 (const char *producer) | |
3601 | { | |
3602 | const char *cs; | |
3603 | int major, minor; | |
3604 | ||
3605 | if (producer == NULL) | |
3606 | { | |
3607 | /* For unknown compilers expect their behavior is not compliant. For GCC | |
3608 | this case can also happen for -gdwarf-4 type units supported since | |
3609 | gcc-4.5. */ | |
3610 | ||
3611 | return -1; | |
3612 | } | |
3613 | ||
3614 | /* Skip any identifier after "GNU " - such as "C++" or "Java". */ | |
3615 | ||
3616 | if (strncmp (producer, "GNU ", strlen ("GNU ")) != 0) | |
3617 | { | |
3618 | /* For non-GCC compilers expect their behavior is not compliant. */ | |
3619 | ||
3620 | return -1; | |
3621 | } | |
3622 | cs = &producer[strlen ("GNU ")]; | |
3623 | while (*cs && !isdigit (*cs)) | |
3624 | cs++; | |
3625 | if (sscanf (cs, "%d.%d", &major, &minor) != 2) | |
3626 | { | |
3627 | /* Not recognized as GCC. */ | |
3628 | ||
3629 | return -1; | |
3630 | } | |
3631 | ||
3632 | if (major < 4) | |
3633 | return -1; | |
3634 | if (major > 4) | |
3635 | return INT_MAX; | |
3636 | return minor; | |
3637 | } | |
3638 | ||
e4ab2fad JK |
3639 | /* Call xfree for each element of CHAR_PTR_VEC and final VEC_free for |
3640 | CHAR_PTR_VEC itself. | |
3641 | ||
3642 | You must not modify CHAR_PTR_VEC after it got registered with this function | |
3643 | by make_cleanup as the CHAR_PTR_VEC base address may change on its updates. | |
3644 | Contrary to VEC_free this function does not (cannot) clear the pointer. */ | |
3645 | ||
3646 | void | |
3647 | free_char_ptr_vec (VEC (char_ptr) *char_ptr_vec) | |
3648 | { | |
3649 | int ix; | |
3650 | char *name; | |
3651 | ||
3652 | for (ix = 0; VEC_iterate (char_ptr, char_ptr_vec, ix, name); ++ix) | |
3653 | xfree (name); | |
3654 | VEC_free (char_ptr, char_ptr_vec); | |
3655 | } | |
3656 | ||
3657 | /* Helper for make_cleanup_free_char_ptr_vec. */ | |
3658 | ||
3659 | static void | |
3660 | do_free_char_ptr_vec (void *arg) | |
3661 | { | |
3662 | VEC (char_ptr) *char_ptr_vec = arg; | |
3663 | ||
3664 | free_char_ptr_vec (char_ptr_vec); | |
3665 | } | |
3666 | ||
3667 | /* Make cleanup handler calling xfree for each element of CHAR_PTR_VEC and | |
3668 | final VEC_free for CHAR_PTR_VEC itself. | |
3669 | ||
3670 | You must not modify CHAR_PTR_VEC after this cleanup registration as the | |
3671 | CHAR_PTR_VEC base address may change on its updates. Contrary to VEC_free | |
3672 | this function does not (cannot) clear the pointer. */ | |
3673 | ||
3674 | struct cleanup * | |
3675 | make_cleanup_free_char_ptr_vec (VEC (char_ptr) *char_ptr_vec) | |
3676 | { | |
3677 | return make_cleanup (do_free_char_ptr_vec, char_ptr_vec); | |
3678 | } | |
3679 | ||
3680 | /* Extended version of dirnames_to_char_ptr_vec - additionally if *VECP is | |
3681 | non-NULL the new list elements from DIRNAMES are appended to the existing | |
3682 | *VECP list of entries. *VECP address will be updated by this call. */ | |
3683 | ||
3684 | void | |
3685 | dirnames_to_char_ptr_vec_append (VEC (char_ptr) **vecp, const char *dirnames) | |
3686 | { | |
3687 | do | |
3688 | { | |
3689 | size_t this_len; | |
3690 | char *next_dir, *this_dir; | |
3691 | ||
3692 | next_dir = strchr (dirnames, DIRNAME_SEPARATOR); | |
3693 | if (next_dir == NULL) | |
3694 | this_len = strlen (dirnames); | |
3695 | else | |
3696 | { | |
3697 | this_len = next_dir - dirnames; | |
3698 | next_dir++; | |
3699 | } | |
3700 | ||
3701 | this_dir = xmalloc (this_len + 1); | |
3702 | memcpy (this_dir, dirnames, this_len); | |
3703 | this_dir[this_len] = '\0'; | |
3704 | VEC_safe_push (char_ptr, *vecp, this_dir); | |
3705 | ||
3706 | dirnames = next_dir; | |
3707 | } | |
3708 | while (dirnames != NULL); | |
3709 | } | |
3710 | ||
3711 | /* Split DIRNAMES by DIRNAME_SEPARATOR delimiter and return a list of all the | |
3712 | elements in their original order. For empty string ("") DIRNAMES return | |
3713 | list of one empty string ("") element. | |
3714 | ||
3715 | You may modify the returned strings. | |
3716 | Read free_char_ptr_vec for its cleanup. */ | |
3717 | ||
3718 | VEC (char_ptr) * | |
3719 | dirnames_to_char_ptr_vec (const char *dirnames) | |
3720 | { | |
3721 | VEC (char_ptr) *retval = NULL; | |
3722 | ||
3723 | dirnames_to_char_ptr_vec_append (&retval, dirnames); | |
3724 | ||
3725 | return retval; | |
3726 | } | |
3727 | ||
6dea1fbd JK |
3728 | /* Substitute all occurences of string FROM by string TO in *STRINGP. *STRINGP |
3729 | must come from xrealloc-compatible allocator and it may be updated. FROM | |
1564a261 JK |
3730 | needs to be delimited by IS_DIR_SEPARATOR or DIRNAME_SEPARATOR (or be |
3731 | located at the start or end of *STRINGP. */ | |
6dea1fbd JK |
3732 | |
3733 | void | |
3734 | substitute_path_component (char **stringp, const char *from, const char *to) | |
3735 | { | |
3736 | char *string = *stringp, *s; | |
3737 | const size_t from_len = strlen (from); | |
3738 | const size_t to_len = strlen (to); | |
3739 | ||
3740 | for (s = string;;) | |
3741 | { | |
3742 | s = strstr (s, from); | |
3743 | if (s == NULL) | |
3744 | break; | |
3745 | ||
1564a261 JK |
3746 | if ((s == string || IS_DIR_SEPARATOR (s[-1]) |
3747 | || s[-1] == DIRNAME_SEPARATOR) | |
3748 | && (s[from_len] == '\0' || IS_DIR_SEPARATOR (s[from_len]) | |
3749 | || s[from_len] == DIRNAME_SEPARATOR)) | |
6dea1fbd JK |
3750 | { |
3751 | char *string_new; | |
3752 | ||
3753 | string_new = xrealloc (string, (strlen (string) + to_len + 1)); | |
3754 | ||
3755 | /* Relocate the current S pointer. */ | |
3756 | s = s - string + string_new; | |
3757 | string = string_new; | |
3758 | ||
3759 | /* Replace from by to. */ | |
3760 | memmove (&s[to_len], &s[from_len], strlen (&s[from_len]) + 1); | |
3761 | memcpy (s, to, to_len); | |
3762 | ||
3763 | s += to_len; | |
3764 | } | |
3765 | else | |
3766 | s++; | |
3767 | } | |
3768 | ||
3769 | *stringp = string; | |
3770 | } | |
3771 | ||
0b6cb71e DE |
3772 | #ifdef HAVE_WAITPID |
3773 | ||
3774 | #ifdef SIGALRM | |
3775 | ||
3776 | /* SIGALRM handler for waitpid_with_timeout. */ | |
3777 | ||
3778 | static void | |
3779 | sigalrm_handler (int signo) | |
3780 | { | |
3781 | /* Nothing to do. */ | |
3782 | } | |
3783 | ||
3784 | #endif | |
3785 | ||
3786 | /* Wrapper to wait for child PID to die with TIMEOUT. | |
3787 | TIMEOUT is the time to stop waiting in seconds. | |
3788 | If TIMEOUT is zero, pass WNOHANG to waitpid. | |
3789 | Returns PID if it was successfully waited for, otherwise -1. | |
3790 | ||
3791 | Timeouts are currently implemented with alarm and SIGALRM. | |
3792 | If the host does not support them, this waits "forever". | |
3793 | It would be odd though for a host to have waitpid and not SIGALRM. */ | |
3794 | ||
3795 | pid_t | |
3796 | wait_to_die_with_timeout (pid_t pid, int *status, int timeout) | |
3797 | { | |
3798 | pid_t waitpid_result; | |
3799 | ||
3800 | gdb_assert (pid > 0); | |
3801 | gdb_assert (timeout >= 0); | |
3802 | ||
3803 | if (timeout > 0) | |
3804 | { | |
3805 | #ifdef SIGALRM | |
3806 | #if defined (HAVE_SIGACTION) && defined (SA_RESTART) | |
3807 | struct sigaction sa, old_sa; | |
3808 | ||
3809 | sa.sa_handler = sigalrm_handler; | |
3810 | sigemptyset (&sa.sa_mask); | |
3811 | sa.sa_flags = 0; | |
3812 | sigaction (SIGALRM, &sa, &old_sa); | |
3813 | #else | |
3814 | void (*ofunc) (); | |
3815 | ||
3816 | ofunc = (void (*)()) signal (SIGALRM, sigalrm_handler); | |
3817 | #endif | |
3818 | ||
3819 | alarm (timeout); | |
3820 | #endif | |
3821 | ||
3822 | waitpid_result = waitpid (pid, status, 0); | |
3823 | ||
3824 | #ifdef SIGALRM | |
3825 | alarm (0); | |
3826 | #if defined (HAVE_SIGACTION) && defined (SA_RESTART) | |
3827 | sigaction (SIGALRM, &old_sa, NULL); | |
3828 | #else | |
3829 | signal (SIGALRM, ofunc); | |
3830 | #endif | |
3831 | #endif | |
3832 | } | |
3833 | else | |
3834 | waitpid_result = waitpid (pid, status, WNOHANG); | |
3835 | ||
3836 | if (waitpid_result == pid) | |
3837 | return pid; | |
3838 | else | |
3839 | return -1; | |
3840 | } | |
3841 | ||
3842 | #endif /* HAVE_WAITPID */ | |
3843 | ||
202cbf1c JK |
3844 | /* Provide fnmatch compatible function for FNM_FILE_NAME matching of host files. |
3845 | Both FNM_FILE_NAME and FNM_NOESCAPE must be set in FLAGS. | |
3846 | ||
3847 | It handles correctly HAVE_DOS_BASED_FILE_SYSTEM and | |
3848 | HAVE_CASE_INSENSITIVE_FILE_SYSTEM. */ | |
3849 | ||
3850 | int | |
3851 | gdb_filename_fnmatch (const char *pattern, const char *string, int flags) | |
3852 | { | |
3853 | gdb_assert ((flags & FNM_FILE_NAME) != 0); | |
3854 | ||
3855 | /* It is unclear how '\' escaping vs. directory separator should coexist. */ | |
3856 | gdb_assert ((flags & FNM_NOESCAPE) != 0); | |
3857 | ||
3858 | #ifdef HAVE_DOS_BASED_FILE_SYSTEM | |
3859 | { | |
3860 | char *pattern_slash, *string_slash; | |
3861 | ||
3862 | /* Replace '\' by '/' in both strings. */ | |
3863 | ||
3864 | pattern_slash = alloca (strlen (pattern) + 1); | |
3865 | strcpy (pattern_slash, pattern); | |
3866 | pattern = pattern_slash; | |
3867 | for (; *pattern_slash != 0; pattern_slash++) | |
3868 | if (IS_DIR_SEPARATOR (*pattern_slash)) | |
3869 | *pattern_slash = '/'; | |
3870 | ||
3871 | string_slash = alloca (strlen (string) + 1); | |
3872 | strcpy (string_slash, string); | |
3873 | string = string_slash; | |
3874 | for (; *string_slash != 0; string_slash++) | |
3875 | if (IS_DIR_SEPARATOR (*string_slash)) | |
3876 | *string_slash = '/'; | |
3877 | } | |
3878 | #endif /* HAVE_DOS_BASED_FILE_SYSTEM */ | |
3879 | ||
3880 | #ifdef HAVE_CASE_INSENSITIVE_FILE_SYSTEM | |
3881 | flags |= FNM_CASEFOLD; | |
3882 | #endif /* HAVE_CASE_INSENSITIVE_FILE_SYSTEM */ | |
3883 | ||
3884 | return fnmatch (pattern, string, flags); | |
3885 | } | |
3886 | ||
2c0b251b PA |
3887 | /* Provide a prototype to silence -Wmissing-prototypes. */ |
3888 | extern initialize_file_ftype _initialize_utils; | |
3889 | ||
3c16cced PA |
3890 | void |
3891 | _initialize_utils (void) | |
3892 | { | |
3893 | add_internal_problem_command (&internal_error_problem); | |
3894 | add_internal_problem_command (&internal_warning_problem); | |
3895 | } |