static void parse_one_string (struct obstack *output, char *data, int len, const char *dest_charset, struct type *type) { char *limit; limit = data + len; while (data < limit) { char *p = data; /* Look for next escape, or the end of the input. */ while (p < limit && *p != '\\') ++p; /* If we saw a run of characters, convert them all. */ if (p > data) convert_between_encodings (host_charset (), dest_charset, (gdb_byte *) data, p - data, 1, output, translit_none); /* If we saw an escape, convert it. */ if (p < limit) p = convert_escape (type, dest_charset, p, limit, output); data = p; } }
static void validate (struct gdbarch *gdbarch) { iconv_t desc; const char *host_cset = host_charset (); const char *target_cset = target_charset (gdbarch); const char *target_wide_cset = target_wide_charset_name; if (!strcmp (target_wide_cset, "auto")) target_wide_cset = gdbarch_auto_wide_charset (gdbarch); desc = iconv_open (target_wide_cset, host_cset); if (desc == (iconv_t) -1) error (_("Cannot convert between character sets `%s' and `%s'"), target_wide_cset, host_cset); iconv_close (desc); desc = iconv_open (target_cset, host_cset); if (desc == (iconv_t) -1) error (_("Cannot convert between character sets `%s' and `%s'"), target_cset, host_cset); iconv_close (desc); /* Clear the cache. */ be_le_arch = NULL; }
/* Converts a Python 8-bit string to a unicode string object. Assumes the 8-bit string is in the host charset. If an error occurs during conversion, returns NULL with a python exception set. As an added bonus, the functions accepts a unicode string and returns it right away, so callers don't need to check which kind of string they've got. In Python 3, all strings are Unicode so this case is always the one that applies. If the given object is not one of the mentioned string types, NULL is returned, with the TypeError python exception set. */ PyObject * python_string_to_unicode (PyObject *obj) { PyObject *unicode_str; /* If obj is already a unicode string, just return it. I wish life was always that simple... */ if (PyUnicode_Check (obj)) { unicode_str = obj; Py_INCREF (obj); } #ifndef IS_PY3K else if (PyString_Check (obj)) unicode_str = PyUnicode_FromEncodedObject (obj, host_charset (), NULL); #endif else { PyErr_SetString (PyExc_TypeError, _("Expected a string or unicode object.")); unicode_str = NULL; } return unicode_str; }
/* sfunc for the 'show charset' command. */ static void show_charset (char *arg, int from_tty) { if (current_host_charset == current_target_charset) { printf_filtered ("The current host and target character set is `%s'.\n", host_charset ()); } else { printf_filtered ("The current host character set is `%s'.\n", host_charset ()); printf_filtered ("The current target character set is `%s'.\n", target_charset ()); } }
/* Transform a gdb parameters's value into a Python value. May return NULL (and set a Python exception) on error. Helper function for get_parameter. */ PyObject * gdbpy_parameter_value (enum var_types type, void *var) { switch (type) { case var_string: case var_string_noescape: case var_optional_filename: case var_filename: case var_enum: { char *str = * (char **) var; if (! str) str = ""; return PyString_Decode (str, strlen (str), host_charset (), NULL); } case var_boolean: { if (* (int *) var) Py_RETURN_TRUE; else Py_RETURN_FALSE; } case var_auto_boolean: { enum auto_boolean ab = * (enum auto_boolean *) var; if (ab == AUTO_BOOLEAN_TRUE) Py_RETURN_TRUE; else if (ab == AUTO_BOOLEAN_FALSE) Py_RETURN_FALSE; else Py_RETURN_NONE; } case var_integer: if ((* (int *) var) == INT_MAX) Py_RETURN_NONE; /* Fall through. */ case var_zinteger: return PyLong_FromLong (* (int *) var); case var_uinteger: { unsigned int val = * (unsigned int *) var; if (val == UINT_MAX) Py_RETURN_NONE; return PyLong_FromUnsignedLong (val); } } return PyErr_Format (PyExc_RuntimeError, _("Programmer error: unhandled type.")); }
/* Converts a python string (8-bit or unicode) to a target string in the host's charset. Returns NULL on error, with a python exception set. */ gdb::unique_xmalloc_ptr<char> python_string_to_host_string (PyObject *obj) { gdbpy_ref<> str (python_string_to_unicode (obj)); if (str == NULL) return NULL; return unicode_to_encoded_string (str.get (), host_charset ()); }
/* An Objfile method which returns the objfile's file name, or None. */ static PyObject * objfpy_get_filename (PyObject *self, void *closure) { objfile_object *obj = (objfile_object *) self; if (obj->objfile && obj->objfile->name) return PyString_Decode (obj->objfile->name, strlen (obj->objfile->name), host_charset (), NULL); Py_RETURN_NONE; }
static PyObject * stpy_fullname (PyObject *self, PyObject *args) { const char *fullname; struct symtab *symtab = NULL; STPY_REQUIRE_VALID (self, symtab); fullname = symtab_to_fullname (symtab); return PyString_Decode (fullname, strlen (fullname), host_charset (), NULL); }
static PyObject * stpy_get_filename (PyObject *self, void *closure) { PyObject *str_obj; struct symtab *symtab = NULL; STPY_REQUIRE_VALID (self, symtab); str_obj = PyString_Decode (symtab->filename, strlen (symtab->filename), host_charset (), NULL); return str_obj; }
/* Converts a python string (8-bit or unicode) to a target string in the host's charset. Returns NULL on error, with a python exception set. The caller is responsible for xfree'ing the string. */ char * python_string_to_host_string (PyObject *obj) { PyObject *str; char *result; str = python_string_to_unicode (obj); if (str == NULL) return NULL; result = unicode_to_encoded_string (str, host_charset ()); Py_DECREF (str); return result; }
static PyObject * stpy_get_filename (PyObject *self, void *closure) { PyObject *str_obj; struct symtab *symtab = NULL; const char *filename; STPY_REQUIRE_VALID (self, symtab); filename = symtab_to_filename_for_display (symtab); str_obj = PyString_Decode (filename, strlen (filename), host_charset (), NULL); return str_obj; }
/* Python function to get the condition expression of a breakpoint. */ static PyObject * bppy_get_condition (PyObject *self, void *closure) { char *str; breakpoint_object *obj = (breakpoint_object *) self; BPPY_REQUIRE_VALID (obj); str = obj->bp->cond_string; if (! str) Py_RETURN_NONE; return PyString_Decode (str, strlen (str), host_charset (), NULL); }
static PyObject * objfpy_get_username (PyObject *self, void *closure) { objfile_object *obj = (objfile_object *) self; if (obj->objfile) { const char *username = obj->objfile->original_name; return PyString_Decode (username, strlen (username), host_charset (), NULL); } Py_RETURN_NONE; }
static PyObject * pspy_get_filename (PyObject *self, void *closure) { pspace_object *obj = (pspace_object *) self; if (obj->pspace) { struct objfile *objfile = obj->pspace->symfile_object_file; if (objfile) return PyString_Decode (objfile->name, strlen (objfile->name), host_charset (), NULL); } Py_RETURN_NONE; }
static PyObject * stpy_get_producer (PyObject *self, void *closure) { struct symtab *symtab = NULL; struct compunit_symtab *cust; STPY_REQUIRE_VALID (self, symtab); cust = SYMTAB_COMPUNIT (symtab); if (COMPUNIT_PRODUCER (cust) != NULL) { const char *producer = COMPUNIT_PRODUCER (cust); return PyString_Decode (producer, strlen (producer), host_charset (), NULL); } Py_RETURN_NONE; }
/* Python function to get the breakpoint expression. */ static PyObject * bppy_get_expression (PyObject *self, void *closure) { char *str; breakpoint_object *obj = (breakpoint_object *) self; BPPY_REQUIRE_VALID (obj); if (obj->bp->type != bp_watchpoint && obj->bp->type != bp_hardware_watchpoint && obj->bp->type != bp_read_watchpoint && obj->bp->type != bp_access_watchpoint) Py_RETURN_NONE; str = obj->bp->exp_string; if (! str) str = ""; return PyString_Decode (str, strlen (str), host_charset (), NULL); }
static void cmdscm_function (struct cmd_list_element *command, char *args, int from_tty) { command_smob *c_smob/*obj*/ = (command_smob *) get_cmd_context (command); SCM arg_scm, tty_scm, result; gdb_assert (c_smob != NULL); if (args == NULL) args = ""; arg_scm = gdbscm_scm_from_string (args, strlen (args), host_charset (), 1); if (gdbscm_is_exception (arg_scm)) error (_("Could not convert arguments to Scheme string.")); tty_scm = scm_from_bool (from_tty); result = gdbscm_safe_call_3 (c_smob->invoke, c_smob->containing_scm, arg_scm, tty_scm, gdbscm_user_error_p); if (gdbscm_is_exception (result)) { /* Don't print the stack if this was an error signalled by the command itself. */ if (gdbscm_user_error_p (gdbscm_exception_key (result))) { char *msg = gdbscm_exception_message_to_string (result); make_cleanup (xfree, msg); error ("%s", msg); } else { gdbscm_print_gdb_exception (SCM_BOOL_F, result); error (_("Error occurred in Scheme-implemented GDB command.")); } } }
void c_emit_char (int c, struct type *type, struct ui_file *stream, int quoter) { enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type)); struct obstack wchar_buf, output; struct cleanup *cleanups; const char *encoding; gdb_byte *buf; struct wchar_iterator *iter; int need_escape = 0; classify_type (type, get_type_arch (type), &encoding); buf = alloca (TYPE_LENGTH (type)); pack_long (buf, type, c); iter = make_wchar_iterator (buf, TYPE_LENGTH (type), encoding, TYPE_LENGTH (type)); cleanups = make_cleanup_wchar_iterator (iter); /* This holds the printable form of the wchar_t data. */ obstack_init (&wchar_buf); make_cleanup_obstack_free (&wchar_buf); while (1) { int num_chars; gdb_wchar_t *chars; const gdb_byte *buf; size_t buflen; int print_escape = 1; enum wchar_iterate_result result; num_chars = wchar_iterate (iter, &result, &chars, &buf, &buflen); if (num_chars < 0) break; if (num_chars > 0) { /* If all characters are printable, print them. Otherwise, we're going to have to print an escape sequence. We check all characters because we want to print the target bytes in the escape sequence, and we don't know character boundaries there. */ int i; print_escape = 0; for (i = 0; i < num_chars; ++i) if (!wchar_printable (chars[i])) { print_escape = 1; break; } if (!print_escape) { for (i = 0; i < num_chars; ++i) print_wchar (chars[i], buf, buflen, TYPE_LENGTH (type), byte_order, &wchar_buf, quoter, &need_escape); } } /* This handles the NUM_CHARS == 0 case as well. */ if (print_escape) print_wchar (gdb_WEOF, buf, buflen, TYPE_LENGTH (type), byte_order, &wchar_buf, quoter, &need_escape); } /* The output in the host encoding. */ obstack_init (&output); make_cleanup_obstack_free (&output); convert_between_encodings (INTERMEDIATE_ENCODING, host_charset (), obstack_base (&wchar_buf), obstack_object_size (&wchar_buf), 1, &output, translit_char); obstack_1grow (&output, '\0'); fputs_filtered (obstack_base (&output), stream); do_cleanups (cleanups); }
{ build_id = build_id_bfd_get (objfile->obfd); } CATCH (except, RETURN_MASK_ALL) { GDB_PY_HANDLE_EXCEPTION (except); } END_CATCH if (build_id != NULL) { char *hex_form = make_hex_string (build_id->data, build_id->size); PyObject *result; result = PyString_Decode (hex_form, strlen (hex_form), host_charset (), NULL); xfree (hex_form); return result; } Py_RETURN_NONE; } /* An Objfile method which returns the objfile's progspace, or None. */ static PyObject * objfpy_get_progspace (PyObject *self, void *closure) { objfile_object *obj = (objfile_object *) self; if (obj->objfile)
PyObject * host_string_to_python_string (const char *str) { return PyString_Decode (str, strlen (str), host_charset (), NULL); }
SCM gdbscm_scm_from_host_string (const char *string, size_t len) { return gdbscm_scm_from_string (string, len, host_charset (), 1); }
char * gdbscm_scm_to_host_string (SCM string, size_t *lenp, SCM *except_scmp) { return gdbscm_scm_to_string (string, lenp, host_charset (), 1, except_scmp); }
gdb::unique_xmalloc_ptr<char> gdbscm_scm_to_host_string (SCM string, size_t *lenp, SCM *except_scmp) { return gdbscm_scm_to_string (string, lenp, host_charset (), 1, except_scmp); }
void c_printstr (struct ui_file *stream, struct type *type, const gdb_byte *string, unsigned int length, const char *user_encoding, int force_ellipses, const struct value_print_options *options) { enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type)); unsigned int i; unsigned int things_printed = 0; int in_quotes = 0; int need_comma = 0; int width = TYPE_LENGTH (type); struct obstack wchar_buf, output; struct cleanup *cleanup; enum c_string_type str_type; const char *type_encoding; const char *encoding; struct wchar_iterator *iter; int finished = 0; int need_escape = 0; if (length == -1) { unsigned long current_char = 1; for (i = 0; current_char; ++i) { QUIT; current_char = extract_unsigned_integer (string + i * width, width, byte_order); } length = i; } /* If the string was not truncated due to `set print elements', and the last byte of it is a null, we don't print that, in traditional C style. */ if (!force_ellipses && length > 0 && (extract_unsigned_integer (string + (length - 1) * width, width, byte_order) == 0)) length--; str_type = (classify_type (type, get_type_arch (type), &type_encoding) & ~C_CHAR); switch (str_type) { case C_STRING: break; case C_WIDE_STRING: fputs_filtered ("L", stream); break; case C_STRING_16: fputs_filtered ("u", stream); break; case C_STRING_32: fputs_filtered ("U", stream); break; } encoding = (user_encoding && *user_encoding) ? user_encoding : type_encoding; if (length == 0) { fputs_filtered ("\"\"", stream); return; } /* Arrange to iterate over the characters, in wchar_t form. */ iter = make_wchar_iterator (string, length * width, encoding, width); cleanup = make_cleanup_wchar_iterator (iter); /* WCHAR_BUF is the obstack we use to represent the string in wchar_t form. */ obstack_init (&wchar_buf); make_cleanup_obstack_free (&wchar_buf); while (!finished && things_printed < options->print_max) { int num_chars; enum wchar_iterate_result result; gdb_wchar_t *chars; const gdb_byte *buf; size_t buflen; QUIT; if (need_comma) { obstack_grow_wstr (&wchar_buf, LCST (", ")); need_comma = 0; } num_chars = wchar_iterate (iter, &result, &chars, &buf, &buflen); /* We only look at repetitions when we were able to convert a single character in isolation. This makes the code simpler and probably does the sensible thing in the majority of cases. */ while (num_chars == 1 && things_printed < options->print_max) { /* Count the number of repetitions. */ unsigned int reps = 0; gdb_wchar_t current_char = chars[0]; const gdb_byte *orig_buf = buf; int orig_len = buflen; if (need_comma) { obstack_grow_wstr (&wchar_buf, LCST (", ")); need_comma = 0; } while (num_chars == 1 && current_char == chars[0]) { num_chars = wchar_iterate (iter, &result, &chars, &buf, &buflen); ++reps; } /* Emit CURRENT_CHAR according to the repetition count and options. */ if (reps > options->repeat_count_threshold) { if (in_quotes) { if (options->inspect_it) obstack_grow_wstr (&wchar_buf, LCST ("\\\", ")); else obstack_grow_wstr (&wchar_buf, LCST ("\", ")); in_quotes = 0; } obstack_grow_wstr (&wchar_buf, LCST ("'")); need_escape = 0; print_wchar (current_char, orig_buf, orig_len, width, byte_order, &wchar_buf, '\'', &need_escape); obstack_grow_wstr (&wchar_buf, LCST ("'")); { /* Painful gyrations. */ int j; char *s = xstrprintf (_(" <repeats %u times>"), reps); for (j = 0; s[j]; ++j) { gdb_wchar_t w = gdb_btowc (s[j]); obstack_grow (&wchar_buf, &w, sizeof (gdb_wchar_t)); } xfree (s); } things_printed += options->repeat_count_threshold; need_comma = 1; } else { /* Saw the character one or more times, but fewer than the repetition threshold. */ if (!in_quotes) { if (options->inspect_it) obstack_grow_wstr (&wchar_buf, LCST ("\\\"")); else obstack_grow_wstr (&wchar_buf, LCST ("\"")); in_quotes = 1; need_escape = 0; } while (reps-- > 0) { print_wchar (current_char, orig_buf, orig_len, width, byte_order, &wchar_buf, '"', &need_escape); ++things_printed; } } } /* NUM_CHARS and the other outputs from wchar_iterate are valid here regardless of which branch was taken above. */ if (num_chars < 0) { /* Hit EOF. */ finished = 1; break; } switch (result) { case wchar_iterate_invalid: if (!in_quotes) { if (options->inspect_it) obstack_grow_wstr (&wchar_buf, LCST ("\\\"")); else obstack_grow_wstr (&wchar_buf, LCST ("\"")); in_quotes = 1; } need_escape = 0; print_wchar (gdb_WEOF, buf, buflen, width, byte_order, &wchar_buf, '"', &need_escape); break; case wchar_iterate_incomplete: if (in_quotes) { if (options->inspect_it) obstack_grow_wstr (&wchar_buf, LCST ("\\\",")); else obstack_grow_wstr (&wchar_buf, LCST ("\",")); in_quotes = 0; } obstack_grow_wstr (&wchar_buf, LCST (" <incomplete sequence ")); print_wchar (gdb_WEOF, buf, buflen, width, byte_order, &wchar_buf, 0, &need_escape); obstack_grow_wstr (&wchar_buf, LCST (">")); finished = 1; break; } } /* Terminate the quotes if necessary. */ if (in_quotes) { if (options->inspect_it) obstack_grow_wstr (&wchar_buf, LCST ("\\\"")); else obstack_grow_wstr (&wchar_buf, LCST ("\"")); } if (force_ellipses || !finished) obstack_grow_wstr (&wchar_buf, LCST ("...")); /* OUTPUT is where we collect `char's for printing. */ obstack_init (&output); make_cleanup_obstack_free (&output); convert_between_encodings (INTERMEDIATE_ENCODING, host_charset (), obstack_base (&wchar_buf), obstack_object_size (&wchar_buf), 1, &output, translit_char); obstack_1grow (&output, '\0'); fputs_filtered (obstack_base (&output), stream); do_cleanups (cleanup); }