mi-interp.c 35.6 KB
Newer Older
1
2
/* MI Interpreter Definitions and Commands for GDB, the GNU debugger.

3
   Copyright (C) 2002-2019 Free Software Foundation, Inc.
4
5
6
7
8

   This file is part of GDB.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
9
   the Free Software Foundation; either version 3 of the License, or
10
11
12
13
14
15
16
17
   (at your option) any later version.

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

   You should have received a copy of the GNU General Public License
18
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20
21
22
23
24

#include "defs.h"
#include "interps.h"
#include "event-top.h"
#include "event-loop.h"
#include "inferior.h"
Pedro Alves's avatar
Pedro Alves committed
25
#include "infrun.h"
26
27
28
29
30
31
#include "ui-out.h"
#include "top.h"
#include "mi-main.h"
#include "mi-cmds.h"
#include "mi-out.h"
#include "mi-console.h"
32
#include "mi-common.h"
Tom Tromey's avatar
Tom Tromey committed
33
#include "observable.h"
34
#include "gdbthread.h"
35
#include "solist.h"
Yao Qi's avatar
gdb:    
Yao Qi committed
36
#include "objfiles.h"
Yao Qi's avatar
Yao Qi committed
37
#include "tracepoint.h"
38
#include "cli-out.h"
39
#include "thread-fsm.h"
40
#include "cli/cli-interp.h"
41

42
43
44
/* These are the interpreter setup, etc. functions for the MI
   interpreter.  */

45
static void mi_execute_command_wrapper (const char *cmd);
46
47
static void mi_execute_command_input_handler
  (gdb::unique_xmalloc_ptr<char> &&cmd);
48
49

/* These are hooks that we put in place while doing interpreter_exec
50
51
52
   so we can report interesting things that happened "behind the MI's
   back" in this command.  */

53
static int mi_interp_query_hook (const char *ctlstr, va_list ap)
54
  ATTRIBUTE_PRINTF (1, 0);
55
56
57

static void mi_insert_notify_hooks (void);
static void mi_remove_notify_hooks (void);
58
59
60
61
62

static void mi_on_signal_received (enum gdb_signal siggnal);
static void mi_on_end_stepping_range (void);
static void mi_on_signal_exited (enum gdb_signal siggnal);
static void mi_on_exited (int exitstatus);
63
static void mi_on_normal_stop (struct bpstats *bs, int print_frame);
64
static void mi_on_no_history (void);
65

66
static void mi_new_thread (struct thread_info *t);
Pedro Alves's avatar
gdb/    
Pedro Alves committed
67
static void mi_thread_exit (struct thread_info *t, int silent);
68
69
static void mi_record_changed (struct inferior*, int, const char *,
			       const char *);
Vladimir Prus's avatar
Vladimir Prus committed
70
71
72
73
static void mi_inferior_added (struct inferior *inf);
static void mi_inferior_appeared (struct inferior *inf);
static void mi_inferior_exit (struct inferior *inf);
static void mi_inferior_removed (struct inferior *inf);
Vladimir Prus's avatar
Vladimir Prus committed
74
static void mi_on_resume (ptid_t ptid);
75
76
static void mi_solib_loaded (struct so_list *solib);
static void mi_solib_unloaded (struct so_list *solib);
Pedro Alves's avatar
gdb/    
Pedro Alves committed
77
static void mi_about_to_proceed (void);
Yao Qi's avatar
Yao Qi committed
78
static void mi_traceframe_changed (int tfnum, int tpnum);
Yao Qi's avatar
Yao Qi committed
79
80
81
static void mi_tsv_created (const struct trace_state_variable *tsv);
static void mi_tsv_deleted (const struct trace_state_variable *tsv);
static void mi_tsv_modified (const struct trace_state_variable *tsv);
82
83
84
static void mi_breakpoint_created (struct breakpoint *b);
static void mi_breakpoint_deleted (struct breakpoint *b);
static void mi_breakpoint_modified (struct breakpoint *b);
Yao Qi's avatar
gdb/    
Yao Qi committed
85
static void mi_command_param_changed (const char *param, const char *value);
Yao Qi's avatar
gdb:    
Yao Qi committed
86
87
static void mi_memory_changed (struct inferior *inf, CORE_ADDR memaddr,
			       ssize_t len, const bfd_byte *myaddr);
88
static void mi_on_sync_execution_done (void);
89

Vladimir Prus's avatar
Vladimir Prus committed
90
91
static int report_initial_inferior (struct inferior *inf, void *closure);

Pedro Alves's avatar
Pedro Alves committed
92
93
94
/* Display the MI prompt.  */

static void
95
display_mi_prompt (struct mi_interp *mi)
Pedro Alves's avatar
Pedro Alves committed
96
{
97
98
  struct ui *ui = current_ui;

99
100
  fputs_unfiltered ("(gdb) \n", mi->raw_stdout);
  gdb_flush (mi->raw_stdout);
101
  ui->prompt_state = PROMPTED;
Pedro Alves's avatar
Pedro Alves committed
102
103
}

