5 #include <readline/readline.h>
6 #include <readline/history.h>
12 #define fatal(fmt...) fatal_func (__FILE__, __LINE__, __PRETTY_FUNCTION__, fmt)
13 #define gdb_assert assert
14 #define gdb_assert_not_reached(msg) fatal ("%s", (msg))
16 static ATTRIBUTE_NORETURN void
17 fatal_func (const char *file, int line, const char *func, const char *fmt, ...)
21 fprintf (stderr, "%s:%d:%s:", file, line, func);
23 vfprintf (stderr, fmt, ap);
31 xstrdup (const char *s)
35 gdb_assert (s != NULL);
37 gdb_assert (retval != NULL);
46 gdb_assert (size != 0);
48 gdb_assert (p != NULL);
53 xrealloc (void *p, size_t size)
56 return xmalloc (size);
57 gdb_assert (size != 0);
58 p = realloc (p, size);
59 gdb_assert (p != NULL);
70 xstrprintf (const char *fmt, ...)
77 i = vasprintf (&retval, fmt, ap);
80 gdb_assert (i == strlen (retval));
85 console_cb (const char *str, void *data)
90 static ATTRIBUTE_UNUSED void
91 to_gdb_cb (const char *str, void *data)
93 printf ("to_gdb: %s\n", str);
96 static ATTRIBUTE_UNUSED void
97 from_gdb_cb (const char *str, void *data)
99 printf ("from_gdb: %s\n", str);
103 h_disconnect (int rc, void *arg)
111 history_save (int rc, void *arg)
113 const char *history_filename = arg;
115 write_history (history_filename);
119 quit_command (mi_h *h, const char *cmd)
125 default_command (mi_h *h, const char *cmd)
129 mi_send (h, "-interpreter-exec console \"%s\"\n", cmd);
133 mi_output *rec, *res;
135 res = mi_get_response_blk (h);
136 gdb_assert (res != NULL);
138 for (rec = res; rec != NULL; rec = rec->next)
145 /* We do not get MI_CL_DONE here, wait for MI_CL_STOPPED. */
152 gdb_assert (rec->c->type == t_const);
153 puts (rec->c->v.cstr);
156 fatal ("mi_get_rrecord == MI_CL_??? (%d)", rec->tclass);
159 mi_free_output (res);
163 static const struct cmd
166 void (*func) (mi_h *h, const char *cmd);
169 { "q", quit_command },
170 { "qu", quit_command },
171 { "qui", quit_command },
172 { "quit", quit_command },
176 executecommand (mi_h *h, const char *cmd)
178 const char *start, *end, *cs;
179 const struct cmd *cmdp;
182 while (isspace (*cs))
185 while (isalnum (*cs))
189 for (cmdp = cmds; cmdp < &cmds[LENGTH (cmds)]; cmdp++)
190 if (strlen (cmdp->name) == end - start
191 && strncmp (cmd, cmdp->name, end - start) == 0)
192 return cmdp->func (h, cmd);
194 return default_command (h, cmd);
198 gdb_done (mi_h *h, const char *command)
202 mi_send (h, "%s\n", command);
203 res = mi_get_response_blk (h);
204 gdb_assert (res != NULL && res->next == NULL && res->tclass == MI_CL_DONE
206 mi_free_output (res);
210 gdb_set_string (mi_h *h, const char *setting, const char *value)
212 char *cmd = xstrprintf ("-gdb-set %s %s", setting, value);
219 gdb_set_bool (mi_h *h, const char *setting, bool value)
221 gdb_set_string (h, setting, value ? "on" : "off");
225 gdb_show_string (mi_h *h, const char *setting)
230 mi_send (h, "-gdb-show %s\n", setting);
231 res = mi_get_response_blk (h);
232 gdb_assert (res != NULL && res->next == NULL && res->tclass == MI_CL_DONE
233 && res->c != NULL && res->c->next == NULL
234 && res->c->type == t_const && strcmp (res->c->var, "value") == 0);
235 retval = xstrdup (res->c->v.cstr);
236 mi_free_output (res);
241 gdb_show_int (mi_h *h, const char *setting)
243 char *string = gdb_show_string (h, setting);
249 retval = l = strtol (string, &end, 10);
250 gdb_assert (errno == 0 && (end == NULL || *end == '\0') && retval == l);
256 gdb_show_bool (mi_h *h, const char *setting)
258 char *string = gdb_show_string (h, setting);
261 retval = strcmp (string, "on") == 0;
262 gdb_assert (retval || strcmp (string, "off") == 0);
267 static mi_h *completion_entry_function_h;
269 static char **completion_entry_function_data;
270 static size_t completion_entry_function_data_used;
271 static size_t completion_entry_function_data_allocated;
274 completion_entry_function_console_cb (const char *str, void *data)
276 int line_start = (intptr_t) data;
280 if (completion_entry_function_data_used
281 == completion_entry_function_data_allocated)
283 if (completion_entry_function_data_allocated > 0)
284 completion_entry_function_data_allocated *= 2;
286 completion_entry_function_data_allocated = 0x100;
287 completion_entry_function_data = xrealloc (completion_entry_function_data,
288 (sizeof (*completion_entry_function_data)
289 * completion_entry_function_data_allocated));
292 cs = strchr (str, '\n');
293 if (cs == NULL || cs[1] != '\0')
294 fatal ("Invalid GDB data: %s", str);
296 if (strncmp (rl_line_buffer, str, rl_point) != 0)
297 fatal ("Completion GDB data do not match, have \"%.*s\", got \"%.*s\".",
298 (int) rl_point, rl_line_buffer, (int) (cs - str), str);
300 s = xmalloc (cs - str - line_start + 1);
301 memcpy (s, &str[line_start], cs - str - line_start);
302 s[cs - str - line_start] = '\0';
303 completion_entry_function_data[completion_entry_function_data_used++] = s;
307 completion_entry_function (const char *text, int matches)
309 mi_h *h = completion_entry_function_h;
311 gdb_assert (matches >= 0);
317 while (completion_entry_function_data_used)
318 xfree (completion_entry_function_data
319 [--completion_entry_function_data_used]);
320 xfree (completion_entry_function_data);
321 completion_entry_function_data = NULL;
322 completion_entry_function_data_used = 0;
323 completion_entry_function_data_allocated = 0;
325 gdb_assert (rl_point >= 0);
326 gdb_assert (strlen (rl_line_buffer) >= rl_point);
327 gdb_assert (strlen (text) <= rl_point);
328 line_start = rl_point - strlen (text);
329 gdb_assert (strncmp (text, &rl_line_buffer[line_start],
330 strlen (text)) == 0);
331 mi_send (h, "-interpreter-exec console \"complete %.*s\"\n",
332 (int) rl_point, rl_line_buffer);
334 mi_set_console_cb (h, completion_entry_function_console_cb,
335 (void *) (intptr_t) line_start);
336 res = mi_get_response_blk (h);
337 gdb_assert (res != NULL && res->next == NULL && res->tclass == MI_CL_DONE
339 mi_free_output (res);
340 mi_set_console_cb (h, console_cb, NULL);
343 if (matches < completion_entry_function_data_used)
344 return xstrdup (completion_entry_function_data[matches]);
345 else if (matches == completion_entry_function_data_used)
348 gdb_assert_not_reached ("too many matches");
352 main (int argc, char **argv)
356 const char *ex[argc];
357 unsigned ex_count = 0, ex_used = 0;
359 setbuf (stdout, NULL);
361 while (*++argv != NULL)
363 if (strcmp (*argv, "-ex") == 0 && argv[1] != NULL)
364 ex[ex_count++] = *++argv;
366 fatal ("Unknown parameter: %s", *argv);
369 mi_set_workaround (MI_PSYM_SEARCH, 0);
371 h = mi_connect_local ();
373 fatal ("Cannot connect to GDB");
375 on_exit (h_disconnect, h);
377 mi_set_console_cb (h, console_cb, NULL);
378 // mi_set_to_gdb_cb (h, to_gdb_cb, NULL);
379 // mi_set_from_gdb_cb (h, from_gdb_cb, NULL);
381 completion_entry_function_h = h;
382 rl_completion_entry_function = completion_entry_function;
383 rl_readline_name = "gdb";
385 if (gdb_show_bool (h, "history save"))
387 int history_size = gdb_show_int (h, "history size");
388 char *history_filename = gdb_show_string (h, "history filename");
390 gdb_set_bool (h, "history save", false);
391 stifle_history (history_size);
392 read_history (history_filename);
393 on_exit (history_save, history_filename);
394 /* Do not free HISTORY_FILENAME. */
399 if (ex_used < ex_count)
401 cmd = xstrdup (ex[ex_used++]);
402 printf ("(gdb) %s\n", cmd);
406 cmd = readline ("(gdb) ");
408 cmd = xstrdup ("quit");
412 executecommand (h, cmd);