]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/python/py-membuf.c
Automatic date update in version.in
[thirdparty/binutils-gdb.git] / gdb / python / py-membuf.c
CommitLineData
625f7b1c
AB
1/* Python memory buffer interface for reading inferior memory.
2
d01e8234 3 Copyright (C) 2009-2025 Free Software Foundation, Inc.
625f7b1c
AB
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
625f7b1c
AB
20#include "python-internal.h"
21
22struct membuf_object {
23 PyObject_HEAD
24
25 /* Pointer to the raw data, and array of gdb_bytes. */
26 void *buffer;
27
28 /* The address from where the data was read, held for mbpy_str. */
29 CORE_ADDR addr;
30
31 /* The number of octets in BUFFER. */
32 CORE_ADDR length;
33};
34
35extern PyTypeObject membuf_object_type
36 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("membuf_object");
37
38/* Wrap BUFFER, ADDRESS, and LENGTH into a gdb.Membuf object. ADDRESS is
39 the address within the inferior that the contents of BUFFER were read,
40 and LENGTH is the number of octets in BUFFER. */
41
42PyObject *
43gdbpy_buffer_to_membuf (gdb::unique_xmalloc_ptr<gdb_byte> buffer,
44 CORE_ADDR address,
45 ULONGEST length)
46{
47 gdbpy_ref<membuf_object> membuf_obj (PyObject_New (membuf_object,
48 &membuf_object_type));
49 if (membuf_obj == nullptr)
50 return nullptr;
51
52 membuf_obj->buffer = buffer.release ();
53 membuf_obj->addr = address;
54 membuf_obj->length = length;
55
edae3fd6 56 return PyMemoryView_FromObject ((PyObject *) membuf_obj.get ());
625f7b1c
AB
57}
58
59/* Destructor for gdb.Membuf objects. */
60
61static void
62mbpy_dealloc (PyObject *self)
63{
64 xfree (((membuf_object *) self)->buffer);
65 Py_TYPE (self)->tp_free (self);
66}
67
68/* Return a description of the gdb.Membuf object. */
69
70static PyObject *
71mbpy_str (PyObject *self)
72{
73 membuf_object *membuf_obj = (membuf_object *) self;
74
5aee4587 75 return PyUnicode_FromFormat (_("Memory buffer for address %s, \
625f7b1c 76which is %s bytes long."),
5aee4587
SM
77 paddress (gdbpy_enter::get_gdbarch (),
78 membuf_obj->addr),
79 pulongest (membuf_obj->length));
625f7b1c
AB
80}
81
625f7b1c
AB
82static int
83get_buffer (PyObject *self, Py_buffer *buf, int flags)
84{
85 membuf_object *membuf_obj = (membuf_object *) self;
86 int ret;
87
88 ret = PyBuffer_FillInfo (buf, self, membuf_obj->buffer,
89 membuf_obj->length, 0,
90 PyBUF_CONTIG);
91
92 /* Despite the documentation saying this field is a "const char *",
93 in Python 3.4 at least, it's really a "char *". */
94 buf->format = (char *) "c";
95
96 return ret;
97}
98
625f7b1c
AB
99/* General Python initialization callback. */
100
3965bff5 101static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
625f7b1c
AB
102gdbpy_initialize_membuf (void)
103{
104 membuf_object_type.tp_new = PyType_GenericNew;
336bb2a1 105 return gdbpy_type_ready (&membuf_object_type);
625f7b1c
AB
106}
107
3965bff5
AB
108GDBPY_INITIALIZE_FILE (gdbpy_initialize_membuf);
109
110\f
111
625f7b1c
AB
112static PyBufferProcs buffer_procs =
113{
114 get_buffer
115};
116
625f7b1c
AB
117PyTypeObject membuf_object_type = {
118 PyVarObject_HEAD_INIT (nullptr, 0)
119 "gdb.Membuf", /*tp_name*/
120 sizeof (membuf_object), /*tp_basicsize*/
121 0, /*tp_itemsize*/
122 mbpy_dealloc, /*tp_dealloc*/
123 0, /*tp_print*/
124 0, /*tp_getattr*/
125 0, /*tp_setattr*/
126 0, /*tp_compare*/
127 0, /*tp_repr*/
128 0, /*tp_as_number*/
129 0, /*tp_as_sequence*/
130 0, /*tp_as_mapping*/
131 0, /*tp_hash */
132 0, /*tp_call*/
133 mbpy_str, /*tp_str*/
134 0, /*tp_getattro*/
135 0, /*tp_setattro*/
136 &buffer_procs, /*tp_as_buffer*/
137 Py_TPFLAGS_DEFAULT, /*tp_flags*/
138 "GDB memory buffer object", /*tp_doc*/
139 0, /* tp_traverse */
140 0, /* tp_clear */
141 0, /* tp_richcompare */
142 0, /* tp_weaklistoffset */
143 0, /* tp_iter */
144 0, /* tp_iternext */
145 0, /* tp_methods */
146 0, /* tp_members */
147 0, /* tp_getset */
148 0, /* tp_base */
149 0, /* tp_dict */
150 0, /* tp_descr_get */
151 0, /* tp_descr_set */
152 0, /* tp_dictoffset */
153 0, /* tp_init */
154 0, /* tp_alloc */
155};