104
105
106
107
108
109
/* Returns the INTERP's data cast as mi_interp if INTERP is an MI, and
   returns NULL otherwise.  */

static struct mi_interp *
as_mi_interp (struct interp *interp)
{
110
  return dynamic_cast<mi_interp *> (interp);
111
112
}

113
114
void
mi_interp::init (bool top_level)
115
{
116
  mi_interp *mi = this;
Pedro Alves's avatar
gdb/    
Pedro Alves committed
117
  int mi_version;
118

119
120
121
122
  /* Store the current output channel, so that we can create a console
     channel that encapsulates and prefixes all gdb_output-type bits
     coming from the rest of the debugger.  */
  mi->raw_stdout = gdb_stdout;
123

124
125
  /* Create MI console channels, each with a different prefix so they
     can be distinguished.  */
126
127
  mi->out = new mi_console_file (mi->raw_stdout, "~", '"');
  mi->err = new mi_console_file (mi->raw_stdout, "&", '"');
128
  mi->log = mi->err;
129
130
  mi->targ = new mi_console_file (mi->raw_stdout, "@", '"');
  mi->event_channel = new mi_console_file (mi->raw_stdout, "=", 0);
131

Pedro Alves's avatar
gdb/    
Pedro Alves committed
132
133
  /* INTERP_MI selects the most recent released version.  "mi2" was
     released as part of GDB 6.0.  */
Tom Tromey's avatar
Tom Tromey committed
134
  if (strcmp (name (), INTERP_MI) == 0)
Pedro Alves's avatar
gdb/    
Pedro Alves committed
135
    mi_version = 2;
Tom Tromey's avatar
Tom Tromey committed
136
  else if (strcmp (name (), INTERP_MI1) == 0)
Pedro Alves's avatar
gdb/    
Pedro Alves committed
137
    mi_version = 1;
Tom Tromey's avatar
Tom Tromey committed
138
  else if (strcmp (name (), INTERP_MI2) == 0)
Pedro Alves's avatar
gdb/    
Pedro Alves committed
139
    mi_version = 2;
Tom Tromey's avatar
Tom Tromey committed
140
  else if (strcmp (name (), INTERP_MI3) == 0)
Pedro Alves's avatar
gdb/    
Pedro Alves committed
141
142
143
144
    mi_version = 3;
  else
    gdb_assert_not_reached ("unhandled MI version");

145
146
147
  mi->mi_uiout = mi_out_new (mi_version);
  mi->cli_uiout = cli_out_new (mi->out);

148
  if (top_level)
149
    {
150
151
152
153
      /* The initial inferior is created before this function is
	 called, so we need to report it explicitly.  Use iteration in
	 case future version of GDB creates more than one inferior
	 up-front.  */
Vladimir Prus's avatar
Vladimir Prus committed
154
      iterate_over_inferiors (report_initial_inferior, mi);
155
    }
156
157
}

158
159
void
mi_interp::resume ()
160
{
161
  struct mi_interp *mi = this;
Pedro Alves's avatar
Pedro Alves committed
162
  struct ui *ui = current_ui;
163

164
165
  /* As per hack note in mi_interpreter_init, swap in the output
     channels... */
166
  gdb_setup_readline (0);
167

Pedro Alves's avatar
Pedro Alves committed
168
169
  ui->call_readline = gdb_readline_no_editing_callback;
  ui->input_handler = mi_execute_command_input_handler;
170
171

  gdb_stdout = mi->out;
172
  /* Route error and log output through the MI.  */
173
174
  gdb_stderr = mi->err;
  gdb_stdlog = mi->log;
175
  /* Route target output through the MI.  */
176
  gdb_stdtarg = mi->targ;
177
  /* Route target error through the MI as well.  */
178
  gdb_stdtargerr = mi->targ;
179
180
181
182
183

  /* Replace all the hooks that we know about.  There really needs to
     be a better way of doing this... */
  clear_interpreter_hooks ();

184
  deprecated_show_load_progress = mi_load_progress;
185
186
}

187
188
void
mi_interp::suspend ()
189
190
191
192
{
  gdb_disable_readline ();
}

193
194
gdb_exception
mi_interp::exec (const char *command)
195
{
196
  mi_execute_command_wrapper (command);
197
  return exception_none;
198
199
}

