disasm.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280
  1. /* Disassemble support for GDB.
  2. Copyright (C) 2000-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 "arch-utils.h"
  16. #include "target.h"
  17. #include "value.h"
  18. #include "ui-out.h"
  19. #include "disasm.h"
  20. #include "gdbcore.h"
  21. #include "gdbcmd.h"
  22. #include "dis-asm.h"
  23. #include "source.h"
  24. #include "safe-ctype.h"
  25. #include <algorithm>
  26. #include "gdbsupport/gdb_optional.h"
  27. #include "valprint.h"
  28. #include "cli/cli-style.h"
  29. /* Disassemble functions.
  30. FIXME: We should get rid of all the duplicate code in gdb that does
  31. the same thing: disassemble_command() and the gdbtk variation. */
  32. /* This variable is used to hold the prospective disassembler_options value
  33. which is set by the "set disassembler_options" command. */
  34. static std::string prospective_options;
  35. /* This structure is used to store line number information for the
  36. deprecated /m option.
  37. We need a different sort of line table from the normal one cuz we can't
  38. depend upon implicit line-end pc's for lines to do the
  39. reordering in this function. */
  40. struct deprecated_dis_line_entry
  41. {
  42. int line;
  43. CORE_ADDR start_pc;
  44. CORE_ADDR end_pc;
  45. };
  46. /* This Structure is used to store line number information.
  47. We need a different sort of line table from the normal one cuz we can't
  48. depend upon implicit line-end pc's for lines to do the
  49. reordering in this function. */
  50. struct dis_line_entry
  51. {
  52. struct symtab *symtab;
  53. int line;
  54. };
  55. /* Hash function for dis_line_entry. */
  56. static hashval_t
  57. hash_dis_line_entry (const void *item)
  58. {
  59. const struct dis_line_entry *dle = (const struct dis_line_entry *) item;
  60. return htab_hash_pointer (dle->symtab) + dle->line;
  61. }
  62. /* Equal function for dis_line_entry. */
  63. static int
  64. eq_dis_line_entry (const void *item_lhs, const void *item_rhs)
  65. {
  66. const struct dis_line_entry *lhs = (const struct dis_line_entry *) item_lhs;
  67. const struct dis_line_entry *rhs = (const struct dis_line_entry *) item_rhs;
  68. return (lhs->symtab == rhs->symtab
  69. && lhs->line == rhs->line);
  70. }
  71. /* Create the table to manage lines for mixed source/disassembly. */
  72. static htab_t
  73. allocate_dis_line_table (void)
  74. {
  75. return htab_create_alloc (41,
  76. hash_dis_line_entry, eq_dis_line_entry,
  77. xfree, xcalloc, xfree);
  78. }
  79. /* Add a new dis_line_entry containing SYMTAB and LINE to TABLE. */
  80. static void
  81. add_dis_line_entry (htab_t table, struct symtab *symtab, int line)
  82. {
  83. void **slot;
  84. struct dis_line_entry dle, *dlep;
  85. dle.symtab = symtab;
  86. dle.line = line;
  87. slot = htab_find_slot (table, &dle, INSERT);
  88. if (*slot == NULL)
  89. {
  90. dlep = XNEW (struct dis_line_entry);
  91. dlep->symtab = symtab;
  92. dlep->line = line;
  93. *slot = dlep;
  94. }
  95. }
  96. /* Return non-zero if SYMTAB, LINE are in TABLE. */
  97. static int
  98. line_has_code_p (htab_t table, struct symtab *symtab, int line)
  99. {
  100. struct dis_line_entry dle;
  101. dle.symtab = symtab;
  102. dle.line = line;
  103. return htab_find (table, &dle) != NULL;
  104. }
  105. /* Wrapper of target_read_code. */
  106. int
  107. gdb_disassembler::dis_asm_read_memory (bfd_vma memaddr, gdb_byte *myaddr,
  108. unsigned int len,
  109. struct disassemble_info *info)
  110. {
  111. return target_read_code (memaddr, myaddr, len);
  112. }
  113. /* Wrapper of memory_error. */
  114. void
  115. gdb_disassembler::dis_asm_memory_error (int err, bfd_vma memaddr,
  116. struct disassemble_info *info)
  117. {
  118. gdb_disassembler *self
  119. = static_cast<gdb_disassembler *>(info->application_data);
  120. self->m_err_memaddr.emplace (memaddr);
  121. }
  122. /* Wrapper of print_address. */
  123. void
  124. gdb_disassembler::dis_asm_print_address (bfd_vma addr,
  125. struct disassemble_info *info)
  126. {
  127. gdb_disassembler *self
  128. = static_cast<gdb_disassembler *>(info->application_data);
  129. print_address (self->arch (), addr, self->stream ());
  130. }
  131. /* Format disassembler output to STREAM. */
  132. int
  133. gdb_disassembler::dis_asm_fprintf (void *stream, const char *format, ...)
  134. {
  135. va_list args;
  136. va_start (args, format);
  137. gdb_vprintf ((struct ui_file *) stream, format, args);
  138. va_end (args);
  139. /* Something non -ve. */
  140. return 0;
  141. }
  142. /* See disasm.h. */
  143. int
  144. gdb_disassembler::dis_asm_styled_fprintf (void *stream,
  145. enum disassembler_style style,
  146. const char *format, ...)
  147. {
  148. va_list args;
  149. va_start (args, format);
  150. gdb_vprintf ((struct ui_file *) stream, format, args);
  151. va_end (args);
  152. /* Something non -ve. */
  153. return 0;
  154. }
  155. static bool
  156. line_is_less_than (const deprecated_dis_line_entry &mle1,
  157. const deprecated_dis_line_entry &mle2)
  158. {
  159. bool val;
  160. /* End of sequence markers have a line number of 0 but don't want to
  161. be sorted to the head of the list, instead sort by PC. */
  162. if (mle1.line == 0 || mle2.line == 0)
  163. {
  164. if (mle1.start_pc != mle2.start_pc)
  165. val = mle1.start_pc < mle2.start_pc;
  166. else
  167. val = mle1.line < mle2.line;
  168. }
  169. else
  170. {
  171. if (mle1.line != mle2.line)
  172. val = mle1.line < mle2.line;
  173. else
  174. val = mle1.start_pc < mle2.start_pc;
  175. }
  176. return val;
  177. }
  178. /* See disasm.h. */
  179. int
  180. gdb_pretty_print_disassembler::pretty_print_insn (const struct disasm_insn *insn,
  181. gdb_disassembly_flags flags)
  182. {
  183. /* parts of the symbolic representation of the address */
  184. int unmapped;
  185. int offset;
  186. int line;
  187. int size;
  188. CORE_ADDR pc;
  189. struct gdbarch *gdbarch = arch ();
  190. {
  191. ui_out_emit_tuple tuple_emitter (m_uiout, NULL);
  192. pc = insn->addr;
  193. if (insn->number != 0)
  194. {
  195. m_uiout->field_unsigned ("insn-number", insn->number);
  196. m_uiout->text ("\t");
  197. }
  198. if ((flags & DISASSEMBLY_SPECULATIVE) != 0)
  199. {
  200. if (insn->is_speculative)
  201. {
  202. m_uiout->field_string ("is-speculative", "?");
  203. /* The speculative execution indication overwrites the first
  204. character of the PC prefix.
  205. We assume a PC prefix length of 3 characters. */
  206. if ((flags & DISASSEMBLY_OMIT_PC) == 0)
  207. m_uiout->text (pc_prefix (pc) + 1);
  208. else
  209. m_uiout->text (" ");
  210. }
  211. else if ((flags & DISASSEMBLY_OMIT_PC) == 0)
  212. m_uiout->text (pc_prefix (pc));
  213. else
  214. m_uiout->text (" ");
  215. }
  216. else if ((flags & DISASSEMBLY_OMIT_PC) == 0)
  217. m_uiout->text (pc_prefix (pc));
  218. m_uiout->field_core_addr ("address", gdbarch, pc);
  219. std::string name, filename;
  220. bool omit_fname = ((flags & DISASSEMBLY_OMIT_FNAME) != 0);
  221. if (!build_address_symbolic (gdbarch, pc, false, omit_fname, &name,
  222. &offset, &filename, &line, &unmapped))
  223. {
  224. /* We don't care now about line, filename and unmapped. But we might in
  225. the future. */
  226. m_uiout->text (" <");
  227. if (!omit_fname)
  228. m_uiout->field_string ("func-name", name,
  229. function_name_style.style ());
  230. /* For negative offsets, avoid displaying them as +-N; the sign of
  231. the offset takes the place of the "+" here. */
  232. if (offset >= 0)
  233. m_uiout->text ("+");
  234. m_uiout->field_signed ("offset", offset);
  235. m_uiout->text (">:\t");
  236. }
  237. else
  238. m_uiout->text (":\t");
  239. /* Clear the buffer into which we will disassemble the instruction. */
  240. m_insn_stb.clear ();
  241. /* A helper function to write the M_INSN_STB buffer, followed by a
  242. newline. This can be called in a couple of situations. */
  243. auto write_out_insn_buffer = [&] ()
  244. {
  245. m_uiout->field_stream ("inst", m_insn_stb);
  246. m_uiout->text ("\n");
  247. };
  248. try
  249. {
  250. /* Now we can disassemble the instruction. If the disassembler
  251. returns a negative value this indicates an error and is handled
  252. within the print_insn call, resulting in an exception being
  253. thrown. Returning zero makes no sense, as this indicates we
  254. disassembled something successfully, but it was something of no
  255. size? */
  256. size = m_di.print_insn (pc);
  257. gdb_assert (size > 0);
  258. }
  259. catch (const gdb_exception &ex)
  260. {
  261. /* An exception was thrown while disassembling the instruction.
  262. However, the disassembler might still have written something
  263. out, so ensure that we flush the instruction buffer before
  264. rethrowing the exception. We can't perform this write from an
  265. object destructor as the write itself might throw an exception
  266. if the pager kicks in, and the user selects quit. */
  267. write_out_insn_buffer ();
  268. throw ex;
  269. }
  270. if (flags & DISASSEMBLY_RAW_INSN)
  271. {
  272. CORE_ADDR end_pc;
  273. bfd_byte data;
  274. const char *spacer = "";
  275. /* Build the opcodes using a temporary stream so we can
  276. write them out in a single go for the MI. */
  277. m_opcode_stb.clear ();
  278. end_pc = pc + size;
  279. for (;pc < end_pc; ++pc)
  280. {
  281. read_code (pc, &data, 1);
  282. m_opcode_stb.printf ("%s%02x", spacer, (unsigned) data);
  283. spacer = " ";
  284. }
  285. m_uiout->field_stream ("opcodes", m_opcode_stb);
  286. m_uiout->text ("\t");
  287. }
  288. /* Disassembly was a success, write out the instruction buffer. */
  289. write_out_insn_buffer ();
  290. }
  291. return size;
  292. }
  293. static int
  294. dump_insns (struct gdbarch *gdbarch,
  295. struct ui_out *uiout, CORE_ADDR low, CORE_ADDR high,
  296. int how_many, gdb_disassembly_flags flags, CORE_ADDR *end_pc)
  297. {
  298. struct disasm_insn insn;
  299. int num_displayed = 0;
  300. memset (&insn, 0, sizeof (insn));
  301. insn.addr = low;
  302. gdb_pretty_print_disassembler disasm (gdbarch, uiout);
  303. while (insn.addr < high && (how_many < 0 || num_displayed < how_many))
  304. {
  305. int size;
  306. size = disasm.pretty_print_insn (&insn, flags);
  307. if (size <= 0)
  308. break;
  309. ++num_displayed;
  310. insn.addr += size;
  311. /* Allow user to bail out with ^C. */
  312. QUIT;
  313. }
  314. if (end_pc != NULL)
  315. *end_pc = insn.addr;
  316. return num_displayed;
  317. }
  318. /* The idea here is to present a source-O-centric view of a
  319. function to the user. This means that things are presented
  320. in source order, with (possibly) out of order assembly
  321. immediately following.
  322. N.B. This view is deprecated. */
  323. static void
  324. do_mixed_source_and_assembly_deprecated
  325. (struct gdbarch *gdbarch, struct ui_out *uiout,
  326. struct symtab *symtab,
  327. CORE_ADDR low, CORE_ADDR high,
  328. int how_many, gdb_disassembly_flags flags)
  329. {
  330. int newlines = 0;
  331. int nlines;
  332. struct linetable_entry *le;
  333. struct deprecated_dis_line_entry *mle;
  334. struct symtab_and_line sal;
  335. int i;
  336. int out_of_order = 0;
  337. int next_line = 0;
  338. int num_displayed = 0;
  339. print_source_lines_flags psl_flags = 0;
  340. gdb_assert (symtab != nullptr && symtab->linetable () != nullptr);
  341. nlines = symtab->linetable ()->nitems;
  342. le = symtab->linetable ()->item;
  343. if (flags & DISASSEMBLY_FILENAME)
  344. psl_flags |= PRINT_SOURCE_LINES_FILENAME;
  345. mle = (struct deprecated_dis_line_entry *)
  346. alloca (nlines * sizeof (struct deprecated_dis_line_entry));
  347. /* Copy linetable entries for this function into our data
  348. structure, creating end_pc's and setting out_of_order as
  349. appropriate. */
  350. /* First, skip all the preceding functions. */
  351. for (i = 0; i < nlines - 1 && le[i].pc < low; i++);
  352. /* Now, copy all entries before the end of this function. */
  353. for (; i < nlines - 1 && le[i].pc < high; i++)
  354. {
  355. if (le[i].line == le[i + 1].line && le[i].pc == le[i + 1].pc)
  356. continue; /* Ignore duplicates. */
  357. /* Skip any end-of-function markers. */
  358. if (le[i].line == 0)
  359. continue;
  360. mle[newlines].line = le[i].line;
  361. if (le[i].line > le[i + 1].line)
  362. out_of_order = 1;
  363. mle[newlines].start_pc = le[i].pc;
  364. mle[newlines].end_pc = le[i + 1].pc;
  365. newlines++;
  366. }
  367. /* If we're on the last line, and it's part of the function,
  368. then we need to get the end pc in a special way. */
  369. if (i == nlines - 1 && le[i].pc < high)
  370. {
  371. mle[newlines].line = le[i].line;
  372. mle[newlines].start_pc = le[i].pc;
  373. sal = find_pc_line (le[i].pc, 0);
  374. mle[newlines].end_pc = sal.end;
  375. newlines++;
  376. }
  377. /* Now, sort mle by line #s (and, then by addresses within lines). */
  378. if (out_of_order)
  379. std::sort (mle, mle + newlines, line_is_less_than);
  380. /* Now, for each line entry, emit the specified lines (unless
  381. they have been emitted before), followed by the assembly code
  382. for that line. */
  383. ui_out_emit_list asm_insns_list (uiout, "asm_insns");
  384. gdb::optional<ui_out_emit_tuple> outer_tuple_emitter;
  385. gdb::optional<ui_out_emit_list> inner_list_emitter;
  386. for (i = 0; i < newlines; i++)
  387. {
  388. /* Print out everything from next_line to the current line. */
  389. if (mle[i].line >= next_line)
  390. {
  391. if (next_line != 0)
  392. {
  393. /* Just one line to print. */
  394. if (next_line == mle[i].line)
  395. {
  396. outer_tuple_emitter.emplace (uiout, "src_and_asm_line");
  397. print_source_lines (symtab, next_line, mle[i].line + 1, psl_flags);
  398. }
  399. else
  400. {
  401. /* Several source lines w/o asm instructions associated. */
  402. for (; next_line < mle[i].line; next_line++)
  403. {
  404. ui_out_emit_tuple tuple_emitter (uiout,
  405. "src_and_asm_line");
  406. print_source_lines (symtab, next_line, next_line + 1,
  407. psl_flags);
  408. ui_out_emit_list temp_list_emitter (uiout,
  409. "line_asm_insn");
  410. }
  411. /* Print the last line and leave list open for
  412. asm instructions to be added. */
  413. outer_tuple_emitter.emplace (uiout, "src_and_asm_line");
  414. print_source_lines (symtab, next_line, mle[i].line + 1, psl_flags);
  415. }
  416. }
  417. else
  418. {
  419. outer_tuple_emitter.emplace (uiout, "src_and_asm_line");
  420. print_source_lines (symtab, mle[i].line, mle[i].line + 1, psl_flags);
  421. }
  422. next_line = mle[i].line + 1;
  423. inner_list_emitter.emplace (uiout, "line_asm_insn");
  424. }
  425. num_displayed += dump_insns (gdbarch, uiout,
  426. mle[i].start_pc, mle[i].end_pc,
  427. how_many, flags, NULL);
  428. /* When we've reached the end of the mle array, or we've seen the last
  429. assembly range for this source line, close out the list/tuple. */
  430. if (i == (newlines - 1) || mle[i + 1].line > mle[i].line)
  431. {
  432. inner_list_emitter.reset ();
  433. outer_tuple_emitter.reset ();
  434. uiout->text ("\n");
  435. }
  436. if (how_many >= 0 && num_displayed >= how_many)
  437. break;
  438. }
  439. }
  440. /* The idea here is to present a source-O-centric view of a
  441. function to the user. This means that things are presented
  442. in source order, with (possibly) out of order assembly
  443. immediately following. */
  444. static void
  445. do_mixed_source_and_assembly (struct gdbarch *gdbarch,
  446. struct ui_out *uiout,
  447. struct symtab *main_symtab,
  448. CORE_ADDR low, CORE_ADDR high,
  449. int how_many, gdb_disassembly_flags flags)
  450. {
  451. const struct linetable_entry *le, *first_le;
  452. int i, nlines;
  453. int num_displayed = 0;
  454. print_source_lines_flags psl_flags = 0;
  455. CORE_ADDR pc;
  456. struct symtab *last_symtab;
  457. int last_line;
  458. gdb_assert (main_symtab != NULL && main_symtab->linetable () != NULL);
  459. /* First pass: collect the list of all source files and lines.
  460. We do this so that we can only print lines containing code once.
  461. We try to print the source text leading up to the next instruction,
  462. but if that text is for code that will be disassembled later, then
  463. we'll want to defer printing it until later with its associated code. */
  464. htab_up dis_line_table (allocate_dis_line_table ());
  465. pc = low;
  466. /* The prologue may be empty, but there may still be a line number entry
  467. for the opening brace which is distinct from the first line of code.
  468. If the prologue has been eliminated find_pc_line may return the source
  469. line after the opening brace. We still want to print this opening brace.
  470. first_le is used to implement this. */
  471. nlines = main_symtab->linetable ()->nitems;
  472. le = main_symtab->linetable ()->item;
  473. first_le = NULL;
  474. /* Skip all the preceding functions. */
  475. for (i = 0; i < nlines && le[i].pc < low; i++)
  476. continue;
  477. if (i < nlines && le[i].pc < high)
  478. first_le = &le[i];
  479. /* Add lines for every pc value. */
  480. while (pc < high)
  481. {
  482. struct symtab_and_line sal;
  483. int length;
  484. sal = find_pc_line (pc, 0);
  485. length = gdb_insn_length (gdbarch, pc);
  486. pc += length;
  487. if (sal.symtab != NULL)
  488. add_dis_line_entry (dis_line_table.get (), sal.symtab, sal.line);
  489. }
  490. /* Second pass: print the disassembly.
  491. Output format, from an MI perspective:
  492. The result is a ui_out list, field name "asm_insns", where elements have
  493. name "src_and_asm_line".
  494. Each element is a tuple of source line specs (field names line, file,
  495. fullname), and field "line_asm_insn" which contains the disassembly.
  496. Field "line_asm_insn" is a list of tuples: address, func-name, offset,
  497. opcodes, inst.
  498. CLI output works on top of this because MI ignores ui_out_text output,
  499. which is where we put file name and source line contents output.
  500. Emitter usage:
  501. asm_insns_emitter
  502. Handles the outer "asm_insns" list.
  503. tuple_emitter
  504. The tuples for each group of consecutive disassemblies.
  505. list_emitter
  506. List of consecutive source lines or disassembled insns. */
  507. if (flags & DISASSEMBLY_FILENAME)
  508. psl_flags |= PRINT_SOURCE_LINES_FILENAME;
  509. ui_out_emit_list asm_insns_emitter (uiout, "asm_insns");
  510. gdb::optional<ui_out_emit_tuple> tuple_emitter;
  511. gdb::optional<ui_out_emit_list> list_emitter;
  512. last_symtab = NULL;
  513. last_line = 0;
  514. pc = low;
  515. while (pc < high)
  516. {
  517. struct symtab_and_line sal;
  518. CORE_ADDR end_pc;
  519. int start_preceding_line_to_display = 0;
  520. int end_preceding_line_to_display = 0;
  521. int new_source_line = 0;
  522. sal = find_pc_line (pc, 0);
  523. if (sal.symtab != last_symtab)
  524. {
  525. /* New source file. */
  526. new_source_line = 1;
  527. /* If this is the first line of output, check for any preceding
  528. lines. */
  529. if (last_line == 0
  530. && first_le != NULL
  531. && first_le->line < sal.line)
  532. {
  533. start_preceding_line_to_display = first_le->line;
  534. end_preceding_line_to_display = sal.line;
  535. }
  536. }
  537. else
  538. {
  539. /* Same source file as last time. */
  540. if (sal.symtab != NULL)
  541. {
  542. if (sal.line > last_line + 1 && last_line != 0)
  543. {
  544. int l;
  545. /* Several preceding source lines. Print the trailing ones
  546. not associated with code that we'll print later. */
  547. for (l = sal.line - 1; l > last_line; --l)
  548. {
  549. if (line_has_code_p (dis_line_table.get (),
  550. sal.symtab, l))
  551. break;
  552. }
  553. if (l < sal.line - 1)
  554. {
  555. start_preceding_line_to_display = l + 1;
  556. end_preceding_line_to_display = sal.line;
  557. }
  558. }
  559. if (sal.line != last_line)
  560. new_source_line = 1;
  561. else
  562. {
  563. /* Same source line as last time. This can happen, depending
  564. on the debug info. */
  565. }
  566. }
  567. }
  568. if (new_source_line)
  569. {
  570. /* Skip the newline if this is the first instruction. */
  571. if (pc > low)
  572. uiout->text ("\n");
  573. if (tuple_emitter.has_value ())
  574. {
  575. gdb_assert (list_emitter.has_value ());
  576. list_emitter.reset ();
  577. tuple_emitter.reset ();
  578. }
  579. if (sal.symtab != last_symtab
  580. && !(flags & DISASSEMBLY_FILENAME))
  581. {
  582. /* Remember MI ignores ui_out_text.
  583. We don't have to do anything here for MI because MI
  584. output includes the source specs for each line. */
  585. if (sal.symtab != NULL)
  586. {
  587. uiout->text (symtab_to_filename_for_display (sal.symtab));
  588. }
  589. else
  590. uiout->text ("unknown");
  591. uiout->text (":\n");
  592. }
  593. if (start_preceding_line_to_display > 0)
  594. {
  595. /* Several source lines w/o asm instructions associated.
  596. We need to preserve the structure of the output, so output
  597. a bunch of line tuples with no asm entries. */
  598. int l;
  599. gdb_assert (sal.symtab != NULL);
  600. for (l = start_preceding_line_to_display;
  601. l < end_preceding_line_to_display;
  602. ++l)
  603. {
  604. ui_out_emit_tuple line_tuple_emitter (uiout,
  605. "src_and_asm_line");
  606. print_source_lines (sal.symtab, l, l + 1, psl_flags);
  607. ui_out_emit_list chain_line_emitter (uiout, "line_asm_insn");
  608. }
  609. }
  610. tuple_emitter.emplace (uiout, "src_and_asm_line");
  611. if (sal.symtab != NULL)
  612. print_source_lines (sal.symtab, sal.line, sal.line + 1, psl_flags);
  613. else
  614. uiout->text (_("--- no source info for this pc ---\n"));
  615. list_emitter.emplace (uiout, "line_asm_insn");
  616. }
  617. else
  618. {
  619. /* Here we're appending instructions to an existing line.
  620. By construction the very first insn will have a symtab
  621. and follow the new_source_line path above. */
  622. gdb_assert (tuple_emitter.has_value ());
  623. gdb_assert (list_emitter.has_value ());
  624. }
  625. if (sal.end != 0)
  626. end_pc = std::min (sal.end, high);
  627. else
  628. end_pc = pc + 1;
  629. num_displayed += dump_insns (gdbarch, uiout, pc, end_pc,
  630. how_many, flags, &end_pc);
  631. pc = end_pc;
  632. if (how_many >= 0 && num_displayed >= how_many)
  633. break;
  634. last_symtab = sal.symtab;
  635. last_line = sal.line;
  636. }
  637. }
  638. static void
  639. do_assembly_only (struct gdbarch *gdbarch, struct ui_out *uiout,
  640. CORE_ADDR low, CORE_ADDR high,
  641. int how_many, gdb_disassembly_flags flags)
  642. {
  643. ui_out_emit_list list_emitter (uiout, "asm_insns");
  644. dump_insns (gdbarch, uiout, low, high, how_many, flags, NULL);
  645. }
  646. /* Combine implicit and user disassembler options and return them
  647. in a newly-created string. */
  648. static std::string
  649. get_all_disassembler_options (struct gdbarch *gdbarch)
  650. {
  651. const char *implicit = gdbarch_disassembler_options_implicit (gdbarch);
  652. const char *options = get_disassembler_options (gdbarch);
  653. const char *comma = ",";
  654. if (implicit == nullptr)
  655. {
  656. implicit = "";
  657. comma = "";
  658. }
  659. if (options == nullptr)
  660. {
  661. options = "";
  662. comma = "";
  663. }
  664. return string_printf ("%s%s%s", implicit, comma, options);
  665. }
  666. gdb_disassembler::gdb_disassembler (struct gdbarch *gdbarch,
  667. struct ui_file *file,
  668. di_read_memory_ftype read_memory_func)
  669. : m_gdbarch (gdbarch),
  670. m_buffer (!use_ext_lang_colorization_p && disassembler_styling
  671. && file->can_emit_style_escape ()),
  672. m_dest (file)
  673. {
  674. init_disassemble_info (&m_di, &m_buffer, dis_asm_fprintf,
  675. dis_asm_styled_fprintf);
  676. m_di.flavour = bfd_target_unknown_flavour;
  677. m_di.memory_error_func = dis_asm_memory_error;
  678. m_di.print_address_func = dis_asm_print_address;
  679. /* NOTE: cagney/2003-04-28: The original code, from the old Insight
  680. disassembler had a local optimization here. By default it would
  681. access the executable file, instead of the target memory (there
  682. was a growing list of exceptions though). Unfortunately, the
  683. heuristic was flawed. Commands like "disassemble &variable"
  684. didn't work as they relied on the access going to the target.
  685. Further, it has been superseeded by trust-read-only-sections
  686. (although that should be superseeded by target_trust..._p()). */
  687. m_di.read_memory_func = read_memory_func;
  688. m_di.arch = gdbarch_bfd_arch_info (gdbarch)->arch;
  689. m_di.mach = gdbarch_bfd_arch_info (gdbarch)->mach;
  690. m_di.endian = gdbarch_byte_order (gdbarch);
  691. m_di.endian_code = gdbarch_byte_order_for_code (gdbarch);
  692. m_di.application_data = this;
  693. m_disassembler_options_holder = get_all_disassembler_options (gdbarch);
  694. if (!m_disassembler_options_holder.empty ())
  695. m_di.disassembler_options = m_disassembler_options_holder.c_str ();
  696. disassemble_init_for_target (&m_di);
  697. }
  698. gdb_disassembler::~gdb_disassembler ()
  699. {
  700. disassemble_free_target (&m_di);
  701. }
  702. /* See disasm.h. */
  703. bool gdb_disassembler::use_ext_lang_colorization_p = true;
  704. /* See disasm.h. */
  705. int
  706. gdb_disassembler::print_insn (CORE_ADDR memaddr,
  707. int *branch_delay_insns)
  708. {
  709. m_err_memaddr.reset ();
  710. m_buffer.clear ();
  711. int length = gdbarch_print_insn (arch (), memaddr, &m_di);
  712. /* If we have successfully disassembled an instruction, styling is on, we
  713. think that the extension language might be able to perform styling for
  714. us, and the destination can support styling, then lets call into the
  715. extension languages in order to style this output. */
  716. if (length > 0 && disassembler_styling
  717. && use_ext_lang_colorization_p
  718. && m_dest->can_emit_style_escape ())
  719. {
  720. gdb::optional<std::string> ext_contents;
  721. ext_contents = ext_lang_colorize_disasm (m_buffer.string (), arch ());
  722. if (ext_contents.has_value ())
  723. m_buffer = std::move (*ext_contents);
  724. else
  725. {
  726. /* The extension language failed to add styling to the
  727. disassembly output. Set the static flag so that next time we
  728. disassemble we don't even bother attempting to use the
  729. extension language for styling. */
  730. use_ext_lang_colorization_p = false;
  731. /* The instruction we just disassembled, and the extension
  732. languages failed to style, might have otherwise had some
  733. minimal styling applied by GDB. To regain that styling we
  734. need to recreate m_buffer, but this time with styling support.
  735. To do this we perform an in-place new, but this time turn on
  736. the styling support, then we can re-disassembly the
  737. instruction, and gain any minimal styling GDB might add. */
  738. gdb_static_assert ((std::is_same<decltype (m_buffer),
  739. string_file>::value));
  740. gdb_assert (!m_buffer.term_out ());
  741. m_buffer.~string_file ();
  742. new (&m_buffer) string_file (true);
  743. length = gdbarch_print_insn (arch (), memaddr, &m_di);
  744. gdb_assert (length > 0);
  745. }
  746. }
  747. /* Push any disassemble output to the real destination stream. We do
  748. this even if the disassembler reported failure (-1) as the
  749. disassembler may have printed something to its output stream. */
  750. m_di.fprintf_func (m_dest, "%s", m_buffer.c_str ());
  751. /* If the disassembler failed then report an appropriate error. */
  752. if (length < 0)
  753. {
  754. if (m_err_memaddr.has_value ())
  755. memory_error (TARGET_XFER_E_IO, *m_err_memaddr);
  756. else
  757. error (_("unknown disassembler error (error = %d)"), length);
  758. }
  759. if (branch_delay_insns != NULL)
  760. {
  761. if (m_di.insn_info_valid)
  762. *branch_delay_insns = m_di.branch_delay_insns;
  763. else
  764. *branch_delay_insns = 0;
  765. }
  766. return length;
  767. }
  768. void
  769. gdb_disassembly (struct gdbarch *gdbarch, struct ui_out *uiout,
  770. gdb_disassembly_flags flags, int how_many,
  771. CORE_ADDR low, CORE_ADDR high)
  772. {
  773. struct symtab *symtab;
  774. int nlines = -1;
  775. /* Assume symtab is valid for whole PC range. */
  776. symtab = find_pc_line_symtab (low);
  777. if (symtab != NULL && symtab->linetable () != NULL)
  778. nlines = symtab->linetable ()->nitems;
  779. if (!(flags & (DISASSEMBLY_SOURCE_DEPRECATED | DISASSEMBLY_SOURCE))
  780. || nlines <= 0)
  781. do_assembly_only (gdbarch, uiout, low, high, how_many, flags);
  782. else if (flags & DISASSEMBLY_SOURCE)
  783. do_mixed_source_and_assembly (gdbarch, uiout, symtab, low, high,
  784. how_many, flags);
  785. else if (flags & DISASSEMBLY_SOURCE_DEPRECATED)
  786. do_mixed_source_and_assembly_deprecated (gdbarch, uiout, symtab,
  787. low, high, how_many, flags);
  788. gdb_flush (gdb_stdout);
  789. }
  790. /* Print the instruction at address MEMADDR in debugged memory,
  791. on STREAM. Returns the length of the instruction, in bytes,
  792. and, if requested, the number of branch delay slot instructions. */
  793. int
  794. gdb_print_insn (struct gdbarch *gdbarch, CORE_ADDR memaddr,
  795. struct ui_file *stream, int *branch_delay_insns)
  796. {
  797. gdb_disassembler di (gdbarch, stream);
  798. return di.print_insn (memaddr, branch_delay_insns);
  799. }
  800. /* Return the length in bytes of the instruction at address MEMADDR in
  801. debugged memory. */
  802. int
  803. gdb_insn_length (struct gdbarch *gdbarch, CORE_ADDR addr)
  804. {
  805. return gdb_print_insn (gdbarch, addr, &null_stream, NULL);
  806. }
  807. /* An fprintf-function for use by the disassembler when we know we don't
  808. want to print anything. Always returns success. */
  809. static int ATTRIBUTE_PRINTF (2, 3)
  810. gdb_disasm_null_printf (void *stream, const char *format, ...)
  811. {
  812. return 0;
  813. }
  814. /* An fprintf-function for use by the disassembler when we know we don't
  815. want to print anything, and the disassembler is using style. Always
  816. returns success. */
  817. static int ATTRIBUTE_PRINTF (3, 4)
  818. gdb_disasm_null_styled_printf (void *stream,
  819. enum disassembler_style style,
  820. const char *format, ...)
  821. {
  822. return 0;
  823. }
  824. /* See disasm.h. */
  825. void
  826. init_disassemble_info_for_no_printing (struct disassemble_info *dinfo)
  827. {
  828. init_disassemble_info (dinfo, nullptr, gdb_disasm_null_printf,
  829. gdb_disasm_null_styled_printf);
  830. }
  831. /* Initialize a struct disassemble_info for gdb_buffered_insn_length.
  832. Upon return, *DISASSEMBLER_OPTIONS_HOLDER owns the string pointed
  833. to by DI.DISASSEMBLER_OPTIONS. */
  834. static void
  835. gdb_buffered_insn_length_init_dis (struct gdbarch *gdbarch,
  836. struct disassemble_info *di,
  837. const gdb_byte *insn, int max_len,
  838. CORE_ADDR addr,
  839. std::string *disassembler_options_holder)
  840. {
  841. init_disassemble_info_for_no_printing (di);
  842. /* init_disassemble_info installs buffer_read_memory, etc.
  843. so we don't need to do that here.
  844. The cast is necessary until disassemble_info is const-ified. */
  845. di->buffer = (gdb_byte *) insn;
  846. di->buffer_length = max_len;
  847. di->buffer_vma = addr;
  848. di->arch = gdbarch_bfd_arch_info (gdbarch)->arch;
  849. di->mach = gdbarch_bfd_arch_info (gdbarch)->mach;
  850. di->endian = gdbarch_byte_order (gdbarch);
  851. di->endian_code = gdbarch_byte_order_for_code (gdbarch);
  852. *disassembler_options_holder = get_all_disassembler_options (gdbarch);
  853. if (!disassembler_options_holder->empty ())
  854. di->disassembler_options = disassembler_options_holder->c_str ();
  855. disassemble_init_for_target (di);
  856. }
  857. /* Return the length in bytes of INSN. MAX_LEN is the size of the
  858. buffer containing INSN. */
  859. int
  860. gdb_buffered_insn_length (struct gdbarch *gdbarch,
  861. const gdb_byte *insn, int max_len, CORE_ADDR addr)
  862. {
  863. struct disassemble_info di;
  864. std::string disassembler_options_holder;
  865. gdb_buffered_insn_length_init_dis (gdbarch, &di, insn, max_len, addr,
  866. &disassembler_options_holder);
  867. int result = gdbarch_print_insn (gdbarch, addr, &di);
  868. disassemble_free_target (&di);
  869. return result;
  870. }
  871. char *
  872. get_disassembler_options (struct gdbarch *gdbarch)
  873. {
  874. char **disassembler_options = gdbarch_disassembler_options (gdbarch);
  875. if (disassembler_options == NULL)
  876. return NULL;
  877. return *disassembler_options;
  878. }
  879. void
  880. set_disassembler_options (const char *prospective_options)
  881. {
  882. struct gdbarch *gdbarch = get_current_arch ();
  883. char **disassembler_options = gdbarch_disassembler_options (gdbarch);
  884. const disasm_options_and_args_t *valid_options_and_args;
  885. const disasm_options_t *valid_options;
  886. gdb::unique_xmalloc_ptr<char> prospective_options_local
  887. = make_unique_xstrdup (prospective_options);
  888. char *options = remove_whitespace_and_extra_commas
  889. (prospective_options_local.get ());
  890. const char *opt;
  891. /* Allow all architectures, even ones that do not support 'set disassembler',
  892. to reset their disassembler options to NULL. */
  893. if (options == NULL)
  894. {
  895. if (disassembler_options != NULL)
  896. {
  897. free (*disassembler_options);
  898. *disassembler_options = NULL;
  899. }
  900. return;
  901. }
  902. valid_options_and_args = gdbarch_valid_disassembler_options (gdbarch);
  903. if (valid_options_and_args == NULL)
  904. {
  905. gdb_printf (gdb_stderr, _("\
  906. 'set disassembler-options ...' is not supported on this architecture.\n"));
  907. return;
  908. }
  909. valid_options = &valid_options_and_args->options;
  910. /* Verify we have valid disassembler options. */
  911. FOR_EACH_DISASSEMBLER_OPTION (opt, options)
  912. {
  913. size_t i;
  914. for (i = 0; valid_options->name[i] != NULL; i++)
  915. if (valid_options->arg != NULL && valid_options->arg[i] != NULL)
  916. {
  917. size_t len = strlen (valid_options->name[i]);
  918. bool found = false;
  919. const char *arg;
  920. size_t j;
  921. if (memcmp (opt, valid_options->name[i], len) != 0)
  922. continue;
  923. arg = opt + len;
  924. for (j = 0; valid_options->arg[i]->values[j] != NULL; j++)
  925. if (disassembler_options_cmp
  926. (arg, valid_options->arg[i]->values[j]) == 0)
  927. {
  928. found = true;
  929. break;
  930. }
  931. if (found)
  932. break;
  933. }
  934. else if (disassembler_options_cmp (opt, valid_options->name[i]) == 0)
  935. break;
  936. if (valid_options->name[i] == NULL)
  937. {
  938. gdb_printf (gdb_stderr,
  939. _("Invalid disassembler option value: '%s'.\n"),
  940. opt);
  941. return;
  942. }
  943. }
  944. free (*disassembler_options);
  945. *disassembler_options = xstrdup (options);
  946. }
  947. static void
  948. set_disassembler_options_sfunc (const char *args, int from_tty,
  949. struct cmd_list_element *c)
  950. {
  951. set_disassembler_options (prospective_options.c_str ());
  952. }
  953. static void
  954. show_disassembler_options_sfunc (struct ui_file *file, int from_tty,
  955. struct cmd_list_element *c, const char *value)
  956. {
  957. struct gdbarch *gdbarch = get_current_arch ();
  958. const disasm_options_and_args_t *valid_options_and_args;
  959. const disasm_option_arg_t *valid_args;
  960. const disasm_options_t *valid_options;
  961. const char *options = get_disassembler_options (gdbarch);
  962. if (options == NULL)
  963. options = "";
  964. gdb_printf (file, _("The current disassembler options are '%s'\n\n"),
  965. options);
  966. valid_options_and_args = gdbarch_valid_disassembler_options (gdbarch);
  967. if (valid_options_and_args == NULL)
  968. {
  969. gdb_puts (_("There are no disassembler options available "
  970. "for this architecture.\n"),
  971. file);
  972. return;
  973. }
  974. valid_options = &valid_options_and_args->options;
  975. gdb_printf (file, _("\
  976. The following disassembler options are supported for use with the\n\
  977. 'set disassembler-options OPTION [,OPTION]...' command:\n"));
  978. if (valid_options->description != NULL)
  979. {
  980. size_t i, max_len = 0;
  981. gdb_printf (file, "\n");
  982. /* Compute the length of the longest option name. */
  983. for (i = 0; valid_options->name[i] != NULL; i++)
  984. {
  985. size_t len = strlen (valid_options->name[i]);
  986. if (valid_options->arg != NULL && valid_options->arg[i] != NULL)
  987. len += strlen (valid_options->arg[i]->name);
  988. if (max_len < len)
  989. max_len = len;
  990. }
  991. for (i = 0, max_len++; valid_options->name[i] != NULL; i++)
  992. {
  993. gdb_printf (file, " %s", valid_options->name[i]);
  994. if (valid_options->arg != NULL && valid_options->arg[i] != NULL)
  995. gdb_printf (file, "%s", valid_options->arg[i]->name);
  996. if (valid_options->description[i] != NULL)
  997. {
  998. size_t len = strlen (valid_options->name[i]);
  999. if (valid_options->arg != NULL && valid_options->arg[i] != NULL)
  1000. len += strlen (valid_options->arg[i]->name);
  1001. gdb_printf (file, "%*c %s", (int) (max_len - len), ' ',
  1002. valid_options->description[i]);
  1003. }
  1004. gdb_printf (file, "\n");
  1005. }
  1006. }
  1007. else
  1008. {
  1009. size_t i;
  1010. gdb_printf (file, " ");
  1011. for (i = 0; valid_options->name[i] != NULL; i++)
  1012. {
  1013. gdb_printf (file, "%s", valid_options->name[i]);
  1014. if (valid_options->arg != NULL && valid_options->arg[i] != NULL)
  1015. gdb_printf (file, "%s", valid_options->arg[i]->name);
  1016. if (valid_options->name[i + 1] != NULL)
  1017. gdb_printf (file, ", ");
  1018. file->wrap_here (2);
  1019. }
  1020. gdb_printf (file, "\n");
  1021. }
  1022. valid_args = valid_options_and_args->args;
  1023. if (valid_args != NULL)
  1024. {
  1025. size_t i, j;
  1026. for (i = 0; valid_args[i].name != NULL; i++)
  1027. {
  1028. gdb_printf (file, _("\n\
  1029. For the options above, the following values are supported for \"%s\":\n "),
  1030. valid_args[i].name);
  1031. for (j = 0; valid_args[i].values[j] != NULL; j++)
  1032. {
  1033. gdb_printf (file, " %s", valid_args[i].values[j]);
  1034. file->wrap_here (3);
  1035. }
  1036. gdb_printf (file, "\n");
  1037. }
  1038. }
  1039. }
  1040. /* A completion function for "set disassembler". */
  1041. static void
  1042. disassembler_options_completer (struct cmd_list_element *ignore,
  1043. completion_tracker &tracker,
  1044. const char *text, const char *word)
  1045. {
  1046. struct gdbarch *gdbarch = get_current_arch ();
  1047. const disasm_options_and_args_t *opts_and_args
  1048. = gdbarch_valid_disassembler_options (gdbarch);
  1049. if (opts_and_args != NULL)
  1050. {
  1051. const disasm_options_t *opts = &opts_and_args->options;
  1052. /* Only attempt to complete on the last option text. */
  1053. const char *separator = strrchr (text, ',');
  1054. if (separator != NULL)
  1055. text = separator + 1;
  1056. text = skip_spaces (text);
  1057. complete_on_enum (tracker, opts->name, text, word);
  1058. }
  1059. }
  1060. /* Initialization code. */
  1061. void _initialize_disasm ();
  1062. void
  1063. _initialize_disasm ()
  1064. {
  1065. /* Add the command that controls the disassembler options. */
  1066. set_show_commands set_show_disas_opts
  1067. = add_setshow_string_noescape_cmd ("disassembler-options", no_class,
  1068. &prospective_options, _("\
  1069. Set the disassembler options.\n\
  1070. Usage: set disassembler-options OPTION [,OPTION]...\n\n\
  1071. See: 'show disassembler-options' for valid option values."), _("\
  1072. Show the disassembler options."), NULL,
  1073. set_disassembler_options_sfunc,
  1074. show_disassembler_options_sfunc,
  1075. &setlist, &showlist);
  1076. set_cmd_completer (set_show_disas_opts.set, disassembler_options_completer);
  1077. }