OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [gdb-7.1/] [gdb/] [mi/] [mi-main.c] - Blame information for rev 834

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 227 jeremybenn
/* MI Command Set.
2
 
3
   Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
4
   Free Software Foundation, Inc.
5
 
6
   Contributed by Cygnus Solutions (a Red Hat company).
7
 
8
   This file is part of GDB.
9
 
10
   This program is free software; you can redistribute it and/or modify
11
   it under the terms of the GNU General Public License as published by
12
   the Free Software Foundation; either version 3 of the License, or
13
   (at your option) any later version.
14
 
15
   This program is distributed in the hope that it will be useful,
16
   but WITHOUT ANY WARRANTY; without even the implied warranty of
17
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
   GNU General Public License for more details.
19
 
20
   You should have received a copy of the GNU General Public License
21
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
22
 
23
/* Work in progress.  */
24
 
25
#include "defs.h"
26
#include "arch-utils.h"
27
#include "target.h"
28
#include "inferior.h"
29
#include "gdb_string.h"
30
#include "exceptions.h"
31
#include "top.h"
32
#include "gdbthread.h"
33
#include "mi-cmds.h"
34
#include "mi-parse.h"
35
#include "mi-getopt.h"
36
#include "mi-console.h"
37
#include "ui-out.h"
38
#include "mi-out.h"
39
#include "interps.h"
40
#include "event-loop.h"
41
#include "event-top.h"
42
#include "gdbcore.h"            /* For write_memory().  */
43
#include "value.h"
44
#include "regcache.h"
45
#include "gdb.h"
46
#include "frame.h"
47
#include "mi-main.h"
48
#include "mi-common.h"
49
#include "language.h"
50
#include "valprint.h"
51
#include "inferior.h"
52
#include "osdata.h"
53
#include "splay-tree.h"
54
 
55
#include <ctype.h>
56
#include <sys/time.h>
57
 
58
#if defined HAVE_SYS_RESOURCE_H
59
#include <sys/resource.h>
60
#endif
61
 
62
#ifdef HAVE_GETRUSAGE
63
struct rusage rusage;
64
#endif
65
 
66
enum
67
  {
68
    FROM_TTY = 0
69
  };
70
 
71
int mi_debug_p;
72
struct ui_file *raw_stdout;
73
 
74
/* This is used to pass the current command timestamp
75
   down to continuation routines.  */
76
static struct mi_timestamp *current_command_ts;
77
 
78
static int do_timings = 0;
79
 
80
char *current_token;
81
int running_result_record_printed = 1;
82
 
83
/* Flag indicating that the target has proceeded since the last
84
   command was issued.  */
85
int mi_proceeded;
86
 
87
extern void _initialize_mi_main (void);
88
static void mi_cmd_execute (struct mi_parse *parse);
89
 
90
static void mi_execute_cli_command (const char *cmd, int args_p,
91
                                    const char *args);
92
static void mi_execute_async_cli_command (char *cli_command,
93
                                                        char **argv, int argc);
94
static int register_changed_p (int regnum, struct regcache *,
95
                               struct regcache *);
96
static void get_register (struct frame_info *, int regnum, int format);
97
 
98
/* Command implementations.  FIXME: Is this libgdb?  No.  This is the MI
99
   layer that calls libgdb.  Any operation used in the below should be
100
   formalized.  */
101
 
102
static void timestamp (struct mi_timestamp *tv);
103
 
104
static void print_diff_now (struct mi_timestamp *start);
105
static void print_diff (struct mi_timestamp *start, struct mi_timestamp *end);
106
 
107
void
108
mi_cmd_gdb_exit (char *command, char **argv, int argc)
109
{
110
  /* We have to print everything right here because we never return.  */
111
  if (current_token)
112
    fputs_unfiltered (current_token, raw_stdout);
113
  fputs_unfiltered ("^exit\n", raw_stdout);
114
  mi_out_put (uiout, raw_stdout);
115
  gdb_flush (raw_stdout);
116
  /* FIXME: The function called is not yet a formal libgdb function.  */
117
  quit_force (NULL, FROM_TTY);
118
}
119
 
120
void
121
mi_cmd_exec_next (char *command, char **argv, int argc)
122
{
123
  /* FIXME: Should call a libgdb function, not a cli wrapper.  */
124
  if (argc > 0 && strcmp(argv[0], "--reverse") == 0)
125
    mi_execute_async_cli_command ("reverse-next", argv + 1, argc - 1);
126
  else
127
    mi_execute_async_cli_command ("next", argv, argc);
128
}
129
 
130
void
131
mi_cmd_exec_next_instruction (char *command, char **argv, int argc)
132
{
133
  /* FIXME: Should call a libgdb function, not a cli wrapper.  */
134
  if (argc > 0 && strcmp(argv[0], "--reverse") == 0)
135
    mi_execute_async_cli_command ("reverse-nexti", argv + 1, argc - 1);
136
  else
137
    mi_execute_async_cli_command ("nexti", argv, argc);
138
}
139
 
140
void
141
mi_cmd_exec_step (char *command, char **argv, int argc)
142
{
143
  /* FIXME: Should call a libgdb function, not a cli wrapper.  */
144
  if (argc > 0 && strcmp(argv[0], "--reverse") == 0)
145
    mi_execute_async_cli_command ("reverse-step", argv + 1, argc - 1);
146
  else
147
    mi_execute_async_cli_command ("step", argv, argc);
148
}
149
 
150
void
151
mi_cmd_exec_step_instruction (char *command, char **argv, int argc)
152
{
153
  /* FIXME: Should call a libgdb function, not a cli wrapper.  */
154
  if (argc > 0 && strcmp(argv[0], "--reverse") == 0)
155
    mi_execute_async_cli_command ("reverse-stepi", argv + 1, argc - 1);
156
  else
157
    mi_execute_async_cli_command ("stepi", argv, argc);
158
}
159
 
160
void
161
mi_cmd_exec_finish (char *command, char **argv, int argc)
162
{
163
  /* FIXME: Should call a libgdb function, not a cli wrapper.  */
164
  if (argc > 0 && strcmp(argv[0], "--reverse") == 0)
165
    mi_execute_async_cli_command ("reverse-finish", argv + 1, argc - 1);
166
  else
167
    mi_execute_async_cli_command ("finish", argv, argc);
168
}
169
 
170
void
171
mi_cmd_exec_return (char *command, char **argv, int argc)
172
{
173
  /* This command doesn't really execute the target, it just pops the
174
     specified number of frames. */
175
  if (argc)
176
    /* Call return_command with from_tty argument equal to 0 so as to
177
       avoid being queried.  */
178
    return_command (*argv, 0);
179
  else
180
    /* Call return_command with from_tty argument equal to 0 so as to
181
       avoid being queried.  */
182
    return_command (NULL, 0);
183
 
184
  /* Because we have called return_command with from_tty = 0, we need
185
     to print the frame here.  */
186
  print_stack_frame (get_selected_frame (NULL), 1, LOC_AND_ADDRESS);
187
}
188
 
189
void
190
mi_cmd_exec_jump (char *args, char **argv, int argc)
191
{
192
  /* FIXME: Should call a libgdb function, not a cli wrapper.  */
193
  mi_execute_async_cli_command ("jump", argv, argc);
194
}
195
 
196
static int
197
proceed_thread_callback (struct thread_info *thread, void *arg)
198
{
199
  int pid = *(int *)arg;
200
 
201
  if (!is_stopped (thread->ptid))
202
    return 0;
203
 
204
  if (PIDGET (thread->ptid) != pid)
205
    return 0;
206
 
207
  switch_to_thread (thread->ptid);
208
  clear_proceed_status ();
209
  proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 0);
210
  return 0;
211
}
212
 
213
static void
214
exec_continue (char **argv, int argc)
215
{
216
  if (argc == 0)
217
    continue_1 (0);
218
  else if (argc == 1 && strcmp (argv[0], "--all") == 0)
219
    continue_1 (1);
220
  else if (argc == 2 && strcmp (argv[0], "--thread-group") == 0)
221
    {
222
      struct cleanup *old_chain;
223
      int pid;
224
      if (argv[1] == NULL || argv[1] == '\0')
225
        error ("Thread group id not specified");
226
      pid = atoi (argv[1]);
227
      if (!in_inferior_list (pid))
228
        error ("Invalid thread group id '%s'", argv[1]);
229
 
230
      old_chain = make_cleanup_restore_current_thread ();
231
      iterate_over_threads (proceed_thread_callback, &pid);
232
      do_cleanups (old_chain);
233
    }
234
  else
235
    error ("Usage: -exec-continue [--reverse] [--all|--thread-group id]");
236
}
237
 
238
/* continue in reverse direction:
239
   XXX: code duplicated from reverse.c */
240
 
241
static void
242
exec_direction_default (void *notused)
243
{
244
  /* Return execution direction to default state.  */
245
  execution_direction = EXEC_FORWARD;
246
}
247
 