200
void
201
mi_cmd_interpreter_exec (const char *command, char **argv, int argc)
202
203
204
205
206
{
  struct interp *interp_to_use;
  int i;

  if (argc < 2)
Tom Tromey's avatar
gdb    
Tom Tromey committed
207
    error (_("-interpreter-exec: "
208
	     "Usage: -interpreter-exec interp command"));
209

Pedro Alves's avatar
Pedro Alves committed
210
  interp_to_use = interp_lookup (current_ui, argv[0]);
211
  if (interp_to_use == NULL)
Tom Tromey's avatar
gdb    
Tom Tromey committed
212
    error (_("-interpreter-exec: could not find interpreter \"%s\""),
213
	   argv[0]);
214

215
216
217
218
  /* Note that unlike the CLI version of this command, we don't
     actually set INTERP_TO_USE as the current interpreter, as we
     still want gdb_stdout, etc. to point at MI streams.  */

219
220
221
222
223
  /* Insert the MI out hooks, making sure to also call the
     interpreter's hooks if it has any.  */
  /* KRS: We shouldn't need this... Events should be installed and
     they should just ALWAYS fire something out down the MI
     channel.  */
224
225
  mi_insert_notify_hooks ();

226
  /* Now run the code.  */
227

228
  std::string mi_error_message;
229
230
  for (i = 1; i < argc; i++)
    {
Vladimir Prus's avatar
Vladimir Prus committed
231
      struct gdb_exception e = interp_exec (interp_to_use, argv[i]);
232

Vladimir Prus's avatar
Vladimir Prus committed
233
234
      if (e.reason < 0)
	{
235
	  mi_error_message = e.message;
Vladimir Prus's avatar
Vladimir Prus committed
236
237
	  break;
	}
238
239
240
241
    }

  mi_remove_notify_hooks ();

242
243
  if (!mi_error_message.empty ())
    error ("%s", mi_error_message.c_str ());
244
245
}

246
247
248
249
250
251
/* This inserts a number of hooks that are meant to produce
   async-notify ("=") MI messages while running commands in another
   interpreter using mi_interpreter_exec.  The canonical use for this
   is to allow access to the gdb CLI interpreter from within the MI,
   while still producing MI style output when actions in the CLI
   command change GDB's state.  */
252
253
254
255

static void
mi_insert_notify_hooks (void)
{
256
  deprecated_query_hook = mi_interp_query_hook;
257
258
259
}

static void
260
mi_remove_notify_hooks (void)
261
{
262
  deprecated_query_hook = NULL;
263
264
265
266
267
268
269
270
271
}

static int
mi_interp_query_hook (const char *ctlstr, va_list ap)
{
  return 1;
}

static void
272
mi_execute_command_wrapper (const char *cmd)
273
{
Pedro Alves's avatar
Pedro Alves committed
274
275
  struct ui *ui = current_ui;

Pedro Alves's avatar
Pedro Alves committed
276
  mi_execute_command (cmd, ui->instream == ui->stdin_stream);
277
278
}

279
280
281
282
283
/* Observer for the synchronous_command_done notification.  */

static void
mi_on_sync_execution_done (void)
{
284
285
286
287
288
  struct mi_interp *mi = as_mi_interp (top_level_interpreter ());

  if (mi == NULL)
    return;

289
290
  /* If MI is sync, then output the MI prompt now, indicating we're
     ready for further input.  */
291
  if (!mi_async_p ())
292
    display_mi_prompt (mi);
293
294
}

Jan Kratochvil's avatar
gdb/    
Jan Kratochvil committed
295
296
297
/* mi_execute_command_wrapper wrapper suitable for INPUT_HANDLER.  */

static void
298
mi_execute_command_input_handler (gdb::unique_xmalloc_ptr<char> &&cmd)
Jan Kratochvil's avatar
gdb/    
Jan Kratochvil committed
299
{
300
  struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
301
302
303
  struct ui *ui = current_ui;

  ui->prompt_state = PROMPT_NEEDED;
304

305
  mi_execute_command_wrapper (cmd.get ());
Jan Kratochvil's avatar
gdb/    
Jan Kratochvil committed
306

307
308
309
310
311
  /* Print a prompt, indicating we're ready for further input, unless
     we just started a synchronous command.  In that case, we're about
     to go back to the event loop and will output the prompt in the
     'synchronous_command_done' observer when the target next
     stops.  */
312
  if (ui->prompt_state == PROMPT_NEEDED)
313
    display_mi_prompt (mi);
Jan Kratochvil's avatar
gdb/    
Jan Kratochvil committed
314
315
}

316
317
void
mi_interp::pre_command_loop ()
318
{
319
  struct mi_interp *mi = this;
320

321
  /* Turn off 8 bit strings in quoted output.  Any character with the
322
     high bit set is printed using C's octal format.  */
323
  sevenbit_strings = 1;
324
325

  /* Tell the world that we're alive.  */
326
  display_mi_prompt (mi);
327
328
}

