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