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