| 1 | /* Interface between gdb and its extension languages. |
| 2 | |
| 3 | Copyright (C) 2014-2016 Free Software Foundation, Inc. |
| 4 | |
| 5 | This file is part of GDB. |
| 6 | |
| 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 |
| 9 | the Free Software Foundation; either version 3 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 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. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | /* Note: With few exceptions, external functions and variables in this file |
| 21 | have "ext_lang" in the name, and no other symbol in gdb does. */ |
| 22 | |
| 23 | #include "defs.h" |
| 24 | #include <signal.h> |
| 25 | #include "target.h" |
| 26 | #include "auto-load.h" |
| 27 | #include "breakpoint.h" |
| 28 | #include "event-top.h" |
| 29 | #include "extension.h" |
| 30 | #include "extension-priv.h" |
| 31 | #include "observer.h" |
| 32 | #include "cli/cli-script.h" |
| 33 | #include "python/python.h" |
| 34 | #include "guile/guile.h" |
| 35 | |
| 36 | /* Iterate over all external extension languages, regardless of whether the |
| 37 | support has been compiled in or not. |
| 38 | This does not include GDB's own scripting language. */ |
| 39 | |
| 40 | #define ALL_EXTENSION_LANGUAGES(i, extlang) \ |
| 41 | for (/*int*/ i = 0, extlang = extension_languages[0]; \ |
| 42 | extlang != NULL; \ |
| 43 | extlang = extension_languages[++i]) |
| 44 | |
| 45 | /* Iterate over all external extension languages that are supported. |
| 46 | This does not include GDB's own scripting language. */ |
| 47 | |
| 48 | #define ALL_ENABLED_EXTENSION_LANGUAGES(i, extlang) \ |
| 49 | for (/*int*/ i = 0, extlang = extension_languages[0]; \ |
| 50 | extlang != NULL; \ |
| 51 | extlang = extension_languages[++i]) \ |
| 52 | if (extlang->ops != NULL) |
| 53 | |
| 54 | static script_sourcer_func source_gdb_script; |
| 55 | static objfile_script_sourcer_func source_gdb_objfile_script; |
| 56 | |
| 57 | /* GDB's own scripting language. |
| 58 | This exists, in part, to support auto-loading ${prog}-gdb.gdb scripts. */ |
| 59 | |
| 60 | static const struct extension_language_script_ops |
| 61 | extension_language_gdb_script_ops = |
| 62 | { |
| 63 | source_gdb_script, |
| 64 | source_gdb_objfile_script, |
| 65 | NULL, /* objfile_script_executor */ |
| 66 | auto_load_gdb_scripts_enabled |
| 67 | }; |
| 68 | |
| 69 | const struct extension_language_defn extension_language_gdb = |
| 70 | { |
| 71 | EXT_LANG_GDB, |
| 72 | "gdb", |
| 73 | "GDB", |
| 74 | |
| 75 | /* We fall back to interpreting a script as a GDB script if it doesn't |
| 76 | match the other scripting languages, but for consistency's sake |
| 77 | give it a formal suffix. */ |
| 78 | ".gdb", |
| 79 | "-gdb.gdb", |
| 80 | |
| 81 | /* cli_control_type: This is never used: GDB's own scripting language |
| 82 | has a variety of control types (if, while, etc.). */ |
| 83 | commands_control, |
| 84 | |
| 85 | &extension_language_gdb_script_ops, |
| 86 | |
| 87 | /* The rest of the extension language interface isn't supported by GDB's own |
| 88 | extension/scripting language. */ |
| 89 | NULL |
| 90 | }; |
| 91 | |
| 92 | /* NULL-terminated table of all external (non-native) extension languages. |
| 93 | |
| 94 | The order of appearance in the table is important. |
| 95 | When multiple extension languages provide the same feature, for example |
| 96 | a pretty-printer for a particular type, which one gets used? |
| 97 | The algorithm employed here is "the first one wins". For example, in |
| 98 | the case of pretty-printers this means the first one to provide a |
| 99 | pretty-printed value is the one that is used. This algorithm is employed |
| 100 | throughout. */ |
| 101 | |
| 102 | static const struct extension_language_defn * const extension_languages[] = |
| 103 | { |
| 104 | /* To preserve existing behaviour, python should always appear first. */ |
| 105 | &extension_language_python, |
| 106 | &extension_language_guile, |
| 107 | NULL |
| 108 | }; |
| 109 | |
| 110 | /* Return a pointer to the struct extension_language_defn object of |
| 111 | extension language LANG. |
| 112 | This always returns a non-NULL pointer, even if support for the language |
| 113 | is not compiled into this copy of GDB. */ |
| 114 | |
| 115 | const struct extension_language_defn * |
| 116 | get_ext_lang_defn (enum extension_language lang) |
| 117 | { |
| 118 | int i; |
| 119 | const struct extension_language_defn *extlang; |
| 120 | |
| 121 | gdb_assert (lang != EXT_LANG_NONE); |
| 122 | |
| 123 | if (lang == EXT_LANG_GDB) |
| 124 | return &extension_language_gdb; |
| 125 | |
| 126 | ALL_EXTENSION_LANGUAGES (i, extlang) |
| 127 | { |
| 128 | if (extlang->language == lang) |
| 129 | return extlang; |
| 130 | } |
| 131 | |
| 132 | gdb_assert_not_reached ("unable to find extension_language_defn"); |
| 133 | } |
| 134 | |
| 135 | /* Return TRUE if FILE has extension EXTENSION. */ |
| 136 | |
| 137 | static int |
| 138 | has_extension (const char *file, const char *extension) |
| 139 | { |
| 140 | int file_len = strlen (file); |
| 141 | int extension_len = strlen (extension); |
| 142 | |
| 143 | return (file_len > extension_len |
| 144 | && strcmp (&file[file_len - extension_len], extension) == 0); |
| 145 | } |
| 146 | |
| 147 | /* Return the extension language of FILE, or NULL if |
| 148 | the extension language of FILE is not recognized. |
| 149 | This is done by looking at the file's suffix. */ |
| 150 | |
| 151 | const struct extension_language_defn * |
| 152 | get_ext_lang_of_file (const char *file) |
| 153 | { |
| 154 | int i; |
| 155 | const struct extension_language_defn *extlang; |
| 156 | |
| 157 | ALL_EXTENSION_LANGUAGES (i, extlang) |
| 158 | { |
| 159 | if (has_extension (file, extlang->suffix)) |
| 160 | return extlang; |
| 161 | } |
| 162 | |
| 163 | return NULL; |
| 164 | } |
| 165 | |
| 166 | /* Return non-zero if support for the specified extension language |
| 167 | is compiled in. */ |
| 168 | |
| 169 | int |
| 170 | ext_lang_present_p (const struct extension_language_defn *extlang) |
| 171 | { |
| 172 | return extlang->script_ops != NULL; |
| 173 | } |
| 174 | |
| 175 | /* Return non-zero if the specified extension language has successfully |
| 176 | initialized. */ |
| 177 | |
| 178 | int |
| 179 | ext_lang_initialized_p (const struct extension_language_defn *extlang) |
| 180 | { |
| 181 | if (extlang->ops != NULL) |
| 182 | { |
| 183 | /* This method is required. */ |
| 184 | gdb_assert (extlang->ops->initialized != NULL); |
| 185 | return extlang->ops->initialized (extlang); |
| 186 | } |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | /* Throw an error indicating EXTLANG is not supported in this copy of GDB. */ |
| 192 | |
| 193 | void |
| 194 | throw_ext_lang_unsupported (const struct extension_language_defn *extlang) |
| 195 | { |
| 196 | error (_("Scripting in the \"%s\" language is not supported" |
| 197 | " in this copy of GDB."), |
| 198 | ext_lang_capitalized_name (extlang)); |
| 199 | } |
| 200 | \f |
| 201 | /* Methods for GDB's own extension/scripting language. */ |
| 202 | |
| 203 | /* The extension_language_script_ops.script_sourcer "method". */ |
| 204 | |
| 205 | static void |
| 206 | source_gdb_script (const struct extension_language_defn *extlang, |
| 207 | FILE *stream, const char *file) |
| 208 | { |
| 209 | script_from_file (stream, file); |
| 210 | } |
| 211 | |
| 212 | /* The extension_language_script_ops.objfile_script_sourcer "method". */ |
| 213 | |
| 214 | static void |
| 215 | source_gdb_objfile_script (const struct extension_language_defn *extlang, |
| 216 | struct objfile *objfile, |
| 217 | FILE *stream, const char *file) |
| 218 | { |
| 219 | script_from_file (stream, file); |
| 220 | } |
| 221 | \f |
| 222 | /* Accessors for "public" attributes of struct extension_language. */ |
| 223 | |
| 224 | /* Return the "name" field of EXTLANG. */ |
| 225 | |
| 226 | const char * |
| 227 | ext_lang_name (const struct extension_language_defn *extlang) |
| 228 | { |
| 229 | return extlang->name; |
| 230 | } |
| 231 | |
| 232 | /* Return the "capitalized_name" field of EXTLANG. */ |
| 233 | |
| 234 | const char * |
| 235 | ext_lang_capitalized_name (const struct extension_language_defn *extlang) |
| 236 | { |
| 237 | return extlang->capitalized_name; |
| 238 | } |
| 239 | |
| 240 | /* Return the "suffix" field of EXTLANG. */ |
| 241 | |
| 242 | const char * |
| 243 | ext_lang_suffix (const struct extension_language_defn *extlang) |
| 244 | { |
| 245 | return extlang->suffix; |
| 246 | } |
| 247 | |
| 248 | /* Return the "auto_load_suffix" field of EXTLANG. */ |
| 249 | |
| 250 | const char * |
| 251 | ext_lang_auto_load_suffix (const struct extension_language_defn *extlang) |
| 252 | { |
| 253 | return extlang->auto_load_suffix; |
| 254 | } |
| 255 | \f |
| 256 | /* extension_language_script_ops wrappers. */ |
| 257 | |
| 258 | /* Return the script "sourcer" function for EXTLANG. |
| 259 | This is the function that loads and processes a script. |
| 260 | If support for this language isn't compiled in, NULL is returned. */ |
| 261 | |
| 262 | script_sourcer_func * |
| 263 | ext_lang_script_sourcer (const struct extension_language_defn *extlang) |
| 264 | { |
| 265 | if (extlang->script_ops == NULL) |
| 266 | return NULL; |
| 267 | |
| 268 | /* The extension language is required to implement this function. */ |
| 269 | gdb_assert (extlang->script_ops->script_sourcer != NULL); |
| 270 | |
| 271 | return extlang->script_ops->script_sourcer; |
| 272 | } |
| 273 | |
| 274 | /* Return the objfile script "sourcer" function for EXTLANG. |
| 275 | This is the function that loads and processes a script for a particular |
| 276 | objfile. |
| 277 | If support for this language isn't compiled in, NULL is returned. */ |
| 278 | |
| 279 | objfile_script_sourcer_func * |
| 280 | ext_lang_objfile_script_sourcer (const struct extension_language_defn *extlang) |
| 281 | { |
| 282 | if (extlang->script_ops == NULL) |
| 283 | return NULL; |
| 284 | |
| 285 | /* The extension language is required to implement this function. */ |
| 286 | gdb_assert (extlang->script_ops->objfile_script_sourcer != NULL); |
| 287 | |
| 288 | return extlang->script_ops->objfile_script_sourcer; |
| 289 | } |
| 290 | |
| 291 | /* Return the objfile script "executor" function for EXTLANG. |
| 292 | This is the function that executes a script for a particular objfile. |
| 293 | If support for this language isn't compiled in, NULL is returned. |
| 294 | The extension language is not required to implement this function. */ |
| 295 | |
| 296 | objfile_script_executor_func * |
| 297 | ext_lang_objfile_script_executor |
| 298 | (const struct extension_language_defn *extlang) |
| 299 | { |
| 300 | if (extlang->script_ops == NULL) |
| 301 | return NULL; |
| 302 | |
| 303 | return extlang->script_ops->objfile_script_executor; |
| 304 | } |
| 305 | |
| 306 | /* Return non-zero if auto-loading of EXTLANG scripts is enabled. |
| 307 | Zero is returned if support for this language isn't compiled in. */ |
| 308 | |
| 309 | int |
| 310 | ext_lang_auto_load_enabled (const struct extension_language_defn *extlang) |
| 311 | { |
| 312 | if (extlang->script_ops == NULL) |
| 313 | return 0; |
| 314 | |
| 315 | /* The extension language is required to implement this function. */ |
| 316 | gdb_assert (extlang->script_ops->auto_load_enabled != NULL); |
| 317 | |
| 318 | return extlang->script_ops->auto_load_enabled (extlang); |
| 319 | } |
| 320 | \f |
| 321 | /* Functions that iterate over all extension languages. |
| 322 | These only iterate over external extension languages, not including |
| 323 | GDB's own extension/scripting language, unless otherwise indicated. */ |
| 324 | |
| 325 | /* Wrapper to call the extension_language_ops.finish_initialization "method" |
| 326 | for each compiled-in extension language. */ |
| 327 | |
| 328 | void |
| 329 | finish_ext_lang_initialization (void) |
| 330 | { |
| 331 | int i; |
| 332 | const struct extension_language_defn *extlang; |
| 333 | |
| 334 | ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang) |
| 335 | { |
| 336 | if (extlang->ops->finish_initialization != NULL) |
| 337 | extlang->ops->finish_initialization (extlang); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | /* Invoke the appropriate extension_language_ops.eval_from_control_command |
| 342 | method to perform CMD, which is a list of commands in an extension language. |
| 343 | |
| 344 | This function is what implements, for example: |
| 345 | |
| 346 | python |
| 347 | print 42 |
| 348 | end |
| 349 | |
| 350 | in a GDB script. */ |
| 351 | |
| 352 | void |
| 353 | eval_ext_lang_from_control_command (struct command_line *cmd) |
| 354 | { |
| 355 | int i; |
| 356 | const struct extension_language_defn *extlang; |
| 357 | |
| 358 | ALL_EXTENSION_LANGUAGES (i, extlang) |
| 359 | { |
| 360 | if (extlang->cli_control_type == cmd->control_type) |
| 361 | { |
| 362 | if (extlang->ops != NULL |
| 363 | && extlang->ops->eval_from_control_command != NULL) |
| 364 | { |
| 365 | extlang->ops->eval_from_control_command (extlang, cmd); |
| 366 | return; |
| 367 | } |
| 368 | /* The requested extension language is not supported in this GDB. */ |
| 369 | throw_ext_lang_unsupported (extlang); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | gdb_assert_not_reached ("unknown extension language in command_line"); |
| 374 | } |
| 375 | |
| 376 | /* Search for and load scripts for OBJFILE written in extension languages. |
| 377 | This includes GDB's own scripting language. |
| 378 | |
| 379 | This function is what implements the loading of OBJFILE-gdb.py and |
| 380 | OBJFILE-gdb.gdb. */ |
| 381 | |
| 382 | void |
| 383 | auto_load_ext_lang_scripts_for_objfile (struct objfile *objfile) |
| 384 | { |
| 385 | int i; |
| 386 | const struct extension_language_defn *extlang; |
| 387 | |
| 388 | extlang = &extension_language_gdb; |
| 389 | if (ext_lang_auto_load_enabled (extlang)) |
| 390 | auto_load_objfile_script (objfile, extlang); |
| 391 | |
| 392 | ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang) |
| 393 | { |
| 394 | if (ext_lang_auto_load_enabled (extlang)) |
| 395 | auto_load_objfile_script (objfile, extlang); |
| 396 | } |
| 397 | } |
| 398 | \f |
| 399 | /* Interface to type pretty-printers implemented in an extension language. */ |
| 400 | |
| 401 | /* Call this at the start when preparing to pretty-print a type. |
| 402 | The result is a pointer to an opaque object (to the caller) to be passed |
| 403 | to apply_ext_lang_type_printers and free_ext_lang_type_printers. |
| 404 | |
| 405 | We don't know in advance which extension language will provide a |
| 406 | pretty-printer for the type, so all are initialized. */ |
| 407 | |
| 408 | struct ext_lang_type_printers * |
| 409 | start_ext_lang_type_printers (void) |
| 410 | { |
| 411 | struct ext_lang_type_printers *printers |
| 412 | = XCNEW (struct ext_lang_type_printers); |
| 413 | int i; |
| 414 | const struct extension_language_defn *extlang; |
| 415 | |
| 416 | ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang) |
| 417 | { |
| 418 | if (extlang->ops->start_type_printers != NULL) |
| 419 | extlang->ops->start_type_printers (extlang, printers); |
| 420 | } |
| 421 | |
| 422 | return printers; |
| 423 | } |
| 424 | |
| 425 | /* Iteratively try the type pretty-printers specified by PRINTERS |
| 426 | according to the standard search order (specified by extension_languages), |
| 427 | returning the result of the first one that succeeds. |
| 428 | If there was an error, or if no printer succeeds, then NULL is returned. */ |
| 429 | |
| 430 | char * |
| 431 | apply_ext_lang_type_printers (struct ext_lang_type_printers *printers, |
| 432 | struct type *type) |
| 433 | { |
| 434 | int i; |
| 435 | const struct extension_language_defn *extlang; |
| 436 | |
| 437 | ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang) |
| 438 | { |
| 439 | char *result = NULL; |
| 440 | enum ext_lang_rc rc; |
| 441 | |
| 442 | if (extlang->ops->apply_type_printers == NULL) |
| 443 | continue; |
| 444 | rc = extlang->ops->apply_type_printers (extlang, printers, type, |
| 445 | &result); |
| 446 | switch (rc) |
| 447 | { |
| 448 | case EXT_LANG_RC_OK: |
| 449 | gdb_assert (result != NULL); |
| 450 | return result; |
| 451 | case EXT_LANG_RC_ERROR: |
| 452 | return NULL; |
| 453 | case EXT_LANG_RC_NOP: |
| 454 | break; |
| 455 | default: |
| 456 | gdb_assert_not_reached ("bad return from apply_type_printers"); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | return NULL; |
| 461 | } |
| 462 | |
| 463 | /* Call this after pretty-printing a type to release all memory held |
| 464 | by PRINTERS. */ |
| 465 | |
| 466 | void |
| 467 | free_ext_lang_type_printers (struct ext_lang_type_printers *printers) |
| 468 | { |
| 469 | int i; |
| 470 | const struct extension_language_defn *extlang; |
| 471 | |
| 472 | ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang) |
| 473 | { |
| 474 | if (extlang->ops->free_type_printers != NULL) |
| 475 | extlang->ops->free_type_printers (extlang, printers); |
| 476 | } |
| 477 | |
| 478 | xfree (printers); |
| 479 | } |
| 480 | \f |
| 481 | /* Try to pretty-print a value of type TYPE located at VALADDR |
| 482 | + EMBEDDED_OFFSET, which came from the inferior at address ADDRESS |
| 483 | + EMBEDDED_OFFSET, onto stdio stream STREAM according to OPTIONS. |
| 484 | VAL is the whole object that came from ADDRESS. VALADDR must point to |
| 485 | the head of VAL's contents buffer. |
| 486 | Returns non-zero if the value was successfully pretty-printed. |
| 487 | |
| 488 | Extension languages are tried in the order specified by |
| 489 | extension_languages. The first one to provide a pretty-printed |
| 490 | value "wins". |
| 491 | |
| 492 | If an error is encountered in a pretty-printer, no further extension |
| 493 | languages are tried. |
| 494 | Note: This is different than encountering a memory error trying to read a |
| 495 | value for pretty-printing. Here we're referring to, e.g., programming |
| 496 | errors that trigger an exception in the extension language. */ |
| 497 | |
| 498 | int |
| 499 | apply_ext_lang_val_pretty_printer (struct type *type, const gdb_byte *valaddr, |
| 500 | int embedded_offset, CORE_ADDR address, |
| 501 | struct ui_file *stream, int recurse, |
| 502 | const struct value *val, |
| 503 | const struct value_print_options *options, |
| 504 | const struct language_defn *language) |
| 505 | { |
| 506 | int i; |
| 507 | const struct extension_language_defn *extlang; |
| 508 | |
| 509 | ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang) |
| 510 | { |
| 511 | enum ext_lang_rc rc; |
| 512 | |
| 513 | if (extlang->ops->apply_val_pretty_printer == NULL) |
| 514 | continue; |
| 515 | rc = extlang->ops->apply_val_pretty_printer (extlang, type, valaddr, |
| 516 | embedded_offset, address, |
| 517 | stream, recurse, val, |
| 518 | options, language); |
| 519 | switch (rc) |
| 520 | { |
| 521 | case EXT_LANG_RC_OK: |
| 522 | return 1; |
| 523 | case EXT_LANG_RC_ERROR: |
| 524 | return 0; |
| 525 | case EXT_LANG_RC_NOP: |
| 526 | break; |
| 527 | default: |
| 528 | gdb_assert_not_reached ("bad return from apply_val_pretty_printer"); |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | return 0; |
| 533 | } |
| 534 | |
| 535 | /* GDB access to the "frame filter" feature. |
| 536 | FRAME is the source frame to start frame-filter invocation. FLAGS is an |
| 537 | integer holding the flags for printing. The following elements of |
| 538 | the FRAME_FILTER_FLAGS enum denotes the make-up of FLAGS: |
| 539 | PRINT_LEVEL is a flag indicating whether to print the frame's |
| 540 | relative level in the output. PRINT_FRAME_INFO is a flag that |
| 541 | indicates whether this function should print the frame |
| 542 | information, PRINT_ARGS is a flag that indicates whether to print |
| 543 | frame arguments, and PRINT_LOCALS, likewise, with frame local |
| 544 | variables. ARGS_TYPE is an enumerator describing the argument |
| 545 | format, OUT is the output stream to print. FRAME_LOW is the |
| 546 | beginning of the slice of frames to print, and FRAME_HIGH is the |
| 547 | upper limit of the frames to count. Returns EXT_LANG_BT_ERROR on error, |
| 548 | or EXT_LANG_BT_COMPLETED on success. |
| 549 | |
| 550 | Extension languages are tried in the order specified by |
| 551 | extension_languages. The first one to provide a filter "wins". |
| 552 | If there is an error (EXT_LANG_BT_ERROR) it is reported immediately |
| 553 | rather than trying filters in other extension languages. */ |
| 554 | |
| 555 | enum ext_lang_bt_status |
| 556 | apply_ext_lang_frame_filter (struct frame_info *frame, int flags, |
| 557 | enum ext_lang_frame_args args_type, |
| 558 | struct ui_out *out, |
| 559 | int frame_low, int frame_high) |
| 560 | { |
| 561 | int i; |
| 562 | const struct extension_language_defn *extlang; |
| 563 | |
| 564 | ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang) |
| 565 | { |
| 566 | enum ext_lang_bt_status status; |
| 567 | |
| 568 | if (extlang->ops->apply_frame_filter == NULL) |
| 569 | continue; |
| 570 | status = extlang->ops->apply_frame_filter (extlang, frame, flags, |
| 571 | args_type, out, |
| 572 | frame_low, frame_high); |
| 573 | /* We use the filters from the first extension language that has |
| 574 | applicable filters. Also, an error is reported immediately |
| 575 | rather than continue trying. */ |
| 576 | if (status != EXT_LANG_BT_NO_FILTERS) |
| 577 | return status; |
| 578 | } |
| 579 | |
| 580 | return EXT_LANG_BT_NO_FILTERS; |
| 581 | } |
| 582 | |
| 583 | /* Update values held by the extension language when OBJFILE is discarded. |
| 584 | New global types must be created for every such value, which must then be |
| 585 | updated to use the new types. |
| 586 | The function typically just iterates over all appropriate values and |
| 587 | calls preserve_one_value for each one. |
| 588 | COPIED_TYPES is used to prevent cycles / duplicates and is passed to |
| 589 | preserve_one_value. */ |
| 590 | |
| 591 | void |
| 592 | preserve_ext_lang_values (struct objfile *objfile, htab_t copied_types) |
| 593 | { |
| 594 | int i; |
| 595 | const struct extension_language_defn *extlang; |
| 596 | |
| 597 | ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang) |
| 598 | { |
| 599 | if (extlang->ops->preserve_values != NULL) |
| 600 | extlang->ops->preserve_values (extlang, objfile, copied_types); |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | /* If there is a stop condition implemented in an extension language for |
| 605 | breakpoint B, return a pointer to the extension language's definition. |
| 606 | Otherwise return NULL. |
| 607 | If SKIP_LANG is not EXT_LANG_NONE, skip checking this language. |
| 608 | This is for the case where we're setting a new condition: Only one |
| 609 | condition is allowed, so when setting a condition for any particular |
| 610 | extension language, we need to check if any other extension language |
| 611 | already has a condition set. */ |
| 612 | |
| 613 | const struct extension_language_defn * |
| 614 | get_breakpoint_cond_ext_lang (struct breakpoint *b, |
| 615 | enum extension_language skip_lang) |
| 616 | { |
| 617 | int i; |
| 618 | const struct extension_language_defn *extlang; |
| 619 | |
| 620 | ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang) |
| 621 | { |
| 622 | if (extlang->language != skip_lang |
| 623 | && extlang->ops->breakpoint_has_cond != NULL |
| 624 | && extlang->ops->breakpoint_has_cond (extlang, b)) |
| 625 | return extlang; |
| 626 | } |
| 627 | |
| 628 | return NULL; |
| 629 | } |
| 630 | |
| 631 | /* Return whether a stop condition for breakpoint B says to stop. |
| 632 | True is also returned if there is no stop condition for B. */ |
| 633 | |
| 634 | int |
| 635 | breakpoint_ext_lang_cond_says_stop (struct breakpoint *b) |
| 636 | { |
| 637 | int i; |
| 638 | const struct extension_language_defn *extlang; |
| 639 | enum ext_lang_bp_stop stop = EXT_LANG_BP_STOP_UNSET; |
| 640 | |
| 641 | ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang) |
| 642 | { |
| 643 | /* There is a rule that a breakpoint can have at most one of any of a |
| 644 | CLI or extension language condition. However, Python hacks in "finish |
| 645 | breakpoints" on top of the "stop" check, so we have to call this for |
| 646 | every language, even if we could first determine whether a "stop" |
| 647 | method exists. */ |
| 648 | if (extlang->ops->breakpoint_cond_says_stop != NULL) |
| 649 | { |
| 650 | enum ext_lang_bp_stop this_stop |
| 651 | = extlang->ops->breakpoint_cond_says_stop (extlang, b); |
| 652 | |
| 653 | if (this_stop != EXT_LANG_BP_STOP_UNSET) |
| 654 | { |
| 655 | /* Even though we have to check every extension language, only |
| 656 | one of them can return yes/no (because only one of them |
| 657 | can have a "stop" condition). */ |
| 658 | gdb_assert (stop == EXT_LANG_BP_STOP_UNSET); |
| 659 | stop = this_stop; |
| 660 | } |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | return stop == EXT_LANG_BP_STOP_NO ? 0 : 1; |
| 665 | } |
| 666 | \f |
| 667 | /* ^C/SIGINT support. |
| 668 | This requires cooperation with the extension languages so the support |
| 669 | is defined here. */ |
| 670 | |
| 671 | /* This flag tracks quit requests when we haven't called out to an |
| 672 | extension language. it also holds quit requests when we transition to |
| 673 | an extension language that doesn't have cooperative SIGINT handling. */ |
| 674 | static int quit_flag; |
| 675 | |
| 676 | /* The current extension language we've called out to, or |
| 677 | extension_language_gdb if there isn't one. |
| 678 | This must be set everytime we call out to an extension language, and reset |
| 679 | to the previous value when it returns. Note that the previous value may |
| 680 | be a different (or the same) extension language. */ |
| 681 | static const struct extension_language_defn *active_ext_lang |
| 682 | = &extension_language_gdb; |
| 683 | |
| 684 | /* Return the currently active extension language. */ |
| 685 | |
| 686 | const struct extension_language_defn * |
| 687 | get_active_ext_lang (void) |
| 688 | { |
| 689 | return active_ext_lang; |
| 690 | } |
| 691 | |
| 692 | /* Install a SIGINT handler. */ |
| 693 | |
| 694 | static void |
| 695 | install_sigint_handler (const struct signal_handler *handler_state) |
| 696 | { |
| 697 | gdb_assert (handler_state->handler_saved); |
| 698 | |
| 699 | signal (SIGINT, handler_state->handler); |
| 700 | } |
| 701 | |
| 702 | /* Install GDB's SIGINT handler, storing the previous version in *PREVIOUS. |
| 703 | As a simple optimization, if the previous version was GDB's SIGINT handler |
| 704 | then mark the previous handler as not having been saved, and thus it won't |
| 705 | be restored. */ |
| 706 | |
| 707 | static void |
| 708 | install_gdb_sigint_handler (struct signal_handler *previous) |
| 709 | { |
| 710 | /* Save here to simplify comparison. */ |
| 711 | sighandler_t handle_sigint_for_compare = handle_sigint; |
| 712 | |
| 713 | previous->handler = signal (SIGINT, handle_sigint); |
| 714 | if (previous->handler != handle_sigint_for_compare) |
| 715 | previous->handler_saved = 1; |
| 716 | else |
| 717 | previous->handler_saved = 0; |
| 718 | } |
| 719 | |
| 720 | /* Set the currently active extension language to NOW_ACTIVE. |
| 721 | The result is a pointer to a malloc'd block of memory to pass to |
| 722 | restore_active_ext_lang. |
| 723 | |
| 724 | N.B. This function must be called every time we call out to an extension |
| 725 | language, and the result must be passed to restore_active_ext_lang |
| 726 | afterwards. |
| 727 | |
| 728 | If there is a pending SIGINT it is "moved" to the now active extension |
| 729 | language, if it supports cooperative SIGINT handling (i.e., it provides |
| 730 | {clear,set,check}_quit_flag methods). If the extension language does not |
| 731 | support cooperative SIGINT handling, then the SIGINT is left queued and |
| 732 | we require the non-cooperative extension language to call check_quit_flag |
| 733 | at appropriate times. |
| 734 | It is important for the extension language to call check_quit_flag if it |
| 735 | installs its own SIGINT handler to prevent the situation where a SIGINT |
| 736 | is queued on entry, extension language code runs for a "long" time possibly |
| 737 | serving one or more SIGINTs, and then returns. Upon return, if |
| 738 | check_quit_flag is not called, the original SIGINT will be thrown. |
| 739 | Non-cooperative extension languages are free to install their own SIGINT |
| 740 | handler but the original must be restored upon return, either itself |
| 741 | or via restore_active_ext_lang. */ |
| 742 | |
| 743 | struct active_ext_lang_state * |
| 744 | set_active_ext_lang (const struct extension_language_defn *now_active) |
| 745 | { |
| 746 | struct active_ext_lang_state *previous |
| 747 | = XCNEW (struct active_ext_lang_state); |
| 748 | |
| 749 | previous->ext_lang = active_ext_lang; |
| 750 | previous->sigint_handler.handler_saved = 0; |
| 751 | active_ext_lang = now_active; |
| 752 | |
| 753 | if (target_terminal_is_ours ()) |
| 754 | { |
| 755 | /* If the newly active extension language uses cooperative SIGINT |
| 756 | handling then ensure GDB's SIGINT handler is installed. */ |
| 757 | if (now_active->language == EXT_LANG_GDB |
| 758 | || now_active->ops->check_quit_flag != NULL) |
| 759 | install_gdb_sigint_handler (&previous->sigint_handler); |
| 760 | |
| 761 | /* If there's a SIGINT recorded in the cooperative extension languages, |
| 762 | move it to the new language, or save it in GDB's global flag if the |
| 763 | newly active extension language doesn't use cooperative SIGINT |
| 764 | handling. */ |
| 765 | if (check_quit_flag ()) |
| 766 | set_quit_flag (); |
| 767 | } |
| 768 | |
| 769 | return previous; |
| 770 | } |
| 771 | |
| 772 | /* Restore active extension language from PREVIOUS. */ |
| 773 | |
| 774 | void |
| 775 | restore_active_ext_lang (struct active_ext_lang_state *previous) |
| 776 | { |
| 777 | const struct extension_language_defn *current = active_ext_lang; |
| 778 | |
| 779 | active_ext_lang = previous->ext_lang; |
| 780 | |
| 781 | if (target_terminal_is_ours ()) |
| 782 | { |
| 783 | /* Restore the previous SIGINT handler if one was saved. */ |
| 784 | if (previous->sigint_handler.handler_saved) |
| 785 | install_sigint_handler (&previous->sigint_handler); |
| 786 | |
| 787 | /* If there's a SIGINT recorded in the cooperative extension languages, |
| 788 | move it to the new language, or save it in GDB's global flag if the |
| 789 | newly active extension language doesn't use cooperative SIGINT |
| 790 | handling. */ |
| 791 | if (check_quit_flag ()) |
| 792 | set_quit_flag (); |
| 793 | } |
| 794 | xfree (previous); |
| 795 | } |
| 796 | |
| 797 | /* Set the quit flag. |
| 798 | This only sets the flag in the currently active extension language. |
| 799 | If the currently active extension language does not have cooperative |
| 800 | SIGINT handling, then GDB's global flag is set, and it is up to the |
| 801 | extension language to call check_quit_flag. The extension language |
| 802 | is free to install its own SIGINT handler, but we still need to handle |
| 803 | the transition. */ |
| 804 | |
| 805 | void |
| 806 | set_quit_flag (void) |
| 807 | { |
| 808 | if (active_ext_lang->ops != NULL |
| 809 | && active_ext_lang->ops->set_quit_flag != NULL) |
| 810 | active_ext_lang->ops->set_quit_flag (active_ext_lang); |
| 811 | else |
| 812 | { |
| 813 | quit_flag = 1; |
| 814 | |
| 815 | /* Now wake up the event loop, or any interruptible_select. Do |
| 816 | this after setting the flag, because signals on Windows |
| 817 | actually run on a separate thread, and thus otherwise the |
| 818 | main code could be woken up and find quit_flag still |
| 819 | clear. */ |
| 820 | quit_serial_event_set (); |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | /* Return true if the quit flag has been set, false otherwise. |
| 825 | Note: The flag is cleared as a side-effect. |
| 826 | The flag is checked in all extension languages that support cooperative |
| 827 | SIGINT handling, not just the current one. This simplifies transitions. */ |
| 828 | |
| 829 | int |
| 830 | check_quit_flag (void) |
| 831 | { |
| 832 | int i, result = 0; |
| 833 | const struct extension_language_defn *extlang; |
| 834 | |
| 835 | ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang) |
| 836 | { |
| 837 | if (extlang->ops->check_quit_flag != NULL) |
| 838 | if (extlang->ops->check_quit_flag (extlang) != 0) |
| 839 | result = 1; |
| 840 | } |
| 841 | |
| 842 | /* This is written in a particular way to avoid races. */ |
| 843 | if (quit_flag) |
| 844 | { |
| 845 | /* No longer need to wake up the event loop or any |
| 846 | interruptible_select. The caller handles the quit |
| 847 | request. */ |
| 848 | quit_serial_event_clear (); |
| 849 | quit_flag = 0; |
| 850 | result = 1; |
| 851 | } |
| 852 | |
| 853 | return result; |
| 854 | } |
| 855 | \f |
| 856 | /* xmethod support. */ |
| 857 | |
| 858 | /* The xmethod API routines do not have "ext_lang" in the name because |
| 859 | the name "xmethod" implies that this routine deals with extension |
| 860 | languages. Plus some of the methods take a xmethod_foo * "self/this" |
| 861 | arg, not an extension_language_defn * arg. */ |
| 862 | |
| 863 | /* Returns a new xmethod_worker with EXTLANG and DATA. Space for the |
| 864 | result must be freed with free_xmethod_worker. */ |
| 865 | |
| 866 | struct xmethod_worker * |
| 867 | new_xmethod_worker (const struct extension_language_defn *extlang, void *data) |
| 868 | { |
| 869 | struct xmethod_worker *worker = XCNEW (struct xmethod_worker); |
| 870 | |
| 871 | worker->extlang = extlang; |
| 872 | worker->data = data; |
| 873 | worker->value = NULL; |
| 874 | |
| 875 | return worker; |
| 876 | } |
| 877 | |
| 878 | /* Clones WORKER and returns a new but identical worker. |
| 879 | The function get_matching_xmethod_workers (see below), returns a |
| 880 | vector of matching workers. If a particular worker is selected by GDB |
| 881 | to invoke a method, then this function can help in cloning the |
| 882 | selected worker and freeing up the vector via a cleanup. |
| 883 | |
| 884 | Space for the result must be freed with free_xmethod_worker. */ |
| 885 | |
| 886 | struct xmethod_worker * |
| 887 | clone_xmethod_worker (struct xmethod_worker *worker) |
| 888 | { |
| 889 | struct xmethod_worker *new_worker; |
| 890 | const struct extension_language_defn *extlang = worker->extlang; |
| 891 | |
| 892 | gdb_assert (extlang->ops->clone_xmethod_worker_data != NULL); |
| 893 | |
| 894 | new_worker = new_xmethod_worker |
| 895 | (extlang, |
| 896 | extlang->ops->clone_xmethod_worker_data (extlang, worker->data)); |
| 897 | |
| 898 | return new_worker; |
| 899 | } |
| 900 | |
| 901 | /* If a method with name METHOD_NAME is to be invoked on an object of type |
| 902 | TYPE, then all entension languages are searched for implementations of |
| 903 | methods with name METHOD. All matches found are returned as a vector |
| 904 | of 'xmethod_worker_ptr' objects. If no matching methods are |
| 905 | found, NULL is returned. */ |
| 906 | |
| 907 | VEC (xmethod_worker_ptr) * |
| 908 | get_matching_xmethod_workers (struct type *type, const char *method_name) |
| 909 | { |
| 910 | VEC (xmethod_worker_ptr) *workers = NULL; |
| 911 | int i; |
| 912 | const struct extension_language_defn *extlang; |
| 913 | |
| 914 | ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang) |
| 915 | { |
| 916 | VEC (xmethod_worker_ptr) *lang_workers, *new_vec; |
| 917 | enum ext_lang_rc rc; |
| 918 | |
| 919 | /* If an extension language does not support xmethods, ignore |
| 920 | it. */ |
| 921 | if (extlang->ops->get_matching_xmethod_workers == NULL) |
| 922 | continue; |
| 923 | |
| 924 | rc = extlang->ops->get_matching_xmethod_workers (extlang, |
| 925 | type, method_name, |
| 926 | &lang_workers); |
| 927 | if (rc == EXT_LANG_RC_ERROR) |
| 928 | { |
| 929 | free_xmethod_worker_vec (workers); |
| 930 | error (_("Error while looking for matching xmethod workers " |
| 931 | "defined in %s."), extlang->capitalized_name); |
| 932 | } |
| 933 | |
| 934 | new_vec = VEC_merge (xmethod_worker_ptr, workers, lang_workers); |
| 935 | /* Free only the vectors and not the elements as NEW_VEC still |
| 936 | contains them. */ |
| 937 | VEC_free (xmethod_worker_ptr, workers); |
| 938 | VEC_free (xmethod_worker_ptr, lang_workers); |
| 939 | workers = new_vec; |
| 940 | } |
| 941 | |
| 942 | return workers; |
| 943 | } |
| 944 | |
| 945 | /* Return the arg types of the xmethod encapsulated in WORKER. |
| 946 | An array of arg types is returned. The length of the array is returned in |
| 947 | NARGS. The type of the 'this' object is returned as the first element of |
| 948 | array. */ |
| 949 | |
| 950 | struct type ** |
| 951 | get_xmethod_arg_types (struct xmethod_worker *worker, int *nargs) |
| 952 | { |
| 953 | enum ext_lang_rc rc; |
| 954 | struct type **type_array = NULL; |
| 955 | const struct extension_language_defn *extlang = worker->extlang; |
| 956 | |
| 957 | gdb_assert (extlang->ops->get_xmethod_arg_types != NULL); |
| 958 | |
| 959 | rc = extlang->ops->get_xmethod_arg_types (extlang, worker, nargs, |
| 960 | &type_array); |
| 961 | if (rc == EXT_LANG_RC_ERROR) |
| 962 | { |
| 963 | error (_("Error while looking for arg types of a xmethod worker " |
| 964 | "defined in %s."), extlang->capitalized_name); |
| 965 | } |
| 966 | |
| 967 | return type_array; |
| 968 | } |
| 969 | |
| 970 | /* Return the type of the result of the xmethod encapsulated in WORKER. |
| 971 | OBJECT, ARGS, NARGS are the same as for invoke_xmethod. */ |
| 972 | |
| 973 | struct type * |
| 974 | get_xmethod_result_type (struct xmethod_worker *worker, |
| 975 | struct value *object, struct value **args, int nargs) |
| 976 | { |
| 977 | enum ext_lang_rc rc; |
| 978 | struct type *result_type; |
| 979 | const struct extension_language_defn *extlang = worker->extlang; |
| 980 | |
| 981 | gdb_assert (extlang->ops->get_xmethod_arg_types != NULL); |
| 982 | |
| 983 | rc = extlang->ops->get_xmethod_result_type (extlang, worker, |
| 984 | object, args, nargs, |
| 985 | &result_type); |
| 986 | if (rc == EXT_LANG_RC_ERROR) |
| 987 | { |
| 988 | error (_("Error while fetching result type of an xmethod worker " |
| 989 | "defined in %s."), extlang->capitalized_name); |
| 990 | } |
| 991 | |
| 992 | return result_type; |
| 993 | } |
| 994 | |
| 995 | /* Invokes the xmethod encapsulated in WORKER and returns the result. |
| 996 | The method is invoked on OBJ with arguments in the ARGS array. NARGS is |
| 997 | the length of the this array. */ |
| 998 | |
| 999 | struct value * |
| 1000 | invoke_xmethod (struct xmethod_worker *worker, struct value *obj, |
| 1001 | struct value **args, int nargs) |
| 1002 | { |
| 1003 | gdb_assert (worker->extlang->ops->invoke_xmethod != NULL); |
| 1004 | |
| 1005 | return worker->extlang->ops->invoke_xmethod (worker->extlang, worker, |
| 1006 | obj, args, nargs); |
| 1007 | } |
| 1008 | |
| 1009 | /* Frees the xmethod worker WORKER. */ |
| 1010 | |
| 1011 | void |
| 1012 | free_xmethod_worker (struct xmethod_worker *worker) |
| 1013 | { |
| 1014 | gdb_assert (worker->extlang->ops->free_xmethod_worker_data != NULL); |
| 1015 | worker->extlang->ops->free_xmethod_worker_data (worker->extlang, |
| 1016 | worker->data); |
| 1017 | xfree (worker); |
| 1018 | } |
| 1019 | |
| 1020 | /* Frees a vector of xmethod_workers VEC. */ |
| 1021 | |
| 1022 | void |
| 1023 | free_xmethod_worker_vec (void *vec) |
| 1024 | { |
| 1025 | int i; |
| 1026 | struct xmethod_worker *worker; |
| 1027 | VEC (xmethod_worker_ptr) *v = (VEC (xmethod_worker_ptr) *) vec; |
| 1028 | |
| 1029 | for (i = 0; VEC_iterate (xmethod_worker_ptr, v, i, worker); i++) |
| 1030 | free_xmethod_worker (worker); |
| 1031 | |
| 1032 | VEC_free (xmethod_worker_ptr, v); |
| 1033 | } |
| 1034 | \f |
| 1035 | /* Called via an observer before gdb prints its prompt. |
| 1036 | Iterate over the extension languages giving them a chance to |
| 1037 | change the prompt. The first one to change the prompt wins, |
| 1038 | and no further languages are tried. */ |
| 1039 | |
| 1040 | static void |
| 1041 | ext_lang_before_prompt (const char *current_gdb_prompt) |
| 1042 | { |
| 1043 | int i; |
| 1044 | const struct extension_language_defn *extlang; |
| 1045 | |
| 1046 | ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang) |
| 1047 | { |
| 1048 | enum ext_lang_rc rc; |
| 1049 | |
| 1050 | if (extlang->ops->before_prompt == NULL) |
| 1051 | continue; |
| 1052 | rc = extlang->ops->before_prompt (extlang, current_gdb_prompt); |
| 1053 | switch (rc) |
| 1054 | { |
| 1055 | case EXT_LANG_RC_OK: |
| 1056 | case EXT_LANG_RC_ERROR: |
| 1057 | return; |
| 1058 | case EXT_LANG_RC_NOP: |
| 1059 | break; |
| 1060 | default: |
| 1061 | gdb_assert_not_reached ("bad return from before_prompt"); |
| 1062 | } |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | extern initialize_file_ftype _initialize_extension; |
| 1067 | |
| 1068 | void |
| 1069 | _initialize_extension (void) |
| 1070 | { |
| 1071 | observer_attach_before_prompt (ext_lang_before_prompt); |
| 1072 | } |