329
330
331
static void
mi_new_thread (struct thread_info *t)
{
332
  SWITCH_THRU_ALL_UIS ()
333
334
    {
      struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
335

336
337
      if (mi == NULL)
	continue;
338

339
340
      target_terminal::scoped_restore_terminal_state term_state;
      target_terminal::ours_for_output ();
341
342
343

      fprintf_unfiltered (mi->event_channel,
			  "thread-created,id=\"%d\",group-id=\"i%d\"",
344
			  t->global_num, t->inf->num);
345
346
      gdb_flush (mi->event_channel);
    }
347
348
}

349
static void
Pedro Alves's avatar
gdb/    
Pedro Alves committed
350
mi_thread_exit (struct thread_info *t, int silent)
351
{
Pedro Alves's avatar
gdb/    
Pedro Alves committed
352
353
354
  if (silent)
    return;

355
  SWITCH_THRU_ALL_UIS ()
356
357
    {
      struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
Vladimir Prus's avatar
Vladimir Prus committed
358

359
360
      if (mi == NULL)
	continue;
361

362
363
      target_terminal::scoped_restore_terminal_state term_state;
      target_terminal::ours_for_output ();
364
365
366
367
368
      fprintf_unfiltered (mi->event_channel,
			  "thread-exited,id=\"%d\",group-id=\"i%d\"",
			  t->global_num, t->inf->num);
      gdb_flush (mi->event_channel);
    }
369
370
}

Yao Qi's avatar
Yao Qi committed
371
372
373
/* Emit notification on changing the state of record.  */

static void
374
375
mi_record_changed (struct inferior *inferior, int started, const char *method,
		   const char *format)
Yao Qi's avatar
Yao Qi committed
376
{
377
  SWITCH_THRU_ALL_UIS ()
378
    {
379
380
381
382
383
      struct mi_interp *mi = as_mi_interp (top_level_interpreter ());

      if (mi == NULL)
	continue;

384
385
      target_terminal::scoped_restore_terminal_state term_state;
      target_terminal::ours_for_output ();
386
387

      if (started)
388
	{
389
390
391
392
393
394
395
396
397
398
399
400
401
402
	  if (format != NULL)
	    {
	      fprintf_unfiltered (mi->event_channel,
				  "record-started,thread-group=\"i%d\","
				  "method=\"%s\",format=\"%s\"",
				  inferior->num, method, format);
	    }
	  else
	    {
	      fprintf_unfiltered (mi->event_channel,
				  "record-started,thread-group=\"i%d\","
				  "method=\"%s\"",
				  inferior->num, method);
	    }
403
	}
404
      else
405
	{
406
407
408
	  fprintf_unfiltered (mi->event_channel,
			      "record-stopped,thread-group=\"i%d\"",
			      inferior->num);
409
	}
410

411
412
      gdb_flush (mi->event_channel);
    }
Yao Qi's avatar
Yao Qi committed
413
414
}

Vladimir Prus's avatar
Vladimir Prus committed
415
416
417
static void
mi_inferior_added (struct inferior *inf)
{
418
  SWITCH_THRU_ALL_UIS ()
419
420
421
    {
      struct interp *interp;
      struct mi_interp *mi;
422

423
424
425
426
427
      /* We'll be called once for the initial inferior, before the top
	 level interpreter is set.  */
      interp = top_level_interpreter ();
      if (interp == NULL)
	continue;
428

429
430
431
432
      mi = as_mi_interp (interp);
      if (mi == NULL)
	continue;

433
434
      target_terminal::scoped_restore_terminal_state term_state;
      target_terminal::ours_for_output ();
435
436
437
438
439
440

      fprintf_unfiltered (mi->event_channel,
			  "thread-group-added,id=\"i%d\"",
			  inf->num);
      gdb_flush (mi->event_channel);
    }
Vladimir Prus's avatar
Vladimir Prus committed
441
442
443
444
}

static void
mi_inferior_appeared (struct inferior *inf)
445
{
446
  SWITCH_THRU_ALL_UIS ()
447
448
    {
      struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
449

450
451
      if (mi == NULL)
	continue;
452

453
454
      target_terminal::scoped_restore_terminal_state term_state;
      target_terminal::ours_for_output ();
455
456
457
458
459
460

      fprintf_unfiltered (mi->event_channel,
			  "thread-group-started,id=\"i%d\",pid=\"%d\"",
			  inf->num, inf->pid);
      gdb_flush (mi->event_channel);
    }
461
462
463
}

static void
Vladimir Prus's avatar
Vladimir Prus committed
464
mi_inferior_exit (struct inferior *inf)
465
{
466
  SWITCH_THRU_ALL_UIS ()
467
468
    {
      struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
469

470
471
      if (mi == NULL)
	continue;
Tom Tromey's avatar
gdb    
Tom Tromey committed
472

473
474
      target_terminal::scoped_restore_terminal_state term_state;
      target_terminal::ours_for_output ();
475
476
477
478
479
480
481
482
483
484
485

      if (inf->has_exit_code)
	fprintf_unfiltered (mi->event_channel,
			    "thread-group-exited,id=\"i%d\",exit-code=\"%s\"",
			    inf->num, int_string (inf->exit_code, 8, 0, 0, 1));
      else
	fprintf_unfiltered (mi->event_channel,
			    "thread-group-exited,id=\"i%d\"", inf->num);

      gdb_flush (mi->event_channel);
    }
486
487
}

