]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/python/py-utils.c
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / python / py-utils.c
CommitLineData
d57a3c85
TJB
1/* General utility routines for GDB/Python.
2
1d506c26 3 Copyright (C) 2008-2024 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"
ef0f16cc 21#include "top.h"
d57a3c85 22#include "charset.h"
595939de 23#include "value.h"
d57a3c85 24#include "python-internal.h"
d57a3c85 25
d57a3c85
TJB
26/* Converts a Python 8-bit string to a unicode string object. Assumes the
27 8-bit string is in the host charset. If an error occurs during conversion,
28 returns NULL with a python exception set.
29
30 As an added bonus, the functions accepts a unicode string and returns it
31 right away, so callers don't need to check which kind of string they've
256458bc 32 got. In Python 3, all strings are Unicode so this case is always the
9a27f2c6 33 one that applies.
d57a3c85
TJB
34
35 If the given object is not one of the mentioned string types, NULL is
36 returned, with the TypeError python exception set. */
833d985d 37gdbpy_ref<>
d57a3c85
TJB
38python_string_to_unicode (PyObject *obj)
39{
40 PyObject *unicode_str;
41
42 /* If obj is already a unicode string, just return it.
43 I wish life was always that simple... */
44 if (PyUnicode_Check (obj))
83390453
PM
45 {
46 unicode_str = obj;
47 Py_INCREF (obj);
48 }
d57a3c85
TJB
49 else
50 {
51 PyErr_SetString (PyExc_TypeError,
edae3fd6 52 _("Expected a string object."));
d57a3c85
TJB
53 unicode_str = NULL;
54 }
55
833d985d 56 return gdbpy_ref<> (unicode_str);
d57a3c85
TJB
57}
58
59/* Returns a newly allocated string with the contents of the given unicode
08c637de 60 string object converted to CHARSET. If an error occurs during the
075c55e0
TT
61 conversion, NULL will be returned and a python exception will be
62 set. */
9b972014 63static gdb::unique_xmalloc_ptr<char>
08c637de 64unicode_to_encoded_string (PyObject *unicode_str, const char *charset)
d57a3c85 65{
08c637de 66 /* Translate string to named charset. */
7780f186 67 gdbpy_ref<> string (PyUnicode_AsEncodedString (unicode_str, charset, NULL));
d57a3c85
TJB
68 if (string == NULL)
69 return NULL;
70
ea38e5df
TT
71 return gdb::unique_xmalloc_ptr<char>
72 (xstrdup (PyBytes_AsString (string.get ())));
08c637de
TJB
73}
74
fbb8f299
PM
75/* Returns a PyObject with the contents of the given unicode string
76 object converted to a named charset. If an error occurs during
77 the conversion, NULL will be returned and a python exception will
78 be set. */
833d985d 79static gdbpy_ref<>
fbb8f299
PM
80unicode_to_encoded_python_string (PyObject *unicode_str, const char *charset)
81{
fbb8f299 82 /* Translate string to named charset. */
833d985d 83 return gdbpy_ref<> (PyUnicode_AsEncodedString (unicode_str, charset, NULL));
fbb8f299
PM
84}
85
9b972014
TT
86/* Returns a newly allocated string with the contents of the given
87 unicode string object converted to the target's charset. If an
88 error occurs during the conversion, NULL will be returned and a
89 python exception will be set. */
90gdb::unique_xmalloc_ptr<char>
08c637de
TJB
91unicode_to_target_string (PyObject *unicode_str)
92{
1da5d0e6
TT
93 return (unicode_to_encoded_string
94 (unicode_str,
95 target_charset (gdbpy_enter::get_gdbarch ())));
d57a3c85
TJB
96}
97
fbb8f299
PM
98/* Returns a PyObject with the contents of the given unicode string
99 object converted to the target's charset. If an error occurs
100 during the conversion, NULL will be returned and a python exception
101 will be set. */
833d985d 102static gdbpy_ref<>
fbb8f299
PM
103unicode_to_target_python_string (PyObject *unicode_str)
104{
1da5d0e6
TT
105 return (unicode_to_encoded_python_string
106 (unicode_str,
107 target_charset (gdbpy_enter::get_gdbarch ())));
fbb8f299
PM
108}
109
d57a3c85 110/* Converts a python string (8-bit or unicode) to a target string in
9b972014
TT
111 the target's charset. Returns NULL on error, with a python
112 exception set. */
113gdb::unique_xmalloc_ptr<char>
d57a3c85
TJB
114python_string_to_target_string (PyObject *obj)
115{
833d985d 116 gdbpy_ref<> str = python_string_to_unicode (obj);
d57a3c85
TJB
117 if (str == NULL)
118 return NULL;
119
830a4934 120 return unicode_to_target_string (str.get ());
d57a3c85 121}
08c637de 122
fbb8f299
PM
123/* Converts a python string (8-bit or unicode) to a target string in the
124 target's charset. Returns NULL on error, with a python exception
9a27f2c6
PK
125 set.
126
127 In Python 3, the returned object is a "bytes" object (not a string). */
833d985d 128gdbpy_ref<>
fbb8f299
PM
129python_string_to_target_python_string (PyObject *obj)
130{
833d985d 131 gdbpy_ref<> str = python_string_to_unicode (obj);
fbb8f299 132 if (str == NULL)
833d985d 133 return str;
fbb8f299 134
830a4934 135 return unicode_to_target_python_string (str.get ());
fbb8f299
PM
136}
137
08c637de 138/* Converts a python string (8-bit or unicode) to a target string in
9b972014
TT
139 the host's charset. Returns NULL on error, with a python exception
140 set. */
141gdb::unique_xmalloc_ptr<char>
08c637de
TJB
142python_string_to_host_string (PyObject *obj)
143{
833d985d 144 gdbpy_ref<> str = python_string_to_unicode (obj);
08c637de
TJB
145 if (str == NULL)
146 return NULL;
147
830a4934 148 return unicode_to_encoded_string (str.get (), host_charset ());
08c637de
TJB
149}
150
4ae6cc19
DE
151/* Convert a host string to a python string. */
152
833d985d 153gdbpy_ref<>
4ae6cc19
DE
154host_string_to_python_string (const char *str)
155{
5aee4587
SM
156 return gdbpy_ref<> (PyUnicode_Decode (str, strlen (str), host_charset (),
157 NULL));
4ae6cc19
DE
158}
159
08c637de
TJB
160/* Return true if OBJ is a Python string or unicode object, false
161 otherwise. */
162
163int
164gdbpy_is_string (PyObject *obj)
165{
9a27f2c6 166 return PyUnicode_Check (obj);
08c637de 167}
07ca107c
DE
168
169/* Return the string representation of OBJ, i.e., str (obj).
07ca107c
DE
170 If the result is NULL a python error occurred, the caller must clear it. */
171
9b972014 172gdb::unique_xmalloc_ptr<char>
07ca107c
DE
173gdbpy_obj_to_string (PyObject *obj)
174{
7780f186 175 gdbpy_ref<> str_obj (PyObject_Str (obj));
07ca107c
DE
176
177 if (str_obj != NULL)
edae3fd6 178 return python_string_to_host_string (str_obj.get ());
07ca107c
DE
179
180 return NULL;
181}
182
5c329e6a 183/* See python-internal.h. */
07ca107c 184
9b972014 185gdb::unique_xmalloc_ptr<char>
5c329e6a 186gdbpy_err_fetch::to_string () const
07ca107c 187{
07ca107c
DE
188 /* There are a few cases to consider.
189 For example:
5c329e6a
TT
190 value is a string when PyErr_SetString is used.
191 value is not a string when raise "foo" is used, instead it is None
192 and type is "foo".
193 So the algorithm we use is to print `str (value)' if it's not
194 None, otherwise we print `str (type)'.
07ca107c
DE
195 Using str (aka PyObject_Str) will fetch the error message from
196 gdb.GdbError ("message"). */
197
8a0b6047
AB
198 if (m_error_value.get () != nullptr && m_error_value.get () != Py_None)
199 return gdbpy_obj_to_string (m_error_value.get ());
07ca107c 200 else
8a0b6047 201 return gdbpy_obj_to_string (m_error_type.get ());
5c329e6a
TT
202}
203
204/* See python-internal.h. */
205
206gdb::unique_xmalloc_ptr<char>
207gdbpy_err_fetch::type_to_string () const
208{
8a0b6047 209 return gdbpy_obj_to_string (m_error_type.get ());
07ca107c 210}
595939de 211
621c8364 212/* Convert a GDB exception to the appropriate Python exception.
256458bc 213
56cc411c 214 This sets the Python error indicator. */
621c8364 215
56cc411c 216void
94aeb44b 217gdbpy_convert_exception (const struct gdb_exception &exception)
621c8364
TT
218{
219 PyObject *exc_class;
220
221 if (exception.reason == RETURN_QUIT)
222 exc_class = PyExc_KeyboardInterrupt;
b940a061
KB
223 else if (exception.reason == RETURN_FORCED_QUIT)
224 quit_force (NULL, 0);
621c8364
TT
225 else if (exception.error == MEMORY_ERROR)
226 exc_class = gdbpy_gdb_memory_error;
227 else
228 exc_class = gdbpy_gdb_error;
229
3d6e9d23 230 PyErr_Format (exc_class, "%s", exception.what ());
621c8364
TT
231}
232
595939de
PM
233/* Converts OBJ to a CORE_ADDR value.
234
b86af38a 235 Returns 0 on success or -1 on failure, with a Python exception set.
595939de
PM
236*/
237
238int
239get_addr_from_python (PyObject *obj, CORE_ADDR *addr)
240{
241 if (gdbpy_is_value_object (obj))
b86af38a 242 {
b86af38a 243
a70b8144 244 try
b86af38a
TT
245 {
246 *addr = value_as_address (value_object_to_value (obj));
247 }
230d2906 248 catch (const gdb_exception &except)
492d29ea
PA
249 {
250 GDB_PY_SET_HANDLE_EXCEPTION (except);
251 }
b86af38a 252 }
74aedc46 253 else
595939de 254 {
7780f186 255 gdbpy_ref<> num (PyNumber_Long (obj));
74aedc46
TT
256 gdb_py_ulongest val;
257
258 if (num == NULL)
b86af38a 259 return -1;
595939de 260
830a4934 261 val = gdb_py_long_as_ulongest (num.get ());
74aedc46 262 if (PyErr_Occurred ())
b86af38a 263 return -1;
595939de 264
74aedc46
TT
265 if (sizeof (val) > sizeof (CORE_ADDR) && ((CORE_ADDR) val) != val)
266 {
267 PyErr_SetString (PyExc_ValueError,
268 _("Overflow converting to address."));
b86af38a 269 return -1;
74aedc46 270 }
595939de 271
74aedc46 272 *addr = val;
595939de
PM
273 }
274
b86af38a 275 return 0;
595939de 276}
74aedc46
TT
277
278/* Convert a LONGEST to the appropriate Python object -- either an
279 integer object or a long object, depending on its value. */
280
12dfa12a 281gdbpy_ref<>
74aedc46
TT
282gdb_py_object_from_longest (LONGEST l)
283{
9a27f2c6 284 if (sizeof (l) > sizeof (long))
12dfa12a
TT
285 return gdbpy_ref<> (PyLong_FromLongLong (l));
286 return gdbpy_ref<> (PyLong_FromLong (l));
74aedc46
TT
287}
288
289/* Convert a ULONGEST to the appropriate Python object -- either an
290 integer object or a long object, depending on its value. */
291
12dfa12a 292gdbpy_ref<>
74aedc46
TT
293gdb_py_object_from_ulongest (ULONGEST l)
294{
9a27f2c6 295 if (sizeof (l) > sizeof (unsigned long))
12dfa12a
TT
296 return gdbpy_ref<> (PyLong_FromUnsignedLongLong (l));
297 return gdbpy_ref<> (PyLong_FromUnsignedLong (l));
74aedc46
TT
298}
299
5aee4587 300/* Like PyLong_AsLong, but returns 0 on failure, 1 on success, and puts
74aedc46
TT
301 the value into an out parameter. */
302
303int
304gdb_py_int_as_long (PyObject *obj, long *result)
305{
5aee4587 306 *result = PyLong_AsLong (obj);
74aedc46
TT
307 return ! (*result == -1 && PyErr_Occurred ());
308}
2e8265fd
TT
309
310\f
311
312/* Generic implementation of the __dict__ attribute for objects that
313 have a dictionary. The CLOSURE argument should be the type object.
314 This only handles positive values for tp_dictoffset. */
315
316PyObject *
317gdb_py_generic_dict (PyObject *self, void *closure)
318{
319 PyObject *result;
19ba03f4 320 PyTypeObject *type_obj = (PyTypeObject *) closure;
2e8265fd
TT
321 char *raw_ptr;
322
323 raw_ptr = (char *) self + type_obj->tp_dictoffset;
324 result = * (PyObject **) raw_ptr;
325
326 Py_INCREF (result);
327 return result;
328}
aa36459a
TT
329
330/* Like PyModule_AddObject, but does not steal a reference to
331 OBJECT. */
332
333int
334gdb_pymodule_addobject (PyObject *module, const char *name, PyObject *object)
335{
336 int result;
337
338 Py_INCREF (object);
6c28e44a 339 result = PyModule_AddObject (module, name, object);
aa36459a 340 if (result < 0)
1915daeb 341 Py_DECREF (object);
aa36459a
TT
342 return result;
343}
2b4ad2fe 344
740b42ce
AB
345/* See python-internal.h. */
346
347void
348gdbpy_error (const char *fmt, ...)
349{
350 va_list ap;
351 va_start (ap, fmt);
352 std::string str = string_vprintf (fmt, ap);
353 va_end (ap);
354
355 const char *msg = str.c_str ();
356 if (msg != nullptr && *msg != '\0')
357 error (_("Error occurred in Python: %s"), msg);
358 else
359 error (_("Error occurred in Python."));
360}
361
2b4ad2fe
TT
362/* Handle a Python exception when the special gdb.GdbError treatment
363 is desired. This should only be called when an exception is set.
364 If the exception is a gdb.GdbError, throw a gdb exception with the
365 exception text. For other exceptions, print the Python stack and
366 then throw a gdb exception. */
367
368void
369gdbpy_handle_exception ()
370{
5c329e6a
TT
371 gdbpy_err_fetch fetched_error;
372 gdb::unique_xmalloc_ptr<char> msg = fetched_error.to_string ();
2b4ad2fe
TT
373
374 if (msg == NULL)
375 {
376 /* An error occurred computing the string representation of the
377 error message. This is rare, but we should inform the user. */
6cb06a8c
TT
378 gdb_printf (_("An error occurred in Python "
379 "and then another occurred computing the "
380 "error message.\n"));
2b4ad2fe
TT
381 gdbpy_print_stack ();
382 }
383
384 /* Don't print the stack for gdb.GdbError exceptions.
385 It is generally used to flag user errors.
386
387 We also don't want to print "Error occurred in Python command"
388 for user errors. However, a missing message for gdb.GdbError
389 exceptions is arguably a bug, so we flag it as such. */
390
5c329e6a 391 if (fetched_error.type_matches (PyExc_KeyboardInterrupt))
bc543c90 392 throw_quit ("Quit");
5c329e6a
TT
393 else if (! fetched_error.type_matches (gdbpy_gdberror_exc)
394 || msg == NULL || *msg == '\0')
2b4ad2fe 395 {
5c329e6a 396 fetched_error.restore ();
2b4ad2fe
TT
397 gdbpy_print_stack ();
398 if (msg != NULL && *msg != '\0')
399 error (_("Error occurred in Python: %s"), msg.get ());
400 else
401 error (_("Error occurred in Python."));
402 }
403 else
5c329e6a 404 error ("%s", msg.get ());
2b4ad2fe 405}
51e8dbe1
AB
406
407/* See python-internal.h. */
408
409gdb::unique_xmalloc_ptr<char>
410gdbpy_fix_doc_string_indentation (gdb::unique_xmalloc_ptr<char> doc)
411{
412 /* A structure used to track the white-space information on each line of
413 DOC. */
414 struct line_whitespace
415 {
416 /* Constructor. OFFSET is the offset from the start of DOC, WS_COUNT
417 is the number of whitespace characters starting at OFFSET. */
418 line_whitespace (size_t offset, int ws_count)
419 : m_offset (offset),
420 m_ws_count (ws_count)
421 { /* Nothing. */ }
422
423 /* The offset from the start of DOC. */
424 size_t offset () const
425 { return m_offset; }
426
427 /* The number of white-space characters at the start of this line. */
428 int ws () const
429 { return m_ws_count; }
430
431 private:
432 /* The offset from the start of DOC to the first character of this
433 line. */
434 size_t m_offset;
435
436 /* White space count on this line, the first character of this
437 whitespace is at OFFSET. */
438 int m_ws_count;
439 };
440
441 /* Count the number of white-space character starting at TXT. We
442 currently only count true single space characters, things like tabs,
443 newlines, etc are not counted. */
444 auto count_whitespace = [] (const char *txt) -> int
445 {
446 int count = 0;
447
448 while (*txt == ' ')
449 {
450 ++txt;
451 ++count;
452 }
453
454 return count;
455 };
456
457 /* In MIN_WHITESPACE we track the smallest number of whitespace
458 characters seen at the start of a line (that has actual content), this
459 is the number of characters that we can delete off all lines without
460 altering the relative indentation of all lines in DOC.
461
462 The first line often has no indentation, but instead starts immediates
463 after the 3-quotes marker within the Python doc string, so, if the
464 first line has zero white-space then we just ignore it, and don't set
465 MIN_WHITESPACE to zero.
466
467 Lines without any content should (ideally) have no white-space at
468 all, but if they do then they might have an artificially low number
469 (user left a single stray space at the start of an otherwise blank
470 line), we don't consider lines without content when updating the
471 MIN_WHITESPACE value. */
6b09f134 472 std::optional<int> min_whitespace;
51e8dbe1
AB
473
474 /* The index into WS_INFO at which the processing of DOC can be
475 considered "all done", that is, after this point there are no further
476 lines with useful content and we should just stop. */
6b09f134 477 std::optional<size_t> all_done_idx;
51e8dbe1
AB
478
479 /* White-space information for each line in DOC. */
480 std::vector<line_whitespace> ws_info;
481
482 /* Now look through DOC and collect the required information. */
483 const char *tmp = doc.get ();
484 while (*tmp != '\0')
485 {
486 /* Add an entry for the offset to the start of this line, and how
487 much white-space there is at the start of this line. */
488 size_t offset = tmp - doc.get ();
489 int ws_count = count_whitespace (tmp);
490 ws_info.emplace_back (offset, ws_count);
491
492 /* Skip over the white-space. */
493 tmp += ws_count;
494
495 /* Remember where the content of this line starts, and skip forward
496 to either the end of this line (newline) or the end of the DOC
497 string (null character), whichever comes first. */
498 const char *content_start = tmp;
499 while (*tmp != '\0' && *tmp != '\n')
500 ++tmp;
501
502 /* If this is not the first line, and if this line has some content,
503 then update MIN_WHITESPACE, this reflects the smallest number of
504 whitespace characters we can delete from all lines without
505 impacting the relative indentation of all the lines of DOC. */
506 if (offset > 0 && tmp > content_start)
507 {
508 if (!min_whitespace.has_value ())
509 min_whitespace = ws_count;
510 else
511 min_whitespace = std::min (*min_whitespace, ws_count);
512 }
513
514 /* Each time we encounter a line that has some content we update
515 ALL_DONE_IDX to be the index of the next line. If the last lines
516 of DOC don't contain any content then ALL_DONE_IDX will be left
517 pointing at an earlier line. When we rewrite DOC, when we reach
518 ALL_DONE_IDX then we can stop, the allows us to trim any blank
519 lines from the end of DOC. */
520 if (tmp > content_start)
521 all_done_idx = ws_info.size ();
522
523 /* If we reached a newline then skip forward to the start of the next
524 line. The other possibility at this point is that we're at the
525 very end of the DOC string (null terminator). */
526 if (*tmp == '\n')
527 ++tmp;
528 }
529
530 /* We found no lines with content, fail safe by just returning the
531 original documentation string. */
532 if (!all_done_idx.has_value () || !min_whitespace.has_value ())
533 return doc;
534
535 /* Setup DST and SRC, both pointing into the DOC string. We're going to
536 rewrite DOC in-place, as we only ever make DOC shorter (by removing
537 white-space), thus we know this will not overflow. */
538 char *dst = doc.get ();
539 char *src = doc.get ();
540
541 /* Array indices used with DST, SRC, and WS_INFO respectively. */
542 size_t dst_offset = 0;
543 size_t src_offset = 0;
544 size_t ws_info_offset = 0;
545
546 /* Now, walk over the source string, this is the original DOC. */
547 while (src[src_offset] != '\0')
548 {
549 /* If we are at the start of the next line (in WS_INFO), then we may
550 need to skip some white-space characters. */
551 if (src_offset == ws_info[ws_info_offset].offset ())
552 {
553 /* If a line has leading white-space then we need to skip over
554 some number of characters now. */
555 if (ws_info[ws_info_offset].ws () > 0)
556 {
557 /* If the line is entirely white-space then we skip all of
558 the white-space, the next character to copy will be the
559 newline or null character. Otherwise, we skip the just
560 some portion of the leading white-space. */
561 if (src[src_offset + ws_info[ws_info_offset].ws ()] == '\n'
562 || src[src_offset + ws_info[ws_info_offset].ws ()] == '\0')
563 src_offset += ws_info[ws_info_offset].ws ();
564 else
565 src_offset += std::min (*min_whitespace,
566 ws_info[ws_info_offset].ws ());
567
568 /* If we skipped white-space, and are now at the end of the
569 input, then we're done. */
570 if (src[src_offset] == '\0')
571 break;
572 }
573 if (ws_info_offset < (ws_info.size () - 1))
574 ++ws_info_offset;
575 if (ws_info_offset > *all_done_idx)
576 break;
577 }
578
579 /* Don't copy a newline to the start of the DST string, this would
580 result in a leading blank line. But in all other cases, copy the
581 next character into the destination string. */
582 if ((dst_offset > 0 || src[src_offset] != '\n'))
583 {
584 dst[dst_offset] = src[src_offset];
585 ++dst_offset;
586 }
587
588 /* Move to the next source character. */
589 ++src_offset;
590 }
591
592 /* Remove the trailing newline character(s), and ensure we have a null
593 terminator in place. */
594 while (dst_offset > 1 && dst[dst_offset - 1] == '\n')
595 --dst_offset;
596 dst[dst_offset] = '\0';
597
598 return doc;
599}
aef117b7
AB
600
601/* See python-internal.h. */
602
603PyObject *
604gdb_py_invalid_object_repr (PyObject *self)
605{
606 return PyUnicode_FromFormat ("<%s (invalid)>", Py_TYPE (self)->tp_name);
607}