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