Vladimir Prus's avatar
Vladimir Prus committed
488
489
490
static void
mi_inferior_removed (struct inferior *inf)
{
491
  SWITCH_THRU_ALL_UIS ()
492
493
    {
      struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
494

495
496
      if (mi == NULL)
	continue;
497

498
499
      target_terminal::scoped_restore_terminal_state term_state;
      target_terminal::ours_for_output ();
500
501
502
503
504
505

      fprintf_unfiltered (mi->event_channel,
			  "thread-group-removed,id=\"i%d\"",
			  inf->num);
      gdb_flush (mi->event_channel);
    }
Vladimir Prus's avatar
Vladimir Prus committed
506
507
}

508
509
510
511
/* Return the MI interpreter, if it is active -- either because it's
   the top-level interpreter or the interpreter executing the current
   command.  Returns NULL if the MI interpreter is not being used.  */

512
513
static struct mi_interp *
find_mi_interp (void)
514
{
515
  struct mi_interp *mi;
516

517
518
519
  mi = as_mi_interp (top_level_interpreter ());
  if (mi != NULL)
    return mi;
520

521
522
523
  mi = as_mi_interp (command_interp ());
  if (mi != NULL)
    return mi;
524
525
526
527
528

  return NULL;
}

/* Observers for several run control events that print why the
Tom Tromey's avatar
Tom Tromey committed
529
   inferior has stopped to both the MI event channel and to the MI
530
531
532
533
534
535
536
   console.  If the MI interpreter is not active, print nothing.  */

/* Observer for the signal_received notification.  */

static void
mi_on_signal_received (enum gdb_signal siggnal)
{
537
  SWITCH_THRU_ALL_UIS ()
538
539
540
541
542
    {
      struct mi_interp *mi = find_mi_interp ();

      if (mi == NULL)
	continue;
543

544
545
546
      print_signal_received_reason (mi->mi_uiout, siggnal);
      print_signal_received_reason (mi->cli_uiout, siggnal);
    }
547
548
549
550
551
552
553
}

/* Observer for the end_stepping_range notification.  */

static void
mi_on_end_stepping_range (void)
{
554
  SWITCH_THRU_ALL_UIS ()
555
556
557
558
559
    {
      struct mi_interp *mi = find_mi_interp ();

      if (mi == NULL)
	continue;
560

561
562
563
      print_end_stepping_range_reason (mi->mi_uiout);
      print_end_stepping_range_reason (mi->cli_uiout);
    }
564
565
566
}

/* Observer for the signal_exited notification.  */
567
568

static void
569
mi_on_signal_exited (enum gdb_signal siggnal)
570
{
571
  SWITCH_THRU_ALL_UIS ()
572
573
574
575
576
    {
      struct mi_interp *mi = find_mi_interp ();

      if (mi == NULL)
	continue;
577

578
579
580
      print_signal_exited_reason (mi->mi_uiout, siggnal);
      print_signal_exited_reason (mi->cli_uiout, siggnal);
    }
581
582
583
584
585
586
587
}

/* Observer for the exited notification.  */

static void
mi_on_exited (int exitstatus)
{
588
  SWITCH_THRU_ALL_UIS ()
589
590
591
592
593
    {
      struct mi_interp *mi = find_mi_interp ();

      if (mi == NULL)
	continue;
594

595
596
597
      print_exited_reason (mi->mi_uiout, exitstatus);
      print_exited_reason (mi->cli_uiout, exitstatus);
    }
598
599
600
601
602
603
604
}

/* Observer for the no_history notification.  */

static void
mi_on_no_history (void)
{
605
  SWITCH_THRU_ALL_UIS ()
606
607
    {
      struct mi_interp *mi = find_mi_interp ();
608

609
610
611
612
613
614
      if (mi == NULL)
	continue;

      print_no_history_reason (mi->mi_uiout);
      print_no_history_reason (mi->cli_uiout);
    }
615
616
}

