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. */
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
);
36 scm_c_export (sv
->name
, NULL
);
40 /* Define FUNCTIONS in the gdb module. */
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
,
52 scm_set_procedure_property_x (proc
, gdbscm_documentation_symbol
,
53 gdbscm_scm_from_c_string (sf
->doc_string
));
55 scm_c_export (sf
->name
, NULL
);
59 /* Define CONSTANTS in the gdb module. */
62 gdbscm_define_integer_constants (const scheme_integer_constant
*constants
,
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
));
71 scm_c_export (sc
->name
, NULL
);
75 /* scm_printf, alas it doesn't exist. */
78 gdbscm_printf (SCM port
, const char *format
, ...)
82 va_start (args
, format
);
83 std::string string
= string_vprintf (format
, args
);
85 scm_puts (string
.c_str (), port
);
88 /* Utility for calling from gdb to "display" an SCM object. */
91 gdbscm_debug_display (SCM obj
)
93 SCM port
= scm_current_output_port ();
95 scm_display (obj
, port
);
97 scm_force_output (port
);
100 /* Utility for calling from gdb to "write" an SCM object. */
103 gdbscm_debug_write (SCM obj
)
105 SCM port
= scm_current_output_port ();
107 scm_write (obj
, port
);
109 scm_force_output (port
);
112 /* Subroutine of gdbscm_parse_function_args to simplify it.
113 Return the number of keyword arguments. */
116 count_keywords (const SCM
*keywords
)
120 if (keywords
== NULL
)
122 for (i
= 0; keywords
[i
] != SCM_BOOL_F
; ++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. */
133 validate_arg_format (const char *format
)
136 int length
= strlen (format
);
137 int optional_position
= -1;
138 int keyword_position
= -1;
141 gdb_assert (length
> 0);
143 for (p
= format
; *p
!= '\0'; ++p
)
158 gdb_assert (keyword_position
< 0);
159 gdb_assert (optional_position
< 0);
160 optional_position
= p
- format
;
163 gdb_assert (keyword_position
< 0);
164 keyword_position
= p
- format
;
167 gdb_assert (p
[1] == '\0');
171 gdb_assert_not_reached ("invalid argument format character");
178 /* Our version of SCM_ASSERT_TYPE that calls gdbscm_make_type_error. */
179 #define CHECK_TYPE(ok, arg, position, func_name, expected_type) \
183 return gdbscm_make_type_error ((func_name), (position), (arg), \
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. */
194 extract_arg (char format_char
, SCM arg
, void *argp
,
195 const char *func_name
, int position
)
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 ();
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
,
215 *arg_ptr
= gdbscm_is_true (arg
);
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
);
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
);
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
);
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
);
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
);
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
);
274 SCM
*arg_ptr
= (SCM
*) argp
;
280 gdb_assert_not_reached ("invalid argument format character");
288 /* Look up KEYWORD in KEYWORD_LIST.
289 The result is the index of the keyword in the list or -1 if not found. */
292 lookup_keyword (const SCM
*keyword_list
, SCM keyword
)
296 while (keyword_list
[i
] != SCM_BOOL_F
)
298 if (scm_is_eq (keyword_list
[i
], keyword
))
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. */
313 gdbscm_parse_function_args_1 (const char *func_name
,
314 int beginning_arg_pos
,
316 const char *format
, va_list args
)
319 int i
, have_rest
, num_keywords
, position
;
320 int have_optional
= 0;
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
);
330 position
= beginning_arg_pos
;
332 /* Process required, optional arguments. */
334 while (*p
&& *p
!= '#' && *p
!= '.')
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
))
354 allocated_strings
.push_back (*(char **) arg_ptr
);
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
== '#');
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
);
387 status
= gdbscm_make_error (scm_arg_type_key
, func_name
,
388 _("Unrecognized keyword: ~a"),
389 scm_list_1 (keyword
), keyword
);
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
);
400 keyword_args
[i
] = scm_cadr (rest
);
401 keyword_positions
[i
] = position
+ 1;
402 rest
= scm_cddr (rest
);
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
))
420 allocated_strings
.push_back (*(char **) arg_ptr
);
425 /* Process "rest" arguments. */
429 if (num_keywords
> 0)
431 SCM
*rest_ptr
= va_arg (args
, SCM
*);
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
);
447 /* Return anything not-an-exception. */
451 for (char *ptr
: allocated_strings
)
454 /* Return the exception, which gdbscm_wrap takes care of
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.
474 s - string -> char *, malloc'd
475 t - boolean (gdb uses "t", for biT?) -> int
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
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. */
527 gdbscm_parse_function_args (const char *func_name
,
528 int beginning_arg_pos
,
530 const char *format
, ...)
533 va_start (args
, format
);
535 gdbscm_wrap (gdbscm_parse_function_args_1
, func_name
,
536 beginning_arg_pos
, keywords
, format
, 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). */
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). */
575 gdbscm_scm_to_ulongest (SCM u
)
577 return scm_to_uint64 (u
);
580 /* Same as scm_dynwind_free, but uses xfree. */
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. */
599 gdbscm_gc_xstrdup (const char *str
)
601 size_t len
= strlen (str
);
603 = (char *) scm_gc_malloc_pointerless (len
+ 1, "gdbscm_gc_xstrdup");
605 strcpy (result
, str
);
609 /* Return a duplicate of ARGV living on the GC heap. */
612 gdbscm_gc_dup_argv (char **argv
)
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 *))
625 "parameter enum list");
626 p
= (char *) &result
[len
+ 1];
628 for (i
= 0; i
< len
; ++i
)
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
)
647 if (major
< gdbscm_guile_major_version
)
649 if (minor
> gdbscm_guile_minor_version
)
651 if (minor
< gdbscm_guile_minor_version
)
653 if (micro
> gdbscm_guile_micro_version
)