Commit | Line | Data |
---|---|---|
d57a3c85 TJB |
1 | /* General utility routines for GDB/Python. |
2 | ||
7b6bb8da | 3 | Copyright (C) 2008, 2009, 2010, 2011 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 | |
57 | got. | |
58 | ||
59 | If the given object is not one of the mentioned string types, NULL is | |
60 | returned, with the TypeError python exception set. */ | |
61 | PyObject * | |
62 | python_string_to_unicode (PyObject *obj) | |
63 | { | |
64 | PyObject *unicode_str; | |
65 | ||
66 | /* If obj is already a unicode string, just return it. | |
67 | I wish life was always that simple... */ | |
68 | if (PyUnicode_Check (obj)) | |
83390453 PM |
69 | { |
70 | unicode_str = obj; | |
71 | Py_INCREF (obj); | |
72 | } | |
73 | ||
d57a3c85 TJB |
74 | else if (PyString_Check (obj)) |
75 | unicode_str = PyUnicode_FromEncodedObject (obj, host_charset (), NULL); | |
76 | else | |
77 | { | |
78 | PyErr_SetString (PyExc_TypeError, | |
79 | _("Expected a string or unicode object.")); | |
80 | unicode_str = NULL; | |
81 | } | |
82 | ||
83 | return unicode_str; | |
84 | } | |
85 | ||
86 | /* Returns a newly allocated string with the contents of the given unicode | |
08c637de TJB |
87 | string object converted to CHARSET. If an error occurs during the |
88 | conversion, NULL will be returned and a python exception will be set. | |
d57a3c85 TJB |
89 | |
90 | The caller is responsible for xfree'ing the string. */ | |
08c637de TJB |
91 | static char * |
92 | unicode_to_encoded_string (PyObject *unicode_str, const char *charset) | |
d57a3c85 | 93 | { |
08c637de | 94 | char *result; |
d57a3c85 TJB |
95 | PyObject *string; |
96 | ||
08c637de TJB |
97 | /* Translate string to named charset. */ |
98 | string = PyUnicode_AsEncodedString (unicode_str, charset, NULL); | |
d57a3c85 TJB |
99 | if (string == NULL) |
100 | return NULL; | |
101 | ||
08c637de | 102 | result = xstrdup (PyString_AsString (string)); |
d57a3c85 TJB |
103 | |
104 | Py_DECREF (string); | |
105 | ||
08c637de TJB |
106 | return result; |
107 | } | |
108 | ||
fbb8f299 PM |
109 | /* Returns a PyObject with the contents of the given unicode string |
110 | object converted to a named charset. If an error occurs during | |
111 | the conversion, NULL will be returned and a python exception will | |
112 | be set. */ | |
113 | static PyObject * | |
114 | unicode_to_encoded_python_string (PyObject *unicode_str, const char *charset) | |
115 | { | |
116 | PyObject *string; | |
117 | ||
118 | /* Translate string to named charset. */ | |
119 | string = PyUnicode_AsEncodedString (unicode_str, charset, NULL); | |
120 | if (string == NULL) | |
121 | return NULL; | |
122 | ||
123 | return string; | |
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. */ | |
142 | PyObject * | |
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 | |
170 | set. */ | |
171 | PyObject * | |
172 | python_string_to_target_python_string (PyObject *obj) | |
173 | { | |
174 | PyObject *str; | |
175 | PyObject *result; | |
176 | ||
177 | str = python_string_to_unicode (obj); | |
178 | if (str == NULL) | |
179 | return NULL; | |
180 | ||
181 | result = unicode_to_target_python_string (str); | |
182 | Py_DECREF (str); | |
183 | return result; | |
184 | } | |
185 | ||
08c637de TJB |
186 | /* Converts a python string (8-bit or unicode) to a target string in |
187 | the host's charset. Returns NULL on error, with a python exception set. | |
188 | ||
189 | The caller is responsible for xfree'ing the string. */ | |
190 | char * | |
191 | python_string_to_host_string (PyObject *obj) | |
192 | { | |
193 | PyObject *str; | |
83390453 | 194 | char *result; |
08c637de TJB |
195 | |
196 | str = python_string_to_unicode (obj); | |
197 | if (str == NULL) | |
198 | return NULL; | |
199 | ||
83390453 PM |
200 | result = unicode_to_encoded_string (str, host_charset ()); |
201 | Py_DECREF (str); | |
202 | return result; | |
08c637de TJB |
203 | } |
204 | ||
b6cb8e7d TJB |
205 | /* Converts a target string of LENGTH bytes in the target's charset to a |
206 | Python Unicode string. If LENGTH is -1, convert until a null byte is found. | |
207 | ||
208 | Returns NULL on error, with a python exception set. */ | |
209 | PyObject * | |
210 | target_string_to_unicode (const gdb_byte *str, int length) | |
211 | { | |
212 | if (length == -1) | |
213 | length = strlen (str); | |
214 | ||
f870a310 | 215 | return PyUnicode_Decode (str, length, target_charset (python_gdbarch), NULL); |
b6cb8e7d TJB |
216 | } |
217 | ||
08c637de TJB |
218 | /* Return true if OBJ is a Python string or unicode object, false |
219 | otherwise. */ | |
220 | ||
221 | int | |
222 | gdbpy_is_string (PyObject *obj) | |
223 | { | |
224 | return PyString_Check (obj) || PyUnicode_Check (obj); | |
225 | } | |
07ca107c DE |
226 | |
227 | /* Return the string representation of OBJ, i.e., str (obj). | |
228 | Space for the result is malloc'd, the caller must free. | |
229 | If the result is NULL a python error occurred, the caller must clear it. */ | |
230 | ||
231 | char * | |
232 | gdbpy_obj_to_string (PyObject *obj) | |
233 | { | |
234 | PyObject *str_obj = PyObject_Str (obj); | |
235 | ||
236 | if (str_obj != NULL) | |
237 | { | |
238 | char *msg = xstrdup (PyString_AsString (str_obj)); | |
239 | ||
240 | Py_DECREF (str_obj); | |
241 | return msg; | |
242 | } | |
243 | ||
244 | return NULL; | |
245 | } | |
246 | ||
247 | /* Return the string representation of the exception represented by | |
248 | TYPE, VALUE which is assumed to have been obtained with PyErr_Fetch, | |
249 | i.e., the error indicator is currently clear. | |
250 | Space for the result is malloc'd, the caller must free. | |
251 | If the result is NULL a python error occurred, the caller must clear it. */ | |
252 | ||
253 | char * | |
254 | gdbpy_exception_to_string (PyObject *ptype, PyObject *pvalue) | |
255 | { | |
256 | PyObject *str_obj = PyObject_Str (pvalue); | |
257 | char *str; | |
258 | ||
259 | /* There are a few cases to consider. | |
260 | For example: | |
261 | pvalue is a string when PyErr_SetString is used. | |
262 | pvalue is not a string when raise "foo" is used, instead it is None | |
263 | and ptype is "foo". | |
264 | So the algorithm we use is to print `str (pvalue)' if it's not | |
265 | None, otherwise we print `str (ptype)'. | |
266 | Using str (aka PyObject_Str) will fetch the error message from | |
267 | gdb.GdbError ("message"). */ | |
268 | ||
269 | if (pvalue && pvalue != Py_None) | |
270 | str = gdbpy_obj_to_string (pvalue); | |
271 | else | |
272 | str = gdbpy_obj_to_string (ptype); | |
273 | ||
274 | return str; | |
275 | } | |
595939de | 276 | |
621c8364 TT |
277 | /* Convert a GDB exception to the appropriate Python exception. |
278 | ||
279 | This sets the Python error indicator, and returns NULL. */ | |
280 | ||
281 | PyObject * | |
282 | gdbpy_convert_exception (struct gdb_exception exception) | |
283 | { | |
284 | PyObject *exc_class; | |
285 | ||
286 | if (exception.reason == RETURN_QUIT) | |
287 | exc_class = PyExc_KeyboardInterrupt; | |
288 | else if (exception.error == MEMORY_ERROR) | |
289 | exc_class = gdbpy_gdb_memory_error; | |
290 | else | |
291 | exc_class = gdbpy_gdb_error; | |
292 | ||
293 | return PyErr_Format (exc_class, "%s", exception.message); | |
294 | } | |
295 | ||
595939de PM |
296 | /* Converts OBJ to a CORE_ADDR value. |
297 | ||
298 | Returns 1 on success or 0 on failure, with a Python exception set. This | |
299 | function can also throw GDB exceptions. | |
300 | */ | |
301 | ||
302 | int | |
303 | get_addr_from_python (PyObject *obj, CORE_ADDR *addr) | |
304 | { | |
305 | if (gdbpy_is_value_object (obj)) | |
306 | *addr = value_as_address (value_object_to_value (obj)); | |
307 | else if (PyLong_Check (obj)) | |
308 | { | |
309 | /* Assume CORE_ADDR corresponds to unsigned long. */ | |
310 | *addr = PyLong_AsUnsignedLong (obj); | |
311 | if (PyErr_Occurred () != NULL) | |
312 | return 0; | |
313 | } | |
314 | else if (PyInt_Check (obj)) | |
315 | { | |
316 | long val; | |
317 | ||
318 | /* Assume CORE_ADDR corresponds to unsigned long. */ | |
319 | val = PyInt_AsLong (obj); | |
320 | ||
321 | if (val >= 0) | |
322 | *addr = val; | |
323 | else | |
324 | { | |
325 | /* If no error ocurred, VAL is indeed negative. */ | |
326 | if (PyErr_Occurred () != NULL) | |
327 | return 0; | |
328 | ||
329 | PyErr_SetString (PyExc_ValueError, | |
330 | _("Supplied address is negative.")); | |
331 | return 0; | |
332 | } | |
333 | } | |
334 | else | |
335 | { | |
336 | PyErr_SetString (PyExc_TypeError, | |
337 | _("Invalid type for address.")); | |
338 | return 0; | |
339 | } | |
340 | ||
341 | return 1; | |
342 | } |