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