248
static void
249
exec_reverse_continue (char **argv, int argc)
250
{
251
  enum exec_direction_kind dir = execution_direction;
252
  struct cleanup *old_chain;
253
 
254
  if (dir == EXEC_ERROR)
255
    error (_("Target %s does not support this command."), target_shortname);
256
 
257
  if (dir == EXEC_REVERSE)
258
    error (_("Already in reverse mode."));
259
 
260
  if (!target_can_execute_reverse)
261
    error (_("Target %s does not support this command."), target_shortname);
262
 
263
  old_chain = make_cleanup (exec_direction_default, NULL);
264
  execution_direction = EXEC_REVERSE;
265
  exec_continue (argv, argc);
266
  do_cleanups (old_chain);
267
}
268
 
269
void
270
mi_cmd_exec_continue (char *command, char **argv, int argc)
271
{
272
  if (argc > 0 && strcmp(argv[0], "--reverse") == 0)
273
    exec_reverse_continue (argv + 1, argc - 1);
274
  else
275
    exec_continue (argv, argc);
276
}
277
 
278
static int
279
interrupt_thread_callback (struct thread_info *thread, void *arg)
280
{
281
  int pid = *(int *)arg;
282
 
283
  if (!is_running (thread->ptid))
284
    return 0;
285
 
286
  if (PIDGET (thread->ptid) != pid)
287
    return 0;
288
 
289
  target_stop (thread->ptid);
290
  return 0;
291
}
292
 
293
/* Interrupt the execution of the target.  Note how we must play around
294
   with the token variables, in order to display the current token in
295
   the result of the interrupt command, and the previous execution
296
   token when the target finally stops.  See comments in
297
   mi_cmd_execute.  */
298
void
299
mi_cmd_exec_interrupt (char *command, char **argv, int argc)
300
{
301
  if (argc == 0)
302
    {
303
      if (!is_running (inferior_ptid))
304
        error ("Current thread is not running.");
305
 
306
      interrupt_target_1 (0);
307
    }
308
  else if (argc == 1 && strcmp (argv[0], "--all") == 0)
309
    {
310
      if (!any_running ())
311
        error ("Inferior not running.");
312
 
313
      interrupt_target_1 (1);
314
    }
315
  else if (argc == 2 && strcmp (argv[0], "--thread-group") == 0)
316
    {
317
      struct cleanup *old_chain;
318
      int pid;
319
      if (argv[1] == NULL || argv[1] == '\0')
320
        error ("Thread group id not specified");
321
      pid = atoi (argv[1]);
322
      if (!in_inferior_list (pid))
323
        error ("Invalid thread group id '%s'", argv[1]);
324
 
325
      old_chain = make_cleanup_restore_current_thread ();
326
      iterate_over_threads (interrupt_thread_callback, &pid);
327
      do_cleanups (old_chain);
328
    }
329
  else
330
    error ("Usage: -exec-interrupt [--all|--thread-group id]");
331
}
332
 
333
static int
334
find_thread_of_process (struct thread_info *ti, void *p)
335
{
336
  int pid = *(int *)p;
337
  if (PIDGET (ti->ptid) == pid && !is_exited (ti->ptid))
338
    return 1;
339
 
340
  return 0;
341
}
342
 
343
void
344
mi_cmd_target_detach (char *command, char **argv, int argc)
345
{
346
  if (argc != 0 && argc != 1)
347
    error ("Usage: -target-detach [thread-group]");
348
 
349
  if (argc == 1)
350
    {
351
      struct thread_info *tp;
352
      char *end = argv[0];
353
      int pid = strtol (argv[0], &end, 10);
354
      if (*end != '\0')
355
        error (_("Cannot parse thread group id '%s'"), argv[0]);
356
 
357
      /* Pick any thread in the desired process.  Current
358
         target_detach deteches from the parent of inferior_ptid.  */
359
      tp = iterate_over_threads (find_thread_of_process, &pid);
360
      if (!tp)
361
        error (_("Thread group is empty"));
362
 
363
      switch_to_thread (tp->ptid);
364
    }
365
 
366
  detach_command (NULL, 0);
367
}
368
 
369
void
370
mi_cmd_thread_select (char *command, char **argv, int argc)
371
{
372
  enum gdb_rc rc;
373
  char *mi_error_message;
374
 
375
  if (argc != 1)
376
    error ("mi_cmd_thread_select: USAGE: threadnum.");
377
 
378
  rc = gdb_thread_select (uiout, argv[0], &mi_error_message);
379
 
380
  if (rc == GDB_RC_FAIL)
381
    {
382
      make_cleanup (xfree, mi_error_message);
383
      error ("%s", mi_error_message);
384
    }
385
}
386
 
387
void
388
mi_cmd_thread_list_ids (char *command, char **argv, int argc)
389
{
390
  enum gdb_rc rc;
391
  char *mi_error_message;
392
 
393
  if (argc != 0)
394
    error ("mi_cmd_thread_list_ids: No arguments required.");
395
 
396
  rc = gdb_list_thread_ids (uiout, &mi_error_message);
397
 
398
  if (rc == GDB_RC_FAIL)
399
    {
400
      make_cleanup (xfree, mi_error_message);
401
      error ("%s", mi_error_message);
402
    }
403
}
404
 
405
void
406
mi_cmd_thread_info (char *command, char **argv, int argc)
407
{
408
  int thread = -1;
409
 
410
  if (argc != 0 && argc != 1)
411
    error ("Invalid MI command");
412
 
413
  if (argc == 1)
414
    thread = atoi (argv[0]);
415
 
416
  print_thread_info (uiout, thread, -1);
417
}
418
 
419
struct collect_cores_data
420
{
421
  int pid;
422
 
423
  VEC (int) *cores;
424
};
425
 
426
static int
427
collect_cores (struct thread_info *ti, void *xdata)
428
{
429
  struct collect_cores_data *data = xdata;
430
 
431
  if (ptid_get_pid (ti->ptid) == data->pid)
432
    {
433
      int core = target_core_of_thread (ti->ptid);
434
      if (core != -1)
435
        VEC_safe_push (int, data->cores, core);
436
    }
437
 
438
  return 0;
439
}
440
 
441
static int *
442
unique (int *b, int *e)
443
{
444
  int *d = b;
445
  while (++b != e)
446
    if (*d != *b)
447
      *++d = *b;
448
  return ++d;
449
}
450
 
451
struct print_one_inferior_data
452
{
453
  int recurse;
454
  VEC (int) *inferiors;
455
};
456
 
457
static int
458
print_one_inferior (struct inferior *inferior, void *xdata)
459
{
460
  struct print_one_inferior_data *top_data = xdata;
461
 
462
  if (VEC_empty (int, top_data->inferiors)
463
      || bsearch (&(inferior->pid), VEC_address (int, top_data->inferiors),
464
                  VEC_length (int, top_data->inferiors), sizeof (int),
465
                  compare_positive_ints))
466
    {
467
      struct collect_cores_data data;
468
      struct cleanup *back_to
469
        = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
470
 
471
      ui_out_field_fmt (uiout, "id", "%d", inferior->pid);
472
      ui_out_field_string (uiout, "type", "process");
473
      ui_out_field_int (uiout, "pid", inferior->pid);
474
 
475
      data.pid = inferior->pid;
476
      data.cores = 0;
477
      iterate_over_threads (collect_cores, &data);
478
 
479
      if (!VEC_empty (int, data.cores))
480
        {
481
          int elt;
482
          int i;
483
          int *b, *e;
484
          struct cleanup *back_to_2 =
485
            make_cleanup_ui_out_list_begin_end (uiout, "cores");
486
 
487
          qsort (VEC_address (int, data.cores),
488
                 VEC_length (int, data.cores), sizeof (int),
489
                 compare_positive_ints);
490
 
491
          b = VEC_address (int, data.cores);
492
          e = b + VEC_length (int, data.cores);
493
          e = unique (b, e);
494
 
495
          for (; b != e; ++b)
496
            ui_out_field_int (uiout, NULL, *b);
497
 
498
          do_cleanups (back_to_2);
499
        }
500
 
501
      if (top_data->recurse)
502
        print_thread_info (uiout, -1, inferior->pid);
503
 
504
      do_cleanups (back_to);
505
    }
506
 
507
  return 0;
508
}
509
 
510
/* Output a field named 'cores' with a list as the value.  The elements of
511
   the list are obtained by splitting 'cores' on comma.  */
512
 
513
static void
514
output_cores (struct ui_out *uiout, const char *field_name, const char *xcores)
515
{
516
  struct cleanup *back_to = make_cleanup_ui_out_list_begin_end (uiout,
517
                                                                field_name);
518
  char *cores = xstrdup (xcores);
519
  char *p = cores;
520
 
521
  make_cleanup (xfree, cores);
522
 
523
  for (p = strtok (p, ","); p;  p = strtok (NULL, ","))
524
    ui_out_field_string (uiout, NULL, p);
525
 
526
  do_cleanups (back_to);
527
}
528
 
