1 /* Python memory buffer interface for reading inferior memory.
3 Copyright (C) 2009-2021 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "python-internal.h"
23 struct membuf_object {
26 /* Pointer to the raw data, and array of gdb_bytes. */
29 /* The address from where the data was read, held for mbpy_str. */
32 /* The number of octets in BUFFER. */
36 extern PyTypeObject membuf_object_type
37 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("membuf_object");
39 /* Wrap BUFFER, ADDRESS, and LENGTH into a gdb.Membuf object. ADDRESS is
40 the address within the inferior that the contents of BUFFER were read,
41 and LENGTH is the number of octets in BUFFER. */
44 gdbpy_buffer_to_membuf (gdb::unique_xmalloc_ptr<gdb_byte> buffer,
48 gdbpy_ref<membuf_object> membuf_obj (PyObject_New (membuf_object,
49 &membuf_object_type));
50 if (membuf_obj == nullptr)
53 membuf_obj->buffer = buffer.release ();
54 membuf_obj->addr = address;
55 membuf_obj->length = length;
59 result = PyMemoryView_FromObject ((PyObject *) membuf_obj.get ());
61 result = PyBuffer_FromReadWriteObject ((PyObject *) membuf_obj.get (), 0,
68 /* Destructor for gdb.Membuf objects. */
71 mbpy_dealloc (PyObject *self)
73 xfree (((membuf_object *) self)->buffer);
74 Py_TYPE (self)->tp_free (self);
77 /* Return a description of the gdb.Membuf object. */
80 mbpy_str (PyObject *self)
82 membuf_object *membuf_obj = (membuf_object *) self;
84 return PyString_FromFormat (_("Memory buffer for address %s, \
85 which is %s bytes long."),
86 paddress (python_gdbarch, membuf_obj->addr),
87 pulongest (membuf_obj->length));
93 get_buffer (PyObject *self, Py_buffer *buf, int flags)
95 membuf_object *membuf_obj = (membuf_object *) self;
98 ret = PyBuffer_FillInfo (buf, self, membuf_obj->buffer,
99 membuf_obj->length, 0,
102 /* Despite the documentation saying this field is a "const char *",
103 in Python 3.4 at least, it's really a "char *". */
104 buf->format = (char *) "c";
112 get_read_buffer (PyObject *self, Py_ssize_t segment, void **ptrptr)
114 membuf_object *membuf_obj = (membuf_object *) self;
118 PyErr_SetString (PyExc_SystemError,
119 _("The memory buffer supports only one segment."));
123 *ptrptr = membuf_obj->buffer;
125 return membuf_obj->length;
129 get_write_buffer (PyObject *self, Py_ssize_t segment, void **ptrptr)
131 return get_read_buffer (self, segment, ptrptr);
135 get_seg_count (PyObject *self, Py_ssize_t *lenp)
138 *lenp = ((membuf_object *) self)->length;
144 get_char_buffer (PyObject *self, Py_ssize_t segment, char **ptrptr)
149 ret = get_read_buffer (self, segment, &ptr);
150 *ptrptr = (char *) ptr;
157 /* General Python initialization callback. */
160 gdbpy_initialize_membuf (void)
162 membuf_object_type.tp_new = PyType_GenericNew;
163 if (PyType_Ready (&membuf_object_type) < 0)
166 return gdb_pymodule_addobject (gdb_module, "Membuf",
167 (PyObject *) &membuf_object_type);
172 static PyBufferProcs buffer_procs =
179 static PyBufferProcs buffer_procs = {
188 PyTypeObject membuf_object_type = {
189 PyVarObject_HEAD_INIT (nullptr, 0)
190 "gdb.Membuf", /*tp_name*/
191 sizeof (membuf_object), /*tp_basicsize*/
193 mbpy_dealloc, /*tp_dealloc*/
200 0, /*tp_as_sequence*/
207 &buffer_procs, /*tp_as_buffer*/
208 Py_TPFLAGS_DEFAULT, /*tp_flags*/
209 "GDB memory buffer object", /*tp_doc*/
212 0, /* tp_richcompare */
213 0, /* tp_weaklistoffset */
221 0, /* tp_descr_get */
222 0, /* tp_descr_set */
223 0, /* tp_dictoffset */