198944Sobrien/* MI Command Set.
2130803Smarcel
3130803Smarcel   Copyright 2000, 2001, 2002, 2003, 2004 Free Software Foundation,
4130803Smarcel   Inc.
5130803Smarcel
698944Sobrien   Contributed by Cygnus Solutions (a Red Hat company).
798944Sobrien
898944Sobrien   This file is part of GDB.
998944Sobrien
1098944Sobrien   This program is free software; you can redistribute it and/or modify
1198944Sobrien   it under the terms of the GNU General Public License as published by
1298944Sobrien   the Free Software Foundation; either version 2 of the License, or
1398944Sobrien   (at your option) any later version.
1498944Sobrien
1598944Sobrien   This program is distributed in the hope that it will be useful,
1698944Sobrien   but WITHOUT ANY WARRANTY; without even the implied warranty of
1798944Sobrien   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1898944Sobrien   GNU General Public License for more details.
1998944Sobrien
2098944Sobrien   You should have received a copy of the GNU General Public License
2198944Sobrien   along with this program; if not, write to the Free Software
2298944Sobrien   Foundation, Inc., 59 Temple Place - Suite 330,
2398944Sobrien   Boston, MA 02111-1307, USA.  */
2498944Sobrien
2598944Sobrien/* Work in progress */
2698944Sobrien
2798944Sobrien#include "defs.h"
2898944Sobrien#include "target.h"
2998944Sobrien#include "inferior.h"
3098944Sobrien#include "gdb_string.h"
3198944Sobrien#include "top.h"
3298944Sobrien#include "gdbthread.h"
3398944Sobrien#include "mi-cmds.h"
3498944Sobrien#include "mi-parse.h"
3598944Sobrien#include "mi-getopt.h"
3698944Sobrien#include "mi-console.h"
3798944Sobrien#include "ui-out.h"
3898944Sobrien#include "mi-out.h"
39130803Smarcel#include "interps.h"
4098944Sobrien#include "event-loop.h"
4198944Sobrien#include "event-top.h"
4298944Sobrien#include "gdbcore.h"		/* for write_memory() */
43130803Smarcel#include "value.h"		/* for deprecated_write_register_bytes() */
4498944Sobrien#include "regcache.h"
4598944Sobrien#include "gdb.h"
46130803Smarcel#include "frame.h"
47130803Smarcel#include "mi-main.h"
48130803Smarcel
4998944Sobrien#include <ctype.h>
5098944Sobrien#include <sys/time.h>
5198944Sobrien
5298944Sobrienenum
5398944Sobrien  {
5498944Sobrien    FROM_TTY = 0
5598944Sobrien  };
5698944Sobrien
57130803Smarcel/* Enumerations of the actions that may result from calling
58130803Smarcel   captured_mi_execute_command */
5998944Sobrien
60130803Smarcelenum captured_mi_execute_command_actions
61130803Smarcel  {
62130803Smarcel    EXECUTE_COMMAND_DISPLAY_PROMPT,
63130803Smarcel    EXECUTE_COMMAND_SUPRESS_PROMPT,
64130803Smarcel    EXECUTE_COMMAND_DISPLAY_ERROR
65130803Smarcel  };
66130803Smarcel
67130803Smarcel/* This structure is used to pass information from captured_mi_execute_command
68130803Smarcel   to mi_execute_command. */
69130803Smarcelstruct captured_mi_execute_command_args
70130803Smarcel{
71130803Smarcel  /* This return result of the MI command (output) */
72130803Smarcel  enum mi_cmd_result rc;
73130803Smarcel
74130803Smarcel  /* What action to perform when the call is finished (output) */
75130803Smarcel  enum captured_mi_execute_command_actions action;
76130803Smarcel
77130803Smarcel  /* The command context to be executed (input) */
78130803Smarcel  struct mi_parse *command;
79130803Smarcel};
80130803Smarcel
8198944Sobrienint mi_debug_p;
8298944Sobrienstruct ui_file *raw_stdout;
8398944Sobrien
8498944Sobrien/* The token of the last asynchronous command */
8598944Sobrienstatic char *last_async_command;
8698944Sobrienstatic char *previous_async_command;
87130803Smarcelchar *mi_error_message;
8898944Sobrienstatic char *old_regs;
8998944Sobrien
9098944Sobrienextern void _initialize_mi_main (void);
9198944Sobrienstatic enum mi_cmd_result mi_cmd_execute (struct mi_parse *parse);
9298944Sobrien
93130803Smarcelstatic void mi_execute_cli_command (const char *cmd, int args_p,
94130803Smarcel				    const char *args);
9598944Sobrienstatic enum mi_cmd_result mi_execute_async_cli_command (char *mi, char *args, int from_tty);
9698944Sobrien
97130803Smarcelstatic void mi_exec_async_cli_cmd_continuation (struct continuation_arg *arg);
9898944Sobrien
9998944Sobrienstatic int register_changed_p (int regnum);
10098944Sobrienstatic int get_register (int regnum, int format);
10198944Sobrien
102130803Smarcel/* A helper function which will set mi_error_message to
103130803Smarcel   error_last_message.  */
104130803Smarcelvoid
105130803Smarcelmi_error_last_message (void)
106130803Smarcel{
107130803Smarcel  char *s = error_last_message ();
108130803Smarcel  xasprintf (&mi_error_message, "%s", s);
109130803Smarcel  xfree (s);
110130803Smarcel}
11198944Sobrien
11298944Sobrien/* Command implementations. FIXME: Is this libgdb? No.  This is the MI
11398944Sobrien   layer that calls libgdb.  Any operation used in the below should be
11498944Sobrien   formalized. */
11598944Sobrien
11698944Sobrienenum mi_cmd_result
11798944Sobrienmi_cmd_gdb_exit (char *command, char **argv, int argc)
11898944Sobrien{
11998944Sobrien  /* We have to print everything right here because we never return */
12098944Sobrien  if (last_async_command)
12198944Sobrien    fputs_unfiltered (last_async_command, raw_stdout);
12298944Sobrien  fputs_unfiltered ("^exit\n", raw_stdout);
12398944Sobrien  mi_out_put (uiout, raw_stdout);
12498944Sobrien  /* FIXME: The function called is not yet a formal libgdb function */
12598944Sobrien  quit_force (NULL, FROM_TTY);
12698944Sobrien  return MI_CMD_DONE;
12798944Sobrien}
12898944Sobrien
12998944Sobrienenum mi_cmd_result
13098944Sobrienmi_cmd_exec_run (char *args, int from_tty)
13198944Sobrien{
13298944Sobrien  /* FIXME: Should call a libgdb function, not a cli wrapper */
13398944Sobrien  return mi_execute_async_cli_command ("run", args, from_tty);
13498944Sobrien}
13598944Sobrien
13698944Sobrienenum mi_cmd_result
13798944Sobrienmi_cmd_exec_next (char *args, int from_tty)
13898944Sobrien{
13998944Sobrien  /* FIXME: Should call a libgdb function, not a cli wrapper */
14098944Sobrien  return mi_execute_async_cli_command ("next", args, from_tty);
14198944Sobrien}
14298944Sobrien
14398944Sobrienenum mi_cmd_result
14498944Sobrienmi_cmd_exec_next_instruction (char *args, int from_tty)
14598944Sobrien{
14698944Sobrien  /* FIXME: Should call a libgdb function, not a cli wrapper */
14798944Sobrien  return mi_execute_async_cli_command ("nexti", args, from_tty);
14898944Sobrien}
14998944Sobrien
15098944Sobrienenum mi_cmd_result
15198944Sobrienmi_cmd_exec_step (char *args, int from_tty)
15298944Sobrien{
15398944Sobrien  /* FIXME: Should call a libgdb function, not a cli wrapper */
15498944Sobrien  return mi_execute_async_cli_command ("step", args, from_tty);
15598944Sobrien}
15698944Sobrien
15798944Sobrienenum mi_cmd_result
15898944Sobrienmi_cmd_exec_step_instruction (char *args, int from_tty)
15998944Sobrien{
16098944Sobrien  /* FIXME: Should call a libgdb function, not a cli wrapper */
16198944Sobrien  return mi_execute_async_cli_command ("stepi", args, from_tty);
16298944Sobrien}
16398944Sobrien
16498944Sobrienenum mi_cmd_result
16598944Sobrienmi_cmd_exec_finish (char *args, int from_tty)
16698944Sobrien{
16798944Sobrien  /* FIXME: Should call a libgdb function, not a cli wrapper */
16898944Sobrien  return mi_execute_async_cli_command ("finish", args, from_tty);
16998944Sobrien}
17098944Sobrien
17198944Sobrienenum mi_cmd_result
17298944Sobrienmi_cmd_exec_until (char *args, int from_tty)
17398944Sobrien{
17498944Sobrien  /* FIXME: Should call a libgdb function, not a cli wrapper */
17598944Sobrien  return mi_execute_async_cli_command ("until", args, from_tty);
17698944Sobrien}
17798944Sobrien
17898944Sobrienenum mi_cmd_result
17998944Sobrienmi_cmd_exec_return (char *args, int from_tty)
18098944Sobrien{
18198944Sobrien  /* This command doesn't really execute the target, it just pops the
18298944Sobrien     specified number of frames. */
18398944Sobrien  if (*args)
18498944Sobrien    /* Call return_command with from_tty argument equal to 0 so as to
18598944Sobrien       avoid being queried. */
186130803Smarcel    return_command (args, 0);
18798944Sobrien  else
18898944Sobrien    /* Call return_command with from_tty argument equal to 0 so as to
18998944Sobrien       avoid being queried. */
190130803Smarcel    return_command (NULL, 0);
19198944Sobrien
19298944Sobrien  /* Because we have called return_command with from_tty = 0, we need
19398944Sobrien     to print the frame here. */
194130803Smarcel  print_stack_frame (deprecated_selected_frame,
195130803Smarcel		     frame_relative_level (deprecated_selected_frame),
196130803Smarcel		     LOC_AND_ADDRESS);
19798944Sobrien
19898944Sobrien  return MI_CMD_DONE;
19998944Sobrien}
20098944Sobrien
20198944Sobrienenum mi_cmd_result
20298944Sobrienmi_cmd_exec_continue (char *args, int from_tty)
20398944Sobrien{
20498944Sobrien  /* FIXME: Should call a libgdb function, not a cli wrapper */
20598944Sobrien  return mi_execute_async_cli_command ("continue", args, from_tty);
20698944Sobrien}
20798944Sobrien
20898944Sobrien/* Interrupt the execution of the target. Note how we must play around
20998944Sobrien   with the token varialbes, in order to display the current token in
21098944Sobrien   the result of the interrupt command, and the previous execution
21198944Sobrien   token when the target finally stops. See comments in
21298944Sobrien   mi_cmd_execute. */
21398944Sobrienenum mi_cmd_result
21498944Sobrienmi_cmd_exec_interrupt (char *args, int from_tty)
21598944Sobrien{
21698944Sobrien  if (!target_executing)
21798944Sobrien    {
21898944Sobrien      xasprintf (&mi_error_message,
21998944Sobrien		 "mi_cmd_exec_interrupt: Inferior not executing.");
22098944Sobrien      return MI_CMD_ERROR;
22198944Sobrien    }
222130803Smarcel  interrupt_target_command (args, from_tty);
22398944Sobrien  if (last_async_command)
22498944Sobrien    fputs_unfiltered (last_async_command, raw_stdout);
22598944Sobrien  fputs_unfiltered ("^done", raw_stdout);
22698944Sobrien  xfree (last_async_command);
22798944Sobrien  if (previous_async_command)
22898944Sobrien    last_async_command = xstrdup (previous_async_command);
22998944Sobrien  xfree (previous_async_command);
23098944Sobrien  previous_async_command = NULL;
23198944Sobrien  mi_out_put (uiout, raw_stdout);
23298944Sobrien  mi_out_rewind (uiout);
23398944Sobrien  fputs_unfiltered ("\n", raw_stdout);
23498944Sobrien  return MI_CMD_QUIET;
23598944Sobrien}
23698944Sobrien
23798944Sobrienenum mi_cmd_result
23898944Sobrienmi_cmd_thread_select (char *command, char **argv, int argc)
23998944Sobrien{
24098944Sobrien  enum gdb_rc rc;
24198944Sobrien
24298944Sobrien  if (argc != 1)
24398944Sobrien    {
24498944Sobrien      xasprintf (&mi_error_message,
24598944Sobrien		 "mi_cmd_thread_select: USAGE: threadnum.");
24698944Sobrien      return MI_CMD_ERROR;
24798944Sobrien    }
24898944Sobrien  else
24998944Sobrien    rc = gdb_thread_select (uiout, argv[0]);
25098944Sobrien
251130803Smarcel  /* RC is enum gdb_rc if it is successful (>=0)
252130803Smarcel     enum return_reason if not (<0). */
253130803Smarcel  if ((int) rc < 0 && (enum return_reason) rc == RETURN_ERROR)
25498944Sobrien    return MI_CMD_CAUGHT_ERROR;
255130803Smarcel  else if ((int) rc >= 0 && rc == GDB_RC_FAIL)
256130803Smarcel    return MI_CMD_ERROR;
25798944Sobrien  else
25898944Sobrien    return MI_CMD_DONE;
25998944Sobrien}
26098944Sobrien
26198944Sobrienenum mi_cmd_result
26298944Sobrienmi_cmd_thread_list_ids (char *command, char **argv, int argc)
26398944Sobrien{
26498944Sobrien  enum gdb_rc rc = MI_CMD_DONE;
26598944Sobrien
26698944Sobrien  if (argc != 0)
26798944Sobrien    {
26898944Sobrien      xasprintf (&mi_error_message,
26998944Sobrien		 "mi_cmd_thread_list_ids: No arguments required.");
27098944Sobrien      return MI_CMD_ERROR;
27198944Sobrien    }
27298944Sobrien  else
27398944Sobrien    rc = gdb_list_thread_ids (uiout);
27498944Sobrien
27598944Sobrien  if (rc == GDB_RC_FAIL)
27698944Sobrien    return MI_CMD_CAUGHT_ERROR;
27798944Sobrien  else
27898944Sobrien    return MI_CMD_DONE;
27998944Sobrien}
28098944Sobrien
28198944Sobrienenum mi_cmd_result
28298944Sobrienmi_cmd_data_list_register_names (char *command, char **argv, int argc)
28398944Sobrien{
28498944Sobrien  int regnum, numregs;
28598944Sobrien  int i;
286130803Smarcel  struct cleanup *cleanup;
28798944Sobrien
28898944Sobrien  /* Note that the test for a valid register must include checking the
28998944Sobrien     REGISTER_NAME because NUM_REGS may be allocated for the union of
29098944Sobrien     the register sets within a family of related processors.  In this
29198944Sobrien     case, some entries of REGISTER_NAME will change depending upon
29298944Sobrien     the particular processor being debugged.  */
29398944Sobrien
29498944Sobrien  numregs = NUM_REGS + NUM_PSEUDO_REGS;
29598944Sobrien
296130803Smarcel  cleanup = make_cleanup_ui_out_list_begin_end (uiout, "register-names");
29798944Sobrien
29898944Sobrien  if (argc == 0)		/* No args, just do all the regs */
29998944Sobrien    {
30098944Sobrien      for (regnum = 0;
30198944Sobrien	   regnum < numregs;
30298944Sobrien	   regnum++)
30398944Sobrien	{
30498944Sobrien	  if (REGISTER_NAME (regnum) == NULL
30598944Sobrien	      || *(REGISTER_NAME (regnum)) == '\0')
30698944Sobrien	    ui_out_field_string (uiout, NULL, "");
30798944Sobrien	  else
30898944Sobrien	    ui_out_field_string (uiout, NULL, REGISTER_NAME (regnum));
30998944Sobrien	}
31098944Sobrien    }
31198944Sobrien
31298944Sobrien  /* Else, list of register #s, just do listed regs */
31398944Sobrien  for (i = 0; i < argc; i++)
31498944Sobrien    {
31598944Sobrien      regnum = atoi (argv[i]);
31698944Sobrien      if (regnum < 0 || regnum >= numregs)
31798944Sobrien	{
318130803Smarcel	  do_cleanups (cleanup);
31998944Sobrien	  xasprintf (&mi_error_message, "bad register number");
32098944Sobrien	  return MI_CMD_ERROR;
32198944Sobrien	}
32298944Sobrien      if (REGISTER_NAME (regnum) == NULL
32398944Sobrien	  || *(REGISTER_NAME (regnum)) == '\0')
32498944Sobrien	ui_out_field_string (uiout, NULL, "");
32598944Sobrien      else
32698944Sobrien	ui_out_field_string (uiout, NULL, REGISTER_NAME (regnum));
32798944Sobrien    }
328130803Smarcel  do_cleanups (cleanup);
32998944Sobrien  return MI_CMD_DONE;
33098944Sobrien}
33198944Sobrien
33298944Sobrienenum mi_cmd_result
33398944Sobrienmi_cmd_data_list_changed_registers (char *command, char **argv, int argc)
33498944Sobrien{
33598944Sobrien  int regnum, numregs, changed;
33698944Sobrien  int i;
337130803Smarcel  struct cleanup *cleanup;
33898944Sobrien
33998944Sobrien  /* Note that the test for a valid register must include checking the
34098944Sobrien     REGISTER_NAME because NUM_REGS may be allocated for the union of
34198944Sobrien     the register sets within a family of related processors.  In this
34298944Sobrien     case, some entries of REGISTER_NAME will change depending upon
34398944Sobrien     the particular processor being debugged.  */
34498944Sobrien
34598944Sobrien  numregs = NUM_REGS;
34698944Sobrien
347130803Smarcel  cleanup = make_cleanup_ui_out_list_begin_end (uiout, "changed-registers");
34898944Sobrien
34998944Sobrien  if (argc == 0)		/* No args, just do all the regs */
35098944Sobrien    {
35198944Sobrien      for (regnum = 0;
35298944Sobrien	   regnum < numregs;
35398944Sobrien	   regnum++)
35498944Sobrien	{
35598944Sobrien	  if (REGISTER_NAME (regnum) == NULL
35698944Sobrien	      || *(REGISTER_NAME (regnum)) == '\0')
35798944Sobrien	    continue;
35898944Sobrien	  changed = register_changed_p (regnum);
35998944Sobrien	  if (changed < 0)
36098944Sobrien	    {
361130803Smarcel	      do_cleanups (cleanup);
36298944Sobrien	      xasprintf (&mi_error_message,
36398944Sobrien			 "mi_cmd_data_list_changed_registers: Unable to read register contents.");
36498944Sobrien	      return MI_CMD_ERROR;
36598944Sobrien	    }
36698944Sobrien	  else if (changed)
36798944Sobrien	    ui_out_field_int (uiout, NULL, regnum);
36898944Sobrien	}
36998944Sobrien    }
37098944Sobrien
37198944Sobrien  /* Else, list of register #s, just do listed regs */
37298944Sobrien  for (i = 0; i < argc; i++)
37398944Sobrien    {
37498944Sobrien      regnum = atoi (argv[i]);
37598944Sobrien
37698944Sobrien      if (regnum >= 0
37798944Sobrien	  && regnum < numregs
37898944Sobrien	  && REGISTER_NAME (regnum) != NULL
37998944Sobrien	  && *REGISTER_NAME (regnum) != '\000')
38098944Sobrien	{
38198944Sobrien	  changed = register_changed_p (regnum);
38298944Sobrien	  if (changed < 0)
38398944Sobrien	    {
384130803Smarcel	      do_cleanups (cleanup);
38598944Sobrien	      xasprintf (&mi_error_message,
38698944Sobrien			 "mi_cmd_data_list_register_change: Unable to read register contents.");
38798944Sobrien	      return MI_CMD_ERROR;
38898944Sobrien	    }
38998944Sobrien	  else if (changed)
39098944Sobrien	    ui_out_field_int (uiout, NULL, regnum);
39198944Sobrien	}
39298944Sobrien      else
39398944Sobrien	{
394130803Smarcel	  do_cleanups (cleanup);
39598944Sobrien	  xasprintf (&mi_error_message, "bad register number");
39698944Sobrien	  return MI_CMD_ERROR;
39798944Sobrien	}
39898944Sobrien    }
399130803Smarcel  do_cleanups (cleanup);
40098944Sobrien  return MI_CMD_DONE;
40198944Sobrien}
40298944Sobrien
40398944Sobrienstatic int
40498944Sobrienregister_changed_p (int regnum)
40598944Sobrien{
406130803Smarcel  char raw_buffer[MAX_REGISTER_SIZE];
40798944Sobrien
408130803Smarcel  if (! frame_register_read (deprecated_selected_frame, regnum, raw_buffer))
40998944Sobrien    return -1;
41098944Sobrien
411130803Smarcel  if (memcmp (&old_regs[DEPRECATED_REGISTER_BYTE (regnum)], raw_buffer,
412130803Smarcel	      DEPRECATED_REGISTER_RAW_SIZE (regnum)) == 0)
41398944Sobrien    return 0;
41498944Sobrien
41598944Sobrien  /* Found a changed register. Return 1. */
41698944Sobrien
417130803Smarcel  memcpy (&old_regs[DEPRECATED_REGISTER_BYTE (regnum)], raw_buffer,
418130803Smarcel	  DEPRECATED_REGISTER_RAW_SIZE (regnum));
41998944Sobrien
42098944Sobrien  return 1;
42198944Sobrien}
42298944Sobrien
42398944Sobrien/* Return a list of register number and value pairs. The valid
42498944Sobrien   arguments expected are: a letter indicating the format in which to
42598944Sobrien   display the registers contents. This can be one of: x (hexadecimal), d
42698944Sobrien   (decimal), N (natural), t (binary), o (octal), r (raw).  After the
42798944Sobrien   format argumetn there can be a sequence of numbers, indicating which
42898944Sobrien   registers to fetch the content of. If the format is the only argument,
42998944Sobrien   a list of all the registers with their values is returned. */
43098944Sobrienenum mi_cmd_result
43198944Sobrienmi_cmd_data_list_register_values (char *command, char **argv, int argc)
43298944Sobrien{
43398944Sobrien  int regnum, numregs, format, result;
43498944Sobrien  int i;
435130803Smarcel  struct cleanup *list_cleanup, *tuple_cleanup;
43698944Sobrien
43798944Sobrien  /* Note that the test for a valid register must include checking the
43898944Sobrien     REGISTER_NAME because NUM_REGS may be allocated for the union of
43998944Sobrien     the register sets within a family of related processors.  In this
44098944Sobrien     case, some entries of REGISTER_NAME will change depending upon
44198944Sobrien     the particular processor being debugged.  */
44298944Sobrien
44398944Sobrien  numregs = NUM_REGS;
44498944Sobrien
44598944Sobrien  if (argc == 0)
44698944Sobrien    {
44798944Sobrien      xasprintf (&mi_error_message,
44898944Sobrien		 "mi_cmd_data_list_register_values: Usage: -data-list-register-values <format> [<regnum1>...<regnumN>]");
44998944Sobrien      return MI_CMD_ERROR;
45098944Sobrien    }
45198944Sobrien
45298944Sobrien  format = (int) argv[0][0];
45398944Sobrien
45498944Sobrien  if (!target_has_registers)
45598944Sobrien    {
45698944Sobrien      xasprintf (&mi_error_message,
45798944Sobrien		 "mi_cmd_data_list_register_values: No registers.");
45898944Sobrien      return MI_CMD_ERROR;
45998944Sobrien    }
46098944Sobrien
461130803Smarcel  list_cleanup = make_cleanup_ui_out_list_begin_end (uiout, "register-values");
46298944Sobrien
46398944Sobrien  if (argc == 1)		/* No args, beside the format: do all the regs */
46498944Sobrien    {
46598944Sobrien      for (regnum = 0;
46698944Sobrien	   regnum < numregs;
46798944Sobrien	   regnum++)
46898944Sobrien	{
46998944Sobrien	  if (REGISTER_NAME (regnum) == NULL
47098944Sobrien	      || *(REGISTER_NAME (regnum)) == '\0')
47198944Sobrien	    continue;
472130803Smarcel	  tuple_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
47398944Sobrien	  ui_out_field_int (uiout, "number", regnum);
47498944Sobrien	  result = get_register (regnum, format);
47598944Sobrien	  if (result == -1)
476130803Smarcel	    {
477130803Smarcel	      do_cleanups (list_cleanup);
478130803Smarcel	      return MI_CMD_ERROR;
479130803Smarcel	    }
480130803Smarcel	  do_cleanups (tuple_cleanup);
48198944Sobrien	}
48298944Sobrien    }
48398944Sobrien
48498944Sobrien  /* Else, list of register #s, just do listed regs */
48598944Sobrien  for (i = 1; i < argc; i++)
48698944Sobrien    {
48798944Sobrien      regnum = atoi (argv[i]);
48898944Sobrien
48998944Sobrien      if (regnum >= 0
49098944Sobrien	  && regnum < numregs
49198944Sobrien	  && REGISTER_NAME (regnum) != NULL
49298944Sobrien	  && *REGISTER_NAME (regnum) != '\000')
49398944Sobrien	{
494130803Smarcel	  tuple_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
49598944Sobrien	  ui_out_field_int (uiout, "number", regnum);
49698944Sobrien	  result = get_register (regnum, format);
49798944Sobrien	  if (result == -1)
498130803Smarcel	    {
499130803Smarcel	      do_cleanups (list_cleanup);
500130803Smarcel	      return MI_CMD_ERROR;
501130803Smarcel	    }
502130803Smarcel	  do_cleanups (tuple_cleanup);
50398944Sobrien	}
50498944Sobrien      else
50598944Sobrien	{
506130803Smarcel	  do_cleanups (list_cleanup);
50798944Sobrien	  xasprintf (&mi_error_message, "bad register number");
50898944Sobrien	  return MI_CMD_ERROR;
50998944Sobrien	}
51098944Sobrien    }
511130803Smarcel  do_cleanups (list_cleanup);
51298944Sobrien  return MI_CMD_DONE;
51398944Sobrien}
51498944Sobrien
51598944Sobrien/* Output one register's contents in the desired format. */
51698944Sobrienstatic int
51798944Sobrienget_register (int regnum, int format)
51898944Sobrien{
519130803Smarcel  char raw_buffer[MAX_REGISTER_SIZE];
520130803Smarcel  char virtual_buffer[MAX_REGISTER_SIZE];
52198944Sobrien  int optim;
522130803Smarcel  int realnum;
523130803Smarcel  CORE_ADDR addr;
524130803Smarcel  enum lval_type lval;
52598944Sobrien  static struct ui_stream *stb = NULL;
52698944Sobrien
52798944Sobrien  stb = ui_out_stream_new (uiout);
52898944Sobrien
52998944Sobrien  if (format == 'N')
53098944Sobrien    format = 0;
53198944Sobrien
532130803Smarcel  frame_register (deprecated_selected_frame, regnum, &optim, &lval, &addr,
533130803Smarcel		  &realnum, raw_buffer);
534130803Smarcel
53598944Sobrien  if (optim)
53698944Sobrien    {
53798944Sobrien      xasprintf (&mi_error_message, "Optimized out");
53898944Sobrien      return -1;
53998944Sobrien    }
54098944Sobrien
54198944Sobrien  /* Convert raw data to virtual format if necessary.  */
54298944Sobrien
543130803Smarcel  if (DEPRECATED_REGISTER_CONVERTIBLE_P ()
544130803Smarcel      && DEPRECATED_REGISTER_CONVERTIBLE (regnum))
54598944Sobrien    {
546130803Smarcel      DEPRECATED_REGISTER_CONVERT_TO_VIRTUAL (regnum,
547130803Smarcel				   register_type (current_gdbarch, regnum),
54898944Sobrien				   raw_buffer, virtual_buffer);
54998944Sobrien    }
55098944Sobrien  else
551130803Smarcel    memcpy (virtual_buffer, raw_buffer, DEPRECATED_REGISTER_VIRTUAL_SIZE (regnum));
55298944Sobrien
55398944Sobrien  if (format == 'r')
55498944Sobrien    {
55598944Sobrien      int j;
55698944Sobrien      char *ptr, buf[1024];
55798944Sobrien
55898944Sobrien      strcpy (buf, "0x");
55998944Sobrien      ptr = buf + 2;
560130803Smarcel      for (j = 0; j < DEPRECATED_REGISTER_RAW_SIZE (regnum); j++)
56198944Sobrien	{
562130803Smarcel	  int idx = TARGET_BYTE_ORDER == BFD_ENDIAN_BIG ? j
563130803Smarcel	  : DEPRECATED_REGISTER_RAW_SIZE (regnum) - 1 - j;
56498944Sobrien	  sprintf (ptr, "%02x", (unsigned char) raw_buffer[idx]);
56598944Sobrien	  ptr += 2;
56698944Sobrien	}
56798944Sobrien      ui_out_field_string (uiout, "value", buf);
56898944Sobrien      /*fputs_filtered (buf, gdb_stdout); */
56998944Sobrien    }
57098944Sobrien  else
57198944Sobrien    {
572130803Smarcel      val_print (register_type (current_gdbarch, regnum), virtual_buffer, 0, 0,
57398944Sobrien		 stb->stream, format, 1, 0, Val_pretty_default);
57498944Sobrien      ui_out_field_stream (uiout, "value", stb);
57598944Sobrien      ui_out_stream_delete (stb);
57698944Sobrien    }
57798944Sobrien  return 1;
57898944Sobrien}
57998944Sobrien
58098944Sobrien/* Write given values into registers. The registers and values are
58198944Sobrien   given as pairs. The corresponding MI command is
58298944Sobrien   -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]*/
58398944Sobrienenum mi_cmd_result
58498944Sobrienmi_cmd_data_write_register_values (char *command, char **argv, int argc)
58598944Sobrien{
58698944Sobrien  int regnum;
58798944Sobrien  int i;
58898944Sobrien  int numregs;
58998944Sobrien  LONGEST value;
59098944Sobrien  char format;
59198944Sobrien
59298944Sobrien  /* Note that the test for a valid register must include checking the
59398944Sobrien     REGISTER_NAME because NUM_REGS may be allocated for the union of
59498944Sobrien     the register sets within a family of related processors.  In this
59598944Sobrien     case, some entries of REGISTER_NAME will change depending upon
59698944Sobrien     the particular processor being debugged.  */
59798944Sobrien
59898944Sobrien  numregs = NUM_REGS;
59998944Sobrien
60098944Sobrien  if (argc == 0)
60198944Sobrien    {
60298944Sobrien      xasprintf (&mi_error_message,
60398944Sobrien		 "mi_cmd_data_write_register_values: Usage: -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]");
60498944Sobrien      return MI_CMD_ERROR;
60598944Sobrien    }
60698944Sobrien
60798944Sobrien  format = (int) argv[0][0];
60898944Sobrien
60998944Sobrien  if (!target_has_registers)
61098944Sobrien    {
61198944Sobrien      xasprintf (&mi_error_message,
61298944Sobrien		 "mi_cmd_data_write_register_values: No registers.");
61398944Sobrien      return MI_CMD_ERROR;
61498944Sobrien    }
61598944Sobrien
61698944Sobrien  if (!(argc - 1))
61798944Sobrien    {
61898944Sobrien      xasprintf (&mi_error_message,
61998944Sobrien		 "mi_cmd_data_write_register_values: No regs and values specified.");
62098944Sobrien      return MI_CMD_ERROR;
62198944Sobrien    }
62298944Sobrien
62398944Sobrien  if ((argc - 1) % 2)
62498944Sobrien    {
62598944Sobrien      xasprintf (&mi_error_message,
62698944Sobrien		 "mi_cmd_data_write_register_values: Regs and vals are not in pairs.");
62798944Sobrien      return MI_CMD_ERROR;
62898944Sobrien    }
62998944Sobrien
63098944Sobrien  for (i = 1; i < argc; i = i + 2)
63198944Sobrien    {
63298944Sobrien      regnum = atoi (argv[i]);
63398944Sobrien
63498944Sobrien      if (regnum >= 0
63598944Sobrien	  && regnum < numregs
63698944Sobrien	  && REGISTER_NAME (regnum) != NULL
63798944Sobrien	  && *REGISTER_NAME (regnum) != '\000')
63898944Sobrien	{
63998944Sobrien	  void *buffer;
64098944Sobrien	  struct cleanup *old_chain;
64198944Sobrien
64298944Sobrien	  /* Get the value as a number */
64398944Sobrien	  value = parse_and_eval_address (argv[i + 1]);
64498944Sobrien	  /* Get the value into an array */
645130803Smarcel	  buffer = xmalloc (DEPRECATED_REGISTER_SIZE);
64698944Sobrien	  old_chain = make_cleanup (xfree, buffer);
647130803Smarcel	  store_signed_integer (buffer, DEPRECATED_REGISTER_SIZE, value);
64898944Sobrien	  /* Write it down */
649130803Smarcel	  deprecated_write_register_bytes (DEPRECATED_REGISTER_BYTE (regnum), buffer, DEPRECATED_REGISTER_RAW_SIZE (regnum));
65098944Sobrien	  /* Free the buffer.  */
65198944Sobrien	  do_cleanups (old_chain);
65298944Sobrien	}
65398944Sobrien      else
65498944Sobrien	{
65598944Sobrien	  xasprintf (&mi_error_message, "bad register number");
65698944Sobrien	  return MI_CMD_ERROR;
65798944Sobrien	}
65898944Sobrien    }
65998944Sobrien  return MI_CMD_DONE;
66098944Sobrien}
66198944Sobrien
66298944Sobrien#if 0
66398944Sobrien/*This is commented out because we decided it was not useful. I leave
66498944Sobrien   it, just in case. ezannoni:1999-12-08 */
66598944Sobrien
66698944Sobrien/* Assign a value to a variable. The expression argument must be in
66798944Sobrien   the form A=2 or "A = 2" (I.e. if there are spaces it needs to be
66898944Sobrien   quoted. */
66998944Sobrienenum mi_cmd_result
67098944Sobrienmi_cmd_data_assign (char *command, char **argv, int argc)
67198944Sobrien{
67298944Sobrien  struct expression *expr;
67398944Sobrien  struct cleanup *old_chain;
67498944Sobrien
67598944Sobrien  if (argc != 1)
67698944Sobrien    {
67798944Sobrien      xasprintf (&mi_error_message,
67898944Sobrien		 "mi_cmd_data_assign: Usage: -data-assign expression");
67998944Sobrien      return MI_CMD_ERROR;
68098944Sobrien    }
68198944Sobrien
68298944Sobrien  /* NOTE what follows is a clone of set_command(). FIXME: ezannoni
68398944Sobrien     01-12-1999: Need to decide what to do with this for libgdb purposes. */
68498944Sobrien
68598944Sobrien  expr = parse_expression (argv[0]);
68698944Sobrien  old_chain = make_cleanup (free_current_contents, &expr);
68798944Sobrien  evaluate_expression (expr);
68898944Sobrien  do_cleanups (old_chain);
68998944Sobrien  return MI_CMD_DONE;
69098944Sobrien}
69198944Sobrien#endif
69298944Sobrien
69398944Sobrien/* Evaluate the value of the argument. The argument is an
69498944Sobrien   expression. If the expression contains spaces it needs to be
69598944Sobrien   included in double quotes. */
69698944Sobrienenum mi_cmd_result
69798944Sobrienmi_cmd_data_evaluate_expression (char *command, char **argv, int argc)
69898944Sobrien{
69998944Sobrien  struct expression *expr;
70098944Sobrien  struct cleanup *old_chain = NULL;
70198944Sobrien  struct value *val;
70298944Sobrien  struct ui_stream *stb = NULL;
70398944Sobrien
70498944Sobrien  stb = ui_out_stream_new (uiout);
70598944Sobrien
70698944Sobrien  if (argc != 1)
70798944Sobrien    {
70898944Sobrien      xasprintf (&mi_error_message,
70998944Sobrien		 "mi_cmd_data_evaluate_expression: Usage: -data-evaluate-expression expression");
71098944Sobrien      return MI_CMD_ERROR;
71198944Sobrien    }
71298944Sobrien
71398944Sobrien  expr = parse_expression (argv[0]);
71498944Sobrien
71598944Sobrien  old_chain = make_cleanup (free_current_contents, &expr);
71698944Sobrien
71798944Sobrien  val = evaluate_expression (expr);
71898944Sobrien
71998944Sobrien  /* Print the result of the expression evaluation. */
72098944Sobrien  val_print (VALUE_TYPE (val), VALUE_CONTENTS (val),
72198944Sobrien	     VALUE_EMBEDDED_OFFSET (val), VALUE_ADDRESS (val),
72298944Sobrien	     stb->stream, 0, 0, 0, 0);
72398944Sobrien
72498944Sobrien  ui_out_field_stream (uiout, "value", stb);
72598944Sobrien  ui_out_stream_delete (stb);
72698944Sobrien
72798944Sobrien  do_cleanups (old_chain);
72898944Sobrien
72998944Sobrien  return MI_CMD_DONE;
73098944Sobrien}
73198944Sobrien
73298944Sobrienenum mi_cmd_result
73398944Sobrienmi_cmd_target_download (char *args, int from_tty)
73498944Sobrien{
73598944Sobrien  char *run;
73698944Sobrien  struct cleanup *old_cleanups = NULL;
73798944Sobrien
73898944Sobrien  xasprintf (&run, "load %s", args);
73998944Sobrien  old_cleanups = make_cleanup (xfree, run);
74098944Sobrien  execute_command (run, from_tty);
74198944Sobrien
74298944Sobrien  do_cleanups (old_cleanups);
74398944Sobrien  return MI_CMD_DONE;
74498944Sobrien}
74598944Sobrien
74698944Sobrien/* Connect to the remote target. */
74798944Sobrienenum mi_cmd_result
74898944Sobrienmi_cmd_target_select (char *args, int from_tty)
74998944Sobrien{
75098944Sobrien  char *run;
75198944Sobrien  struct cleanup *old_cleanups = NULL;
75298944Sobrien
75398944Sobrien  xasprintf (&run, "target %s", args);
75498944Sobrien  old_cleanups = make_cleanup (xfree, run);
75598944Sobrien
75698944Sobrien  /* target-select is always synchronous.  once the call has returned
75798944Sobrien     we know that we are connected. */
75898944Sobrien  /* NOTE: At present all targets that are connected are also
75998944Sobrien     (implicitly) talking to a halted target.  In the future this may
76098944Sobrien     change. */
76198944Sobrien  execute_command (run, from_tty);
76298944Sobrien
76398944Sobrien  do_cleanups (old_cleanups);
76498944Sobrien
76598944Sobrien  /* Issue the completion message here. */
76698944Sobrien  if (last_async_command)
76798944Sobrien    fputs_unfiltered (last_async_command, raw_stdout);
76898944Sobrien  fputs_unfiltered ("^connected", raw_stdout);
76998944Sobrien  mi_out_put (uiout, raw_stdout);
77098944Sobrien  mi_out_rewind (uiout);
77198944Sobrien  fputs_unfiltered ("\n", raw_stdout);
77298944Sobrien  do_exec_cleanups (ALL_CLEANUPS);
77398944Sobrien  return MI_CMD_QUIET;
77498944Sobrien}
77598944Sobrien
77698944Sobrien/* DATA-MEMORY-READ:
77798944Sobrien
77898944Sobrien   ADDR: start address of data to be dumped.
77998944Sobrien   WORD-FORMAT: a char indicating format for the ``word''. See
78098944Sobrien   the ``x'' command.
78198944Sobrien   WORD-SIZE: size of each ``word''; 1,2,4, or 8 bytes
78298944Sobrien   NR_ROW: Number of rows.
78398944Sobrien   NR_COL: The number of colums (words per row).
78498944Sobrien   ASCHAR: (OPTIONAL) Append an ascii character dump to each row.  Use
78598944Sobrien   ASCHAR for unprintable characters.
78698944Sobrien
78798944Sobrien   Reads SIZE*NR_ROW*NR_COL bytes starting at ADDR from memory and
78898944Sobrien   displayes them.  Returns:
78998944Sobrien
79098944Sobrien   {addr="...",rowN={wordN="..." ,... [,ascii="..."]}, ...}
79198944Sobrien
79298944Sobrien   Returns:
79398944Sobrien   The number of bytes read is SIZE*ROW*COL. */
79498944Sobrien
79598944Sobrienenum mi_cmd_result
79698944Sobrienmi_cmd_data_read_memory (char *command, char **argv, int argc)
79798944Sobrien{
79898944Sobrien  struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
79998944Sobrien  CORE_ADDR addr;
80098944Sobrien  long total_bytes;
80198944Sobrien  long nr_cols;
80298944Sobrien  long nr_rows;
80398944Sobrien  char word_format;
80498944Sobrien  struct type *word_type;
80598944Sobrien  long word_size;
80698944Sobrien  char word_asize;
80798944Sobrien  char aschar;
80898944Sobrien  char *mbuf;
80998944Sobrien  int nr_bytes;
81098944Sobrien  long offset = 0;
81198944Sobrien  int optind = 0;
81298944Sobrien  char *optarg;
81398944Sobrien  enum opt
81498944Sobrien    {
81598944Sobrien      OFFSET_OPT
81698944Sobrien    };
81798944Sobrien  static struct mi_opt opts[] =
81898944Sobrien  {
81998944Sobrien    {"o", OFFSET_OPT, 1},
82098944Sobrien    0
82198944Sobrien  };
82298944Sobrien
82398944Sobrien  while (1)
82498944Sobrien    {
82598944Sobrien      int opt = mi_getopt ("mi_cmd_data_read_memory", argc, argv, opts,
82698944Sobrien			   &optind, &optarg);
82798944Sobrien      if (opt < 0)
82898944Sobrien	break;
82998944Sobrien      switch ((enum opt) opt)
83098944Sobrien	{
83198944Sobrien	case OFFSET_OPT:
83298944Sobrien	  offset = atol (optarg);
83398944Sobrien	  break;
83498944Sobrien	}
83598944Sobrien    }
83698944Sobrien  argv += optind;
83798944Sobrien  argc -= optind;
83898944Sobrien
83998944Sobrien  if (argc < 5 || argc > 6)
84098944Sobrien    {
84198944Sobrien      xasprintf (&mi_error_message,
84298944Sobrien		 "mi_cmd_data_read_memory: Usage: ADDR WORD-FORMAT WORD-SIZE NR-ROWS NR-COLS [ASCHAR].");
84398944Sobrien      return MI_CMD_ERROR;
84498944Sobrien    }
84598944Sobrien
84698944Sobrien  /* Extract all the arguments. */
84798944Sobrien
84898944Sobrien  /* Start address of the memory dump. */
84998944Sobrien  addr = parse_and_eval_address (argv[0]) + offset;
85098944Sobrien  /* The format character to use when displaying a memory word. See
85198944Sobrien     the ``x'' command. */
85298944Sobrien  word_format = argv[1][0];
85398944Sobrien  /* The size of the memory word. */
85498944Sobrien  word_size = atol (argv[2]);
85598944Sobrien  switch (word_size)
85698944Sobrien    {
85798944Sobrien    case 1:
85898944Sobrien      word_type = builtin_type_int8;
85998944Sobrien      word_asize = 'b';
86098944Sobrien      break;
86198944Sobrien    case 2:
86298944Sobrien      word_type = builtin_type_int16;
86398944Sobrien      word_asize = 'h';
86498944Sobrien      break;
86598944Sobrien    case 4:
86698944Sobrien      word_type = builtin_type_int32;
86798944Sobrien      word_asize = 'w';
86898944Sobrien      break;
86998944Sobrien    case 8:
87098944Sobrien      word_type = builtin_type_int64;
87198944Sobrien      word_asize = 'g';
87298944Sobrien      break;
87398944Sobrien    default:
87498944Sobrien      word_type = builtin_type_int8;
87598944Sobrien      word_asize = 'b';
87698944Sobrien    }
87798944Sobrien  /* The number of rows */
87898944Sobrien  nr_rows = atol (argv[3]);
87998944Sobrien  if (nr_rows <= 0)
88098944Sobrien    {
88198944Sobrien      xasprintf (&mi_error_message,
88298944Sobrien		 "mi_cmd_data_read_memory: invalid number of rows.");
88398944Sobrien      return MI_CMD_ERROR;
88498944Sobrien    }
88598944Sobrien  /* number of bytes per row. */
88698944Sobrien  nr_cols = atol (argv[4]);
88798944Sobrien  if (nr_cols <= 0)
88898944Sobrien    {
88998944Sobrien      xasprintf (&mi_error_message,
89098944Sobrien		 "mi_cmd_data_read_memory: invalid number of columns.");
89198944Sobrien    }
89298944Sobrien  /* The un-printable character when printing ascii. */
89398944Sobrien  if (argc == 6)
89498944Sobrien    aschar = *argv[5];
89598944Sobrien  else
89698944Sobrien    aschar = 0;
89798944Sobrien
89898944Sobrien  /* create a buffer and read it in. */
89998944Sobrien  total_bytes = word_size * nr_rows * nr_cols;
90098944Sobrien  mbuf = xcalloc (total_bytes, 1);
90198944Sobrien  make_cleanup (xfree, mbuf);
90298944Sobrien  if (mbuf == NULL)
90398944Sobrien    {
90498944Sobrien      xasprintf (&mi_error_message,
90598944Sobrien		 "mi_cmd_data_read_memory: out of memory.");
90698944Sobrien      return MI_CMD_ERROR;
90798944Sobrien    }
90898944Sobrien  nr_bytes = 0;
90998944Sobrien  while (nr_bytes < total_bytes)
91098944Sobrien    {
91198944Sobrien      int error;
91298944Sobrien      long num = target_read_memory_partial (addr + nr_bytes, mbuf + nr_bytes,
91398944Sobrien					     total_bytes - nr_bytes,
91498944Sobrien					     &error);
91598944Sobrien      if (num <= 0)
91698944Sobrien	break;
91798944Sobrien      nr_bytes += num;
91898944Sobrien    }
91998944Sobrien
92098944Sobrien  /* output the header information. */
92198944Sobrien  ui_out_field_core_addr (uiout, "addr", addr);
92298944Sobrien  ui_out_field_int (uiout, "nr-bytes", nr_bytes);
92398944Sobrien  ui_out_field_int (uiout, "total-bytes", total_bytes);
92498944Sobrien  ui_out_field_core_addr (uiout, "next-row", addr + word_size * nr_cols);
92598944Sobrien  ui_out_field_core_addr (uiout, "prev-row", addr - word_size * nr_cols);
92698944Sobrien  ui_out_field_core_addr (uiout, "next-page", addr + total_bytes);
92798944Sobrien  ui_out_field_core_addr (uiout, "prev-page", addr - total_bytes);
92898944Sobrien
92998944Sobrien  /* Build the result as a two dimentional table. */
93098944Sobrien  {
93198944Sobrien    struct ui_stream *stream = ui_out_stream_new (uiout);
932130803Smarcel    struct cleanup *cleanup_list_memory;
93398944Sobrien    int row;
93498944Sobrien    int row_byte;
935130803Smarcel    cleanup_list_memory = make_cleanup_ui_out_list_begin_end (uiout, "memory");
93698944Sobrien    for (row = 0, row_byte = 0;
93798944Sobrien	 row < nr_rows;
93898944Sobrien	 row++, row_byte += nr_cols * word_size)
93998944Sobrien      {
94098944Sobrien	int col;
94198944Sobrien	int col_byte;
942130803Smarcel	struct cleanup *cleanup_tuple;
943130803Smarcel	struct cleanup *cleanup_list_data;
944130803Smarcel	cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
94598944Sobrien	ui_out_field_core_addr (uiout, "addr", addr + row_byte);
94698944Sobrien	/* ui_out_field_core_addr_symbolic (uiout, "saddr", addr + row_byte); */
947130803Smarcel	cleanup_list_data = make_cleanup_ui_out_list_begin_end (uiout, "data");
94898944Sobrien	for (col = 0, col_byte = row_byte;
94998944Sobrien	     col < nr_cols;
95098944Sobrien	     col++, col_byte += word_size)
95198944Sobrien	  {
95298944Sobrien	    if (col_byte + word_size > nr_bytes)
95398944Sobrien	      {
95498944Sobrien		ui_out_field_string (uiout, NULL, "N/A");
95598944Sobrien	      }
95698944Sobrien	    else
95798944Sobrien	      {
95898944Sobrien		ui_file_rewind (stream->stream);
95998944Sobrien		print_scalar_formatted (mbuf + col_byte, word_type, word_format,
96098944Sobrien					word_asize, stream->stream);
96198944Sobrien		ui_out_field_stream (uiout, NULL, stream);
96298944Sobrien	      }
96398944Sobrien	  }
964130803Smarcel	do_cleanups (cleanup_list_data);
96598944Sobrien	if (aschar)
96698944Sobrien	  {
96798944Sobrien	    int byte;
96898944Sobrien	    ui_file_rewind (stream->stream);
96998944Sobrien	    for (byte = row_byte; byte < row_byte + word_size * nr_cols; byte++)
97098944Sobrien	      {
97198944Sobrien		if (byte >= nr_bytes)
97298944Sobrien		  {
97398944Sobrien		    fputc_unfiltered ('X', stream->stream);
97498944Sobrien		  }
97598944Sobrien		else if (mbuf[byte] < 32 || mbuf[byte] > 126)
97698944Sobrien		  {
97798944Sobrien		    fputc_unfiltered (aschar, stream->stream);
97898944Sobrien		  }
97998944Sobrien		else
98098944Sobrien		  fputc_unfiltered (mbuf[byte], stream->stream);
98198944Sobrien	      }
98298944Sobrien	    ui_out_field_stream (uiout, "ascii", stream);
98398944Sobrien	  }
984130803Smarcel	do_cleanups (cleanup_tuple);
98598944Sobrien      }
98698944Sobrien    ui_out_stream_delete (stream);
987130803Smarcel    do_cleanups (cleanup_list_memory);
98898944Sobrien  }
98998944Sobrien  do_cleanups (cleanups);
99098944Sobrien  return MI_CMD_DONE;
99198944Sobrien}
99298944Sobrien
99398944Sobrien/* DATA-MEMORY-WRITE:
99498944Sobrien
99598944Sobrien   COLUMN_OFFSET: optional argument. Must be preceeded by '-o'. The
99698944Sobrien   offset from the beginning of the memory grid row where the cell to
99798944Sobrien   be written is.
99898944Sobrien   ADDR: start address of the row in the memory grid where the memory
99998944Sobrien   cell is, if OFFSET_COLUMN is specified. Otherwise, the address of
100098944Sobrien   the location to write to.
100198944Sobrien   FORMAT: a char indicating format for the ``word''. See
100298944Sobrien   the ``x'' command.
100398944Sobrien   WORD_SIZE: size of each ``word''; 1,2,4, or 8 bytes
100498944Sobrien   VALUE: value to be written into the memory address.
100598944Sobrien
100698944Sobrien   Writes VALUE into ADDR + (COLUMN_OFFSET * WORD_SIZE).
100798944Sobrien
100898944Sobrien   Prints nothing. */
100998944Sobrienenum mi_cmd_result
101098944Sobrienmi_cmd_data_write_memory (char *command, char **argv, int argc)
101198944Sobrien{
101298944Sobrien  CORE_ADDR addr;
101398944Sobrien  char word_format;
101498944Sobrien  long word_size;
101598944Sobrien  /* FIXME: ezannoni 2000-02-17 LONGEST could possibly not be big
101698944Sobrien     enough when using a compiler other than GCC. */
101798944Sobrien  LONGEST value;
101898944Sobrien  void *buffer;
101998944Sobrien  struct cleanup *old_chain;
102098944Sobrien  long offset = 0;
102198944Sobrien  int optind = 0;
102298944Sobrien  char *optarg;
102398944Sobrien  enum opt
102498944Sobrien    {
102598944Sobrien      OFFSET_OPT
102698944Sobrien    };
102798944Sobrien  static struct mi_opt opts[] =
102898944Sobrien  {
102998944Sobrien    {"o", OFFSET_OPT, 1},
103098944Sobrien    0
103198944Sobrien  };
103298944Sobrien
103398944Sobrien  while (1)
103498944Sobrien    {
103598944Sobrien      int opt = mi_getopt ("mi_cmd_data_write_memory", argc, argv, opts,
103698944Sobrien			   &optind, &optarg);
103798944Sobrien      if (opt < 0)
103898944Sobrien	break;
103998944Sobrien      switch ((enum opt) opt)
104098944Sobrien	{
104198944Sobrien	case OFFSET_OPT:
104298944Sobrien	  offset = atol (optarg);
104398944Sobrien	  break;
104498944Sobrien	}
104598944Sobrien    }
104698944Sobrien  argv += optind;
104798944Sobrien  argc -= optind;
104898944Sobrien
104998944Sobrien  if (argc != 4)
105098944Sobrien    {
105198944Sobrien      xasprintf (&mi_error_message,
105298944Sobrien		 "mi_cmd_data_write_memory: Usage: [-o COLUMN_OFFSET] ADDR FORMAT WORD-SIZE VALUE.");
105398944Sobrien      return MI_CMD_ERROR;
105498944Sobrien    }
105598944Sobrien
105698944Sobrien  /* Extract all the arguments. */
105798944Sobrien  /* Start address of the memory dump. */
105898944Sobrien  addr = parse_and_eval_address (argv[0]);
105998944Sobrien  /* The format character to use when displaying a memory word. See
106098944Sobrien     the ``x'' command. */
106198944Sobrien  word_format = argv[1][0];
106298944Sobrien  /* The size of the memory word. */
106398944Sobrien  word_size = atol (argv[2]);
106498944Sobrien
106598944Sobrien  /* Calculate the real address of the write destination. */
106698944Sobrien  addr += (offset * word_size);
106798944Sobrien
106898944Sobrien  /* Get the value as a number */
106998944Sobrien  value = parse_and_eval_address (argv[3]);
107098944Sobrien  /* Get the value into an array */
107198944Sobrien  buffer = xmalloc (word_size);
107298944Sobrien  old_chain = make_cleanup (xfree, buffer);
107398944Sobrien  store_signed_integer (buffer, word_size, value);
107498944Sobrien  /* Write it down to memory */
107598944Sobrien  write_memory (addr, buffer, word_size);
107698944Sobrien  /* Free the buffer.  */
107798944Sobrien  do_cleanups (old_chain);
107898944Sobrien
107998944Sobrien  return MI_CMD_DONE;
108098944Sobrien}
108198944Sobrien
1082130803Smarcel/* Execute a command within a safe environment.
1083130803Smarcel   Return <0 for error; >=0 for ok.
108498944Sobrien
1085130803Smarcel   args->action will tell mi_execute_command what action
1086130803Smarcel   to perfrom after the given command has executed (display/supress
1087130803Smarcel   prompt, display error). */
1088130803Smarcel
108998944Sobrienstatic int
1090130803Smarcelcaptured_mi_execute_command (struct ui_out *uiout, void *data)
109198944Sobrien{
1092130803Smarcel  struct captured_mi_execute_command_args *args =
1093130803Smarcel    (struct captured_mi_execute_command_args *) data;
1094130803Smarcel  struct mi_parse *context = args->command;
109598944Sobrien
109698944Sobrien  switch (context->op)
109798944Sobrien    {
109898944Sobrien
109998944Sobrien    case MI_COMMAND:
110098944Sobrien      /* A MI command was read from the input stream */
110198944Sobrien      if (mi_debug_p)
110298944Sobrien	/* FIXME: gdb_???? */
110398944Sobrien	fprintf_unfiltered (raw_stdout, " token=`%s' command=`%s' args=`%s'\n",
110498944Sobrien			    context->token, context->command, context->args);
110598944Sobrien      /* FIXME: cagney/1999-09-25: Rather than this convoluted
110698944Sobrien         condition expression, each function should return an
110798944Sobrien         indication of what action is required and then switch on
110898944Sobrien         that. */
1109130803Smarcel      args->action = EXECUTE_COMMAND_DISPLAY_PROMPT;
1110130803Smarcel      args->rc = mi_cmd_execute (context);
1111130803Smarcel
111298944Sobrien      if (!target_can_async_p () || !target_executing)
111398944Sobrien	{
1114130803Smarcel	  /* print the result if there were no errors
1115130803Smarcel
1116130803Smarcel	     Remember that on the way out of executing a command, you have
1117130803Smarcel	     to directly use the mi_interp's uiout, since the command could
1118130803Smarcel	     have reset the interpreter, in which case the current uiout
1119130803Smarcel	     will most likely crash in the mi_out_* routines.  */
1120130803Smarcel	  if (args->rc == MI_CMD_DONE)
112198944Sobrien	    {
112298944Sobrien	      fputs_unfiltered (context->token, raw_stdout);
112398944Sobrien	      fputs_unfiltered ("^done", raw_stdout);
112498944Sobrien	      mi_out_put (uiout, raw_stdout);
112598944Sobrien	      mi_out_rewind (uiout);
112698944Sobrien	      fputs_unfiltered ("\n", raw_stdout);
112798944Sobrien	    }
1128130803Smarcel	  else if (args->rc == MI_CMD_ERROR)
112998944Sobrien	    {
113098944Sobrien	      if (mi_error_message)
113198944Sobrien		{
113298944Sobrien		  fputs_unfiltered (context->token, raw_stdout);
113398944Sobrien		  fputs_unfiltered ("^error,msg=\"", raw_stdout);
113498944Sobrien		  fputstr_unfiltered (mi_error_message, '"', raw_stdout);
113598944Sobrien		  xfree (mi_error_message);
113698944Sobrien		  fputs_unfiltered ("\"\n", raw_stdout);
113798944Sobrien		}
113898944Sobrien	      mi_out_rewind (uiout);
113998944Sobrien	    }
1140130803Smarcel	  else if (args->rc == MI_CMD_CAUGHT_ERROR)
114198944Sobrien	    {
114298944Sobrien	      mi_out_rewind (uiout);
1143130803Smarcel	      args->action = EXECUTE_COMMAND_DISPLAY_ERROR;
1144130803Smarcel	      return 1;
114598944Sobrien	    }
114698944Sobrien	  else
114798944Sobrien	    mi_out_rewind (uiout);
114898944Sobrien	}
114998944Sobrien      else if (sync_execution)
1150130803Smarcel	{
1151130803Smarcel	  /* Don't print the prompt. We are executing the target in
1152130803Smarcel	     synchronous mode. */
1153130803Smarcel	  args->action = EXECUTE_COMMAND_SUPRESS_PROMPT;
1154130803Smarcel	  return 1;
1155130803Smarcel	}
115698944Sobrien      break;
115798944Sobrien
115898944Sobrien    case CLI_COMMAND:
115998944Sobrien      /* A CLI command was read from the input stream */
116098944Sobrien      /* This will be removed as soon as we have a complete set of
116198944Sobrien         mi commands */
116298944Sobrien      /* echo the command on the console. */
116398944Sobrien      fprintf_unfiltered (gdb_stdlog, "%s\n", context->command);
1164130803Smarcel      mi_execute_cli_command (context->command, 0, NULL);
1165130803Smarcel
1166130803Smarcel      /* If we changed interpreters, DON'T print out anything. */
1167130803Smarcel      if (current_interp_named_p (INTERP_MI)
1168130803Smarcel	  || current_interp_named_p (INTERP_MI1)
1169130803Smarcel	  || current_interp_named_p (INTERP_MI2)
1170130803Smarcel	  || current_interp_named_p (INTERP_MI3))
1171130803Smarcel	{
1172130803Smarcel	  /* print the result */
1173130803Smarcel	  /* FIXME: Check for errors here. */
1174130803Smarcel	  fputs_unfiltered (context->token, raw_stdout);
1175130803Smarcel	  fputs_unfiltered ("^done", raw_stdout);
1176130803Smarcel	  mi_out_put (uiout, raw_stdout);
1177130803Smarcel	  mi_out_rewind (uiout);
1178130803Smarcel	  fputs_unfiltered ("\n", raw_stdout);
1179130803Smarcel	  args->action = EXECUTE_COMMAND_DISPLAY_PROMPT;
1180130803Smarcel	  args->rc = MI_CMD_DONE;
1181130803Smarcel	}
118298944Sobrien      break;
118398944Sobrien
118498944Sobrien    }
1185130803Smarcel
118698944Sobrien  return 1;
118798944Sobrien}
118898944Sobrien
118998944Sobrien
119098944Sobrienvoid
119198944Sobrienmi_execute_command (char *cmd, int from_tty)
119298944Sobrien{
119398944Sobrien  struct mi_parse *command;
1194130803Smarcel  struct captured_mi_execute_command_args args;
1195130803Smarcel  struct ui_out *saved_uiout = uiout;
1196130803Smarcel  int result;
119798944Sobrien
119898944Sobrien  /* This is to handle EOF (^D). We just quit gdb. */
119998944Sobrien  /* FIXME: we should call some API function here. */
120098944Sobrien  if (cmd == 0)
120198944Sobrien    quit_force (NULL, from_tty);
120298944Sobrien
120398944Sobrien  command = mi_parse (cmd);
120498944Sobrien
120598944Sobrien  if (command != NULL)
120698944Sobrien    {
1207130803Smarcel      /* FIXME: cagney/1999-11-04: Can this use of catch_exceptions either
120898944Sobrien         be pushed even further down or even eliminated? */
1209130803Smarcel      args.command = command;
1210130803Smarcel      result = catch_exceptions (uiout, captured_mi_execute_command, &args, "",
1211130803Smarcel				 RETURN_MASK_ALL);
1212130803Smarcel
1213130803Smarcel      if (args.action == EXECUTE_COMMAND_SUPRESS_PROMPT)
121498944Sobrien	{
121598944Sobrien	  /* The command is executing synchronously.  Bail out early
121698944Sobrien	     suppressing the finished prompt. */
121798944Sobrien	  mi_parse_free (command);
121898944Sobrien	  return;
121998944Sobrien	}
1220130803Smarcel      if (args.action == EXECUTE_COMMAND_DISPLAY_ERROR || result < 0)
122198944Sobrien	{
122298944Sobrien	  char *msg = error_last_message ();
122398944Sobrien	  struct cleanup *cleanup = make_cleanup (xfree, msg);
122498944Sobrien	  /* The command execution failed and error() was called
122598944Sobrien	     somewhere */
122698944Sobrien	  fputs_unfiltered (command->token, raw_stdout);
122798944Sobrien	  fputs_unfiltered ("^error,msg=\"", raw_stdout);
122898944Sobrien	  fputstr_unfiltered (msg, '"', raw_stdout);
122998944Sobrien	  fputs_unfiltered ("\"\n", raw_stdout);
123098944Sobrien	}
123198944Sobrien      mi_parse_free (command);
123298944Sobrien    }
123398944Sobrien
123498944Sobrien  fputs_unfiltered ("(gdb) \n", raw_stdout);
123598944Sobrien  gdb_flush (raw_stdout);
123698944Sobrien  /* print any buffered hook code */
123798944Sobrien  /* ..... */
123898944Sobrien}
123998944Sobrien
124098944Sobrienstatic enum mi_cmd_result
124198944Sobrienmi_cmd_execute (struct mi_parse *parse)
124298944Sobrien{
124398944Sobrien  if (parse->cmd->argv_func != NULL
124498944Sobrien      || parse->cmd->args_func != NULL)
124598944Sobrien    {
124698944Sobrien      /* FIXME: We need to save the token because the command executed
124798944Sobrien         may be asynchronous and need to print the token again.
124898944Sobrien         In the future we can pass the token down to the func
124998944Sobrien         and get rid of the last_async_command */
125098944Sobrien      /* The problem here is to keep the token around when we launch
125198944Sobrien         the target, and we want to interrupt it later on.  The
125298944Sobrien         interrupt command will have its own token, but when the
125398944Sobrien         target stops, we must display the token corresponding to the
125498944Sobrien         last execution command given. So we have another string where
125598944Sobrien         we copy the token (previous_async_command), if this was
125698944Sobrien         indeed the token of an execution command, and when we stop we
125798944Sobrien         print that one. This is possible because the interrupt
125898944Sobrien         command, when over, will copy that token back into the
125998944Sobrien         default token string (last_async_command). */
126098944Sobrien
126198944Sobrien      if (target_executing)
126298944Sobrien	{
126398944Sobrien	  if (!previous_async_command)
126498944Sobrien	    previous_async_command = xstrdup (last_async_command);
126598944Sobrien	  if (strcmp (parse->command, "exec-interrupt"))
126698944Sobrien	    {
126798944Sobrien	      fputs_unfiltered (parse->token, raw_stdout);
126898944Sobrien	      fputs_unfiltered ("^error,msg=\"", raw_stdout);
126998944Sobrien	      fputs_unfiltered ("Cannot execute command ", raw_stdout);
127098944Sobrien	      fputstr_unfiltered (parse->command, '"', raw_stdout);
127198944Sobrien	      fputs_unfiltered (" while target running", raw_stdout);
127298944Sobrien	      fputs_unfiltered ("\"\n", raw_stdout);
127398944Sobrien	      return MI_CMD_ERROR;
127498944Sobrien	    }
127598944Sobrien	}
127698944Sobrien      last_async_command = xstrdup (parse->token);
127798944Sobrien      make_exec_cleanup (free_current_contents, &last_async_command);
127898944Sobrien      /* FIXME: DELETE THIS! */
127998944Sobrien      if (parse->cmd->args_func != NULL)
128098944Sobrien	return parse->cmd->args_func (parse->args, 0 /*from_tty */ );
128198944Sobrien      return parse->cmd->argv_func (parse->command, parse->argv, parse->argc);
128298944Sobrien    }
1283130803Smarcel  else if (parse->cmd->cli.cmd != 0)
128498944Sobrien    {
128598944Sobrien      /* FIXME: DELETE THIS. */
128698944Sobrien      /* The operation is still implemented by a cli command */
128798944Sobrien      /* Must be a synchronous one */
1288130803Smarcel      mi_execute_cli_command (parse->cmd->cli.cmd, parse->cmd->cli.args_p,
1289130803Smarcel			      parse->args);
129098944Sobrien      return MI_CMD_DONE;
129198944Sobrien    }
129298944Sobrien  else
129398944Sobrien    {
129498944Sobrien      /* FIXME: DELETE THIS. */
129598944Sobrien      fputs_unfiltered (parse->token, raw_stdout);
129698944Sobrien      fputs_unfiltered ("^error,msg=\"", raw_stdout);
129798944Sobrien      fputs_unfiltered ("Undefined mi command: ", raw_stdout);
129898944Sobrien      fputstr_unfiltered (parse->command, '"', raw_stdout);
129998944Sobrien      fputs_unfiltered (" (missing implementation)", raw_stdout);
130098944Sobrien      fputs_unfiltered ("\"\n", raw_stdout);
130198944Sobrien      return MI_CMD_ERROR;
130298944Sobrien    }
130398944Sobrien}
130498944Sobrien
130598944Sobrien/* FIXME: This is just a hack so we can get some extra commands going.
130698944Sobrien   We don't want to channel things through the CLI, but call libgdb directly */
130798944Sobrien/* Use only for synchronous commands */
130898944Sobrien
130998944Sobrienvoid
1310130803Smarcelmi_execute_cli_command (const char *cmd, int args_p, const char *args)
131198944Sobrien{
1312130803Smarcel  if (cmd != 0)
131398944Sobrien    {
131498944Sobrien      struct cleanup *old_cleanups;
131598944Sobrien      char *run;
1316130803Smarcel      if (args_p)
1317130803Smarcel	xasprintf (&run, "%s %s", cmd, args);
1318130803Smarcel      else
1319130803Smarcel	run = xstrdup (cmd);
132098944Sobrien      if (mi_debug_p)
132198944Sobrien	/* FIXME: gdb_???? */
132298944Sobrien	fprintf_unfiltered (gdb_stdout, "cli=%s run=%s\n",
1323130803Smarcel			    cmd, run);
132498944Sobrien      old_cleanups = make_cleanup (xfree, run);
132598944Sobrien      execute_command ( /*ui */ run, 0 /*from_tty */ );
132698944Sobrien      do_cleanups (old_cleanups);
132798944Sobrien      return;
132898944Sobrien    }
132998944Sobrien}
133098944Sobrien
133198944Sobrienenum mi_cmd_result
133298944Sobrienmi_execute_async_cli_command (char *mi, char *args, int from_tty)
133398944Sobrien{
133498944Sobrien  struct cleanup *old_cleanups;
133598944Sobrien  char *run;
133698944Sobrien  char *async_args;
133798944Sobrien
133898944Sobrien  if (target_can_async_p ())
133998944Sobrien    {
134098944Sobrien      async_args = (char *) xmalloc (strlen (args) + 2);
134198944Sobrien      make_exec_cleanup (free, async_args);
134298944Sobrien      strcpy (async_args, args);
134398944Sobrien      strcat (async_args, "&");
134498944Sobrien      xasprintf (&run, "%s %s", mi, async_args);
134598944Sobrien      make_exec_cleanup (free, run);
134698944Sobrien      add_continuation (mi_exec_async_cli_cmd_continuation, NULL);
134798944Sobrien      old_cleanups = NULL;
134898944Sobrien    }
134998944Sobrien  else
135098944Sobrien    {
135198944Sobrien      xasprintf (&run, "%s %s", mi, args);
135298944Sobrien      old_cleanups = make_cleanup (xfree, run);
135398944Sobrien    }
135498944Sobrien
135598944Sobrien  if (!target_can_async_p ())
135698944Sobrien    {
135798944Sobrien      /* NOTE: For synchronous targets asynchronous behavour is faked by
135898944Sobrien         printing out the GDB prompt before we even try to execute the
135998944Sobrien         command. */
136098944Sobrien      if (last_async_command)
136198944Sobrien	fputs_unfiltered (last_async_command, raw_stdout);
136298944Sobrien      fputs_unfiltered ("^running\n", raw_stdout);
136398944Sobrien      fputs_unfiltered ("(gdb) \n", raw_stdout);
136498944Sobrien      gdb_flush (raw_stdout);
136598944Sobrien    }
136698944Sobrien  else
136798944Sobrien    {
136898944Sobrien      /* FIXME: cagney/1999-11-29: Printing this message before
136998944Sobrien         calling execute_command is wrong.  It should only be printed
137098944Sobrien         once gdb has confirmed that it really has managed to send a
137198944Sobrien         run command to the target. */
137298944Sobrien      if (last_async_command)
137398944Sobrien	fputs_unfiltered (last_async_command, raw_stdout);
137498944Sobrien      fputs_unfiltered ("^running\n", raw_stdout);
137598944Sobrien    }
137698944Sobrien
137798944Sobrien  execute_command ( /*ui */ run, 0 /*from_tty */ );
137898944Sobrien
137998944Sobrien  if (!target_can_async_p ())
138098944Sobrien    {
138198944Sobrien      /* Do this before doing any printing.  It would appear that some
138298944Sobrien         print code leaves garbage around in the buffer. */
138398944Sobrien      do_cleanups (old_cleanups);
138498944Sobrien      /* If the target was doing the operation synchronously we fake
138598944Sobrien         the stopped message. */
138698944Sobrien      if (last_async_command)
138798944Sobrien	fputs_unfiltered (last_async_command, raw_stdout);
138898944Sobrien      fputs_unfiltered ("*stopped", raw_stdout);
138998944Sobrien      mi_out_put (uiout, raw_stdout);
139098944Sobrien      mi_out_rewind (uiout);
139198944Sobrien      fputs_unfiltered ("\n", raw_stdout);
139298944Sobrien      return MI_CMD_QUIET;
139398944Sobrien    }
139498944Sobrien  return MI_CMD_DONE;
139598944Sobrien}
139698944Sobrien
139798944Sobrienvoid
139898944Sobrienmi_exec_async_cli_cmd_continuation (struct continuation_arg *arg)
139998944Sobrien{
140098944Sobrien  if (last_async_command)
140198944Sobrien    fputs_unfiltered (last_async_command, raw_stdout);
140298944Sobrien  fputs_unfiltered ("*stopped", raw_stdout);
140398944Sobrien  mi_out_put (uiout, raw_stdout);
140498944Sobrien  fputs_unfiltered ("\n", raw_stdout);
140598944Sobrien  fputs_unfiltered ("(gdb) \n", raw_stdout);
140698944Sobrien  gdb_flush (raw_stdout);
140798944Sobrien  do_exec_cleanups (ALL_CLEANUPS);
140898944Sobrien}
140998944Sobrien
1410130803Smarcelvoid
141198944Sobrienmi_load_progress (const char *section_name,
141298944Sobrien		  unsigned long sent_so_far,
141398944Sobrien		  unsigned long total_section,
141498944Sobrien		  unsigned long total_sent,
141598944Sobrien		  unsigned long grand_total)
141698944Sobrien{
141798944Sobrien  struct timeval time_now, delta, update_threshold;
141898944Sobrien  static struct timeval last_update;
141998944Sobrien  static char *previous_sect_name = NULL;
142098944Sobrien  int new_section;
142198944Sobrien
1422130803Smarcel  if (!current_interp_named_p (INTERP_MI)
1423130803Smarcel      && !current_interp_named_p (INTERP_MI1))
142498944Sobrien    return;
142598944Sobrien
142698944Sobrien  update_threshold.tv_sec = 0;
142798944Sobrien  update_threshold.tv_usec = 500000;
142898944Sobrien  gettimeofday (&time_now, NULL);
142998944Sobrien
143098944Sobrien  delta.tv_usec = time_now.tv_usec - last_update.tv_usec;
143198944Sobrien  delta.tv_sec = time_now.tv_sec - last_update.tv_sec;
143298944Sobrien
143398944Sobrien  if (delta.tv_usec < 0)
143498944Sobrien    {
143598944Sobrien      delta.tv_sec -= 1;
143698944Sobrien      delta.tv_usec += 1000000;
143798944Sobrien    }
143898944Sobrien
143998944Sobrien  new_section = (previous_sect_name ?
144098944Sobrien		 strcmp (previous_sect_name, section_name) : 1);
144198944Sobrien  if (new_section)
144298944Sobrien    {
1443130803Smarcel      struct cleanup *cleanup_tuple;
144498944Sobrien      xfree (previous_sect_name);
144598944Sobrien      previous_sect_name = xstrdup (section_name);
144698944Sobrien
144798944Sobrien      if (last_async_command)
144898944Sobrien	fputs_unfiltered (last_async_command, raw_stdout);
144998944Sobrien      fputs_unfiltered ("+download", raw_stdout);
1450130803Smarcel      cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
145198944Sobrien      ui_out_field_string (uiout, "section", section_name);
145298944Sobrien      ui_out_field_int (uiout, "section-size", total_section);
145398944Sobrien      ui_out_field_int (uiout, "total-size", grand_total);
1454130803Smarcel      do_cleanups (cleanup_tuple);
145598944Sobrien      mi_out_put (uiout, raw_stdout);
145698944Sobrien      fputs_unfiltered ("\n", raw_stdout);
145798944Sobrien      gdb_flush (raw_stdout);
145898944Sobrien    }
145998944Sobrien
146098944Sobrien  if (delta.tv_sec >= update_threshold.tv_sec &&
146198944Sobrien      delta.tv_usec >= update_threshold.tv_usec)
146298944Sobrien    {
1463130803Smarcel      struct cleanup *cleanup_tuple;
146498944Sobrien      last_update.tv_sec = time_now.tv_sec;
146598944Sobrien      last_update.tv_usec = time_now.tv_usec;
146698944Sobrien      if (last_async_command)
146798944Sobrien	fputs_unfiltered (last_async_command, raw_stdout);
146898944Sobrien      fputs_unfiltered ("+download", raw_stdout);
1469130803Smarcel      cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
147098944Sobrien      ui_out_field_string (uiout, "section", section_name);
147198944Sobrien      ui_out_field_int (uiout, "section-sent", sent_so_far);
147298944Sobrien      ui_out_field_int (uiout, "section-size", total_section);
147398944Sobrien      ui_out_field_int (uiout, "total-sent", total_sent);
147498944Sobrien      ui_out_field_int (uiout, "total-size", grand_total);
1475130803Smarcel      do_cleanups (cleanup_tuple);
147698944Sobrien      mi_out_put (uiout, raw_stdout);
147798944Sobrien      fputs_unfiltered ("\n", raw_stdout);
147898944Sobrien      gdb_flush (raw_stdout);
147998944Sobrien    }
148098944Sobrien}
148198944Sobrien
1482130803Smarcelvoid
1483130803Smarcelmi_setup_architecture_data (void)
148498944Sobrien{
1485130803Smarcel  old_regs = xmalloc ((NUM_REGS + NUM_PSEUDO_REGS) * MAX_REGISTER_SIZE + 1);
1486130803Smarcel  memset (old_regs, 0, (NUM_REGS + NUM_PSEUDO_REGS) * MAX_REGISTER_SIZE + 1);
148798944Sobrien}
148898944Sobrien
148998944Sobrienvoid
149098944Sobrien_initialize_mi_main (void)
149198944Sobrien{
1492130803Smarcel  DEPRECATED_REGISTER_GDBARCH_SWAP (old_regs);
1493130803Smarcel  deprecated_register_gdbarch_swap (NULL, 0, mi_setup_architecture_data);
149498944Sobrien}
1495