1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
|
/* General utility routines for GDB/Scheme code.
Copyright (C) 2014-2018 Free Software Foundation, Inc.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* See README file in this directory for implementation notes, coding
conventions, et.al. */
#include "defs.h"
#include "guile-internal.h"
/* Define VARIABLES in the gdb module. */
void
gdbscm_define_variables (const scheme_variable *variables, int is_public)
{
const scheme_variable *sv;
for (sv = variables; sv->name != NULL; ++sv)
{
scm_c_define (sv->name, sv->value);
if (is_public)
scm_c_export (sv->name, NULL);
}
}
/* Define FUNCTIONS in the gdb module. */
void
gdbscm_define_functions (const scheme_function *functions, int is_public)
{
const scheme_function *sf;
for (sf = functions; sf->name != NULL; ++sf)
{
SCM proc = scm_c_define_gsubr (sf->name, sf->required, sf->optional,
sf->rest, sf->func);
scm_set_procedure_property_x (proc, gdbscm_documentation_symbol,
gdbscm_scm_from_c_string (sf->doc_string));
if (is_public)
scm_c_export (sf->name, NULL);
}
}
/* Define CONSTANTS in the gdb module. */
void
gdbscm_define_integer_constants (const scheme_integer_constant *constants,
int is_public)
{
const scheme_integer_constant *sc;
for (sc = constants; sc->name != NULL; ++sc)
{
scm_c_define (sc->name, scm_from_int (sc->value));
if (is_public)
scm_c_export (sc->name, NULL);
}
}
/* scm_printf, alas it doesn't exist. */
void
gdbscm_printf (SCM port, const char *format, ...)
{
va_list args;
char *string;
va_start (args, format);
string = xstrvprintf (format, args);
va_end (args);
scm_puts (string, port);
xfree (string);
}
/* Utility for calling from gdb to "display" an SCM object. */
void
gdbscm_debug_display (SCM obj)
{
SCM port = scm_current_output_port ();
scm_display (obj, port);
scm_newline (port);
scm_force_output (port);
}
/* Utility for calling from gdb to "write" an SCM object. */
void
gdbscm_debug_write (SCM obj)
{
SCM port = scm_current_output_port ();
scm_write (obj, port);
scm_newline (port);
scm_force_output (port);
}
/* Subroutine of gdbscm_parse_function_args to simplify it.
Return the number of keyword arguments. */
static int
count_keywords (const SCM *keywords)
{
int i;
if (keywords == NULL)
return 0;
for (i = 0; keywords[i] != SCM_BOOL_F; ++i)
continue;
return i;
}
/* Subroutine of gdbscm_parse_function_args to simplify it.
Validate an argument format string.
The result is a boolean indicating if "." was seen. */
static int
validate_arg_format (const char *format)
{
const char *p;
int length = strlen (format);
int optional_position = -1;
int keyword_position = -1;
int dot_seen = 0;
gdb_assert (length > 0);
for (p = format; *p != '\0'; ++p)
{
switch (*p)
{
case 's':
case 't':
case 'i':
case 'u':
case 'l':
case 'n':
case 'L':
case 'U':
case 'O':
break;
case '|':
gdb_assert (keyword_position < 0);
gdb_assert (optional_position < 0);
optional_position = p - format;
break;
case '#':
gdb_assert (keyword_position < 0);
keyword_position = p - format;
break;
case '.':
gdb_assert (p[1] == '\0');
dot_seen = 1;
break;
default:
gdb_assert_not_reached ("invalid argument format character");
}
}
return dot_seen;
}
/* Our version of SCM_ASSERT_TYPE that calls gdbscm_make_type_error. */
#define CHECK_TYPE(ok, arg, position, func_name, expected_type) \
do { \
if (!(ok)) \
{ \
return gdbscm_make_type_error ((func_name), (position), (arg), \
(expected_type)); \
} \
} while (0)
/* Subroutine of gdbscm_parse_function_args to simplify it.
Check the type of ARG against FORMAT_CHAR and extract the value.
POSITION is the position of ARG in the argument list.
The result is #f upon success or a <gdb:exception> object. */
static SCM
extract_arg (char format_char, SCM arg, void *argp,
const char *func_name, int position)
{
switch (format_char)
{
case 's':
{
char **arg_ptr = (char **) argp;
CHECK_TYPE (gdbscm_is_true (scm_string_p (arg)), arg, position,
func_name, _("string"));
*arg_ptr = gdbscm_scm_to_c_string (arg).release ();
break;
}
case 't':
{
int *arg_ptr = (int *) argp;
/* While in Scheme, anything non-#f is "true", we're strict. */
CHECK_TYPE (gdbscm_is_bool (arg), arg, position, func_name,
_("boolean"));
*arg_ptr = gdbscm_is_true (arg);
break;
}
case 'i':
{
int *arg_ptr = (int *) argp;
CHECK_TYPE (scm_is_signed_integer (arg, INT_MIN, INT_MAX),
arg, position, func_name, _("int"));
*arg_ptr = scm_to_int (arg);
break;
}
case 'u':
{
int *arg_ptr = (int *) argp;
CHECK_TYPE (scm_is_unsigned_integer (arg, 0, UINT_MAX),
arg, position, func_name, _("unsigned int"));
*arg_ptr = scm_to_uint (arg);
break;
}
case 'l':
{
long *arg_ptr = (long *) argp;
CHECK_TYPE (scm_is_signed_integer (arg, LONG_MIN, LONG_MAX),
arg, position, func_name, _("long"));
*arg_ptr = scm_to_long (arg);
break;
}
case 'n':
{
unsigned long *arg_ptr = (unsigned long *) argp;
CHECK_TYPE (scm_is_unsigned_integer (arg, 0, ULONG_MAX),
arg, position, func_name, _("unsigned long"));
*arg_ptr = scm_to_ulong (arg);
break;
}
case 'L':
{
LONGEST *arg_ptr = (LONGEST *) argp;
CHECK_TYPE (scm_is_signed_integer (arg, INT64_MIN, INT64_MAX),
arg, position, func_name, _("LONGEST"));
*arg_ptr = gdbscm_scm_to_longest (arg);
break;
}
case 'U':
{
ULONGEST *arg_ptr = (ULONGEST *) argp;
CHECK_TYPE (scm_is_unsigned_integer (arg, 0, UINT64_MAX),
arg, position, func_name, _("ULONGEST"));
*arg_ptr = gdbscm_scm_to_ulongest (arg);
break;
}
case 'O':
{
SCM *arg_ptr = (SCM *) argp;
*arg_ptr = arg;
break;
}
default:
gdb_assert_not_reached ("invalid argument format character");
}
return SCM_BOOL_F;
}
#undef CHECK_TYPE
/* Look up KEYWORD in KEYWORD_LIST.
The result is the index of the keyword in the list or -1 if not found. */
static int
lookup_keyword (const SCM *keyword_list, SCM keyword)
{
int i = 0;
while (keyword_list[i] != SCM_BOOL_F)
{
if (scm_is_eq (keyword_list[i], keyword))
return i;
++i;
}
return -1;
}
/* Utility to parse required, optional, and keyword arguments to Scheme
functions. Modelled on PyArg_ParseTupleAndKeywords, but no attempt is made
at similarity or functionality.
There is no result, if there's an error a Scheme exception is thrown.
Guile provides scm_c_bind_keyword_arguments, and feel free to use it.
This is for times when we want a bit more parsing.
BEGINNING_ARG_POS is the position of the first argument passed to this
routine. It should be one of the SCM_ARGn values. It could be > SCM_ARG1
if the caller chooses not to parse one or more required arguments.
KEYWORDS may be NULL if there are no keywords.
FORMAT:
s - string -> char *, malloc'd
t - boolean (gdb uses "t", for biT?) -> int
i - int
u - unsigned int
l - long
n - unsigned long
L - longest
U - unsigned longest
O - random scheme object
| - indicates the next set is for optional arguments
# - indicates the next set is for keyword arguments (must follow |)
. - indicates "rest" arguments are present, this character must appear last
FORMAT must match the definition from scm_c_{make,define}_gsubr.
Required and optional arguments appear in order in the format string.
Afterwards, keyword-based arguments are processed. There must be as many
remaining characters in the format string as their are keywords.
Except for "|#.", the number of characters in the format string must match
#required + #optional + #keywords.
The function is required to be defined in a compatible manner:
#required-args and #optional-arguments must match, and rest-arguments
must be specified if keyword args are desired, and/or regular "rest" args.
Example: For this function,
scm_c_define_gsubr ("execute", 2, 3, 1, foo);
the format string + keyword list could be any of:
1) "ss|ttt#tt", { "key1", "key2", NULL }
2) "ss|ttt.", { NULL }
3) "ss|ttt#t.", { "key1", NULL }
For required and optional args pass the SCM of the argument, and a
pointer to the value to hold the parsed result (type depends on format
char). After that pass the SCM containing the "rest" arguments followed
by pointers to values to hold parsed keyword arguments, and if specified
a pointer to hold the remaining contents of "rest".
For keyword arguments pass two pointers: the first is a pointer to an int
that will contain the position of the argument in the arg list, and the
second will contain result of processing the argument. The int pointed
to by the first value should be initialized to -1. It can then be used
to tell whether the keyword was present.
If both keyword and rest arguments are present, the caller must pass a
pointer to contain the new value of rest (after keyword args have been
removed).
There's currently no way, that I know of, to specify default values for
optional arguments in C-provided functions. At the moment they're a
work-in-progress. The caller should test SCM_UNBNDP for each optional
argument. Unbound optional arguments are ignored. */
void
gdbscm_parse_function_args (const char *func_name,
int beginning_arg_pos,
const SCM *keywords,
const char *format, ...)
{
va_list args;
const char *p;
int i, have_rest, num_keywords, position;
int have_optional = 0;
SCM status;
SCM rest = SCM_EOL;
/* Keep track of malloc'd strings. We need to free them upon error. */
std::vector<char *> allocated_strings;
char *ptr;
have_rest = validate_arg_format (format);
num_keywords = count_keywords (keywords);
va_start (args, format);
p = format;
position = beginning_arg_pos;
/* Process required, optional arguments. */
while (*p && *p != '#' && *p != '.')
{
SCM arg;
void *arg_ptr;
if (*p == '|')
{
have_optional = 1;
++p;
continue;
}
arg = va_arg (args, SCM);
if (!have_optional || !SCM_UNBNDP (arg))
{
arg_ptr = va_arg (args, void *);
status = extract_arg (*p, arg, arg_ptr, func_name, position);
if (!gdbscm_is_false (status))
goto fail;
if (*p == 's')
allocated_strings.push_back (*(char **) arg_ptr);
}
++p;
++position;
}
/* Process keyword arguments. */
if (have_rest || num_keywords > 0)
rest = va_arg (args, SCM);
if (num_keywords > 0)
{
SCM *keyword_args = XALLOCAVEC (SCM, num_keywords);
int *keyword_positions = XALLOCAVEC (int, num_keywords);
gdb_assert (*p == '#');
++p;
for (i = 0; i < num_keywords; ++i)
{
keyword_args[i] = SCM_UNSPECIFIED;
keyword_positions[i] = -1;
}
while (scm_is_pair (rest)
&& scm_is_keyword (scm_car (rest)))
{
SCM keyword = scm_car (rest);
i = lookup_keyword (keywords, keyword);
if (i < 0)
{
status = gdbscm_make_error (scm_arg_type_key, func_name,
_("Unrecognized keyword: ~a"),
scm_list_1 (keyword), keyword);
goto fail;
}
if (!scm_is_pair (scm_cdr (rest)))
{
status = gdbscm_make_error
(scm_arg_type_key, func_name,
_("Missing value for keyword argument"),
scm_list_1 (keyword), keyword);
goto fail;
}
keyword_args[i] = scm_cadr (rest);
keyword_positions[i] = position + 1;
rest = scm_cddr (rest);
position += 2;
}
for (i = 0; i < num_keywords; ++i)
{
int *arg_pos_ptr = va_arg (args, int *);
void *arg_ptr = va_arg (args, void *);
SCM arg = keyword_args[i];
if (! scm_is_eq (arg, SCM_UNSPECIFIED))
{
*arg_pos_ptr = keyword_positions[i];
status = extract_arg (p[i], arg, arg_ptr, func_name,
keyword_positions[i]);
if (!gdbscm_is_false (status))
goto fail;
if (p[i] == 's')
allocated_strings.push_back (*(char **) arg_ptr);
}
}
}
/* Process "rest" arguments. */
if (have_rest)
{
if (num_keywords > 0)
{
SCM *rest_ptr = va_arg (args, SCM *);
*rest_ptr = rest;
}
}
else
{
if (! scm_is_null (rest))
{
status = gdbscm_make_error (scm_args_number_key, func_name,
_("Too many arguments"),
SCM_EOL, SCM_BOOL_F);
goto fail;
}
}
va_end (args);
return;
fail:
va_end (args);
for (char *ptr : allocated_strings)
xfree (ptr);
gdbscm_throw (status);
}
/* Return longest L as a scheme object. */
SCM
gdbscm_scm_from_longest (LONGEST l)
{
return scm_from_int64 (l);
}
/* Convert scheme object L to LONGEST.
It is an error to call this if L is not an integer in range of LONGEST.
(because the underlying Scheme function will thrown an exception,
which is not part of our contract with the caller). */
LONGEST
gdbscm_scm_to_longest (SCM l)
{
return scm_to_int64 (l);
}
/* Return unsigned longest L as a scheme object. */
SCM
gdbscm_scm_from_ulongest (ULONGEST l)
{
return scm_from_uint64 (l);
}
/* Convert scheme object U to ULONGEST.
It is an error to call this if U is not an integer in range of ULONGEST
(because the underlying Scheme function will thrown an exception,
which is not part of our contract with the caller). */
ULONGEST
gdbscm_scm_to_ulongest (SCM u)
{
return scm_to_uint64 (u);
}
/* Same as scm_dynwind_free, but uses xfree. */
void
gdbscm_dynwind_xfree (void *ptr)
{
scm_dynwind_unwind_handler (xfree, ptr, SCM_F_WIND_EXPLICITLY);
}
/* Return non-zero if PROC is a procedure. */
int
gdbscm_is_procedure (SCM proc)
{
return gdbscm_is_true (scm_procedure_p (proc));
}
/* Same as xstrdup, but the string is allocated on the GC heap. */
char *
gdbscm_gc_xstrdup (const char *str)
{
size_t len = strlen (str);
char *result
= (char *) scm_gc_malloc_pointerless (len + 1, "gdbscm_gc_xstrdup");
strcpy (result, str);
return result;
}
/* Return a duplicate of ARGV living on the GC heap. */
const char * const *
gdbscm_gc_dup_argv (char **argv)
{
int i, len;
size_t string_space;
char *p, **result;
for (len = 0, string_space = 0; argv[len] != NULL; ++len)
string_space += strlen (argv[len]) + 1;
/* Allocating "pointerless" works because the pointers are all
self-contained within the object. */
result = (char **) scm_gc_malloc_pointerless (((len + 1) * sizeof (char *))
+ string_space,
"parameter enum list");
p = (char *) &result[len + 1];
for (i = 0; i < len; ++i)
{
result[i] = p;
strcpy (p, argv[i]);
p += strlen (p) + 1;
}
result[i] = NULL;
return (const char * const *) result;
}
/* Return non-zero if the version of Guile being used it at least
MAJOR.MINOR.MICRO. */
int
gdbscm_guile_version_is_at_least (int major, int minor, int micro)
{
if (major > gdbscm_guile_major_version)
return 0;
if (major < gdbscm_guile_major_version)
return 1;
if (minor > gdbscm_guile_minor_version)
return 0;
if (minor < gdbscm_guile_minor_version)
return 1;
if (micro > gdbscm_guile_micro_version)
return 0;
return 1;
}
|