529
static void
530
free_vector_of_ints (void *xvector)
531
{
532
  VEC (int) **vector = xvector;
533
  VEC_free (int, *vector);
534
}
535
 
536
static void
537
do_nothing (splay_tree_key k)
538
{
539
}
540
 
541
static void
542
free_vector_of_osdata_items (splay_tree_value xvalue)
543
{
544
  VEC (osdata_item_s) *value = (VEC (osdata_item_s) *) xvalue;
545
  /* We don't free the items itself, it will be done separately.  */
546
  VEC_free (osdata_item_s, value);
547
}
548
 
549
static int
550
splay_tree_int_comparator (splay_tree_key xa, splay_tree_key xb)
551
{
552
  int a = xa;
553
  int b = xb;
554
  return a - b;
555
}
556
 
557
static void
558
free_splay_tree (void *xt)
559
{
560
  splay_tree t = xt;
561
  splay_tree_delete (t);
562
}
563
 
564
static void
565
list_available_thread_groups (VEC (int) *ids, int recurse)
566
{
567
  struct osdata *data;
568
  struct osdata_item *item;
569
  int ix_items;
570
  /* This keeps a map from integer (pid) to VEC (struct osdata_item *)*
571
     The vector contains information about all threads for the given pid.
572
     This is assigned an initial value to avoid "may be used uninitialized"
573
     warning from gcc.  */
574
  splay_tree tree = NULL;
575
 
576
  /* get_osdata will throw if it cannot return data.  */
577
  data = get_osdata ("processes");
578
  make_cleanup_osdata_free (data);
579
 
580
  if (recurse)
581
    {
582
      struct osdata *threads = get_osdata ("threads");
583
      make_cleanup_osdata_free (threads);
584
 
585
      tree = splay_tree_new (splay_tree_int_comparator,
586
                             do_nothing,
587
                             free_vector_of_osdata_items);
588
      make_cleanup (free_splay_tree, tree);
589
 
590
      for (ix_items = 0;
591
           VEC_iterate (osdata_item_s, threads->items,
592
                        ix_items, item);
593
           ix_items++)
594
        {
595
          const char *pid = get_osdata_column (item, "pid");
596
          int pid_i = strtoul (pid, NULL, 0);
597
          VEC (osdata_item_s) *vec = 0;
598
 
599
          splay_tree_node n = splay_tree_lookup (tree, pid_i);
600
          if (!n)
601
            {
602
              VEC_safe_push (osdata_item_s, vec, item);
603
              splay_tree_insert (tree, pid_i, (splay_tree_value)vec);
604
            }
605
          else
606
            {
607
              vec = (VEC (osdata_item_s) *) n->value;
608
              VEC_safe_push (osdata_item_s, vec, item);
609
              n->value = (splay_tree_value) vec;
610
            }
611
        }
612
    }
613
 
614
  make_cleanup_ui_out_list_begin_end (uiout, "groups");
615
 
616
  for (ix_items = 0;
617
       VEC_iterate (osdata_item_s, data->items,
618
                    ix_items, item);
619
       ix_items++)
620
    {
621
      struct cleanup *back_to;
622
 
623
      const char *pid = get_osdata_column (item, "pid");
624
      const char *cmd = get_osdata_column (item, "command");
625
      const char *user = get_osdata_column (item, "user");
626
      const char *cores = get_osdata_column (item, "cores");
627
 
628
      int pid_i = strtoul (pid, NULL, 0);
629
 
630
      /* At present, the target will return all available processes
631
         and if information about specific ones was required, we filter
632
         undesired processes here.  */
633
      if (ids && bsearch (&pid_i, VEC_address (int, ids),
634
                          VEC_length (int, ids),
635
                          sizeof (int), compare_positive_ints) == NULL)
636
        continue;
637
 
638
 
639
      back_to = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
640
 
641
      ui_out_field_fmt (uiout, "id", "%s", pid);
642
      ui_out_field_string (uiout, "type", "process");
643
      if (cmd)
644
        ui_out_field_string (uiout, "description", cmd);
645
      if (user)
646
        ui_out_field_string (uiout, "user", user);
647
      if (cores)
648
        output_cores (uiout, "cores", cores);
649
 
650
      if (recurse)
651
        {
652
          splay_tree_node n = splay_tree_lookup (tree, pid_i);
653
          if (n)
654
            {
655
              VEC (osdata_item_s) *children = (VEC (osdata_item_s) *) n->value;
656
              struct osdata_item *child;
657
              int ix_child;
658
 
659
              make_cleanup_ui_out_list_begin_end (uiout, "threads");
660
 
661
              for (ix_child = 0;
662
                   VEC_iterate (osdata_item_s, children, ix_child, child);
663
                   ++ix_child)
664
                {
665
                  struct cleanup *back_to_2 =
666
                    make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
667
 
668
                  const char *tid = get_osdata_column (child, "tid");
669
                  const char *tcore = get_osdata_column (child, "core");
670
                  ui_out_field_string (uiout, "id", tid);
671
                  if (tcore)
672
                    ui_out_field_string (uiout, "core", tcore);
673
 
674
                  do_cleanups (back_to_2);
675
                }
676
            }
677
        }
678
 
679
      do_cleanups (back_to);
680
    }
681
}
682
 
683
void
684
mi_cmd_list_thread_groups (char *command, char **argv, int argc)
685
{
686
  struct cleanup *back_to;
687
  int available = 0;
688
  int recurse = 0;
689
  VEC (int) *ids = 0;
690
 
691
  enum opt
692
    {
693
      AVAILABLE_OPT, RECURSE_OPT
694
    };
695
  static struct mi_opt opts[] =
696
  {
697
    {"-available", AVAILABLE_OPT, 0},
698
    {"-recurse", RECURSE_OPT, 1},
699
    { 0, 0, 0 }
700
  };
701
 
702
  int optind = 0;
703
  char *optarg;
704
 
705
  while (1)
706
    {
707
      int opt = mi_getopt ("-list-thread-groups", argc, argv, opts,
708
                           &optind, &optarg);
709
      if (opt < 0)
710
        break;
711
      switch ((enum opt) opt)
712
        {
713
        case AVAILABLE_OPT:
714
          available = 1;
715
          break;
716
        case RECURSE_OPT:
717
          if (strcmp (optarg, "0") == 0)
718
            ;
719
          else if (strcmp (optarg, "1") == 0)
720
            recurse = 1;
721
          else
722
            error ("only '0' and '1' are valid values for the '--recurse' option");
723
          break;
724
        }
725
    }
726
 
727
  for (; optind < argc; ++optind)
728
    {
729
      char *end;
730
      int inf = strtoul (argv[optind], &end, 0);
731
      if (*end != '\0')
732
        error ("invalid group id '%s'", argv[optind]);
733
      VEC_safe_push (int, ids, inf);
734
    }
735
  if (VEC_length (int, ids) > 1)
736
    qsort (VEC_address (int, ids),
737
           VEC_length (int, ids),
738
           sizeof (int), compare_positive_ints);
739
 
740
  back_to = make_cleanup (free_vector_of_ints, &ids);
741
 
742
  if (available)
743
    {
744
      list_available_thread_groups (ids, recurse);
745
    }
746
  else if (VEC_length (int, ids) == 1)
747
    {
748
      /* Local thread groups, single id. */
749
      int pid = *VEC_address (int, ids);
750
      if (!in_inferior_list (pid))
751
        error ("Invalid thread group id '%d'", pid);
752
      print_thread_info (uiout, -1, pid);
753
    }
754
  else
755
    {
756
      struct print_one_inferior_data data;
757
      data.recurse = recurse;
758
      data.inferiors = ids;
759
 
760
      /* Local thread groups.  Either no explicit ids -- and we
761
         print everything, or several explicit ids.  In both cases,
762
         we print more than one group, and have to use 'groups'
763
         as the top-level element.  */
764
      make_cleanup_ui_out_list_begin_end (uiout, "groups");
765
      update_thread_list ();
766
      iterate_over_inferiors (print_one_inferior, &data);
767
    }
768
 
769
  do_cleanups (back_to);
770
}
771
 
772
void
773
mi_cmd_data_list_register_names (char *command, char **argv, int argc)
774
{
775
  struct gdbarch *gdbarch;
776
  int regnum, numregs;
777
  int i;
778
  struct cleanup *cleanup;
779
 
780
  /* Note that the test for a valid register must include checking the
781
     gdbarch_register_name because gdbarch_num_regs may be allocated for
782
     the union of the register sets within a family of related processors.
783
     In this case, some entries of gdbarch_register_name will change depending
784
     upon the particular processor being debugged.  */
785
 
786
  gdbarch = get_current_arch ();
787
  numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
788
 
789
  cleanup = make_cleanup_ui_out_list_begin_end (uiout, "register-names");
790
 
791
  if (argc == 0)         /* No args, just do all the regs.  */
792
    {
793
      for (regnum = 0;
794
           regnum < numregs;
795
           regnum++)
796
        {
797
          if (gdbarch_register_name (gdbarch, regnum) == NULL
798
              || *(gdbarch_register_name (gdbarch, regnum)) == '\0')
799
            ui_out_field_string (uiout, NULL, "");
800
          else
801
            ui_out_field_string (uiout, NULL,
802
                                 gdbarch_register_name (gdbarch, regnum));
803
        }
804
    }
805
 
806
  /* Else, list of register #s, just do listed regs.  */
807
  for (i = 0; i < argc; i++)
808
    {
809
      regnum = atoi (argv[i]);
810
      if (regnum < 0 || regnum >= numregs)
811
        error ("bad register number");
812
 
813
      if (gdbarch_register_name (gdbarch, regnum) == NULL
814
          || *(gdbarch_register_name (gdbarch, regnum)) == '\0')
815
        ui_out_field_string (uiout, NULL, "");
816
      else
817
        ui_out_field_string (uiout, NULL,
818
                             gdbarch_register_name (gdbarch, regnum));
819
    }
820
  do_cleanups (cleanup);
821
}
822
 
