GDB (xrefs)
py-utils.c
Go to the documentation of this file.
1 /* General utility routines for GDB/Python.
2 
3  Copyright (C) 2008-2015 Free Software Foundation, Inc.
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 
20 #include "defs.h"
21 #include "charset.h"
22 #include "value.h"
23 #include "python-internal.h"
24 
25 
26 /* This is a cleanup function which decrements the refcount on a
27  Python object. */
28 
29 static void
30 py_decref (void *p)
31 {
32  PyObject *py = p;
33 
34  Py_DECREF (py);
35 }
36 
37 /* Return a new cleanup which will decrement the Python object's
38  refcount when run. */
39 
40 struct cleanup *
41 make_cleanup_py_decref (PyObject *py)
42 {
43  return make_cleanup (py_decref, (void *) py);
44 }
45 
46 /* This is a cleanup function which decrements the refcount on a
47  Python object. This function accounts appropriately for NULL
48  references. */
49 
50 static void
51 py_xdecref (void *p)
52 {
53  PyObject *py = p;
54 
55  Py_XDECREF (py);
56 }
57 
58 /* Return a new cleanup which will decrement the Python object's
59  refcount when run. Account for and operate on NULL references
60  correctly. */
61 
62 struct cleanup *
64 {
65  return make_cleanup (py_xdecref, py);
66 }
67 
68 /* Converts a Python 8-bit string to a unicode string object. Assumes the
69  8-bit string is in the host charset. If an error occurs during conversion,
70  returns NULL with a python exception set.
71 
72  As an added bonus, the functions accepts a unicode string and returns it
73  right away, so callers don't need to check which kind of string they've
74  got. In Python 3, all strings are Unicode so this case is always the
75  one that applies.
76 
77  If the given object is not one of the mentioned string types, NULL is
78  returned, with the TypeError python exception set. */
79 PyObject *
80 python_string_to_unicode (PyObject *obj)
81 {
82  PyObject *unicode_str;
83 
84  /* If obj is already a unicode string, just return it.
85  I wish life was always that simple... */
86  if (PyUnicode_Check (obj))
87  {
88  unicode_str = obj;
89  Py_INCREF (obj);
90  }
91 #ifndef IS_PY3K
92  else if (PyString_Check (obj))
93  unicode_str = PyUnicode_FromEncodedObject (obj, host_charset (), NULL);
94 #endif
95  else
96  {
97  PyErr_SetString (PyExc_TypeError,
98  _("Expected a string or unicode object."));
99  unicode_str = NULL;
100  }
101 
102  return unicode_str;
103 }
104 
105 /* Returns a newly allocated string with the contents of the given unicode
106  string object converted to CHARSET. If an error occurs during the
107  conversion, NULL will be returned and a python exception will be set.
108 
109  The caller is responsible for xfree'ing the string. */
110 static char *
111 unicode_to_encoded_string (PyObject *unicode_str, const char *charset)
112 {
113  char *result;
114  PyObject *string;
115 
116  /* Translate string to named charset. */
117  string = PyUnicode_AsEncodedString (unicode_str, charset, NULL);
118  if (string == NULL)
119  return NULL;
120 
121 #ifdef IS_PY3K
122  result = xstrdup (PyBytes_AsString (string));
123 #else
124  result = xstrdup (PyString_AsString (string));
125 #endif
126 
127  Py_DECREF (string);
128 
129  return result;
130 }
131 
132 /* Returns a PyObject with the contents of the given unicode string
133  object converted to a named charset. If an error occurs during
134  the conversion, NULL will be returned and a python exception will
135  be set. */
136 static PyObject *
137 unicode_to_encoded_python_string (PyObject *unicode_str, const char *charset)
138 {
139  /* Translate string to named charset. */
140  return PyUnicode_AsEncodedString (unicode_str, charset, NULL);
141 }
142 
143 /* Returns a newly allocated string with the contents of the given unicode
144  string object converted to the target's charset. If an error occurs during
145  the conversion, NULL will be returned and a python exception will be set.
146 
147  The caller is responsible for xfree'ing the string. */
148 char *
149 unicode_to_target_string (PyObject *unicode_str)
150 {
151  return unicode_to_encoded_string (unicode_str,
153 }
154 
155 /* Returns a PyObject with the contents of the given unicode string
156  object converted to the target's charset. If an error occurs
157  during the conversion, NULL will be returned and a python exception
158  will be set. */
159 static PyObject *
160 unicode_to_target_python_string (PyObject *unicode_str)
161 {
162  return unicode_to_encoded_python_string (unicode_str,
164 }
165 
166 /* Converts a python string (8-bit or unicode) to a target string in
167  the target's charset. Returns NULL on error, with a python exception set.
168 
169  The caller is responsible for xfree'ing the string. */
170 char *
172 {
173  PyObject *str;
174  char *result;
175 
176  str = python_string_to_unicode (obj);
177  if (str == NULL)
178  return NULL;
179 
180  result = unicode_to_target_string (str);
181  Py_DECREF (str);
182  return result;
183 }
184 
185 /* Converts a python string (8-bit or unicode) to a target string in the
186  target's charset. Returns NULL on error, with a python exception
187  set.
188 
189  In Python 3, the returned object is a "bytes" object (not a string). */
190 PyObject *
192 {
193  PyObject *str;
194  PyObject *result;
195 
196  str = python_string_to_unicode (obj);
197  if (str == NULL)
198  return NULL;
199 
200  result = unicode_to_target_python_string (str);
201  Py_DECREF (str);
202  return result;
203 }
204 
205 /* Converts a python string (8-bit or unicode) to a target string in
206  the host's charset. Returns NULL on error, with a python exception set.
207 
208  The caller is responsible for xfree'ing the string. */
209 char *
211 {
212  PyObject *str;
213  char *result;
214 
215  str = python_string_to_unicode (obj);
216  if (str == NULL)
217  return NULL;
218 
219  result = unicode_to_encoded_string (str, host_charset ());
220  Py_DECREF (str);
221  return result;
222 }
223 
224 /* Return true if OBJ is a Python string or unicode object, false
225  otherwise. */
226 
227 int
228 gdbpy_is_string (PyObject *obj)
229 {
230 #ifdef IS_PY3K
231  return PyUnicode_Check (obj);
232 #else
233  return PyString_Check (obj) || PyUnicode_Check (obj);
234 #endif
235 }
236 
237 /* Return the string representation of OBJ, i.e., str (obj).
238  Space for the result is malloc'd, the caller must free.
239  If the result is NULL a python error occurred, the caller must clear it. */
240 
241 char *
242 gdbpy_obj_to_string (PyObject *obj)
243 {
244  PyObject *str_obj = PyObject_Str (obj);
245 
246  if (str_obj != NULL)
247  {
248 #ifdef IS_PY3K
249  char *msg = python_string_to_host_string (str_obj);
250 #else
251  char *msg = xstrdup (PyString_AsString (str_obj));
252 #endif
253 
254  Py_DECREF (str_obj);
255  return msg;
256  }
257 
258  return NULL;
259 }
260 
261 /* Return the string representation of the exception represented by
262  TYPE, VALUE which is assumed to have been obtained with PyErr_Fetch,
263  i.e., the error indicator is currently clear.
264  Space for the result is malloc'd, the caller must free.
265  If the result is NULL a python error occurred, the caller must clear it. */
266 
267 char *
268 gdbpy_exception_to_string (PyObject *ptype, PyObject *pvalue)
269 {
270  char *str;
271 
272  /* There are a few cases to consider.
273  For example:
274  pvalue is a string when PyErr_SetString is used.
275  pvalue is not a string when raise "foo" is used, instead it is None
276  and ptype is "foo".
277  So the algorithm we use is to print `str (pvalue)' if it's not
278  None, otherwise we print `str (ptype)'.
279  Using str (aka PyObject_Str) will fetch the error message from
280  gdb.GdbError ("message"). */
281 
282  if (pvalue && pvalue != Py_None)
283  str = gdbpy_obj_to_string (pvalue);
284  else
285  str = gdbpy_obj_to_string (ptype);
286 
287  return str;
288 }
289 
290 /* Convert a GDB exception to the appropriate Python exception.
291 
292  This sets the Python error indicator. */
293 
294 void
296 {
297  PyObject *exc_class;
298 
299  if (exception.reason == RETURN_QUIT)
300  exc_class = PyExc_KeyboardInterrupt;
301  else if (exception.error == MEMORY_ERROR)
302  exc_class = gdbpy_gdb_memory_error;
303  else
304  exc_class = gdbpy_gdb_error;
305 
306  PyErr_Format (exc_class, "%s", exception.message);
307 }
308 
309 /* Converts OBJ to a CORE_ADDR value.
310 
311  Returns 0 on success or -1 on failure, with a Python exception set.
312 */
313 
314 int
315 get_addr_from_python (PyObject *obj, CORE_ADDR *addr)
316 {
317  if (gdbpy_is_value_object (obj))
318  {
319 
320  TRY
321  {
322  *addr = value_as_address (value_object_to_value (obj));
323  }
324  CATCH (except, RETURN_MASK_ALL)
325  {
327  }
328  END_CATCH
329  }
330  else
331  {
332  PyObject *num = PyNumber_Long (obj);
333  gdb_py_ulongest val;
334 
335  if (num == NULL)
336  return -1;
337 
338  val = gdb_py_long_as_ulongest (num);
339  Py_XDECREF (num);
340  if (PyErr_Occurred ())
341  return -1;
342 
343  if (sizeof (val) > sizeof (CORE_ADDR) && ((CORE_ADDR) val) != val)
344  {
345  PyErr_SetString (PyExc_ValueError,
346  _("Overflow converting to address."));
347  return -1;
348  }
349 
350  *addr = val;
351  }
352 
353  return 0;
354 }
355 
356 /* Convert a LONGEST to the appropriate Python object -- either an
357  integer object or a long object, depending on its value. */
358 
359 PyObject *
361 {
362 #ifdef IS_PY3K
363  if (sizeof (l) > sizeof (long))
364  return PyLong_FromLongLong (l);
365  return PyLong_FromLong (l);
366 #else
367 #ifdef HAVE_LONG_LONG /* Defined by Python. */
368  /* If we have 'long long', and the value overflows a 'long', use a
369  Python Long; otherwise use a Python Int. */
370  if (sizeof (l) > sizeof (long)
371  && (l > PyInt_GetMax () || l < (- (LONGEST) PyInt_GetMax ()) - 1))
372  return PyLong_FromLongLong (l);
373 #endif
374  return PyInt_FromLong (l);
375 #endif
376 }
377 
378 /* Convert a ULONGEST to the appropriate Python object -- either an
379  integer object or a long object, depending on its value. */
380 
381 PyObject *
383 {
384 #ifdef IS_PY3K
385  if (sizeof (l) > sizeof (unsigned long))
386  return PyLong_FromUnsignedLongLong (l);
387  return PyLong_FromUnsignedLong (l);
388 #else
389 #ifdef HAVE_LONG_LONG /* Defined by Python. */
390  /* If we have 'long long', and the value overflows a 'long', use a
391  Python Long; otherwise use a Python Int. */
392  if (sizeof (l) > sizeof (unsigned long) && l > PyInt_GetMax ())
393  return PyLong_FromUnsignedLongLong (l);
394 #endif
395 
396  if (l > PyInt_GetMax ())
397  return PyLong_FromUnsignedLong (l);
398 
399  return PyInt_FromLong (l);
400 #endif
401 }
402 
403 /* Like PyInt_AsLong, but returns 0 on failure, 1 on success, and puts
404  the value into an out parameter. */
405 
406 int
407 gdb_py_int_as_long (PyObject *obj, long *result)
408 {
409  *result = PyInt_AsLong (obj);
410  return ! (*result == -1 && PyErr_Occurred ());
411 }
412 
413 
414 
415 /* Generic implementation of the __dict__ attribute for objects that
416  have a dictionary. The CLOSURE argument should be the type object.
417  This only handles positive values for tp_dictoffset. */
418 
419 PyObject *
420 gdb_py_generic_dict (PyObject *self, void *closure)
421 {
422  PyObject *result;
423  PyTypeObject *type_obj = closure;
424  char *raw_ptr;
425 
426  raw_ptr = (char *) self + type_obj->tp_dictoffset;
427  result = * (PyObject **) raw_ptr;
428 
429  Py_INCREF (result);
430  return result;
431 }
432 
433 /* Like PyModule_AddObject, but does not steal a reference to
434  OBJECT. */
435 
436 int
437 gdb_pymodule_addobject (PyObject *module, const char *name, PyObject *object)
438 {
439  int result;
440 
441  Py_INCREF (object);
442  /* Python 2.4 did not have a 'const' here. */
443  result = PyModule_AddObject (module, (char *) name, object);
444  if (result < 0)
445  Py_DECREF (object);
446  return result;
447 }
const char * string
Definition: signals.c:50
int gdb_pymodule_addobject(PyObject *module, const char *name, PyObject *object)
Definition: py-utils.c:437
#define Py_DECREF(op)
const char * target_charset(struct gdbarch *gdbarch)
Definition: charset.c:407
PyObject * gdb_py_generic_dict(PyObject *self, void *closure)
Definition: py-utils.c:420
bfd_vma CORE_ADDR
Definition: common-types.h:41
int gdbpy_is_string(PyObject *obj)
Definition: py-utils.c:228
unsigned long gdb_py_ulongest
char * gdbpy_obj_to_string(PyObject *obj)
Definition: py-utils.c:242
enum errors error
PyObject * python_string_to_target_python_string(PyObject *obj)
Definition: py-utils.c:191
#define _(String)
Definition: gdb_locale.h:40
char * python_string_to_target_string(PyObject *obj)
Definition: py-utils.c:171
#define END_CATCH
PyObject * python_string_to_unicode(PyObject *obj)
Definition: py-utils.c:80
static char * unicode_to_encoded_string(PyObject *unicode_str, const char *charset)
Definition: py-utils.c:111
#define TRY
struct cleanup * make_cleanup_py_xdecref(PyObject *py)
Definition: py-utils.c:63
const char *const name
Definition: aarch64-tdep.c:68
#define CATCH(EXCEPTION, MASK)
PyObject * gdb_py_object_from_longest(LONGEST l)
Definition: py-utils.c:360
PyObject * gdbpy_gdb_error
Definition: python.c:127
struct cleanup * make_cleanup(make_cleanup_ftype *function, void *arg)
Definition: cleanups.c:117
char * python_string_to_host_string(PyObject *obj)
Definition: py-utils.c:210
#define GDB_PY_SET_HANDLE_EXCEPTION(Exception)
struct gdbarch * python_gdbarch
Definition: python.c:201
char * unicode_to_target_string(PyObject *unicode_str)
Definition: py-utils.c:149
int gdbpy_is_value_object(PyObject *obj)
Definition: py-value.c:1700
static void py_xdecref(void *p)
Definition: py-utils.c:51
static void py_decref(void *p)
Definition: py-utils.c:30
PyObject * gdb_py_object_from_ulongest(ULONGEST l)
Definition: py-utils.c:382
void gdbpy_convert_exception(struct gdb_exception exception)
Definition: py-utils.c:295
static PyObject * unicode_to_target_python_string(PyObject *unicode_str)
Definition: py-utils.c:160
PyObject * gdbpy_gdb_memory_error
Definition: python.c:130
#define gdb_py_long_as_ulongest
unsigned long long ULONGEST
Definition: common-types.h:53
int gdb_py_int_as_long(PyObject *obj, long *result)
Definition: py-utils.c:407
const char * message
int get_addr_from_python(PyObject *obj, CORE_ADDR *addr)
Definition: py-utils.c:315
CORE_ADDR value_as_address(struct value *val)
Definition: value.c:2679
struct value * value_object_to_value(PyObject *self)
Definition: py-value.c:1544
const char * host_charset(void)
Definition: charset.c:399
static PyObject * unicode_to_encoded_python_string(PyObject *unicode_str, const char *charset)
Definition: py-utils.c:137
struct cleanup * make_cleanup_py_decref(PyObject *py)
Definition: py-utils.c:41
char * gdbpy_exception_to_string(PyObject *ptype, PyObject *pvalue)
Definition: py-utils.c:268
long long LONGEST
Definition: common-types.h:52
enum return_reason reason