617
static void
618
mi_on_normal_stop_1 (struct bpstats *bs, int print_frame)
619
620
621
622
{
  /* Since this can be called when CLI command is executing,
     using cli interpreter, be sure to use MI uiout for output,
     not the current one.  */
Tom Tromey's avatar
Tom Tromey committed
623
  struct ui_out *mi_uiout = top_level_interpreter ()->interp_ui_out ();
624
  struct mi_interp *mi = (struct mi_interp *) top_level_interpreter ();
625

626
627
  if (print_frame)
    {
628
      struct thread_info *tp;
Vladimir Prus's avatar
Vladimir Prus committed
629
      int core;
630
      struct interp *console_interp;
631

632
      tp = inferior_thread ();
Tom Tromey's avatar
gdb    
Tom Tromey committed
633

634
635
636
637
      if (tp->thread_fsm != NULL
	  && thread_fsm_finished_p (tp->thread_fsm))
	{
	  enum async_reply_reason reason;
Tom Tromey's avatar
gdb    
Tom Tromey committed
638

639
	  reason = thread_fsm_async_reply_reason (tp->thread_fsm);
Simon Marchi's avatar
Simon Marchi committed
640
	  mi_uiout->field_string ("reason", async_reason_lookup (reason));
641
	}
642
643
      print_stop_event (mi_uiout);

644
645
      console_interp = interp_lookup (current_ui, INTERP_CONSOLE);
      if (should_print_stop_to_console (console_interp, tp))
646
	print_stop_event (mi->cli_uiout);
647

Simon Marchi's avatar
Simon Marchi committed
648
      mi_uiout->field_int ("thread-id", tp->global_num);
649
650
      if (non_stop)
	{
Tom Tromey's avatar
Tom Tromey committed
651
	  ui_out_emit_list list_emitter (mi_uiout, "stopped-threads");
652

Simon Marchi's avatar
Simon Marchi committed
653
	  mi_uiout->field_int (NULL, tp->global_num);
654
655
	}
      else
Simon Marchi's avatar
Simon Marchi committed
656
	mi_uiout->field_string ("stopped-threads", "all");
Vladimir Prus's avatar
Vladimir Prus committed
657

658
      core = target_core_of_thread (tp->ptid);
Vladimir Prus's avatar
Vladimir Prus committed
659
      if (core != -1)
Simon Marchi's avatar
Simon Marchi committed
660
	mi_uiout->field_int ("core", core);
661
662
    }
  
663
664
  fputs_unfiltered ("*stopped", mi->raw_stdout);
  mi_out_put (mi_uiout, mi->raw_stdout);
665
  mi_out_rewind (mi_uiout);
666
667
668
  mi_print_timing_maybe (mi->raw_stdout);
  fputs_unfiltered ("\n", mi->raw_stdout);
  gdb_flush (mi->raw_stdout);
669
670
}

671
672
673
static void
mi_on_normal_stop (struct bpstats *bs, int print_frame)
{
674
  SWITCH_THRU_ALL_UIS ()
675
676
677
678
679
680
681
682
    {
      if (as_mi_interp (top_level_interpreter ()) == NULL)
	continue;

      mi_on_normal_stop_1 (bs, print_frame);
    }
}

Pedro Alves's avatar
gdb/    
Pedro Alves committed
683
684
685
686
687
static void
mi_about_to_proceed (void)
{
  /* Suppress output while calling an inferior function.  */

Tom Tromey's avatar
Tom Tromey committed
688
  if (inferior_ptid != null_ptid)
Pedro Alves's avatar
gdb/    
Pedro Alves committed
689
690
    {
      struct thread_info *tp = inferior_thread ();
691

Jan Kratochvil's avatar
gdb/    
Jan Kratochvil committed
692
      if (tp->control.in_infcall)
Pedro Alves's avatar
gdb/    
Pedro Alves committed
693
694
695
696
697
698
	return;
    }

  mi_proceeded = 1;
}

Yao Qi's avatar
gdb/    
Yao Qi committed
699
700
/* When the element is non-zero, no MI notifications will be emitted in
   response to the corresponding observers.  */
701

Yao Qi's avatar
gdb/    
Yao Qi committed
702
703
704
705
struct mi_suppress_notification mi_suppress_notification =
  {
    0,
    0,
Yao Qi's avatar
Yao Qi committed
706
    0,
707
    0,
Yao Qi's avatar
gdb/    
Yao Qi committed
708
  };
709

Yao Qi's avatar
Yao Qi committed
710
711
712
713
714
715
716
717
/* Emit notification on changing a traceframe.  */

static void
mi_traceframe_changed (int tfnum, int tpnum)
{
  if (mi_suppress_notification.traceframe)
    return;

718
  SWITCH_THRU_ALL_UIS ()
719
720
    {
      struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
Yao Qi's avatar
Yao Qi committed
721

722
723
      if (mi == NULL)
	continue;
Yao Qi's avatar
Yao Qi committed
724

725
726
      target_terminal::scoped_restore_terminal_state term_state;
      target_terminal::ours_for_output ();
727

728
729
730
731
732
733
734
735
736
      if (tfnum >= 0)
	fprintf_unfiltered (mi->event_channel, "traceframe-changed,"
			    "num=\"%d\",tracepoint=\"%d\"\n",
			    tfnum, tpnum);
      else
	fprintf_unfiltered (mi->event_channel, "traceframe-changed,end");

      gdb_flush (mi->event_channel);
    }
Yao Qi's avatar
Yao Qi committed
737
738
}

