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