Make raw_stdout be per MI instance
[external/binutils.git] / gdb / mi / mi-interp.c
1 /* MI Interpreter Definitions and Commands for GDB, the GNU debugger.
2
3    Copyright (C) 2002-2016 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    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    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    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 #include "defs.h"
21 #include "interps.h"
22 #include "event-top.h"
23 #include "event-loop.h"
24 #include "inferior.h"
25 #include "infrun.h"
26 #include "ui-out.h"
27 #include "top.h"
28 #include "mi-main.h"
29 #include "mi-cmds.h"
30 #include "mi-out.h"
31 #include "mi-console.h"
32 #include "mi-common.h"
33 #include "observer.h"
34 #include "gdbthread.h"
35 #include "solist.h"
36 #include "gdb.h"
37 #include "objfiles.h"
38 #include "tracepoint.h"
39 #include "cli-out.h"
40 #include "thread-fsm.h"
41
42 /* These are the interpreter setup, etc. functions for the MI
43    interpreter.  */
44
45 static void mi_execute_command_wrapper (const char *cmd);
46 static void mi_execute_command_input_handler (char *cmd);
47 static void mi_command_loop (void *data);
48
49 /* These are hooks that we put in place while doing interpreter_exec
50    so we can report interesting things that happened "behind the MI's
51    back" in this command.  */
52
53 static int mi_interp_query_hook (const char *ctlstr, va_list ap)
54   ATTRIBUTE_PRINTF (1, 0);
55
56 static void mi_insert_notify_hooks (void);
57 static void mi_remove_notify_hooks (void);
58
59 static void mi_on_signal_received (enum gdb_signal siggnal);
60 static void mi_on_end_stepping_range (void);
61 static void mi_on_signal_exited (enum gdb_signal siggnal);
62 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);
67 static void mi_thread_exit (struct thread_info *t, int silent);
68 static void mi_record_changed (struct inferior*, int, const char *,
69                                const char *);
70 static void mi_inferior_added (struct inferior *inf);
71 static void mi_inferior_appeared (struct inferior *inf);
72 static void mi_inferior_exit (struct inferior *inf);
73 static void mi_inferior_removed (struct inferior *inf);
74 static void mi_on_resume (ptid_t ptid);
75 static void mi_solib_loaded (struct so_list *solib);
76 static void mi_solib_unloaded (struct so_list *solib);
77 static void mi_about_to_proceed (void);
78 static void mi_traceframe_changed (int tfnum, int tpnum);
79 static void mi_tsv_created (const struct trace_state_variable *tsv);
80 static void mi_tsv_deleted (const struct trace_state_variable *tsv);
81 static void mi_tsv_modified (const struct trace_state_variable *tsv);
82 static void mi_breakpoint_created (struct breakpoint *b);
83 static void mi_breakpoint_deleted (struct breakpoint *b);
84 static void mi_breakpoint_modified (struct breakpoint *b);
85 static void mi_command_param_changed (const char *param, const char *value);
86 static void mi_memory_changed (struct inferior *inf, CORE_ADDR memaddr,
87                                ssize_t len, const bfd_byte *myaddr);
88 static void mi_on_sync_execution_done (void);
89
90 static int report_initial_inferior (struct inferior *inf, void *closure);
91
92 /* Display the MI prompt.  */
93
94 static void
95 display_mi_prompt (struct mi_interp *mi)
96 {
97   fputs_unfiltered ("(gdb) \n", mi->raw_stdout);
98   gdb_flush (mi->raw_stdout);
99 }
100
101 /* Returns the INTERP's data cast as mi_interp if INTERP is an MI, and
102    returns NULL otherwise.  */
103
104 static struct mi_interp *
105 as_mi_interp (struct interp *interp)
106 {
107   if (ui_out_is_mi_like_p (interp_ui_out (interp)))
108     return (struct mi_interp *) interp_data (interp);
109   return NULL;
110 }
111
112 static void *
113 mi_interpreter_init (struct interp *interp, int top_level)
114 {
115   struct mi_interp *mi = XNEW (struct mi_interp);
116   const char *name;
117   int mi_version;
118
119   /* Store the current output channel, so that we can create a console
120      channel that encapsulates and prefixes all gdb_output-type bits
121      coming from the rest of the debugger.  */
122   mi->raw_stdout = gdb_stdout;
123
124   /* Create MI console channels, each with a different prefix so they
125      can be distinguished.  */
126   mi->out = mi_console_file_new (mi->raw_stdout, "~", '"');
127   mi->err = mi_console_file_new (mi->raw_stdout, "&", '"');
128   mi->log = mi->err;
129   mi->targ = mi_console_file_new (mi->raw_stdout, "@", '"');
130   mi->event_channel = mi_console_file_new (mi->raw_stdout, "=", 0);
131
132   name = interp_name (interp);
133   /* INTERP_MI selects the most recent released version.  "mi2" was
134      released as part of GDB 6.0.  */
135   if (strcmp (name, INTERP_MI) == 0)
136     mi_version = 2;
137   else if (strcmp (name, INTERP_MI1) == 0)
138     mi_version = 1;
139   else if (strcmp (name, INTERP_MI2) == 0)
140     mi_version = 2;
141   else if (strcmp (name, INTERP_MI3) == 0)
142     mi_version = 3;
143   else
144     gdb_assert_not_reached ("unhandled MI version");
145
146   mi->mi_uiout = mi_out_new (mi_version);
147   mi->cli_uiout = cli_out_new (mi->out);
148
149   if (top_level)
150     {
151       /* The initial inferior is created before this function is
152          called, so we need to report it explicitly.  Use iteration in
153          case future version of GDB creates more than one inferior
154          up-front.  */
155       iterate_over_inferiors (report_initial_inferior, mi);
156     }
157
158   return mi;
159 }
160
161 static int
162 mi_interpreter_resume (void *data)
163 {
164   struct mi_interp *mi = (struct mi_interp *) data;
165   struct ui *ui = current_ui;
166
167   /* As per hack note in mi_interpreter_init, swap in the output
168      channels... */
169   gdb_setup_readline (0);
170
171   ui->call_readline = gdb_readline_no_editing_callback;
172   ui->input_handler = mi_execute_command_input_handler;
173   /* FIXME: This is a total hack for now.  PB's use of the MI
174      implicitly relies on a bug in the async support which allows
175      asynchronous commands to leak through the commmand loop.  The bug
176      involves (but is not limited to) the fact that sync_execution was
177      erroneously initialized to 0.  Duplicate by initializing it thus
178      here...  */
179   sync_execution = 0;
180
181   gdb_stdout = mi->out;
182   /* Route error and log output through the MI.  */
183   gdb_stderr = mi->err;
184   gdb_stdlog = mi->log;
185   /* Route target output through the MI.  */
186   gdb_stdtarg = mi->targ;
187   /* Route target error through the MI as well.  */
188   gdb_stdtargerr = mi->targ;
189
190   /* Replace all the hooks that we know about.  There really needs to
191      be a better way of doing this... */
192   clear_interpreter_hooks ();
193
194   deprecated_show_load_progress = mi_load_progress;
195
196   return 1;
197 }
198
199 static int
200 mi_interpreter_suspend (void *data)
201 {
202   gdb_disable_readline ();
203   return 1;
204 }
205
206 static struct gdb_exception
207 mi_interpreter_exec (void *data, const char *command)
208 {
209   mi_execute_command_wrapper (command);
210   return exception_none;
211 }
212
213 void
214 mi_cmd_interpreter_exec (char *command, char **argv, int argc)
215 {
216   struct interp *interp_to_use;
217   int i;
218   char *mi_error_message = NULL;
219   struct cleanup *old_chain;
220
221   if (argc < 2)
222     error (_("-interpreter-exec: "
223              "Usage: -interpreter-exec interp command"));
224
225   interp_to_use = interp_lookup (current_ui, argv[0]);
226   if (interp_to_use == NULL)
227     error (_("-interpreter-exec: could not find interpreter \"%s\""),
228            argv[0]);
229
230   /* Note that unlike the CLI version of this command, we don't
231      actually set INTERP_TO_USE as the current interpreter, as we
232      still want gdb_stdout, etc. to point at MI streams.  */
233
234   /* Insert the MI out hooks, making sure to also call the
235      interpreter's hooks if it has any.  */
236   /* KRS: We shouldn't need this... Events should be installed and
237      they should just ALWAYS fire something out down the MI
238      channel.  */
239   mi_insert_notify_hooks ();
240
241   /* Now run the code.  */
242
243   old_chain = make_cleanup (null_cleanup, 0);
244   for (i = 1; i < argc; i++)
245     {
246       struct gdb_exception e = interp_exec (interp_to_use, argv[i]);
247
248       if (e.reason < 0)
249         {
250           mi_error_message = xstrdup (e.message);
251           make_cleanup (xfree, mi_error_message);
252           break;
253         }
254     }
255
256   mi_remove_notify_hooks ();
257
258   if (mi_error_message != NULL)
259     error ("%s", mi_error_message);
260   do_cleanups (old_chain);
261 }
262
263 /* This inserts a number of hooks that are meant to produce
264    async-notify ("=") MI messages while running commands in another
265    interpreter using mi_interpreter_exec.  The canonical use for this
266    is to allow access to the gdb CLI interpreter from within the MI,
267    while still producing MI style output when actions in the CLI
268    command change GDB's state.  */
269
270 static void
271 mi_insert_notify_hooks (void)
272 {
273   deprecated_query_hook = mi_interp_query_hook;
274 }
275
276 static void
277 mi_remove_notify_hooks (void)
278 {
279   deprecated_query_hook = NULL;
280 }
281
282 static int
283 mi_interp_query_hook (const char *ctlstr, va_list ap)
284 {
285   return 1;
286 }
287
288 static void
289 mi_execute_command_wrapper (const char *cmd)
290 {
291   struct ui *ui = current_ui;
292
293   mi_execute_command (cmd, stdin == ui->instream);
294 }
295
296 /* Observer for the synchronous_command_done notification.  */
297
298 static void
299 mi_on_sync_execution_done (void)
300 {
301   struct ui *ui = current_ui;
302   struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
303
304   if (mi == NULL)
305     return;
306
307   /* If MI is sync, then output the MI prompt now, indicating we're
308      ready for further input.  */
309   if (!mi_async_p ())
310     display_mi_prompt (mi);
311 }
312
313 /* mi_execute_command_wrapper wrapper suitable for INPUT_HANDLER.  */
314
315 static void
316 mi_execute_command_input_handler (char *cmd)
317 {
318   struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
319
320   mi_execute_command_wrapper (cmd);
321
322   /* Print a prompt, indicating we're ready for further input, unless
323      we just started a synchronous command.  In that case, we're about
324      to go back to the event loop and will output the prompt in the
325      'synchronous_command_done' observer when the target next
326      stops.  */
327   if (!sync_execution)
328     display_mi_prompt (mi);
329 }
330
331 static void
332 mi_command_loop (void *data)
333 {
334   struct mi_interp *mi = (struct mi_interp *) data;
335
336   /* Turn off 8 bit strings in quoted output.  Any character with the
337      high bit set is printed using C's octal format.  */
338   sevenbit_strings = 1;
339
340   /* Tell the world that we're alive.  */
341   display_mi_prompt (mi);
342
343   start_event_loop ();
344 }
345
346 static void
347 mi_new_thread (struct thread_info *t)
348 {
349   struct inferior *inf = find_inferior_ptid (t->ptid);
350   struct switch_thru_all_uis state;
351
352   gdb_assert (inf);
353
354   SWITCH_THRU_ALL_UIS (state)
355     {
356       struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
357       struct cleanup *old_chain;
358
359       if (mi == NULL)
360         continue;
361
362       old_chain = make_cleanup_restore_target_terminal ();
363       target_terminal_ours_for_output ();
364
365       fprintf_unfiltered (mi->event_channel,
366                           "thread-created,id=\"%d\",group-id=\"i%d\"",
367                           t->global_num, inf->num);
368       gdb_flush (mi->event_channel);
369
370       do_cleanups (old_chain);
371     }
372 }
373
374 static void
375 mi_thread_exit (struct thread_info *t, int silent)
376 {
377   struct switch_thru_all_uis state;
378
379   if (silent)
380     return;
381
382   SWITCH_THRU_ALL_UIS (state)
383     {
384       struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
385       struct cleanup *old_chain;
386
387       if (mi == NULL)
388         continue;
389
390       old_chain = make_cleanup_restore_target_terminal ();
391       target_terminal_ours_for_output ();
392       fprintf_unfiltered (mi->event_channel,
393                           "thread-exited,id=\"%d\",group-id=\"i%d\"",
394                           t->global_num, t->inf->num);
395       gdb_flush (mi->event_channel);
396
397       do_cleanups (old_chain);
398     }
399 }
400
401 /* Emit notification on changing the state of record.  */
402
403 static void
404 mi_record_changed (struct inferior *inferior, int started, const char *method,
405                    const char *format)
406 {
407   struct switch_thru_all_uis state;
408
409   SWITCH_THRU_ALL_UIS (state)
410     {
411       struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
412       struct cleanup *old_chain;
413
414       if (mi == NULL)
415         continue;
416
417       old_chain = make_cleanup_restore_target_terminal ();
418       target_terminal_ours_for_output ();
419
420       if (started)
421         {
422           if (format != NULL)
423             {
424               fprintf_unfiltered (mi->event_channel,
425                                   "record-started,thread-group=\"i%d\","
426                                   "method=\"%s\",format=\"%s\"",
427                                   inferior->num, method, format);
428             }
429           else
430             {
431               fprintf_unfiltered (mi->event_channel,
432                                   "record-started,thread-group=\"i%d\","
433                                   "method=\"%s\"",
434                                   inferior->num, method);
435             }
436         }
437       else
438         {
439           fprintf_unfiltered (mi->event_channel,
440                               "record-stopped,thread-group=\"i%d\"",
441                               inferior->num);
442         }
443
444       gdb_flush (mi->event_channel);
445
446       do_cleanups (old_chain);
447     }
448 }
449
450 static void
451 mi_inferior_added (struct inferior *inf)
452 {
453   struct switch_thru_all_uis state;
454
455   SWITCH_THRU_ALL_UIS (state)
456     {
457       struct interp *interp;
458       struct mi_interp *mi;
459       struct cleanup *old_chain;
460
461       /* We'll be called once for the initial inferior, before the top
462          level interpreter is set.  */
463       interp = top_level_interpreter ();
464       if (interp == NULL)
465         continue;
466
467       mi = as_mi_interp (interp);
468       if (mi == NULL)
469         continue;
470
471       old_chain = make_cleanup_restore_target_terminal ();
472       target_terminal_ours_for_output ();
473
474       fprintf_unfiltered (mi->event_channel,
475                           "thread-group-added,id=\"i%d\"",
476                           inf->num);
477       gdb_flush (mi->event_channel);
478
479       do_cleanups (old_chain);
480     }
481 }
482
483 static void
484 mi_inferior_appeared (struct inferior *inf)
485 {
486   struct switch_thru_all_uis state;
487
488   SWITCH_THRU_ALL_UIS (state)
489     {
490       struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
491       struct cleanup *old_chain;
492
493       if (mi == NULL)
494         continue;
495
496       old_chain = make_cleanup_restore_target_terminal ();
497       target_terminal_ours_for_output ();
498
499       fprintf_unfiltered (mi->event_channel,
500                           "thread-group-started,id=\"i%d\",pid=\"%d\"",
501                           inf->num, inf->pid);
502       gdb_flush (mi->event_channel);
503       do_cleanups (old_chain);
504     }
505 }
506
507 static void
508 mi_inferior_exit (struct inferior *inf)
509 {
510   struct switch_thru_all_uis state;
511
512   SWITCH_THRU_ALL_UIS (state)
513     {
514       struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
515       struct cleanup *old_chain;
516
517       if (mi == NULL)
518         continue;
519
520       old_chain = make_cleanup_restore_target_terminal ();
521       target_terminal_ours_for_output ();
522
523       if (inf->has_exit_code)
524         fprintf_unfiltered (mi->event_channel,
525                             "thread-group-exited,id=\"i%d\",exit-code=\"%s\"",
526                             inf->num, int_string (inf->exit_code, 8, 0, 0, 1));
527       else
528         fprintf_unfiltered (mi->event_channel,
529                             "thread-group-exited,id=\"i%d\"", inf->num);
530
531       gdb_flush (mi->event_channel);
532       do_cleanups (old_chain);
533     }
534 }
535
536 static void
537 mi_inferior_removed (struct inferior *inf)
538 {
539   struct switch_thru_all_uis state;
540
541   SWITCH_THRU_ALL_UIS (state)
542     {
543       struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
544       struct cleanup *old_chain;
545
546       if (mi == NULL)
547         continue;
548
549       old_chain = make_cleanup_restore_target_terminal ();
550       target_terminal_ours_for_output ();
551
552       fprintf_unfiltered (mi->event_channel,
553                           "thread-group-removed,id=\"i%d\"",
554                           inf->num);
555       gdb_flush (mi->event_channel);
556
557       do_cleanups (old_chain);
558     }
559 }
560
561 /* Return the MI interpreter, if it is active -- either because it's
562    the top-level interpreter or the interpreter executing the current
563    command.  Returns NULL if the MI interpreter is not being used.  */
564
565 static struct mi_interp *
566 find_mi_interp (void)
567 {
568   struct mi_interp *mi;
569
570   mi = as_mi_interp (top_level_interpreter ());
571   if (mi != NULL)
572     return mi;
573
574   mi = as_mi_interp (command_interp ());
575   if (mi != NULL)
576     return mi;
577
578   return NULL;
579 }
580
581 /* Observers for several run control events that print why the
582    inferior has stopped to both the the MI event channel and to the MI
583    console.  If the MI interpreter is not active, print nothing.  */
584
585 /* Observer for the signal_received notification.  */
586
587 static void
588 mi_on_signal_received (enum gdb_signal siggnal)
589 {
590   struct switch_thru_all_uis state;
591
592   SWITCH_THRU_ALL_UIS (state)
593     {
594       struct mi_interp *mi = find_mi_interp ();
595
596       if (mi == NULL)
597         continue;
598
599       print_signal_received_reason (mi->mi_uiout, siggnal);
600       print_signal_received_reason (mi->cli_uiout, siggnal);
601     }
602 }
603
604 /* Observer for the end_stepping_range notification.  */
605
606 static void
607 mi_on_end_stepping_range (void)
608 {
609   struct switch_thru_all_uis state;
610
611   SWITCH_THRU_ALL_UIS (state)
612     {
613       struct mi_interp *mi = find_mi_interp ();
614
615       if (mi == NULL)
616         continue;
617
618       print_end_stepping_range_reason (mi->mi_uiout);
619       print_end_stepping_range_reason (mi->cli_uiout);
620     }
621 }
622
623 /* Observer for the signal_exited notification.  */
624
625 static void
626 mi_on_signal_exited (enum gdb_signal siggnal)
627 {
628   struct switch_thru_all_uis state;
629
630   SWITCH_THRU_ALL_UIS (state)
631     {
632       struct mi_interp *mi = find_mi_interp ();
633
634       if (mi == NULL)
635         continue;
636
637       print_signal_exited_reason (mi->mi_uiout, siggnal);
638       print_signal_exited_reason (mi->cli_uiout, siggnal);
639     }
640 }
641
642 /* Observer for the exited notification.  */
643
644 static void
645 mi_on_exited (int exitstatus)
646 {
647   struct switch_thru_all_uis state;
648
649   SWITCH_THRU_ALL_UIS (state)
650     {
651       struct mi_interp *mi = find_mi_interp ();
652
653       if (mi == NULL)
654         continue;
655
656       print_exited_reason (mi->mi_uiout, exitstatus);
657       print_exited_reason (mi->cli_uiout, exitstatus);
658     }
659 }
660
661 /* Observer for the no_history notification.  */
662
663 static void
664 mi_on_no_history (void)
665 {
666   struct switch_thru_all_uis state;
667
668   SWITCH_THRU_ALL_UIS (state)
669     {
670       struct mi_interp *mi = find_mi_interp ();
671
672       if (mi == NULL)
673         continue;
674
675       print_no_history_reason (mi->mi_uiout);
676       print_no_history_reason (mi->cli_uiout);
677     }
678 }
679
680 static void
681 mi_on_normal_stop_1 (struct bpstats *bs, int print_frame)
682 {
683   /* Since this can be called when CLI command is executing,
684      using cli interpreter, be sure to use MI uiout for output,
685      not the current one.  */
686   struct ui_out *mi_uiout = interp_ui_out (top_level_interpreter ());
687   struct mi_interp *mi = (struct mi_interp *) top_level_interpreter_data ();
688
689   if (print_frame)
690     {
691       struct thread_info *tp;
692       int core;
693
694       tp = inferior_thread ();
695
696       if (tp->thread_fsm != NULL
697           && thread_fsm_finished_p (tp->thread_fsm))
698         {
699           enum async_reply_reason reason;
700
701           reason = thread_fsm_async_reply_reason (tp->thread_fsm);
702           ui_out_field_string (mi_uiout, "reason",
703                                async_reason_lookup (reason));
704         }
705       print_stop_event (mi_uiout);
706
707       /* Breakpoint hits should always be mirrored to the console.
708          Deciding what to mirror to the console wrt to breakpoints and
709          random stops gets messy real fast.  E.g., say "s" trips on a
710          breakpoint.  We'd clearly want to mirror the event to the
711          console in this case.  But what about more complicated cases
712          like "s&; thread n; s&", and one of those steps spawning a
713          new thread, and that thread hitting a breakpoint?  It's
714          impossible in general to track whether the thread had any
715          relation to the commands that had been executed.  So we just
716          simplify and always mirror breakpoints and random events to
717          the console.
718
719          OTOH, we should print the source line to the console when
720          stepping or other similar commands, iff the step was started
721          by a console command, but not if it was started with
722          -exec-step or similar.  */
723       if ((bpstat_what (tp->control.stop_bpstat).main_action
724            == BPSTAT_WHAT_STOP_NOISY)
725           || !(tp->thread_fsm != NULL
726                && thread_fsm_finished_p (tp->thread_fsm))
727           || (tp->control.command_interp != NULL
728               && tp->control.command_interp != top_level_interpreter ()))
729         print_stop_event (mi->cli_uiout);
730
731       tp = inferior_thread ();
732       ui_out_field_int (mi_uiout, "thread-id", tp->global_num);
733       if (non_stop)
734         {
735           struct cleanup *back_to = make_cleanup_ui_out_list_begin_end 
736             (mi_uiout, "stopped-threads");
737
738           ui_out_field_int (mi_uiout, NULL, tp->global_num);
739           do_cleanups (back_to);
740         }
741       else
742         ui_out_field_string (mi_uiout, "stopped-threads", "all");
743
744       core = target_core_of_thread (inferior_ptid);
745       if (core != -1)
746         ui_out_field_int (mi_uiout, "core", core);
747     }
748   
749   fputs_unfiltered ("*stopped", mi->raw_stdout);
750   mi_out_put (mi_uiout, mi->raw_stdout);
751   mi_out_rewind (mi_uiout);
752   mi_print_timing_maybe (mi->raw_stdout);
753   fputs_unfiltered ("\n", mi->raw_stdout);
754   gdb_flush (mi->raw_stdout);
755 }
756
757 static void
758 mi_on_normal_stop (struct bpstats *bs, int print_frame)
759 {
760   struct switch_thru_all_uis state;
761
762   SWITCH_THRU_ALL_UIS (state)
763     {
764       if (as_mi_interp (top_level_interpreter ()) == NULL)
765         continue;
766
767       mi_on_normal_stop_1 (bs, print_frame);
768     }
769 }
770
771 static void
772 mi_about_to_proceed (void)
773 {
774   /* Suppress output while calling an inferior function.  */
775
776   if (!ptid_equal (inferior_ptid, null_ptid))
777     {
778       struct thread_info *tp = inferior_thread ();
779
780       if (tp->control.in_infcall)
781         return;
782     }
783
784   mi_proceeded = 1;
785 }
786
787 /* When the element is non-zero, no MI notifications will be emitted in
788    response to the corresponding observers.  */
789
790 struct mi_suppress_notification mi_suppress_notification =
791   {
792     0,
793     0,
794     0,
795   };
796
797 /* Emit notification on changing a traceframe.  */
798
799 static void
800 mi_traceframe_changed (int tfnum, int tpnum)
801 {
802   struct switch_thru_all_uis state;
803
804   if (mi_suppress_notification.traceframe)
805     return;
806
807   SWITCH_THRU_ALL_UIS (state)
808     {
809       struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
810       struct cleanup *old_chain;
811
812       if (mi == NULL)
813         continue;
814
815       old_chain = make_cleanup_restore_target_terminal ();
816       target_terminal_ours_for_output ();
817
818       if (tfnum >= 0)
819         fprintf_unfiltered (mi->event_channel, "traceframe-changed,"
820                             "num=\"%d\",tracepoint=\"%d\"\n",
821                             tfnum, tpnum);
822       else
823         fprintf_unfiltered (mi->event_channel, "traceframe-changed,end");
824
825       gdb_flush (mi->event_channel);
826
827       do_cleanups (old_chain);
828     }
829 }
830
831 /* Emit notification on creating a trace state variable.  */
832
833 static void
834 mi_tsv_created (const struct trace_state_variable *tsv)
835 {
836   struct switch_thru_all_uis state;
837
838   SWITCH_THRU_ALL_UIS (state)
839     {
840       struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
841       struct cleanup *old_chain;
842
843       if (mi == NULL)
844         continue;
845
846       old_chain = make_cleanup_restore_target_terminal ();
847       target_terminal_ours_for_output ();
848
849       fprintf_unfiltered (mi->event_channel, "tsv-created,"
850                           "name=\"%s\",initial=\"%s\"\n",
851                           tsv->name, plongest (tsv->initial_value));
852
853       gdb_flush (mi->event_channel);
854
855       do_cleanups (old_chain);
856     }
857 }
858
859 /* Emit notification on deleting a trace state variable.  */
860
861 static void
862 mi_tsv_deleted (const struct trace_state_variable *tsv)
863 {
864   struct switch_thru_all_uis state;
865
866   SWITCH_THRU_ALL_UIS (state)
867     {
868       struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
869       struct cleanup *old_chain;
870
871       if (mi == NULL)
872         continue;
873
874       old_chain = make_cleanup_restore_target_terminal ();
875       target_terminal_ours_for_output ();
876
877       if (tsv != NULL)
878         fprintf_unfiltered (mi->event_channel, "tsv-deleted,"
879                             "name=\"%s\"\n", tsv->name);
880       else
881         fprintf_unfiltered (mi->event_channel, "tsv-deleted\n");
882
883       gdb_flush (mi->event_channel);
884
885       do_cleanups (old_chain);
886     }
887 }
888
889 /* Emit notification on modifying a trace state variable.  */
890
891 static void
892 mi_tsv_modified (const struct trace_state_variable *tsv)
893 {
894   struct switch_thru_all_uis state;
895
896   SWITCH_THRU_ALL_UIS (state)
897     {
898       struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
899       struct ui_out *mi_uiout;
900       struct cleanup *old_chain;
901
902       if (mi == NULL)
903         continue;
904
905       mi_uiout = interp_ui_out (top_level_interpreter ());
906
907       old_chain = make_cleanup_restore_target_terminal ();
908       target_terminal_ours_for_output ();
909
910       fprintf_unfiltered (mi->event_channel,
911                           "tsv-modified");
912
913       ui_out_redirect (mi_uiout, mi->event_channel);
914
915       ui_out_field_string (mi_uiout, "name", tsv->name);
916       ui_out_field_string (mi_uiout, "initial",
917                            plongest (tsv->initial_value));
918       if (tsv->value_known)
919         ui_out_field_string (mi_uiout, "current", plongest (tsv->value));
920
921       ui_out_redirect (mi_uiout, NULL);
922
923       gdb_flush (mi->event_channel);
924
925       do_cleanups (old_chain);
926     }
927 }
928
929 /* Emit notification about a created breakpoint.  */
930
931 static void
932 mi_breakpoint_created (struct breakpoint *b)
933 {
934   struct switch_thru_all_uis state;
935
936   if (mi_suppress_notification.breakpoint)
937     return;
938
939   if (b->number <= 0)
940     return;
941
942   SWITCH_THRU_ALL_UIS (state)
943     {
944       struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
945       struct ui_out *mi_uiout;
946       struct cleanup *old_chain;
947
948       if (mi == NULL)
949         continue;
950
951       mi_uiout = interp_ui_out (top_level_interpreter ());
952
953       old_chain = make_cleanup_restore_target_terminal ();
954       target_terminal_ours_for_output ();
955
956       fprintf_unfiltered (mi->event_channel,
957                           "breakpoint-created");
958       /* We want the output from gdb_breakpoint_query to go to
959          mi->event_channel.  One approach would be to just call
960          gdb_breakpoint_query, and then use mi_out_put to send the current
961          content of mi_outout into mi->event_channel.  However, that will
962          break if anything is output to mi_uiout prior to calling the
963          breakpoint_created notifications.  So, we use
964          ui_out_redirect.  */
965       ui_out_redirect (mi_uiout, mi->event_channel);
966       TRY
967         {
968           gdb_breakpoint_query (mi_uiout, b->number, NULL);
969         }
970       CATCH (e, RETURN_MASK_ERROR)
971         {
972         }
973       END_CATCH
974
975       ui_out_redirect (mi_uiout, NULL);
976
977       gdb_flush (mi->event_channel);
978
979       do_cleanups (old_chain);
980     }
981 }
982
983 /* Emit notification about deleted breakpoint.  */
984
985 static void
986 mi_breakpoint_deleted (struct breakpoint *b)
987 {
988   struct switch_thru_all_uis state;
989
990   if (mi_suppress_notification.breakpoint)
991     return;
992
993   if (b->number <= 0)
994     return;
995
996   SWITCH_THRU_ALL_UIS (state)
997     {
998       struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
999       struct cleanup *old_chain;
1000
1001       if (mi == NULL)
1002         continue;
1003
1004       old_chain = make_cleanup_restore_target_terminal ();
1005       target_terminal_ours_for_output ();
1006
1007       fprintf_unfiltered (mi->event_channel, "breakpoint-deleted,id=\"%d\"",
1008                           b->number);
1009
1010       gdb_flush (mi->event_channel);
1011
1012       do_cleanups (old_chain);
1013     }
1014 }
1015
1016 /* Emit notification about modified breakpoint.  */
1017
1018 static void
1019 mi_breakpoint_modified (struct breakpoint *b)
1020 {
1021   struct switch_thru_all_uis state;
1022
1023   if (mi_suppress_notification.breakpoint)
1024     return;
1025
1026   if (b->number <= 0)
1027     return;
1028
1029   SWITCH_THRU_ALL_UIS (state)
1030     {
1031       struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
1032       struct cleanup *old_chain;
1033
1034       if (mi == NULL)
1035         continue;
1036
1037       old_chain = make_cleanup_restore_target_terminal ();
1038       target_terminal_ours_for_output ();
1039       fprintf_unfiltered (mi->event_channel,
1040                           "breakpoint-modified");
1041       /* We want the output from gdb_breakpoint_query to go to
1042          mi->event_channel.  One approach would be to just call
1043          gdb_breakpoint_query, and then use mi_out_put to send the current
1044          content of mi_outout into mi->event_channel.  However, that will
1045          break if anything is output to mi_uiout prior to calling the
1046          breakpoint_created notifications.  So, we use
1047          ui_out_redirect.  */
1048       ui_out_redirect (mi->mi_uiout, mi->event_channel);
1049       TRY
1050         {
1051           gdb_breakpoint_query (mi->mi_uiout, b->number, NULL);
1052         }
1053       CATCH (e, RETURN_MASK_ERROR)
1054         {
1055         }
1056       END_CATCH
1057
1058       ui_out_redirect (mi->mi_uiout, NULL);
1059
1060       gdb_flush (mi->event_channel);
1061
1062       do_cleanups (old_chain);
1063     }
1064 }
1065
1066 static int
1067 mi_output_running_pid (struct thread_info *info, void *arg)
1068 {
1069   ptid_t *ptid = (ptid_t *) arg;
1070   struct switch_thru_all_uis state;
1071
1072   SWITCH_THRU_ALL_UIS (state)
1073     {
1074       struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
1075
1076       if (mi == NULL)
1077         continue;
1078
1079       if (ptid_get_pid (*ptid) == ptid_get_pid (info->ptid))
1080         fprintf_unfiltered (mi->raw_stdout,
1081                             "*running,thread-id=\"%d\"\n",
1082                             info->global_num);
1083     }
1084
1085   return 0;
1086 }
1087
1088 static int
1089 mi_inferior_count (struct inferior *inf, void *arg)
1090 {
1091   if (inf->pid != 0)
1092     {
1093       int *count_p = (int *) arg;
1094       (*count_p)++;
1095     }
1096
1097   return 0;
1098 }
1099
1100 static void
1101 mi_on_resume_1 (struct mi_interp *mi, ptid_t ptid)
1102 {
1103   /* To cater for older frontends, emit ^running, but do it only once
1104      per each command.  We do it here, since at this point we know
1105      that the target was successfully resumed, and in non-async mode,
1106      we won't return back to MI interpreter code until the target
1107      is done running, so delaying the output of "^running" until then
1108      will make it impossible for frontend to know what's going on.
1109
1110      In future (MI3), we'll be outputting "^done" here.  */
1111   if (!running_result_record_printed && mi_proceeded)
1112     {
1113       fprintf_unfiltered (mi->raw_stdout, "%s^running\n",
1114                           current_token ? current_token : "");
1115     }
1116
1117   if (ptid_get_pid (ptid) == -1)
1118     fprintf_unfiltered (mi->raw_stdout, "*running,thread-id=\"all\"\n");
1119   else if (ptid_is_pid (ptid))
1120     {
1121       int count = 0;
1122
1123       /* Backwards compatibility.  If there's only one inferior,
1124          output "all", otherwise, output each resumed thread
1125          individually.  */
1126       iterate_over_inferiors (mi_inferior_count, &count);
1127
1128       if (count == 1)
1129         fprintf_unfiltered (mi->raw_stdout, "*running,thread-id=\"all\"\n");
1130       else
1131         iterate_over_threads (mi_output_running_pid, &ptid);
1132     }
1133   else
1134     {
1135       struct thread_info *ti = find_thread_ptid (ptid);
1136
1137       gdb_assert (ti);
1138       fprintf_unfiltered (mi->raw_stdout, "*running,thread-id=\"%d\"\n",
1139                           ti->global_num);
1140     }
1141
1142   if (!running_result_record_printed && mi_proceeded)
1143     {
1144       running_result_record_printed = 1;
1145       /* This is what gdb used to do historically -- printing prompt even if
1146          it cannot actually accept any input.  This will be surely removed
1147          for MI3, and may be removed even earlier.  SYNC_EXECUTION is
1148          checked here because we only need to emit a prompt if a
1149          synchronous command was issued when the target is async.  */
1150       if (!target_can_async_p () || sync_execution)
1151         fputs_unfiltered ("(gdb) \n", mi->raw_stdout);
1152     }
1153   gdb_flush (mi->raw_stdout);
1154 }
1155
1156 static void
1157 mi_on_resume (ptid_t ptid)
1158 {
1159   struct thread_info *tp = NULL;
1160   struct switch_thru_all_uis state;
1161
1162   if (ptid_equal (ptid, minus_one_ptid) || ptid_is_pid (ptid))
1163     tp = inferior_thread ();
1164   else
1165     tp = find_thread_ptid (ptid);
1166
1167   /* Suppress output while calling an inferior function.  */
1168   if (tp->control.in_infcall)
1169     return;
1170
1171   SWITCH_THRU_ALL_UIS (state)
1172     {
1173       struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
1174       struct cleanup *old_chain;
1175
1176       if (mi == NULL)
1177         continue;
1178
1179       old_chain = make_cleanup_restore_target_terminal ();
1180       target_terminal_ours_for_output ();
1181
1182       mi_on_resume_1 (mi, ptid);
1183
1184       do_cleanups (old_chain);
1185     }
1186 }
1187
1188 static void
1189 mi_solib_loaded (struct so_list *solib)
1190 {
1191   struct switch_thru_all_uis state;
1192
1193   SWITCH_THRU_ALL_UIS (state)
1194     {
1195       struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
1196       struct ui_out *uiout;
1197       struct cleanup *old_chain;
1198
1199       if (mi == NULL)
1200         continue;
1201
1202       uiout = interp_ui_out (top_level_interpreter ());
1203
1204       old_chain = make_cleanup_restore_target_terminal ();
1205       target_terminal_ours_for_output ();
1206
1207       fprintf_unfiltered (mi->event_channel, "library-loaded");
1208
1209       ui_out_redirect (uiout, mi->event_channel);
1210
1211       ui_out_field_string (uiout, "id", solib->so_original_name);
1212       ui_out_field_string (uiout, "target-name", solib->so_original_name);
1213       ui_out_field_string (uiout, "host-name", solib->so_name);
1214       ui_out_field_int (uiout, "symbols-loaded", solib->symbols_loaded);
1215       if (!gdbarch_has_global_solist (target_gdbarch ()))
1216         {
1217           ui_out_field_fmt (uiout, "thread-group", "i%d",
1218                             current_inferior ()->num);
1219         }
1220
1221       ui_out_redirect (uiout, NULL);
1222
1223       gdb_flush (mi->event_channel);
1224
1225       do_cleanups (old_chain);
1226     }
1227 }
1228
1229 static void
1230 mi_solib_unloaded (struct so_list *solib)
1231 {
1232   struct switch_thru_all_uis state;
1233
1234   SWITCH_THRU_ALL_UIS (state)
1235     {
1236       struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
1237       struct ui_out *uiout;
1238       struct cleanup *old_chain;
1239
1240       if (mi == NULL)
1241         continue;
1242
1243       uiout = interp_ui_out (top_level_interpreter ());
1244
1245       old_chain = make_cleanup_restore_target_terminal ();
1246       target_terminal_ours_for_output ();
1247
1248       fprintf_unfiltered (mi->event_channel, "library-unloaded");
1249
1250       ui_out_redirect (uiout, mi->event_channel);
1251
1252       ui_out_field_string (uiout, "id", solib->so_original_name);
1253       ui_out_field_string (uiout, "target-name", solib->so_original_name);
1254       ui_out_field_string (uiout, "host-name", solib->so_name);
1255       if (!gdbarch_has_global_solist (target_gdbarch ()))
1256         {
1257           ui_out_field_fmt (uiout, "thread-group", "i%d",
1258                             current_inferior ()->num);
1259         }
1260
1261       ui_out_redirect (uiout, NULL);
1262
1263       gdb_flush (mi->event_channel);
1264
1265       do_cleanups (old_chain);
1266     }
1267 }
1268
1269 /* Emit notification about the command parameter change.  */
1270
1271 static void
1272 mi_command_param_changed (const char *param, const char *value)
1273 {
1274   struct switch_thru_all_uis state;
1275
1276   if (mi_suppress_notification.cmd_param_changed)
1277     return;
1278
1279   SWITCH_THRU_ALL_UIS (state)
1280     {
1281       struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
1282       struct ui_out *mi_uiout;
1283       struct cleanup *old_chain;
1284
1285       if (mi == NULL)
1286         continue;
1287
1288       mi_uiout = interp_ui_out (top_level_interpreter ());
1289
1290       old_chain = make_cleanup_restore_target_terminal ();
1291       target_terminal_ours_for_output ();
1292
1293       fprintf_unfiltered (mi->event_channel, "cmd-param-changed");
1294
1295       ui_out_redirect (mi_uiout, mi->event_channel);
1296
1297       ui_out_field_string (mi_uiout, "param", param);
1298       ui_out_field_string (mi_uiout, "value", value);
1299
1300       ui_out_redirect (mi_uiout, NULL);
1301
1302       gdb_flush (mi->event_channel);
1303
1304       do_cleanups (old_chain);
1305     }
1306 }
1307
1308 /* Emit notification about the target memory change.  */
1309
1310 static void
1311 mi_memory_changed (struct inferior *inferior, CORE_ADDR memaddr,
1312                    ssize_t len, const bfd_byte *myaddr)
1313 {
1314   struct switch_thru_all_uis state;
1315
1316   if (mi_suppress_notification.memory)
1317     return;
1318
1319   SWITCH_THRU_ALL_UIS (state)
1320     {
1321       struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
1322       struct ui_out *mi_uiout;
1323       struct obj_section *sec;
1324       struct cleanup *old_chain;
1325
1326       if (mi == NULL)
1327         continue;
1328
1329       mi_uiout = interp_ui_out (top_level_interpreter ());
1330
1331       old_chain = make_cleanup_restore_target_terminal ();
1332       target_terminal_ours_for_output ();
1333
1334       fprintf_unfiltered (mi->event_channel, "memory-changed");
1335
1336       ui_out_redirect (mi_uiout, mi->event_channel);
1337
1338       ui_out_field_fmt (mi_uiout, "thread-group", "i%d", inferior->num);
1339       ui_out_field_core_addr (mi_uiout, "addr", target_gdbarch (), memaddr);
1340       ui_out_field_fmt (mi_uiout, "len", "%s", hex_string (len));
1341
1342       /* Append 'type=code' into notification if MEMADDR falls in the range of
1343          sections contain code.  */
1344       sec = find_pc_section (memaddr);
1345       if (sec != NULL && sec->objfile != NULL)
1346         {
1347           flagword flags = bfd_get_section_flags (sec->objfile->obfd,
1348                                                   sec->the_bfd_section);
1349
1350           if (flags & SEC_CODE)
1351             ui_out_field_string (mi_uiout, "type", "code");
1352         }
1353
1354       ui_out_redirect (mi_uiout, NULL);
1355
1356       gdb_flush (mi->event_channel);
1357
1358       do_cleanups (old_chain);
1359     }
1360 }
1361
1362 static int
1363 report_initial_inferior (struct inferior *inf, void *closure)
1364 {
1365   /* This function is called from mi_interpreter_init, and since
1366      mi_inferior_added assumes that inferior is fully initialized
1367      and top_level_interpreter_data is set, we cannot call
1368      it here.  */
1369   struct mi_interp *mi = (struct mi_interp *) closure;
1370   struct cleanup *old_chain;
1371
1372   old_chain = make_cleanup_restore_target_terminal ();
1373   target_terminal_ours_for_output ();
1374
1375   fprintf_unfiltered (mi->event_channel,
1376                       "thread-group-added,id=\"i%d\"",
1377                       inf->num);
1378   gdb_flush (mi->event_channel);
1379
1380   do_cleanups (old_chain);
1381   return 0;
1382 }
1383
1384 static struct ui_out *
1385 mi_ui_out (struct interp *interp)
1386 {
1387   struct mi_interp *mi = (struct mi_interp *) interp_data (interp);
1388
1389   return mi->mi_uiout;
1390 }
1391
1392 /* Do MI-specific logging actions; save raw_stdout, and change all
1393    the consoles to use the supplied ui-file(s).  */
1394
1395 static int
1396 mi_set_logging (struct interp *interp, int start_log,
1397                 struct ui_file *out, struct ui_file *logfile)
1398 {
1399   struct mi_interp *mi = (struct mi_interp *) interp_data (interp);
1400
1401   if (!mi)
1402     return 0;
1403
1404   if (start_log)
1405     {
1406       /* The tee created already is based on gdb_stdout, which for MI
1407          is a console and so we end up in an infinite loop of console
1408          writing to ui_file writing to console etc.  So discard the
1409          existing tee (it hasn't been used yet, and MI won't ever use
1410          it), and create one based on raw_stdout instead.  */
1411       if (logfile)
1412         {
1413           ui_file_delete (out);
1414           out = tee_file_new (mi->raw_stdout, 0, logfile, 0);
1415         }
1416
1417       mi->saved_raw_stdout = mi->raw_stdout;
1418       mi->raw_stdout = out;
1419     }
1420   else
1421     {
1422       mi->raw_stdout = mi->saved_raw_stdout;
1423       mi->saved_raw_stdout = NULL;
1424     }
1425   
1426   mi_console_set_raw (mi->out, mi->raw_stdout);
1427   mi_console_set_raw (mi->err, mi->raw_stdout);
1428   mi_console_set_raw (mi->log, mi->raw_stdout);
1429   mi_console_set_raw (mi->targ, mi->raw_stdout);
1430   mi_console_set_raw (mi->event_channel, mi->raw_stdout);
1431
1432   return 1;
1433 }
1434
1435 /* The MI interpreter's vtable.  */
1436
1437 static const struct interp_procs mi_interp_procs =
1438 {
1439   mi_interpreter_init,          /* init_proc */
1440   mi_interpreter_resume,        /* resume_proc */
1441   mi_interpreter_suspend,       /* suspend_proc */
1442   mi_interpreter_exec,          /* exec_proc */
1443   mi_ui_out,                    /* ui_out_proc */
1444   mi_set_logging,               /* set_logging_proc */
1445   mi_command_loop               /* command_loop_proc */
1446 };
1447
1448 /* Factory for MI interpreters.  */
1449
1450 static struct interp *
1451 mi_interp_factory (const char *name)
1452 {
1453   return interp_new (name, &mi_interp_procs, NULL);
1454 }
1455
1456 extern initialize_file_ftype _initialize_mi_interp; /* -Wmissing-prototypes */
1457
1458 void
1459 _initialize_mi_interp (void)
1460 {
1461   /* The various interpreter levels.  */
1462   interp_factory_register (INTERP_MI1, mi_interp_factory);
1463   interp_factory_register (INTERP_MI2, mi_interp_factory);
1464   interp_factory_register (INTERP_MI3, mi_interp_factory);
1465   interp_factory_register (INTERP_MI, mi_interp_factory);
1466
1467   observer_attach_signal_received (mi_on_signal_received);
1468   observer_attach_end_stepping_range (mi_on_end_stepping_range);
1469   observer_attach_signal_exited (mi_on_signal_exited);
1470   observer_attach_exited (mi_on_exited);
1471   observer_attach_no_history (mi_on_no_history);
1472   observer_attach_new_thread (mi_new_thread);
1473   observer_attach_thread_exit (mi_thread_exit);
1474   observer_attach_inferior_added (mi_inferior_added);
1475   observer_attach_inferior_appeared (mi_inferior_appeared);
1476   observer_attach_inferior_exit (mi_inferior_exit);
1477   observer_attach_inferior_removed (mi_inferior_removed);
1478   observer_attach_record_changed (mi_record_changed);
1479   observer_attach_normal_stop (mi_on_normal_stop);
1480   observer_attach_target_resumed (mi_on_resume);
1481   observer_attach_solib_loaded (mi_solib_loaded);
1482   observer_attach_solib_unloaded (mi_solib_unloaded);
1483   observer_attach_about_to_proceed (mi_about_to_proceed);
1484   observer_attach_traceframe_changed (mi_traceframe_changed);
1485   observer_attach_tsv_created (mi_tsv_created);
1486   observer_attach_tsv_deleted (mi_tsv_deleted);
1487   observer_attach_tsv_modified (mi_tsv_modified);
1488   observer_attach_breakpoint_created (mi_breakpoint_created);
1489   observer_attach_breakpoint_deleted (mi_breakpoint_deleted);
1490   observer_attach_breakpoint_modified (mi_breakpoint_modified);
1491   observer_attach_command_param_changed (mi_command_param_changed);
1492   observer_attach_memory_changed (mi_memory_changed);
1493   observer_attach_sync_execution_done (mi_on_sync_execution_done);
1494 }