Yao Qi's avatar
Yao Qi committed
739
740
741
/* Emit notification on creating a trace state variable.  */

static void
Yao Qi's avatar
Yao Qi committed
742
mi_tsv_created (const struct trace_state_variable *tsv)
Yao Qi's avatar
Yao Qi committed
743
{
744
  SWITCH_THRU_ALL_UIS ()
745
746
    {
      struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
Yao Qi's avatar
Yao Qi committed
747

748
749
      if (mi == NULL)
	continue;
Yao Qi's avatar
Yao Qi committed
750

751
752
      target_terminal::scoped_restore_terminal_state term_state;
      target_terminal::ours_for_output ();
753

754
755
      fprintf_unfiltered (mi->event_channel, "tsv-created,"
			  "name=\"%s\",initial=\"%s\"\n",
756
			  tsv->name.c_str (), plongest (tsv->initial_value));
757
758
759

      gdb_flush (mi->event_channel);
    }
Yao Qi's avatar
Yao Qi committed
760
761
762
763
764
}

/* Emit notification on deleting a trace state variable.  */

static void
Yao Qi's avatar
Yao Qi committed
765
mi_tsv_deleted (const struct trace_state_variable *tsv)
Yao Qi's avatar
Yao Qi committed
766
{
767
  SWITCH_THRU_ALL_UIS ()
768
769
    {
      struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
Yao Qi's avatar
Yao Qi committed
770

771
772
      if (mi == NULL)
	continue;
Yao Qi's avatar
Yao Qi committed
773

774
775
      target_terminal::scoped_restore_terminal_state term_state;
      target_terminal::ours_for_output ();
776

777
778
      if (tsv != NULL)
	fprintf_unfiltered (mi->event_channel, "tsv-deleted,"
779
			    "name=\"%s\"\n", tsv->name.c_str ());
780
781
782
783
784
      else
	fprintf_unfiltered (mi->event_channel, "tsv-deleted\n");

      gdb_flush (mi->event_channel);
    }
Yao Qi's avatar
Yao Qi committed
785
786
}

Yao Qi's avatar
Yao Qi committed
787
788
789
790
791
/* Emit notification on modifying a trace state variable.  */

static void
mi_tsv_modified (const struct trace_state_variable *tsv)
{
792
  SWITCH_THRU_ALL_UIS ()
793
794
795
    {
      struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
      struct ui_out *mi_uiout;
Yao Qi's avatar
Yao Qi committed
796

797
798
      if (mi == NULL)
	continue;
Yao Qi's avatar
Yao Qi committed
799

Tom Tromey's avatar
Tom Tromey committed
800
      mi_uiout = top_level_interpreter ()->interp_ui_out ();
Yao Qi's avatar
Yao Qi committed
801

802
803
      target_terminal::scoped_restore_terminal_state term_state;
      target_terminal::ours_for_output ();
Yao Qi's avatar
Yao Qi committed
804

805
806
      fprintf_unfiltered (mi->event_channel,
			  "tsv-modified");
Yao Qi's avatar
Yao Qi committed
807

Simon Marchi's avatar
Simon Marchi committed
808
      mi_uiout->redirect (mi->event_channel);
809

Simon Marchi's avatar
Simon Marchi committed
810
811
      mi_uiout->field_string ("name", tsv->name);
      mi_uiout->field_string ("initial",
812
813
			   plongest (tsv->initial_value));
      if (tsv->value_known)
Simon Marchi's avatar
Simon Marchi committed
814
	mi_uiout->field_string ("current", plongest (tsv->value));
815

Simon Marchi's avatar
Simon Marchi committed
816
      mi_uiout->redirect (NULL);
817
818
819

      gdb_flush (mi->event_channel);
    }
Yao Qi's avatar
Yao Qi committed
820
821
}

822
823
824
825
826
/* Print breakpoint BP on MI's event channel.  */

static void
mi_print_breakpoint_for_event (struct mi_interp *mi, breakpoint *bp)
{
Tom Tromey's avatar
Tom Tromey committed
827
  ui_out *mi_uiout = mi->interp_ui_out ();
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853

  /* We want the output from print_breakpoint to go to
     mi->event_channel.  One approach would be to just call
     print_breakpoint, and then use mi_out_put to send the current
     content of mi_uiout into mi->event_channel.  However, that will
     break if anything is output to mi_uiout prior to calling the
     breakpoint_created notifications.  So, we use
     ui_out_redirect.  */
  mi_uiout->redirect (mi->event_channel);

  TRY
    {
      scoped_restore restore_uiout
	= make_scoped_restore (&current_uiout, mi_uiout);

      print_breakpoint (bp);
    }
  CATCH (ex, RETURN_MASK_ALL)
    {
      exception_print (gdb_stderr, ex);
    }
  END_CATCH

  mi_uiout->redirect (NULL);
}