823
void
824
mi_cmd_data_list_changed_registers (char *command, char **argv, int argc)
825
{
826
  static struct regcache *this_regs = NULL;
827
  struct regcache *prev_regs;
828
  struct gdbarch *gdbarch;
829
  int regnum, numregs, changed;
830
  int i;
831
  struct cleanup *cleanup;
832
 
833
  /* The last time we visited this function, the current frame's register
834
     contents were saved in THIS_REGS.  Move THIS_REGS over to PREV_REGS,
835
     and refresh THIS_REGS with the now-current register contents.  */
836
 
837
  prev_regs = this_regs;
838
  this_regs = frame_save_as_regcache (get_selected_frame (NULL));
839
  cleanup = make_cleanup_regcache_xfree (prev_regs);
840
 
841
  /* Note that the test for a valid register must include checking the
842
     gdbarch_register_name because gdbarch_num_regs may be allocated for
843
     the union of the register sets within a family of related processors.
844
     In this  case, some entries of gdbarch_register_name will change depending
845
     upon the particular processor being debugged.  */
846
 
847
  gdbarch = get_regcache_arch (this_regs);
848
  numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
849
 
850
  make_cleanup_ui_out_list_begin_end (uiout, "changed-registers");
851
 
852
  if (argc == 0)         /* No args, just do all the regs.  */
853
    {
854
      for (regnum = 0;
855
           regnum < numregs;
856
           regnum++)
857
        {
858
          if (gdbarch_register_name (gdbarch, regnum) == NULL
859
              || *(gdbarch_register_name (gdbarch, regnum)) == '\0')
860
            continue;
861
          changed = register_changed_p (regnum, prev_regs, this_regs);
862
          if (changed < 0)
863
            error ("mi_cmd_data_list_changed_registers: Unable to read register contents.");
864
          else if (changed)
865
            ui_out_field_int (uiout, NULL, regnum);
866
        }
867
    }
868
 
869
  /* Else, list of register #s, just do listed regs.  */
870
  for (i = 0; i < argc; i++)
871
    {
872
      regnum = atoi (argv[i]);
873
 
874
      if (regnum >= 0
875
          && regnum < numregs
876
          && gdbarch_register_name (gdbarch, regnum) != NULL
877
          && *gdbarch_register_name (gdbarch, regnum) != '\000')
878
        {
879
          changed = register_changed_p (regnum, prev_regs, this_regs);
880
          if (changed < 0)
881
            error ("mi_cmd_data_list_register_change: Unable to read register contents.");
882
          else if (changed)
883
            ui_out_field_int (uiout, NULL, regnum);
884
        }
885
      else
886
        error ("bad register number");
887
    }
888
  do_cleanups (cleanup);
889
}
890
 
891
static int
892
register_changed_p (int regnum, struct regcache *prev_regs,
893
                    struct regcache *this_regs)
894
{
895
  struct gdbarch *gdbarch = get_regcache_arch (this_regs);
896
  gdb_byte prev_buffer[MAX_REGISTER_SIZE];
897
  gdb_byte this_buffer[MAX_REGISTER_SIZE];
898
 
899
  /* Registers not valid in this frame return count as unchanged.  */
900
  if (!regcache_valid_p (this_regs, regnum))
901
    return 0;
902
 
903
  /* First time through or after gdbarch change consider all registers as
904
     changed.  Same for registers not valid in the previous frame.  */
905
  if (!prev_regs || get_regcache_arch (prev_regs) != gdbarch
906
      || !regcache_valid_p (prev_regs, regnum))
907
    return 1;
908
 
909
  /* Get register contents and compare.  */
910
  regcache_cooked_read (prev_regs, regnum, prev_buffer);
911
  regcache_cooked_read (this_regs, regnum, this_buffer);
912
 
913
  return memcmp (prev_buffer, this_buffer,
914
                 register_size (gdbarch, regnum)) != 0;
915
}
916
 
917
/* Return a list of register number and value pairs.  The valid
918
   arguments expected are: a letter indicating the format in which to
919
   display the registers contents.  This can be one of: x (hexadecimal), d
920
   (decimal), N (natural), t (binary), o (octal), r (raw).  After the
921
   format argumetn there can be a sequence of numbers, indicating which
922
   registers to fetch the content of.  If the format is the only argument,
923
   a list of all the registers with their values is returned.  */
924
void
925
mi_cmd_data_list_register_values (char *command, char **argv, int argc)
926
{
927
  struct frame_info *frame;
928
  struct gdbarch *gdbarch;
929
  int regnum, numregs, format;
930
  int i;
931
  struct cleanup *list_cleanup, *tuple_cleanup;
932
 
933
  /* Note that the test for a valid register must include checking the
934
     gdbarch_register_name because gdbarch_num_regs may be allocated for
935
     the union of the register sets within a family of related processors.
936
     In this case, some entries of gdbarch_register_name will change depending
937
     upon the particular processor being debugged.  */
938
 
939
  if (argc == 0)
940
    error ("mi_cmd_data_list_register_values: Usage: -data-list-register-values <format> [<regnum1>...<regnumN>]");
941
 
942
  format = (int) argv[0][0];
943
 
944
  frame = get_selected_frame (NULL);
945
  gdbarch = get_frame_arch (frame);
946
  numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
947
 
948
  list_cleanup = make_cleanup_ui_out_list_begin_end (uiout, "register-values");
949
 
950
  if (argc == 1)            /* No args, beside the format: do all the regs.  */
951
    {
952
      for (regnum = 0;
953
           regnum < numregs;
954
           regnum++)
955
        {
956
          if (gdbarch_register_name (gdbarch, regnum) == NULL
957
              || *(gdbarch_register_name (gdbarch, regnum)) == '\0')
958
            continue;
959
          tuple_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
960
          ui_out_field_int (uiout, "number", regnum);
961
          get_register (frame, regnum, format);
962
          do_cleanups (tuple_cleanup);
963
        }
964
    }
965
 
966
  /* Else, list of register #s, just do listed regs.  */
967
  for (i = 1; i < argc; i++)
968
    {
969
      regnum = atoi (argv[i]);
970
 
971
      if (regnum >= 0
972
          && regnum < numregs
973
          && gdbarch_register_name (gdbarch, regnum) != NULL
974
          && *gdbarch_register_name (gdbarch, regnum) != '\000')
975
        {
976
          tuple_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
977
          ui_out_field_int (uiout, "number", regnum);
978
          get_register (frame, regnum, format);
979
          do_cleanups (tuple_cleanup);
980
        }
981
      else
982
        error ("bad register number");
983
    }
984
  do_cleanups (list_cleanup);
985
}
986
 
987
/* Output one register's contents in the desired format.  */
988
static void
989
get_register (struct frame_info *frame, int regnum, int format)
990
{
991
  struct gdbarch *gdbarch = get_frame_arch (frame);
992
  gdb_byte buffer[MAX_REGISTER_SIZE];
993
  int optim;
994
  int realnum;
995
  CORE_ADDR addr;
996
  enum lval_type lval;
997
  static struct ui_stream *stb = NULL;
998
 
999
  stb = ui_out_stream_new (uiout);
1000
 
1001
  if (format == 'N')
1002
    format = 0;
1003
 
1004
  frame_register (frame, regnum, &optim, &lval, &addr, &realnum, buffer);
1005
 
1006
  if (optim)
1007
    error ("Optimized out");
1008
 
1009
  if (format == 'r')
1010
    {
1011
      int j;
1012
      char *ptr, buf[1024];
1013
 
1014
      strcpy (buf, "0x");
1015
      ptr = buf + 2;
1016
      for (j = 0; j < register_size (gdbarch, regnum); j++)
1017
        {
1018
          int idx = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG ?
1019
                    j : register_size (gdbarch, regnum) - 1 - j;
1020
          sprintf (ptr, "%02x", (unsigned char) buffer[idx]);
1021
          ptr += 2;
1022
        }
1023
      ui_out_field_string (uiout, "value", buf);
1024
      /*fputs_filtered (buf, gdb_stdout); */
1025
    }
1026
  else
1027
    {
1028
      struct value_print_options opts;
1029
      get_formatted_print_options (&opts, format);
1030
      opts.deref_ref = 1;
1031
      val_print (register_type (gdbarch, regnum), buffer, 0, 0,
1032
                 stb->stream, 0, &opts, current_language);
1033
      ui_out_field_stream (uiout, "value", stb);
1034
      ui_out_stream_delete (stb);
1035
    }
1036
}
1037
 
