Home | History | Annotate | Line # | Download | only in gdb
disasm.h revision 1.1.1.9
      1 /* Disassemble support for GDB.
      2    Copyright (C) 2002-2024 Free Software Foundation, Inc.
      3 
      4    This file is part of GDB.
      5 
      6    This program is free software; you can redistribute it and/or modify
      7    it under the terms of the GNU General Public License as published by
      8    the Free Software Foundation; either version 3 of the License, or
      9    (at your option) any later version.
     10 
     11    This program is distributed in the hope that it will be useful,
     12    but WITHOUT ANY WARRANTY; without even the implied warranty of
     13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14    GNU General Public License for more details.
     15 
     16    You should have received a copy of the GNU General Public License
     17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     18 
     19 #ifndef GDB_DISASM_H
     20 #define GDB_DISASM_H
     21 
     22 #include "dis-asm.h"
     23 #include "disasm-flags.h"
     24 #include "ui-out.h"
     25 
     26 struct gdbarch;
     27 struct ui_out;
     28 struct ui_file;
     29 
     30 /* A wrapper around a disassemble_info and a gdbarch.  This is the core
     31    set of data that all disassembler sub-classes will need.  This class
     32    doesn't actually implement the disassembling process, that is something
     33    that sub-classes will do, with each sub-class doing things slightly
     34    differently.
     35 
     36    The constructor of this class is protected, you should not create
     37    instances of this class directly, instead create an instance of an
     38    appropriate sub-class.  */
     39 
     40 struct gdb_disassemble_info
     41 {
     42   DISABLE_COPY_AND_ASSIGN (gdb_disassemble_info);
     43 
     44   /* Return the gdbarch we are disassembling for.  */
     45   struct gdbarch *arch ()
     46   { return m_gdbarch; }
     47 
     48   /* Return a pointer to the disassemble_info, this will be needed for
     49      passing into the libopcodes disassembler.  */
     50   struct disassemble_info *disasm_info ()
     51   { return &m_di; }
     52 
     53 protected:
     54 
     55   /* Types for the function callbacks within m_di.  The actual function
     56      signatures here are taken from include/dis-asm.h.  */
     57   using read_memory_ftype
     58     = int (*) (bfd_vma, bfd_byte *, unsigned int, struct disassemble_info *)
     59 	noexcept;
     60   using memory_error_ftype
     61     = void (*) (int, bfd_vma, struct disassemble_info *) noexcept;
     62   using print_address_ftype
     63     = void (*) (bfd_vma, struct disassemble_info *) noexcept;
     64   using fprintf_ftype
     65     = int (*) (void *, const char *, ...) noexcept;
     66   using fprintf_styled_ftype
     67     = int (*) (void *, enum disassembler_style, const char *, ...) noexcept;
     68 
     69   /* Constructor, many fields in m_di are initialized from GDBARCH.  The
     70      remaining arguments are function callbacks that are written into m_di.
     71      Of these function callbacks FPRINTF_FUNC and FPRINTF_STYLED_FUNC must
     72      not be nullptr.  If READ_MEMORY_FUNC, MEMORY_ERROR_FUNC, or
     73      PRINT_ADDRESS_FUNC are nullptr, then that field within m_di is left
     74      with its default value (see the libopcodes function
     75      init_disassemble_info for the defaults).  */
     76   gdb_disassemble_info (struct gdbarch *gdbarch,
     77 			read_memory_ftype read_memory_func,
     78 			memory_error_ftype memory_error_func,
     79 			print_address_ftype print_address_func,
     80 			fprintf_ftype fprintf_func,
     81 			fprintf_styled_ftype fprintf_styled_func);
     82 
     83   /* Destructor.  */
     84   virtual ~gdb_disassemble_info ();
     85 
     86   /* Stores data required for disassembling instructions in
     87      opcodes.  */
     88   struct disassemble_info m_di;
     89 
     90 private:
     91   /* The architecture we are disassembling for.  */
     92   struct gdbarch *m_gdbarch;
     93 
     94   /* If we own the string in `m_di.disassembler_options', we do so
     95      using this field.  */
     96   std::string m_disassembler_options_holder;
     97 };
     98 
     99 /* A wrapper around gdb_disassemble_info.  This class adds default
    100    print functions that are supplied to the disassemble_info within the
    101    parent class.  These default print functions write to the stream, which
    102    is also contained in the parent class.
    103 
    104    As with the parent class, the constructor for this class is protected,
    105    you should not create instances of this class, but create an
    106    appropriate sub-class instead.  */
    107 
    108 struct gdb_printing_disassembler : public gdb_disassemble_info
    109 {
    110   DISABLE_COPY_AND_ASSIGN (gdb_printing_disassembler);
    111 
    112   /* The stream that disassembler output is being written too.  */
    113   struct ui_file *stream ()
    114   { return m_stream; }
    115 
    116 protected:
    117 
    118   /* Constructor.  All the arguments are just passed to the parent class.
    119      We also add the two print functions to the arguments passed to the
    120      parent.  See gdb_disassemble_info for a description of how the
    121      arguments are handled.  */
    122   gdb_printing_disassembler (struct gdbarch *gdbarch,
    123 			     struct ui_file *stream,
    124 			     read_memory_ftype read_memory_func,
    125 			     memory_error_ftype memory_error_func,
    126 			     print_address_ftype print_address_func)
    127     : gdb_disassemble_info (gdbarch, read_memory_func,
    128 			    memory_error_func, print_address_func,
    129 			    fprintf_func, fprintf_styled_func),
    130       m_stream (stream)
    131   {
    132     gdb_assert (stream != nullptr);
    133   }
    134 
    135   /* Callback used as the disassemble_info's fprintf_func callback.  The
    136      DIS_INFO pointer is a pointer to a gdb_printing_disassembler object.
    137      Content is written to the m_stream extracted from DIS_INFO.  */
    138   static int fprintf_func (void *dis_info, const char *format, ...) noexcept
    139     ATTRIBUTE_PRINTF (2, 3);
    140 
    141   /* Callback used as the disassemble_info's fprintf_styled_func callback.
    142      The DIS_INFO pointer is a pointer to a gdb_printing_disassembler
    143      object.  Content is written to the m_stream extracted from DIS_INFO.  */
    144   static int fprintf_styled_func (void *dis_info,
    145 				  enum disassembler_style style,
    146 				  const char *format, ...) noexcept
    147     ATTRIBUTE_PRINTF(3,4);
    148 
    149   /* Return true if the disassembler is considered inside a comment, false
    150      otherwise.  */
    151   bool in_comment_p () const
    152   { return m_in_comment; }
    153 
    154   /* Set whether the disassembler should be considered as within comment
    155      text or not.  */
    156   void set_in_comment (bool c)
    157   { m_in_comment = c; }
    158 
    159 private:
    160 
    161   /* When libopcodes calls the fprintf_func and fprintf_styled_func
    162      callbacks, a 'void *' argument is passed.  We arrange, through our
    163      call to init_disassemble_info that this argument will be a pointer to
    164      a gdb_disassemble_info sub-class, specifically, a
    165      gdb_printing_disassembler pointer.  This helper function casts
    166      DIS_INFO to the correct type (with some asserts), and then returns the
    167      m_stream member variable.  */
    168   static ui_file *stream_from_gdb_disassemble_info (void *dis_info);
    169 
    170   /* The stream to which output should be sent.  */
    171   struct ui_file *m_stream;
    172 
    173   /* Are we inside a comment?  This will be set true if the disassembler
    174      uses styled output and emits a start of comment character.  It is up
    175      to the code that uses this disassembler class to reset this flag back
    176      to false at a suitable time (e.g. at the end of every line).  */
    177   bool m_in_comment = false;
    178 };
    179 
    180 /* A basic disassembler that doesn't actually print anything.  */
    181 
    182 struct gdb_non_printing_disassembler : public gdb_disassemble_info
    183 {
    184   gdb_non_printing_disassembler (struct gdbarch *gdbarch,
    185 				 read_memory_ftype read_memory_func)
    186     : gdb_disassemble_info (gdbarch,
    187 			    read_memory_func,
    188 			    nullptr /* memory_error_func */,
    189 			    nullptr /* print_address_func */,
    190 			    null_fprintf_func,
    191 			    null_fprintf_styled_func)
    192   { /* Nothing.  */ }
    193 
    194 private:
    195 
    196   /* Callback used as the disassemble_info's fprintf_func callback, this
    197      doesn't write anything to STREAM, but just returns 0.  */
    198   static int null_fprintf_func (void *stream, const char *format, ...) noexcept
    199     ATTRIBUTE_PRINTF(2,3);
    200 
    201   /* Callback used as the disassemble_info's fprintf_styled_func callback,
    202      , this doesn't write anything to STREAM, but just returns 0.  */
    203   static int null_fprintf_styled_func (void *stream,
    204 				       enum disassembler_style style,
    205 				       const char *format, ...) noexcept
    206     ATTRIBUTE_PRINTF(3,4);
    207 };
    208 
    209 /* This is a helper class, for use as an additional base-class, by some of
    210    the disassembler classes below.  This class just defines a static method
    211    for reading from target memory, which can then be used by the various
    212    disassembler sub-classes.  */
    213 
    214 struct gdb_disassembler_memory_reader
    215 {
    216   /* Implements the read_memory_func disassemble_info callback.  */
    217   static int dis_asm_read_memory (bfd_vma memaddr, gdb_byte *myaddr,
    218 				  unsigned int len,
    219 				  struct disassemble_info *info) noexcept;
    220 };
    221 
    222 /* A non-printing disassemble_info management class.  The disassemble_info
    223    setup by this class will not print anything to the output stream (there
    224    is no output stream), and the instruction to be disassembled will be
    225    read from target memory.  */
    226 
    227 struct gdb_non_printing_memory_disassembler
    228   : public gdb_non_printing_disassembler,
    229     private gdb_disassembler_memory_reader
    230 {
    231   /* Constructor.  GDBARCH is the architecture to disassemble for.  */
    232   gdb_non_printing_memory_disassembler (struct gdbarch *gdbarch)
    233     :gdb_non_printing_disassembler (gdbarch, dis_asm_read_memory)
    234   { /* Nothing.  */ }
    235 };
    236 
    237 /* A disassembler class that provides 'print_insn', a method for
    238    disassembling a single instruction to the output stream.  */
    239 
    240 struct gdb_disassembler : public gdb_printing_disassembler,
    241 			  private gdb_disassembler_memory_reader
    242 {
    243   gdb_disassembler (struct gdbarch *gdbarch, struct ui_file *file)
    244     : gdb_disassembler (gdbarch, file, dis_asm_read_memory)
    245   { /* Nothing.  */ }
    246 
    247   DISABLE_COPY_AND_ASSIGN (gdb_disassembler);
    248 
    249   /* Disassemble a single instruction at MEMADDR to the ui_file* that was
    250      passed to the constructor.  If a memory error occurs while
    251      disassembling this instruction then an error will be thrown.  */
    252   int print_insn (CORE_ADDR memaddr, int *branch_delay_insns = NULL);
    253 
    254 protected:
    255   gdb_disassembler (struct gdbarch *gdbarch, struct ui_file *file,
    256 		    read_memory_ftype func);
    257 
    258 private:
    259   /* This member variable is given a value by calling dis_asm_memory_error.
    260      If after calling into the libopcodes disassembler we get back a
    261      negative value (which indicates an error), then, if this variable has
    262      a value, we report a memory error to the user, otherwise, we report a
    263      non-memory error.  */
    264   std::optional<CORE_ADDR> m_err_memaddr;
    265 
    266   /* The stream to which disassembler output will be written.  */
    267   ui_file *m_dest;
    268 
    269   /* Disassembler output is built up into this buffer.  Whether this
    270      string_file is created with styling support or not depends on the
    271      value of use_ext_lang_colorization_p, as well as whether disassembler
    272      styling in general is turned on, and also, whether *m_dest supports
    273      styling or not.  */
    274   string_file m_buffer;
    275 
    276   /* When true, m_buffer will be created without styling support,
    277      otherwise, m_buffer will be created with styling support.
    278 
    279      This field will initially be true, but will be set to false if
    280      ext_lang_colorize_disasm fails to add styling at any time.
    281 
    282      If the extension language is going to add the styling then m_buffer
    283      should be created without styling support, the extension language will
    284      then add styling at the end of the disassembly process.
    285 
    286      If the extension language is not going to add the styling, then we
    287      create m_buffer with styling support, and GDB will add minimal styling
    288      (currently just to addresses and symbols) as it goes.  */
    289   static bool use_ext_lang_colorization_p;
    290 
    291   static void dis_asm_memory_error (int err, bfd_vma memaddr,
    292 				    struct disassemble_info *info) noexcept;
    293   static void dis_asm_print_address (bfd_vma addr,
    294 				     struct disassemble_info *info) noexcept;
    295 
    296   /* Return true if we should use the extension language to apply
    297      disassembler styling.  This requires disassembler styling to be on
    298      (i.e. 'set style disassembler enabled on'), the output stream needs to
    299      support styling, and libopcode styling needs to be either off, or not
    300      supported for the current architecture (libopcodes is used in
    301      preference to the extension language method).  */
    302   bool use_ext_lang_for_styling () const;
    303 
    304   /* Return true if we should use libopcodes to apply disassembler styling.
    305      This requires disassembler styling to be on (i.e. 'set style
    306      disassembler enabled on'), the output stream needs to support styling,
    307      and libopcodes styling needs to be supported for the current
    308      architecture, and not disabled by the user.  */
    309   bool use_libopcodes_for_styling () const;
    310 };
    311 
    312 /* An instruction to be disassembled.  */
    313 
    314 struct disasm_insn
    315 {
    316   /* The address of the memory containing the instruction.  */
    317   CORE_ADDR addr;
    318 
    319   /* An optional instruction number.  If non-zero, it is printed first.  */
    320   unsigned int number;
    321 
    322   /* True if the instruction was executed speculatively.  */
    323   unsigned int is_speculative:1;
    324 };
    325 
    326 extern void gdb_disassembly (struct gdbarch *gdbarch, struct ui_out *uiout,
    327 			     gdb_disassembly_flags flags, int how_many,
    328 			     CORE_ADDR low, CORE_ADDR high);
    329 
    330 /* Print the instruction at address MEMADDR in debugged memory,
    331    on STREAM.  Returns the length of the instruction, in bytes,
    332    and, if requested, the number of branch delay slot instructions.  */
    333 
    334 extern int gdb_print_insn (struct gdbarch *gdbarch, CORE_ADDR memaddr,
    335 			   struct ui_file *stream, int *branch_delay_insns);
    336 
    337 /* Class used to pretty-print instructions.  */
    338 
    339 class gdb_pretty_print_disassembler
    340 {
    341 public:
    342   explicit gdb_pretty_print_disassembler (struct gdbarch *gdbarch,
    343 					  struct ui_out *uiout)
    344     : m_uiout (uiout),
    345       m_insn_stb (uiout->can_emit_style_escape ()),
    346       m_di (gdbarch, &m_insn_stb)
    347   {}
    348 
    349   /* Prints the instruction INSN into the saved ui_out and returns the
    350      length of the printed instruction in bytes.  */
    351   int pretty_print_insn (const struct disasm_insn *insn,
    352 			 gdb_disassembly_flags flags);
    353 
    354 private:
    355   /* Returns the architecture used for disassembling.  */
    356   struct gdbarch *arch () { return m_di.arch (); }
    357 
    358   /* The ui_out that is used by pretty_print_insn.  */
    359   struct ui_out *m_uiout;
    360 
    361   /* The buffer used to build the instruction string.  The
    362      disassembler is initialized with this stream.  */
    363   string_file m_insn_stb;
    364 
    365   /* The disassembler used for instruction printing.  */
    366   gdb_disassembler m_di;
    367 
    368   /* The buffer used to build the raw opcodes string.  */
    369   string_file m_opcode_stb;
    370 
    371   /* The buffer used to hold the opcode bytes (if required).  */
    372   gdb::byte_vector m_opcode_data;
    373 };
    374 
    375 /* Return the length in bytes of the instruction at address MEMADDR in
    376    debugged memory.  */
    377 
    378 extern int gdb_insn_length (struct gdbarch *gdbarch, CORE_ADDR memaddr);
    379 
    380 /* Return the length in bytes of INSN, originally at MEMADDR.  MAX_LEN
    381    is the size of the buffer containing INSN.  */
    382 
    383 extern int gdb_buffered_insn_length (struct gdbarch *gdbarch,
    384 				     const gdb_byte *insn, int max_len,
    385 				     CORE_ADDR memaddr);
    386 
    387 /* Returns GDBARCH's disassembler options.  */
    388 
    389 extern const char *get_disassembler_options (struct gdbarch *gdbarch);
    390 
    391 /* Sets the active gdbarch's disassembler options to OPTIONS.  */
    392 
    393 extern void set_disassembler_options (const char *options);
    394 
    395 #endif /* GDB_DISASM_H */
    396