854
/* Emit notification about a created breakpoint.  */
855

856
857
858
static void
mi_breakpoint_created (struct breakpoint *b)
{
Yao Qi's avatar
gdb/    
Yao Qi committed
859
  if (mi_suppress_notification.breakpoint)
860
861
862
863
864
    return;

  if (b->number <= 0)
    return;

865
  SWITCH_THRU_ALL_UIS ()
Pedro Alves's avatar
Pedro Alves committed
866
    {
867
      struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
Pedro Alves's avatar
Pedro Alves committed
868

869
870
      if (mi == NULL)
	continue;
871

872
873
      target_terminal::scoped_restore_terminal_state term_state;
      target_terminal::ours_for_output ();
874
875
876

      fprintf_unfiltered (mi->event_channel,
			  "breakpoint-created");
877
      mi_print_breakpoint_for_event (mi, b);
878
879
880

      gdb_flush (mi->event_channel);
    }
881
882
883
}

/* Emit notification about deleted breakpoint.  */
884

885
886
887
static void
mi_breakpoint_deleted (struct breakpoint *b)
{
Yao Qi's avatar
gdb/    
Yao Qi committed
888
  if (mi_suppress_notification.breakpoint)
889
890
891
892
893
    return;

  if (b->number <= 0)
    return;

894
  SWITCH_THRU_ALL_UIS ()
895
896
    {
      struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
897

898
899
      if (mi == NULL)
	continue;
900

901
902
      target_terminal::scoped_restore_terminal_state term_state;
      target_terminal::ours_for_output ();
903

904
905
906
907
908
      fprintf_unfiltered (mi->event_channel, "breakpoint-deleted,id=\"%d\"",
			  b->number);

      gdb_flush (mi->event_channel);
    }
909
910
911
}

/* Emit notification about modified breakpoint.  */
912

913
914
915
static void
mi_breakpoint_modified (struct breakpoint *b)
{
Yao Qi's avatar
gdb/    
Yao Qi committed
916
  if (mi_suppress_notification.breakpoint)
917
918
919
920
921
    return;

  if (b->number <= 0)
    return;

922
  SWITCH_THRU_ALL_UIS ()
Pedro Alves's avatar
Pedro Alves committed
923
    {
924
      struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
Pedro Alves's avatar
Pedro Alves committed
925

926
927
      if (mi == NULL)
	continue;
928

929
930
      target_terminal::scoped_restore_terminal_state term_state;
      target_terminal::ours_for_output ();
931
932
      fprintf_unfiltered (mi->event_channel,
			  "breakpoint-modified");
933
      mi_print_breakpoint_for_event (mi, b);
934
935
936

      gdb_flush (mi->event_channel);
    }
937
938
}

939
940
static void
mi_output_running (struct thread_info *thread)
941
{
942
  SWITCH_THRU_ALL_UIS ()
943
944
945
946
947
948
    {
      struct mi_interp *mi = as_mi_interp (top_level_interpreter ());

      if (mi == NULL)
	continue;

949
950
951
      fprintf_unfiltered (mi->raw_stdout,
			  "*running,thread-id=\"%d\"\n",
			  thread->global_num);
952
    }
953
954
}

955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
/* Return true if there are multiple inferiors loaded.  This is used
   for backwards compatibility -- if there's only one inferior, output
   "all", otherwise, output each resumed thread individually.  */

static bool
multiple_inferiors_p ()
{
  int count = 0;
  for (inferior *inf ATTRIBUTE_UNUSED : all_non_exited_inferiors ())
    {
      count++;
      if (count > 1)
	return true;
    }

  return false;
}

Vladimir Prus's avatar
Vladimir Prus committed
973
static void
974
mi_on_resume_1 (struct mi_interp *mi, ptid_t ptid)
Vladimir Prus's avatar
Vladimir Prus committed
975
{
Vladimir Prus's avatar
Vladimir Prus committed
976
977
978
979
980
981
982
983
  /* To cater for older frontends, emit ^running, but do it only once
     per each command.  We do it here, since at this point we know
     that the target was successfully resumed, and in non-async mode,
     we won't return back to MI interpreter code until the target
     is done running, so delaying the output of "^running" until then
     will make it impossible for frontend to know what's going on.

     In future (MI3), we'll be outputting "^done" here.  */
Pedro Alves's avatar
gdb/    
Pedro Alves committed
984
  if (!running_result_record_printed && mi_proceeded)
Vladimir Prus's avatar
Vladimir Prus committed
985
    {
986
      fprintf_unfiltered (mi->raw_stdout, "%s^running\n",
987
			  current_token ? current_token : "");
Vladimir Prus's avatar
Vladimir Prus committed
988