1038
/* Write given values into registers. The registers and values are
1039
   given as pairs.  The corresponding MI command is
1040
   -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]*/
1041
void
1042
mi_cmd_data_write_register_values (char *command, char **argv, int argc)
1043
{
1044
  struct regcache *regcache;
1045
  struct gdbarch *gdbarch;
1046
  int numregs, i;
1047
  char format;
1048
 
1049
  /* Note that the test for a valid register must include checking the
1050
     gdbarch_register_name because gdbarch_num_regs may be allocated for
1051
     the union of the register sets within a family of related processors.
1052
     In this case, some entries of gdbarch_register_name will change depending
1053
     upon the particular processor being debugged.  */
1054
 
1055
  regcache = get_current_regcache ();
1056
  gdbarch = get_regcache_arch (regcache);
1057
  numregs = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
1058
 
1059
  if (argc == 0)
1060
    error ("mi_cmd_data_write_register_values: Usage: -data-write-register-values <format> [<regnum1> <value1>...<regnumN> <valueN>]");
1061
 
1062
  format = (int) argv[0][0];
1063
 
1064
  if (!target_has_registers)
1065
    error ("mi_cmd_data_write_register_values: No registers.");
1066
 
1067
  if (!(argc - 1))
1068
    error ("mi_cmd_data_write_register_values: No regs and values specified.");
1069
 
1070
  if ((argc - 1) % 2)
1071
    error ("mi_cmd_data_write_register_values: Regs and vals are not in pairs.");
1072
 
1073
  for (i = 1; i < argc; i = i + 2)
1074
    {
1075
      int regnum = atoi (argv[i]);
1076
 
1077
      if (regnum >= 0 && regnum < numregs
1078
          && gdbarch_register_name (gdbarch, regnum)
1079
          && *gdbarch_register_name (gdbarch, regnum))
1080
        {
1081
          LONGEST value;
1082
 
1083
          /* Get the value as a number.  */
1084
          value = parse_and_eval_address (argv[i + 1]);
1085
 
1086
          /* Write it down.  */
1087
          regcache_cooked_write_signed (regcache, regnum, value);
1088
        }
1089
      else
1090
        error ("bad register number");
1091
    }
1092
}
1093
 
1094
/* Evaluate the value of the argument.  The argument is an
1095
   expression. If the expression contains spaces it needs to be
1096
   included in double quotes.  */
1097
void
1098
mi_cmd_data_evaluate_expression (char *command, char **argv, int argc)
1099
{
1100
  struct expression *expr;
1101
  struct cleanup *old_chain = NULL;
1102
  struct value *val;
1103
  struct ui_stream *stb = NULL;
1104
  struct value_print_options opts;
1105
 
1106
  stb = ui_out_stream_new (uiout);
1107
 
1108
  if (argc != 1)
1109
    {
1110
      ui_out_stream_delete (stb);
1111
      error ("mi_cmd_data_evaluate_expression: Usage: -data-evaluate-expression expression");
1112
    }
1113
 
1114
  expr = parse_expression (argv[0]);
1115
 
1116
  old_chain = make_cleanup (free_current_contents, &expr);
1117
 
1118
  val = evaluate_expression (expr);
1119
 
1120
  /* Print the result of the expression evaluation.  */
1121
  get_user_print_options (&opts);
1122
  opts.deref_ref = 0;
1123
  val_print (value_type (val), value_contents (val),
1124
             value_embedded_offset (val), value_address (val),
1125
             stb->stream, 0, &opts, current_language);
1126
 
1127
  ui_out_field_stream (uiout, "value", stb);
1128
  ui_out_stream_delete (stb);
1129
 
1130
  do_cleanups (old_chain);
1131
}
1132
 
1133
/* DATA-MEMORY-READ:
1134
 
1135
   ADDR: start address of data to be dumped.
1136
   WORD-FORMAT: a char indicating format for the ``word''.  See
1137
   the ``x'' command.
1138
   WORD-SIZE: size of each ``word''; 1,2,4, or 8 bytes.
1139
   NR_ROW: Number of rows.
1140
   NR_COL: The number of colums (words per row).
1141
   ASCHAR: (OPTIONAL) Append an ascii character dump to each row.  Use
1142
   ASCHAR for unprintable characters.
1143
 
1144
   Reads SIZE*NR_ROW*NR_COL bytes starting at ADDR from memory and
1145
   displayes them.  Returns:
1146
 
1147
   {addr="...",rowN={wordN="..." ,... [,ascii="..."]}, ...}
1148
 
1149
   Returns:
1150
   The number of bytes read is SIZE*ROW*COL. */
1151
 
1152
void
1153
mi_cmd_data_read_memory (char *command, char **argv, int argc)
1154
{
1155
  struct gdbarch *gdbarch = get_current_arch ();
1156
  struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
1157
  CORE_ADDR addr;
1158
  long total_bytes;
1159
  long nr_cols;
1160
  long nr_rows;
1161
  char word_format;
1162
  struct type *word_type;
1163
  long word_size;
1164
  char word_asize;
1165
  char aschar;
1166
  gdb_byte *mbuf;
1167
  int nr_bytes;
1168
  long offset = 0;
1169
  int optind = 0;
1170
  char *optarg;
1171
  enum opt
1172
    {
1173
      OFFSET_OPT
1174
    };
1175
  static struct mi_opt opts[] =
1176
  {
1177
    {"o", OFFSET_OPT, 1},
1178
    { 0, 0, 0 }
1179
  };
1180
 
1181
  while (1)
1182
    {
1183
      int opt = mi_getopt ("mi_cmd_data_read_memory", argc, argv, opts,
1184
                           &optind, &optarg);
1185
      if (opt < 0)
1186
        break;
1187
      switch ((enum opt) opt)
1188
        {
1189
        case OFFSET_OPT:
1190
          offset = atol (optarg);
1191
          break;
1192
        }
1193
    }
1194
  argv += optind;
1195
  argc -= optind;
1196
 
1197
  if (argc < 5 || argc > 6)
1198
    error ("mi_cmd_data_read_memory: Usage: ADDR WORD-FORMAT WORD-SIZE NR-ROWS NR-COLS [ASCHAR].");
1199
 
1200
  /* Extract all the arguments. */
1201
 
1202
  /* Start address of the memory dump.  */
1203
  addr = parse_and_eval_address (argv[0]) + offset;
1204
  /* The format character to use when displaying a memory word.  See
1205
     the ``x'' command. */
1206
  word_format = argv[1][0];
1207
  /* The size of the memory word.  */
1208
  word_size = atol (argv[2]);
1209
  switch (word_size)
1210
    {
1211
    case 1:
1212
      word_type = builtin_type (gdbarch)->builtin_int8;
1213
      word_asize = 'b';
1214
      break;
1215
    case 2:
1216
      word_type = builtin_type (gdbarch)->builtin_int16;
1217
      word_asize = 'h';
1218
      break;
1219
    case 4:
1220
      word_type = builtin_type (gdbarch)->builtin_int32;
1221
      word_asize = 'w';
1222
      break;
1223
    case 8:
1224
      word_type = builtin_type (gdbarch)->builtin_int64;
1225
      word_asize = 'g';
1226
      break;
1227
    default:
1228
      word_type = builtin_type (gdbarch)->builtin_int8;
1229
      word_asize = 'b';
1230
    }
1231
  /* The number of rows.  */
1232
  nr_rows = atol (argv[3]);
1233
  if (nr_rows <= 0)
1234
    error ("mi_cmd_data_read_memory: invalid number of rows.");
1235
 
1236
  /* Number of bytes per row.  */
1237
  nr_cols = atol (argv[4]);
1238
  if (nr_cols <= 0)
1239
    error ("mi_cmd_data_read_memory: invalid number of columns.");
1240
 
1241
  /* The un-printable character when printing ascii.  */
1242
  if (argc == 6)
1243
    aschar = *argv[5];
1244
  else
1245
    aschar = 0;
1246
 
1247
  /* Create a buffer and read it in.  */
1248
  total_bytes = word_size * nr_rows * nr_cols;
1249
  mbuf = xcalloc (total_bytes, 1);
1250
  make_cleanup (xfree, mbuf);
1251
 
1252
  /* Dispatch memory reads to the topmost target, not the flattened
1253
     current_target.  */
1254
  nr_bytes = target_read_until_error (current_target.beneath,
1255
                                      TARGET_OBJECT_MEMORY, NULL, mbuf,
1256
                                      addr, total_bytes);
1257
  if (nr_bytes <= 0)
1258
    error ("Unable to read memory.");
1259
 
1260
  /* Output the header information.  */
1261
  ui_out_field_core_addr (uiout, "addr", gdbarch, addr);
1262
  ui_out_field_int (uiout, "nr-bytes", nr_bytes);
1263
  ui_out_field_int (uiout, "total-bytes", total_bytes);
1264
  ui_out_field_core_addr (uiout, "next-row",
1265
                          gdbarch, addr + word_size * nr_cols);
1266
  ui_out_field_core_addr (uiout, "prev-row",
1267
                          gdbarch, addr - word_size * nr_cols);
1268
  ui_out_field_core_addr (uiout, "next-page", gdbarch, addr + total_bytes);
1269
  ui_out_field_core_addr (uiout, "prev-page", gdbarch, addr - total_bytes);
1270
 
1271
  /* Build the result as a two dimentional table.  */
1272
  {
1273
    struct ui_stream *stream = ui_out_stream_new (uiout);
1274
    struct cleanup *cleanup_list_memory;
1275
    int row;
1276
    int row_byte;
1277
    cleanup_list_memory = make_cleanup_ui_out_list_begin_end (uiout, "memory");
1278
    for (row = 0, row_byte = 0;
1279
         row < nr_rows;
1280
         row++, row_byte += nr_cols * word_size)
1281
      {
1282
        int col;
1283
        int col_byte;
1284
        struct cleanup *cleanup_tuple;
1285
        struct cleanup *cleanup_list_data;
1286
        struct value_print_options opts;
1287
 
1288
        cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
1289
        ui_out_field_core_addr (uiout, "addr", gdbarch, addr + row_byte);
1290
        /* ui_out_field_core_addr_symbolic (uiout, "saddr", addr + row_byte); */
1291
        cleanup_list_data = make_cleanup_ui_out_list_begin_end (uiout, "data");
1292
        get_formatted_print_options (&opts, word_format);
1293
        for (col = 0, col_byte = row_byte;
1294
             col < nr_cols;
1295
             col++, col_byte += word_size)
1296
          {
1297
            if (col_byte + word_size > nr_bytes)
1298
              {
1299
                ui_out_field_string (uiout, NULL, "N/A");
1300
              }
1301
            else
1302
              {
1303
                ui_file_rewind (stream->stream);
1304
                print_scalar_formatted (mbuf + col_byte, word_type, &opts,
1305
                                        word_asize, stream->stream);
1306
                ui_out_field_stream (uiout, NULL, stream);
1307
              }
1308
          }
1309
        do_cleanups (cleanup_list_data);
1310
        if (aschar)
1311
          {
1312
            int byte;
1313
            ui_file_rewind (stream->stream);
1314
            for (byte = row_byte; byte < row_byte + word_size * nr_cols; byte++)
1315
              {
1316
                if (byte >= nr_bytes)
1317
                  {
1318
                    fputc_unfiltered ('X', stream->stream);
1319
                  }
1320
                else if (mbuf[byte] < 32 || mbuf[byte] > 126)
1321
                  {
1322
                    fputc_unfiltered (aschar, stream->stream);
1323
                  }
1324
                else
1325
                  fputc_unfiltered (mbuf[byte], stream->stream);
1326
              }
1327
            ui_out_field_stream (uiout, "ascii", stream);
1328
          }
1329
        do_cleanups (cleanup_tuple);
1330
      }
1331
    ui_out_stream_delete (stream);
1332
    do_cleanups (cleanup_list_memory);
1333
  }
1334
  do_cleanups (cleanups);
1335
}
1336
 
