annotate.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. /* Annotation routines for GDB.
  2. Copyright (C) 1986-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 "annotate.h"
  16. #include "value.h"
  17. #include "target.h"
  18. #include "gdbtypes.h"
  19. #include "breakpoint.h"
  20. #include "observable.h"
  21. #include "inferior.h"
  22. #include "infrun.h"
  23. #include "top.h"
  24. #include "source.h"
  25. #include "objfiles.h"
  26. #include "source-cache.h"
  27. /* Prototypes for local functions. */
  28. static void print_value_flags (struct type *);
  29. static void breakpoint_changed (struct breakpoint *b);
  30. void (*deprecated_annotate_signalled_hook) (void);
  31. void (*deprecated_annotate_signal_hook) (void);
  32. /* Booleans indicating whether we've emitted certain notifications.
  33. Used to suppress useless repeated notifications until the next time
  34. we're ready to accept more commands. Reset whenever a prompt is
  35. displayed. */
  36. static int frames_invalid_emitted;
  37. static int breakpoints_invalid_emitted;
  38. static void
  39. print_value_flags (struct type *t)
  40. {
  41. if (can_dereference (t))
  42. printf_unfiltered (("*"));
  43. else
  44. printf_unfiltered (("-"));
  45. }
  46. static void
  47. annotate_breakpoints_invalid (void)
  48. {
  49. if (annotation_level == 2
  50. && (!breakpoints_invalid_emitted
  51. || current_ui->prompt_state != PROMPT_BLOCKED))
  52. {
  53. target_terminal::scoped_restore_terminal_state term_state;
  54. target_terminal::ours_for_output ();
  55. printf_unfiltered (("\n\032\032breakpoints-invalid\n"));
  56. breakpoints_invalid_emitted = 1;
  57. }
  58. }
  59. void
  60. annotate_breakpoint (int num)
  61. {
  62. if (annotation_level > 1)
  63. printf_unfiltered (("\n\032\032breakpoint %d\n"), num);
  64. }
  65. void
  66. annotate_catchpoint (int num)
  67. {
  68. if (annotation_level > 1)
  69. printf_unfiltered (("\n\032\032catchpoint %d\n"), num);
  70. }
  71. void
  72. annotate_watchpoint (int num)
  73. {
  74. if (annotation_level > 1)
  75. printf_unfiltered (("\n\032\032watchpoint %d\n"), num);
  76. }
  77. void
  78. annotate_starting (void)
  79. {
  80. if (annotation_level > 1)
  81. printf_unfiltered (("\n\032\032starting\n"));
  82. }
  83. void
  84. annotate_stopped (void)
  85. {
  86. if (annotation_level > 1)
  87. printf_unfiltered (("\n\032\032stopped\n"));
  88. }
  89. void
  90. annotate_exited (int exitstatus)
  91. {
  92. if (annotation_level > 1)
  93. printf_unfiltered (("\n\032\032exited %d\n"), exitstatus);
  94. }
  95. void
  96. annotate_signalled (void)
  97. {
  98. if (deprecated_annotate_signalled_hook)
  99. deprecated_annotate_signalled_hook ();
  100. if (annotation_level > 1)
  101. printf_unfiltered (("\n\032\032signalled\n"));
  102. }
  103. void
  104. annotate_signal_name (void)
  105. {
  106. if (annotation_level == 2)
  107. printf_unfiltered (("\n\032\032signal-name\n"));
  108. }
  109. void
  110. annotate_signal_name_end (void)
  111. {
  112. if (annotation_level == 2)
  113. printf_unfiltered (("\n\032\032signal-name-end\n"));
  114. }
  115. void
  116. annotate_signal_string (void)
  117. {
  118. if (annotation_level == 2)
  119. printf_unfiltered (("\n\032\032signal-string\n"));
  120. }
  121. void
  122. annotate_signal_string_end (void)
  123. {
  124. if (annotation_level == 2)
  125. printf_unfiltered (("\n\032\032signal-string-end\n"));
  126. }
  127. void
  128. annotate_signal (void)
  129. {
  130. if (deprecated_annotate_signal_hook)
  131. deprecated_annotate_signal_hook ();
  132. if (annotation_level > 1)
  133. printf_unfiltered (("\n\032\032signal\n"));
  134. }
  135. void
  136. annotate_breakpoints_headers (void)
  137. {
  138. if (annotation_level == 2)
  139. printf_unfiltered (("\n\032\032breakpoints-headers\n"));
  140. }
  141. void
  142. annotate_field (int num)
  143. {
  144. if (annotation_level == 2)
  145. printf_unfiltered (("\n\032\032field %d\n"), num);
  146. }
  147. void
  148. annotate_breakpoints_table (void)
  149. {
  150. if (annotation_level == 2)
  151. printf_unfiltered (("\n\032\032breakpoints-table\n"));
  152. }
  153. void
  154. annotate_record (void)
  155. {
  156. if (annotation_level == 2)
  157. printf_unfiltered (("\n\032\032record\n"));
  158. }
  159. void
  160. annotate_breakpoints_table_end (void)
  161. {
  162. if (annotation_level == 2)
  163. printf_unfiltered (("\n\032\032breakpoints-table-end\n"));
  164. }
  165. void
  166. annotate_frames_invalid (void)
  167. {
  168. if (annotation_level == 2
  169. && (!frames_invalid_emitted
  170. || current_ui->prompt_state != PROMPT_BLOCKED))
  171. {
  172. target_terminal::scoped_restore_terminal_state term_state;
  173. target_terminal::ours_for_output ();
  174. printf_unfiltered (("\n\032\032frames-invalid\n"));
  175. frames_invalid_emitted = 1;
  176. }
  177. }
  178. void
  179. annotate_new_thread (void)
  180. {
  181. if (annotation_level > 1)
  182. {
  183. printf_unfiltered (("\n\032\032new-thread\n"));
  184. }
  185. }
  186. void
  187. annotate_thread_changed (void)
  188. {
  189. if (annotation_level > 1)
  190. {
  191. printf_unfiltered (("\n\032\032thread-changed\n"));
  192. }
  193. }
  194. /* Emit notification on thread exit. */
  195. static void
  196. annotate_thread_exited (struct thread_info *t, int silent)
  197. {
  198. if (annotation_level > 1)
  199. {
  200. printf_unfiltered (("\n\032\032thread-exited,"
  201. "id=\"%d\",group-id=\"i%d\"\n"),
  202. t->global_num, t->inf->num);
  203. }
  204. }
  205. void
  206. annotate_field_begin (struct type *type)
  207. {
  208. if (annotation_level == 2)
  209. {
  210. printf_unfiltered (("\n\032\032field-begin "));
  211. print_value_flags (type);
  212. printf_unfiltered (("\n"));
  213. }
  214. }
  215. void
  216. annotate_field_name_end (void)
  217. {
  218. if (annotation_level == 2)
  219. printf_unfiltered (("\n\032\032field-name-end\n"));
  220. }
  221. void
  222. annotate_field_value (void)
  223. {
  224. if (annotation_level == 2)
  225. printf_unfiltered (("\n\032\032field-value\n"));
  226. }
  227. void
  228. annotate_field_end (void)
  229. {
  230. if (annotation_level == 2)
  231. printf_unfiltered (("\n\032\032field-end\n"));
  232. }
  233. void
  234. annotate_quit (void)
  235. {
  236. if (annotation_level > 1)
  237. printf_unfiltered (("\n\032\032quit\n"));
  238. }
  239. void
  240. annotate_error (void)
  241. {
  242. if (annotation_level > 1)
  243. printf_unfiltered (("\n\032\032error\n"));
  244. }
  245. void
  246. annotate_error_begin (void)
  247. {
  248. if (annotation_level > 1)
  249. gdb_printf (gdb_stderr, "\n\032\032error-begin\n");
  250. }
  251. void
  252. annotate_value_history_begin (int histindex, struct type *type)
  253. {
  254. if (annotation_level == 2)
  255. {
  256. printf_unfiltered (("\n\032\032value-history-begin %d "), histindex);
  257. print_value_flags (type);
  258. printf_unfiltered (("\n"));
  259. }
  260. }
  261. void
  262. annotate_value_begin (struct type *type)
  263. {
  264. if (annotation_level == 2)
  265. {
  266. printf_unfiltered (("\n\032\032value-begin "));
  267. print_value_flags (type);
  268. printf_unfiltered (("\n"));
  269. }
  270. }
  271. void
  272. annotate_value_history_value (void)
  273. {
  274. if (annotation_level == 2)
  275. printf_unfiltered (("\n\032\032value-history-value\n"));
  276. }
  277. void
  278. annotate_value_history_end (void)
  279. {
  280. if (annotation_level == 2)
  281. printf_unfiltered (("\n\032\032value-history-end\n"));
  282. }
  283. void
  284. annotate_value_end (void)
  285. {
  286. if (annotation_level == 2)
  287. printf_unfiltered (("\n\032\032value-end\n"));
  288. }
  289. void
  290. annotate_display_begin (void)
  291. {
  292. if (annotation_level == 2)
  293. printf_unfiltered (("\n\032\032display-begin\n"));
  294. }
  295. void
  296. annotate_display_number_end (void)
  297. {
  298. if (annotation_level == 2)
  299. printf_unfiltered (("\n\032\032display-number-end\n"));
  300. }
  301. void
  302. annotate_display_format (void)
  303. {
  304. if (annotation_level == 2)
  305. printf_unfiltered (("\n\032\032display-format\n"));
  306. }
  307. void
  308. annotate_display_expression (void)
  309. {
  310. if (annotation_level == 2)
  311. printf_unfiltered (("\n\032\032display-expression\n"));
  312. }
  313. void
  314. annotate_display_expression_end (void)
  315. {
  316. if (annotation_level == 2)
  317. printf_unfiltered (("\n\032\032display-expression-end\n"));
  318. }
  319. void
  320. annotate_display_value (void)
  321. {
  322. if (annotation_level == 2)
  323. printf_unfiltered (("\n\032\032display-value\n"));
  324. }
  325. void
  326. annotate_display_end (void)
  327. {
  328. if (annotation_level == 2)
  329. printf_unfiltered (("\n\032\032display-end\n"));
  330. }
  331. void
  332. annotate_arg_begin (void)
  333. {
  334. if (annotation_level == 2)
  335. printf_unfiltered (("\n\032\032arg-begin\n"));
  336. }
  337. void
  338. annotate_arg_name_end (void)
  339. {
  340. if (annotation_level == 2)
  341. printf_unfiltered (("\n\032\032arg-name-end\n"));
  342. }
  343. void
  344. annotate_arg_value (struct type *type)
  345. {
  346. if (annotation_level == 2)
  347. {
  348. printf_unfiltered (("\n\032\032arg-value "));
  349. print_value_flags (type);
  350. printf_unfiltered (("\n"));
  351. }
  352. }
  353. void
  354. annotate_arg_end (void)
  355. {
  356. if (annotation_level == 2)
  357. printf_unfiltered (("\n\032\032arg-end\n"));
  358. }
  359. static void
  360. annotate_source (const char *filename, int line, int character, int mid,
  361. struct gdbarch *gdbarch, CORE_ADDR pc)
  362. {
  363. if (annotation_level > 1)
  364. printf_unfiltered (("\n\032\032source "));
  365. else
  366. printf_unfiltered (("\032\032"));
  367. printf_unfiltered (("%s:%d:%d:%s:%s\n"), filename, line, character,
  368. mid ? "middle" : "beg", paddress (gdbarch, pc));
  369. }
  370. /* See annotate.h. */
  371. bool
  372. annotate_source_line (struct symtab *s, int line, int mid_statement,
  373. CORE_ADDR pc)
  374. {
  375. if (annotation_level > 0)
  376. {
  377. const std::vector<off_t> *offsets;
  378. if (!g_source_cache.get_line_charpos (s, &offsets))
  379. return false;
  380. if (line > offsets->size ())
  381. return false;
  382. annotate_source (s->fullname, line, (int) (*offsets)[line - 1],
  383. mid_statement, s->compunit ()->objfile ()->arch (),
  384. pc);
  385. /* Update the current symtab and line. */
  386. symtab_and_line sal;
  387. sal.pspace = s->compunit ()->objfile ()->pspace;
  388. sal.symtab = s;
  389. sal.line = line;
  390. set_current_source_symtab_and_line (sal);
  391. return true;
  392. }
  393. return false;
  394. }
  395. void
  396. annotate_frame_begin (int level, struct gdbarch *gdbarch, CORE_ADDR pc)
  397. {
  398. if (annotation_level > 1)
  399. printf_unfiltered (("\n\032\032frame-begin %d %s\n"),
  400. level, paddress (gdbarch, pc));
  401. }
  402. void
  403. annotate_function_call (void)
  404. {
  405. if (annotation_level == 2)
  406. printf_unfiltered (("\n\032\032function-call\n"));
  407. }
  408. void
  409. annotate_signal_handler_caller (void)
  410. {
  411. if (annotation_level == 2)
  412. printf_unfiltered (("\n\032\032signal-handler-caller\n"));
  413. }
  414. void
  415. annotate_frame_address (void)
  416. {
  417. if (annotation_level == 2)
  418. printf_unfiltered (("\n\032\032frame-address\n"));
  419. }
  420. void
  421. annotate_frame_address_end (void)
  422. {
  423. if (annotation_level == 2)
  424. printf_unfiltered (("\n\032\032frame-address-end\n"));
  425. }
  426. void
  427. annotate_frame_function_name (void)
  428. {
  429. if (annotation_level == 2)
  430. printf_unfiltered (("\n\032\032frame-function-name\n"));
  431. }
  432. void
  433. annotate_frame_args (void)
  434. {
  435. if (annotation_level == 2)
  436. printf_unfiltered (("\n\032\032frame-args\n"));
  437. }
  438. void
  439. annotate_frame_source_begin (void)
  440. {
  441. if (annotation_level == 2)
  442. printf_unfiltered (("\n\032\032frame-source-begin\n"));
  443. }
  444. void
  445. annotate_frame_source_file (void)
  446. {
  447. if (annotation_level == 2)
  448. printf_unfiltered (("\n\032\032frame-source-file\n"));
  449. }
  450. void
  451. annotate_frame_source_file_end (void)
  452. {
  453. if (annotation_level == 2)
  454. printf_unfiltered (("\n\032\032frame-source-file-end\n"));
  455. }
  456. void
  457. annotate_frame_source_line (void)
  458. {
  459. if (annotation_level == 2)
  460. printf_unfiltered (("\n\032\032frame-source-line\n"));
  461. }
  462. void
  463. annotate_frame_source_end (void)
  464. {
  465. if (annotation_level == 2)
  466. printf_unfiltered (("\n\032\032frame-source-end\n"));
  467. }
  468. void
  469. annotate_frame_where (void)
  470. {
  471. if (annotation_level == 2)
  472. printf_unfiltered (("\n\032\032frame-where\n"));
  473. }
  474. void
  475. annotate_frame_end (void)
  476. {
  477. if (annotation_level == 2)
  478. printf_unfiltered (("\n\032\032frame-end\n"));
  479. }
  480. void
  481. annotate_array_section_begin (int idx, struct type *elttype)
  482. {
  483. if (annotation_level == 2)
  484. {
  485. printf_unfiltered (("\n\032\032array-section-begin %d "), idx);
  486. print_value_flags (elttype);
  487. printf_unfiltered (("\n"));
  488. }
  489. }
  490. void
  491. annotate_elt_rep (unsigned int repcount)
  492. {
  493. if (annotation_level == 2)
  494. printf_unfiltered (("\n\032\032elt-rep %u\n"), repcount);
  495. }
  496. void
  497. annotate_elt_rep_end (void)
  498. {
  499. if (annotation_level == 2)
  500. printf_unfiltered (("\n\032\032elt-rep-end\n"));
  501. }
  502. void
  503. annotate_elt (void)
  504. {
  505. if (annotation_level == 2)
  506. printf_unfiltered (("\n\032\032elt\n"));
  507. }
  508. void
  509. annotate_array_section_end (void)
  510. {
  511. if (annotation_level == 2)
  512. printf_unfiltered (("\n\032\032array-section-end\n"));
  513. }
  514. /* Called when GDB is about to display the prompt. Used to reset
  515. annotation suppression whenever we're ready to accept new
  516. frontend/user commands. */
  517. void
  518. annotate_display_prompt (void)
  519. {
  520. frames_invalid_emitted = 0;
  521. breakpoints_invalid_emitted = 0;
  522. }
  523. static void
  524. breakpoint_changed (struct breakpoint *b)
  525. {
  526. if (b->number <= 0)
  527. return;
  528. annotate_breakpoints_invalid ();
  529. }
  530. void _initialize_annotate ();
  531. void
  532. _initialize_annotate ()
  533. {
  534. gdb::observers::breakpoint_created.attach (breakpoint_changed, "annotate");
  535. gdb::observers::breakpoint_deleted.attach (breakpoint_changed, "annotate");
  536. gdb::observers::breakpoint_modified.attach (breakpoint_changed, "annotate");
  537. gdb::observers::thread_exit.attach (annotate_thread_exited, "annotate");
  538. }