1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
|
/* GDB/Scheme exception support.
Copyright (C) 2014-2015 Free Software Foundation, Inc.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* See README file in this directory for implementation notes, coding
conventions, et.al. */
/* Notes:
IWBN to support SRFI 34/35. At the moment we follow Guile's own
exception mechanism.
The non-static functions in this file have prefix gdbscm_ and
not exscm_ on purpose. */
#include "defs.h"
#include <signal.h>
#include "guile-internal.h"
/* The <gdb:exception> smob.
This is used to record and handle Scheme exceptions.
One important invariant is that <gdb:exception> smobs are never a valid
result of a function, other than to signify an exception occurred. */
typedef struct
{
/* This always appears first. */
gdb_smob base;
/* The key and args parameters to "throw". */
SCM key;
SCM args;
} exception_smob;
static const char exception_smob_name[] = "gdb:exception";
/* The tag Guile knows the exception smob by. */
static scm_t_bits exception_smob_tag;
/* A generic error in struct gdb_exception.
I.e., not RETURN_QUIT and not MEMORY_ERROR. */
static SCM error_symbol;
/* An error occurred accessing inferior memory.
This is not a Scheme programming error. */
static SCM memory_error_symbol;
/* User interrupt, e.g., RETURN_QUIT in struct gdb_exception. */
static SCM signal_symbol;
/* A user error, e.g., bad arg to gdb command. */
static SCM user_error_symbol;
/* Printing the stack is done by first capturing the stack and recording it in
a <gdb:exception> object with this key and with the ARGS field set to
(cons real-key (cons stack real-args)).
See gdbscm_make_exception_with_stack. */
static SCM with_stack_error_symbol;
/* The key to use for an invalid object exception. An invalid object is one
where the underlying object has been removed from GDB. */
SCM gdbscm_invalid_object_error_symbol;
/* Values for "guile print-stack" as symbols. */
static SCM none_symbol;
static SCM message_symbol;
static SCM full_symbol;
static const char percent_print_exception_message_name[] =
"%print-exception-message";
/* Variable containing %print-exception-message.
It is not defined until late in initialization, after our init routine
has run. Cope by looking it up lazily. */
static SCM percent_print_exception_message_var = SCM_BOOL_F;
static const char percent_print_exception_with_stack_name[] =
"%print-exception-with-stack";
/* Variable containing %print-exception-with-stack.
It is not defined until late in initialization, after our init routine
has run. Cope by looking it up lazily. */
static SCM percent_print_exception_with_stack_var = SCM_BOOL_F;
/* Counter to keep track of the number of times we create a <gdb:exception>
object, for performance monitoring purposes. */
static unsigned long gdbscm_exception_count = 0;
/* Administrivia for exception smobs. */
/* The smob "print" function for <gdb:exception>. */
static int
exscm_print_exception_smob (SCM self, SCM port, scm_print_state *pstate)
{
exception_smob *e_smob = (exception_smob *) SCM_SMOB_DATA (self);
gdbscm_printf (port, "#<%s ", exception_smob_name);
scm_write (e_smob->key, port);
scm_puts (" ", port);
scm_write (e_smob->args, port);
scm_puts (">", port);
scm_remember_upto_here_1 (self);
/* Non-zero means success. */
return 1;
}
/* (make-exception key args) -> <gdb:exception> */
SCM
gdbscm_make_exception (SCM key, SCM args)
{
exception_smob *e_smob = (exception_smob *)
scm_gc_malloc (sizeof (exception_smob), exception_smob_name);
SCM smob;
e_smob->key = key;
e_smob->args = args;
smob = scm_new_smob (exception_smob_tag, (scm_t_bits) e_smob);
gdbscm_init_gsmob (&e_smob->base);
++gdbscm_exception_count;
return smob;
}
/* Return non-zero if SCM is a <gdb:exception> object. */
int
gdbscm_is_exception (SCM scm)
{
return SCM_SMOB_PREDICATE (exception_smob_tag, scm);
}
/* (exception? scm) -> boolean */
static SCM
gdbscm_exception_p (SCM scm)
{
return scm_from_bool (gdbscm_is_exception (scm));
}
/* (exception-key <gdb:exception>) -> key */
SCM
gdbscm_exception_key (SCM self)
{
exception_smob *e_smob;
SCM_ASSERT_TYPE (gdbscm_is_exception (self), self, SCM_ARG1, FUNC_NAME,
"gdb:exception");
e_smob = (exception_smob *) SCM_SMOB_DATA (self);
return e_smob->key;
}
/* (exception-args <gdb:exception>) -> arg-list */
SCM
gdbscm_exception_args (SCM self)
{
exception_smob *e_smob;
SCM_ASSERT_TYPE (gdbscm_is_exception (self), self, SCM_ARG1, FUNC_NAME,
"gdb:exception");
e_smob = (exception_smob *) SCM_SMOB_DATA (self);
return e_smob->args;
}
/* Wrap an exception in a <gdb:exception> object that includes STACK.
gdbscm_print_exception_with_stack knows how to unwrap it. */
SCM
gdbscm_make_exception_with_stack (SCM key, SCM args, SCM stack)
{
return gdbscm_make_exception (with_stack_error_symbol,
scm_cons (key, scm_cons (stack, args)));
}
/* Version of scm_error_scm that creates a gdb:exception object that can later
be passed to gdbscm_throw.
KEY is a symbol denoting the kind of error.
SUBR is either #f or a string marking the function in which the error
occurred.
MESSAGE is either #f or the error message string. It may contain ~a and ~s
modifiers, provided by ARGS.
ARGS is a list of args to MESSAGE.
DATA is an arbitrary object, its value depends on KEY. The value to pass
here is a bit underspecified by Guile. */
SCM
gdbscm_make_error_scm (SCM key, SCM subr, SCM message, SCM args, SCM data)
{
return gdbscm_make_exception (key, scm_list_4 (subr, message, args, data));
}
/* Version of scm_error that creates a gdb:exception object that can later
be passed to gdbscm_throw.
See gdbscm_make_error_scm for a description of the arguments. */
SCM
gdbscm_make_error (SCM key, const char *subr, const char *message,
SCM args, SCM data)
{
return gdbscm_make_error_scm
(key,
subr == NULL ? SCM_BOOL_F : scm_from_latin1_string (subr),
message == NULL ? SCM_BOOL_F : scm_from_latin1_string (message),
args, data);
}
/* Version of SCM_ASSERT_TYPE/scm_wrong_type_arg_msg that creates a
gdb:exception object that can later be passed to gdbscm_throw. */
SCM
gdbscm_make_type_error (const char *subr, int arg_pos, SCM bad_value,
const char *expected_type)
{
char *msg;
SCM result;
if (arg_pos > 0)
{
if (expected_type != NULL)
{
msg = xstrprintf (_("Wrong type argument in position %d"
" (expecting %s): ~S"),
arg_pos, expected_type);
}
else
{
msg = xstrprintf (_("Wrong type argument in position %d: ~S"),
arg_pos);
}
}
else
{
if (expected_type != NULL)
{
msg = xstrprintf (_("Wrong type argument (expecting %s): ~S"),
expected_type);
}
else
msg = xstrprintf (_("Wrong type argument: ~S"));
}
result = gdbscm_make_error (scm_arg_type_key, subr, msg,
scm_list_1 (bad_value), scm_list_1 (bad_value));
xfree (msg);
return result;
}
/* A variant of gdbscm_make_type_error for non-type argument errors.
ERROR_PREFIX and ERROR are combined to build the error message.
Care needs to be taken so that the i18n composed form is still
reasonable, but no one is going to translate these anyway so we don't
worry too much.
ERROR_PREFIX may be NULL, ERROR may not be NULL. */
static SCM
gdbscm_make_arg_error (SCM key, const char *subr, int arg_pos, SCM bad_value,
const char *error_prefix, const char *error)
{
char *msg;
SCM result;
if (error_prefix != NULL)
{
if (arg_pos > 0)
{
msg = xstrprintf (_("%s %s in position %d: ~S"),
error_prefix, error, arg_pos);
}
else
msg = xstrprintf (_("%s %s: ~S"), error_prefix, error);
}
else
{
if (arg_pos > 0)
msg = xstrprintf (_("%s in position %d: ~S"), error, arg_pos);
else
msg = xstrprintf (_("%s: ~S"), error);
}
result = gdbscm_make_error (key, subr, msg,
scm_list_1 (bad_value), scm_list_1 (bad_value));
xfree (msg);
return result;
}
/* Make an invalid-object error <gdb:exception> object.
OBJECT is the name of the kind of object that is invalid. */
SCM
gdbscm_make_invalid_object_error (const char *subr, int arg_pos, SCM bad_value,
const char *object)
{
return gdbscm_make_arg_error (gdbscm_invalid_object_error_symbol,
subr, arg_pos, bad_value,
_("Invalid object:"), object);
}
/* Throw an invalid-object error.
OBJECT is the name of the kind of object that is invalid. */
void
gdbscm_invalid_object_error (const char *subr, int arg_pos, SCM bad_value,
const char *object)
{
SCM exception
= gdbscm_make_invalid_object_error (subr, arg_pos, bad_value, object);
gdbscm_throw (exception);
}
/* Make an out-of-range error <gdb:exception> object. */
SCM
gdbscm_make_out_of_range_error (const char *subr, int arg_pos, SCM bad_value,
const char *error)
{
return gdbscm_make_arg_error (scm_out_of_range_key,
subr, arg_pos, bad_value,
_("Out of range:"), error);
}
/* Throw an out-of-range error.
This is the standard Guile out-of-range exception. */
void
gdbscm_out_of_range_error (const char *subr, int arg_pos, SCM bad_value,
const char *error)
{
SCM exception
= gdbscm_make_out_of_range_error (subr, arg_pos, bad_value, error);
gdbscm_throw (exception);
}
/* Make a misc-error <gdb:exception> object. */
SCM
gdbscm_make_misc_error (const char *subr, int arg_pos, SCM bad_value,
const char *error)
{
return gdbscm_make_arg_error (scm_misc_error_key,
subr, arg_pos, bad_value, NULL, error);
}
/* Throw a misc-error error. */
void
gdbscm_misc_error (const char *subr, int arg_pos, SCM bad_value,
const char *error)
{
SCM exception = gdbscm_make_misc_error (subr, arg_pos, bad_value, error);
gdbscm_throw (exception);
}
/* Return a <gdb:exception> object for gdb:memory-error. */
SCM
gdbscm_make_memory_error (const char *subr, const char *msg, SCM args)
{
return gdbscm_make_error (memory_error_symbol, subr, msg, args,
SCM_EOL);
}
/* Throw a gdb:memory-error exception. */
void
gdbscm_memory_error (const char *subr, const char *msg, SCM args)
{
SCM exception = gdbscm_make_memory_error (subr, msg, args);
gdbscm_throw (exception);
}
/* Return non-zero if KEY is gdb:memory-error.
Note: This is an excp_matcher_func function. */
int
gdbscm_memory_error_p (SCM key)
{
return scm_is_eq (key, memory_error_symbol);
}
/* Return non-zero if KEY is gdb:user-error.
Note: This is an excp_matcher_func function. */
int
gdbscm_user_error_p (SCM key)
{
return scm_is_eq (key, user_error_symbol);
}
/* Wrapper around scm_throw to throw a gdb:exception.
This function does not return.
This function cannot be called from inside TRY_CATCH. */
void
gdbscm_throw (SCM exception)
{
scm_throw (gdbscm_exception_key (exception),
gdbscm_exception_args (exception));
gdb_assert_not_reached ("scm_throw returned");
}
/* Convert a GDB exception to a <gdb:exception> object. */
SCM
gdbscm_scm_from_gdb_exception (struct gdb_exception exception)
{
SCM key;
if (exception.reason == RETURN_QUIT)
{
/* Handle this specially to be consistent with top-repl.scm. */
return gdbscm_make_error (signal_symbol, NULL, _("User interrupt"),
SCM_EOL, scm_list_1 (scm_from_int (SIGINT)));
}
if (exception.error == MEMORY_ERROR)
key = memory_error_symbol;
else
key = error_symbol;
return gdbscm_make_error (key, NULL, "~A",
scm_list_1 (gdbscm_scm_from_c_string
(exception.message)),
SCM_BOOL_F);
}
/* Convert a GDB exception to the appropriate Scheme exception and throw it.
This function does not return. */
void
gdbscm_throw_gdb_exception (struct gdb_exception exception)
{
gdbscm_throw (gdbscm_scm_from_gdb_exception (exception));
}
/* Print the error message portion of an exception.
If PORT is #f, use the standard error port.
KEY cannot be gdb:with-stack.
Basically this function is just a wrapper around calling
%print-exception-message. */
static void
gdbscm_print_exception_message (SCM port, SCM frame, SCM key, SCM args)
{
SCM printer, status;
if (gdbscm_is_false (port))
port = scm_current_error_port ();
gdb_assert (!scm_is_eq (key, with_stack_error_symbol));
/* This does not use scm_print_exception because we tweak the output a bit.
Compare Guile's print-exception with our %print-exception-message for
details. */
if (gdbscm_is_false (percent_print_exception_message_var))
{
percent_print_exception_message_var
= scm_c_private_variable (gdbscm_init_module_name,
percent_print_exception_message_name);
/* If we can't find %print-exception-message, there's a problem on the
Scheme side. Don't kill GDB, just flag an error and leave it at
that. */
if (gdbscm_is_false (percent_print_exception_message_var))
{
gdbscm_printf (port, _("Error in Scheme exception printing,"
" can't find %s.\n"),
percent_print_exception_message_name);
return;
}
}
printer = scm_variable_ref (percent_print_exception_message_var);
status = gdbscm_safe_call_4 (printer, port, frame, key, args, NULL);
/* If that failed still tell the user something.
But don't use the exception printing machinery! */
if (gdbscm_is_exception (status))
{
gdbscm_printf (port, _("Error in Scheme exception printing:\n"));
scm_display (status, port);
scm_newline (port);
}
}
/* Print the description of exception KEY, ARGS to PORT, according to the
setting of "set guile print-stack".
If PORT is #f, use the standard error port.
If STACK is #f, never print the stack, regardless of whether printing it
is enabled. If STACK is #t, then print it if it is contained in ARGS
(i.e., KEY is gdb:with-stack). Otherwise STACK is the result of calling
scm_make_stack (which will be ignored in favor of the stack in ARGS if
KEY is gdb:with-stack).
KEY, ARGS are the standard arguments to scm_throw, et.al.
Basically this function is just a wrapper around calling
%print-exception-with-stack. */
void
gdbscm_print_exception_with_stack (SCM port, SCM stack, SCM key, SCM args)
{
SCM printer, status;
if (gdbscm_is_false (port))
port = scm_current_error_port ();
if (gdbscm_is_false (percent_print_exception_with_stack_var))
{
percent_print_exception_with_stack_var
= scm_c_private_variable (gdbscm_init_module_name,
percent_print_exception_with_stack_name);
/* If we can't find %print-exception-with-stack, there's a problem on the
Scheme side. Don't kill GDB, just flag an error and leave it at
that. */
if (gdbscm_is_false (percent_print_exception_with_stack_var))
{
gdbscm_printf (port, _("Error in Scheme exception printing,"
" can't find %s.\n"),
percent_print_exception_with_stack_name);
return;
}
}
printer = scm_variable_ref (percent_print_exception_with_stack_var);
status = gdbscm_safe_call_4 (printer, port, stack, key, args, NULL);
/* If that failed still tell the user something.
But don't use the exception printing machinery! */
if (gdbscm_is_exception (status))
{
gdbscm_printf (port, _("Error in Scheme exception printing:\n"));
scm_display (status, port);
scm_newline (port);
}
}
/* Print EXCEPTION, a <gdb:exception> object, to PORT.
If PORT is #f, use the standard error port. */
void
gdbscm_print_gdb_exception (SCM port, SCM exception)
{
gdb_assert (gdbscm_is_exception (exception));
gdbscm_print_exception_with_stack (port, SCM_BOOL_T,
gdbscm_exception_key (exception),
gdbscm_exception_args (exception));
}
/* Return a string description of <gdb:exception> EXCEPTION.
If EXCEPTION is a gdb:with-stack exception, unwrap it, a backtrace
is never returned as part of the result.
Space for the result is malloc'd, the caller must free. */
char *
gdbscm_exception_message_to_string (SCM exception)
{
SCM port = scm_open_output_string ();
SCM key, args;
char *result;
gdb_assert (gdbscm_is_exception (exception));
key = gdbscm_exception_key (exception);
args = gdbscm_exception_args (exception);
if (scm_is_eq (key, with_stack_error_symbol)
/* Don't crash on a badly generated gdb:with-stack exception. */
&& scm_is_pair (args)
&& scm_is_pair (scm_cdr (args)))
{
key = scm_car (args);
args = scm_cddr (args);
}
gdbscm_print_exception_message (port, SCM_BOOL_F, key, args);
result = gdbscm_scm_to_c_string (scm_get_output_string (port));
scm_close_port (port);
return result;
}
/* Return the value of the "guile print-stack" option as one of:
'none, 'message, 'full. */
static SCM
gdbscm_percent_exception_print_style (void)
{
if (gdbscm_print_excp == gdbscm_print_excp_none)
return none_symbol;
if (gdbscm_print_excp == gdbscm_print_excp_message)
return message_symbol;
if (gdbscm_print_excp == gdbscm_print_excp_full)
return full_symbol;
gdb_assert_not_reached ("bad value for \"guile print-stack\"");
}
/* Return the current <gdb:exception> counter.
This is for debugging purposes. */
static SCM
gdbscm_percent_exception_count (void)
{
return scm_from_ulong (gdbscm_exception_count);
}
/* Initialize the Scheme exception support. */
static const scheme_function exception_functions[] =
{
{ "make-exception", 2, 0, 0, as_a_scm_t_subr (gdbscm_make_exception),
"\
Create a <gdb:exception> object.\n\
\n\
Arguments: key args\n\
These are the standard key,args arguments of \"throw\"." },
{ "exception?", 1, 0, 0, as_a_scm_t_subr (gdbscm_exception_p),
"\
Return #t if the object is a <gdb:exception> object." },
{ "exception-key", 1, 0, 0, as_a_scm_t_subr (gdbscm_exception_key),
"\
Return the exception's key." },
{ "exception-args", 1, 0, 0, as_a_scm_t_subr (gdbscm_exception_args),
"\
Return the exception's arg list." },
END_FUNCTIONS
};
static const scheme_function private_exception_functions[] =
{
{ "%exception-print-style", 0, 0, 0,
as_a_scm_t_subr (gdbscm_percent_exception_print_style),
"\
Return the value of the \"guile print-stack\" option." },
{ "%exception-count", 0, 0, 0,
as_a_scm_t_subr (gdbscm_percent_exception_count),
"\
Return a count of the number of <gdb:exception> objects created.\n\
This is for debugging purposes." },
END_FUNCTIONS
};
void
gdbscm_initialize_exceptions (void)
{
exception_smob_tag = gdbscm_make_smob_type (exception_smob_name,
sizeof (exception_smob));
scm_set_smob_print (exception_smob_tag, exscm_print_exception_smob);
gdbscm_define_functions (exception_functions, 1);
gdbscm_define_functions (private_exception_functions, 0);
error_symbol = scm_from_latin1_symbol ("gdb:error");
memory_error_symbol = scm_from_latin1_symbol ("gdb:memory-error");
user_error_symbol = scm_from_latin1_symbol ("gdb:user-error");
gdbscm_invalid_object_error_symbol
= scm_from_latin1_symbol ("gdb:invalid-object-error");
with_stack_error_symbol = scm_from_latin1_symbol ("gdb:with-stack");
/* The text of this symbol is taken from Guile's top-repl.scm. */
signal_symbol = scm_from_latin1_symbol ("signal");
none_symbol = scm_from_latin1_symbol ("none");
message_symbol = scm_from_latin1_symbol ("message");
full_symbol = scm_from_latin1_symbol ("full");
}
|