1337
/* DATA-MEMORY-WRITE:
1338
 
1339
   COLUMN_OFFSET: optional argument. Must be preceeded by '-o'. The
1340
   offset from the beginning of the memory grid row where the cell to
1341
   be written is.
1342
   ADDR: start address of the row in the memory grid where the memory
1343
   cell is, if OFFSET_COLUMN is specified.  Otherwise, the address of
1344
   the location to write to.
1345
   FORMAT: a char indicating format for the ``word''.  See
1346
   the ``x'' command.
1347
   WORD_SIZE: size of each ``word''; 1,2,4, or 8 bytes
1348
   VALUE: value to be written into the memory address.
1349
 
1350
   Writes VALUE into ADDR + (COLUMN_OFFSET * WORD_SIZE).
1351
 
1352
   Prints nothing.  */
1353
void
1354
mi_cmd_data_write_memory (char *command, char **argv, int argc)
1355
{
1356
  struct gdbarch *gdbarch = get_current_arch ();
1357
  enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1358
  CORE_ADDR addr;
1359
  char word_format;
1360
  long word_size;
1361
  /* FIXME: ezannoni 2000-02-17 LONGEST could possibly not be big
1362
     enough when using a compiler other than GCC.  */
1363
  LONGEST value;
1364
  void *buffer;
1365
  struct cleanup *old_chain;
1366
  long offset = 0;
1367
  int optind = 0;
1368
  char *optarg;
1369
  enum opt
1370
    {
1371
      OFFSET_OPT
1372
    };
1373
  static struct mi_opt opts[] =
1374
  {
1375
    {"o", OFFSET_OPT, 1},
1376
    { 0, 0, 0 }
1377
  };
1378
 
1379
  while (1)
1380
    {
1381
      int opt = mi_getopt ("mi_cmd_data_write_memory", argc, argv, opts,
1382
                           &optind, &optarg);
1383
      if (opt < 0)
1384
        break;
1385
      switch ((enum opt) opt)
1386
        {
1387
        case OFFSET_OPT:
1388
          offset = atol (optarg);
1389
          break;
1390
        }
1391
    }
1392
  argv += optind;
1393
  argc -= optind;
1394
 
1395
  if (argc != 4)
1396
    error ("mi_cmd_data_write_memory: Usage: [-o COLUMN_OFFSET] ADDR FORMAT WORD-SIZE VALUE.");
1397
 
1398
  /* Extract all the arguments.  */
1399
  /* Start address of the memory dump.  */
1400
  addr = parse_and_eval_address (argv[0]);
1401
  /* The format character to use when displaying a memory word.  See
1402
     the ``x'' command.  */
1403
  word_format = argv[1][0];
1404
  /* The size of the memory word. */
1405
  word_size = atol (argv[2]);
1406
 
1407
  /* Calculate the real address of the write destination.  */
1408
  addr += (offset * word_size);
1409
 
1410
  /* Get the value as a number.  */
1411
  value = parse_and_eval_address (argv[3]);
1412
  /* Get the value into an array.  */
1413
  buffer = xmalloc (word_size);
1414
  old_chain = make_cleanup (xfree, buffer);
1415
  store_signed_integer (buffer, word_size, byte_order, value);
1416
  /* Write it down to memory.  */
1417
  write_memory (addr, buffer, word_size);
1418
  /* Free the buffer.  */
1419
  do_cleanups (old_chain);
1420
}
1421
 
1422
void
1423
mi_cmd_enable_timings (char *command, char **argv, int argc)
1424
{
1425
  if (argc == 0)
1426
    do_timings = 1;
1427
  else if (argc == 1)
1428
    {
1429
      if (strcmp (argv[0], "yes") == 0)
1430
        do_timings = 1;
1431
      else if (strcmp (argv[0], "no") == 0)
1432
        do_timings = 0;
1433
      else
1434
        goto usage_error;
1435
    }
1436
  else
1437
    goto usage_error;
1438
 
1439
  return;
1440
 
1441
 usage_error:
1442
  error ("mi_cmd_enable_timings: Usage: %s {yes|no}", command);
1443
}
1444
 
1445
void
1446
mi_cmd_list_features (char *command, char **argv, int argc)
1447
{
1448
  if (argc == 0)
1449
    {
1450
      struct cleanup *cleanup = NULL;
1451
      cleanup = make_cleanup_ui_out_list_begin_end (uiout, "features");
1452
 
1453
      ui_out_field_string (uiout, NULL, "frozen-varobjs");
1454
      ui_out_field_string (uiout, NULL, "pending-breakpoints");
1455
      ui_out_field_string (uiout, NULL, "thread-info");
1456
 
1457
#if HAVE_PYTHON
1458
      ui_out_field_string (uiout, NULL, "python");
1459
#endif
1460
 
1461
      do_cleanups (cleanup);
1462
      return;
1463
    }
1464
 
1465
  error ("-list-features should be passed no arguments");
1466
}
1467
 
1468
void
1469
mi_cmd_list_target_features (char *command, char **argv, int argc)
1470
{
1471
  if (argc == 0)
1472
    {
1473
      struct cleanup *cleanup = NULL;
1474
      cleanup = make_cleanup_ui_out_list_begin_end (uiout, "features");
1475
 
1476
      if (target_can_async_p ())
1477
        ui_out_field_string (uiout, NULL, "async");
1478
 
1479
      do_cleanups (cleanup);
1480
      return;
1481
    }
1482
 
1483
  error ("-list-target-features should be passed no arguments");
1484
}
1485
 
