1 /* Scheme interface to lazy strings.
3 Copyright (C) 2010-2015 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. */
28 #include "guile-internal.h"
30 /* The <gdb:lazy-string> smob. */
34 /* This always appears first. */
37 /* Holds the address of the lazy string. */
40 /* Holds the encoding that will be applied to the string when the string
41 is printed by GDB. If the encoding is set to NULL then GDB will select
42 the most appropriate encoding when the sting is printed.
43 Space for this is malloc'd and will be freed when the object is
47 /* Holds the length of the string in characters. If the length is -1,
48 then the string will be fetched and encoded up to the first null of
52 /* This attribute holds the type that is represented by the lazy
57 static const char lazy_string_smob_name
[] = "gdb:lazy-string";
59 /* The tag Guile knows the lazy string smob by. */
60 static scm_t_bits lazy_string_smob_tag
;
62 /* Administrivia for lazy string smobs. */
64 /* The smob "free" function for <gdb:lazy-string>. */
67 lsscm_free_lazy_string_smob (SCM self
)
69 lazy_string_smob
*v_smob
= (lazy_string_smob
*) SCM_SMOB_DATA (self
);
71 xfree (v_smob
->encoding
);
76 /* The smob "print" function for <gdb:lazy-string>. */
79 lsscm_print_lazy_string_smob (SCM self
, SCM port
, scm_print_state
*pstate
)
81 lazy_string_smob
*ls_smob
= (lazy_string_smob
*) SCM_SMOB_DATA (self
);
83 gdbscm_printf (port
, "#<%s", lazy_string_smob_name
);
84 gdbscm_printf (port
, " @%s", hex_string (ls_smob
->address
));
85 if (ls_smob
->length
>= 0)
86 gdbscm_printf (port
, " length %d", ls_smob
->length
);
87 if (ls_smob
->encoding
!= NULL
)
88 gdbscm_printf (port
, " encoding %s", ls_smob
->encoding
);
91 scm_remember_upto_here_1 (self
);
93 /* Non-zero means success. */
97 /* Low level routine to create a <gdb:lazy-string> object.
98 The caller must verify !(address == 0 && length != 0). */
101 lsscm_make_lazy_string_smob (CORE_ADDR address
, int length
,
102 const char *encoding
, struct type
*type
)
104 lazy_string_smob
*ls_smob
= (lazy_string_smob
*)
105 scm_gc_malloc (sizeof (lazy_string_smob
), lazy_string_smob_name
);
108 /* Caller must verify this. */
109 gdb_assert (!(address
== 0 && length
!= 0));
110 gdb_assert (type
!= NULL
);
112 ls_smob
->address
= address
;
113 /* Coerce all values < 0 to -1. */
114 ls_smob
->length
= length
< 0 ? -1 : length
;
115 if (encoding
== NULL
|| strcmp (encoding
, "") == 0)
116 ls_smob
->encoding
= NULL
;
118 ls_smob
->encoding
= xstrdup (encoding
);
119 ls_smob
->type
= type
;
121 ls_scm
= scm_new_smob (lazy_string_smob_tag
, (scm_t_bits
) ls_smob
);
122 gdbscm_init_gsmob (&ls_smob
->base
);
127 /* Return non-zero if SCM is a <gdb:lazy-string> object. */
130 lsscm_is_lazy_string (SCM scm
)
132 return SCM_SMOB_PREDICATE (lazy_string_smob_tag
, scm
);
135 /* (lazy-string? object) -> boolean */
138 gdbscm_lazy_string_p (SCM scm
)
140 return scm_from_bool (lsscm_is_lazy_string (scm
));
143 /* Main entry point to create a <gdb:lazy-string> object.
144 If there's an error a <gdb:exception> object is returned. */
147 lsscm_make_lazy_string (CORE_ADDR address
, int length
,
148 const char *encoding
, struct type
*type
)
150 if (address
== 0 && length
!= 0)
152 return gdbscm_make_out_of_range_error
153 (NULL
, 0, scm_from_int (length
),
154 _("cannot create a lazy string with address 0x0"
155 " and a non-zero length"));
160 return gdbscm_make_out_of_range_error
161 (NULL
, 0, scm_from_int (0), _("a lazy string's type cannot be NULL"));
164 return lsscm_make_lazy_string_smob (address
, length
, encoding
, type
);
167 /* Returns the <gdb:lazy-string> smob in SELF.
168 Throws an exception if SELF is not a <gdb:lazy-string> object. */
171 lsscm_get_lazy_string_arg_unsafe (SCM self
, int arg_pos
, const char *func_name
)
173 SCM_ASSERT_TYPE (lsscm_is_lazy_string (self
), self
, arg_pos
, func_name
,
174 lazy_string_smob_name
);
179 /* Lazy string methods. */
181 /* (lazy-string-address <gdb:lazy-string>) -> address */
184 gdbscm_lazy_string_address (SCM self
)
186 SCM ls_scm
= lsscm_get_lazy_string_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
187 lazy_string_smob
*ls_smob
= (lazy_string_smob
*) SCM_SMOB_DATA (ls_scm
);
189 return gdbscm_scm_from_ulongest (ls_smob
->address
);
192 /* (lazy-string-length <gdb:lazy-string>) -> integer */
195 gdbscm_lazy_string_length (SCM self
)
197 SCM ls_scm
= lsscm_get_lazy_string_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
198 lazy_string_smob
*ls_smob
= (lazy_string_smob
*) SCM_SMOB_DATA (ls_scm
);
200 return scm_from_int (ls_smob
->length
);
203 /* (lazy-string-encoding <gdb:lazy-string>) -> string */
206 gdbscm_lazy_string_encoding (SCM self
)
208 SCM ls_scm
= lsscm_get_lazy_string_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
209 lazy_string_smob
*ls_smob
= (lazy_string_smob
*) SCM_SMOB_DATA (ls_scm
);
211 /* An encoding can be set to NULL by the user, so check first.
212 If NULL return #f. */
214 return gdbscm_scm_from_c_string (ls_smob
->encoding
);
218 /* (lazy-string-type <gdb:lazy-string>) -> <gdb:type> */
221 gdbscm_lazy_string_type (SCM self
)
223 SCM ls_scm
= lsscm_get_lazy_string_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
224 lazy_string_smob
*ls_smob
= (lazy_string_smob
*) SCM_SMOB_DATA (ls_scm
);
226 return tyscm_scm_from_type (ls_smob
->type
);
229 /* (lazy-string->value <gdb:lazy-string>) -> <gdb:value> */
232 gdbscm_lazy_string_to_value (SCM self
)
234 SCM ls_scm
= lsscm_get_lazy_string_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
235 lazy_string_smob
*ls_smob
= (lazy_string_smob
*) SCM_SMOB_DATA (ls_scm
);
236 struct value
*value
= NULL
;
237 volatile struct gdb_exception except
;
239 if (ls_smob
->address
== 0)
241 gdbscm_throw (gdbscm_make_out_of_range_error (FUNC_NAME
, SCM_ARG1
, self
,
242 _("cannot create a value from NULL")));
245 TRY_CATCH (except
, RETURN_MASK_ALL
)
247 value
= value_at_lazy (ls_smob
->type
, ls_smob
->address
);
249 GDBSCM_HANDLE_GDB_EXCEPTION (except
);
251 return vlscm_scm_from_value (value
);
254 /* A "safe" version of gdbscm_lazy_string_to_value for use by
255 vlscm_convert_typed_value_from_scheme.
256 The result, upon success, is the value of <gdb:lazy-string> STRING.
257 ARG_POS is the argument position of STRING in the original Scheme
258 function call, used in exception text.
259 If there's an error, NULL is returned and a <gdb:exception> object
260 is stored in *except_scmp.
262 Note: The result is still "lazy". The caller must call value_fetch_lazy
263 to actually fetch the value. */
266 lsscm_safe_lazy_string_to_value (SCM string
, int arg_pos
,
267 const char *func_name
, SCM
*except_scmp
)
269 lazy_string_smob
*ls_smob
;
270 struct value
*value
= NULL
;
271 volatile struct gdb_exception except
;
273 gdb_assert (lsscm_is_lazy_string (string
));
275 ls_smob
= (lazy_string_smob
*) SCM_SMOB_DATA (string
);
276 *except_scmp
= SCM_BOOL_F
;
278 if (ls_smob
->address
== 0)
281 = gdbscm_make_out_of_range_error (FUNC_NAME
, SCM_ARG1
, string
,
282 _("cannot create a value from NULL"));
286 TRY_CATCH (except
, RETURN_MASK_ALL
)
288 value
= value_at_lazy (ls_smob
->type
, ls_smob
->address
);
290 if (except
.reason
< 0)
292 *except_scmp
= gdbscm_scm_from_gdb_exception (except
);
299 /* Print a lazy string to STREAM using val_print_string.
300 STRING must be a <gdb:lazy-string> object. */
303 lsscm_val_print_lazy_string (SCM string
, struct ui_file
*stream
,
304 const struct value_print_options
*options
)
306 lazy_string_smob
*ls_smob
;
308 gdb_assert (lsscm_is_lazy_string (string
));
310 ls_smob
= (lazy_string_smob
*) SCM_SMOB_DATA (string
);
312 val_print_string (ls_smob
->type
, ls_smob
->encoding
,
313 ls_smob
->address
, ls_smob
->length
,
317 /* Initialize the Scheme lazy-strings code. */
319 static const scheme_function lazy_string_functions
[] =
321 { "lazy-string?", 1, 0, 0, gdbscm_lazy_string_p
,
323 Return #t if the object is a <gdb:lazy-string> object." },
325 { "lazy-string-address", 1, 0, 0, gdbscm_lazy_string_address
,
327 Return the address of the lazy-string." },
329 { "lazy-string-length", 1, 0, 0, gdbscm_lazy_string_length
,
331 Return the length of the lazy-string.\n\
332 If the length is -1 then the length is determined by the first null\n\
333 of appropriate width." },
335 { "lazy-string-encoding", 1, 0, 0, gdbscm_lazy_string_encoding
,
337 Return the encoding of the lazy-string." },
339 { "lazy-string-type", 1, 0, 0, gdbscm_lazy_string_type
,
341 Return the <gdb:type> of the lazy-string." },
343 { "lazy-string->value", 1, 0, 0, gdbscm_lazy_string_to_value
,
345 Return the <gdb:value> representation of the lazy-string." },
351 gdbscm_initialize_lazy_strings (void)
353 lazy_string_smob_tag
= gdbscm_make_smob_type (lazy_string_smob_name
,
354 sizeof (lazy_string_smob
));
355 scm_set_smob_free (lazy_string_smob_tag
, lsscm_free_lazy_string_smob
);
356 scm_set_smob_print (lazy_string_smob_tag
, lsscm_print_lazy_string_smob
);
358 gdbscm_define_functions (lazy_string_functions
, 1);