gdb/mi/mi-cmd-disas.c - gdb

Functions defined

Source code

  1. /* MI Command Set - disassemble commands.
  2.    Copyright (C) 2000-2015 Free Software Foundation, Inc.
  3.    Contributed by Cygnus Solutions (a Red Hat company).

  4.    This file is part of GDB.

  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 3 of the License, or
  8.    (at your option) any later version.

  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.

  13.    You should have received a copy of the GNU General Public License
  14.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  15. #include "defs.h"
  16. #include "arch-utils.h"
  17. #include "target.h"
  18. #include "value.h"
  19. #include "mi-cmds.h"
  20. #include "mi-getopt.h"
  21. #include "ui-out.h"
  22. #include "disasm.h"

  23. /* The arguments to be passed on the command line and parsed here are
  24.    either:

  25.    START-ADDRESS: address to start the disassembly at.
  26.    END-ADDRESS: address to end the disassembly at.

  27.    or:

  28.    FILENAME: The name of the file where we want disassemble from.
  29.    LINE: The line around which we want to disassemble. It will
  30.    disassemble the function that contins that line.
  31.    HOW_MANY: Number of disassembly lines to display. With source, it
  32.    is the number of disassembly lines only, not counting the source
  33.    lines.

  34.    always required:

  35.    MODE: 0 -- disassembly.
  36.          1 -- disassembly and source.
  37.          2 -- disassembly and opcodes.
  38.          3 -- disassembly, source and opcodes.
  39. */

  40. void
  41. mi_cmd_disassemble (char *command, char **argv, int argc)
  42. {
  43.   struct gdbarch *gdbarch = get_current_arch ();
  44.   struct ui_out *uiout = current_uiout;
  45.   CORE_ADDR start;

  46.   int mode, disasm_flags;
  47.   struct symtab *s;

  48.   /* Which options have we processed ... */
  49.   int file_seen = 0;
  50.   int line_seen = 0;
  51.   int num_seen = 0;
  52.   int start_seen = 0;
  53.   int end_seen = 0;

  54.   /* ... and their corresponding value. */
  55.   char *file_string = NULL;
  56.   int line_num = -1;
  57.   int how_many = -1;
  58.   CORE_ADDR low = 0;
  59.   CORE_ADDR high = 0;
  60.   struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);

  61.   /* Options processing stuff.  */
  62.   int oind = 0;
  63.   char *oarg;
  64.   enum opt
  65.   {
  66.     FILE_OPT, LINE_OPT, NUM_OPT, START_OPT, END_OPT
  67.   };
  68.   static const struct mi_opt opts[] =
  69.     {
  70.       {"f", FILE_OPT, 1},
  71.       {"l", LINE_OPT, 1},
  72.       {"n", NUM_OPT, 1},
  73.       {"s", START_OPT, 1},
  74.       {"e", END_OPT, 1},
  75.       { 0, 0, 0 }
  76.     };

  77.   /* Get the options with their arguments. Keep track of what we
  78.      encountered.  */
  79.   while (1)
  80.     {
  81.       int opt = mi_getopt ("-data-disassemble", argc, argv, opts,
  82.                            &oind, &oarg);
  83.       if (opt < 0)
  84.         break;
  85.       switch ((enum opt) opt)
  86.         {
  87.         case FILE_OPT:
  88.           file_string = xstrdup (oarg);
  89.           file_seen = 1;
  90.           make_cleanup (xfree, file_string);
  91.           break;
  92.         case LINE_OPT:
  93.           line_num = atoi (oarg);
  94.           line_seen = 1;
  95.           break;
  96.         case NUM_OPT:
  97.           how_many = atoi (oarg);
  98.           num_seen = 1;
  99.           break;
  100.         case START_OPT:
  101.           low = parse_and_eval_address (oarg);
  102.           start_seen = 1;
  103.           break;
  104.         case END_OPT:
  105.           high = parse_and_eval_address (oarg);
  106.           end_seen = 1;
  107.           break;
  108.         }
  109.     }
  110.   argv += oind;
  111.   argc -= oind;

  112.   /* Allow only filename + linenum (with how_many which is not
  113.      required) OR start_addr + end_addr.  */

  114.   if (!((line_seen && file_seen && num_seen && !start_seen && !end_seen)
  115.         || (line_seen && file_seen && !num_seen && !start_seen && !end_seen)
  116.         || (!line_seen && !file_seen && !num_seen && start_seen && end_seen)))
  117.     error (_("-data-disassemble: Usage: ( [-f filename -l linenum [-n "
  118.              "howmany]] | [-s startaddr -e endaddr]) [--] mode."));

  119.   if (argc != 1)
  120.     error (_("-data-disassemble: Usage: [-f filename -l linenum "
  121.              "[-n howmany]] [-s startaddr -e endaddr] [--] mode."));

  122.   mode = atoi (argv[0]);
  123.   if (mode < 0 || mode > 3)
  124.     error (_("-data-disassemble: Mode argument must be 0, 1, 2, or 3."));

  125.   /* Convert the mode into a set of disassembly flags.  */

  126.   disasm_flags = 0;
  127.   if (mode & 0x1)
  128.     disasm_flags |= DISASSEMBLY_SOURCE;
  129.   if (mode & 0x2)
  130.     disasm_flags |= DISASSEMBLY_RAW_INSN;

  131.   /* We must get the function beginning and end where line_num is
  132.      contained.  */

  133.   if (line_seen && file_seen)
  134.     {
  135.       s = lookup_symtab (file_string);
  136.       if (s == NULL)
  137.         error (_("-data-disassemble: Invalid filename."));
  138.       if (!find_line_pc (s, line_num, &start))
  139.         error (_("-data-disassemble: Invalid line number"));
  140.       if (find_pc_partial_function (start, NULL, &low, &high) == 0)
  141.         error (_("-data-disassemble: "
  142.                  "No function contains specified address"));
  143.     }

  144.   gdb_disassembly (gdbarch, uiout,
  145.                      file_string,
  146.                      disasm_flags,
  147.                    how_many, low, high);

  148.   do_cleanups (cleanups);
  149. }