1 1.1 christos /* Python interface to instruction disassembly. 2 1.1 christos 3 1.1.1.2 christos Copyright (C) 2021-2024 Free Software Foundation, Inc. 4 1.1 christos 5 1.1 christos This file is part of GDB. 6 1.1 christos 7 1.1 christos This program is free software; you can redistribute it and/or modify 8 1.1 christos it under the terms of the GNU General Public License as published by 9 1.1 christos the Free Software Foundation; either version 3 of the License, or 10 1.1 christos (at your option) any later version. 11 1.1 christos 12 1.1 christos This program is distributed in the hope that it will be useful, 13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of 14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 1.1 christos GNU General Public License for more details. 16 1.1 christos 17 1.1 christos You should have received a copy of the GNU General Public License 18 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 19 1.1 christos 20 1.1 christos #include "python-internal.h" 21 1.1.1.2 christos #include "language.h" 22 1.1 christos #include "dis-asm.h" 23 1.1 christos #include "arch-utils.h" 24 1.1 christos #include "charset.h" 25 1.1 christos #include "disasm.h" 26 1.1 christos #include "progspace.h" 27 1.1 christos 28 1.1 christos /* Implement gdb.disassembler.DisassembleInfo type. An object of this type 29 1.1 christos represents a single disassembler request from GDB. */ 30 1.1 christos 31 1.1 christos struct disasm_info_object 32 1.1 christos { 33 1.1 christos PyObject_HEAD 34 1.1 christos 35 1.1 christos /* The architecture in which we are disassembling. */ 36 1.1 christos struct gdbarch *gdbarch; 37 1.1 christos 38 1.1 christos /* The program_space in which we are disassembling. */ 39 1.1 christos struct program_space *program_space; 40 1.1 christos 41 1.1 christos /* Address of the instruction to disassemble. */ 42 1.1 christos bfd_vma address; 43 1.1 christos 44 1.1 christos /* The disassemble_info passed from core GDB, this contains the 45 1.1 christos callbacks necessary to read the instruction from core GDB, and to 46 1.1 christos print the disassembled instruction. */ 47 1.1 christos disassemble_info *gdb_info; 48 1.1 christos 49 1.1 christos /* If copies of this object are created then they are chained together 50 1.1 christos via this NEXT pointer, this allows all the copies to be invalidated at 51 1.1 christos the same time as the parent object. */ 52 1.1 christos struct disasm_info_object *next; 53 1.1 christos }; 54 1.1 christos 55 1.1 christos extern PyTypeObject disasm_info_object_type 56 1.1 christos CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("disasm_info_object"); 57 1.1 christos 58 1.1.1.2 christos /* Implement gdb.disassembler.DisassembleAddressPart type. An object of 59 1.1.1.2 christos this type represents a small part of a disassembled instruction; a part 60 1.1.1.2 christos that is an address that should be printed using a call to GDB's 61 1.1.1.2 christos internal print_address function. */ 62 1.1.1.2 christos 63 1.1.1.2 christos struct disasm_addr_part_object 64 1.1.1.2 christos { 65 1.1.1.2 christos PyObject_HEAD 66 1.1.1.2 christos 67 1.1.1.2 christos /* The address to be formatted. */ 68 1.1.1.2 christos bfd_vma address; 69 1.1.1.2 christos 70 1.1.1.2 christos /* A gdbarch. This is only needed in the case where the user asks for 71 1.1.1.2 christos the DisassemblerAddressPart to be converted to a string. When we 72 1.1.1.2 christos return this part to GDB within a DisassemblerResult then GDB will use 73 1.1.1.2 christos the gdbarch from the initial disassembly request. */ 74 1.1.1.2 christos struct gdbarch *gdbarch; 75 1.1.1.2 christos }; 76 1.1.1.2 christos 77 1.1.1.2 christos extern PyTypeObject disasm_addr_part_object_type 78 1.1.1.2 christos CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("disasm_addr_part_object"); 79 1.1.1.2 christos 80 1.1.1.2 christos /* Implement gdb.disassembler.DisassembleTextPart type. An object of 81 1.1.1.2 christos this type represents a small part of a disassembled instruction; a part 82 1.1.1.2 christos that is a piece of test along with an associated style. */ 83 1.1.1.2 christos 84 1.1.1.2 christos struct disasm_text_part_object 85 1.1.1.2 christos { 86 1.1.1.2 christos PyObject_HEAD 87 1.1.1.2 christos 88 1.1.1.2 christos /* The string that is this part. */ 89 1.1.1.2 christos std::string *string; 90 1.1.1.2 christos 91 1.1.1.2 christos /* The style to use when displaying this part. */ 92 1.1.1.2 christos enum disassembler_style style; 93 1.1.1.2 christos }; 94 1.1.1.2 christos 95 1.1.1.2 christos extern PyTypeObject disasm_text_part_object_type 96 1.1.1.2 christos CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("disasm_text_part_object"); 97 1.1.1.2 christos 98 1.1.1.2 christos extern PyTypeObject disasm_part_object_type 99 1.1.1.2 christos CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("PyObject"); 100 1.1.1.2 christos 101 1.1 christos /* Implement gdb.disassembler.DisassemblerResult type, an object that holds 102 1.1 christos the result of calling the disassembler. This is mostly the length of 103 1.1 christos the disassembled instruction (in bytes), and the string representing the 104 1.1 christos disassembled instruction. */ 105 1.1 christos 106 1.1 christos struct disasm_result_object 107 1.1 christos { 108 1.1 christos PyObject_HEAD 109 1.1 christos 110 1.1 christos /* The length of the disassembled instruction in bytes. */ 111 1.1 christos int length; 112 1.1 christos 113 1.1.1.2 christos /* A vector containing all the parts of the disassembled instruction. 114 1.1.1.2 christos Each part will be a DisassemblerPart sub-class. */ 115 1.1.1.2 christos std::vector<gdbpy_ref<>> *parts; 116 1.1 christos }; 117 1.1 christos 118 1.1 christos extern PyTypeObject disasm_result_object_type 119 1.1 christos CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("disasm_result_object"); 120 1.1 christos 121 1.1 christos /* When this is false we fast path out of gdbpy_print_insn, which should 122 1.1 christos keep the performance impact of the Python disassembler down. This is 123 1.1 christos set to true from Python by calling gdb.disassembler._set_enabled() when 124 1.1 christos the user registers a disassembler. */ 125 1.1 christos 126 1.1 christos static bool python_print_insn_enabled = false; 127 1.1 christos 128 1.1 christos /* A sub-class of gdb_disassembler that holds a pointer to a Python 129 1.1 christos DisassembleInfo object. A pointer to an instance of this class is 130 1.1 christos placed in the application_data field of the disassemble_info that is 131 1.1 christos used when we call gdbarch_print_insn. */ 132 1.1 christos 133 1.1.1.2 christos struct gdbpy_disassembler : public gdb_disassemble_info 134 1.1 christos { 135 1.1 christos /* Constructor. */ 136 1.1 christos gdbpy_disassembler (disasm_info_object *obj, PyObject *memory_source); 137 1.1 christos 138 1.1 christos /* Get the DisassembleInfo object pointer. */ 139 1.1 christos disasm_info_object * 140 1.1 christos py_disasm_info () const 141 1.1 christos { 142 1.1 christos return m_disasm_info_object; 143 1.1 christos } 144 1.1 christos 145 1.1 christos /* Callbacks used by disassemble_info. */ 146 1.1 christos static void memory_error_func (int status, bfd_vma memaddr, 147 1.1 christos struct disassemble_info *info) noexcept; 148 1.1 christos static void print_address_func (bfd_vma addr, 149 1.1 christos struct disassemble_info *info) noexcept; 150 1.1 christos static int read_memory_func (bfd_vma memaddr, gdb_byte *buff, 151 1.1 christos unsigned int len, 152 1.1 christos struct disassemble_info *info) noexcept; 153 1.1 christos 154 1.1.1.2 christos /* Callback used as the disassemble_info's fprintf_func callback. The 155 1.1.1.2 christos DIS_INFO pointer is a pointer to a gdbpy_disassembler object. */ 156 1.1.1.2 christos static int fprintf_func (void *dis_info, const char *format, ...) noexcept 157 1.1.1.2 christos ATTRIBUTE_PRINTF(2,3); 158 1.1.1.2 christos 159 1.1.1.2 christos /* Callback used as the disassemble_info's fprintf_styled_func callback. 160 1.1.1.2 christos The DIS_INFO pointer is a pointer to a gdbpy_disassembler. */ 161 1.1.1.2 christos static int fprintf_styled_func (void *dis_info, 162 1.1.1.2 christos enum disassembler_style style, 163 1.1.1.2 christos const char *format, ...) noexcept 164 1.1.1.2 christos ATTRIBUTE_PRINTF(3,4); 165 1.1.1.2 christos 166 1.1.1.2 christos /* Helper used by fprintf_func and fprintf_styled_func. This function 167 1.1.1.2 christos creates a new DisassemblerTextPart and adds it to the disassembler's 168 1.1.1.2 christos parts list. The actual disassembler is accessed through DIS_INFO, 169 1.1.1.2 christos which is a pointer to the gdbpy_disassembler object. */ 170 1.1.1.2 christos static int vfprintf_styled_func (void *dis_info, 171 1.1.1.2 christos enum disassembler_style style, 172 1.1.1.2 christos const char *format, va_list args) noexcept 173 1.1.1.2 christos ATTRIBUTE_PRINTF(3,0); 174 1.1.1.2 christos 175 1.1 christos /* Return a reference to an optional that contains the address at which a 176 1.1 christos memory error occurred. The optional will only have a value if a 177 1.1 christos memory error actually occurred. */ 178 1.1.1.2 christos const std::optional<CORE_ADDR> &memory_error_address () const 179 1.1 christos { return m_memory_error_address; } 180 1.1 christos 181 1.1 christos /* Return the content of the disassembler as a string. The contents are 182 1.1 christos moved out of the disassembler, so after this call the disassembler 183 1.1 christos contents have been reset back to empty. */ 184 1.1.1.2 christos std::vector<gdbpy_ref<>> release () 185 1.1 christos { 186 1.1.1.2 christos return std::move (m_parts); 187 1.1 christos } 188 1.1 christos 189 1.1 christos /* If there is a Python exception stored in this disassembler then 190 1.1 christos restore it (i.e. set the PyErr_* state), clear the exception within 191 1.1 christos this disassembler, and return true. There must be no current 192 1.1 christos exception set (i.e. !PyErr_Occurred()) when this function is called, 193 1.1 christos as any such exception might get lost. 194 1.1 christos 195 1.1 christos Otherwise, there is no exception stored in this disassembler, return 196 1.1 christos false. */ 197 1.1 christos bool restore_exception () 198 1.1 christos { 199 1.1 christos gdb_assert (!PyErr_Occurred ()); 200 1.1 christos if (m_stored_exception.has_value ()) 201 1.1 christos { 202 1.1 christos gdbpy_err_fetch ex = std::move (*m_stored_exception); 203 1.1 christos m_stored_exception.reset (); 204 1.1 christos ex.restore (); 205 1.1 christos return true; 206 1.1 christos } 207 1.1 christos 208 1.1 christos return false; 209 1.1 christos } 210 1.1 christos 211 1.1 christos private: 212 1.1 christos 213 1.1.1.2 christos /* The list of all the parts that make up this disassembled instruction. 214 1.1.1.2 christos This is populated as a result of the callbacks from libopcodes as the 215 1.1.1.2 christos instruction is disassembled. */ 216 1.1.1.2 christos std::vector<gdbpy_ref<>> m_parts; 217 1.1 christos 218 1.1 christos /* The DisassembleInfo object we are disassembling for. */ 219 1.1 christos disasm_info_object *m_disasm_info_object; 220 1.1 christos 221 1.1 christos /* When the user indicates that a memory error has occurred then the 222 1.1 christos address of the memory error is stored in here. */ 223 1.1.1.2 christos std::optional<CORE_ADDR> m_memory_error_address; 224 1.1 christos 225 1.1 christos /* When the user calls the builtin_disassemble function, if they pass a 226 1.1 christos memory source object then a pointer to the object is placed in here, 227 1.1 christos otherwise, this field is nullptr. */ 228 1.1 christos PyObject *m_memory_source; 229 1.1 christos 230 1.1 christos /* Move the exception EX into this disassembler object. */ 231 1.1 christos void store_exception (gdbpy_err_fetch &&ex) 232 1.1 christos { 233 1.1 christos /* The only calls to store_exception are from read_memory_func, which 234 1.1 christos will return early if there's already an exception stored. */ 235 1.1 christos gdb_assert (!m_stored_exception.has_value ()); 236 1.1 christos m_stored_exception.emplace (std::move (ex)); 237 1.1 christos } 238 1.1 christos 239 1.1 christos /* Return true if there is an exception stored in this disassembler. */ 240 1.1 christos bool has_stored_exception () const 241 1.1 christos { 242 1.1 christos return m_stored_exception.has_value (); 243 1.1 christos } 244 1.1 christos 245 1.1 christos /* Store a single exception. This is used to pass Python exceptions back 246 1.1 christos from ::memory_read to disasmpy_builtin_disassemble. */ 247 1.1.1.2 christos std::optional<gdbpy_err_fetch> m_stored_exception; 248 1.1 christos }; 249 1.1 christos 250 1.1 christos /* Return true if OBJ is still valid, otherwise, return false. A valid OBJ 251 1.1 christos will have a non-nullptr gdb_info field. */ 252 1.1 christos 253 1.1 christos static bool 254 1.1 christos disasm_info_object_is_valid (disasm_info_object *obj) 255 1.1 christos { 256 1.1 christos return obj->gdb_info != nullptr; 257 1.1 christos } 258 1.1 christos 259 1.1 christos /* Fill in OBJ with all the other arguments. */ 260 1.1 christos 261 1.1 christos static void 262 1.1 christos disasm_info_fill (disasm_info_object *obj, struct gdbarch *gdbarch, 263 1.1 christos program_space *progspace, bfd_vma address, 264 1.1 christos disassemble_info *di, disasm_info_object *next) 265 1.1 christos { 266 1.1 christos obj->gdbarch = gdbarch; 267 1.1 christos obj->program_space = progspace; 268 1.1 christos obj->address = address; 269 1.1 christos obj->gdb_info = di; 270 1.1 christos obj->next = next; 271 1.1 christos } 272 1.1 christos 273 1.1 christos /* Implement DisassembleInfo.__init__. Takes a single argument that must 274 1.1 christos be another DisassembleInfo object and copies the contents from the 275 1.1 christos argument into this new object. */ 276 1.1 christos 277 1.1 christos static int 278 1.1 christos disasm_info_init (PyObject *self, PyObject *args, PyObject *kwargs) 279 1.1 christos { 280 1.1 christos static const char *keywords[] = { "info", NULL }; 281 1.1 christos PyObject *info_obj; 282 1.1 christos if (!gdb_PyArg_ParseTupleAndKeywords (args, kwargs, "O!", keywords, 283 1.1 christos &disasm_info_object_type, 284 1.1 christos &info_obj)) 285 1.1 christos return -1; 286 1.1 christos 287 1.1 christos disasm_info_object *other = (disasm_info_object *) info_obj; 288 1.1 christos disasm_info_object *info = (disasm_info_object *) self; 289 1.1 christos disasm_info_fill (info, other->gdbarch, other->program_space, 290 1.1 christos other->address, other->gdb_info, other->next); 291 1.1 christos other->next = info; 292 1.1 christos 293 1.1 christos /* As the OTHER object now holds a pointer to INFO we inc the ref count 294 1.1 christos on INFO. This stops INFO being deleted until OTHER has gone away. */ 295 1.1 christos Py_INCREF ((PyObject *) info); 296 1.1 christos return 0; 297 1.1 christos } 298 1.1 christos 299 1.1 christos /* The tp_dealloc callback for the DisassembleInfo type. */ 300 1.1 christos 301 1.1 christos static void 302 1.1 christos disasm_info_dealloc (PyObject *self) 303 1.1 christos { 304 1.1 christos disasm_info_object *obj = (disasm_info_object *) self; 305 1.1 christos 306 1.1 christos /* We no longer care about the object our NEXT pointer points at, so we 307 1.1 christos can decrement its reference count. This macro handles the case when 308 1.1 christos NEXT is nullptr. */ 309 1.1 christos Py_XDECREF ((PyObject *) obj->next); 310 1.1 christos 311 1.1 christos /* Now core deallocation behaviour. */ 312 1.1 christos Py_TYPE (self)->tp_free (self); 313 1.1 christos } 314 1.1 christos 315 1.1.1.2 christos /* Implement __repr__ for the DisassembleInfo type. */ 316 1.1.1.2 christos 317 1.1.1.2 christos static PyObject * 318 1.1.1.2 christos disasmpy_info_repr (PyObject *self) 319 1.1.1.2 christos { 320 1.1.1.2 christos disasm_info_object *obj = (disasm_info_object *) self; 321 1.1.1.2 christos 322 1.1.1.2 christos const char *arch_name 323 1.1.1.2 christos = (gdbarch_bfd_arch_info (obj->gdbarch))->printable_name; 324 1.1.1.2 christos return PyUnicode_FromFormat ("<%s address=%s architecture=%s>", 325 1.1.1.2 christos Py_TYPE (obj)->tp_name, 326 1.1.1.2 christos core_addr_to_string_nz (obj->address), 327 1.1.1.2 christos arch_name); 328 1.1.1.2 christos } 329 1.1.1.2 christos 330 1.1 christos /* Implement DisassembleInfo.is_valid(), really just a wrapper around the 331 1.1 christos disasm_info_object_is_valid function above. */ 332 1.1 christos 333 1.1 christos static PyObject * 334 1.1 christos disasmpy_info_is_valid (PyObject *self, PyObject *args) 335 1.1 christos { 336 1.1 christos disasm_info_object *disasm_obj = (disasm_info_object *) self; 337 1.1 christos 338 1.1 christos if (disasm_info_object_is_valid (disasm_obj)) 339 1.1 christos Py_RETURN_TRUE; 340 1.1 christos 341 1.1 christos Py_RETURN_FALSE; 342 1.1 christos } 343 1.1 christos 344 1.1 christos /* Set the Python exception to be a gdb.MemoryError object, with ADDRESS 345 1.1 christos as its payload. */ 346 1.1 christos 347 1.1 christos static void 348 1.1 christos disasmpy_set_memory_error_for_address (CORE_ADDR address) 349 1.1 christos { 350 1.1 christos PyObject *address_obj = gdb_py_object_from_longest (address).release (); 351 1.1 christos PyErr_SetObject (gdbpy_gdb_memory_error, address_obj); 352 1.1 christos } 353 1.1 christos 354 1.1.1.2 christos /* Create a new DisassemblerTextPart and return a gdbpy_ref wrapper for 355 1.1.1.2 christos the new object. STR is the string content of the part and STYLE is the 356 1.1.1.2 christos style to be used when GDB displays this part. */ 357 1.1.1.2 christos 358 1.1.1.2 christos static gdbpy_ref<> 359 1.1.1.2 christos make_disasm_text_part (std::string &&str, enum disassembler_style style) 360 1.1.1.2 christos { 361 1.1.1.2 christos PyTypeObject *type = &disasm_text_part_object_type; 362 1.1.1.2 christos disasm_text_part_object *text_part 363 1.1.1.2 christos = (disasm_text_part_object *) type->tp_alloc (type, 0); 364 1.1.1.2 christos text_part->string = new std::string (str); 365 1.1.1.2 christos text_part->style = style; 366 1.1.1.2 christos 367 1.1.1.2 christos return gdbpy_ref<> ((PyObject *) text_part); 368 1.1.1.2 christos } 369 1.1.1.2 christos 370 1.1.1.2 christos /* Create a new DisassemblerAddressPart and return a gdbpy_ref wrapper for 371 1.1.1.2 christos the new object. GDBARCH is the architecture used when formatting the 372 1.1.1.2 christos address, and ADDRESS is the numerical address to be displayed. */ 373 1.1.1.2 christos 374 1.1.1.2 christos static gdbpy_ref<> 375 1.1.1.2 christos make_disasm_addr_part (struct gdbarch *gdbarch, CORE_ADDR address) 376 1.1.1.2 christos { 377 1.1.1.2 christos PyTypeObject *type = &disasm_addr_part_object_type; 378 1.1.1.2 christos disasm_addr_part_object *addr_part 379 1.1.1.2 christos = (disasm_addr_part_object *) type->tp_alloc (type, 0); 380 1.1.1.2 christos addr_part->address = address; 381 1.1.1.2 christos addr_part->gdbarch = gdbarch; 382 1.1.1.2 christos 383 1.1.1.2 christos return gdbpy_ref<> ((PyObject *) addr_part); 384 1.1.1.2 christos } 385 1.1.1.2 christos 386 1.1 christos /* Ensure that a gdb.disassembler.DisassembleInfo is valid. */ 387 1.1 christos 388 1.1 christos #define DISASMPY_DISASM_INFO_REQUIRE_VALID(Info) \ 389 1.1 christos do { \ 390 1.1 christos if (!disasm_info_object_is_valid (Info)) \ 391 1.1 christos { \ 392 1.1 christos PyErr_SetString (PyExc_RuntimeError, \ 393 1.1 christos _("DisassembleInfo is no longer valid.")); \ 394 1.1 christos return nullptr; \ 395 1.1 christos } \ 396 1.1 christos } while (0) 397 1.1 christos 398 1.1.1.2 christos /* Implement DisassembleInfo.text_part method. Creates and returns a new 399 1.1.1.2 christos DisassemblerTextPart object. */ 400 1.1.1.2 christos 401 1.1.1.2 christos static PyObject * 402 1.1.1.2 christos disasmpy_info_make_text_part (PyObject *self, PyObject *args, 403 1.1.1.2 christos PyObject *kwargs) 404 1.1.1.2 christos { 405 1.1.1.2 christos disasm_info_object *obj = (disasm_info_object *) self; 406 1.1.1.2 christos DISASMPY_DISASM_INFO_REQUIRE_VALID (obj); 407 1.1.1.2 christos 408 1.1.1.2 christos static const char *keywords[] = { "style", "string", NULL }; 409 1.1.1.2 christos int style_num; 410 1.1.1.2 christos const char *string; 411 1.1.1.2 christos if (!gdb_PyArg_ParseTupleAndKeywords (args, kwargs, "is", keywords, 412 1.1.1.2 christos &style_num, &string)) 413 1.1.1.2 christos return nullptr; 414 1.1.1.2 christos 415 1.1.1.2 christos if (style_num < 0 || style_num > ((int) dis_style_comment_start)) 416 1.1.1.2 christos { 417 1.1.1.2 christos PyErr_SetString (PyExc_ValueError, 418 1.1.1.2 christos _("Invalid disassembler style.")); 419 1.1.1.2 christos return nullptr; 420 1.1.1.2 christos } 421 1.1.1.2 christos 422 1.1.1.2 christos if (strlen (string) == 0) 423 1.1.1.2 christos { 424 1.1.1.2 christos PyErr_SetString (PyExc_ValueError, 425 1.1.1.2 christos _("String must not be empty.")); 426 1.1.1.2 christos return nullptr; 427 1.1.1.2 christos } 428 1.1.1.2 christos 429 1.1.1.2 christos gdbpy_ref<> text_part 430 1.1.1.2 christos = make_disasm_text_part (std::string (string), 431 1.1.1.2 christos (enum disassembler_style) style_num); 432 1.1.1.2 christos return text_part.release (); 433 1.1.1.2 christos } 434 1.1.1.2 christos 435 1.1.1.2 christos /* Implement DisassembleInfo.address_part method. Creates and returns a 436 1.1.1.2 christos new DisassemblerAddressPart object. */ 437 1.1.1.2 christos 438 1.1.1.2 christos static PyObject * 439 1.1.1.2 christos disasmpy_info_make_address_part (PyObject *self, PyObject *args, 440 1.1.1.2 christos PyObject *kwargs) 441 1.1.1.2 christos { 442 1.1.1.2 christos disasm_info_object *obj = (disasm_info_object *) self; 443 1.1.1.2 christos DISASMPY_DISASM_INFO_REQUIRE_VALID (obj); 444 1.1.1.2 christos 445 1.1.1.2 christos static const char *keywords[] = { "address", NULL }; 446 1.1.1.2 christos CORE_ADDR address; 447 1.1.1.2 christos PyObject *address_object; 448 1.1.1.2 christos if (!gdb_PyArg_ParseTupleAndKeywords (args, kwargs, "O", keywords, 449 1.1.1.2 christos &address_object)) 450 1.1.1.2 christos return nullptr; 451 1.1.1.2 christos 452 1.1.1.2 christos if (get_addr_from_python (address_object, &address) < 0) 453 1.1.1.2 christos return nullptr; 454 1.1.1.2 christos 455 1.1.1.2 christos return make_disasm_addr_part (obj->gdbarch, address).release (); 456 1.1.1.2 christos } 457 1.1.1.2 christos 458 1.1.1.2 christos /* Return a string representation of TEXT_PART. The returned string does 459 1.1.1.2 christos not include any styling. */ 460 1.1.1.2 christos 461 1.1.1.2 christos static std::string 462 1.1.1.2 christos disasmpy_part_to_string (const disasm_text_part_object *text_part) 463 1.1.1.2 christos { 464 1.1.1.2 christos gdb_assert (text_part->string != nullptr); 465 1.1.1.2 christos return *(text_part->string); 466 1.1.1.2 christos } 467 1.1.1.2 christos 468 1.1.1.2 christos /* Return a string representation of ADDR_PART. The returned string does 469 1.1.1.2 christos not include any styling. */ 470 1.1.1.2 christos 471 1.1.1.2 christos static std::string 472 1.1.1.2 christos disasmpy_part_to_string (const disasm_addr_part_object *addr_part) 473 1.1.1.2 christos { 474 1.1.1.2 christos string_file buf; 475 1.1.1.2 christos print_address (addr_part->gdbarch, addr_part->address, &buf); 476 1.1.1.2 christos return buf.release (); 477 1.1.1.2 christos } 478 1.1.1.2 christos 479 1.1.1.2 christos /* PARTS is a vector of Python objects, each is a sub-class of 480 1.1.1.2 christos DisassemblerPart. Create a string by concatenating the string 481 1.1.1.2 christos representation of each part, and return this new string. 482 1.1.1.2 christos 483 1.1.1.2 christos Converting an address part requires that we call back into GDB core, 484 1.1.1.2 christos which could throw an exception. As such, calls to this function should 485 1.1.1.2 christos be wrapped with a try/catch. */ 486 1.1.1.2 christos 487 1.1.1.2 christos static std::string 488 1.1.1.2 christos disasmpy_parts_list_to_string (const std::vector<gdbpy_ref<>> &parts) 489 1.1.1.2 christos { 490 1.1.1.2 christos std::string str; 491 1.1.1.2 christos for (auto p : parts) 492 1.1.1.2 christos { 493 1.1.1.2 christos if (Py_TYPE (p.get ()) == &disasm_text_part_object_type) 494 1.1.1.2 christos { 495 1.1.1.2 christos disasm_text_part_object *text_part 496 1.1.1.2 christos = (disasm_text_part_object *) p.get (); 497 1.1.1.2 christos str += disasmpy_part_to_string (text_part); 498 1.1.1.2 christos } 499 1.1.1.2 christos else 500 1.1.1.2 christos { 501 1.1.1.2 christos gdb_assert (Py_TYPE (p.get ()) == &disasm_addr_part_object_type); 502 1.1.1.2 christos 503 1.1.1.2 christos disasm_addr_part_object *addr_part 504 1.1.1.2 christos = (disasm_addr_part_object *) p.get (); 505 1.1.1.2 christos str += disasmpy_part_to_string (addr_part); 506 1.1.1.2 christos } 507 1.1.1.2 christos } 508 1.1.1.2 christos 509 1.1.1.2 christos return str; 510 1.1.1.2 christos } 511 1.1.1.2 christos 512 1.1.1.2 christos /* Initialise OBJ, a DisassemblerResult object with LENGTH and PARTS. 513 1.1 christos OBJ might already have been initialised, in which case any existing 514 1.1.1.2 christos content should be discarded before the new PARTS are moved in. */ 515 1.1 christos 516 1.1 christos static void 517 1.1 christos disasmpy_init_disassembler_result (disasm_result_object *obj, int length, 518 1.1.1.2 christos std::vector<gdbpy_ref<>> &&parts) 519 1.1 christos { 520 1.1.1.2 christos if (obj->parts == nullptr) 521 1.1.1.2 christos obj->parts = new std::vector<gdbpy_ref<>>; 522 1.1 christos else 523 1.1.1.2 christos obj->parts->clear (); 524 1.1 christos 525 1.1 christos obj->length = length; 526 1.1.1.2 christos *(obj->parts) = std::move (parts); 527 1.1 christos } 528 1.1 christos 529 1.1 christos /* Implement gdb.disassembler.builtin_disassemble(). Calls back into GDB's 530 1.1 christos builtin disassembler. The first argument is a DisassembleInfo object 531 1.1 christos describing what to disassemble. The second argument is optional and 532 1.1 christos provides a mechanism to modify the memory contents that the builtin 533 1.1 christos disassembler will actually disassemble. 534 1.1 christos 535 1.1 christos Returns an instance of gdb.disassembler.DisassemblerResult, an object 536 1.1 christos that wraps a disassembled instruction, or it raises a 537 1.1 christos gdb.MemoryError. */ 538 1.1 christos 539 1.1 christos static PyObject * 540 1.1 christos disasmpy_builtin_disassemble (PyObject *self, PyObject *args, PyObject *kw) 541 1.1 christos { 542 1.1 christos PyObject *info_obj, *memory_source_obj = nullptr; 543 1.1 christos static const char *keywords[] = { "info", "memory_source", nullptr }; 544 1.1 christos if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "O!|O", keywords, 545 1.1 christos &disasm_info_object_type, &info_obj, 546 1.1 christos &memory_source_obj)) 547 1.1 christos return nullptr; 548 1.1 christos 549 1.1 christos disasm_info_object *disasm_info = (disasm_info_object *) info_obj; 550 1.1 christos DISASMPY_DISASM_INFO_REQUIRE_VALID (disasm_info); 551 1.1 christos 552 1.1 christos /* Where the result will be written. */ 553 1.1 christos gdbpy_disassembler disassembler (disasm_info, memory_source_obj); 554 1.1 christos 555 1.1 christos /* Now actually perform the disassembly. LENGTH is set to the length of 556 1.1 christos the disassembled instruction, or -1 if there was a memory-error 557 1.1 christos encountered while disassembling. See below more more details on 558 1.1 christos handling of -1 return value. */ 559 1.1 christos int length = gdbarch_print_insn (disasm_info->gdbarch, disasm_info->address, 560 1.1 christos disassembler.disasm_info ()); 561 1.1 christos 562 1.1 christos /* It is possible that, while calling a user overridden memory read 563 1.1 christos function, a Python exception was raised that couldn't be 564 1.1 christos translated into a standard memory-error. In this case the first such 565 1.1 christos exception is stored in the disassembler and restored here. */ 566 1.1 christos if (disassembler.restore_exception ()) 567 1.1 christos return nullptr; 568 1.1 christos 569 1.1 christos if (length == -1) 570 1.1 christos { 571 1.1 christos 572 1.1 christos /* In an ideal world, every disassembler should always call the 573 1.1 christos memory error function before returning a status of -1 as the only 574 1.1 christos error a disassembler should encounter is a failure to read 575 1.1 christos memory. Unfortunately, there are some disassemblers who don't 576 1.1 christos follow this rule, and will return -1 without calling the memory 577 1.1 christos error function. 578 1.1 christos 579 1.1 christos To make the Python API simpler, we just classify everything as a 580 1.1 christos memory error, but the message has to be modified for the case 581 1.1 christos where the disassembler didn't call the memory error function. */ 582 1.1 christos if (disassembler.memory_error_address ().has_value ()) 583 1.1 christos { 584 1.1 christos CORE_ADDR addr = *disassembler.memory_error_address (); 585 1.1 christos disasmpy_set_memory_error_for_address (addr); 586 1.1 christos } 587 1.1 christos else 588 1.1 christos { 589 1.1.1.2 christos auto content = disassembler.release (); 590 1.1.1.2 christos std::string str; 591 1.1.1.2 christos 592 1.1.1.2 christos try 593 1.1.1.2 christos { 594 1.1.1.2 christos str = disasmpy_parts_list_to_string (content); 595 1.1.1.2 christos } 596 1.1.1.2 christos catch (const gdb_exception &except) 597 1.1.1.2 christos { 598 1.1.1.2 christos GDB_PY_HANDLE_EXCEPTION (except); 599 1.1.1.2 christos } 600 1.1.1.2 christos if (!str.empty ()) 601 1.1.1.2 christos PyErr_SetString (gdbpy_gdberror_exc, str.c_str ()); 602 1.1 christos else 603 1.1 christos PyErr_SetString (gdbpy_gdberror_exc, 604 1.1 christos _("Unknown disassembly error.")); 605 1.1 christos } 606 1.1 christos return nullptr; 607 1.1 christos } 608 1.1 christos 609 1.1 christos /* Instructions are either non-zero in length, or we got an error, 610 1.1 christos indicated by a length of -1, which we handled above. */ 611 1.1 christos gdb_assert (length > 0); 612 1.1 christos 613 1.1 christos /* We should not have seen a memory error in this case. */ 614 1.1 christos gdb_assert (!disassembler.memory_error_address ().has_value ()); 615 1.1 christos 616 1.1 christos /* Create a DisassemblerResult containing the results. */ 617 1.1 christos PyTypeObject *type = &disasm_result_object_type; 618 1.1 christos gdbpy_ref<disasm_result_object> res 619 1.1 christos ((disasm_result_object *) type->tp_alloc (type, 0)); 620 1.1.1.2 christos auto content = disassembler.release (); 621 1.1 christos disasmpy_init_disassembler_result (res.get (), length, std::move (content)); 622 1.1 christos return reinterpret_cast<PyObject *> (res.release ()); 623 1.1 christos } 624 1.1 christos 625 1.1 christos /* Implement gdb._set_enabled function. Takes a boolean parameter, and 626 1.1 christos sets whether GDB should enter the Python disassembler code or not. 627 1.1 christos 628 1.1 christos This is called from within the Python code when a new disassembler is 629 1.1 christos registered. When no disassemblers are registered the global C++ flag 630 1.1 christos is set to false, and GDB never even enters the Python environment to 631 1.1 christos check for a disassembler. 632 1.1 christos 633 1.1 christos When the user registers a new Python disassembler, the global C++ flag 634 1.1 christos is set to true, and now GDB will enter the Python environment to check 635 1.1 christos if there's a disassembler registered for the current architecture. */ 636 1.1 christos 637 1.1 christos static PyObject * 638 1.1 christos disasmpy_set_enabled (PyObject *self, PyObject *args, PyObject *kw) 639 1.1 christos { 640 1.1 christos PyObject *newstate; 641 1.1 christos static const char *keywords[] = { "state", nullptr }; 642 1.1 christos if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "O", keywords, 643 1.1 christos &newstate)) 644 1.1 christos return nullptr; 645 1.1 christos 646 1.1 christos if (!PyBool_Check (newstate)) 647 1.1 christos { 648 1.1 christos PyErr_SetString (PyExc_TypeError, 649 1.1 christos _("The value passed to `_set_enabled' must be a boolean.")); 650 1.1 christos return nullptr; 651 1.1 christos } 652 1.1 christos 653 1.1 christos python_print_insn_enabled = PyObject_IsTrue (newstate); 654 1.1 christos Py_RETURN_NONE; 655 1.1 christos } 656 1.1 christos 657 1.1 christos /* Implement DisassembleInfo.read_memory(LENGTH, OFFSET). Read LENGTH 658 1.1 christos bytes at OFFSET from the start of the instruction currently being 659 1.1 christos disassembled, and return a memory buffer containing the bytes. 660 1.1 christos 661 1.1 christos OFFSET defaults to zero if it is not provided. LENGTH is required. If 662 1.1 christos the read fails then this will raise a gdb.MemoryError exception. */ 663 1.1 christos 664 1.1 christos static PyObject * 665 1.1 christos disasmpy_info_read_memory (PyObject *self, PyObject *args, PyObject *kw) 666 1.1 christos { 667 1.1 christos disasm_info_object *obj = (disasm_info_object *) self; 668 1.1 christos DISASMPY_DISASM_INFO_REQUIRE_VALID (obj); 669 1.1 christos 670 1.1.1.2 christos gdb_py_longest length, offset = 0; 671 1.1 christos gdb::unique_xmalloc_ptr<gdb_byte> buffer; 672 1.1 christos static const char *keywords[] = { "length", "offset", nullptr }; 673 1.1 christos 674 1.1.1.2 christos if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, 675 1.1.1.2 christos GDB_PY_LL_ARG "|" GDB_PY_LL_ARG, 676 1.1.1.2 christos keywords, &length, &offset)) 677 1.1 christos return nullptr; 678 1.1 christos 679 1.1 christos /* The apparent address from which we are reading memory. Note that in 680 1.1 christos some cases GDB actually disassembles instructions from a buffer, so 681 1.1 christos we might not actually be reading this information directly from the 682 1.1 christos inferior memory. This is all hidden behind the read_memory_func API 683 1.1 christos within the disassemble_info structure. */ 684 1.1 christos CORE_ADDR address = obj->address + offset; 685 1.1 christos 686 1.1 christos /* Setup a buffer to hold the result. */ 687 1.1 christos buffer.reset ((gdb_byte *) xmalloc (length)); 688 1.1 christos 689 1.1 christos /* Read content into BUFFER. If the read fails then raise a memory 690 1.1 christos error, otherwise, convert BUFFER to a Python memory buffer, and return 691 1.1 christos it to the user. */ 692 1.1 christos disassemble_info *info = obj->gdb_info; 693 1.1 christos if (info->read_memory_func ((bfd_vma) address, buffer.get (), 694 1.1 christos (unsigned int) length, info) != 0) 695 1.1 christos { 696 1.1 christos disasmpy_set_memory_error_for_address (address); 697 1.1 christos return nullptr; 698 1.1 christos } 699 1.1 christos return gdbpy_buffer_to_membuf (std::move (buffer), address, length); 700 1.1 christos } 701 1.1 christos 702 1.1 christos /* Implement DisassembleInfo.address attribute, return the address at which 703 1.1 christos GDB would like an instruction disassembled. */ 704 1.1 christos 705 1.1 christos static PyObject * 706 1.1 christos disasmpy_info_address (PyObject *self, void *closure) 707 1.1 christos { 708 1.1 christos disasm_info_object *obj = (disasm_info_object *) self; 709 1.1 christos DISASMPY_DISASM_INFO_REQUIRE_VALID (obj); 710 1.1 christos return gdb_py_object_from_longest (obj->address).release (); 711 1.1 christos } 712 1.1 christos 713 1.1 christos /* Implement DisassembleInfo.architecture attribute. Return the 714 1.1 christos gdb.Architecture in which we are disassembling. */ 715 1.1 christos 716 1.1 christos static PyObject * 717 1.1 christos disasmpy_info_architecture (PyObject *self, void *closure) 718 1.1 christos { 719 1.1 christos disasm_info_object *obj = (disasm_info_object *) self; 720 1.1 christos DISASMPY_DISASM_INFO_REQUIRE_VALID (obj); 721 1.1 christos return gdbarch_to_arch_object (obj->gdbarch); 722 1.1 christos } 723 1.1 christos 724 1.1 christos /* Implement DisassembleInfo.progspace attribute. Return the 725 1.1 christos gdb.Progspace in which we are disassembling. */ 726 1.1 christos 727 1.1 christos static PyObject * 728 1.1 christos disasmpy_info_progspace (PyObject *self, void *closure) 729 1.1 christos { 730 1.1 christos disasm_info_object *obj = (disasm_info_object *) self; 731 1.1 christos DISASMPY_DISASM_INFO_REQUIRE_VALID (obj); 732 1.1 christos return pspace_to_pspace_object (obj->program_space).release (); 733 1.1 christos } 734 1.1 christos 735 1.1.1.2 christos /* Helper function called when the libopcodes disassembler produces some 736 1.1.1.2 christos output. FORMAT and ARGS are used to create a string which GDB will 737 1.1.1.2 christos display using STYLE. The string is either added as a new 738 1.1.1.2 christos DisassemblerTextPart to the list of parts being built in the current 739 1.1.1.2 christos gdbpy_disassembler object (accessed through DIS_INFO). Or, if the last 740 1.1.1.2 christos part in the gdbpy_disassembler is a text part in the same STYLE, then 741 1.1.1.2 christos the new string is appended to the previous part. 742 1.1.1.2 christos 743 1.1.1.2 christos The merging behaviour make the Python API a little more user friendly, 744 1.1.1.2 christos some disassemblers produce their output character at a time, there's no 745 1.1.1.2 christos particular reason for this, it's just how they are implemented. By 746 1.1.1.2 christos merging parts with the same style we make it easier for the user to 747 1.1.1.2 christos analyse the disassembler output. */ 748 1.1.1.2 christos 749 1.1.1.2 christos int 750 1.1.1.2 christos gdbpy_disassembler::vfprintf_styled_func (void *dis_info, 751 1.1.1.2 christos enum disassembler_style style, 752 1.1.1.2 christos const char *format, 753 1.1.1.2 christos va_list args) noexcept 754 1.1.1.2 christos { 755 1.1.1.2 christos gdb_disassemble_info *di = (gdb_disassemble_info *) dis_info; 756 1.1.1.2 christos gdbpy_disassembler *dis 757 1.1.1.2 christos = gdb::checked_static_cast<gdbpy_disassembler *> (di); 758 1.1.1.2 christos 759 1.1.1.2 christos if (!dis->m_parts.empty () 760 1.1.1.2 christos && Py_TYPE (dis->m_parts.back ().get ()) == &disasm_text_part_object_type 761 1.1.1.2 christos && (((disasm_text_part_object *) dis->m_parts.back ().get ())->style 762 1.1.1.2 christos == style)) 763 1.1.1.2 christos { 764 1.1.1.2 christos std::string *string 765 1.1.1.2 christos = ((disasm_text_part_object *) dis->m_parts.back ().get ())->string; 766 1.1.1.2 christos string_vappendf (*string, format, args); 767 1.1.1.2 christos } 768 1.1.1.2 christos else 769 1.1.1.2 christos { 770 1.1.1.2 christos std::string str = string_vprintf (format, args); 771 1.1.1.2 christos if (str.size () > 0) 772 1.1.1.2 christos { 773 1.1.1.2 christos gdbpy_ref<> text_part 774 1.1.1.2 christos = make_disasm_text_part (std::move (str), style); 775 1.1.1.2 christos dis->m_parts.emplace_back (std::move (text_part)); 776 1.1.1.2 christos } 777 1.1.1.2 christos } 778 1.1.1.2 christos 779 1.1.1.2 christos /* Something non -ve. */ 780 1.1.1.2 christos return 0; 781 1.1.1.2 christos } 782 1.1.1.2 christos 783 1.1.1.2 christos /* Disassembler callback for architectures where libopcodes doesn't 784 1.1.1.2 christos created styled output. In these cases we format all the output using 785 1.1.1.2 christos the (default) text style. */ 786 1.1.1.2 christos 787 1.1.1.2 christos int 788 1.1.1.2 christos gdbpy_disassembler::fprintf_func (void *dis_info, 789 1.1.1.2 christos const char *format, ...) noexcept 790 1.1.1.2 christos { 791 1.1.1.2 christos va_list args; 792 1.1.1.2 christos va_start (args, format); 793 1.1.1.2 christos vfprintf_styled_func (dis_info, dis_style_text, format, args); 794 1.1.1.2 christos va_end (args); 795 1.1.1.2 christos 796 1.1.1.2 christos /* Something non -ve. */ 797 1.1.1.2 christos return 0; 798 1.1.1.2 christos } 799 1.1.1.2 christos 800 1.1.1.2 christos /* Disassembler callback for architectures where libopcodes does create 801 1.1.1.2 christos styled output. Just creates a new text part with the given STYLE. */ 802 1.1.1.2 christos 803 1.1.1.2 christos int 804 1.1.1.2 christos gdbpy_disassembler::fprintf_styled_func (void *dis_info, 805 1.1.1.2 christos enum disassembler_style style, 806 1.1.1.2 christos const char *format, ...) noexcept 807 1.1.1.2 christos { 808 1.1.1.2 christos va_list args; 809 1.1.1.2 christos va_start (args, format); 810 1.1.1.2 christos vfprintf_styled_func (dis_info, style, format, args); 811 1.1.1.2 christos va_end (args); 812 1.1.1.2 christos 813 1.1.1.2 christos /* Something non -ve. */ 814 1.1.1.2 christos return 0; 815 1.1.1.2 christos } 816 1.1.1.2 christos 817 1.1 christos /* This implements the disassemble_info read_memory_func callback and is 818 1.1 christos called from the libopcodes disassembler when the disassembler wants to 819 1.1 christos read memory. 820 1.1 christos 821 1.1 christos From the INFO argument we can find the gdbpy_disassembler object for 822 1.1 christos which we are disassembling, and from that object we can find the 823 1.1 christos DisassembleInfo for the current disassembly call. 824 1.1 christos 825 1.1 christos This function reads the instruction bytes by calling the read_memory 826 1.1 christos method on the DisassembleInfo object. This method might have been 827 1.1 christos overridden by user code. 828 1.1 christos 829 1.1 christos Read LEN bytes from MEMADDR and place them into BUFF. Return 0 on 830 1.1 christos success (in which case BUFF has been filled), or -1 on error, in which 831 1.1 christos case the contents of BUFF are undefined. */ 832 1.1 christos 833 1.1 christos int 834 1.1 christos gdbpy_disassembler::read_memory_func (bfd_vma memaddr, gdb_byte *buff, 835 1.1 christos unsigned int len, 836 1.1 christos struct disassemble_info *info) noexcept 837 1.1 christos { 838 1.1 christos gdbpy_disassembler *dis 839 1.1 christos = static_cast<gdbpy_disassembler *> (info->application_data); 840 1.1 christos disasm_info_object *obj = dis->py_disasm_info (); 841 1.1 christos 842 1.1 christos /* If a previous read attempt resulted in an exception, then we don't 843 1.1 christos allow any further reads to succeed. We only do this check for the 844 1.1 christos read_memory_func as this is the only one the user can hook into, 845 1.1 christos thus, this check prevents us calling back into user code if a 846 1.1 christos previous call has already thrown an error. */ 847 1.1 christos if (dis->has_stored_exception ()) 848 1.1 christos return -1; 849 1.1 christos 850 1.1 christos /* The DisassembleInfo.read_memory method expects an offset from the 851 1.1 christos address stored within the DisassembleInfo object; calculate that 852 1.1 christos offset here. */ 853 1.1.1.2 christos gdb_py_longest offset 854 1.1.1.2 christos = (gdb_py_longest) memaddr - (gdb_py_longest) obj->address; 855 1.1 christos 856 1.1 christos /* Now call the DisassembleInfo.read_memory method. This might have been 857 1.1 christos overridden by the user. */ 858 1.1 christos gdbpy_ref<> result_obj (PyObject_CallMethod ((PyObject *) obj, 859 1.1 christos "read_memory", 860 1.1.1.2 christos "I" GDB_PY_LL_ARG, len, offset)); 861 1.1 christos 862 1.1 christos /* Handle any exceptions. */ 863 1.1 christos if (result_obj == nullptr) 864 1.1 christos { 865 1.1 christos /* If we got a gdb.MemoryError then we ignore this and just report 866 1.1 christos that the read failed to the caller. The caller is then 867 1.1 christos responsible for calling the memory_error_func if it wants to. 868 1.1 christos Remember, the disassembler might just be probing to see if these 869 1.1 christos bytes can be read, if we automatically call the memory error 870 1.1 christos function, we can end up registering an error prematurely. */ 871 1.1 christos if (PyErr_ExceptionMatches (gdbpy_gdb_memory_error)) 872 1.1 christos { 873 1.1 christos PyErr_Clear (); 874 1.1 christos return -1; 875 1.1 christos } 876 1.1 christos 877 1.1 christos /* For any other exception type we capture the value of the Python 878 1.1 christos exception and throw it, this will then be caught in 879 1.1 christos disasmpy_builtin_disassemble, at which point the exception will be 880 1.1 christos restored. */ 881 1.1 christos dis->store_exception (gdbpy_err_fetch ()); 882 1.1 christos return -1; 883 1.1 christos } 884 1.1 christos 885 1.1 christos /* Convert the result to a buffer. */ 886 1.1 christos Py_buffer py_buff; 887 1.1 christos if (!PyObject_CheckBuffer (result_obj.get ()) 888 1.1 christos || PyObject_GetBuffer (result_obj.get(), &py_buff, PyBUF_CONTIG_RO) < 0) 889 1.1 christos { 890 1.1 christos PyErr_Format (PyExc_TypeError, 891 1.1 christos _("Result from read_memory is not a buffer")); 892 1.1 christos dis->store_exception (gdbpy_err_fetch ()); 893 1.1 christos return -1; 894 1.1 christos } 895 1.1 christos 896 1.1 christos /* Wrap PY_BUFF so that it is cleaned up correctly at the end of this 897 1.1 christos scope. */ 898 1.1 christos Py_buffer_up buffer_up (&py_buff); 899 1.1 christos 900 1.1 christos /* Validate that the buffer is the correct length. */ 901 1.1 christos if (py_buff.len != len) 902 1.1 christos { 903 1.1 christos PyErr_Format (PyExc_ValueError, 904 1.1 christos _("Buffer returned from read_memory is sized %d instead of the expected %d"), 905 1.1 christos py_buff.len, len); 906 1.1 christos dis->store_exception (gdbpy_err_fetch ()); 907 1.1 christos return -1; 908 1.1 christos } 909 1.1 christos 910 1.1 christos /* Copy the data out of the Python buffer and return success. */ 911 1.1 christos const gdb_byte *buffer = (const gdb_byte *) py_buff.buf; 912 1.1 christos memcpy (buff, buffer, len); 913 1.1 christos return 0; 914 1.1 christos } 915 1.1 christos 916 1.1.1.2 christos /* Implement __str__ for the DisassemblerResult type. */ 917 1.1.1.2 christos 918 1.1.1.2 christos static PyObject * 919 1.1.1.2 christos disasmpy_result_str (PyObject *self) 920 1.1.1.2 christos { 921 1.1.1.2 christos disasm_result_object *obj = (disasm_result_object *) self; 922 1.1.1.2 christos 923 1.1.1.2 christos /* These conditions are all enforced when the DisassemblerResult object 924 1.1.1.2 christos is created. */ 925 1.1.1.2 christos gdb_assert (obj->parts != nullptr); 926 1.1.1.2 christos gdb_assert (obj->parts->size () > 0); 927 1.1.1.2 christos gdb_assert (obj->length > 0); 928 1.1.1.2 christos 929 1.1.1.2 christos std::string str; 930 1.1.1.2 christos 931 1.1.1.2 christos try 932 1.1.1.2 christos { 933 1.1.1.2 christos str = disasmpy_parts_list_to_string (*obj->parts); 934 1.1.1.2 christos } 935 1.1.1.2 christos catch (const gdb_exception &except) 936 1.1.1.2 christos { 937 1.1.1.2 christos GDB_PY_HANDLE_EXCEPTION (except); 938 1.1.1.2 christos } 939 1.1.1.2 christos 940 1.1.1.2 christos return PyUnicode_Decode (str.c_str (), str.size (), 941 1.1.1.2 christos host_charset (), nullptr); 942 1.1.1.2 christos } 943 1.1.1.2 christos 944 1.1 christos /* Implement DisassemblerResult.length attribute, return the length of the 945 1.1 christos disassembled instruction. */ 946 1.1 christos 947 1.1 christos static PyObject * 948 1.1 christos disasmpy_result_length (PyObject *self, void *closure) 949 1.1 christos { 950 1.1 christos disasm_result_object *obj = (disasm_result_object *) self; 951 1.1 christos return gdb_py_object_from_longest (obj->length).release (); 952 1.1 christos } 953 1.1 christos 954 1.1 christos /* Implement DisassemblerResult.string attribute, return the content string 955 1.1 christos of the disassembled instruction. */ 956 1.1 christos 957 1.1 christos static PyObject * 958 1.1 christos disasmpy_result_string (PyObject *self, void *closure) 959 1.1 christos { 960 1.1.1.2 christos return disasmpy_result_str (self); 961 1.1.1.2 christos } 962 1.1.1.2 christos 963 1.1.1.2 christos /* Implement DisassemblerResult.parts method. Returns a list of all the 964 1.1.1.2 christos parts that make up this result. There should always be at least one 965 1.1.1.2 christos part, so the returned list should never be empty. */ 966 1.1.1.2 christos 967 1.1.1.2 christos static PyObject * 968 1.1.1.2 christos disasmpy_result_parts (PyObject *self, void *closure) 969 1.1.1.2 christos { 970 1.1 christos disasm_result_object *obj = (disasm_result_object *) self; 971 1.1 christos 972 1.1.1.2 christos /* These conditions are all enforced when the DisassemblerResult object 973 1.1.1.2 christos is created. */ 974 1.1.1.2 christos gdb_assert (obj->parts != nullptr); 975 1.1.1.2 christos gdb_assert (obj->parts->size () > 0); 976 1.1 christos gdb_assert (obj->length > 0); 977 1.1.1.2 christos 978 1.1.1.2 christos gdbpy_ref<> result_list (PyList_New (obj->parts->size ())); 979 1.1.1.2 christos if (result_list == nullptr) 980 1.1.1.2 christos return nullptr; 981 1.1.1.2 christos Py_ssize_t idx = 0; 982 1.1.1.2 christos for (auto p : *obj->parts) 983 1.1.1.2 christos { 984 1.1.1.2 christos gdbpy_ref<> item = gdbpy_ref<>::new_reference (p.get ()); 985 1.1.1.2 christos PyList_SET_ITEM (result_list.get (), idx, item.release ()); 986 1.1.1.2 christos ++idx; 987 1.1.1.2 christos } 988 1.1.1.2 christos 989 1.1.1.2 christos /* This should follow naturally from the obj->parts list being 990 1.1.1.2 christos non-empty. */ 991 1.1.1.2 christos gdb_assert (PyList_Size (result_list.get()) > 0); 992 1.1.1.2 christos 993 1.1.1.2 christos return result_list.release (); 994 1.1 christos } 995 1.1 christos 996 1.1 christos /* Implement DisassemblerResult.__init__. Takes two arguments, an 997 1.1 christos integer, the length in bytes of the disassembled instruction, and a 998 1.1 christos string, the disassembled content of the instruction. */ 999 1.1 christos 1000 1.1 christos static int 1001 1.1 christos disasmpy_result_init (PyObject *self, PyObject *args, PyObject *kwargs) 1002 1.1 christos { 1003 1.1.1.2 christos static const char *keywords[] = { "length", "string", "parts", NULL }; 1004 1.1 christos int length; 1005 1.1.1.2 christos const char *string = nullptr; 1006 1.1.1.2 christos PyObject *parts_list = nullptr; 1007 1.1.1.2 christos if (!gdb_PyArg_ParseTupleAndKeywords (args, kwargs, "i|zO", keywords, 1008 1.1.1.2 christos &length, &string, &parts_list)) 1009 1.1 christos return -1; 1010 1.1 christos 1011 1.1 christos if (length <= 0) 1012 1.1 christos { 1013 1.1 christos PyErr_SetString (PyExc_ValueError, 1014 1.1 christos _("Length must be greater than 0.")); 1015 1.1 christos return -1; 1016 1.1 christos } 1017 1.1 christos 1018 1.1.1.2 christos if (parts_list == Py_None) 1019 1.1.1.2 christos parts_list = nullptr; 1020 1.1.1.2 christos 1021 1.1.1.2 christos if (string != nullptr && parts_list != nullptr) 1022 1.1 christos { 1023 1.1.1.2 christos PyErr_Format (PyExc_ValueError, 1024 1.1.1.2 christos _("Cannot use 'string' and 'parts' when creating %s."), 1025 1.1.1.2 christos Py_TYPE (self)->tp_name); 1026 1.1 christos return -1; 1027 1.1 christos } 1028 1.1 christos 1029 1.1.1.2 christos if (string != nullptr) 1030 1.1.1.2 christos { 1031 1.1.1.2 christos if (strlen (string) == 0) 1032 1.1.1.2 christos { 1033 1.1.1.2 christos PyErr_SetString (PyExc_ValueError, 1034 1.1.1.2 christos _("String must not be empty.")); 1035 1.1.1.2 christos return -1; 1036 1.1.1.2 christos } 1037 1.1.1.2 christos 1038 1.1.1.2 christos disasm_result_object *obj = (disasm_result_object *) self; 1039 1.1.1.2 christos std::vector<gdbpy_ref<>> content; 1040 1.1.1.2 christos gdbpy_ref<> text_part 1041 1.1.1.2 christos = make_disasm_text_part (std::string (string), dis_style_text); 1042 1.1.1.2 christos content.emplace_back (text_part.release ()); 1043 1.1.1.2 christos disasmpy_init_disassembler_result (obj, length, std::move (content)); 1044 1.1.1.2 christos } 1045 1.1.1.2 christos else 1046 1.1.1.2 christos { 1047 1.1.1.2 christos if (!PySequence_Check (parts_list)) 1048 1.1.1.2 christos { 1049 1.1.1.2 christos PyErr_SetString (PyExc_TypeError, 1050 1.1.1.2 christos _("'parts' argument is not a sequence")); 1051 1.1.1.2 christos return -1; 1052 1.1.1.2 christos } 1053 1.1.1.2 christos 1054 1.1.1.2 christos Py_ssize_t parts_count = PySequence_Size (parts_list); 1055 1.1.1.2 christos if (parts_count <= 0) 1056 1.1.1.2 christos { 1057 1.1.1.2 christos PyErr_SetString (PyExc_ValueError, 1058 1.1.1.2 christos _("'parts' list must not be empty.")); 1059 1.1.1.2 christos return -1; 1060 1.1.1.2 christos } 1061 1.1.1.2 christos 1062 1.1.1.2 christos disasm_result_object *obj = (disasm_result_object *) self; 1063 1.1.1.2 christos std::vector<gdbpy_ref<>> content (parts_count); 1064 1.1.1.2 christos 1065 1.1.1.2 christos struct gdbarch *gdbarch = nullptr; 1066 1.1.1.2 christos for (Py_ssize_t i = 0; i < parts_count; ++i) 1067 1.1.1.2 christos { 1068 1.1.1.2 christos gdbpy_ref<> part (PySequence_GetItem (parts_list, i)); 1069 1.1.1.2 christos 1070 1.1.1.2 christos if (part == nullptr) 1071 1.1.1.2 christos return -1; 1072 1.1.1.2 christos 1073 1.1.1.2 christos if (Py_TYPE (part.get ()) == &disasm_addr_part_object_type) 1074 1.1.1.2 christos { 1075 1.1.1.2 christos disasm_addr_part_object *addr_part 1076 1.1.1.2 christos = (disasm_addr_part_object *) part.get (); 1077 1.1.1.2 christos gdb_assert (addr_part->gdbarch != nullptr); 1078 1.1.1.2 christos if (gdbarch == nullptr) 1079 1.1.1.2 christos gdbarch = addr_part->gdbarch; 1080 1.1.1.2 christos else if (addr_part->gdbarch != gdbarch) 1081 1.1.1.2 christos { 1082 1.1.1.2 christos PyErr_SetString (PyExc_ValueError, 1083 1.1.1.2 christos _("Inconsistent gdb.Architectures used " 1084 1.1.1.2 christos "in 'parts' sequence.")); 1085 1.1.1.2 christos return -1; 1086 1.1.1.2 christos } 1087 1.1.1.2 christos } 1088 1.1.1.2 christos 1089 1.1.1.2 christos content[i] = std::move (part); 1090 1.1.1.2 christos } 1091 1.1.1.2 christos 1092 1.1.1.2 christos disasmpy_init_disassembler_result (obj, length, std::move (content)); 1093 1.1.1.2 christos } 1094 1.1 christos 1095 1.1 christos return 0; 1096 1.1.1.2 christos 1097 1.1.1.2 christos } 1098 1.1.1.2 christos 1099 1.1.1.2 christos /* Implement __repr__ for the DisassemblerResult type. */ 1100 1.1.1.2 christos 1101 1.1.1.2 christos static PyObject * 1102 1.1.1.2 christos disasmpy_result_repr (PyObject *self) 1103 1.1.1.2 christos { 1104 1.1.1.2 christos disasm_result_object *obj = (disasm_result_object *) self; 1105 1.1.1.2 christos 1106 1.1.1.2 christos gdb_assert (obj->parts != nullptr); 1107 1.1.1.2 christos 1108 1.1.1.2 christos return PyUnicode_FromFormat ("<%s length=%d string=\"%U\">", 1109 1.1.1.2 christos Py_TYPE (obj)->tp_name, 1110 1.1.1.2 christos obj->length, 1111 1.1.1.2 christos disasmpy_result_str (self)); 1112 1.1 christos } 1113 1.1 christos 1114 1.1 christos /* Implement memory_error_func callback for disassemble_info. Extract the 1115 1.1 christos underlying DisassembleInfo Python object, and set a memory error on 1116 1.1 christos it. */ 1117 1.1 christos 1118 1.1 christos void 1119 1.1 christos gdbpy_disassembler::memory_error_func (int status, bfd_vma memaddr, 1120 1.1 christos struct disassemble_info *info) noexcept 1121 1.1 christos { 1122 1.1 christos gdbpy_disassembler *dis 1123 1.1 christos = static_cast<gdbpy_disassembler *> (info->application_data); 1124 1.1 christos dis->m_memory_error_address.emplace (memaddr); 1125 1.1 christos } 1126 1.1 christos 1127 1.1 christos /* Wrapper of print_address. */ 1128 1.1 christos 1129 1.1 christos void 1130 1.1 christos gdbpy_disassembler::print_address_func (bfd_vma addr, 1131 1.1 christos struct disassemble_info *info) noexcept 1132 1.1 christos { 1133 1.1 christos gdbpy_disassembler *dis 1134 1.1 christos = static_cast<gdbpy_disassembler *> (info->application_data); 1135 1.1.1.2 christos 1136 1.1.1.2 christos gdbpy_ref<> addr_part 1137 1.1.1.2 christos = make_disasm_addr_part (dis->arch (), addr); 1138 1.1.1.2 christos dis->m_parts.emplace_back (std::move (addr_part)); 1139 1.1 christos } 1140 1.1 christos 1141 1.1 christos /* constructor. */ 1142 1.1 christos 1143 1.1 christos gdbpy_disassembler::gdbpy_disassembler (disasm_info_object *obj, 1144 1.1 christos PyObject *memory_source) 1145 1.1.1.2 christos : gdb_disassemble_info (obj->gdbarch, 1146 1.1.1.2 christos read_memory_func, 1147 1.1.1.2 christos memory_error_func, 1148 1.1.1.2 christos print_address_func, 1149 1.1.1.2 christos fprintf_func, 1150 1.1.1.2 christos fprintf_styled_func), 1151 1.1 christos m_disasm_info_object (obj), 1152 1.1 christos m_memory_source (memory_source) 1153 1.1 christos { /* Nothing. */ } 1154 1.1 christos 1155 1.1 christos /* A wrapper around a reference to a Python DisassembleInfo object, which 1156 1.1 christos ensures that the object is marked as invalid when we leave the enclosing 1157 1.1 christos scope. 1158 1.1 christos 1159 1.1 christos Each DisassembleInfo is created in gdbpy_print_insn, and is done with by 1160 1.1 christos the time that function returns. However, there's nothing to stop a user 1161 1.1 christos caching a reference to the DisassembleInfo, and thus keeping the object 1162 1.1 christos around. 1163 1.1 christos 1164 1.1 christos We therefore have the notion of a DisassembleInfo becoming invalid, this 1165 1.1 christos happens when gdbpy_print_insn returns. This class is responsible for 1166 1.1 christos marking the DisassembleInfo as invalid in its destructor. */ 1167 1.1 christos 1168 1.1 christos struct scoped_disasm_info_object 1169 1.1 christos { 1170 1.1 christos /* Constructor. */ 1171 1.1 christos scoped_disasm_info_object (struct gdbarch *gdbarch, CORE_ADDR memaddr, 1172 1.1 christos disassemble_info *info) 1173 1.1 christos : m_disasm_info (allocate_disasm_info_object ()) 1174 1.1 christos { 1175 1.1 christos disasm_info_fill (m_disasm_info.get (), gdbarch, current_program_space, 1176 1.1 christos memaddr, info, nullptr); 1177 1.1 christos } 1178 1.1 christos 1179 1.1.1.2 christos /* Upon destruction mark m_disasm_info as invalid. */ 1180 1.1 christos ~scoped_disasm_info_object () 1181 1.1 christos { 1182 1.1 christos /* Invalidate the original DisassembleInfo object as well as any copies 1183 1.1 christos that the user might have made. */ 1184 1.1 christos for (disasm_info_object *obj = m_disasm_info.get (); 1185 1.1 christos obj != nullptr; 1186 1.1 christos obj = obj->next) 1187 1.1 christos obj->gdb_info = nullptr; 1188 1.1 christos } 1189 1.1 christos 1190 1.1 christos /* Return a pointer to the underlying disasm_info_object instance. */ 1191 1.1 christos disasm_info_object * 1192 1.1 christos get () const 1193 1.1 christos { 1194 1.1 christos return m_disasm_info.get (); 1195 1.1 christos } 1196 1.1 christos 1197 1.1 christos private: 1198 1.1 christos 1199 1.1 christos /* Wrapper around the call to PyObject_New, this wrapper function can be 1200 1.1 christos called from the constructor initialization list, while PyObject_New, a 1201 1.1 christos macro, can't. */ 1202 1.1 christos static disasm_info_object * 1203 1.1 christos allocate_disasm_info_object () 1204 1.1 christos { 1205 1.1 christos return (disasm_info_object *) PyObject_New (disasm_info_object, 1206 1.1 christos &disasm_info_object_type); 1207 1.1 christos } 1208 1.1 christos 1209 1.1 christos /* A reference to a gdb.disassembler.DisassembleInfo object. When this 1210 1.1 christos containing instance goes out of scope this reference is released, 1211 1.1 christos however, the user might be holding other references to the 1212 1.1 christos DisassembleInfo object in Python code, so the underlying object might 1213 1.1 christos not be deleted. */ 1214 1.1 christos gdbpy_ref<disasm_info_object> m_disasm_info; 1215 1.1 christos }; 1216 1.1 christos 1217 1.1 christos /* See python-internal.h. */ 1218 1.1 christos 1219 1.1.1.2 christos std::optional<int> 1220 1.1 christos gdbpy_print_insn (struct gdbarch *gdbarch, CORE_ADDR memaddr, 1221 1.1 christos disassemble_info *info) 1222 1.1 christos { 1223 1.1 christos /* Early exit case. This must be done as early as possible, and 1224 1.1 christos definitely before we enter Python environment. The 1225 1.1 christos python_print_insn_enabled flag is set (from Python) only when the user 1226 1.1 christos has installed one (or more) Python disassemblers. So in the common 1227 1.1 christos case (no custom disassembler installed) this flag will be false, 1228 1.1 christos allowing for a quick return. */ 1229 1.1 christos if (!gdb_python_initialized || !python_print_insn_enabled) 1230 1.1 christos return {}; 1231 1.1 christos 1232 1.1 christos gdbpy_enter enter_py (get_current_arch (), current_language); 1233 1.1 christos 1234 1.1 christos /* Import the gdb.disassembler module. */ 1235 1.1 christos gdbpy_ref<> gdb_python_disassembler_module 1236 1.1 christos (PyImport_ImportModule ("gdb.disassembler")); 1237 1.1 christos if (gdb_python_disassembler_module == nullptr) 1238 1.1 christos { 1239 1.1 christos gdbpy_print_stack (); 1240 1.1 christos return {}; 1241 1.1 christos } 1242 1.1 christos 1243 1.1 christos /* Get the _print_insn attribute from the module, this should be the 1244 1.1 christos function we are going to call to actually perform the disassembly. */ 1245 1.1 christos gdbpy_ref<> hook 1246 1.1 christos (PyObject_GetAttrString (gdb_python_disassembler_module.get (), 1247 1.1 christos "_print_insn")); 1248 1.1 christos if (hook == nullptr) 1249 1.1 christos { 1250 1.1 christos gdbpy_print_stack (); 1251 1.1 christos return {}; 1252 1.1 christos } 1253 1.1 christos 1254 1.1 christos /* Create the new DisassembleInfo object we will pass into Python. This 1255 1.1 christos object will be marked as invalid when we leave this scope. */ 1256 1.1 christos scoped_disasm_info_object scoped_disasm_info (gdbarch, memaddr, info); 1257 1.1 christos disasm_info_object *disasm_info = scoped_disasm_info.get (); 1258 1.1 christos 1259 1.1 christos /* Call into the registered disassembler to (possibly) perform the 1260 1.1 christos disassembly. */ 1261 1.1 christos PyObject *insn_disas_obj = (PyObject *) disasm_info; 1262 1.1 christos gdbpy_ref<> result (PyObject_CallFunctionObjArgs (hook.get (), 1263 1.1 christos insn_disas_obj, 1264 1.1 christos nullptr)); 1265 1.1 christos 1266 1.1 christos if (result == nullptr) 1267 1.1 christos { 1268 1.1 christos /* The call into Python code resulted in an exception. If this was a 1269 1.1 christos gdb.MemoryError, then we can figure out an address and call the 1270 1.1 christos disassemble_info::memory_error_func to report the error back to 1271 1.1 christos core GDB. Any other exception type we report back to core GDB as 1272 1.1 christos an unknown error (return -1 without first calling the 1273 1.1 christos memory_error_func callback). */ 1274 1.1 christos 1275 1.1 christos if (PyErr_ExceptionMatches (gdbpy_gdb_memory_error)) 1276 1.1 christos { 1277 1.1 christos /* A gdb.MemoryError might have an address attribute which 1278 1.1 christos contains the address at which the memory error occurred. If 1279 1.1 christos this is the case then use this address, otherwise, fallback to 1280 1.1 christos just using the address of the instruction we were asked to 1281 1.1 christos disassemble. */ 1282 1.1 christos gdbpy_err_fetch err; 1283 1.1 christos PyErr_Clear (); 1284 1.1 christos 1285 1.1 christos CORE_ADDR addr; 1286 1.1 christos if (err.value () != nullptr 1287 1.1 christos && PyObject_HasAttrString (err.value ().get (), "address")) 1288 1.1 christos { 1289 1.1 christos PyObject *addr_obj 1290 1.1 christos = PyObject_GetAttrString (err.value ().get (), "address"); 1291 1.1 christos if (get_addr_from_python (addr_obj, &addr) < 0) 1292 1.1 christos addr = disasm_info->address; 1293 1.1 christos } 1294 1.1 christos else 1295 1.1 christos addr = disasm_info->address; 1296 1.1 christos 1297 1.1 christos info->memory_error_func (-1, addr, info); 1298 1.1.1.2 christos return std::optional<int> (-1); 1299 1.1 christos } 1300 1.1 christos else if (PyErr_ExceptionMatches (gdbpy_gdberror_exc)) 1301 1.1 christos { 1302 1.1 christos gdbpy_err_fetch err; 1303 1.1 christos gdb::unique_xmalloc_ptr<char> msg = err.to_string (); 1304 1.1 christos 1305 1.1 christos info->fprintf_func (info->stream, "%s", msg.get ()); 1306 1.1.1.2 christos return std::optional<int> (-1); 1307 1.1 christos } 1308 1.1 christos else 1309 1.1 christos { 1310 1.1 christos gdbpy_print_stack (); 1311 1.1.1.2 christos return std::optional<int> (-1); 1312 1.1 christos } 1313 1.1 christos 1314 1.1 christos } 1315 1.1 christos else if (result == Py_None) 1316 1.1 christos { 1317 1.1 christos /* A return value of None indicates that the Python code could not, 1318 1.1 christos or doesn't want to, disassemble this instruction. Just return an 1319 1.1 christos empty result and core GDB will try to disassemble this for us. */ 1320 1.1 christos return {}; 1321 1.1 christos } 1322 1.1 christos 1323 1.1 christos /* Check the result is a DisassemblerResult (or a sub-class). */ 1324 1.1 christos if (!PyObject_IsInstance (result.get (), 1325 1.1 christos (PyObject *) &disasm_result_object_type)) 1326 1.1 christos { 1327 1.1 christos PyErr_SetString (PyExc_TypeError, 1328 1.1 christos _("Result is not a DisassemblerResult.")); 1329 1.1 christos gdbpy_print_stack (); 1330 1.1.1.2 christos return std::optional<int> (-1); 1331 1.1 christos } 1332 1.1 christos 1333 1.1.1.2 christos /* The result from the Python disassembler has the correct type. Convert 1334 1.1.1.2 christos this back to the underlying C++ object and read the state directly 1335 1.1.1.2 christos from this object. */ 1336 1.1.1.2 christos struct disasm_result_object *result_obj 1337 1.1.1.2 christos = (struct disasm_result_object *) result.get (); 1338 1.1 christos 1339 1.1.1.2 christos /* Validate the length of the disassembled instruction. */ 1340 1.1.1.2 christos long length = result_obj->length; 1341 1.1 christos long max_insn_length = (gdbarch_max_insn_length_p (gdbarch) ? 1342 1.1 christos gdbarch_max_insn_length (gdbarch) : INT_MAX); 1343 1.1 christos if (length <= 0) 1344 1.1 christos { 1345 1.1 christos PyErr_SetString 1346 1.1 christos (PyExc_ValueError, 1347 1.1 christos _("Invalid length attribute: length must be greater than 0.")); 1348 1.1 christos gdbpy_print_stack (); 1349 1.1.1.2 christos return std::optional<int> (-1); 1350 1.1 christos } 1351 1.1 christos if (length > max_insn_length) 1352 1.1 christos { 1353 1.1 christos PyErr_Format 1354 1.1 christos (PyExc_ValueError, 1355 1.1 christos _("Invalid length attribute: length %d greater than architecture maximum of %d"), 1356 1.1 christos length, max_insn_length); 1357 1.1 christos gdbpy_print_stack (); 1358 1.1.1.2 christos return std::optional<int> (-1); 1359 1.1 christos } 1360 1.1 christos 1361 1.1.1.2 christos /* It is impossible to create a DisassemblerResult object with an empty 1362 1.1.1.2 christos parts list. We know that each part results in a non-empty string, so 1363 1.1.1.2 christos we know that the instruction disassembly will not be the empty 1364 1.1.1.2 christos string. */ 1365 1.1.1.2 christos gdb_assert (result_obj->parts->size () > 0); 1366 1.1.1.2 christos 1367 1.1.1.2 christos /* Now print out the parts that make up this instruction. */ 1368 1.1.1.2 christos for (auto &p : *result_obj->parts) 1369 1.1 christos { 1370 1.1.1.2 christos if (Py_TYPE (p.get ()) == &disasm_text_part_object_type) 1371 1.1.1.2 christos { 1372 1.1.1.2 christos disasm_text_part_object *text_part 1373 1.1.1.2 christos = (disasm_text_part_object *) p.get (); 1374 1.1.1.2 christos gdb_assert (text_part->string != nullptr); 1375 1.1.1.2 christos info->fprintf_styled_func (info->stream, text_part->style, 1376 1.1.1.2 christos "%s", text_part->string->c_str ()); 1377 1.1.1.2 christos } 1378 1.1.1.2 christos else 1379 1.1.1.2 christos { 1380 1.1.1.2 christos gdb_assert (Py_TYPE (p.get ()) == &disasm_addr_part_object_type); 1381 1.1.1.2 christos disasm_addr_part_object *addr_part 1382 1.1.1.2 christos = (disasm_addr_part_object *) p.get (); 1383 1.1.1.2 christos /* A DisassemblerAddressPart can only be created by calling a 1384 1.1.1.2 christos method on DisassembleInfo, and the gdbarch is copied from the 1385 1.1.1.2 christos DisassembleInfo into the DisassemblerAddressPart. As the 1386 1.1.1.2 christos DisassembleInfo has its gdbarch initialised from GDBARCH in 1387 1.1.1.2 christos this scope, and this architecture can't be changed, then the 1388 1.1.1.2 christos following assert should hold. */ 1389 1.1.1.2 christos gdb_assert (addr_part->gdbarch == gdbarch); 1390 1.1.1.2 christos info->print_address_func (addr_part->address, info); 1391 1.1.1.2 christos } 1392 1.1 christos } 1393 1.1 christos 1394 1.1.1.2 christos return std::optional<int> (length); 1395 1.1 christos } 1396 1.1 christos 1397 1.1 christos /* The tp_dealloc callback for the DisassemblerResult type. Takes care of 1398 1.1 christos deallocating the content buffer. */ 1399 1.1 christos 1400 1.1 christos static void 1401 1.1 christos disasmpy_dealloc_result (PyObject *self) 1402 1.1 christos { 1403 1.1 christos disasm_result_object *obj = (disasm_result_object *) self; 1404 1.1.1.2 christos delete obj->parts; 1405 1.1 christos Py_TYPE (self)->tp_free (self); 1406 1.1 christos } 1407 1.1 christos 1408 1.1.1.2 christos /* The tp_init callback for the DisassemblerPart type. This just raises an 1409 1.1.1.2 christos exception, which prevents the user from creating objects of this type. 1410 1.1.1.2 christos Instead the user should create instances of a sub-class. */ 1411 1.1.1.2 christos 1412 1.1.1.2 christos static int 1413 1.1.1.2 christos disasmpy_part_init (PyObject *self, PyObject *args, PyObject *kwargs) 1414 1.1.1.2 christos { 1415 1.1.1.2 christos PyErr_SetString (PyExc_RuntimeError, 1416 1.1.1.2 christos _("Cannot create instances of DisassemblerPart.")); 1417 1.1.1.2 christos return -1; 1418 1.1.1.2 christos } 1419 1.1.1.2 christos 1420 1.1.1.2 christos /* Return a string representing STYLE. The returned string is used as a 1421 1.1.1.2 christos constant defined in the gdb.disassembler module. */ 1422 1.1.1.2 christos 1423 1.1.1.2 christos static const char * 1424 1.1.1.2 christos get_style_name (enum disassembler_style style) 1425 1.1.1.2 christos { 1426 1.1.1.2 christos switch (style) 1427 1.1.1.2 christos { 1428 1.1.1.2 christos case dis_style_text: return "STYLE_TEXT"; 1429 1.1.1.2 christos case dis_style_mnemonic: return "STYLE_MNEMONIC"; 1430 1.1.1.2 christos case dis_style_sub_mnemonic: return "STYLE_SUB_MNEMONIC"; 1431 1.1.1.2 christos case dis_style_assembler_directive: return "STYLE_ASSEMBLER_DIRECTIVE"; 1432 1.1.1.2 christos case dis_style_register: return "STYLE_REGISTER"; 1433 1.1.1.2 christos case dis_style_immediate: return "STYLE_IMMEDIATE"; 1434 1.1.1.2 christos case dis_style_address: return "STYLE_ADDRESS"; 1435 1.1.1.2 christos case dis_style_address_offset: return "STYLE_ADDRESS_OFFSET"; 1436 1.1.1.2 christos case dis_style_symbol: return "STYLE_SYMBOL"; 1437 1.1.1.2 christos case dis_style_comment_start: return "STYLE_COMMENT_START"; 1438 1.1.1.2 christos } 1439 1.1.1.2 christos 1440 1.1.1.2 christos gdb_assert_not_reached ("unknown disassembler style"); 1441 1.1.1.2 christos } 1442 1.1.1.2 christos 1443 1.1.1.2 christos /* Implement DisassemblerTextPart.__repr__ method. */ 1444 1.1.1.2 christos 1445 1.1.1.2 christos static PyObject * 1446 1.1.1.2 christos disasmpy_text_part_repr (PyObject *self) 1447 1.1.1.2 christos { 1448 1.1.1.2 christos disasm_text_part_object *obj = (disasm_text_part_object *) self; 1449 1.1.1.2 christos 1450 1.1.1.2 christos gdb_assert (obj->string != nullptr); 1451 1.1.1.2 christos 1452 1.1.1.2 christos return PyUnicode_FromFormat ("<%s string='%s', style='%s'>", 1453 1.1.1.2 christos Py_TYPE (obj)->tp_name, 1454 1.1.1.2 christos obj->string->c_str (), 1455 1.1.1.2 christos get_style_name (obj->style)); 1456 1.1.1.2 christos } 1457 1.1.1.2 christos 1458 1.1.1.2 christos /* Implement DisassemblerTextPart.__str__ attribute. */ 1459 1.1.1.2 christos 1460 1.1.1.2 christos static PyObject * 1461 1.1.1.2 christos disasmpy_text_part_str (PyObject *self) 1462 1.1.1.2 christos { 1463 1.1.1.2 christos disasm_text_part_object *obj = (disasm_text_part_object *) self; 1464 1.1.1.2 christos 1465 1.1.1.2 christos return PyUnicode_Decode (obj->string->c_str (), obj->string->size (), 1466 1.1.1.2 christos host_charset (), nullptr); 1467 1.1.1.2 christos } 1468 1.1.1.2 christos 1469 1.1.1.2 christos /* Implement DisassemblerTextPart.string attribute. */ 1470 1.1.1.2 christos 1471 1.1.1.2 christos static PyObject * 1472 1.1.1.2 christos disasmpy_text_part_string (PyObject *self, void *closure) 1473 1.1.1.2 christos { 1474 1.1.1.2 christos return disasmpy_text_part_str (self); 1475 1.1.1.2 christos } 1476 1.1.1.2 christos 1477 1.1.1.2 christos /* Implement DisassemblerTextPart.style attribute. */ 1478 1.1.1.2 christos 1479 1.1.1.2 christos static PyObject * 1480 1.1.1.2 christos disasmpy_text_part_style (PyObject *self, void *closure) 1481 1.1.1.2 christos { 1482 1.1.1.2 christos disasm_text_part_object *obj = (disasm_text_part_object *) self; 1483 1.1.1.2 christos 1484 1.1.1.2 christos LONGEST style_val = (LONGEST) obj->style; 1485 1.1.1.2 christos return gdb_py_object_from_longest (style_val).release (); 1486 1.1.1.2 christos } 1487 1.1.1.2 christos 1488 1.1.1.2 christos /* Implement DisassemblerAddressPart.__repr__ method. */ 1489 1.1.1.2 christos 1490 1.1.1.2 christos static PyObject * 1491 1.1.1.2 christos disasmpy_addr_part_repr (PyObject *self) 1492 1.1.1.2 christos { 1493 1.1.1.2 christos disasm_addr_part_object *obj = (disasm_addr_part_object *) self; 1494 1.1.1.2 christos 1495 1.1.1.2 christos return PyUnicode_FromFormat ("<%s address='%s'>", 1496 1.1.1.2 christos Py_TYPE (obj)->tp_name, 1497 1.1.1.2 christos core_addr_to_string_nz (obj->address)); 1498 1.1.1.2 christos } 1499 1.1.1.2 christos 1500 1.1.1.2 christos /* Implement DisassemblerAddressPart.__str__ attribute. */ 1501 1.1.1.2 christos 1502 1.1.1.2 christos static PyObject * 1503 1.1.1.2 christos disasmpy_addr_part_str (PyObject *self) 1504 1.1.1.2 christos { 1505 1.1.1.2 christos disasm_addr_part_object *obj = (disasm_addr_part_object *) self; 1506 1.1.1.2 christos 1507 1.1.1.2 christos std::string str; 1508 1.1.1.2 christos try 1509 1.1.1.2 christos { 1510 1.1.1.2 christos string_file buf; 1511 1.1.1.2 christos print_address (obj->gdbarch, obj->address, &buf); 1512 1.1.1.2 christos str = buf.release (); 1513 1.1.1.2 christos } 1514 1.1.1.2 christos catch (const gdb_exception &except) 1515 1.1.1.2 christos { 1516 1.1.1.2 christos GDB_PY_HANDLE_EXCEPTION (except); 1517 1.1.1.2 christos } 1518 1.1.1.2 christos 1519 1.1.1.2 christos return PyUnicode_Decode (str.c_str (), str.size (), 1520 1.1.1.2 christos host_charset (), nullptr); 1521 1.1.1.2 christos } 1522 1.1.1.2 christos 1523 1.1.1.2 christos /* Implement DisassemblerAddressPart.string attribute. */ 1524 1.1.1.2 christos 1525 1.1.1.2 christos static PyObject * 1526 1.1.1.2 christos disasmpy_addr_part_string (PyObject *self, void *closure) 1527 1.1.1.2 christos { 1528 1.1.1.2 christos return disasmpy_addr_part_str (self); 1529 1.1.1.2 christos } 1530 1.1.1.2 christos 1531 1.1.1.2 christos /* Implement DisassemblerAddressPart.address attribute. */ 1532 1.1.1.2 christos 1533 1.1.1.2 christos static PyObject * 1534 1.1.1.2 christos disasmpy_addr_part_address (PyObject *self, void *closure) 1535 1.1.1.2 christos { 1536 1.1.1.2 christos disasm_addr_part_object *obj = (disasm_addr_part_object *) self; 1537 1.1.1.2 christos 1538 1.1.1.2 christos return gdb_py_object_from_longest (obj->address).release (); 1539 1.1.1.2 christos } 1540 1.1.1.2 christos 1541 1.1 christos /* The get/set attributes of the gdb.disassembler.DisassembleInfo type. */ 1542 1.1 christos 1543 1.1 christos static gdb_PyGetSetDef disasm_info_object_getset[] = { 1544 1.1 christos { "address", disasmpy_info_address, nullptr, 1545 1.1 christos "Start address of the instruction to disassemble.", nullptr }, 1546 1.1 christos { "architecture", disasmpy_info_architecture, nullptr, 1547 1.1 christos "Architecture to disassemble in", nullptr }, 1548 1.1 christos { "progspace", disasmpy_info_progspace, nullptr, 1549 1.1 christos "Program space to disassemble in", nullptr }, 1550 1.1 christos { nullptr } /* Sentinel */ 1551 1.1 christos }; 1552 1.1 christos 1553 1.1 christos /* The methods of the gdb.disassembler.DisassembleInfo type. */ 1554 1.1 christos 1555 1.1 christos static PyMethodDef disasm_info_object_methods[] = { 1556 1.1 christos { "read_memory", (PyCFunction) disasmpy_info_read_memory, 1557 1.1 christos METH_VARARGS | METH_KEYWORDS, 1558 1.1 christos "read_memory (LEN, OFFSET = 0) -> Octets[]\n\ 1559 1.1 christos Read LEN octets for the instruction to disassemble." }, 1560 1.1 christos { "is_valid", disasmpy_info_is_valid, METH_NOARGS, 1561 1.1 christos "is_valid () -> Boolean.\n\ 1562 1.1 christos Return true if this DisassembleInfo is valid, false if not." }, 1563 1.1.1.2 christos { "text_part", (PyCFunction) disasmpy_info_make_text_part, 1564 1.1.1.2 christos METH_VARARGS | METH_KEYWORDS, 1565 1.1.1.2 christos "text_part (STRING, STYLE) -> DisassemblerTextPart\n\ 1566 1.1.1.2 christos Create a new text part, with contents STRING styled with STYLE." }, 1567 1.1.1.2 christos { "address_part", (PyCFunction) disasmpy_info_make_address_part, 1568 1.1.1.2 christos METH_VARARGS | METH_KEYWORDS, 1569 1.1.1.2 christos "address_part (ADDRESS) -> DisassemblerAddressPart\n\ 1570 1.1.1.2 christos Create a new address part representing ADDRESS." }, 1571 1.1 christos {nullptr} /* Sentinel */ 1572 1.1 christos }; 1573 1.1 christos 1574 1.1 christos /* The get/set attributes of the gdb.disassembler.DisassemblerResult type. */ 1575 1.1 christos 1576 1.1 christos static gdb_PyGetSetDef disasm_result_object_getset[] = { 1577 1.1 christos { "length", disasmpy_result_length, nullptr, 1578 1.1 christos "Length of the disassembled instruction.", nullptr }, 1579 1.1 christos { "string", disasmpy_result_string, nullptr, 1580 1.1 christos "String representing the disassembled instruction.", nullptr }, 1581 1.1.1.2 christos { "parts", disasmpy_result_parts, nullptr, 1582 1.1.1.2 christos "List of all the separate disassembly parts", nullptr }, 1583 1.1.1.2 christos { nullptr } /* Sentinel */ 1584 1.1.1.2 christos }; 1585 1.1.1.2 christos 1586 1.1.1.2 christos /* The get/set attributes of the gdb.disassembler.DisassemblerTextPart type. */ 1587 1.1.1.2 christos 1588 1.1.1.2 christos static gdb_PyGetSetDef disasmpy_text_part_getset[] = { 1589 1.1.1.2 christos { "string", disasmpy_text_part_string, nullptr, 1590 1.1.1.2 christos "String representing a text part.", nullptr }, 1591 1.1.1.2 christos { "style", disasmpy_text_part_style, nullptr, 1592 1.1.1.2 christos "The style of this text part.", nullptr }, 1593 1.1.1.2 christos { nullptr } /* Sentinel */ 1594 1.1.1.2 christos }; 1595 1.1.1.2 christos 1596 1.1.1.2 christos /* The get/set attributes of the gdb.disassembler.DisassemblerAddressPart type. */ 1597 1.1.1.2 christos 1598 1.1.1.2 christos static gdb_PyGetSetDef disasmpy_addr_part_getset[] = { 1599 1.1.1.2 christos { "string", disasmpy_addr_part_string, nullptr, 1600 1.1.1.2 christos "String representing an address part.", nullptr }, 1601 1.1.1.2 christos { "address", disasmpy_addr_part_address, nullptr, 1602 1.1.1.2 christos "The address of this address part.", nullptr }, 1603 1.1 christos { nullptr } /* Sentinel */ 1604 1.1 christos }; 1605 1.1 christos 1606 1.1 christos /* These are the methods we add into the _gdb.disassembler module, which 1607 1.1 christos are then imported into the gdb.disassembler module. These are global 1608 1.1 christos functions that support performing disassembly. */ 1609 1.1 christos 1610 1.1 christos PyMethodDef python_disassembler_methods[] = 1611 1.1 christos { 1612 1.1 christos { "builtin_disassemble", (PyCFunction) disasmpy_builtin_disassemble, 1613 1.1 christos METH_VARARGS | METH_KEYWORDS, 1614 1.1 christos "builtin_disassemble (INFO, MEMORY_SOURCE = None) -> None\n\ 1615 1.1 christos Disassemble using GDB's builtin disassembler. INFO is an instance of\n\ 1616 1.1 christos gdb.disassembler.DisassembleInfo. The MEMORY_SOURCE, if not None, should\n\ 1617 1.1 christos be an object with the read_memory method." }, 1618 1.1 christos { "_set_enabled", (PyCFunction) disasmpy_set_enabled, 1619 1.1 christos METH_VARARGS | METH_KEYWORDS, 1620 1.1 christos "_set_enabled (STATE) -> None\n\ 1621 1.1 christos Set whether GDB should call into the Python _print_insn code or not." }, 1622 1.1 christos {nullptr, nullptr, 0, nullptr} 1623 1.1 christos }; 1624 1.1 christos 1625 1.1 christos /* Structure to define the _gdb.disassembler module. */ 1626 1.1 christos 1627 1.1 christos static struct PyModuleDef python_disassembler_module_def = 1628 1.1 christos { 1629 1.1 christos PyModuleDef_HEAD_INIT, 1630 1.1 christos "_gdb.disassembler", 1631 1.1 christos nullptr, 1632 1.1 christos -1, 1633 1.1 christos python_disassembler_methods, 1634 1.1 christos nullptr, 1635 1.1 christos nullptr, 1636 1.1 christos nullptr, 1637 1.1 christos nullptr 1638 1.1 christos }; 1639 1.1 christos 1640 1.1 christos /* Called to initialize the Python structures in this file. */ 1641 1.1 christos 1642 1.1.1.2 christos static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION 1643 1.1 christos gdbpy_initialize_disasm () 1644 1.1 christos { 1645 1.1 christos /* Create the _gdb.disassembler module, and add it to the _gdb module. */ 1646 1.1 christos 1647 1.1 christos PyObject *gdb_disassembler_module; 1648 1.1 christos gdb_disassembler_module = PyModule_Create (&python_disassembler_module_def); 1649 1.1 christos if (gdb_disassembler_module == nullptr) 1650 1.1 christos return -1; 1651 1.1.1.2 christos if (gdb_pymodule_addobject (gdb_module, "disassembler", 1652 1.1.1.2 christos gdb_disassembler_module) < 0) 1653 1.1.1.2 christos return -1; 1654 1.1 christos 1655 1.1 christos /* This is needed so that 'import _gdb.disassembler' will work. */ 1656 1.1 christos PyObject *dict = PyImport_GetModuleDict (); 1657 1.1.1.2 christos if (PyDict_SetItemString (dict, "_gdb.disassembler", 1658 1.1.1.2 christos gdb_disassembler_module) < 0) 1659 1.1.1.2 christos return -1; 1660 1.1.1.2 christos 1661 1.1.1.2 christos for (int i = 0; i <= (int) dis_style_comment_start; ++i) 1662 1.1.1.2 christos { 1663 1.1.1.2 christos const char *style_name = get_style_name ((enum disassembler_style) i); 1664 1.1.1.2 christos if (PyModule_AddIntConstant (gdb_disassembler_module, style_name, i) < 0) 1665 1.1.1.2 christos return -1; 1666 1.1.1.2 christos } 1667 1.1 christos 1668 1.1 christos disasm_info_object_type.tp_new = PyType_GenericNew; 1669 1.1 christos if (PyType_Ready (&disasm_info_object_type) < 0) 1670 1.1 christos return -1; 1671 1.1 christos 1672 1.1 christos if (gdb_pymodule_addobject (gdb_disassembler_module, "DisassembleInfo", 1673 1.1 christos (PyObject *) &disasm_info_object_type) < 0) 1674 1.1 christos return -1; 1675 1.1 christos 1676 1.1 christos disasm_result_object_type.tp_new = PyType_GenericNew; 1677 1.1 christos if (PyType_Ready (&disasm_result_object_type) < 0) 1678 1.1 christos return -1; 1679 1.1 christos 1680 1.1 christos if (gdb_pymodule_addobject (gdb_disassembler_module, "DisassemblerResult", 1681 1.1 christos (PyObject *) &disasm_result_object_type) < 0) 1682 1.1 christos return -1; 1683 1.1 christos 1684 1.1.1.2 christos disasm_part_object_type.tp_new = PyType_GenericNew; 1685 1.1.1.2 christos if (PyType_Ready (&disasm_part_object_type) < 0) 1686 1.1.1.2 christos return -1; 1687 1.1.1.2 christos 1688 1.1.1.2 christos if (gdb_pymodule_addobject (gdb_disassembler_module, "DisassemblerPart", 1689 1.1.1.2 christos (PyObject *) &disasm_part_object_type) < 0) 1690 1.1.1.2 christos return -1; 1691 1.1.1.2 christos 1692 1.1.1.2 christos disasm_addr_part_object_type.tp_new = PyType_GenericNew; 1693 1.1.1.2 christos if (PyType_Ready (&disasm_addr_part_object_type) < 0) 1694 1.1.1.2 christos return -1; 1695 1.1.1.2 christos 1696 1.1.1.2 christos if (gdb_pymodule_addobject (gdb_disassembler_module, 1697 1.1.1.2 christos "DisassemblerAddressPart", 1698 1.1.1.2 christos (PyObject *) &disasm_addr_part_object_type) < 0) 1699 1.1.1.2 christos return -1; 1700 1.1.1.2 christos 1701 1.1.1.2 christos disasm_text_part_object_type.tp_new = PyType_GenericNew; 1702 1.1.1.2 christos if (PyType_Ready (&disasm_text_part_object_type) < 0) 1703 1.1.1.2 christos return -1; 1704 1.1.1.2 christos 1705 1.1.1.2 christos if (gdb_pymodule_addobject (gdb_disassembler_module, 1706 1.1.1.2 christos "DisassemblerTextPart", 1707 1.1.1.2 christos (PyObject *) &disasm_text_part_object_type) < 0) 1708 1.1.1.2 christos return -1; 1709 1.1.1.2 christos 1710 1.1 christos return 0; 1711 1.1 christos } 1712 1.1 christos 1713 1.1.1.2 christos GDBPY_INITIALIZE_FILE (gdbpy_initialize_disasm); 1714 1.1.1.2 christos 1715 1.1.1.2 christos 1716 1.1.1.2 christos 1718 1.1 christos /* Describe the gdb.disassembler.DisassembleInfo type. */ 1719 1.1 christos 1720 1.1 christos PyTypeObject disasm_info_object_type = { 1721 1.1 christos PyVarObject_HEAD_INIT (nullptr, 0) 1722 1.1 christos "gdb.disassembler.DisassembleInfo", /*tp_name*/ 1723 1.1 christos sizeof (disasm_info_object), /*tp_basicsize*/ 1724 1.1 christos 0, /*tp_itemsize*/ 1725 1.1 christos disasm_info_dealloc, /*tp_dealloc*/ 1726 1.1 christos 0, /*tp_print*/ 1727 1.1 christos 0, /*tp_getattr*/ 1728 1.1 christos 0, /*tp_setattr*/ 1729 1.1.1.2 christos 0, /*tp_compare*/ 1730 1.1 christos disasmpy_info_repr, /*tp_repr*/ 1731 1.1 christos 0, /*tp_as_number*/ 1732 1.1 christos 0, /*tp_as_sequence*/ 1733 1.1 christos 0, /*tp_as_mapping*/ 1734 1.1 christos 0, /*tp_hash */ 1735 1.1 christos 0, /*tp_call*/ 1736 1.1 christos 0, /*tp_str*/ 1737 1.1 christos 0, /*tp_getattro*/ 1738 1.1 christos 0, /*tp_setattro*/ 1739 1.1 christos 0, /*tp_as_buffer*/ 1740 1.1 christos Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ 1741 1.1 christos "GDB instruction disassembler object", /* tp_doc */ 1742 1.1 christos 0, /* tp_traverse */ 1743 1.1 christos 0, /* tp_clear */ 1744 1.1 christos 0, /* tp_richcompare */ 1745 1.1 christos 0, /* tp_weaklistoffset */ 1746 1.1 christos 0, /* tp_iter */ 1747 1.1 christos 0, /* tp_iternext */ 1748 1.1 christos disasm_info_object_methods, /* tp_methods */ 1749 1.1 christos 0, /* tp_members */ 1750 1.1 christos disasm_info_object_getset, /* tp_getset */ 1751 1.1 christos 0, /* tp_base */ 1752 1.1 christos 0, /* tp_dict */ 1753 1.1 christos 0, /* tp_descr_get */ 1754 1.1 christos 0, /* tp_descr_set */ 1755 1.1 christos 0, /* tp_dictoffset */ 1756 1.1 christos disasm_info_init, /* tp_init */ 1757 1.1 christos 0, /* tp_alloc */ 1758 1.1 christos }; 1759 1.1 christos 1760 1.1 christos /* Describe the gdb.disassembler.DisassemblerResult type. */ 1761 1.1 christos 1762 1.1 christos PyTypeObject disasm_result_object_type = { 1763 1.1 christos PyVarObject_HEAD_INIT (nullptr, 0) 1764 1.1 christos "gdb.disassembler.DisassemblerResult", /*tp_name*/ 1765 1.1 christos sizeof (disasm_result_object), /*tp_basicsize*/ 1766 1.1 christos 0, /*tp_itemsize*/ 1767 1.1 christos disasmpy_dealloc_result, /*tp_dealloc*/ 1768 1.1 christos 0, /*tp_print*/ 1769 1.1 christos 0, /*tp_getattr*/ 1770 1.1 christos 0, /*tp_setattr*/ 1771 1.1.1.2 christos 0, /*tp_compare*/ 1772 1.1 christos disasmpy_result_repr, /*tp_repr*/ 1773 1.1 christos 0, /*tp_as_number*/ 1774 1.1 christos 0, /*tp_as_sequence*/ 1775 1.1 christos 0, /*tp_as_mapping*/ 1776 1.1 christos 0, /*tp_hash */ 1777 1.1.1.2 christos 0, /*tp_call*/ 1778 1.1 christos disasmpy_result_str, /*tp_str*/ 1779 1.1 christos 0, /*tp_getattro*/ 1780 1.1 christos 0, /*tp_setattro*/ 1781 1.1.1.2 christos 0, /*tp_as_buffer*/ 1782 1.1 christos Py_TPFLAGS_DEFAULT, /*tp_flags*/ 1783 1.1 christos "GDB object, representing a disassembler result", /* tp_doc */ 1784 1.1 christos 0, /* tp_traverse */ 1785 1.1 christos 0, /* tp_clear */ 1786 1.1 christos 0, /* tp_richcompare */ 1787 1.1 christos 0, /* tp_weaklistoffset */ 1788 1.1 christos 0, /* tp_iter */ 1789 1.1 christos 0, /* tp_iternext */ 1790 1.1 christos 0, /* tp_methods */ 1791 1.1 christos 0, /* tp_members */ 1792 1.1 christos disasm_result_object_getset, /* tp_getset */ 1793 1.1 christos 0, /* tp_base */ 1794 1.1 christos 0, /* tp_dict */ 1795 1.1 christos 0, /* tp_descr_get */ 1796 1.1 christos 0, /* tp_descr_set */ 1797 1.1 christos 0, /* tp_dictoffset */ 1798 1.1 christos disasmpy_result_init, /* tp_init */ 1799 1.1 christos 0, /* tp_alloc */ 1800 1.1.1.2 christos }; 1801 1.1.1.2 christos 1802 1.1.1.2 christos /* Describe the gdb.disassembler.DisassemblerPart type. This type exists 1803 1.1.1.2 christos only as an abstract base-class for the various part sub-types. The 1804 1.1.1.2 christos init method for this type throws an error. As such we don't both to 1805 1.1.1.2 christos provide a tp_repr method for this parent class. */ 1806 1.1.1.2 christos 1807 1.1.1.2 christos PyTypeObject disasm_part_object_type = { 1808 1.1.1.2 christos PyVarObject_HEAD_INIT (nullptr, 0) 1809 1.1.1.2 christos "gdb.disassembler.DisassemblerPart", /*tp_name*/ 1810 1.1.1.2 christos sizeof (PyObject), /*tp_basicsize*/ 1811 1.1.1.2 christos 0, /*tp_itemsize*/ 1812 1.1.1.2 christos 0, /*tp_dealloc*/ 1813 1.1.1.2 christos 0, /*tp_print*/ 1814 1.1.1.2 christos 0, /*tp_getattr*/ 1815 1.1.1.2 christos 0, /*tp_setattr*/ 1816 1.1.1.2 christos 0, /*tp_compare*/ 1817 1.1.1.2 christos 0, /*tp_repr*/ 1818 1.1.1.2 christos 0, /*tp_as_number*/ 1819 1.1.1.2 christos 0, /*tp_as_sequence*/ 1820 1.1.1.2 christos 0, /*tp_as_mapping*/ 1821 1.1.1.2 christos 0, /*tp_hash */ 1822 1.1.1.2 christos 0, /*tp_call*/ 1823 1.1.1.2 christos 0, /*tp_str*/ 1824 1.1.1.2 christos 0, /*tp_getattro*/ 1825 1.1.1.2 christos 0, /*tp_setattro*/ 1826 1.1.1.2 christos 0, /*tp_as_buffer*/ 1827 1.1.1.2 christos Py_TPFLAGS_DEFAULT, /*tp_flags*/ 1828 1.1.1.2 christos "GDB object, representing part of a disassembled instruction", /* tp_doc */ 1829 1.1.1.2 christos 0, /* tp_traverse */ 1830 1.1.1.2 christos 0, /* tp_clear */ 1831 1.1.1.2 christos 0, /* tp_richcompare */ 1832 1.1.1.2 christos 0, /* tp_weaklistoffset */ 1833 1.1.1.2 christos 0, /* tp_iter */ 1834 1.1.1.2 christos 0, /* tp_iternext */ 1835 1.1.1.2 christos 0, /* tp_methods */ 1836 1.1.1.2 christos 0, /* tp_members */ 1837 1.1.1.2 christos 0, /* tp_getset */ 1838 1.1.1.2 christos 0, /* tp_base */ 1839 1.1.1.2 christos 0, /* tp_dict */ 1840 1.1.1.2 christos 0, /* tp_descr_get */ 1841 1.1.1.2 christos 0, /* tp_descr_set */ 1842 1.1.1.2 christos 0, /* tp_dictoffset */ 1843 1.1.1.2 christos disasmpy_part_init, /* tp_init */ 1844 1.1.1.2 christos 0, /* tp_alloc */ 1845 1.1.1.2 christos }; 1846 1.1.1.2 christos 1847 1.1.1.2 christos /* Describe the gdb.disassembler.DisassemblerTextPart type. */ 1848 1.1.1.2 christos 1849 1.1.1.2 christos PyTypeObject disasm_text_part_object_type = { 1850 1.1.1.2 christos PyVarObject_HEAD_INIT (nullptr, 0) 1851 1.1.1.2 christos "gdb.disassembler.DisassemblerTextPart", /*tp_name*/ 1852 1.1.1.2 christos sizeof (disasm_text_part_object_type), /*tp_basicsize*/ 1853 1.1.1.2 christos 0, /*tp_itemsize*/ 1854 1.1.1.2 christos 0, /*tp_dealloc*/ 1855 1.1.1.2 christos 0, /*tp_print*/ 1856 1.1.1.2 christos 0, /*tp_getattr*/ 1857 1.1.1.2 christos 0, /*tp_setattr*/ 1858 1.1.1.2 christos 0, /*tp_compare*/ 1859 1.1.1.2 christos disasmpy_text_part_repr, /*tp_repr*/ 1860 1.1.1.2 christos 0, /*tp_as_number*/ 1861 1.1.1.2 christos 0, /*tp_as_sequence*/ 1862 1.1.1.2 christos 0, /*tp_as_mapping*/ 1863 1.1.1.2 christos 0, /*tp_hash */ 1864 1.1.1.2 christos 0, /*tp_call*/ 1865 1.1.1.2 christos disasmpy_text_part_str, /*tp_str*/ 1866 1.1.1.2 christos 0, /*tp_getattro*/ 1867 1.1.1.2 christos 0, /*tp_setattro*/ 1868 1.1.1.2 christos 0, /*tp_as_buffer*/ 1869 1.1.1.2 christos Py_TPFLAGS_DEFAULT, /*tp_flags*/ 1870 1.1.1.2 christos "GDB object, representing a text part of an instruction", /* tp_doc */ 1871 1.1.1.2 christos 0, /* tp_traverse */ 1872 1.1.1.2 christos 0, /* tp_clear */ 1873 1.1.1.2 christos 0, /* tp_richcompare */ 1874 1.1.1.2 christos 0, /* tp_weaklistoffset */ 1875 1.1.1.2 christos 0, /* tp_iter */ 1876 1.1.1.2 christos 0, /* tp_iternext */ 1877 1.1.1.2 christos 0, /* tp_methods */ 1878 1.1.1.2 christos 0, /* tp_members */ 1879 1.1.1.2 christos disasmpy_text_part_getset, /* tp_getset */ 1880 1.1.1.2 christos &disasm_part_object_type, /* tp_base */ 1881 1.1.1.2 christos 0, /* tp_dict */ 1882 1.1.1.2 christos 0, /* tp_descr_get */ 1883 1.1.1.2 christos 0, /* tp_descr_set */ 1884 1.1.1.2 christos 0, /* tp_dictoffset */ 1885 1.1.1.2 christos 0, /* tp_init */ 1886 1.1.1.2 christos 0, /* tp_alloc */ 1887 1.1.1.2 christos }; 1888 1.1.1.2 christos 1889 1.1.1.2 christos /* Describe the gdb.disassembler.DisassemblerAddressPart type. */ 1890 1.1.1.2 christos 1891 1.1.1.2 christos PyTypeObject disasm_addr_part_object_type = { 1892 1.1.1.2 christos PyVarObject_HEAD_INIT (nullptr, 0) 1893 1.1.1.2 christos "gdb.disassembler.DisassemblerAddressPart", /*tp_name*/ 1894 1.1.1.2 christos sizeof (disasm_addr_part_object), /*tp_basicsize*/ 1895 1.1.1.2 christos 0, /*tp_itemsize*/ 1896 1.1.1.2 christos 0, /*tp_dealloc*/ 1897 1.1.1.2 christos 0, /*tp_print*/ 1898 1.1.1.2 christos 0, /*tp_getattr*/ 1899 1.1.1.2 christos 0, /*tp_setattr*/ 1900 1.1.1.2 christos 0, /*tp_compare*/ 1901 1.1.1.2 christos disasmpy_addr_part_repr, /*tp_repr*/ 1902 1.1.1.2 christos 0, /*tp_as_number*/ 1903 1.1.1.2 christos 0, /*tp_as_sequence*/ 1904 1.1.1.2 christos 0, /*tp_as_mapping*/ 1905 1.1.1.2 christos 0, /*tp_hash */ 1906 1.1.1.2 christos 0, /*tp_call*/ 1907 1.1.1.2 christos disasmpy_addr_part_str, /*tp_str*/ 1908 1.1.1.2 christos 0, /*tp_getattro*/ 1909 1.1.1.2 christos 0, /*tp_setattro*/ 1910 1.1.1.2 christos 0, /*tp_as_buffer*/ 1911 1.1.1.2 christos Py_TPFLAGS_DEFAULT, /*tp_flags*/ 1912 1.1.1.2 christos "GDB object, representing an address part of an instruction", /* tp_doc */ 1913 1.1.1.2 christos 0, /* tp_traverse */ 1914 1.1.1.2 christos 0, /* tp_clear */ 1915 1.1.1.2 christos 0, /* tp_richcompare */ 1916 1.1.1.2 christos 0, /* tp_weaklistoffset */ 1917 1.1.1.2 christos 0, /* tp_iter */ 1918 1.1.1.2 christos 0, /* tp_iternext */ 1919 1.1.1.2 christos 0, /* tp_methods */ 1920 1.1.1.2 christos 0, /* tp_members */ 1921 1.1.1.2 christos disasmpy_addr_part_getset, /* tp_getset */ 1922 1.1.1.2 christos &disasm_part_object_type, /* tp_base */ 1923 1.1.1.2 christos 0, /* tp_dict */ 1924 1.1.1.2 christos 0, /* tp_descr_get */ 1925 1.1.1.2 christos 0, /* tp_descr_set */ 1926 1.1.1.2 christos 0, /* tp_dictoffset */ 1927 1.1.1.2 christos 0, /* tp_init */ 1928 1.1.1.2 christos 0, /* tp_alloc */ 1929 }; 1930