extension.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  1. /* Interface between gdb and its extension languages.
  2. Copyright (C) 2014-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. /* Note: With few exceptions, external functions and variables in this file
  15. have "ext_lang" in the name, and no other symbol in gdb does. */
  16. #include "defs.h"
  17. #include <signal.h>
  18. #include "target.h"
  19. #include "auto-load.h"
  20. #include "breakpoint.h"
  21. #include "event-top.h"
  22. #include "extension.h"
  23. #include "extension-priv.h"
  24. #include "observable.h"
  25. #include "cli/cli-script.h"
  26. #include "python/python.h"
  27. #include "guile/guile.h"
  28. #include <array>
  29. static script_sourcer_func source_gdb_script;
  30. static objfile_script_sourcer_func source_gdb_objfile_script;
  31. /* GDB's own scripting language.
  32. This exists, in part, to support auto-loading ${prog}-gdb.gdb scripts. */
  33. static const struct extension_language_script_ops
  34. extension_language_gdb_script_ops =
  35. {
  36. source_gdb_script,
  37. source_gdb_objfile_script,
  38. NULL, /* objfile_script_executor */
  39. auto_load_gdb_scripts_enabled
  40. };
  41. const struct extension_language_defn extension_language_gdb =
  42. {
  43. EXT_LANG_GDB,
  44. "gdb",
  45. "GDB",
  46. /* We fall back to interpreting a script as a GDB script if it doesn't
  47. match the other scripting languages, but for consistency's sake
  48. give it a formal suffix. */
  49. ".gdb",
  50. "-gdb.gdb",
  51. /* cli_control_type: This is never used: GDB's own scripting language
  52. has a variety of control types (if, while, etc.). */
  53. commands_control,
  54. &extension_language_gdb_script_ops,
  55. /* The rest of the extension language interface isn't supported by GDB's own
  56. extension/scripting language. */
  57. NULL
  58. };
  59. /* NULL-terminated table of all external (non-native) extension languages.
  60. The order of appearance in the table is important.
  61. When multiple extension languages provide the same feature, for example
  62. a pretty-printer for a particular type, which one gets used?
  63. The algorithm employed here is "the first one wins". For example, in
  64. the case of pretty-printers this means the first one to provide a
  65. pretty-printed value is the one that is used. This algorithm is employed
  66. throughout. */
  67. static const std::array<const extension_language_defn *, 2> extension_languages
  68. {
  69. /* To preserve existing behaviour, python should always appear first. */
  70. &extension_language_python,
  71. &extension_language_guile,
  72. };
  73. /* Return a pointer to the struct extension_language_defn object of
  74. extension language LANG.
  75. This always returns a non-NULL pointer, even if support for the language
  76. is not compiled into this copy of GDB. */
  77. const struct extension_language_defn *
  78. get_ext_lang_defn (enum extension_language lang)
  79. {
  80. gdb_assert (lang != EXT_LANG_NONE);
  81. if (lang == EXT_LANG_GDB)
  82. return &extension_language_gdb;
  83. for (const struct extension_language_defn *extlang : extension_languages)
  84. {
  85. if (extlang->language == lang)
  86. return extlang;
  87. }
  88. gdb_assert_not_reached ("unable to find extension_language_defn");
  89. }
  90. /* Return TRUE if FILE has extension EXTENSION. */
  91. static int
  92. has_extension (const char *file, const char *extension)
  93. {
  94. int file_len = strlen (file);
  95. int extension_len = strlen (extension);
  96. return (file_len > extension_len
  97. && strcmp (&file[file_len - extension_len], extension) == 0);
  98. }
  99. /* Return the extension language of FILE, or NULL if
  100. the extension language of FILE is not recognized.
  101. This is done by looking at the file's suffix. */
  102. const struct extension_language_defn *
  103. get_ext_lang_of_file (const char *file)
  104. {
  105. if (has_extension (file, extension_language_gdb.suffix))
  106. return &extension_language_gdb;
  107. for (const struct extension_language_defn *extlang : extension_languages)
  108. {
  109. if (has_extension (file, extlang->suffix))
  110. return extlang;
  111. }
  112. return NULL;
  113. }
  114. /* Return non-zero if support for the specified extension language
  115. is compiled in. */
  116. int
  117. ext_lang_present_p (const struct extension_language_defn *extlang)
  118. {
  119. return extlang->script_ops != NULL;
  120. }
  121. /* Return non-zero if the specified extension language has successfully
  122. initialized. */
  123. int
  124. ext_lang_initialized_p (const struct extension_language_defn *extlang)
  125. {
  126. if (extlang->ops != NULL)
  127. {
  128. /* This method is required. */
  129. gdb_assert (extlang->ops->initialized != NULL);
  130. return extlang->ops->initialized (extlang);
  131. }
  132. return 0;
  133. }
  134. /* Throw an error indicating EXTLANG is not supported in this copy of GDB. */
  135. void
  136. throw_ext_lang_unsupported (const struct extension_language_defn *extlang)
  137. {
  138. error (_("Scripting in the \"%s\" language is not supported"
  139. " in this copy of GDB."),
  140. ext_lang_capitalized_name (extlang));
  141. }
  142. /* Methods for GDB's own extension/scripting language. */
  143. /* The extension_language_script_ops.script_sourcer "method". */
  144. static void
  145. source_gdb_script (const struct extension_language_defn *extlang,
  146. FILE *stream, const char *file)
  147. {
  148. script_from_file (stream, file);
  149. }
  150. /* The extension_language_script_ops.objfile_script_sourcer "method". */
  151. static void
  152. source_gdb_objfile_script (const struct extension_language_defn *extlang,
  153. struct objfile *objfile,
  154. FILE *stream, const char *file)
  155. {
  156. script_from_file (stream, file);
  157. }
  158. /* Accessors for "public" attributes of struct extension_language. */
  159. /* Return the "name" field of EXTLANG. */
  160. const char *
  161. ext_lang_name (const struct extension_language_defn *extlang)
  162. {
  163. return extlang->name;
  164. }
  165. /* Return the "capitalized_name" field of EXTLANG. */
  166. const char *
  167. ext_lang_capitalized_name (const struct extension_language_defn *extlang)
  168. {
  169. return extlang->capitalized_name;
  170. }
  171. /* Return the "suffix" field of EXTLANG. */
  172. const char *
  173. ext_lang_suffix (const struct extension_language_defn *extlang)
  174. {
  175. return extlang->suffix;
  176. }
  177. /* Return the "auto_load_suffix" field of EXTLANG. */
  178. const char *
  179. ext_lang_auto_load_suffix (const struct extension_language_defn *extlang)
  180. {
  181. return extlang->auto_load_suffix;
  182. }
  183. /* extension_language_script_ops wrappers. */
  184. /* Return the script "sourcer" function for EXTLANG.
  185. This is the function that loads and processes a script.
  186. If support for this language isn't compiled in, NULL is returned. */
  187. script_sourcer_func *
  188. ext_lang_script_sourcer (const struct extension_language_defn *extlang)
  189. {
  190. if (extlang->script_ops == NULL)
  191. return NULL;
  192. /* The extension language is required to implement this function. */
  193. gdb_assert (extlang->script_ops->script_sourcer != NULL);
  194. return extlang->script_ops->script_sourcer;
  195. }
  196. /* Return the objfile script "sourcer" function for EXTLANG.
  197. This is the function that loads and processes a script for a particular
  198. objfile.
  199. If support for this language isn't compiled in, NULL is returned. */
  200. objfile_script_sourcer_func *
  201. ext_lang_objfile_script_sourcer (const struct extension_language_defn *extlang)
  202. {
  203. if (extlang->script_ops == NULL)
  204. return NULL;
  205. /* The extension language is required to implement this function. */
  206. gdb_assert (extlang->script_ops->objfile_script_sourcer != NULL);
  207. return extlang->script_ops->objfile_script_sourcer;
  208. }
  209. /* Return the objfile script "executor" function for EXTLANG.
  210. This is the function that executes a script for a particular objfile.
  211. If support for this language isn't compiled in, NULL is returned.
  212. The extension language is not required to implement this function. */
  213. objfile_script_executor_func *
  214. ext_lang_objfile_script_executor
  215. (const struct extension_language_defn *extlang)
  216. {
  217. if (extlang->script_ops == NULL)
  218. return NULL;
  219. return extlang->script_ops->objfile_script_executor;
  220. }
  221. /* See extension.h. */
  222. bool
  223. ext_lang_auto_load_enabled (const struct extension_language_defn *extlang)
  224. {
  225. if (extlang->script_ops == NULL)
  226. return false;
  227. /* The extension language is required to implement this function. */
  228. gdb_assert (extlang->script_ops->auto_load_enabled != NULL);
  229. return extlang->script_ops->auto_load_enabled (extlang);
  230. }
  231. /* RAII class used to temporarily return SIG to its default handler. */
  232. template<int SIG>
  233. struct scoped_default_signal
  234. {
  235. scoped_default_signal ()
  236. { m_old_sig_handler = signal (SIG, SIG_DFL); }
  237. ~scoped_default_signal ()
  238. { signal (SIG, m_old_sig_handler); }
  239. DISABLE_COPY_AND_ASSIGN (scoped_default_signal);
  240. private:
  241. /* The previous signal handler that needs to be restored. */
  242. sighandler_t m_old_sig_handler;
  243. };
  244. /* Class to temporarily return SIGINT to its default handler. */
  245. using scoped_default_sigint = scoped_default_signal<SIGINT>;
  246. /* Functions that iterate over all extension languages.
  247. These only iterate over external extension languages, not including
  248. GDB's own extension/scripting language, unless otherwise indicated. */
  249. /* Wrapper to call the extension_language_ops.initialize "method" for each
  250. compiled-in extension language. */
  251. void
  252. ext_lang_initialization (void)
  253. {
  254. for (const struct extension_language_defn *extlang : extension_languages)
  255. {
  256. if (extlang->ops != nullptr
  257. && extlang->ops->initialize != NULL)
  258. {
  259. scoped_default_sigint set_sigint_to_default_handler;
  260. extlang->ops->initialize (extlang);
  261. }
  262. }
  263. }
  264. /* Invoke the appropriate extension_language_ops.eval_from_control_command
  265. method to perform CMD, which is a list of commands in an extension language.
  266. This function is what implements, for example:
  267. python
  268. print 42
  269. end
  270. in a GDB script. */
  271. void
  272. eval_ext_lang_from_control_command (struct command_line *cmd)
  273. {
  274. for (const struct extension_language_defn *extlang : extension_languages)
  275. {
  276. if (extlang->cli_control_type == cmd->control_type)
  277. {
  278. if (extlang->ops != NULL
  279. && extlang->ops->eval_from_control_command != NULL)
  280. {
  281. extlang->ops->eval_from_control_command (extlang, cmd);
  282. return;
  283. }
  284. /* The requested extension language is not supported in this GDB. */
  285. throw_ext_lang_unsupported (extlang);
  286. }
  287. }
  288. gdb_assert_not_reached ("unknown extension language in command_line");
  289. }
  290. /* Search for and load scripts for OBJFILE written in extension languages.
  291. This includes GDB's own scripting language.
  292. This function is what implements the loading of OBJFILE-gdb.py and
  293. OBJFILE-gdb.gdb. */
  294. void
  295. auto_load_ext_lang_scripts_for_objfile (struct objfile *objfile)
  296. {
  297. const struct extension_language_defn *gdb = &extension_language_gdb;
  298. if (ext_lang_auto_load_enabled (gdb))
  299. auto_load_objfile_script (objfile, gdb);
  300. for (const struct extension_language_defn *extlang : extension_languages)
  301. {
  302. if (extlang->ops != nullptr
  303. && ext_lang_auto_load_enabled (extlang))
  304. auto_load_objfile_script (objfile, extlang);
  305. }
  306. }
  307. /* Interface to type pretty-printers implemented in an extension language. */
  308. /* Call this at the start when preparing to pretty-print a type.
  309. The result is a pointer to an opaque object (to the caller) to be passed
  310. to apply_ext_lang_type_printers and free_ext_lang_type_printers.
  311. We don't know in advance which extension language will provide a
  312. pretty-printer for the type, so all are initialized. */
  313. ext_lang_type_printers::ext_lang_type_printers ()
  314. {
  315. for (const struct extension_language_defn *extlang : extension_languages)
  316. {
  317. if (extlang->ops != nullptr
  318. && extlang->ops->start_type_printers != NULL)
  319. extlang->ops->start_type_printers (extlang, this);
  320. }
  321. }
  322. /* Iteratively try the type pretty-printers specified by PRINTERS
  323. according to the standard search order (specified by extension_languages),
  324. returning the result of the first one that succeeds.
  325. If there was an error, or if no printer succeeds, then NULL is returned. */
  326. char *
  327. apply_ext_lang_type_printers (struct ext_lang_type_printers *printers,
  328. struct type *type)
  329. {
  330. for (const struct extension_language_defn *extlang : extension_languages)
  331. {
  332. char *result = NULL;
  333. enum ext_lang_rc rc;
  334. if (extlang->ops == nullptr
  335. || extlang->ops->apply_type_printers == NULL)
  336. continue;
  337. rc = extlang->ops->apply_type_printers (extlang, printers, type,
  338. &result);
  339. switch (rc)
  340. {
  341. case EXT_LANG_RC_OK:
  342. gdb_assert (result != NULL);
  343. return result;
  344. case EXT_LANG_RC_ERROR:
  345. return NULL;
  346. case EXT_LANG_RC_NOP:
  347. break;
  348. default:
  349. gdb_assert_not_reached ("bad return from apply_type_printers");
  350. }
  351. }
  352. return NULL;
  353. }
  354. ext_lang_type_printers::~ext_lang_type_printers ()
  355. {
  356. for (const struct extension_language_defn *extlang : extension_languages)
  357. {
  358. if (extlang->ops != nullptr
  359. && extlang->ops->free_type_printers != NULL)
  360. extlang->ops->free_type_printers (extlang, this);
  361. }
  362. }
  363. /* Try to pretty-print a value onto stdio stream STREAM according to
  364. OPTIONS. VAL is the object to print. Returns non-zero if the
  365. value was successfully pretty-printed.
  366. Extension languages are tried in the order specified by
  367. extension_languages. The first one to provide a pretty-printed
  368. value "wins".
  369. If an error is encountered in a pretty-printer, no further extension
  370. languages are tried.
  371. Note: This is different than encountering a memory error trying to read a
  372. value for pretty-printing. Here we're referring to, e.g., programming
  373. errors that trigger an exception in the extension language. */
  374. int
  375. apply_ext_lang_val_pretty_printer (struct value *val,
  376. struct ui_file *stream, int recurse,
  377. const struct value_print_options *options,
  378. const struct language_defn *language)
  379. {
  380. for (const struct extension_language_defn *extlang : extension_languages)
  381. {
  382. enum ext_lang_rc rc;
  383. if (extlang->ops == nullptr
  384. || extlang->ops->apply_val_pretty_printer == NULL)
  385. continue;
  386. rc = extlang->ops->apply_val_pretty_printer (extlang, val, stream,
  387. recurse, options, language);
  388. switch (rc)
  389. {
  390. case EXT_LANG_RC_OK:
  391. return 1;
  392. case EXT_LANG_RC_ERROR:
  393. return 0;
  394. case EXT_LANG_RC_NOP:
  395. break;
  396. default:
  397. gdb_assert_not_reached ("bad return from apply_val_pretty_printer");
  398. }
  399. }
  400. return 0;
  401. }
  402. /* GDB access to the "frame filter" feature.
  403. FRAME is the source frame to start frame-filter invocation. FLAGS is an
  404. integer holding the flags for printing. The following elements of
  405. the FRAME_FILTER_FLAGS enum denotes the make-up of FLAGS:
  406. PRINT_LEVEL is a flag indicating whether to print the frame's
  407. relative level in the output. PRINT_FRAME_INFO is a flag that
  408. indicates whether this function should print the frame
  409. information, PRINT_ARGS is a flag that indicates whether to print
  410. frame arguments, and PRINT_LOCALS, likewise, with frame local
  411. variables. ARGS_TYPE is an enumerator describing the argument
  412. format, OUT is the output stream to print. FRAME_LOW is the
  413. beginning of the slice of frames to print, and FRAME_HIGH is the
  414. upper limit of the frames to count. Returns EXT_LANG_BT_ERROR on error,
  415. or EXT_LANG_BT_COMPLETED on success.
  416. Extension languages are tried in the order specified by
  417. extension_languages. The first one to provide a filter "wins".
  418. If there is an error (EXT_LANG_BT_ERROR) it is reported immediately
  419. rather than trying filters in other extension languages. */
  420. enum ext_lang_bt_status
  421. apply_ext_lang_frame_filter (struct frame_info *frame,
  422. frame_filter_flags flags,
  423. enum ext_lang_frame_args args_type,
  424. struct ui_out *out,
  425. int frame_low, int frame_high)
  426. {
  427. for (const struct extension_language_defn *extlang : extension_languages)
  428. {
  429. enum ext_lang_bt_status status;
  430. if (extlang->ops == nullptr
  431. || extlang->ops->apply_frame_filter == NULL)
  432. continue;
  433. status = extlang->ops->apply_frame_filter (extlang, frame, flags,
  434. args_type, out,
  435. frame_low, frame_high);
  436. /* We use the filters from the first extension language that has
  437. applicable filters. Also, an error is reported immediately
  438. rather than continue trying. */
  439. if (status != EXT_LANG_BT_NO_FILTERS)
  440. return status;
  441. }
  442. return EXT_LANG_BT_NO_FILTERS;
  443. }
  444. /* Update values held by the extension language when OBJFILE is discarded.
  445. New global types must be created for every such value, which must then be
  446. updated to use the new types.
  447. The function typically just iterates over all appropriate values and
  448. calls preserve_one_value for each one.
  449. COPIED_TYPES is used to prevent cycles / duplicates and is passed to
  450. preserve_one_value. */
  451. void
  452. preserve_ext_lang_values (struct objfile *objfile, htab_t copied_types)
  453. {
  454. for (const struct extension_language_defn *extlang : extension_languages)
  455. {
  456. if (extlang->ops != nullptr
  457. && extlang->ops->preserve_values != NULL)
  458. extlang->ops->preserve_values (extlang, objfile, copied_types);
  459. }
  460. }
  461. /* If there is a stop condition implemented in an extension language for
  462. breakpoint B, return a pointer to the extension language's definition.
  463. Otherwise return NULL.
  464. If SKIP_LANG is not EXT_LANG_NONE, skip checking this language.
  465. This is for the case where we're setting a new condition: Only one
  466. condition is allowed, so when setting a condition for any particular
  467. extension language, we need to check if any other extension language
  468. already has a condition set. */
  469. const struct extension_language_defn *
  470. get_breakpoint_cond_ext_lang (struct breakpoint *b,
  471. enum extension_language skip_lang)
  472. {
  473. for (const struct extension_language_defn *extlang : extension_languages)
  474. {
  475. if (extlang->ops != nullptr
  476. && extlang->language != skip_lang
  477. && extlang->ops->breakpoint_has_cond != NULL
  478. && extlang->ops->breakpoint_has_cond (extlang, b))
  479. return extlang;
  480. }
  481. return NULL;
  482. }
  483. /* Return whether a stop condition for breakpoint B says to stop.
  484. True is also returned if there is no stop condition for B. */
  485. int
  486. breakpoint_ext_lang_cond_says_stop (struct breakpoint *b)
  487. {
  488. enum ext_lang_bp_stop stop = EXT_LANG_BP_STOP_UNSET;
  489. for (const struct extension_language_defn *extlang : extension_languages)
  490. {
  491. /* There is a rule that a breakpoint can have at most one of any of a
  492. CLI or extension language condition. However, Python hacks in "finish
  493. breakpoints" on top of the "stop" check, so we have to call this for
  494. every language, even if we could first determine whether a "stop"
  495. method exists. */
  496. if (extlang->ops != nullptr
  497. && extlang->ops->breakpoint_cond_says_stop != NULL)
  498. {
  499. enum ext_lang_bp_stop this_stop
  500. = extlang->ops->breakpoint_cond_says_stop (extlang, b);
  501. if (this_stop != EXT_LANG_BP_STOP_UNSET)
  502. {
  503. /* Even though we have to check every extension language, only
  504. one of them can return yes/no (because only one of them
  505. can have a "stop" condition). */
  506. gdb_assert (stop == EXT_LANG_BP_STOP_UNSET);
  507. stop = this_stop;
  508. }
  509. }
  510. }
  511. return stop == EXT_LANG_BP_STOP_NO ? 0 : 1;
  512. }
  513. /* ^C/SIGINT support.
  514. This requires cooperation with the extension languages so the support
  515. is defined here. */
  516. /* This flag tracks quit requests when we haven't called out to an
  517. extension language. it also holds quit requests when we transition to
  518. an extension language that doesn't have cooperative SIGINT handling. */
  519. static int quit_flag;
  520. /* The current extension language we've called out to, or
  521. extension_language_gdb if there isn't one.
  522. This must be set everytime we call out to an extension language, and reset
  523. to the previous value when it returns. Note that the previous value may
  524. be a different (or the same) extension language. */
  525. static const struct extension_language_defn *active_ext_lang
  526. = &extension_language_gdb;
  527. /* Return the currently active extension language. */
  528. const struct extension_language_defn *
  529. get_active_ext_lang (void)
  530. {
  531. return active_ext_lang;
  532. }
  533. /* Install a SIGINT handler. */
  534. static void
  535. install_sigint_handler (const struct signal_handler *handler_state)
  536. {
  537. gdb_assert (handler_state->handler_saved);
  538. signal (SIGINT, handler_state->handler);
  539. }
  540. /* Install GDB's SIGINT handler, storing the previous version in *PREVIOUS.
  541. As a simple optimization, if the previous version was GDB's SIGINT handler
  542. then mark the previous handler as not having been saved, and thus it won't
  543. be restored. */
  544. static void
  545. install_gdb_sigint_handler (struct signal_handler *previous)
  546. {
  547. /* Save here to simplify comparison. */
  548. sighandler_t handle_sigint_for_compare = handle_sigint;
  549. previous->handler = signal (SIGINT, handle_sigint);
  550. if (previous->handler != handle_sigint_for_compare)
  551. previous->handler_saved = 1;
  552. else
  553. previous->handler_saved = 0;
  554. }
  555. #if GDB_SELF_TEST
  556. namespace selftests {
  557. void (*hook_set_active_ext_lang) () = nullptr;
  558. }
  559. #endif
  560. /* Set the currently active extension language to NOW_ACTIVE.
  561. The result is a pointer to a malloc'd block of memory to pass to
  562. restore_active_ext_lang.
  563. N.B. This function must be called every time we call out to an extension
  564. language, and the result must be passed to restore_active_ext_lang
  565. afterwards.
  566. If there is a pending SIGINT it is "moved" to the now active extension
  567. language, if it supports cooperative SIGINT handling (i.e., it provides
  568. {clear,set,check}_quit_flag methods). If the extension language does not
  569. support cooperative SIGINT handling, then the SIGINT is left queued and
  570. we require the non-cooperative extension language to call check_quit_flag
  571. at appropriate times.
  572. It is important for the extension language to call check_quit_flag if it
  573. installs its own SIGINT handler to prevent the situation where a SIGINT
  574. is queued on entry, extension language code runs for a "long" time possibly
  575. serving one or more SIGINTs, and then returns. Upon return, if
  576. check_quit_flag is not called, the original SIGINT will be thrown.
  577. Non-cooperative extension languages are free to install their own SIGINT
  578. handler but the original must be restored upon return, either itself
  579. or via restore_active_ext_lang. */
  580. struct active_ext_lang_state *
  581. set_active_ext_lang (const struct extension_language_defn *now_active)
  582. {
  583. #if GDB_SELF_TEST
  584. if (selftests::hook_set_active_ext_lang)
  585. selftests::hook_set_active_ext_lang ();
  586. #endif
  587. struct active_ext_lang_state *previous
  588. = XCNEW (struct active_ext_lang_state);
  589. previous->ext_lang = active_ext_lang;
  590. previous->sigint_handler.handler_saved = 0;
  591. active_ext_lang = now_active;
  592. if (target_terminal::is_ours ())
  593. {
  594. /* If the newly active extension language uses cooperative SIGINT
  595. handling then ensure GDB's SIGINT handler is installed. */
  596. if (now_active->language == EXT_LANG_GDB
  597. || now_active->ops->check_quit_flag != NULL)
  598. install_gdb_sigint_handler (&previous->sigint_handler);
  599. /* If there's a SIGINT recorded in the cooperative extension languages,
  600. move it to the new language, or save it in GDB's global flag if the
  601. newly active extension language doesn't use cooperative SIGINT
  602. handling. */
  603. if (check_quit_flag ())
  604. set_quit_flag ();
  605. }
  606. return previous;
  607. }
  608. /* Restore active extension language from PREVIOUS. */
  609. void
  610. restore_active_ext_lang (struct active_ext_lang_state *previous)
  611. {
  612. active_ext_lang = previous->ext_lang;
  613. if (target_terminal::is_ours ())
  614. {
  615. /* Restore the previous SIGINT handler if one was saved. */
  616. if (previous->sigint_handler.handler_saved)
  617. install_sigint_handler (&previous->sigint_handler);
  618. /* If there's a SIGINT recorded in the cooperative extension languages,
  619. move it to the new language, or save it in GDB's global flag if the
  620. newly active extension language doesn't use cooperative SIGINT
  621. handling. */
  622. if (check_quit_flag ())
  623. set_quit_flag ();
  624. }
  625. xfree (previous);
  626. }
  627. /* Set the quit flag.
  628. This only sets the flag in the currently active extension language.
  629. If the currently active extension language does not have cooperative
  630. SIGINT handling, then GDB's global flag is set, and it is up to the
  631. extension language to call check_quit_flag. The extension language
  632. is free to install its own SIGINT handler, but we still need to handle
  633. the transition. */
  634. void
  635. set_quit_flag (void)
  636. {
  637. if (active_ext_lang->ops != NULL
  638. && active_ext_lang->ops->set_quit_flag != NULL)
  639. active_ext_lang->ops->set_quit_flag (active_ext_lang);
  640. else
  641. {
  642. quit_flag = 1;
  643. /* Now wake up the event loop, or any interruptible_select. Do
  644. this after setting the flag, because signals on Windows
  645. actually run on a separate thread, and thus otherwise the
  646. main code could be woken up and find quit_flag still
  647. clear. */
  648. quit_serial_event_set ();
  649. }
  650. }
  651. /* Return true if the quit flag has been set, false otherwise.
  652. Note: The flag is cleared as a side-effect.
  653. The flag is checked in all extension languages that support cooperative
  654. SIGINT handling, not just the current one. This simplifies transitions. */
  655. int
  656. check_quit_flag (void)
  657. {
  658. int result = 0;
  659. for (const struct extension_language_defn *extlang : extension_languages)
  660. {
  661. if (extlang->ops != nullptr
  662. && extlang->ops->check_quit_flag != NULL)
  663. if (extlang->ops->check_quit_flag (extlang) != 0)
  664. result = 1;
  665. }
  666. /* This is written in a particular way to avoid races. */
  667. if (quit_flag)
  668. {
  669. /* No longer need to wake up the event loop or any
  670. interruptible_select. The caller handles the quit
  671. request. */
  672. quit_serial_event_clear ();
  673. quit_flag = 0;
  674. result = 1;
  675. }
  676. return result;
  677. }
  678. /* See extension.h. */
  679. void
  680. get_matching_xmethod_workers (struct type *type, const char *method_name,
  681. std::vector<xmethod_worker_up> *workers)
  682. {
  683. for (const struct extension_language_defn *extlang : extension_languages)
  684. {
  685. enum ext_lang_rc rc;
  686. /* If an extension language does not support xmethods, ignore
  687. it. */
  688. if (extlang->ops == nullptr
  689. || extlang->ops->get_matching_xmethod_workers == NULL)
  690. continue;
  691. rc = extlang->ops->get_matching_xmethod_workers (extlang,
  692. type, method_name,
  693. workers);
  694. if (rc == EXT_LANG_RC_ERROR)
  695. error (_("Error while looking for matching xmethod workers "
  696. "defined in %s."), extlang->capitalized_name);
  697. }
  698. }
  699. /* See extension.h. */
  700. std::vector<type *>
  701. xmethod_worker::get_arg_types ()
  702. {
  703. std::vector<type *> type_array;
  704. ext_lang_rc rc = do_get_arg_types (&type_array);
  705. if (rc == EXT_LANG_RC_ERROR)
  706. error (_("Error while looking for arg types of a xmethod worker "
  707. "defined in %s."), m_extlang->capitalized_name);
  708. return type_array;
  709. }
  710. /* See extension.h. */
  711. struct type *
  712. xmethod_worker::get_result_type (value *object, gdb::array_view<value *> args)
  713. {
  714. type *result_type;
  715. ext_lang_rc rc = do_get_result_type (object, args, &result_type);
  716. if (rc == EXT_LANG_RC_ERROR)
  717. {
  718. error (_("Error while fetching result type of an xmethod worker "
  719. "defined in %s."), m_extlang->capitalized_name);
  720. }
  721. return result_type;
  722. }
  723. /* See extension.h. */
  724. gdb::optional<std::string>
  725. ext_lang_colorize (const std::string &filename, const std::string &contents)
  726. {
  727. gdb::optional<std::string> result;
  728. for (const struct extension_language_defn *extlang : extension_languages)
  729. {
  730. if (extlang->ops == nullptr
  731. || extlang->ops->colorize == nullptr)
  732. continue;
  733. result = extlang->ops->colorize (filename, contents);
  734. if (result.has_value ())
  735. return result;
  736. }
  737. return result;
  738. }
  739. /* See extension.h. */
  740. gdb::optional<std::string>
  741. ext_lang_colorize_disasm (const std::string &content, gdbarch *gdbarch)
  742. {
  743. gdb::optional<std::string> result;
  744. for (const struct extension_language_defn *extlang : extension_languages)
  745. {
  746. if (extlang->ops == nullptr
  747. || extlang->ops->colorize_disasm == nullptr)
  748. continue;
  749. result = extlang->ops->colorize_disasm (content, gdbarch);
  750. if (result.has_value ())
  751. return result;
  752. }
  753. return result;
  754. }
  755. /* Called via an observer before gdb prints its prompt.
  756. Iterate over the extension languages giving them a chance to
  757. change the prompt. The first one to change the prompt wins,
  758. and no further languages are tried. */
  759. static void
  760. ext_lang_before_prompt (const char *current_gdb_prompt)
  761. {
  762. for (const struct extension_language_defn *extlang : extension_languages)
  763. {
  764. enum ext_lang_rc rc;
  765. if (extlang->ops == nullptr
  766. || extlang->ops->before_prompt == NULL)
  767. continue;
  768. rc = extlang->ops->before_prompt (extlang, current_gdb_prompt);
  769. switch (rc)
  770. {
  771. case EXT_LANG_RC_OK:
  772. case EXT_LANG_RC_ERROR:
  773. return;
  774. case EXT_LANG_RC_NOP:
  775. break;
  776. default:
  777. gdb_assert_not_reached ("bad return from before_prompt");
  778. }
  779. }
  780. }
  781. void _initialize_extension ();
  782. void
  783. _initialize_extension ()
  784. {
  785. gdb::observers::before_prompt.attach (ext_lang_before_prompt, "extension");
  786. }