aboutsummaryrefslogtreecommitdiff
path: root/gdb/guile/scm-disasm.c
diff options
context:
space:
mode:
authorDoug Evans <xdje42@gmail.com>2014-02-09 19:40:01 -0800
committerDoug Evans <xdje42@gmail.com>2014-02-09 19:40:01 -0800
commited3ef33944c39d9a3cea72b9a7cef3c20f0e3461 (patch)
tree4e67d95b8ea65bb36a9cade5e37df2ad6289052e /gdb/guile/scm-disasm.c
parent7026a7c16ee82d39e84823f8cc3097a9a940ddb2 (diff)
downloadgdb-ed3ef33944c39d9a3cea72b9a7cef3c20f0e3461.zip
gdb-ed3ef33944c39d9a3cea72b9a7cef3c20f0e3461.tar.gz
gdb-ed3ef33944c39d9a3cea72b9a7cef3c20f0e3461.tar.bz2
Add Guile as an extension language.
* NEWS: Mention Guile scripting. * Makefile.in (SUBDIR_GUILE_OBS): New variable. (SUBDIR_GUILE_SRCS, SUBDIR_GUILE_DEPS): New variables (SUBDIR_GUILE_LDFLAGS, SUBDIR_GUILE_CFLAGS): New variables. (INTERNAL_CPPFLAGS): Add GUILE_CPPFLAGS. (CLIBS): Add GUILE_LIBS. (install-guile): New rule. (guile.o): New rule. (scm-arch.o, scm-auto-load.o, scm-block.o): New rules. (scm-breakpoint.o, scm-disasm.o, scm-exception.o): New rules. (scm-frame.o, scm-iterator.o, scm-lazy-string.o): New rules. (scm-math.o, scm-objfile.o, scm-ports.o): New rules. (scm-pretty-print.o, scm-safe-call.o, scm-gsmob.o): New rules. (scm-string.o, scm-symbol.o, scm-symtab.o): New rules. (scm-type.o, scm-utils.o, scm-value.o): New rules. * configure.ac: New option --with-guile. * configure: Regenerate. * config.in: Regenerate. * auto-load.c: Remove #include "python/python.h". Add #include "gdb/section-scripts.h". (source_section_scripts): Handle Guile scripts. (_initialize_auto_load): Add name of Guile objfile script to scripts-directory help text. * breakpoint.c (condition_command): Tweak comment to include Scheme. * breakpoint.h (gdbscm_breakpoint_object): Add forward decl. (struct breakpoint): New member scm_bp_object. * defs.h (enum command_control_type): New value guile_control. * cli/cli-cmds.c: Remove #include "python/python.h". Add #include "extension.h". (show_user): Update comment. (_initialize_cli_cmds): Update help text for "show user". Update help text for max-user-call-depth. * cli/cli-script.c: Remove #include "python/python.h". Add #include "extension.h". (multi_line_command_p): Add guile_control. (print_command_lines): Handle guile_control. (execute_control_command, recurse_read_control_structure): Ditto. (process_next_line): Recognize "guile" commands. * disasm.c (gdb_disassemble_info): Make non-static. * disasm.h: #include "dis-asm.h". (struct gdbarch): Add forward decl. (gdb_disassemble_info): Declare. * extension.c: #include "guile/guile.h". (extension_languages): Add guile. (get_ext_lang_defn): Handle EXT_LANG_GDB. * extension.h (enum extension_language): New value EXT_LANG_GUILE. * gdbtypes.c (get_unsigned_type_max): New function. (get_signed_type_minmax): New function. * gdbtypes.h (get_unsigned_type_max): Declare. (get_signed_type_minmax): Declare. * guile/README: New file. * guile/guile-internal.h: New file. * guile/guile.c: New file. * guile/guile.h: New file. * guile/scm-arch.c: New file. * guile/scm-auto-load.c: New file. * guile/scm-block.c: New file. * guile/scm-breakpoint.c: New file. * guile/scm-disasm.c: New file. * guile/scm-exception.c: New file. * guile/scm-frame.c: New file. * guile/scm-gsmob.c: New file. * guile/scm-iterator.c: New file. * guile/scm-lazy-string.c: New file. * guile/scm-math.c: New file. * guile/scm-objfile.c: New file. * guile/scm-ports.c: New file. * guile/scm-pretty-print.c: New file. * guile/scm-safe-call.c: New file. * guile/scm-string.c: New file. * guile/scm-symbol.c: New file. * guile/scm-symtab.c: New file. * guile/scm-type.c: New file. * guile/scm-utils.c: New file. * guile/scm-value.c: New file. * guile/lib/gdb.scm: New file. * guile/lib/gdb/boot.scm: New file. * guile/lib/gdb/experimental.scm: New file. * guile/lib/gdb/init.scm: New file. * guile/lib/gdb/iterator.scm: New file. * guile/lib/gdb/printing.scm: New file. * guile/lib/gdb/types.scm: New file. * data-directory/Makefile.in (GUILE_SRCDIR): New variable. (VPATH): Add $(GUILE_SRCDIR). (GUILE_DIR): New variable. (GUILE_INSTALL_DIR, GUILE_FILES): New variables. (all): Add stamp-guile dependency. (stamp-guile): New rule. (clean-guile, install-guile, uninstall-guile): New rules. (install-only): Add install-guile dependency. (uninstall): Add uninstall-guile dependency. (clean): Add clean-guile dependency. doc/ * Makefile.in (GDB_DOC_FILES): Add guile.texi. * gdb.texinfo (Auto-loading): Add set/show auto-load guile-scripts. (Extending GDB): New menu entries Guile, Multiple Extension Languages. (Guile docs): Include guile.texi. (objfile-gdbdotext file): Add objfile-gdb.scm. (dotdebug_gdb_scripts section): Mention Guile scripts. (Multiple Extension Languages): New node. * guile.texi: New file. testsuite/ * configure.ac (AC_OUTPUT): Add gdb.guile. * configure: Regenerate. * lib/gdb-guile.exp: New file. * lib/gdb.exp (get_target_charset): New function. * gdb.base/help.exp: Update expected output from "apropos apropos". * gdb.guile/Makefile.in: New file. * gdb.guile/guile.exp: New file. * gdb.guile/scm-arch.c: New file. * gdb.guile/scm-arch.exp: New file. * gdb.guile/scm-block.c: New file. * gdb.guile/scm-block.exp: New file. * gdb.guile/scm-breakpoint.c: New file. * gdb.guile/scm-breakpoint.exp: New file. * gdb.guile/scm-disasm.c: New file. * gdb.guile/scm-disasm.exp: New file. * gdb.guile/scm-equal.c: New file. * gdb.guile/scm-equal.exp: New file. * gdb.guile/scm-error.exp: New file. * gdb.guile/scm-error.scm: New file. * gdb.guile/scm-frame-args.c: New file. * gdb.guile/scm-frame-args.exp: New file. * gdb.guile/scm-frame-args.scm: New file. * gdb.guile/scm-frame-inline.c: New file. * gdb.guile/scm-frame-inline.exp: New file. * gdb.guile/scm-frame.c: New file. * gdb.guile/scm-frame.exp: New file. * gdb.guile/scm-generics.exp: New file. * gdb.guile/scm-gsmob.exp: New file. * gdb.guile/scm-iterator.c: New file. * gdb.guile/scm-iterator.exp: New file. * gdb.guile/scm-math.c: New file. * gdb.guile/scm-math.exp: New file. * gdb.guile/scm-objfile-script-gdb.in: New file. * gdb.guile/scm-objfile-script.c: New file. * gdb.guile/scm-objfile-script.exp: New file. * gdb.guile/scm-objfile.c: New file. * gdb.guile/scm-objfile.exp: New file. * gdb.guile/scm-ports.exp: New file. * gdb.guile/scm-pretty-print.c: New file. * gdb.guile/scm-pretty-print.exp: New file. * gdb.guile/scm-pretty-print.scm: New file. * gdb.guile/scm-section-script.c: New file. * gdb.guile/scm-section-script.exp: New file. * gdb.guile/scm-section-script.scm: New file. * gdb.guile/scm-symbol.c: New file. * gdb.guile/scm-symbol.exp: New file. * gdb.guile/scm-symtab-2.c: New file. * gdb.guile/scm-symtab.c: New file. * gdb.guile/scm-symtab.exp: New file. * gdb.guile/scm-type.c: New file. * gdb.guile/scm-type.exp: New file. * gdb.guile/scm-value-cc.cc: New file. * gdb.guile/scm-value-cc.exp: New file. * gdb.guile/scm-value.c: New file. * gdb.guile/scm-value.exp: New file. * gdb.guile/source2.scm: New file. * gdb.guile/types-module.cc: New file. * gdb.guile/types-module.exp: New file.
Diffstat (limited to 'gdb/guile/scm-disasm.c')
-rw-r--r--gdb/guile/scm-disasm.c355
1 files changed, 355 insertions, 0 deletions
diff --git a/gdb/guile/scm-disasm.c b/gdb/guile/scm-disasm.c
new file mode 100644
index 0000000..dc76b98
--- /dev/null
+++ b/gdb/guile/scm-disasm.c
@@ -0,0 +1,355 @@
+/* Scheme interface to architecture.
+
+ Copyright (C) 2014 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 "arch-utils.h"
+#include "disasm.h"
+#include "dis-asm.h"
+#include "gdbarch.h"
+#include "gdbcore.h" /* Why is memory_error here? */
+#include "guile-internal.h"
+
+static SCM port_keyword;
+static SCM offset_keyword;
+static SCM size_keyword;
+static SCM count_keyword;
+
+static SCM address_symbol;
+static SCM asm_symbol;
+static SCM length_symbol;
+
+/* Struct used to pass "application data" in disassemble_info. */
+
+struct gdbscm_disasm_data
+{
+ struct gdbarch *gdbarch;
+ SCM port;
+ /* The offset of the address of the first instruction in PORT. */
+ ULONGEST offset;
+};
+
+/* Struct used to pass data from gdbscm_disasm_read_memory to
+ gdbscm_disasm_read_memory_worker. */
+
+struct gdbscm_disasm_read_data
+{
+ bfd_vma memaddr;
+ bfd_byte *myaddr;
+ unsigned int length;
+ struct disassemble_info *dinfo;
+};
+
+/* Subroutine of gdbscm_arch_disassemble to simplify it.
+ Return the result for one instruction. */
+
+static SCM
+dascm_make_insn (CORE_ADDR pc, const char *assembly, int insn_len)
+{
+ return scm_list_3 (scm_cons (address_symbol,
+ gdbscm_scm_from_ulongest (pc)),
+ scm_cons (asm_symbol,
+ gdbscm_scm_from_c_string (assembly)),
+ scm_cons (length_symbol,
+ scm_from_int (insn_len)));
+}
+
+/* Helper function for gdbscm_disasm_read_memory to safely read from a
+ Scheme port. Called via gdbscm_call_guile.
+ The result is a statically allocated error message or NULL if success. */
+
+static void *
+gdbscm_disasm_read_memory_worker (void *datap)
+{
+ struct gdbscm_disasm_read_data *data = datap;
+ struct disassemble_info *dinfo = data->dinfo;
+ struct gdbscm_disasm_data *disasm_data = dinfo->application_data;
+ SCM seekto, newpos, port = disasm_data->port;
+ size_t bytes_read;
+
+ seekto = gdbscm_scm_from_ulongest (data->memaddr - disasm_data->offset);
+ newpos = scm_seek (port, seekto, scm_from_int (SEEK_SET));
+ if (!scm_is_eq (seekto, newpos))
+ return "seek error";
+
+ bytes_read = scm_c_read (port, data->myaddr, data->length);
+
+ if (bytes_read != data->length)
+ return "short read";
+
+ /* If we get here the read succeeded. */
+ return NULL;
+}
+
+/* disassemble_info.read_memory_func for gdbscm_print_insn_from_port. */
+
+static int
+gdbscm_disasm_read_memory (bfd_vma memaddr, bfd_byte *myaddr,
+ unsigned int length,
+ struct disassemble_info *dinfo)
+{
+ struct gdbscm_disasm_read_data data;
+ void *status;
+
+ data.memaddr = memaddr;
+ data.myaddr = myaddr;
+ data.length = length;
+ data.dinfo = dinfo;
+
+ status = gdbscm_with_guile (gdbscm_disasm_read_memory_worker, &data);
+
+ /* TODO: IWBN to distinguish problems reading target memory versus problems
+ with the port (e.g., EOF).
+ We return TARGET_XFER_E_IO here as that's what memory_error looks for. */
+ return status != NULL ? TARGET_XFER_E_IO : 0;
+}
+
+/* disassemble_info.memory_error_func for gdbscm_print_insn_from_port.
+ Technically speaking, we don't need our own memory_error_func,
+ but to not provide one would leave a subtle dependency in the code.
+ This function exists to keep a clear boundary. */
+
+static void
+gdbscm_disasm_memory_error (int status, bfd_vma memaddr,
+ struct disassemble_info *info)
+{
+ memory_error (status, memaddr);
+}
+
+/* disassemble_info.print_address_func for gdbscm_print_insn_from_port.
+ Since we need to use our own application_data value, we need to supply
+ this routine as well. */
+
+static void
+gdbscm_disasm_print_address (bfd_vma addr, struct disassemble_info *info)
+{
+ struct gdbscm_disasm_data *data = info->application_data;
+ struct gdbarch *gdbarch = data->gdbarch;
+
+ print_address (gdbarch, addr, info->stream);
+}
+
+/* Subroutine of gdbscm_arch_disassemble to simplify it.
+ Call gdbarch_print_insn using a port for input.
+ PORT must be seekable.
+ OFFSET is the offset in PORT from which addresses begin.
+ For example, when printing from a bytevector, addresses passed to the
+ bv seek routines must be in the range [0,size). However, the bytevector
+ may represent an instruction at address 0x1234. To handle this case pass
+ 0x1234 for OFFSET.
+ This is based on gdb_print_insn, see it for details. */
+
+static int
+gdbscm_print_insn_from_port (struct gdbarch *gdbarch,
+ SCM port, ULONGEST offset, CORE_ADDR memaddr,
+ struct ui_file *stream, int *branch_delay_insns)
+{
+ struct disassemble_info di;
+ int length;
+ struct gdbscm_disasm_data data;
+
+ di = gdb_disassemble_info (gdbarch, stream);
+ data.gdbarch = gdbarch;
+ data.port = port;
+ data.offset = offset;
+ di.application_data = &data;
+ di.read_memory_func = gdbscm_disasm_read_memory;
+ di.memory_error_func = gdbscm_disasm_memory_error;
+ di.print_address_func = gdbscm_disasm_print_address;
+
+ length = gdbarch_print_insn (gdbarch, memaddr, &di);
+
+ if (branch_delay_insns)
+ {
+ if (di.insn_info_valid)
+ *branch_delay_insns = di.branch_delay_insns;
+ else
+ *branch_delay_insns = 0;
+ }
+
+ return length;
+}
+
+/* (arch-disassemble <gdb:arch> address
+ [#:port port] [#:offset address] [#:size integer] [#:count integer])
+ -> list
+
+ Returns a list of disassembled instructions.
+ If PORT is provided, read bytes from it. Otherwise read target memory.
+ If PORT is #f, read target memory.
+ PORT must be seekable. IWBN to remove this restriction, and a future
+ release may. For now the restriction is in place because it's not clear
+ all disassemblers are strictly sequential.
+ If SIZE is provided, limit the number of bytes read to this amount.
+ If COUNT is provided, limit the number of instructions to this amount.
+
+ Each instruction in the result is an alist:
+ (('address . address) ('asm . disassembly) ('length . length)).
+ We could use a hash table (dictionary) but there aren't that many fields. */
+
+static SCM
+gdbscm_arch_disassemble (SCM self, SCM start_scm, SCM rest)
+{
+ arch_smob *a_smob
+ = arscm_get_arch_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
+ struct gdbarch *gdbarch = arscm_get_gdbarch (a_smob);
+ const SCM keywords[] = {
+ port_keyword, offset_keyword, size_keyword, count_keyword, SCM_BOOL_F
+ };
+ int port_arg_pos = -1, offset_arg_pos = -1;
+ int size_arg_pos = -1, count_arg_pos = -1;
+ SCM port = SCM_BOOL_F;
+ ULONGEST offset = 0;
+ unsigned int count = 1;
+ unsigned int size;
+ ULONGEST start_arg;
+ CORE_ADDR start, end;
+ CORE_ADDR pc;
+ unsigned int i;
+ int using_port;
+ SCM result;
+
+ gdbscm_parse_function_args (FUNC_NAME, SCM_ARG2, keywords, "U#OUuu",
+ start_scm, &start_arg, rest,
+ &port_arg_pos, &port,
+ &offset_arg_pos, &offset,
+ &size_arg_pos, &size,
+ &count_arg_pos, &count);
+ /* START is first stored in a ULONGEST because we don't have a format char
+ for CORE_ADDR, and it's not really worth it to have one yet. */
+ start = start_arg;
+
+ if (port_arg_pos > 0)
+ {
+ SCM_ASSERT_TYPE (gdbscm_is_false (port)
+ || gdbscm_is_true (scm_input_port_p (port)),
+ port, port_arg_pos, FUNC_NAME, _("input port"));
+ }
+ using_port = gdbscm_is_true (port);
+
+ if (offset_arg_pos > 0
+ && (port_arg_pos < 0
+ || gdbscm_is_false (port)))
+ {
+ gdbscm_out_of_range_error (FUNC_NAME, offset_arg_pos,
+ gdbscm_scm_from_ulongest (offset),
+ _("offset provided but port is missing"));
+ }
+
+ if (size_arg_pos > 0)
+ {
+ if (size == 0)
+ return SCM_EOL;
+ /* For now be strict about start+size overflowing. If it becomes
+ a nuisance we can relax things later. */
+ if (start + size < start)
+ {
+ gdbscm_out_of_range_error (FUNC_NAME, 0,
+ scm_list_2 (gdbscm_scm_from_ulongest (start),
+ gdbscm_scm_from_ulongest (size)),
+ _("start+size overflows"));
+ }
+ end = start + size - 1;
+ }
+ else
+ end = ~(CORE_ADDR) 0;
+
+ if (count == 0)
+ return SCM_EOL;
+
+ result = SCM_EOL;
+
+ for (pc = start, i = 0; pc <= end && i < count; )
+ {
+ int insn_len = 0;
+ char *as = NULL;
+ struct ui_file *memfile = mem_fileopen ();
+ struct cleanup *cleanups = make_cleanup_ui_file_delete (memfile);
+ volatile struct gdb_exception except;
+
+ TRY_CATCH (except, RETURN_MASK_ALL)
+ {
+ if (using_port)
+ {
+ insn_len = gdbscm_print_insn_from_port (gdbarch, port, offset,
+ pc, memfile, NULL);
+ }
+ else
+ insn_len = gdb_print_insn (gdbarch, pc, memfile, NULL);
+ }
+ GDBSCM_HANDLE_GDB_EXCEPTION_WITH_CLEANUPS (except, cleanups);
+
+ as = ui_file_xstrdup (memfile, NULL);
+
+ result = scm_cons (dascm_make_insn (pc, as, insn_len),
+ result);
+
+ pc += insn_len;
+ i++;
+ do_cleanups (cleanups);
+ xfree (as);
+ }
+
+ return scm_reverse_x (result, SCM_EOL);
+}
+
+/* Initialize the Scheme architecture support. */
+
+static const scheme_function disasm_functions[] =
+{
+ { "arch-disassemble", 2, 0, 1, gdbscm_arch_disassemble,
+ "\
+Return list of disassembled instructions in memory.\n\
+\n\
+ Arguments: <gdb:arch> start-address\n\
+ [#:port port] [#:offset address]\n\
+ [#:size <integer>] [#:count <integer>]\n\
+ port: If non-#f, it is an input port to read bytes from.\n\
+ offset: Specifies the address offset of the first byte in the port.\n\
+ This is useful if the input is from something other than memory\n\
+ (e.g., a bytevector) and you want the result to be as if the bytes\n\
+ came from that address. The value to pass for start-address is\n\
+ then also the desired disassembly address, not the offset in, e.g.,\n\
+ the bytevector.\n\
+ size: Limit the number of bytes read to this amount.\n\
+ count: Limit the number of instructions to this amount.\n\
+\n\
+ Returns:\n\
+ Each instruction in the result is an alist:\n\
+ (('address . address) ('asm . disassembly) ('length . length))." },
+
+ END_FUNCTIONS
+};
+
+void
+gdbscm_initialize_disasm (void)
+{
+ gdbscm_define_functions (disasm_functions, 1);
+
+ port_keyword = scm_from_latin1_keyword ("port");
+ offset_keyword = scm_from_latin1_keyword ("offset");
+ size_keyword = scm_from_latin1_keyword ("size");
+ count_keyword = scm_from_latin1_keyword ("count");
+
+ address_symbol = scm_from_latin1_symbol ("address");
+ asm_symbol = scm_from_latin1_symbol ("asm");
+ length_symbol = scm_from_latin1_symbol ("length");
+}