Bump GDB's version number to 15.0.91.DATE-git.
[binutils-gdb.git] / gdb / guile / scm-utils.c
blob45f42ba0ccf044d2816cd334f0dba9bf9e3bbd4e
1 /* General utility routines for GDB/Scheme code.
3 Copyright (C) 2014-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 /* See README file in this directory for implementation notes, coding
21 conventions, et.al. */
23 #include "guile-internal.h"
25 /* Define VARIABLES in the gdb module. */
27 void
28 gdbscm_define_variables (const scheme_variable *variables, int is_public)
30 const scheme_variable *sv;
32 for (sv = variables; sv->name != NULL; ++sv)
34 scm_c_define (sv->name, sv->value);
35 if (is_public)
36 scm_c_export (sv->name, NULL);
40 /* Define FUNCTIONS in the gdb module. */
42 void
43 gdbscm_define_functions (const scheme_function *functions, int is_public)
45 const scheme_function *sf;
47 for (sf = functions; sf->name != NULL; ++sf)
49 SCM proc = scm_c_define_gsubr (sf->name, sf->required, sf->optional,
50 sf->rest, sf->func);
52 scm_set_procedure_property_x (proc, gdbscm_documentation_symbol,
53 gdbscm_scm_from_c_string (sf->doc_string));
54 if (is_public)
55 scm_c_export (sf->name, NULL);
59 /* Define CONSTANTS in the gdb module. */
61 void
62 gdbscm_define_integer_constants (const scheme_integer_constant *constants,
63 int is_public)
65 const scheme_integer_constant *sc;
67 for (sc = constants; sc->name != NULL; ++sc)
69 scm_c_define (sc->name, scm_from_int (sc->value));
70 if (is_public)
71 scm_c_export (sc->name, NULL);
75 /* scm_printf, alas it doesn't exist. */
77 void
78 gdbscm_printf (SCM port, const char *format, ...)
80 va_list args;
82 va_start (args, format);
83 std::string string = string_vprintf (format, args);
84 va_end (args);
85 scm_puts (string.c_str (), port);
88 /* Utility for calling from gdb to "display" an SCM object. */
90 void
91 gdbscm_debug_display (SCM obj)
93 SCM port = scm_current_output_port ();
95 scm_display (obj, port);
96 scm_newline (port);
97 scm_force_output (port);
100 /* Utility for calling from gdb to "write" an SCM object. */
102 void
103 gdbscm_debug_write (SCM obj)
105 SCM port = scm_current_output_port ();
107 scm_write (obj, port);
108 scm_newline (port);
109 scm_force_output (port);
112 /* Subroutine of gdbscm_parse_function_args to simplify it.
113 Return the number of keyword arguments. */
115 static int
116 count_keywords (const SCM *keywords)
118 int i;
120 if (keywords == NULL)
121 return 0;
122 for (i = 0; keywords[i] != SCM_BOOL_F; ++i)
123 continue;
125 return i;
128 /* Subroutine of gdbscm_parse_function_args to simplify it.
129 Validate an argument format string.
130 The result is a boolean indicating if "." was seen. */
132 static int
133 validate_arg_format (const char *format)
135 const char *p;
136 int length = strlen (format);
137 int optional_position = -1;
138 int keyword_position = -1;
139 int dot_seen = 0;
141 gdb_assert (length > 0);
143 for (p = format; *p != '\0'; ++p)
145 switch (*p)
147 case 's':
148 case 't':
149 case 'i':
150 case 'u':
151 case 'l':
152 case 'n':
153 case 'L':
154 case 'U':
155 case 'O':
156 break;
157 case '|':
158 gdb_assert (keyword_position < 0);
159 gdb_assert (optional_position < 0);
160 optional_position = p - format;
161 break;
162 case '#':
163 gdb_assert (keyword_position < 0);
164 keyword_position = p - format;
165 break;
166 case '.':
167 gdb_assert (p[1] == '\0');
168 dot_seen = 1;
169 break;
170 default:
171 gdb_assert_not_reached ("invalid argument format character");
175 return dot_seen;
178 /* Our version of SCM_ASSERT_TYPE that calls gdbscm_make_type_error. */
179 #define CHECK_TYPE(ok, arg, position, func_name, expected_type) \
180 do { \
181 if (!(ok)) \
183 return gdbscm_make_type_error ((func_name), (position), (arg), \
184 (expected_type)); \
186 } while (0)
188 /* Subroutine of gdbscm_parse_function_args to simplify it.
189 Check the type of ARG against FORMAT_CHAR and extract the value.
190 POSITION is the position of ARG in the argument list.
191 The result is #f upon success or a <gdb:exception> object. */
193 static SCM
194 extract_arg (char format_char, SCM arg, void *argp,
195 const char *func_name, int position)
197 switch (format_char)
199 case 's':
201 char **arg_ptr = (char **) argp;
203 CHECK_TYPE (gdbscm_is_true (scm_string_p (arg)), arg, position,
204 func_name, _("string"));
205 *arg_ptr = gdbscm_scm_to_c_string (arg).release ();
206 break;
208 case 't':
210 int *arg_ptr = (int *) argp;
212 /* While in Scheme, anything non-#f is "true", we're strict. */
213 CHECK_TYPE (gdbscm_is_bool (arg), arg, position, func_name,
214 _("boolean"));
215 *arg_ptr = gdbscm_is_true (arg);
216 break;
218 case 'i':
220 int *arg_ptr = (int *) argp;
222 CHECK_TYPE (scm_is_signed_integer (arg, INT_MIN, INT_MAX),
223 arg, position, func_name, _("int"));
224 *arg_ptr = scm_to_int (arg);
225 break;
227 case 'u':
229 int *arg_ptr = (int *) argp;
231 CHECK_TYPE (scm_is_unsigned_integer (arg, 0, UINT_MAX),
232 arg, position, func_name, _("unsigned int"));
233 *arg_ptr = scm_to_uint (arg);
234 break;
236 case 'l':
238 long *arg_ptr = (long *) argp;
240 CHECK_TYPE (scm_is_signed_integer (arg, LONG_MIN, LONG_MAX),
241 arg, position, func_name, _("long"));
242 *arg_ptr = scm_to_long (arg);
243 break;
245 case 'n':
247 unsigned long *arg_ptr = (unsigned long *) argp;
249 CHECK_TYPE (scm_is_unsigned_integer (arg, 0, ULONG_MAX),
250 arg, position, func_name, _("unsigned long"));
251 *arg_ptr = scm_to_ulong (arg);
252 break;
254 case 'L':
256 LONGEST *arg_ptr = (LONGEST *) argp;
258 CHECK_TYPE (scm_is_signed_integer (arg, INT64_MIN, INT64_MAX),
259 arg, position, func_name, _("LONGEST"));
260 *arg_ptr = gdbscm_scm_to_longest (arg);
261 break;
263 case 'U':
265 ULONGEST *arg_ptr = (ULONGEST *) argp;
267 CHECK_TYPE (scm_is_unsigned_integer (arg, 0, UINT64_MAX),
268 arg, position, func_name, _("ULONGEST"));
269 *arg_ptr = gdbscm_scm_to_ulongest (arg);
270 break;
272 case 'O':
274 SCM *arg_ptr = (SCM *) argp;
276 *arg_ptr = arg;
277 break;
279 default:
280 gdb_assert_not_reached ("invalid argument format character");
283 return SCM_BOOL_F;
286 #undef CHECK_TYPE
288 /* Look up KEYWORD in KEYWORD_LIST.
289 The result is the index of the keyword in the list or -1 if not found. */
291 static int
292 lookup_keyword (const SCM *keyword_list, SCM keyword)
294 int i = 0;
296 while (keyword_list[i] != SCM_BOOL_F)
298 if (scm_is_eq (keyword_list[i], keyword))
299 return i;
300 ++i;
303 return -1;
307 /* Helper for gdbscm_parse_function_args that does most of the work,
308 in a separate function wrapped with gdbscm_wrap so that we can use
309 non-trivial-dtor objects here. The result is #f upon success or a
310 <gdb:exception> object otherwise. */
312 static SCM
313 gdbscm_parse_function_args_1 (const char *func_name,
314 int beginning_arg_pos,
315 const SCM *keywords,
316 const char *format, va_list args)
318 const char *p;
319 int i, have_rest, num_keywords, position;
320 int have_optional = 0;
321 SCM status;
322 SCM rest = SCM_EOL;
323 /* Keep track of malloc'd strings. We need to free them upon error. */
324 std::vector<char *> allocated_strings;
326 have_rest = validate_arg_format (format);
327 num_keywords = count_keywords (keywords);
329 p = format;
330 position = beginning_arg_pos;
332 /* Process required, optional arguments. */
334 while (*p && *p != '#' && *p != '.')
336 SCM arg;
337 void *arg_ptr;
339 if (*p == '|')
341 have_optional = 1;
342 ++p;
343 continue;
346 arg = va_arg (args, SCM);
347 if (!have_optional || !SCM_UNBNDP (arg))
349 arg_ptr = va_arg (args, void *);
350 status = extract_arg (*p, arg, arg_ptr, func_name, position);
351 if (!gdbscm_is_false (status))
352 goto fail;
353 if (*p == 's')
354 allocated_strings.push_back (*(char **) arg_ptr);
356 ++p;
357 ++position;
360 /* Process keyword arguments. */
362 if (have_rest || num_keywords > 0)
363 rest = va_arg (args, SCM);
365 if (num_keywords > 0)
367 SCM *keyword_args = XALLOCAVEC (SCM, num_keywords);
368 int *keyword_positions = XALLOCAVEC (int, num_keywords);
370 gdb_assert (*p == '#');
371 ++p;
373 for (i = 0; i < num_keywords; ++i)
375 keyword_args[i] = SCM_UNSPECIFIED;
376 keyword_positions[i] = -1;
379 while (scm_is_pair (rest)
380 && scm_is_keyword (scm_car (rest)))
382 SCM keyword = scm_car (rest);
384 i = lookup_keyword (keywords, keyword);
385 if (i < 0)
387 status = gdbscm_make_error (scm_arg_type_key, func_name,
388 _("Unrecognized keyword: ~a"),
389 scm_list_1 (keyword), keyword);
390 goto fail;
392 if (!scm_is_pair (scm_cdr (rest)))
394 status = gdbscm_make_error
395 (scm_arg_type_key, func_name,
396 _("Missing value for keyword argument"),
397 scm_list_1 (keyword), keyword);
398 goto fail;
400 keyword_args[i] = scm_cadr (rest);
401 keyword_positions[i] = position + 1;
402 rest = scm_cddr (rest);
403 position += 2;
406 for (i = 0; i < num_keywords; ++i)
408 int *arg_pos_ptr = va_arg (args, int *);
409 void *arg_ptr = va_arg (args, void *);
410 SCM arg = keyword_args[i];
412 if (! scm_is_eq (arg, SCM_UNSPECIFIED))
414 *arg_pos_ptr = keyword_positions[i];
415 status = extract_arg (p[i], arg, arg_ptr, func_name,
416 keyword_positions[i]);
417 if (!gdbscm_is_false (status))
418 goto fail;
419 if (p[i] == 's')
420 allocated_strings.push_back (*(char **) arg_ptr);
425 /* Process "rest" arguments. */
427 if (have_rest)
429 if (num_keywords > 0)
431 SCM *rest_ptr = va_arg (args, SCM *);
433 *rest_ptr = rest;
436 else
438 if (! scm_is_null (rest))
440 status = gdbscm_make_error (scm_args_number_key, func_name,
441 _("Too many arguments"),
442 SCM_EOL, SCM_BOOL_F);
443 goto fail;
447 /* Return anything not-an-exception. */
448 return SCM_BOOL_F;
450 fail:
451 for (char *ptr : allocated_strings)
452 xfree (ptr);
454 /* Return the exception, which gdbscm_wrap takes care of
455 throwing. */
456 return status;
459 /* Utility to parse required, optional, and keyword arguments to Scheme
460 functions. Modelled on PyArg_ParseTupleAndKeywords, but no attempt is made
461 at similarity or functionality.
462 There is no result, if there's an error a Scheme exception is thrown.
464 Guile provides scm_c_bind_keyword_arguments, and feel free to use it.
465 This is for times when we want a bit more parsing.
467 BEGINNING_ARG_POS is the position of the first argument passed to this
468 routine. It should be one of the SCM_ARGn values. It could be > SCM_ARG1
469 if the caller chooses not to parse one or more required arguments.
471 KEYWORDS may be NULL if there are no keywords.
473 FORMAT:
474 s - string -> char *, malloc'd
475 t - boolean (gdb uses "t", for biT?) -> int
476 i - int
477 u - unsigned int
478 l - long
479 n - unsigned long
480 L - longest
481 U - unsigned longest
482 O - random scheme object
483 | - indicates the next set is for optional arguments
484 # - indicates the next set is for keyword arguments (must follow |)
485 . - indicates "rest" arguments are present, this character must appear last
487 FORMAT must match the definition from scm_c_{make,define}_gsubr.
488 Required and optional arguments appear in order in the format string.
489 Afterwards, keyword-based arguments are processed. There must be as many
490 remaining characters in the format string as their are keywords.
491 Except for "|#.", the number of characters in the format string must match
492 #required + #optional + #keywords.
494 The function is required to be defined in a compatible manner:
495 #required-args and #optional-arguments must match, and rest-arguments
496 must be specified if keyword args are desired, and/or regular "rest" args.
498 Example: For this function,
499 scm_c_define_gsubr ("execute", 2, 3, 1, foo);
500 the format string + keyword list could be any of:
501 1) "ss|ttt#tt", { "key1", "key2", NULL }
502 2) "ss|ttt.", { NULL }
503 3) "ss|ttt#t.", { "key1", NULL }
505 For required and optional args pass the SCM of the argument, and a
506 pointer to the value to hold the parsed result (type depends on format
507 char). After that pass the SCM containing the "rest" arguments followed
508 by pointers to values to hold parsed keyword arguments, and if specified
509 a pointer to hold the remaining contents of "rest".
511 For keyword arguments pass two pointers: the first is a pointer to an int
512 that will contain the position of the argument in the arg list, and the
513 second will contain result of processing the argument. The int pointed
514 to by the first value should be initialized to -1. It can then be used
515 to tell whether the keyword was present.
517 If both keyword and rest arguments are present, the caller must pass a
518 pointer to contain the new value of rest (after keyword args have been
519 removed).
521 There's currently no way, that I know of, to specify default values for
522 optional arguments in C-provided functions. At the moment they're a
523 work-in-progress. The caller should test SCM_UNBNDP for each optional
524 argument. Unbound optional arguments are ignored. */
526 void
527 gdbscm_parse_function_args (const char *func_name,
528 int beginning_arg_pos,
529 const SCM *keywords,
530 const char *format, ...)
532 va_list args;
533 va_start (args, format);
535 gdbscm_wrap (gdbscm_parse_function_args_1, func_name,
536 beginning_arg_pos, keywords, format, args);
538 va_end (args);
542 /* Return longest L as a scheme object. */
545 gdbscm_scm_from_longest (LONGEST l)
547 return scm_from_int64 (l);
550 /* Convert scheme object L to LONGEST.
551 It is an error to call this if L is not an integer in range of LONGEST.
552 (because the underlying Scheme function will thrown an exception,
553 which is not part of our contract with the caller). */
555 LONGEST
556 gdbscm_scm_to_longest (SCM l)
558 return scm_to_int64 (l);
561 /* Return unsigned longest L as a scheme object. */
564 gdbscm_scm_from_ulongest (ULONGEST l)
566 return scm_from_uint64 (l);
569 /* Convert scheme object U to ULONGEST.
570 It is an error to call this if U is not an integer in range of ULONGEST
571 (because the underlying Scheme function will thrown an exception,
572 which is not part of our contract with the caller). */
574 ULONGEST
575 gdbscm_scm_to_ulongest (SCM u)
577 return scm_to_uint64 (u);
580 /* Same as scm_dynwind_free, but uses xfree. */
582 void
583 gdbscm_dynwind_xfree (void *ptr)
585 scm_dynwind_unwind_handler (xfree, ptr, SCM_F_WIND_EXPLICITLY);
588 /* Return non-zero if PROC is a procedure. */
591 gdbscm_is_procedure (SCM proc)
593 return gdbscm_is_true (scm_procedure_p (proc));
596 /* Same as xstrdup, but the string is allocated on the GC heap. */
598 char *
599 gdbscm_gc_xstrdup (const char *str)
601 size_t len = strlen (str);
602 char *result
603 = (char *) scm_gc_malloc_pointerless (len + 1, "gdbscm_gc_xstrdup");
605 strcpy (result, str);
606 return result;
609 /* Return a duplicate of ARGV living on the GC heap. */
611 const char * const *
612 gdbscm_gc_dup_argv (char **argv)
614 int i, len;
615 size_t string_space;
616 char *p, **result;
618 for (len = 0, string_space = 0; argv[len] != NULL; ++len)
619 string_space += strlen (argv[len]) + 1;
621 /* Allocating "pointerless" works because the pointers are all
622 self-contained within the object. */
623 result = (char **) scm_gc_malloc_pointerless (((len + 1) * sizeof (char *))
624 + string_space,
625 "parameter enum list");
626 p = (char *) &result[len + 1];
628 for (i = 0; i < len; ++i)
630 result[i] = p;
631 strcpy (p, argv[i]);
632 p += strlen (p) + 1;
634 result[i] = NULL;
636 return (const char * const *) result;
639 /* Return non-zero if the version of Guile being used it at least
640 MAJOR.MINOR.MICRO. */
643 gdbscm_guile_version_is_at_least (int major, int minor, int micro)
645 if (major > gdbscm_guile_major_version)
646 return 0;
647 if (major < gdbscm_guile_major_version)
648 return 1;
649 if (minor > gdbscm_guile_minor_version)
650 return 0;
651 if (minor < gdbscm_guile_minor_version)
652 return 1;
653 if (micro > gdbscm_guile_micro_version)
654 return 0;
655 return 1;