5 #include <readline/readline.h>
6 #include <readline/history.h>
13 #define fatal(fmt...) fatal_func (__FILE__, __LINE__, __PRETTY_FUNCTION__, fmt)
14 #define gdb_assert assert
15 #define gdb_assert_not_reached(msg) fatal ("%s", (msg))
18 static ATTRIBUTE_NORETURN void
19 fatal_func (const char *file, int line, const char *func, const char *fmt, ...)
23 fprintf (stderr, "%s:%d:%s:", file, line, func);
25 vfprintf (stderr, fmt, ap);
33 xstrdup (const char *s)
37 gdb_assert (s != NULL);
39 gdb_assert (retval != NULL);
48 gdb_assert (size != 0);
50 gdb_assert (p != NULL);
55 xrealloc (void *p, size_t size)
58 return xmalloc (size);
59 gdb_assert (size != 0);
60 p = realloc (p, size);
61 gdb_assert (p != NULL);
72 xstrprintf (const char *fmt, ...)
79 i = vasprintf (&retval, fmt, ap);
82 gdb_assert (i == strlen (retval));
87 console_cb (const char *str, void *data)
93 time_out_cb (void *data)
95 fatal ("GDB response has timed out");
100 static ATTRIBUTE_UNUSED void
101 to_gdb_cb (const char *str, void *data)
103 printf ("to_gdb: %s\n", str);
106 static ATTRIBUTE_UNUSED void
107 from_gdb_cb (const char *str, void *data)
109 printf ("from_gdb: %s\n", str);
113 h_disconnect (int rc, void *arg)
121 history_save (int rc, void *arg)
123 const char *history_filename = arg;
125 write_history (history_filename);
129 default_command (mi_h *h, const char *cmd)
133 gdb_assert (strchr (cmd, '\n') == 0);
134 mi_send (h, "-interpreter-exec console \"%s\"\n", cmd);
138 mi_output *rec, *res;
140 res = mi_get_response_blk (h);
141 gdb_assert (res != NULL);
143 for (rec = res; rec != NULL; rec = rec->next)
150 /* We do not get MI_CL_DONE here, wait for MI_CL_STOPPED. */
157 gdb_assert (rec->c->type == t_const);
158 puts (rec->c->v.cstr);
161 fatal ("mi_get_rrecord == MI_CL_??? (%d)", rec->tclass);
164 mi_free_output (res);
169 quit_command (mi_h *h, const char *cmd)
175 mi_escape (const char *cs)
177 char *d, *retval = xmalloc (strlen (cs) * 2 + 1);
182 if (*cs == '"' || *cs == '\\')
191 gdb_done (mi_h *h, const char *command)
195 mi_send (h, "%s\n", command);
196 res = mi_get_response_blk (h);
197 gdb_assert (res != NULL && res->next == NULL && res->tclass == MI_CL_DONE
199 mi_free_output (res);
202 static jmp_buf gdb_readline_jmp_buf;
205 gdb_readline_sigint_handler (int signum)
207 gdb_assert (signum == SIGINT);
208 longjmp (gdb_readline_jmp_buf, 1);
212 gdb_readline (const char *prompt)
215 sighandler_t saved_handler;
219 i = sigprocmask (SIG_SETMASK, NULL, &mask);
221 saved_handler = signal (SIGINT, gdb_readline_sigint_handler);
222 if (setjmp (gdb_readline_jmp_buf) != 0)
224 rl_free_line_state ();
225 rl_cleanup_after_signal ();
227 /* GDB prints this. */
230 i = sigprocmask (SIG_SETMASK, &mask, NULL);
233 retval = readline (prompt);
234 saved_handler = signal (SIGINT, saved_handler);
235 gdb_assert (saved_handler == gdb_readline_sigint_handler);
240 commands_command_console_cb (const char *str, void *data)
242 char **strptr = data;
244 gdb_assert (*strptr == NULL);
245 *strptr = xstrdup (str);
249 commands_command (mi_h *h, const char *cmd_param)
251 char *cmd = xstrdup ("");
257 if (*cmd_param == '\0')
259 mi_set_console_cb (h, commands_command_console_cb, &bpnum);
260 gdb_done (h, "output $bpnum");
261 mi_set_console_cb (h, console_cb, NULL);
262 gdb_assert (bpnum != NULL);
266 printf (_("Type commands for breakpoint(s) %s, one per line.\n"
267 "End with a line saying just \"end\".\n"), cmd_param);
272 char *prompt, *data, *data2, *start, *end;
276 prompt = xstrprintf ("%*s>", (int) nesting);
277 data = gdb_readline (prompt);
280 data = xstrdup ("end");
282 for (start = data; isspace (*start); start++);
283 for (end = data + strlen (data); end > start && isspace (end[-1]); end--);
284 data2 = xmalloc (end - start + 1);
285 memcpy (data2, start, end - start);
286 data2[end - start] = '\0';
287 if (strcmp (data2, "python") == 0)
289 if (strcmp (data2, "end") == 0)
291 for (end = data2; *end && !isspace (*end); end++);
294 /* Here is a bug in GDB, it does not recognize command shortcuts. */
295 if (strcmp (data2, "while") == 0 || strcmp (data2, "if") == 0
296 || strcmp (data2, "commands") == 0
297 || strcmp (data2, "while-stepping") == 0
298 || strcmp (data2, "stepping") == 0 || strcmp (data2, "ws") == 0)
306 data2 = mi_escape (data);
308 data2_len = strlen (data2);
309 cmd = xrealloc (cmd, cmd_len + 2 + data2_len + 2);
311 cmd[cmd_len + 1] = '"';
312 memcpy (&cmd[cmd_len + 2], data2, data2_len);
313 cmd[cmd_len + 2 + data2_len] = '"';
314 cmd[cmd_len + 2 + data2_len + 1] = '\0';
315 cmd_len += 2 + data2_len + 1;
319 mi_send (h, "-break-commands %s%s\n", cmd_param, cmd);
323 res = mi_get_response_blk (h);
324 gdb_assert (res != NULL && res->next == NULL && res->tclass == MI_CL_DONE
326 mi_free_output (res);
329 static const struct cmd
333 void (*func) (mi_h *h, const char *cmd);
336 { "quit", 1, quit_command },
337 { "commands", 4, commands_command },
341 executecommand (mi_h *h, const char *cmd)
343 const char *start, *end, *cs;
344 const struct cmd *cmdp;
347 while (isspace (*cs))
350 while (isalnum (*cs))
353 while (isspace (*cs))
356 for (cmdp = cmds; cmdp < &cmds[LENGTH (cmds)]; cmdp++)
357 if (cmdp->min_shortcut <= end - start && end - start <= strlen (cmdp->name)
358 && strncmp (start, cmdp->name, end - start) == 0)
359 return cmdp->func (h, cs);
361 return default_command (h, cmd);
365 gdb_set_string (mi_h *h, const char *setting, const char *value)
367 char *cmd = xstrprintf ("-gdb-set %s %s", setting, value);
374 gdb_set_bool (mi_h *h, const char *setting, bool value)
376 gdb_set_string (h, setting, value ? "on" : "off");
380 gdb_show_string (mi_h *h, const char *setting)
385 mi_send (h, "-gdb-show %s\n", setting);
386 res = mi_get_response_blk (h);
387 gdb_assert (res != NULL && res->next == NULL && res->tclass == MI_CL_DONE
388 && res->c != NULL && res->c->next == NULL
389 && res->c->type == t_const && strcmp (res->c->var, "value") == 0);
390 retval = xstrdup (res->c->v.cstr);
391 mi_free_output (res);
396 gdb_show_int (mi_h *h, const char *setting)
398 char *string = gdb_show_string (h, setting);
404 retval = l = strtol (string, &end, 10);
405 gdb_assert (errno == 0 && (end == NULL || *end == '\0') && retval == l);
411 gdb_show_bool (mi_h *h, const char *setting)
413 char *string = gdb_show_string (h, setting);
416 retval = strcmp (string, "on") == 0;
417 gdb_assert (retval || strcmp (string, "off") == 0);
422 static mi_h *completion_entry_function_h;
424 static char **completion_entry_function_data;
425 static size_t completion_entry_function_data_used;
426 static size_t completion_entry_function_data_allocated;
429 completion_entry_function_console_cb (const char *str, void *data)
431 int line_start = (intptr_t) data;
435 if (completion_entry_function_data_used
436 == completion_entry_function_data_allocated)
438 if (completion_entry_function_data_allocated > 0)
439 completion_entry_function_data_allocated *= 2;
441 completion_entry_function_data_allocated = 0x100;
442 completion_entry_function_data = xrealloc (completion_entry_function_data,
443 (sizeof (*completion_entry_function_data)
444 * completion_entry_function_data_allocated));
447 cs = strchr (str, '\n');
448 if (cs == NULL || cs[1] != '\0')
449 fatal ("Invalid GDB data: %s", str);
451 if (strncmp (rl_line_buffer, str, rl_point) != 0)
452 fatal ("Completion GDB data do not match, have \"%.*s\", got \"%.*s\".",
453 (int) rl_point, rl_line_buffer, (int) (cs - str), str);
455 s = xmalloc (cs - str - line_start + 1);
456 memcpy (s, &str[line_start], cs - str - line_start);
457 s[cs - str - line_start] = '\0';
458 completion_entry_function_data[completion_entry_function_data_used++] = s;
462 completion_entry_function (const char *text, int matches)
464 mi_h *h = completion_entry_function_h;
466 gdb_assert (matches >= 0);
472 while (completion_entry_function_data_used)
473 xfree (completion_entry_function_data
474 [--completion_entry_function_data_used]);
475 xfree (completion_entry_function_data);
476 completion_entry_function_data = NULL;
477 completion_entry_function_data_used = 0;
478 completion_entry_function_data_allocated = 0;
480 gdb_assert (rl_point >= 0);
481 gdb_assert (strlen (rl_line_buffer) >= rl_point);
482 gdb_assert (strlen (text) <= rl_point);
483 line_start = rl_point - strlen (text);
484 gdb_assert (strncmp (text, &rl_line_buffer[line_start],
485 strlen (text)) == 0);
486 mi_send (h, "-interpreter-exec console \"complete %.*s\"\n",
487 (int) rl_point, rl_line_buffer);
489 mi_set_console_cb (h, completion_entry_function_console_cb,
490 (void *) (intptr_t) line_start);
491 res = mi_get_response_blk (h);
492 gdb_assert (res != NULL && res->next == NULL && res->tclass == MI_CL_DONE
494 mi_free_output (res);
495 mi_set_console_cb (h, console_cb, NULL);
498 if (matches < completion_entry_function_data_used)
499 return xstrdup (completion_entry_function_data[matches]);
500 else if (matches == completion_entry_function_data_used)
503 gdb_assert_not_reached ("too many matches");
506 extern char **mi_gdb_argv;
507 extern void (*mi_gdb_start_hook) (mi_h *h);
512 on_exit (h_disconnect, h);
513 mi_set_console_cb (h, console_cb, NULL);
514 mi_set_time_out_cb (h, time_out_cb, NULL);
516 // mi_set_to_gdb_cb (h, to_gdb_cb, NULL);
517 // mi_set_from_gdb_cb (h, from_gdb_cb, NULL);
521 main (int argc, char **argv)
526 setbuf (stdout, NULL);
528 mi_gdb_argv = xmalloc ((argc + 2) * sizeof (*mi_gdb_argv));
529 memcpy (&mi_gdb_argv[2], &argv[1], argc * sizeof (*mi_gdb_argv));
530 mi_gdb_argv[0] = "gdb";
531 mi_gdb_argv[1] = "--interpreter=mi";
533 mi_gdb_start_hook = start_hook;
535 h = mi_connect_local ();
537 fatal ("Cannot connect to GDB");
539 /* First eat the prompt. Then run empty command so that additional results
540 from -ex or -x during mi_connect_local are flushed. */
541 res = mi_get_response_blk (h);
542 gdb_assert (res == NULL);
543 default_command (h, "echo");
545 completion_entry_function_h = h;
546 rl_completion_entry_function = completion_entry_function;
547 rl_readline_name = "gdb";
549 if (gdb_show_bool (h, "history save"))
551 int history_size = gdb_show_int (h, "history size");
552 char *history_filename = gdb_show_string (h, "history filename");
554 gdb_set_bool (h, "history save", false);
555 stifle_history (history_size);
556 read_history (history_filename);
557 on_exit (history_save, history_filename);
558 /* Do not free HISTORY_FILENAME. */
565 prompt = gdb_show_string (h, "prompt");
566 cmd = gdb_readline (prompt);
570 cmd = xstrdup ("quit");
574 executecommand (h, cmd);