auto-load.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671
  1. /* GDB routines for supporting auto-loaded scripts.
  2. Copyright (C) 2012-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. #include "defs.h"
  15. #include <ctype.h>
  16. #include "auto-load.h"
  17. #include "progspace.h"
  18. #include "gdbsupport/gdb_regex.h"
  19. #include "ui-out.h"
  20. #include "filenames.h"
  21. #include "command.h"
  22. #include "observable.h"
  23. #include "objfiles.h"
  24. #include "cli/cli-script.h"
  25. #include "gdbcmd.h"
  26. #include "cli/cli-cmds.h"
  27. #include "cli/cli-decode.h"
  28. #include "cli/cli-setshow.h"
  29. #include "readline/tilde.h"
  30. #include "completer.h"
  31. #include "fnmatch.h"
  32. #include "top.h"
  33. #include "gdbsupport/filestuff.h"
  34. #include "extension.h"
  35. #include "gdb/section-scripts.h"
  36. #include <algorithm>
  37. #include "gdbsupport/pathstuff.h"
  38. #include "cli/cli-style.h"
  39. /* The section to look in for auto-loaded scripts (in file formats that
  40. support sections).
  41. Each entry in this section is a record that begins with a leading byte
  42. identifying the record type.
  43. At the moment we only support one record type: A leading byte of 1,
  44. followed by the path of a python script to load. */
  45. #define AUTO_SECTION_NAME ".debug_gdb_scripts"
  46. /* The section to look in for the name of a separate debug file. */
  47. #define DEBUGLINK_SECTION_NAME ".gnu_debuglink"
  48. static void maybe_print_unsupported_script_warning
  49. (struct auto_load_pspace_info *, struct objfile *objfile,
  50. const struct extension_language_defn *language,
  51. const char *section_name, unsigned offset);
  52. static void maybe_print_script_not_found_warning
  53. (struct auto_load_pspace_info *, struct objfile *objfile,
  54. const struct extension_language_defn *language,
  55. const char *section_name, unsigned offset);
  56. /* See auto-load.h. */
  57. bool debug_auto_load = false;
  58. /* "show" command for the debug_auto_load configuration variable. */
  59. static void
  60. show_debug_auto_load (struct ui_file *file, int from_tty,
  61. struct cmd_list_element *c, const char *value)
  62. {
  63. gdb_printf (file, _("Debugging output for files "
  64. "of 'set auto-load ...' is %s.\n"),
  65. value);
  66. }
  67. /* User-settable option to enable/disable auto-loading of GDB_AUTO_FILE_NAME
  68. scripts:
  69. set auto-load gdb-scripts on|off
  70. This is true if we should auto-load associated scripts when an objfile
  71. is opened, false otherwise. */
  72. static bool auto_load_gdb_scripts = true;
  73. /* "show" command for the auto_load_gdb_scripts configuration variable. */
  74. static void
  75. show_auto_load_gdb_scripts (struct ui_file *file, int from_tty,
  76. struct cmd_list_element *c, const char *value)
  77. {
  78. gdb_printf (file, _("Auto-loading of canned sequences of commands "
  79. "scripts is %s.\n"),
  80. value);
  81. }
  82. /* See auto-load.h. */
  83. bool
  84. auto_load_gdb_scripts_enabled (const struct extension_language_defn *extlang)
  85. {
  86. return auto_load_gdb_scripts;
  87. }
  88. /* Internal-use flag to enable/disable auto-loading.
  89. This is true if we should auto-load python code when an objfile is opened,
  90. false otherwise.
  91. Both auto_load_scripts && global_auto_load must be true to enable
  92. auto-loading.
  93. This flag exists to facilitate deferring auto-loading during start-up
  94. until after ./.gdbinit has been read; it may augment the search directories
  95. used to find the scripts. */
  96. bool global_auto_load = true;
  97. /* Auto-load .gdbinit file from the current directory? */
  98. bool auto_load_local_gdbinit = true;
  99. /* Absolute pathname to the current directory .gdbinit, if it exists. */
  100. char *auto_load_local_gdbinit_pathname = NULL;
  101. /* if AUTO_LOAD_LOCAL_GDBINIT_PATHNAME has been loaded. */
  102. bool auto_load_local_gdbinit_loaded = false;
  103. /* "show" command for the auto_load_local_gdbinit configuration variable. */
  104. static void
  105. show_auto_load_local_gdbinit (struct ui_file *file, int from_tty,
  106. struct cmd_list_element *c, const char *value)
  107. {
  108. gdb_printf (file, _("Auto-loading of .gdbinit script from current "
  109. "directory is %s.\n"),
  110. value);
  111. }
  112. /* Directory list from which to load auto-loaded scripts. It is not checked
  113. for absolute paths but they are strongly recommended. It is initialized by
  114. _initialize_auto_load. */
  115. static std::string auto_load_dir = AUTO_LOAD_DIR;
  116. /* "set" command for the auto_load_dir configuration variable. */
  117. static void
  118. set_auto_load_dir (const char *args, int from_tty, struct cmd_list_element *c)
  119. {
  120. /* Setting the variable to "" resets it to the compile time defaults. */
  121. if (auto_load_dir.empty ())
  122. auto_load_dir = AUTO_LOAD_DIR;
  123. }
  124. /* "show" command for the auto_load_dir configuration variable. */
  125. static void
  126. show_auto_load_dir (struct ui_file *file, int from_tty,
  127. struct cmd_list_element *c, const char *value)
  128. {
  129. gdb_printf (file, _("List of directories from which to load "
  130. "auto-loaded scripts is %s.\n"),
  131. value);
  132. }
  133. /* Directory list safe to hold auto-loaded files. It is not checked for
  134. absolute paths but they are strongly recommended. It is initialized by
  135. _initialize_auto_load. */
  136. static std::string auto_load_safe_path = AUTO_LOAD_SAFE_PATH;
  137. /* Vector of directory elements of AUTO_LOAD_SAFE_PATH with each one normalized
  138. by tilde_expand and possibly each entries has added its gdb_realpath
  139. counterpart. */
  140. static std::vector<gdb::unique_xmalloc_ptr<char>> auto_load_safe_path_vec;
  141. /* Expand $datadir and $debugdir in STRING according to the rules of
  142. substitute_path_component. */
  143. static std::vector<gdb::unique_xmalloc_ptr<char>>
  144. auto_load_expand_dir_vars (const char *string)
  145. {
  146. char *s = xstrdup (string);
  147. substitute_path_component (&s, "$datadir", gdb_datadir.c_str ());
  148. substitute_path_component (&s, "$debugdir", debug_file_directory.c_str ());
  149. if (debug_auto_load && strcmp (s, string) != 0)
  150. auto_load_debug_printf ("Expanded $-variables to \"%s\".", s);
  151. std::vector<gdb::unique_xmalloc_ptr<char>> dir_vec
  152. = dirnames_to_char_ptr_vec (s);
  153. xfree(s);
  154. return dir_vec;
  155. }
  156. /* Update auto_load_safe_path_vec from current AUTO_LOAD_SAFE_PATH. */
  157. static void
  158. auto_load_safe_path_vec_update (void)
  159. {
  160. auto_load_debug_printf ("Updating directories of \"%s\".",
  161. auto_load_safe_path.c_str ());
  162. auto_load_safe_path_vec
  163. = auto_load_expand_dir_vars (auto_load_safe_path.c_str ());
  164. size_t len = auto_load_safe_path_vec.size ();
  165. /* Apply tilde_expand and gdb_realpath to each AUTO_LOAD_SAFE_PATH_VEC
  166. element. */
  167. for (size_t i = 0; i < len; i++)
  168. {
  169. gdb::unique_xmalloc_ptr<char> &in_vec = auto_load_safe_path_vec[i];
  170. gdb::unique_xmalloc_ptr<char> expanded (tilde_expand (in_vec.get ()));
  171. gdb::unique_xmalloc_ptr<char> real_path = gdb_realpath (expanded.get ());
  172. /* Ensure the current entry is at least tilde_expand-ed. ORIGINAL makes
  173. sure we free the original string. */
  174. gdb::unique_xmalloc_ptr<char> original = std::move (in_vec);
  175. in_vec = std::move (expanded);
  176. if (debug_auto_load)
  177. {
  178. if (strcmp (in_vec.get (), original.get ()) == 0)
  179. auto_load_debug_printf ("Using directory \"%s\".",
  180. in_vec.get ());
  181. else
  182. auto_load_debug_printf ("Resolved directory \"%s\" as \"%s\".",
  183. original.get (), in_vec.get ());
  184. }
  185. /* If gdb_realpath returns a different content, append it. */
  186. if (strcmp (real_path.get (), in_vec.get ()) != 0)
  187. {
  188. auto_load_debug_printf ("And canonicalized as \"%s\".",
  189. real_path.get ());
  190. auto_load_safe_path_vec.push_back (std::move (real_path));
  191. }
  192. }
  193. }
  194. /* Variable gdb_datadir has been set. Update content depending on $datadir. */
  195. static void
  196. auto_load_gdb_datadir_changed (void)
  197. {
  198. auto_load_safe_path_vec_update ();
  199. }
  200. /* "set" command for the auto_load_safe_path configuration variable. */
  201. static void
  202. set_auto_load_safe_path (const char *args,
  203. int from_tty, struct cmd_list_element *c)
  204. {
  205. /* Setting the variable to "" resets it to the compile time defaults. */
  206. if (auto_load_safe_path.empty ())
  207. auto_load_safe_path = AUTO_LOAD_SAFE_PATH;
  208. auto_load_safe_path_vec_update ();
  209. }
  210. /* "show" command for the auto_load_safe_path configuration variable. */
  211. static void
  212. show_auto_load_safe_path (struct ui_file *file, int from_tty,
  213. struct cmd_list_element *c, const char *value)
  214. {
  215. const char *cs;
  216. /* Check if user has entered either "/" or for example ":".
  217. But while more complicate content like ":/foo" would still also
  218. permit any location do not hide those. */
  219. for (cs = value; *cs && (*cs == DIRNAME_SEPARATOR || IS_DIR_SEPARATOR (*cs));
  220. cs++);
  221. if (*cs == 0)
  222. gdb_printf (file, _("Auto-load files are safe to load from any "
  223. "directory.\n"));
  224. else
  225. gdb_printf (file, _("List of directories from which it is safe to "
  226. "auto-load files is %s.\n"),
  227. value);
  228. }
  229. /* "add-auto-load-safe-path" command for the auto_load_safe_path configuration
  230. variable. */
  231. static void
  232. add_auto_load_safe_path (const char *args, int from_tty)
  233. {
  234. if (args == NULL || *args == 0)
  235. error (_("\
  236. Directory argument required.\n\
  237. Use 'set auto-load safe-path /' for disabling the auto-load safe-path security.\
  238. "));
  239. auto_load_safe_path = string_printf ("%s%c%s", auto_load_safe_path.c_str (),
  240. DIRNAME_SEPARATOR, args);
  241. auto_load_safe_path_vec_update ();
  242. }
  243. /* "add-auto-load-scripts-directory" command for the auto_load_dir configuration
  244. variable. */
  245. static void
  246. add_auto_load_dir (const char *args, int from_tty)
  247. {
  248. if (args == NULL || *args == 0)
  249. error (_("Directory argument required."));
  250. auto_load_dir = string_printf ("%s%c%s", auto_load_dir.c_str (),
  251. DIRNAME_SEPARATOR, args);
  252. }
  253. /* Implementation for filename_is_in_pattern overwriting the caller's FILENAME
  254. and PATTERN. */
  255. static int
  256. filename_is_in_pattern_1 (char *filename, char *pattern)
  257. {
  258. size_t pattern_len = strlen (pattern);
  259. size_t filename_len = strlen (filename);
  260. auto_load_debug_printf ("Matching file \"%s\" to pattern \"%s\"",
  261. filename, pattern);
  262. /* Trim trailing slashes ("/") from PATTERN. Even for "d:\" paths as
  263. trailing slashes are trimmed also from FILENAME it still matches
  264. correctly. */
  265. while (pattern_len && IS_DIR_SEPARATOR (pattern[pattern_len - 1]))
  266. pattern_len--;
  267. pattern[pattern_len] = '\0';
  268. /* Ensure auto_load_safe_path "/" matches any FILENAME. On MS-Windows
  269. platform FILENAME even after gdb_realpath does not have to start with
  270. IS_DIR_SEPARATOR character, such as the 'C:\x.exe' filename. */
  271. if (pattern_len == 0)
  272. {
  273. auto_load_debug_printf ("Matched - empty pattern");
  274. return 1;
  275. }
  276. for (;;)
  277. {
  278. /* Trim trailing slashes ("/"). PATTERN also has slashes trimmed the
  279. same way so they will match. */
  280. while (filename_len && IS_DIR_SEPARATOR (filename[filename_len - 1]))
  281. filename_len--;
  282. filename[filename_len] = '\0';
  283. if (filename_len == 0)
  284. {
  285. auto_load_debug_printf ("Not matched - pattern \"%s\".", pattern);
  286. return 0;
  287. }
  288. if (gdb_filename_fnmatch (pattern, filename, FNM_FILE_NAME | FNM_NOESCAPE)
  289. == 0)
  290. {
  291. auto_load_debug_printf ("Matched - file \"%s\" to pattern \"%s\".",
  292. filename, pattern);
  293. return 1;
  294. }
  295. /* Trim trailing FILENAME component. */
  296. while (filename_len > 0 && !IS_DIR_SEPARATOR (filename[filename_len - 1]))
  297. filename_len--;
  298. }
  299. }
  300. /* Return 1 if FILENAME matches PATTERN or if FILENAME resides in
  301. a subdirectory of a directory that matches PATTERN. Return 0 otherwise.
  302. gdb_realpath normalization is never done here. */
  303. static ATTRIBUTE_PURE int
  304. filename_is_in_pattern (const char *filename, const char *pattern)
  305. {
  306. char *filename_copy, *pattern_copy;
  307. filename_copy = (char *) alloca (strlen (filename) + 1);
  308. strcpy (filename_copy, filename);
  309. pattern_copy = (char *) alloca (strlen (pattern) + 1);
  310. strcpy (pattern_copy, pattern);
  311. return filename_is_in_pattern_1 (filename_copy, pattern_copy);
  312. }
  313. /* Return 1 if FILENAME belongs to one of directory components of
  314. AUTO_LOAD_SAFE_PATH_VEC. Return 0 otherwise.
  315. auto_load_safe_path_vec_update is never called.
  316. *FILENAME_REALP may be updated by gdb_realpath of FILENAME. */
  317. static int
  318. filename_is_in_auto_load_safe_path_vec (const char *filename,
  319. gdb::unique_xmalloc_ptr<char> *filename_realp)
  320. {
  321. const char *pattern = NULL;
  322. for (const gdb::unique_xmalloc_ptr<char> &p : auto_load_safe_path_vec)
  323. if (*filename_realp == NULL && filename_is_in_pattern (filename, p.get ()))
  324. {
  325. pattern = p.get ();
  326. break;
  327. }
  328. if (pattern == NULL)
  329. {
  330. if (*filename_realp == NULL)
  331. {
  332. *filename_realp = gdb_realpath (filename);
  333. if (debug_auto_load && strcmp (filename_realp->get (), filename) != 0)
  334. auto_load_debug_printf ("Resolved file \"%s\" as \"%s\".",
  335. filename, filename_realp->get ());
  336. }
  337. if (strcmp (filename_realp->get (), filename) != 0)
  338. for (const gdb::unique_xmalloc_ptr<char> &p : auto_load_safe_path_vec)
  339. if (filename_is_in_pattern (filename_realp->get (), p.get ()))
  340. {
  341. pattern = p.get ();
  342. break;
  343. }
  344. }
  345. if (pattern != NULL)
  346. {
  347. auto_load_debug_printf ("File \"%s\" matches directory \"%s\".",
  348. filename, pattern);
  349. return 1;
  350. }
  351. return 0;
  352. }
  353. /* See auto-load.h. */
  354. bool
  355. file_is_auto_load_safe (const char *filename)
  356. {
  357. gdb::unique_xmalloc_ptr<char> filename_real;
  358. static bool advice_printed = false;
  359. if (filename_is_in_auto_load_safe_path_vec (filename, &filename_real))
  360. return true;
  361. auto_load_safe_path_vec_update ();
  362. if (filename_is_in_auto_load_safe_path_vec (filename, &filename_real))
  363. return true;
  364. warning (_("File \"%ps\" auto-loading has been declined by your "
  365. "`auto-load safe-path' set to \"%s\"."),
  366. styled_string (file_name_style.style (), filename_real.get ()),
  367. auto_load_safe_path.c_str ());
  368. if (!advice_printed)
  369. {
  370. /* Find the existing home directory config file. */
  371. struct stat buf;
  372. std::string home_config = find_gdb_home_config_file (GDBINIT, &buf);
  373. if (home_config.empty ())
  374. {
  375. /* The user doesn't have an existing home directory config file,
  376. so we should suggest a suitable path for them to use. */
  377. std::string config_dir_file
  378. = get_standard_config_filename (GDBINIT);
  379. if (!config_dir_file.empty ())
  380. home_config = config_dir_file;
  381. else
  382. {
  383. const char *homedir = getenv ("HOME");
  384. if (homedir == nullptr)
  385. homedir = "$HOME";
  386. home_config = (std::string (homedir) + SLASH_STRING
  387. + std::string (GDBINIT));
  388. }
  389. }
  390. gdb_printf (_("\
  391. To enable execution of this file add\n\
  392. \tadd-auto-load-safe-path %s\n\
  393. line to your configuration file \"%ps\".\n\
  394. To completely disable this security protection add\n\
  395. \tset auto-load safe-path /\n\
  396. line to your configuration file \"%ps\".\n\
  397. For more information about this security protection see the\n\
  398. \"Auto-loading safe path\" section in the GDB manual. E.g., run from the shell:\n\
  399. \tinfo \"(gdb)Auto-loading safe path\"\n"),
  400. filename_real.get (),
  401. styled_string (file_name_style.style (),
  402. home_config.c_str ()),
  403. styled_string (file_name_style.style (),
  404. home_config.c_str ()));
  405. advice_printed = true;
  406. }
  407. return false;
  408. }
  409. /* For scripts specified in .debug_gdb_scripts, multiple objfiles may load
  410. the same script. There's no point in loading the script multiple times,
  411. and there can be a lot of objfiles and scripts, so we keep track of scripts
  412. loaded this way. */
  413. struct auto_load_pspace_info
  414. {
  415. /* For each program space we keep track of loaded scripts, both when
  416. specified as file names and as scripts to be executed directly. */
  417. htab_up loaded_script_files;
  418. htab_up loaded_script_texts;
  419. /* Non-zero if we've issued the warning about an auto-load script not being
  420. supported. We only want to issue this warning once. */
  421. bool unsupported_script_warning_printed = false;
  422. /* Non-zero if we've issued the warning about an auto-load script not being
  423. found. We only want to issue this warning once. */
  424. bool script_not_found_warning_printed = false;
  425. };
  426. /* Objects of this type are stored in the loaded_script hash table. */
  427. struct loaded_script
  428. {
  429. /* Name as provided by the objfile. */
  430. const char *name;
  431. /* Full path name or NULL if script wasn't found (or was otherwise
  432. inaccessible), or NULL for loaded_script_texts. */
  433. const char *full_path;
  434. /* True if this script has been loaded. */
  435. bool loaded;
  436. const struct extension_language_defn *language;
  437. };
  438. /* Per-program-space data key. */
  439. static const struct program_space_key<struct auto_load_pspace_info>
  440. auto_load_pspace_data;
  441. /* Get the current autoload data. If none is found yet, add it now. This
  442. function always returns a valid object. */
  443. static struct auto_load_pspace_info *
  444. get_auto_load_pspace_data (struct program_space *pspace)
  445. {
  446. struct auto_load_pspace_info *info;
  447. info = auto_load_pspace_data.get (pspace);
  448. if (info == NULL)
  449. info = auto_load_pspace_data.emplace (pspace);
  450. return info;
  451. }
  452. /* Hash function for the loaded script hash. */
  453. static hashval_t
  454. hash_loaded_script_entry (const void *data)
  455. {
  456. const struct loaded_script *e = (const struct loaded_script *) data;
  457. return htab_hash_string (e->name) ^ htab_hash_pointer (e->language);
  458. }
  459. /* Equality function for the loaded script hash. */
  460. static int
  461. eq_loaded_script_entry (const void *a, const void *b)
  462. {
  463. const struct loaded_script *ea = (const struct loaded_script *) a;
  464. const struct loaded_script *eb = (const struct loaded_script *) b;
  465. return strcmp (ea->name, eb->name) == 0 && ea->language == eb->language;
  466. }
  467. /* Initialize the table to track loaded scripts.
  468. Each entry is hashed by the full path name. */
  469. static void
  470. init_loaded_scripts_info (struct auto_load_pspace_info *pspace_info)
  471. {
  472. /* Choose 31 as the starting size of the hash table, somewhat arbitrarily.
  473. Space for each entry is obtained with one malloc so we can free them
  474. easily. */
  475. pspace_info->loaded_script_files.reset
  476. (htab_create (31,
  477. hash_loaded_script_entry,
  478. eq_loaded_script_entry,
  479. xfree));
  480. pspace_info->loaded_script_texts.reset
  481. (htab_create (31,
  482. hash_loaded_script_entry,
  483. eq_loaded_script_entry,
  484. xfree));
  485. pspace_info->unsupported_script_warning_printed = false;
  486. pspace_info->script_not_found_warning_printed = false;
  487. }
  488. /* Wrapper on get_auto_load_pspace_data to also allocate the hash table
  489. for loading scripts. */
  490. struct auto_load_pspace_info *
  491. get_auto_load_pspace_data_for_loading (struct program_space *pspace)
  492. {
  493. struct auto_load_pspace_info *info;
  494. info = get_auto_load_pspace_data (pspace);
  495. if (info->loaded_script_files == NULL)
  496. init_loaded_scripts_info (info);
  497. return info;
  498. }
  499. /* Add script file NAME in LANGUAGE to hash table of PSPACE_INFO.
  500. LOADED is true if the script has been (is going to) be loaded, false
  501. otherwise (such as if it has not been found).
  502. FULL_PATH is NULL if the script wasn't found.
  503. The result is true if the script was already in the hash table. */
  504. static bool
  505. maybe_add_script_file (struct auto_load_pspace_info *pspace_info, bool loaded,
  506. const char *name, const char *full_path,
  507. const struct extension_language_defn *language)
  508. {
  509. struct htab *htab = pspace_info->loaded_script_files.get ();
  510. struct loaded_script **slot, entry;
  511. entry.name = name;
  512. entry.language = language;
  513. slot = (struct loaded_script **) htab_find_slot (htab, &entry, INSERT);
  514. bool in_hash_table = *slot != NULL;
  515. /* If this script is not in the hash table, add it. */
  516. if (!in_hash_table)
  517. {
  518. char *p;
  519. /* Allocate all space in one chunk so it's easier to free. */
  520. *slot = ((struct loaded_script *)
  521. xmalloc (sizeof (**slot)
  522. + strlen (name) + 1
  523. + (full_path != NULL ? (strlen (full_path) + 1) : 0)));
  524. p = ((char*) *slot) + sizeof (**slot);
  525. strcpy (p, name);
  526. (*slot)->name = p;
  527. if (full_path != NULL)
  528. {
  529. p += strlen (p) + 1;
  530. strcpy (p, full_path);
  531. (*slot)->full_path = p;
  532. }
  533. else
  534. (*slot)->full_path = NULL;
  535. (*slot)->loaded = loaded;
  536. (*slot)->language = language;
  537. }
  538. return in_hash_table;
  539. }
  540. /* Add script contents NAME in LANGUAGE to hash table of PSPACE_INFO.
  541. LOADED is true if the script has been (is going to) be loaded, false
  542. otherwise (such as if it has not been found).
  543. The result is true if the script was already in the hash table. */
  544. static bool
  545. maybe_add_script_text (struct auto_load_pspace_info *pspace_info,
  546. bool loaded, const char *name,
  547. const struct extension_language_defn *language)
  548. {
  549. struct htab *htab = pspace_info->loaded_script_texts.get ();
  550. struct loaded_script **slot, entry;
  551. entry.name = name;
  552. entry.language = language;
  553. slot = (struct loaded_script **) htab_find_slot (htab, &entry, INSERT);
  554. bool in_hash_table = *slot != NULL;
  555. /* If this script is not in the hash table, add it. */
  556. if (!in_hash_table)
  557. {
  558. char *p;
  559. /* Allocate all space in one chunk so it's easier to free. */
  560. *slot = ((struct loaded_script *)
  561. xmalloc (sizeof (**slot) + strlen (name) + 1));
  562. p = ((char*) *slot) + sizeof (**slot);
  563. strcpy (p, name);
  564. (*slot)->name = p;
  565. (*slot)->full_path = NULL;
  566. (*slot)->loaded = loaded;
  567. (*slot)->language = language;
  568. }
  569. return in_hash_table;
  570. }
  571. /* Clear the table of loaded section scripts. */
  572. static void
  573. clear_section_scripts (void)
  574. {
  575. struct program_space *pspace = current_program_space;
  576. struct auto_load_pspace_info *info;
  577. info = auto_load_pspace_data.get (pspace);
  578. if (info != NULL && info->loaded_script_files != NULL)
  579. auto_load_pspace_data.clear (pspace);
  580. }
  581. /* Look for the auto-load script in LANGUAGE associated with OBJFILE where
  582. OBJFILE's gdb_realpath is REALNAME and load it. Return 1 if we found any
  583. matching script, return 0 otherwise. */
  584. static int
  585. auto_load_objfile_script_1 (struct objfile *objfile, const char *realname,
  586. const struct extension_language_defn *language)
  587. {
  588. const char *debugfile;
  589. int retval;
  590. const char *suffix = ext_lang_auto_load_suffix (language);
  591. std::string filename = std::string (realname) + suffix;
  592. gdb_file_up input = gdb_fopen_cloexec (filename.c_str (), "r");
  593. debugfile = filename.c_str ();
  594. auto_load_debug_printf ("Attempted file \"%ps\" %s.",
  595. styled_string (file_name_style.style (), debugfile),
  596. input != nullptr ? "exists" : "does not exist");
  597. std::string debugfile_holder;
  598. if (!input)
  599. {
  600. /* Also try the same file in a subdirectory of gdb's data
  601. directory. */
  602. std::vector<gdb::unique_xmalloc_ptr<char>> vec
  603. = auto_load_expand_dir_vars (auto_load_dir.c_str ());
  604. auto_load_debug_printf
  605. ("Searching 'set auto-load scripts-directory' path \"%s\".",
  606. auto_load_dir.c_str ());
  607. /* Convert Windows file name from c:/dir/file to /c/dir/file. */
  608. if (HAS_DRIVE_SPEC (debugfile))
  609. filename = (std::string("\\") + debugfile[0]
  610. + STRIP_DRIVE_SPEC (debugfile));
  611. for (const gdb::unique_xmalloc_ptr<char> &dir : vec)
  612. {
  613. /* FILENAME is absolute, so we don't need a "/" here. */
  614. debugfile_holder = dir.get () + filename;
  615. debugfile = debugfile_holder.c_str ();
  616. input = gdb_fopen_cloexec (debugfile, "r");
  617. auto_load_debug_printf ("Attempted file \"%ps\" %s.",
  618. styled_string (file_name_style.style (),
  619. debugfile),
  620. (input != nullptr
  621. ? "exists"
  622. : "does not exist"));
  623. if (input != NULL)
  624. break;
  625. }
  626. }
  627. if (input)
  628. {
  629. struct auto_load_pspace_info *pspace_info;
  630. auto_load_debug_printf
  631. ("Loading %s script \"%s\" by extension for objfile \"%s\".",
  632. ext_lang_name (language), debugfile, objfile_name (objfile));
  633. bool is_safe = file_is_auto_load_safe (debugfile);
  634. /* Add this script to the hash table too so
  635. "info auto-load ${lang}-scripts" can print it. */
  636. pspace_info
  637. = get_auto_load_pspace_data_for_loading (current_program_space);
  638. maybe_add_script_file (pspace_info, is_safe, debugfile, debugfile,
  639. language);
  640. /* To preserve existing behaviour we don't check for whether the
  641. script was already in the table, and always load it.
  642. It's highly unlikely that we'd ever load it twice,
  643. and these scripts are required to be idempotent under multiple
  644. loads anyway. */
  645. if (is_safe)
  646. {
  647. objfile_script_sourcer_func *sourcer
  648. = ext_lang_objfile_script_sourcer (language);
  649. /* We shouldn't get here if support for the language isn't
  650. compiled in. And the extension language is required to implement
  651. this function. */
  652. gdb_assert (sourcer != NULL);
  653. sourcer (language, objfile, input.get (), debugfile);
  654. }
  655. retval = 1;
  656. }
  657. else
  658. retval = 0;
  659. return retval;
  660. }
  661. /* Look for the auto-load script in LANGUAGE associated with OBJFILE and load
  662. it. */
  663. void
  664. auto_load_objfile_script (struct objfile *objfile,
  665. const struct extension_language_defn *language)
  666. {
  667. gdb::unique_xmalloc_ptr<char> realname
  668. = gdb_realpath (objfile_name (objfile));
  669. if (auto_load_objfile_script_1 (objfile, realname.get (), language))
  670. return;
  671. /* For Windows/DOS .exe executables, strip the .exe suffix, so that
  672. FOO-gdb.gdb could be used for FOO.exe, and try again. */
  673. size_t len = strlen (realname.get ());
  674. const size_t lexe = sizeof (".exe") - 1;
  675. if (len > lexe && strcasecmp (realname.get () + len - lexe, ".exe") == 0)
  676. {
  677. len -= lexe;
  678. realname.get ()[len] = '\0';
  679. auto_load_debug_printf
  680. ("Stripped .exe suffix, retrying with \"%s\".", realname.get ());
  681. auto_load_objfile_script_1 (objfile, realname.get (), language);
  682. return;
  683. }
  684. /* If OBJFILE is a separate debug file and its name does not match
  685. the name given in the parent's .gnu_debuglink section, try to
  686. find the auto-load script using the parent's path and the
  687. debuglink name. */
  688. struct objfile *parent = objfile->separate_debug_objfile_backlink;
  689. if (parent != nullptr)
  690. {
  691. unsigned long crc32;
  692. gdb::unique_xmalloc_ptr<char> debuglink
  693. (bfd_get_debug_link_info (parent->obfd, &crc32));
  694. if (debuglink.get () != nullptr
  695. && strcmp (debuglink.get (), lbasename (realname.get ())) != 0)
  696. {
  697. /* Replace the last component of the parent's path with the
  698. debuglink name. */
  699. std::string p_realname = gdb_realpath (objfile_name (parent)).get ();
  700. size_t last = p_realname.find_last_of ('/');
  701. if (last != std::string::npos)
  702. {
  703. p_realname.replace (last + 1, std::string::npos,
  704. debuglink.get ());
  705. auto_load_debug_printf
  706. ("Debug filename mismatch, retrying with \"%s\".",
  707. p_realname.c_str ());
  708. auto_load_objfile_script_1 (objfile,
  709. p_realname.c_str (), language);
  710. }
  711. }
  712. }
  713. }
  714. /* Subroutine of source_section_scripts to simplify it.
  715. Load FILE as a script in extension language LANGUAGE.
  716. The script is from section SECTION_NAME in OBJFILE at offset OFFSET. */
  717. static void
  718. source_script_file (struct auto_load_pspace_info *pspace_info,
  719. struct objfile *objfile,
  720. const struct extension_language_defn *language,
  721. const char *section_name, unsigned int offset,
  722. const char *file)
  723. {
  724. objfile_script_sourcer_func *sourcer;
  725. /* Skip this script if support is not compiled in. */
  726. sourcer = ext_lang_objfile_script_sourcer (language);
  727. if (sourcer == NULL)
  728. {
  729. /* We don't throw an error, the program is still debuggable. */
  730. maybe_print_unsupported_script_warning (pspace_info, objfile, language,
  731. section_name, offset);
  732. /* We *could* still try to open it, but there's no point. */
  733. maybe_add_script_file (pspace_info, 0, file, NULL, language);
  734. return;
  735. }
  736. /* Skip this script if auto-loading it has been disabled. */
  737. if (!ext_lang_auto_load_enabled (language))
  738. {
  739. /* No message is printed, just skip it. */
  740. return;
  741. }
  742. gdb::optional<open_script> opened = find_and_open_script (file,
  743. 1 /*search_path*/);
  744. if (opened)
  745. {
  746. auto_load_debug_printf
  747. ("Loading %s script \"%s\" from section \"%s\" of objfile \"%s\".",
  748. ext_lang_name (language), opened->full_path.get (),
  749. section_name, objfile_name (objfile));
  750. if (!file_is_auto_load_safe (opened->full_path.get ()))
  751. opened.reset ();
  752. }
  753. else
  754. {
  755. /* If one script isn't found it's not uncommon for more to not be
  756. found either. We don't want to print a message for each script,
  757. too much noise. Instead, we print the warning once and tell the
  758. user how to find the list of scripts that weren't loaded.
  759. We don't throw an error, the program is still debuggable.
  760. IWBN if complaints.c were more general-purpose. */
  761. maybe_print_script_not_found_warning (pspace_info, objfile, language,
  762. section_name, offset);
  763. }
  764. bool in_hash_table
  765. = maybe_add_script_file (pspace_info, bool (opened), file,
  766. (opened ? opened->full_path.get (): NULL),
  767. language);
  768. /* If this file is not currently loaded, load it. */
  769. if (opened && !in_hash_table)
  770. sourcer (language, objfile, opened->stream.get (),
  771. opened->full_path.get ());
  772. }
  773. /* Subroutine of source_section_scripts to simplify it.
  774. Execute SCRIPT as a script in extension language LANG.
  775. The script is from section SECTION_NAME in OBJFILE at offset OFFSET. */
  776. static void
  777. execute_script_contents (struct auto_load_pspace_info *pspace_info,
  778. struct objfile *objfile,
  779. const struct extension_language_defn *language,
  780. const char *section_name, unsigned int offset,
  781. const char *script)
  782. {
  783. objfile_script_executor_func *executor;
  784. const char *newline, *script_text;
  785. const char *name;
  786. /* The first line of the script is the name of the script.
  787. It must not contain any kind of space character. */
  788. name = NULL;
  789. newline = strchr (script, '\n');
  790. std::string name_holder;
  791. if (newline != NULL)
  792. {
  793. const char *buf, *p;
  794. /* Put the name in a buffer and validate it. */
  795. name_holder = std::string (script, newline - script);
  796. buf = name_holder.c_str ();
  797. for (p = buf; *p != '\0'; ++p)
  798. {
  799. if (isspace (*p))
  800. break;
  801. }
  802. /* We don't allow nameless scripts, they're not helpful to the user. */
  803. if (p != buf && *p == '\0')
  804. name = buf;
  805. }
  806. if (name == NULL)
  807. {
  808. /* We don't throw an error, the program is still debuggable. */
  809. warning (_("\
  810. Missing/bad script name in entry at offset %u in section %s\n\
  811. of file %ps."),
  812. offset, section_name,
  813. styled_string (file_name_style.style (),
  814. objfile_name (objfile)));
  815. return;
  816. }
  817. script_text = newline + 1;
  818. /* Skip this script if support is not compiled in. */
  819. executor = ext_lang_objfile_script_executor (language);
  820. if (executor == NULL)
  821. {
  822. /* We don't throw an error, the program is still debuggable. */
  823. maybe_print_unsupported_script_warning (pspace_info, objfile, language,
  824. section_name, offset);
  825. maybe_add_script_text (pspace_info, 0, name, language);
  826. return;
  827. }
  828. /* Skip this script if auto-loading it has been disabled. */
  829. if (!ext_lang_auto_load_enabled (language))
  830. {
  831. /* No message is printed, just skip it. */
  832. return;
  833. }
  834. auto_load_debug_printf
  835. ("Loading %s script \"%s\" from section \"%s\" of objfile \"%s\".",
  836. ext_lang_name (language), name, section_name, objfile_name (objfile));
  837. bool is_safe = file_is_auto_load_safe (objfile_name (objfile));
  838. bool in_hash_table
  839. = maybe_add_script_text (pspace_info, is_safe, name, language);
  840. /* If this file is not currently loaded, load it. */
  841. if (is_safe && !in_hash_table)
  842. executor (language, objfile, name, script_text);
  843. }
  844. /* Load scripts specified in OBJFILE.
  845. START,END delimit a buffer containing a list of nul-terminated
  846. file names.
  847. SECTION_NAME is used in error messages.
  848. Scripts specified as file names are found per normal "source -s" command
  849. processing. First the script is looked for in $cwd. If not found there
  850. the source search path is used.
  851. The section contains a list of path names of script files to load or
  852. actual script contents. Each entry is nul-terminated. */
  853. static void
  854. source_section_scripts (struct objfile *objfile, const char *section_name,
  855. const char *start, const char *end)
  856. {
  857. const char *p;
  858. struct auto_load_pspace_info *pspace_info;
  859. pspace_info = get_auto_load_pspace_data_for_loading (current_program_space);
  860. for (p = start; p < end; ++p)
  861. {
  862. const char *entry;
  863. const struct extension_language_defn *language;
  864. unsigned int offset = p - start;
  865. int code = *p;
  866. switch (code)
  867. {
  868. case SECTION_SCRIPT_ID_PYTHON_FILE:
  869. case SECTION_SCRIPT_ID_PYTHON_TEXT:
  870. language = get_ext_lang_defn (EXT_LANG_PYTHON);
  871. break;
  872. case SECTION_SCRIPT_ID_SCHEME_FILE:
  873. case SECTION_SCRIPT_ID_SCHEME_TEXT:
  874. language = get_ext_lang_defn (EXT_LANG_GUILE);
  875. break;
  876. default:
  877. warning (_("Invalid entry in %s section"), section_name);
  878. /* We could try various heuristics to find the next valid entry,
  879. but it's safer to just punt. */
  880. return;
  881. }
  882. entry = ++p;
  883. while (p < end && *p != '\0')
  884. ++p;
  885. if (p == end)
  886. {
  887. warning (_("Non-nul-terminated entry in %s at offset %u"),
  888. section_name, offset);
  889. /* Don't load/execute it. */
  890. break;
  891. }
  892. switch (code)
  893. {
  894. case SECTION_SCRIPT_ID_PYTHON_FILE:
  895. case SECTION_SCRIPT_ID_SCHEME_FILE:
  896. if (p == entry)
  897. {
  898. warning (_("Empty entry in %s at offset %u"),
  899. section_name, offset);
  900. continue;
  901. }
  902. source_script_file (pspace_info, objfile, language,
  903. section_name, offset, entry);
  904. break;
  905. case SECTION_SCRIPT_ID_PYTHON_TEXT:
  906. case SECTION_SCRIPT_ID_SCHEME_TEXT:
  907. execute_script_contents (pspace_info, objfile, language,
  908. section_name, offset, entry);
  909. break;
  910. }
  911. }
  912. }
  913. /* Load scripts specified in section SECTION_NAME of OBJFILE. */
  914. static void
  915. auto_load_section_scripts (struct objfile *objfile, const char *section_name)
  916. {
  917. bfd *abfd = objfile->obfd;
  918. asection *scripts_sect;
  919. bfd_byte *data = NULL;
  920. scripts_sect = bfd_get_section_by_name (abfd, section_name);
  921. if (scripts_sect == NULL
  922. || (bfd_section_flags (scripts_sect) & SEC_HAS_CONTENTS) == 0)
  923. return;
  924. if (!bfd_get_full_section_contents (abfd, scripts_sect, &data))
  925. warning (_("Couldn't read %s section of %ps"),
  926. section_name,
  927. styled_string (file_name_style.style (),
  928. bfd_get_filename (abfd)));
  929. else
  930. {
  931. gdb::unique_xmalloc_ptr<bfd_byte> data_holder (data);
  932. char *p = (char *) data;
  933. source_section_scripts (objfile, section_name, p,
  934. p + bfd_section_size (scripts_sect));
  935. }
  936. }
  937. /* Load any auto-loaded scripts for OBJFILE. */
  938. void
  939. load_auto_scripts_for_objfile (struct objfile *objfile)
  940. {
  941. /* Return immediately if auto-loading has been globally disabled.
  942. This is to handle sequencing of operations during gdb startup.
  943. Also return immediately if OBJFILE was not created from a file
  944. on the local filesystem. */
  945. if (!global_auto_load
  946. || (objfile->flags & OBJF_NOT_FILENAME) != 0
  947. || is_target_filename (objfile->original_name))
  948. return;
  949. /* Load any extension language scripts for this objfile.
  950. E.g., foo-gdb.gdb, foo-gdb.py. */
  951. auto_load_ext_lang_scripts_for_objfile (objfile);
  952. /* Load any scripts mentioned in AUTO_SECTION_NAME (.debug_gdb_scripts). */
  953. auto_load_section_scripts (objfile, AUTO_SECTION_NAME);
  954. }
  955. /* This is a new_objfile observer callback to auto-load scripts.
  956. Two flavors of auto-loaded scripts are supported.
  957. 1) based on the path to the objfile
  958. 2) from .debug_gdb_scripts section */
  959. static void
  960. auto_load_new_objfile (struct objfile *objfile)
  961. {
  962. if (!objfile)
  963. {
  964. /* OBJFILE is NULL when loading a new "main" symbol-file. */
  965. clear_section_scripts ();
  966. return;
  967. }
  968. load_auto_scripts_for_objfile (objfile);
  969. }
  970. /* Collect scripts to be printed in a vec. */
  971. struct collect_matching_scripts_data
  972. {
  973. collect_matching_scripts_data (std::vector<loaded_script *> *scripts_p_,
  974. const extension_language_defn *language_)
  975. : scripts_p (scripts_p_), language (language_)
  976. {}
  977. std::vector<loaded_script *> *scripts_p;
  978. const struct extension_language_defn *language;
  979. };
  980. /* Traversal function for htab_traverse.
  981. Collect the entry if it matches the regexp. */
  982. static int
  983. collect_matching_scripts (void **slot, void *info)
  984. {
  985. struct loaded_script *script = (struct loaded_script *) *slot;
  986. struct collect_matching_scripts_data *data
  987. = (struct collect_matching_scripts_data *) info;
  988. if (script->language == data->language && re_exec (script->name))
  989. data->scripts_p->push_back (script);
  990. return 1;
  991. }
  992. /* Print SCRIPT. */
  993. static void
  994. print_script (struct loaded_script *script)
  995. {
  996. struct ui_out *uiout = current_uiout;
  997. ui_out_emit_tuple tuple_emitter (uiout, NULL);
  998. uiout->field_string ("loaded", script->loaded ? "Yes" : "No");
  999. uiout->field_string ("script", script->name);
  1000. uiout->text ("\n");
  1001. /* If the name isn't the full path, print it too. */
  1002. if (script->full_path != NULL
  1003. && strcmp (script->name, script->full_path) != 0)
  1004. {
  1005. uiout->text ("\tfull name: ");
  1006. uiout->field_string ("full_path", script->full_path);
  1007. uiout->text ("\n");
  1008. }
  1009. }
  1010. /* Helper for info_auto_load_scripts to sort the scripts by name. */
  1011. static bool
  1012. sort_scripts_by_name (loaded_script *a, loaded_script *b)
  1013. {
  1014. return FILENAME_CMP (a->name, b->name) < 0;
  1015. }
  1016. /* Special internal GDB value of auto_load_info_scripts's PATTERN identify
  1017. the "info auto-load XXX" command has been executed through the general
  1018. "info auto-load" invocation. Extra newline will be printed if needed. */
  1019. char auto_load_info_scripts_pattern_nl[] = "";
  1020. /* Subroutine of auto_load_info_scripts to simplify it.
  1021. Print SCRIPTS. */
  1022. static void
  1023. print_scripts (const std::vector<loaded_script *> &scripts)
  1024. {
  1025. for (loaded_script *script : scripts)
  1026. print_script (script);
  1027. }
  1028. /* Implementation for "info auto-load gdb-scripts"
  1029. (and "info auto-load python-scripts"). List scripts in LANGUAGE matching
  1030. PATTERN. FROM_TTY is the usual GDB boolean for user interactivity. */
  1031. void
  1032. auto_load_info_scripts (const char *pattern, int from_tty,
  1033. const struct extension_language_defn *language)
  1034. {
  1035. struct ui_out *uiout = current_uiout;
  1036. struct auto_load_pspace_info *pspace_info;
  1037. dont_repeat ();
  1038. pspace_info = get_auto_load_pspace_data (current_program_space);
  1039. if (pattern && *pattern)
  1040. {
  1041. char *re_err = re_comp (pattern);
  1042. if (re_err)
  1043. error (_("Invalid regexp: %s"), re_err);
  1044. }
  1045. else
  1046. {
  1047. re_comp ("");
  1048. }
  1049. /* We need to know the number of rows before we build the table.
  1050. Plus we want to sort the scripts by name.
  1051. So first traverse the hash table collecting the matching scripts. */
  1052. std::vector<loaded_script *> script_files, script_texts;
  1053. if (pspace_info != NULL && pspace_info->loaded_script_files != NULL)
  1054. {
  1055. collect_matching_scripts_data data (&script_files, language);
  1056. /* Pass a pointer to scripts as VEC_safe_push can realloc space. */
  1057. htab_traverse_noresize (pspace_info->loaded_script_files.get (),
  1058. collect_matching_scripts, &data);
  1059. std::sort (script_files.begin (), script_files.end (),
  1060. sort_scripts_by_name);
  1061. }
  1062. if (pspace_info != NULL && pspace_info->loaded_script_texts != NULL)
  1063. {
  1064. collect_matching_scripts_data data (&script_texts, language);
  1065. /* Pass a pointer to scripts as VEC_safe_push can realloc space. */
  1066. htab_traverse_noresize (pspace_info->loaded_script_texts.get (),
  1067. collect_matching_scripts, &data);
  1068. std::sort (script_texts.begin (), script_texts.end (),
  1069. sort_scripts_by_name);
  1070. }
  1071. int nr_scripts = script_files.size () + script_texts.size ();
  1072. /* Table header shifted right by preceding "gdb-scripts: " would not match
  1073. its columns. */
  1074. if (nr_scripts > 0 && pattern == auto_load_info_scripts_pattern_nl)
  1075. uiout->text ("\n");
  1076. {
  1077. ui_out_emit_table table_emitter (uiout, 2, nr_scripts,
  1078. "AutoLoadedScriptsTable");
  1079. uiout->table_header (7, ui_left, "loaded", "Loaded");
  1080. uiout->table_header (70, ui_left, "script", "Script");
  1081. uiout->table_body ();
  1082. print_scripts (script_files);
  1083. print_scripts (script_texts);
  1084. }
  1085. if (nr_scripts == 0)
  1086. {
  1087. if (pattern && *pattern)
  1088. uiout->message ("No auto-load scripts matching %s.\n", pattern);
  1089. else
  1090. uiout->message ("No auto-load scripts.\n");
  1091. }
  1092. }
  1093. /* Wrapper for "info auto-load gdb-scripts". */
  1094. static void
  1095. info_auto_load_gdb_scripts (const char *pattern, int from_tty)
  1096. {
  1097. auto_load_info_scripts (pattern, from_tty, &extension_language_gdb);
  1098. }
  1099. /* Implement 'info auto-load local-gdbinit'. */
  1100. static void
  1101. info_auto_load_local_gdbinit (const char *args, int from_tty)
  1102. {
  1103. if (auto_load_local_gdbinit_pathname == NULL)
  1104. gdb_printf (_("Local .gdbinit file was not found.\n"));
  1105. else if (auto_load_local_gdbinit_loaded)
  1106. gdb_printf (_("Local .gdbinit file \"%ps\" has been loaded.\n"),
  1107. styled_string (file_name_style.style (),
  1108. auto_load_local_gdbinit_pathname));
  1109. else
  1110. gdb_printf (_("Local .gdbinit file \"%ps\" has not been loaded.\n"),
  1111. styled_string (file_name_style.style (),
  1112. auto_load_local_gdbinit_pathname));
  1113. }
  1114. /* Print an "unsupported script" warning if it has not already been printed.
  1115. The script is in language LANGUAGE at offset OFFSET in section SECTION_NAME
  1116. of OBJFILE. */
  1117. static void
  1118. maybe_print_unsupported_script_warning
  1119. (struct auto_load_pspace_info *pspace_info,
  1120. struct objfile *objfile, const struct extension_language_defn *language,
  1121. const char *section_name, unsigned offset)
  1122. {
  1123. if (!pspace_info->unsupported_script_warning_printed)
  1124. {
  1125. warning (_("\
  1126. Unsupported auto-load script at offset %u in section %s\n\
  1127. of file %ps.\n\
  1128. Use `info auto-load %s-scripts [REGEXP]' to list them."),
  1129. offset, section_name,
  1130. styled_string (file_name_style.style (),
  1131. objfile_name (objfile)),
  1132. ext_lang_name (language));
  1133. pspace_info->unsupported_script_warning_printed = true;
  1134. }
  1135. }
  1136. /* Return non-zero if SCRIPT_NOT_FOUND_WARNING_PRINTED of PSPACE_INFO was unset
  1137. before calling this function. Always set SCRIPT_NOT_FOUND_WARNING_PRINTED
  1138. of PSPACE_INFO. */
  1139. static void
  1140. maybe_print_script_not_found_warning
  1141. (struct auto_load_pspace_info *pspace_info,
  1142. struct objfile *objfile, const struct extension_language_defn *language,
  1143. const char *section_name, unsigned offset)
  1144. {
  1145. if (!pspace_info->script_not_found_warning_printed)
  1146. {
  1147. warning (_("\
  1148. Missing auto-load script at offset %u in section %s\n\
  1149. of file %ps.\n\
  1150. Use `info auto-load %s-scripts [REGEXP]' to list them."),
  1151. offset, section_name,
  1152. styled_string (file_name_style.style (),
  1153. objfile_name (objfile)),
  1154. ext_lang_name (language));
  1155. pspace_info->script_not_found_warning_printed = true;
  1156. }
  1157. }
  1158. /* The only valid "set auto-load" argument is off|0|no|disable. */
  1159. static void
  1160. set_auto_load_cmd (const char *args, int from_tty)
  1161. {
  1162. struct cmd_list_element *list;
  1163. size_t length;
  1164. /* See parse_binary_operation in use by the sub-commands. */
  1165. length = args ? strlen (args) : 0;
  1166. while (length > 0 && (args[length - 1] == ' ' || args[length - 1] == '\t'))
  1167. length--;
  1168. if (length == 0 || (strncmp (args, "off", length) != 0
  1169. && strncmp (args, "0", length) != 0
  1170. && strncmp (args, "no", length) != 0
  1171. && strncmp (args, "disable", length) != 0))
  1172. error (_("Valid is only global 'set auto-load no'; "
  1173. "otherwise check the auto-load sub-commands."));
  1174. for (list = *auto_load_set_cmdlist_get (); list != NULL; list = list->next)
  1175. if (list->var->type () == var_boolean)
  1176. {
  1177. gdb_assert (list->type == set_cmd);
  1178. do_set_command (args, from_tty, list);
  1179. }
  1180. }
  1181. /* Initialize "set auto-load " commands prefix and return it. */
  1182. struct cmd_list_element **
  1183. auto_load_set_cmdlist_get (void)
  1184. {
  1185. static struct cmd_list_element *retval;
  1186. if (retval == NULL)
  1187. add_prefix_cmd ("auto-load", class_maintenance, set_auto_load_cmd, _("\
  1188. Auto-loading specific settings.\n\
  1189. Configure various auto-load-specific variables such as\n\
  1190. automatic loading of Python scripts."),
  1191. &retval, 1/*allow-unknown*/, &setlist);
  1192. return &retval;
  1193. }
  1194. /* Initialize "show auto-load " commands prefix and return it. */
  1195. struct cmd_list_element **
  1196. auto_load_show_cmdlist_get (void)
  1197. {
  1198. static struct cmd_list_element *retval;
  1199. if (retval == NULL)
  1200. add_show_prefix_cmd ("auto-load", class_maintenance, _("\
  1201. Show auto-loading specific settings.\n\
  1202. Show configuration of various auto-load-specific variables such as\n\
  1203. automatic loading of Python scripts."),
  1204. &retval, 0/*allow-unknown*/, &showlist);
  1205. return &retval;
  1206. }
  1207. /* Command "info auto-load" displays whether the various auto-load files have
  1208. been loaded. This is reimplementation of cmd_show_list which inserts
  1209. newlines at proper places. */
  1210. static void
  1211. info_auto_load_cmd (const char *args, int from_tty)
  1212. {
  1213. struct cmd_list_element *list;
  1214. struct ui_out *uiout = current_uiout;
  1215. ui_out_emit_tuple tuple_emitter (uiout, "infolist");
  1216. for (list = *auto_load_info_cmdlist_get (); list != NULL; list = list->next)
  1217. {
  1218. ui_out_emit_tuple option_emitter (uiout, "option");
  1219. gdb_assert (!list->is_prefix ());
  1220. gdb_assert (list->type == not_set_cmd);
  1221. uiout->field_string ("name", list->name);
  1222. uiout->text (": ");
  1223. cmd_func (list, auto_load_info_scripts_pattern_nl, from_tty);
  1224. }
  1225. }
  1226. /* Initialize "info auto-load " commands prefix and return it. */
  1227. struct cmd_list_element **
  1228. auto_load_info_cmdlist_get (void)
  1229. {
  1230. static struct cmd_list_element *retval;
  1231. if (retval == NULL)
  1232. add_prefix_cmd ("auto-load", class_info, info_auto_load_cmd, _("\
  1233. Print current status of auto-loaded files.\n\
  1234. Print whether various files like Python scripts or .gdbinit files have been\n\
  1235. found and/or loaded."),
  1236. &retval, 0/*allow-unknown*/, &infolist);
  1237. return &retval;
  1238. }
  1239. /* See auto-load.h. */
  1240. gdb::observers::token auto_load_new_objfile_observer_token;
  1241. void _initialize_auto_load ();
  1242. void
  1243. _initialize_auto_load ()
  1244. {
  1245. struct cmd_list_element *cmd;
  1246. gdb::unique_xmalloc_ptr<char> scripts_directory_help, gdb_name_help,
  1247. python_name_help, guile_name_help;
  1248. const char *suffix;
  1249. gdb::observers::new_objfile.attach (auto_load_new_objfile,
  1250. auto_load_new_objfile_observer_token,
  1251. "auto-load");
  1252. add_setshow_boolean_cmd ("gdb-scripts", class_support,
  1253. &auto_load_gdb_scripts, _("\
  1254. Enable or disable auto-loading of canned sequences of commands scripts."), _("\
  1255. Show whether auto-loading of canned sequences of commands scripts is enabled."),
  1256. _("\
  1257. If enabled, canned sequences of commands are loaded when the debugger reads\n\
  1258. an executable or shared library.\n\
  1259. This option has security implications for untrusted inferiors."),
  1260. NULL, show_auto_load_gdb_scripts,
  1261. auto_load_set_cmdlist_get (),
  1262. auto_load_show_cmdlist_get ());
  1263. add_cmd ("gdb-scripts", class_info, info_auto_load_gdb_scripts,
  1264. _("Print the list of automatically loaded sequences of commands.\n\
  1265. Usage: info auto-load gdb-scripts [REGEXP]"),
  1266. auto_load_info_cmdlist_get ());
  1267. add_setshow_boolean_cmd ("local-gdbinit", class_support,
  1268. &auto_load_local_gdbinit, _("\
  1269. Enable or disable auto-loading of .gdbinit script in current directory."), _("\
  1270. Show whether auto-loading .gdbinit script in current directory is enabled."),
  1271. _("\
  1272. If enabled, canned sequences of commands are loaded when debugger starts\n\
  1273. from .gdbinit file in current directory. Such files are deprecated,\n\
  1274. use a script associated with inferior executable file instead.\n\
  1275. This option has security implications for untrusted inferiors."),
  1276. NULL, show_auto_load_local_gdbinit,
  1277. auto_load_set_cmdlist_get (),
  1278. auto_load_show_cmdlist_get ());
  1279. add_cmd ("local-gdbinit", class_info, info_auto_load_local_gdbinit,
  1280. _("Print whether current directory .gdbinit file has been loaded.\n\
  1281. Usage: info auto-load local-gdbinit"),
  1282. auto_load_info_cmdlist_get ());
  1283. suffix = ext_lang_auto_load_suffix (get_ext_lang_defn (EXT_LANG_GDB));
  1284. gdb_name_help
  1285. = xstrprintf (_("\
  1286. GDB scripts: OBJFILE%s\n"),
  1287. suffix);
  1288. python_name_help = NULL;
  1289. #ifdef HAVE_PYTHON
  1290. suffix = ext_lang_auto_load_suffix (get_ext_lang_defn (EXT_LANG_PYTHON));
  1291. python_name_help
  1292. = xstrprintf (_("\
  1293. Python scripts: OBJFILE%s\n"),
  1294. suffix);
  1295. #endif
  1296. guile_name_help = NULL;
  1297. #ifdef HAVE_GUILE
  1298. suffix = ext_lang_auto_load_suffix (get_ext_lang_defn (EXT_LANG_GUILE));
  1299. guile_name_help
  1300. = xstrprintf (_("\
  1301. Guile scripts: OBJFILE%s\n"),
  1302. suffix);
  1303. #endif
  1304. scripts_directory_help
  1305. = xstrprintf (_("\
  1306. Automatically loaded scripts are located in one of the directories listed\n\
  1307. by this option.\n\
  1308. \n\
  1309. Script names:\n\
  1310. %s%s%s\
  1311. \n\
  1312. This option is ignored for the kinds of scripts \
  1313. having 'set auto-load ... off'.\n\
  1314. Directories listed here need to be present also \
  1315. in the 'set auto-load safe-path'\n\
  1316. option."),
  1317. gdb_name_help.get (),
  1318. python_name_help.get () ? python_name_help.get () : "",
  1319. guile_name_help.get () ? guile_name_help.get () : "");
  1320. add_setshow_optional_filename_cmd ("scripts-directory", class_support,
  1321. &auto_load_dir, _("\
  1322. Set the list of directories from which to load auto-loaded scripts."), _("\
  1323. Show the list of directories from which to load auto-loaded scripts."),
  1324. scripts_directory_help.get (),
  1325. set_auto_load_dir, show_auto_load_dir,
  1326. auto_load_set_cmdlist_get (),
  1327. auto_load_show_cmdlist_get ());
  1328. auto_load_safe_path_vec_update ();
  1329. add_setshow_optional_filename_cmd ("safe-path", class_support,
  1330. &auto_load_safe_path, _("\
  1331. Set the list of files and directories that are safe for auto-loading."), _("\
  1332. Show the list of files and directories that are safe for auto-loading."), _("\
  1333. Various files loaded automatically for the 'set auto-load ...' options must\n\
  1334. be located in one of the directories listed by this option. Warning will be\n\
  1335. printed and file will not be used otherwise.\n\
  1336. You can mix both directory and filename entries.\n\
  1337. Setting this parameter to an empty list resets it to its default value.\n\
  1338. Setting this parameter to '/' (without the quotes) allows any file\n\
  1339. for the 'set auto-load ...' options. Each path entry can be also shell\n\
  1340. wildcard pattern; '*' does not match directory separator.\n\
  1341. This option is ignored for the kinds of files having 'set auto-load ... off'.\n\
  1342. This option has security implications for untrusted inferiors."),
  1343. set_auto_load_safe_path,
  1344. show_auto_load_safe_path,
  1345. auto_load_set_cmdlist_get (),
  1346. auto_load_show_cmdlist_get ());
  1347. gdb::observers::gdb_datadir_changed.attach (auto_load_gdb_datadir_changed,
  1348. "auto-load");
  1349. cmd = add_cmd ("add-auto-load-safe-path", class_support,
  1350. add_auto_load_safe_path,
  1351. _("Add entries to the list of directories from which it is safe "
  1352. "to auto-load files.\n\
  1353. See the commands 'set auto-load safe-path' and 'show auto-load safe-path' to\n\
  1354. access the current full list setting."),
  1355. &cmdlist);
  1356. set_cmd_completer (cmd, filename_completer);
  1357. cmd = add_cmd ("add-auto-load-scripts-directory", class_support,
  1358. add_auto_load_dir,
  1359. _("Add entries to the list of directories from which to load "
  1360. "auto-loaded scripts.\n\
  1361. See the commands 'set auto-load scripts-directory' and\n\
  1362. 'show auto-load scripts-directory' to access the current full list setting."),
  1363. &cmdlist);
  1364. set_cmd_completer (cmd, filename_completer);
  1365. add_setshow_boolean_cmd ("auto-load", class_maintenance,
  1366. &debug_auto_load, _("\
  1367. Set auto-load verifications debugging."), _("\
  1368. Show auto-load verifications debugging."), _("\
  1369. When non-zero, debugging output for files of 'set auto-load ...'\n\
  1370. is displayed."),
  1371. NULL, show_debug_auto_load,
  1372. &setdebuglist, &showdebuglist);
  1373. }