1486
/* Execute a command within a safe environment.
1487
   Return <0 for error; >=0 for ok.
1488
 
1489
   args->action will tell mi_execute_command what action
1490
   to perfrom after the given command has executed (display/suppress
1491
   prompt, display error). */
1492
 
1493
static void
1494
captured_mi_execute_command (struct ui_out *uiout, void *data)
1495
{
1496
  struct cleanup *cleanup;
1497
  struct mi_parse *context = (struct mi_parse *) data;
1498
 
1499
  if (do_timings)
1500
    current_command_ts = context->cmd_start;
1501
 
1502
  current_token = xstrdup (context->token);
1503
  cleanup = make_cleanup (free_current_contents, &current_token);
1504
 
1505
  running_result_record_printed = 0;
1506
  mi_proceeded = 0;
1507
  switch (context->op)
1508
    {
1509
    case MI_COMMAND:
1510
      /* A MI command was read from the input stream.  */
1511
      if (mi_debug_p)
1512
        /* FIXME: gdb_???? */
1513
        fprintf_unfiltered (raw_stdout, " token=`%s' command=`%s' args=`%s'\n",
1514
                            context->token, context->command, context->args);
1515
 
1516
 
1517
      mi_cmd_execute (context);
1518
 
1519
      /* Print the result if there were no errors.
1520
 
1521
         Remember that on the way out of executing a command, you have
1522
         to directly use the mi_interp's uiout, since the command could
1523
         have reset the interpreter, in which case the current uiout
1524
         will most likely crash in the mi_out_* routines.  */
1525
      if (!running_result_record_printed)
1526
        {
1527
          fputs_unfiltered (context->token, raw_stdout);
1528
          /* There's no particularly good reason why target-connect results
1529
             in not ^done.  Should kill ^connected for MI3.  */
1530
          fputs_unfiltered (strcmp (context->command, "target-select") == 0
1531
                            ? "^connected" : "^done", raw_stdout);
1532
          mi_out_put (uiout, raw_stdout);
1533
          mi_out_rewind (uiout);
1534
          mi_print_timing_maybe ();
1535
          fputs_unfiltered ("\n", raw_stdout);
1536
        }
1537
      else
1538
            /* The command does not want anything to be printed.  In that
1539
               case, the command probably should not have written anything
1540
               to uiout, but in case it has written something, discard it.  */
1541
        mi_out_rewind (uiout);
1542
      break;
1543
 
1544
    case CLI_COMMAND:
1545
      {
1546
        char *argv[2];
1547
        /* A CLI command was read from the input stream.  */
1548
        /* This "feature" will be removed as soon as we have a
1549
           complete set of mi commands.  */
1550
        /* Echo the command on the console.  */
1551
        fprintf_unfiltered (gdb_stdlog, "%s\n", context->command);
1552
        /* Call the "console" interpreter.  */
1553
        argv[0] = "console";
1554
        argv[1] = context->command;
1555
        mi_cmd_interpreter_exec ("-interpreter-exec", argv, 2);
1556
 
1557
        /* If we changed interpreters, DON'T print out anything.  */
1558
        if (current_interp_named_p (INTERP_MI)
1559
            || current_interp_named_p (INTERP_MI1)
1560
            || current_interp_named_p (INTERP_MI2)
1561
            || current_interp_named_p (INTERP_MI3))
1562
          {
1563
            if (!running_result_record_printed)
1564
              {
1565
                fputs_unfiltered (context->token, raw_stdout);
1566
                fputs_unfiltered ("^done", raw_stdout);
1567
                mi_out_put (uiout, raw_stdout);
1568
                mi_out_rewind (uiout);
1569
                mi_print_timing_maybe ();
1570
                fputs_unfiltered ("\n", raw_stdout);
1571
              }
1572
            else
1573
              mi_out_rewind (uiout);
1574
          }
1575
        break;
1576
      }
1577
 
1578
    }
1579
 
1580
  do_cleanups (cleanup);
1581
 
1582
  return;
1583
}
1584
 
1585
 
1586
void
1587
mi_execute_command (char *cmd, int from_tty)
1588
{
1589
  struct mi_parse *command;
1590
  struct ui_out *saved_uiout = uiout;
1591
 
1592
  /* This is to handle EOF (^D). We just quit gdb.  */
1593
  /* FIXME: we should call some API function here.  */
1594
  if (cmd == 0)
1595
    quit_force (NULL, from_tty);
1596
 
1597
  target_log_command (cmd);
1598
 
1599
  command = mi_parse (cmd);
1600
 
1601
  if (command != NULL)
1602
    {
1603
      struct gdb_exception result;
1604
      ptid_t previous_ptid = inferior_ptid;
1605
 
1606
      if (do_timings)
1607
        {
1608
          command->cmd_start = (struct mi_timestamp *)
1609
            xmalloc (sizeof (struct mi_timestamp));
1610
          timestamp (command->cmd_start);
1611
        }
1612
 
1613
      result = catch_exception (uiout, captured_mi_execute_command, command,
1614
                                RETURN_MASK_ALL);
1615
      if (result.reason < 0)
1616
        {
1617
          /* The command execution failed and error() was called
1618
             somewhere.  */
1619
          fputs_unfiltered (command->token, raw_stdout);
1620
          fputs_unfiltered ("^error,msg=\"", raw_stdout);
1621
          if (result.message == NULL)
1622
            fputs_unfiltered ("unknown error", raw_stdout);
1623
          else
1624
            fputstr_unfiltered (result.message, '"', raw_stdout);
1625
          fputs_unfiltered ("\"\n", raw_stdout);
1626
          mi_out_rewind (uiout);
1627
        }
1628
 
1629
      bpstat_do_actions ();
1630
 
1631
      if (/* The notifications are only output when the top-level
1632
             interpreter (specified on the command line) is MI.  */
1633
          ui_out_is_mi_like_p (interp_ui_out (top_level_interpreter ()))
1634
          /* Don't try report anything if there are no threads --
1635
             the program is dead.  */
1636
          && thread_count () != 0
1637
          /* -thread-select explicitly changes thread. If frontend uses that
1638
             internally, we don't want to emit =thread-selected, since
1639
             =thread-selected is supposed to indicate user's intentions.  */
1640
          && strcmp (command->command, "thread-select") != 0)
1641
        {
1642
          struct mi_interp *mi = top_level_interpreter_data ();
1643
          int report_change = 0;
1644
 
1645
          if (command->thread == -1)
1646
            {
1647
              report_change = (!ptid_equal (previous_ptid, null_ptid)
1648
                               && !ptid_equal (inferior_ptid, previous_ptid)
1649
                               && !ptid_equal (inferior_ptid, null_ptid));
1650
            }
1651
          else if (!ptid_equal (inferior_ptid, null_ptid))
1652
            {
1653
              struct thread_info *ti = inferior_thread ();
1654
              report_change = (ti->num != command->thread);
1655
            }
1656
 
1657
          if (report_change)
1658
            {
1659
              struct thread_info *ti = inferior_thread ();
1660
              target_terminal_ours ();
1661
              fprintf_unfiltered (mi->event_channel,
1662
                                  "thread-selected,id=\"%d\"",
1663
                                  ti->num);
1664
              gdb_flush (mi->event_channel);
1665
            }
1666
        }
1667
 
1668
      mi_parse_free (command);
1669
    }
1670
 
1671
  fputs_unfiltered ("(gdb) \n", raw_stdout);
1672
  gdb_flush (raw_stdout);
1673
  /* Print any buffered hook code.  */
1674
  /* ..... */
1675
}
1676
 
1677
static void
1678
mi_cmd_execute (struct mi_parse *parse)
1679
{
1680
  struct cleanup *cleanup;
1681
  int i;
1682
 
1683
  prepare_execute_command ();
1684
 
1685
  cleanup = make_cleanup (null_cleanup, NULL);
1686
 
1687
  if (parse->frame != -1 && parse->thread == -1)
1688
    error (_("Cannot specify --frame without --thread"));
1689
 
1690
  if (parse->thread != -1)
1691
    {
1692
      struct thread_info *tp = find_thread_id (parse->thread);
1693
      if (!tp)
1694
        error (_("Invalid thread id: %d"), parse->thread);
1695
 
1696
      if (is_exited (tp->ptid))
1697
        error (_("Thread id: %d has terminated"), parse->thread);
1698
 
1699
      switch_to_thread (tp->ptid);
1700
    }
1701
 
1702
  if (parse->frame != -1)
1703
    {
1704
      struct frame_info *fid;
1705
      int frame = parse->frame;
1706
      fid = find_relative_frame (get_current_frame (), &frame);
1707
      if (frame == 0)
1708
        /* find_relative_frame was successful */
1709
        select_frame (fid);
1710
      else
1711
        error (_("Invalid frame id: %d"), frame);
1712
    }
1713
 
1714
  if (parse->cmd->argv_func != NULL)
1715
    parse->cmd->argv_func (parse->command, parse->argv, parse->argc);
1716
  else if (parse->cmd->cli.cmd != 0)
1717
    {
1718
      /* FIXME: DELETE THIS. */
1719
      /* The operation is still implemented by a cli command.  */
1720
      /* Must be a synchronous one.  */
1721
      mi_execute_cli_command (parse->cmd->cli.cmd, parse->cmd->cli.args_p,
1722
                              parse->args);
1723
    }
1724
  else
1725
    {
1726
      /* FIXME: DELETE THIS.  */
1727
      struct ui_file *stb;
1728
 
1729
      stb = mem_fileopen ();
1730
 
1731
      fputs_unfiltered ("Undefined mi command: ", stb);
1732
      fputstr_unfiltered (parse->command, '"', stb);
1733
      fputs_unfiltered (" (missing implementation)", stb);
1734
 
1735
      make_cleanup_ui_file_delete (stb);
1736
      error_stream (stb);
1737
    }
1738
  do_cleanups (cleanup);
1739
}
1740
 
