Include group-id in thread-created notification.
[external/binutils.git] / gdb / mi / mi-interp.c
1 /* MI Interpreter Definitions and Commands for GDB, the GNU debugger.
2
3    Copyright (C) 2002, 2003, 2004, 2005, 2007, 2008
4    Free Software Foundation, Inc.
5
6    This file is part of GDB.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20
21 #include "defs.h"
22 #include "gdb_string.h"
23 #include "interps.h"
24 #include "event-top.h"
25 #include "event-loop.h"
26 #include "inferior.h"
27 #include "ui-out.h"
28 #include "top.h"
29 #include "exceptions.h"
30 #include "mi-main.h"
31 #include "mi-cmds.h"
32 #include "mi-out.h"
33 #include "mi-console.h"
34 #include "observer.h"
35 #include "gdbthread.h"
36
37 struct mi_interp
38 {
39   /* MI's output channels */
40   struct ui_file *out;
41   struct ui_file *err;
42   struct ui_file *log;
43   struct ui_file *targ;
44   struct ui_file *event_channel;
45
46   /* This is the interpreter for the mi... */
47   struct interp *mi2_interp;
48   struct interp *mi1_interp;
49   struct interp *mi_interp;
50 };
51
52 /* These are the interpreter setup, etc. functions for the MI interpreter */
53 static void mi_execute_command_wrapper (char *cmd);
54 static void mi_command_loop (int mi_version);
55
56 /* These are hooks that we put in place while doing interpreter_exec
57    so we can report interesting things that happened "behind the mi's
58    back" in this command */
59 static int mi_interp_query_hook (const char *ctlstr, va_list ap)
60      ATTR_FORMAT (printf, 1, 0);
61
62 static void mi3_command_loop (void);
63 static void mi2_command_loop (void);
64 static void mi1_command_loop (void);
65
66 static void mi_insert_notify_hooks (void);
67 static void mi_remove_notify_hooks (void);
68 static void mi_on_normal_stop (struct bpstats *bs);
69
70 static void mi_new_thread (struct thread_info *t);
71 static void mi_thread_exit (struct thread_info *t);
72 static void mi_new_inferior (int pid);
73 static void mi_inferior_exit (int pid);
74 static void mi_on_resume (ptid_t ptid);
75
76 static void *
77 mi_interpreter_init (int top_level)
78 {
79   struct mi_interp *mi = XMALLOC (struct mi_interp);
80
81   /* HACK: We need to force stdout/stderr to point at the console.  This avoids
82      any potential side effects caused by legacy code that is still
83      using the TUI / fputs_unfiltered_hook.  So we set up output channels for
84      this now, and swap them in when we are run. */
85
86   raw_stdout = stdio_fileopen (stdout);
87
88   /* Create MI channels */
89   mi->out = mi_console_file_new (raw_stdout, "~", '"');
90   mi->err = mi_console_file_new (raw_stdout, "&", '"');
91   mi->log = mi->err;
92   mi->targ = mi_console_file_new (raw_stdout, "@", '"');
93   mi->event_channel = mi_console_file_new (raw_stdout, "=", 0);
94
95   if (top_level)
96     {
97       observer_attach_new_thread (mi_new_thread);
98       observer_attach_thread_exit (mi_thread_exit);
99       observer_attach_new_inferior (mi_new_inferior);
100       observer_attach_inferior_exit (mi_inferior_exit);
101       observer_attach_normal_stop (mi_on_normal_stop);
102       observer_attach_target_resumed (mi_on_resume);
103     }
104
105   return mi;
106 }
107
108 static int
109 mi_interpreter_resume (void *data)
110 {
111   struct mi_interp *mi = data;
112   /* As per hack note in mi_interpreter_init, swap in the output channels... */
113
114   gdb_setup_readline ();
115
116   /* These overwrite some of the initialization done in
117      _intialize_event_loop.  */
118   call_readline = gdb_readline2;
119   input_handler = mi_execute_command_wrapper;
120   add_file_handler (input_fd, stdin_event_handler, 0);
121   async_command_editing_p = 0;
122   /* FIXME: This is a total hack for now.  PB's use of the MI
123      implicitly relies on a bug in the async support which allows
124      asynchronous commands to leak through the commmand loop.  The bug
125      involves (but is not limited to) the fact that sync_execution was
126      erroneously initialized to 0.  Duplicate by initializing it thus
127      here...  */
128   sync_execution = 0;
129
130   gdb_stdout = mi->out;
131   /* Route error and log output through the MI */
132   gdb_stderr = mi->err;
133   gdb_stdlog = mi->log;
134   /* Route target output through the MI. */
135   gdb_stdtarg = mi->targ;
136   /* Route target error through the MI as well. */
137   gdb_stdtargerr = mi->targ;
138
139   /* Replace all the hooks that we know about.  There really needs to
140      be a better way of doing this... */
141   clear_interpreter_hooks ();
142
143   deprecated_show_load_progress = mi_load_progress;
144
145   /* If we're _the_ interpreter, take control. */
146   if (current_interp_named_p (INTERP_MI1))
147     deprecated_command_loop_hook = mi1_command_loop;
148   else if (current_interp_named_p (INTERP_MI2))
149     deprecated_command_loop_hook = mi2_command_loop;
150   else if (current_interp_named_p (INTERP_MI3))
151     deprecated_command_loop_hook = mi3_command_loop;
152   else
153     deprecated_command_loop_hook = mi2_command_loop;
154
155   return 1;
156 }
157
158 static int
159 mi_interpreter_suspend (void *data)
160 {
161   gdb_disable_readline ();
162   return 1;
163 }
164
165 static struct gdb_exception
166 mi_interpreter_exec (void *data, const char *command)
167 {
168   static struct gdb_exception ok;
169   char *tmp = alloca (strlen (command) + 1);
170   strcpy (tmp, command);
171   mi_execute_command_wrapper (tmp);
172   return exception_none;
173 }
174
175 /* Never display the default gdb prompt in mi case.  */
176 static int
177 mi_interpreter_prompt_p (void *data)
178 {
179   return 0;
180 }
181
182 void
183 mi_cmd_interpreter_exec (char *command, char **argv, int argc)
184 {
185   struct interp *interp_to_use;
186   int i;
187   struct interp_procs *procs;
188   char *mi_error_message = NULL;
189   struct cleanup *old_chain;
190
191   if (argc < 2)
192     error ("mi_cmd_interpreter_exec: Usage: -interpreter-exec interp command");
193
194   interp_to_use = interp_lookup (argv[0]);
195   if (interp_to_use == NULL)
196     error ("mi_cmd_interpreter_exec: could not find interpreter \"%s\"", argv[0]);
197
198   if (!interp_exec_p (interp_to_use))
199     error ("mi_cmd_interpreter_exec: interpreter \"%s\" does not support command execution",
200               argv[0]);
201
202   /* Insert the MI out hooks, making sure to also call the interpreter's hooks
203      if it has any. */
204   /* KRS: We shouldn't need this... Events should be installed and they should
205      just ALWAYS fire something out down the MI channel... */
206   mi_insert_notify_hooks ();
207
208   /* Now run the code... */
209
210   old_chain = make_cleanup (null_cleanup, 0);
211   for (i = 1; i < argc; i++)
212     {
213       struct gdb_exception e = interp_exec (interp_to_use, argv[i]);
214       if (e.reason < 0)
215         {
216           mi_error_message = xstrdup (e.message);
217           make_cleanup (xfree, mi_error_message);
218           break;
219         }
220     }
221
222   mi_remove_notify_hooks ();
223
224   if (mi_error_message != NULL)
225     error ("%s", mi_error_message);
226   do_cleanups (old_chain);
227 }
228
229 /*
230  * mi_insert_notify_hooks - This inserts a number of hooks that are meant to produce
231  * async-notify ("=") MI messages while running commands in another interpreter
232  * using mi_interpreter_exec.  The canonical use for this is to allow access to
233  * the gdb CLI interpreter from within the MI, while still producing MI style output
234  * when actions in the CLI command change gdb's state.
235 */
236
237 static void
238 mi_insert_notify_hooks (void)
239 {
240   deprecated_query_hook = mi_interp_query_hook;
241 }
242
243 static void
244 mi_remove_notify_hooks (void)
245 {
246   deprecated_query_hook = NULL;
247 }
248
249 static int
250 mi_interp_query_hook (const char *ctlstr, va_list ap)
251 {
252   return 1;
253 }
254
255 static void
256 mi_execute_command_wrapper (char *cmd)
257 {
258   mi_execute_command (cmd, stdin == instream);
259 }
260
261 static void
262 mi1_command_loop (void)
263 {
264   mi_command_loop (1);
265 }
266
267 static void
268 mi2_command_loop (void)
269 {
270   mi_command_loop (2);
271 }
272
273 static void
274 mi3_command_loop (void)
275 {
276   mi_command_loop (3);
277 }
278
279 static void
280 mi_command_loop (int mi_version)
281 {
282   /* Turn off 8 bit strings in quoted output.  Any character with the
283      high bit set is printed using C's octal format. */
284   sevenbit_strings = 1;
285   /* Tell the world that we're alive */
286   fputs_unfiltered ("(gdb) \n", raw_stdout);
287   gdb_flush (raw_stdout);
288   start_event_loop ();
289 }
290
291 static void
292 mi_new_thread (struct thread_info *t)
293 {
294   struct mi_interp *mi = top_level_interpreter_data ();
295
296   fprintf_unfiltered (mi->event_channel, 
297                       "thread-created,id=\"%d\",group-id=\"%d\"", 
298                       t->num, t->ptid.pid);
299   gdb_flush (mi->event_channel);
300 }
301
302 static void
303 mi_thread_exit (struct thread_info *t)
304 {
305   struct mi_interp *mi = top_level_interpreter_data ();
306   target_terminal_ours ();
307   fprintf_unfiltered (mi->event_channel, 
308                       "thread-exited,id=\"%d\",group-id=\"%d\"", 
309                       t->num,t->ptid.pid);
310   gdb_flush (mi->event_channel);
311 }
312
313 static void
314 mi_new_inferior (int pid)
315 {
316   struct mi_interp *mi = top_level_interpreter_data ();
317   target_terminal_ours ();
318   fprintf_unfiltered (mi->event_channel, "thread-group-created,id=\"%d\"", 
319                       pid);
320   gdb_flush (mi->event_channel);
321 }
322
323 static void
324 mi_inferior_exit (int pid)
325 {
326   struct mi_interp *mi = top_level_interpreter_data ();
327   target_terminal_ours ();
328   fprintf_unfiltered (mi->event_channel, "thread-group-exited,id=\"%d\"", 
329                       pid);
330   gdb_flush (mi->event_channel);  
331 }
332
333 static void
334 mi_on_normal_stop (struct bpstats *bs)
335 {
336   /* Since this can be called when CLI command is executing,
337      using cli interpreter, be sure to use MI uiout for output,
338      not the current one.  */
339   struct ui_out *uiout = interp_ui_out (top_level_interpreter ());
340   struct mi_interp *mi = top_level_interpreter_data ();
341
342   fputs_unfiltered ("*stopped", raw_stdout);
343   mi_out_put (uiout, raw_stdout);
344   mi_out_rewind (uiout);
345   fputs_unfiltered ("\n", raw_stdout);
346   gdb_flush (raw_stdout);
347 }
348
349 static void
350 mi_on_resume (ptid_t ptid)
351 {
352   /* To cater for older frontends, emit ^running, but do it only once
353      per each command.  We do it here, since at this point we know
354      that the target was successfully resumed, and in non-async mode,
355      we won't return back to MI interpreter code until the target
356      is done running, so delaying the output of "^running" until then
357      will make it impossible for frontend to know what's going on.
358
359      In future (MI3), we'll be outputting "^done" here.  */
360   if (!running_result_record_printed)
361     {
362       if (current_token)
363         fputs_unfiltered (current_token, raw_stdout);
364       fputs_unfiltered ("^running\n", raw_stdout);
365     }
366
367   if (PIDGET (ptid) == -1)
368     fprintf_unfiltered (raw_stdout, "*running,thread-id=\"all\"\n");
369   else if (thread_count () == 0)
370     {
371       /* This is a target where for single-threaded programs the thread
372          table has zero threads.  Don't print any thread-id field.  */
373       fprintf_unfiltered (raw_stdout, "*running\n");
374     }
375   else
376     {
377       struct thread_info *ti = find_thread_pid (ptid);
378       gdb_assert (ti);
379       fprintf_unfiltered (raw_stdout, "*running,thread-id=\"%d\"\n", ti->num);
380     }
381
382   if (!running_result_record_printed)
383     {
384       running_result_record_printed = 1;
385       /* This is what gdb used to do historically -- printing prompt even if
386          it cannot actually accept any input.  This will be surely removed
387          for MI3, and may be removed even earler.  */
388       /* FIXME: review the use of target_is_async_p here -- is that
389          what we want? */
390       if (!target_is_async_p ())
391         fputs_unfiltered ("(gdb) \n", raw_stdout);
392     }
393   gdb_flush (raw_stdout);
394 }
395
396 extern initialize_file_ftype _initialize_mi_interp; /* -Wmissing-prototypes */
397
398 void
399 _initialize_mi_interp (void)
400 {
401   static const struct interp_procs procs =
402   {
403     mi_interpreter_init,        /* init_proc */
404     mi_interpreter_resume,      /* resume_proc */
405     mi_interpreter_suspend,     /* suspend_proc */
406     mi_interpreter_exec,        /* exec_proc */
407     mi_interpreter_prompt_p     /* prompt_proc_p */
408   };
409
410   /* The various interpreter levels.  */
411   interp_add (interp_new (INTERP_MI1, NULL, mi_out_new (1), &procs));
412   interp_add (interp_new (INTERP_MI2, NULL, mi_out_new (2), &procs));
413   interp_add (interp_new (INTERP_MI3, NULL, mi_out_new (3), &procs));
414
415   /* "mi" selects the most recent released version.  "mi2" was
416      released as part of GDB 6.0.  */
417   interp_add (interp_new (INTERP_MI, NULL, mi_out_new (2), &procs));
418 }