1 /* C language support routines for GDB, the GNU debugger.
3 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2002, 2003,
4 2004, 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "expression.h"
25 #include "parser-defs.h"
29 #include "macroscope.h"
30 #include "gdb_assert.h"
32 #include "gdb_string.h"
35 #include "cp-support.h"
37 extern void _initialize_c_language (void);
38 static void c_emit_char (int c, struct ui_file * stream, int quoter);
40 /* Print the character C on STREAM as part of the contents of a literal
41 string whose delimiter is QUOTER. Note that that format for printing
42 characters and strings is language specific. */
45 c_emit_char (int c, struct ui_file *stream, int quoter)
50 c &= 0xFF; /* Avoid sign bit follies */
52 escape = c_target_char_has_backslash_escape (c);
55 if (quoter == '"' && strcmp (escape, "0") == 0)
56 /* Print nulls embedded in double quoted strings as \000 to
58 fprintf_filtered (stream, "\\000");
60 fprintf_filtered (stream, "\\%s", escape);
62 else if (target_char_to_host (c, &host_char)
63 && host_char_print_literally (host_char))
65 if (host_char == '\\' || host_char == quoter)
66 fputs_filtered ("\\", stream);
67 fprintf_filtered (stream, "%c", host_char);
70 fprintf_filtered (stream, "\\%.3o", (unsigned int) c);
74 c_printchar (int c, struct ui_file *stream)
76 fputc_filtered ('\'', stream);
77 LA_EMIT_CHAR (c, stream, '\'');
78 fputc_filtered ('\'', stream);
81 /* Print the character string STRING, printing at most LENGTH characters.
82 LENGTH is -1 if the string is nul terminated. Each character is WIDTH bytes
83 long. Printing stops early if the number hits print_max; repeat counts are
84 printed as appropriate. Print ellipses at the end if we had to stop before
85 printing LENGTH characters, or if FORCE_ELLIPSES. */
88 c_printstr (struct ui_file *stream, const gdb_byte *string,
89 unsigned int length, int width, int force_ellipses,
90 const struct value_print_options *options)
93 unsigned int things_printed = 0;
97 /* If the string was not truncated due to `set print elements', and
98 the last byte of it is a null, we don't print that, in traditional C
102 && (extract_unsigned_integer (string + (length - 1) * width, width)
108 fputs_filtered ("\"\"", stream);
112 for (i = 0; i < length && things_printed < options->print_max; ++i)
114 /* Position of the character we are examining
115 to see whether it is repeated. */
117 /* Number of repetitions we have detected so far. */
119 unsigned long current_char;
125 fputs_filtered (", ", stream);
129 current_char = extract_unsigned_integer (string + i * width, width);
134 && extract_unsigned_integer (string + rep1 * width, width)
141 if (reps > options->repeat_count_threshold)
145 if (options->inspect_it)
146 fputs_filtered ("\\\", ", stream);
148 fputs_filtered ("\", ", stream);
151 LA_PRINT_CHAR (current_char, stream);
152 fprintf_filtered (stream, _(" <repeats %u times>"), reps);
154 things_printed += options->repeat_count_threshold;
161 if (options->inspect_it)
162 fputs_filtered ("\\\"", stream);
164 fputs_filtered ("\"", stream);
167 LA_EMIT_CHAR (current_char, stream, '"');
172 /* Terminate the quotes if necessary. */
175 if (options->inspect_it)
176 fputs_filtered ("\\\"", stream);
178 fputs_filtered ("\"", stream);
181 if (force_ellipses || i < length)
182 fputs_filtered ("...", stream);
185 /* Obtain a C string from the inferior storing it in a newly allocated
186 buffer in BUFFER, which should be freed by the caller. The string is
187 read until a null character is found. If VALUE is an array with known
188 length, the function will not read past the end of the array. LENGTH
189 will contain the size of the string in bytes (not counting the null
192 Assumes strings are terminated by a null character. The size of a character
193 is determined by the length of the target type of the pointer or array.
194 This means that a null byte present in a multi-byte character will not
195 terminate the string unless the whole character is null.
197 CHARSET is always set to the target charset. */
200 c_get_string (struct value *value, gdb_byte **buffer, int *length,
201 const char **charset)
204 unsigned int fetchlimit;
205 struct type *type = check_typedef (value_type (value));
206 struct type *element_type = TYPE_TARGET_TYPE (type);
208 if (element_type == NULL)
211 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
213 /* If we know the size of the array, we can use it as a limit on the
214 number of characters to be fetched. */
215 if (TYPE_NFIELDS (type) == 1
216 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_RANGE)
218 LONGEST low_bound, high_bound;
220 get_discrete_bounds (TYPE_FIELD_TYPE (type, 0),
221 &low_bound, &high_bound);
222 fetchlimit = high_bound - low_bound + 1;
225 fetchlimit = UINT_MAX;
227 else if (TYPE_CODE (type) == TYPE_CODE_PTR)
228 fetchlimit = UINT_MAX;
230 /* We work only with arrays and pointers. */
233 element_type = check_typedef (element_type);
234 if (TYPE_CODE (element_type) != TYPE_CODE_INT
235 && TYPE_CODE (element_type) != TYPE_CODE_CHAR)
236 /* If the elements are not integers or characters, we don't consider it
240 width = TYPE_LENGTH (element_type);
242 /* If the string lives in GDB's memory intead of the inferior's, then we
243 just need to copy it to BUFFER. Also, since such strings are arrays
244 with known size, FETCHLIMIT will hold the size of the array. */
245 if ((VALUE_LVAL (value) == not_lval
246 || VALUE_LVAL (value) == lval_internalvar)
247 && fetchlimit != UINT_MAX)
250 const gdb_byte *contents = value_contents (value);
252 /* Look for a null character. */
253 for (i = 0; i < fetchlimit; i++)
254 if (extract_unsigned_integer (contents + i * width, width) == 0)
257 /* I is now either the number of non-null characters, or FETCHLIMIT. */
259 *buffer = xmalloc (*length);
260 memcpy (*buffer, contents, *length);
265 err = read_string (value_as_address (value), -1, width, fetchlimit,
270 error (_("Error reading string from inferior: %s"),
271 safe_strerror (err));
275 /* If the last character is null, subtract it from LENGTH. */
277 && extract_unsigned_integer (*buffer + *length - width, width) == 0)
280 *charset = target_charset ();
288 type_str = type_to_string (type);
291 make_cleanup (xfree, type_str);
292 error (_("Trying to read string with inappropriate type `%s'."),
296 error (_("Trying to read string with inappropriate type."));
301 /* Preprocessing and parsing C and C++ expressions. */
305 /* Table mapping opcodes into strings for printing operators
306 and precedences of the operators. */
308 const struct op_print c_op_print_tab[] =
310 {",", BINOP_COMMA, PREC_COMMA, 0},
311 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
312 {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
313 {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
314 {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
315 {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
316 {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
317 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
318 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
319 {"<=", BINOP_LEQ, PREC_ORDER, 0},
320 {">=", BINOP_GEQ, PREC_ORDER, 0},
321 {">", BINOP_GTR, PREC_ORDER, 0},
322 {"<", BINOP_LESS, PREC_ORDER, 0},
323 {">>", BINOP_RSH, PREC_SHIFT, 0},
324 {"<<", BINOP_LSH, PREC_SHIFT, 0},
325 {"+", BINOP_ADD, PREC_ADD, 0},
326 {"-", BINOP_SUB, PREC_ADD, 0},
327 {"*", BINOP_MUL, PREC_MUL, 0},
328 {"/", BINOP_DIV, PREC_MUL, 0},
329 {"%", BINOP_REM, PREC_MUL, 0},
330 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
331 {"-", UNOP_NEG, PREC_PREFIX, 0},
332 {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
333 {"~", UNOP_COMPLEMENT, PREC_PREFIX, 0},
334 {"*", UNOP_IND, PREC_PREFIX, 0},
335 {"&", UNOP_ADDR, PREC_PREFIX, 0},
336 {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
337 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
338 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
342 enum c_primitive_types {
343 c_primitive_type_int,
344 c_primitive_type_long,
345 c_primitive_type_short,
346 c_primitive_type_char,
347 c_primitive_type_float,
348 c_primitive_type_double,
349 c_primitive_type_void,
350 c_primitive_type_long_long,
351 c_primitive_type_signed_char,
352 c_primitive_type_unsigned_char,
353 c_primitive_type_unsigned_short,
354 c_primitive_type_unsigned_int,
355 c_primitive_type_unsigned_long,
356 c_primitive_type_unsigned_long_long,
357 c_primitive_type_long_double,
358 c_primitive_type_complex,
359 c_primitive_type_double_complex,
360 c_primitive_type_decfloat,
361 c_primitive_type_decdouble,
362 c_primitive_type_declong,
367 c_language_arch_info (struct gdbarch *gdbarch,
368 struct language_arch_info *lai)
370 const struct builtin_type *builtin = builtin_type (gdbarch);
371 lai->string_char_type = builtin->builtin_char;
372 lai->primitive_type_vector
373 = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_c_primitive_types + 1,
375 lai->primitive_type_vector [c_primitive_type_int] = builtin->builtin_int;
376 lai->primitive_type_vector [c_primitive_type_long] = builtin->builtin_long;
377 lai->primitive_type_vector [c_primitive_type_short] = builtin->builtin_short;
378 lai->primitive_type_vector [c_primitive_type_char] = builtin->builtin_char;
379 lai->primitive_type_vector [c_primitive_type_float] = builtin->builtin_float;
380 lai->primitive_type_vector [c_primitive_type_double] = builtin->builtin_double;
381 lai->primitive_type_vector [c_primitive_type_void] = builtin->builtin_void;
382 lai->primitive_type_vector [c_primitive_type_long_long] = builtin->builtin_long_long;
383 lai->primitive_type_vector [c_primitive_type_signed_char] = builtin->builtin_signed_char;
384 lai->primitive_type_vector [c_primitive_type_unsigned_char] = builtin->builtin_unsigned_char;
385 lai->primitive_type_vector [c_primitive_type_unsigned_short] = builtin->builtin_unsigned_short;
386 lai->primitive_type_vector [c_primitive_type_unsigned_int] = builtin->builtin_unsigned_int;
387 lai->primitive_type_vector [c_primitive_type_unsigned_long] = builtin->builtin_unsigned_long;
388 lai->primitive_type_vector [c_primitive_type_unsigned_long_long] = builtin->builtin_unsigned_long_long;
389 lai->primitive_type_vector [c_primitive_type_long_double] = builtin->builtin_long_double;
390 lai->primitive_type_vector [c_primitive_type_complex] = builtin->builtin_complex;
391 lai->primitive_type_vector [c_primitive_type_double_complex] = builtin->builtin_double_complex;
392 lai->primitive_type_vector [c_primitive_type_decfloat] = builtin->builtin_decfloat;
393 lai->primitive_type_vector [c_primitive_type_decdouble] = builtin->builtin_decdouble;
394 lai->primitive_type_vector [c_primitive_type_declong] = builtin->builtin_declong;
396 lai->bool_type_default = builtin->builtin_int;
399 const struct language_defn c_language_defn =
401 "c", /* Language name */
408 &exp_descriptor_standard,
412 c_printchar, /* Print a character constant */
413 c_printstr, /* Function to print string constant */
414 c_emit_char, /* Print a single char */
415 c_print_type, /* Print a type using appropriate syntax */
416 c_print_typedef, /* Print a typedef using appropriate syntax */
417 c_val_print, /* Print a value using appropriate syntax */
418 c_value_print, /* Print a top-level value */
419 NULL, /* Language specific skip_trampoline */
420 NULL, /* name_of_this */
421 basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
422 basic_lookup_transparent_type,/* lookup_transparent_type */
423 NULL, /* Language specific symbol demangler */
424 NULL, /* Language specific class_name_from_physname */
425 c_op_print_tab, /* expression operators for printing */
426 1, /* c-style arrays */
427 0, /* String lower bound */
428 default_word_break_characters,
429 default_make_symbol_completion_list,
430 c_language_arch_info,
431 default_print_array_index,
432 default_pass_by_reference,
437 enum cplus_primitive_types {
438 cplus_primitive_type_int,
439 cplus_primitive_type_long,
440 cplus_primitive_type_short,
441 cplus_primitive_type_char,
442 cplus_primitive_type_float,
443 cplus_primitive_type_double,
444 cplus_primitive_type_void,
445 cplus_primitive_type_long_long,
446 cplus_primitive_type_signed_char,
447 cplus_primitive_type_unsigned_char,
448 cplus_primitive_type_unsigned_short,
449 cplus_primitive_type_unsigned_int,
450 cplus_primitive_type_unsigned_long,
451 cplus_primitive_type_unsigned_long_long,
452 cplus_primitive_type_long_double,
453 cplus_primitive_type_complex,
454 cplus_primitive_type_double_complex,
455 cplus_primitive_type_bool,
456 cplus_primitive_type_decfloat,
457 cplus_primitive_type_decdouble,
458 cplus_primitive_type_declong,
459 nr_cplus_primitive_types
463 cplus_language_arch_info (struct gdbarch *gdbarch,
464 struct language_arch_info *lai)
466 const struct builtin_type *builtin = builtin_type (gdbarch);
467 lai->string_char_type = builtin->builtin_char;
468 lai->primitive_type_vector
469 = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_cplus_primitive_types + 1,
471 lai->primitive_type_vector [cplus_primitive_type_int]
472 = builtin->builtin_int;
473 lai->primitive_type_vector [cplus_primitive_type_long]
474 = builtin->builtin_long;
475 lai->primitive_type_vector [cplus_primitive_type_short]
476 = builtin->builtin_short;
477 lai->primitive_type_vector [cplus_primitive_type_char]
478 = builtin->builtin_char;
479 lai->primitive_type_vector [cplus_primitive_type_float]
480 = builtin->builtin_float;
481 lai->primitive_type_vector [cplus_primitive_type_double]
482 = builtin->builtin_double;
483 lai->primitive_type_vector [cplus_primitive_type_void]
484 = builtin->builtin_void;
485 lai->primitive_type_vector [cplus_primitive_type_long_long]
486 = builtin->builtin_long_long;
487 lai->primitive_type_vector [cplus_primitive_type_signed_char]
488 = builtin->builtin_signed_char;
489 lai->primitive_type_vector [cplus_primitive_type_unsigned_char]
490 = builtin->builtin_unsigned_char;
491 lai->primitive_type_vector [cplus_primitive_type_unsigned_short]
492 = builtin->builtin_unsigned_short;
493 lai->primitive_type_vector [cplus_primitive_type_unsigned_int]
494 = builtin->builtin_unsigned_int;
495 lai->primitive_type_vector [cplus_primitive_type_unsigned_long]
496 = builtin->builtin_unsigned_long;
497 lai->primitive_type_vector [cplus_primitive_type_unsigned_long_long]
498 = builtin->builtin_unsigned_long_long;
499 lai->primitive_type_vector [cplus_primitive_type_long_double]
500 = builtin->builtin_long_double;
501 lai->primitive_type_vector [cplus_primitive_type_complex]
502 = builtin->builtin_complex;
503 lai->primitive_type_vector [cplus_primitive_type_double_complex]
504 = builtin->builtin_double_complex;
505 lai->primitive_type_vector [cplus_primitive_type_bool]
506 = builtin->builtin_bool;
507 lai->primitive_type_vector [cplus_primitive_type_decfloat]
508 = builtin->builtin_decfloat;
509 lai->primitive_type_vector [cplus_primitive_type_decdouble]
510 = builtin->builtin_decdouble;
511 lai->primitive_type_vector [cplus_primitive_type_declong]
512 = builtin->builtin_declong;
514 lai->bool_type_symbol = "bool";
515 lai->bool_type_default = builtin->builtin_bool;
518 const struct language_defn cplus_language_defn =
520 "c++", /* Language name */
527 &exp_descriptor_standard,
531 c_printchar, /* Print a character constant */
532 c_printstr, /* Function to print string constant */
533 c_emit_char, /* Print a single char */
534 c_print_type, /* Print a type using appropriate syntax */
535 c_print_typedef, /* Print a typedef using appropriate syntax */
536 c_val_print, /* Print a value using appropriate syntax */
537 c_value_print, /* Print a top-level value */
538 cplus_skip_trampoline, /* Language specific skip_trampoline */
539 "this", /* name_of_this */
540 cp_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
541 cp_lookup_transparent_type, /* lookup_transparent_type */
542 cplus_demangle, /* Language specific symbol demangler */
543 cp_class_name_from_physname, /* Language specific class_name_from_physname */
544 c_op_print_tab, /* expression operators for printing */
545 1, /* c-style arrays */
546 0, /* String lower bound */
547 default_word_break_characters,
548 default_make_symbol_completion_list,
549 cplus_language_arch_info,
550 default_print_array_index,
551 cp_pass_by_reference,
556 const struct language_defn asm_language_defn =
558 "asm", /* Language name */
565 &exp_descriptor_standard,
569 c_printchar, /* Print a character constant */
570 c_printstr, /* Function to print string constant */
571 c_emit_char, /* Print a single char */
572 c_print_type, /* Print a type using appropriate syntax */
573 c_print_typedef, /* Print a typedef using appropriate syntax */
574 c_val_print, /* Print a value using appropriate syntax */
575 c_value_print, /* Print a top-level value */
576 NULL, /* Language specific skip_trampoline */
577 NULL, /* name_of_this */
578 basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
579 basic_lookup_transparent_type,/* lookup_transparent_type */
580 NULL, /* Language specific symbol demangler */
581 NULL, /* Language specific class_name_from_physname */
582 c_op_print_tab, /* expression operators for printing */
583 1, /* c-style arrays */
584 0, /* String lower bound */
585 default_word_break_characters,
586 default_make_symbol_completion_list,
587 c_language_arch_info, /* FIXME: la_language_arch_info. */
588 default_print_array_index,
589 default_pass_by_reference,
594 /* The following language_defn does not represent a real language.
595 It just provides a minimal support a-la-C that should allow users
596 to do some simple operations when debugging applications that use
597 a language currently not supported by GDB. */
599 const struct language_defn minimal_language_defn =
601 "minimal", /* Language name */
608 &exp_descriptor_standard,
612 c_printchar, /* Print a character constant */
613 c_printstr, /* Function to print string constant */
614 c_emit_char, /* Print a single char */
615 c_print_type, /* Print a type using appropriate syntax */
616 c_print_typedef, /* Print a typedef using appropriate syntax */
617 c_val_print, /* Print a value using appropriate syntax */
618 c_value_print, /* Print a top-level value */
619 NULL, /* Language specific skip_trampoline */
620 NULL, /* name_of_this */
621 basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
622 basic_lookup_transparent_type,/* lookup_transparent_type */
623 NULL, /* Language specific symbol demangler */
624 NULL, /* Language specific class_name_from_physname */
625 c_op_print_tab, /* expression operators for printing */
626 1, /* c-style arrays */
627 0, /* String lower bound */
628 default_word_break_characters,
629 default_make_symbol_completion_list,
630 c_language_arch_info,
631 default_print_array_index,
632 default_pass_by_reference,
638 _initialize_c_language (void)
640 add_language (&c_language_defn);
641 add_language (&cplus_language_defn);
642 add_language (&asm_language_defn);
643 add_language (&minimal_language_defn);