1741
/* FIXME: This is just a hack so we can get some extra commands going.
1742
   We don't want to channel things through the CLI, but call libgdb directly.
1743
   Use only for synchronous commands.  */
1744
 
1745
void
1746
mi_execute_cli_command (const char *cmd, int args_p, const char *args)
1747
{
1748
  if (cmd != 0)
1749
    {
1750
      struct cleanup *old_cleanups;
1751
      char *run;
1752
      if (args_p)
1753
        run = xstrprintf ("%s %s", cmd, args);
1754
      else
1755
        run = xstrdup (cmd);
1756
      if (mi_debug_p)
1757
        /* FIXME: gdb_???? */
1758
        fprintf_unfiltered (gdb_stdout, "cli=%s run=%s\n",
1759
                            cmd, run);
1760
      old_cleanups = make_cleanup (xfree, run);
1761
      execute_command ( /*ui */ run, 0 /*from_tty */ );
1762
      do_cleanups (old_cleanups);
1763
      return;
1764
    }
1765
}
1766
 
1767
void
1768
mi_execute_async_cli_command (char *cli_command, char **argv, int argc)
1769
{
1770
  struct cleanup *old_cleanups;
1771
  char *run;
1772
 
1773
  if (target_can_async_p ())
1774
    run = xstrprintf ("%s %s&", cli_command, argc ? *argv : "");
1775
  else
1776
    run = xstrprintf ("%s %s", cli_command, argc ? *argv : "");
1777
  old_cleanups = make_cleanup (xfree, run);
1778
 
1779
  execute_command ( /*ui */ run, 0 /*from_tty */ );
1780
 
1781
  if (target_can_async_p ())
1782
    {
1783
      /* If we're not executing, an exception should have been throw.  */
1784
      gdb_assert (is_running (inferior_ptid));
1785
      do_cleanups (old_cleanups);
1786
    }
1787
  else
1788
    {
1789
      /* Do this before doing any printing.  It would appear that some
1790
         print code leaves garbage around in the buffer.  */
1791
      do_cleanups (old_cleanups);
1792
    }
1793
}
1794
 
1795
void
1796
mi_load_progress (const char *section_name,
1797
                  unsigned long sent_so_far,
1798
                  unsigned long total_section,
1799
                  unsigned long total_sent,
1800
                  unsigned long grand_total)
1801
{
1802
  struct timeval time_now, delta, update_threshold;
1803
  static struct timeval last_update;
1804
  static char *previous_sect_name = NULL;
1805
  int new_section;
1806
  struct ui_out *saved_uiout;
1807
 
1808
  /* This function is called through deprecated_show_load_progress
1809
     which means uiout may not be correct.  Fix it for the duration
1810
     of this function.  */
1811
  saved_uiout = uiout;
1812
 
1813
  if (current_interp_named_p (INTERP_MI)
1814
      || current_interp_named_p (INTERP_MI2))
1815
    uiout = mi_out_new (2);
1816
  else if (current_interp_named_p (INTERP_MI1))
1817
    uiout = mi_out_new (1);
1818
  else if (current_interp_named_p (INTERP_MI3))
1819
    uiout = mi_out_new (3);
1820
  else
1821
    return;
1822
 
1823
  update_threshold.tv_sec = 0;
1824
  update_threshold.tv_usec = 500000;
1825
  gettimeofday (&time_now, NULL);
1826
 
1827
  delta.tv_usec = time_now.tv_usec - last_update.tv_usec;
1828
  delta.tv_sec = time_now.tv_sec - last_update.tv_sec;
1829
 
1830
  if (delta.tv_usec < 0)
1831
    {
1832
      delta.tv_sec -= 1;
1833
      delta.tv_usec += 1000000L;
1834
    }
1835
 
1836
  new_section = (previous_sect_name ?
1837
                 strcmp (previous_sect_name, section_name) : 1);
1838
  if (new_section)
1839
    {
1840
      struct cleanup *cleanup_tuple;
1841
      xfree (previous_sect_name);
1842
      previous_sect_name = xstrdup (section_name);
1843
 
1844
      if (current_token)
1845
        fputs_unfiltered (current_token, raw_stdout);
1846
      fputs_unfiltered ("+download", raw_stdout);
1847
      cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
1848
      ui_out_field_string (uiout, "section", section_name);
1849
      ui_out_field_int (uiout, "section-size", total_section);
1850
      ui_out_field_int (uiout, "total-size", grand_total);
1851
      do_cleanups (cleanup_tuple);
1852
      mi_out_put (uiout, raw_stdout);
1853
      fputs_unfiltered ("\n", raw_stdout);
1854
      gdb_flush (raw_stdout);
1855
    }
1856
 
1857
  if (delta.tv_sec >= update_threshold.tv_sec &&
1858
      delta.tv_usec >= update_threshold.tv_usec)
1859
    {
1860
      struct cleanup *cleanup_tuple;
1861
      last_update.tv_sec = time_now.tv_sec;
1862
      last_update.tv_usec = time_now.tv_usec;
1863
      if (current_token)
1864
        fputs_unfiltered (current_token, raw_stdout);
1865
      fputs_unfiltered ("+download", raw_stdout);
1866
      cleanup_tuple = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
1867
      ui_out_field_string (uiout, "section", section_name);
1868
      ui_out_field_int (uiout, "section-sent", sent_so_far);
1869
      ui_out_field_int (uiout, "section-size", total_section);
1870
      ui_out_field_int (uiout, "total-sent", total_sent);
1871
      ui_out_field_int (uiout, "total-size", grand_total);
1872
      do_cleanups (cleanup_tuple);
1873
      mi_out_put (uiout, raw_stdout);
1874
      fputs_unfiltered ("\n", raw_stdout);
1875
      gdb_flush (raw_stdout);
1876
    }
1877
 
1878
  xfree (uiout);
1879
  uiout = saved_uiout;
1880
}
1881
 
1882
static void
1883
timestamp (struct mi_timestamp *tv)
1884
  {
1885
    long usec;
1886
    gettimeofday (&tv->wallclock, NULL);
1887
#ifdef HAVE_GETRUSAGE
1888
    getrusage (RUSAGE_SELF, &rusage);
1889
    tv->utime.tv_sec = rusage.ru_utime.tv_sec;
1890
    tv->utime.tv_usec = rusage.ru_utime.tv_usec;
1891
    tv->stime.tv_sec = rusage.ru_stime.tv_sec;
1892
    tv->stime.tv_usec = rusage.ru_stime.tv_usec;
1893
#else
1894
    usec = get_run_time ();
1895
    tv->utime.tv_sec = usec/1000000L;
1896
    tv->utime.tv_usec = usec - 1000000L*tv->utime.tv_sec;
1897
    tv->stime.tv_sec = 0;
1898
    tv->stime.tv_usec = 0;
1899
#endif
1900
  }
1901
 
1902
static void
1903
print_diff_now (struct mi_timestamp *start)
1904
  {
1905
    struct mi_timestamp now;
1906
    timestamp (&now);
1907
    print_diff (start, &now);
1908
  }
1909
 
1910
void
1911
mi_print_timing_maybe (void)
1912
{
1913
  /* If the command is -enable-timing then do_timings may be
1914
     true whilst current_command_ts is not initialized.  */
1915
  if (do_timings && current_command_ts)
1916
    print_diff_now (current_command_ts);
1917
}
1918
 
1919
static long
1920
timeval_diff (struct timeval start, struct timeval end)
1921
  {
1922
    return ((end.tv_sec - start.tv_sec) * 1000000L)
1923
      + (end.tv_usec - start.tv_usec);
1924
  }
1925
 
1926
static void
1927
print_diff (struct mi_timestamp *start, struct mi_timestamp *end)
1928
  {
1929
    fprintf_unfiltered
1930
      (raw_stdout,
1931
       ",time={wallclock=\"%0.5f\",user=\"%0.5f\",system=\"%0.5f\"}",
1932
       timeval_diff (start->wallclock, end->wallclock) / 1000000.0,
1933
       timeval_diff (start->utime, end->utime) / 1000000.0,
1934
       timeval_diff (start->stime, end->stime) / 1000000.0);
1935
  }

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.