3
* gdb-io-read.c - Output reading functions for GDB wrapper library.
5
* See the file "gdb-io.h" for license information.
17
#include "gdb-io-priv.h"
22
static GSList *source_files = NULL;
24
static gboolean starting = FALSE;
29
free_string_list(GSList ** list)
32
for (p = *list; p; p = p->next)
46
free_string_list(&source_files);
52
find_file_and_fullname(gconstpointer data, gconstpointer user_data)
54
GdbLxValue *v = (GdbLxValue *) data;
55
gchar *ref = (gchar *) user_data;
56
HSTR(v->hash, fullname);
58
return (fullname && file
59
&& (g_str_equal(ref, file) || g_str_equal(ref, fullname))) ? 0 : -1;
65
parse_file_list_cb(gpointer data, gpointer user_data)
67
GdbLxValue *v = (GdbLxValue *) data;
68
if (v && (v->type == vt_HASH))
70
HSTR(v->hash, fullname);
72
if (file && !fullname)
74
if (g_slist_find_custom((GSList *) user_data, file, find_file_and_fullname))
85
if (!g_slist_find_custom(source_files, file, (GCompareFunc) strcmp))
87
source_files = g_slist_append(source_files, g_strdup(file));
95
static void handle_response_line(gchar * str, gchar ** list);
99
handle_response_lines(gchar ** list)
104
for (i = 0; list[i]; i++)
106
handle_response_line(list[i], list);
114
response_is_error(gchar * resp, gchar ** list)
116
if (strncmp(resp, "^error", 6) == 0)
118
handle_response_line(resp, list);
127
#define CHK_RESP_ERR(resp, list) if (response_is_error(resp,list)) { return; }
129
#define IsDigit g_ascii_isdigit
132
parse_process_info(gint seq, gchar ** list, gchar * resp)
134
CHK_RESP_ERR(resp, list);
136
if (g_str_equal(resp, "^done"))
138
gchar *pidstr = strchr(list[0], ' ');
142
while (g_ascii_isspace(*pidstr))
146
if (IsDigit(*pidstr))
149
while (IsDigit(*end))
154
pid = gdbio_atoi(pidstr);
155
if ((pid > 0) && (!gdbio_get_target_pid()))
157
gdbio_set_target_pid(pid);
158
gdbio_send_cmd("-exec-continue\n");
167
gdbio_get_results(gchar * resp, gchar ** list)
169
if (strncmp(resp, "^error", 6) == 0)
173
GHashTable *h = gdblx_parse_results(resp + 7);
178
if (g_str_equal(msg, "unknown error"))
180
gint len = g_strv_length(list);
181
if ((len > 1) && list[len - 2] && *list[len - 2])
188
tmp = g_strcompress(tmp);
193
gdbio_error_func(msg);
201
g_hash_table_destroy(h);
206
if (strncmp(resp, "^done,", 6) == 0)
207
return gdblx_parse_results(resp + 6);
208
if (strncmp(resp, "*stopped,", 9) == 0)
210
gdbio_do_status(GdbStopped);
211
return gdblx_parse_results(resp + 9);
218
static void handle_response_line(gchar * str, gchar ** list);
222
gdbio_set_starting(gboolean s)
228
gdbio_target_started(gint seq, gchar ** list, gchar * resp)
230
if ((strncmp(resp, "^error", 6) == 0) && (!gdbio_get_target_pid()))
232
gdbio_error_func(_("Error starting target process!\n"));
233
gdbio_do_status(GdbFinished);
237
handle_response_lines(list);
244
set_main_break(gint seq, gchar ** list, gchar * resp)
246
GHashTable *h = gdbio_get_results(resp, list);
251
if (gdblx_check_keyval(bkpt, "number", "1"))
253
gdbio_do_status(GdbLoaded);
257
g_hash_table_destroy(h);
262
gdbio_parse_file_list(gint seq, gchar ** list, gchar * resp)
264
GHashTable *h = gdbio_get_results(resp, list);
270
g_slist_foreach(files, parse_file_list_cb, files);
272
gdbio_send_seq_cmd(set_main_break, "-break-insert _start\n");
277
(_("This executable does not appear to contain the required debugging information."));
280
g_hash_table_destroy(h);
288
do_step_func(GHashTable * h, gchar * reason)
291
HSTR(frame, fullname);
293
if (fullname && line)
295
if (gdbio_setup.step_func)
298
for (p = reason; *p; p++)
305
gdbio_setup.step_func(fullname, line, reason);
309
gdbio_info_func("%s:%s", fullname, line);
326
#define reason_is(r) (r && reason && g_str_equal(reason, r))
329
finish_function(gint seq, gchar ** list, gchar * resp)
331
if (strncmp(resp, "^running", 8) == 0)
333
gdbio_set_running(TRUE);
334
gdbio_do_status(GdbRunning);
338
GHashTable *h = gdbio_get_results(resp, list);
341
if (reason_is("function-finished"))
343
gdbio_do_status(GdbStopped);
344
do_step_func(h, reason);
348
handle_response_lines(list);
351
g_hash_table_destroy(h);
357
return_function(gint seq, gchar ** list, gchar * resp)
359
GHashTable *h = gdbio_get_results(resp, list);
363
do_step_func(h, "returned");
367
handle_response_lines(list);
375
watchpoint_trigger(GHashTable * h, GHashTable * wp, gchar * reason)
382
gchar *readval = gdblx_lookup_string(value, "value");
385
gdbio_info_func("%s #%s expression:%s old-value:%s new-value:%s\n",
386
reason, number ? number : "?", exp ? exp : "?", old, new);
392
gdbio_info_func("%s #%s expression:%s value:%s", reason,
393
number ? number : "?", exp ? exp : "?", old);
399
gdbio_info_func("%s #%s expression:%s value:%s", reason,
400
number ? number : "?", exp ? exp : "?", new);
406
gdbio_info_func("%s #%s expression:%s value:%s", reason,
407
number ? number : "?", exp ? exp : "?",
412
gdbio_info_func("%s #%s expression:%s", reason,
413
number ? number : "?", exp ? exp : "?");
421
handle_results_hash(GHashTable * h, gchar * rectype, gchar ** list)
423
if (g_str_equal(rectype, "^error"))
429
if (g_str_equal(msg, "unknown error"))
431
gint len = g_strv_length(list);
432
if ((len > 1) && list[len - 2] && *list[len - 2])
439
tmp = g_strcompress(tmp);
444
gdbio_error_func(msg);
454
if (g_str_equal(rectype, "^done"))
460
HSTR(frame, fullname);
462
if (fullname && line)
464
return do_step_func(h, "done");
469
if (g_str_equal(rectype, "*stopped"))
476
if (reason_is("breakpoint-hit"))
478
if (gdblx_check_keyval(h, "bkptno", "1"))
480
gdbio_send_seq_cmd(parse_process_info,
481
"-interpreter-exec console \"info proc\"\n");
486
return (do_step_func(h, reason));
490
gdbio_set_running(FALSE);
491
if (reason_is("signal-received"))
493
HSTR(h, signal_name);
494
HSTR(h, signal_meaning);
499
HSTR(frame, fullname);
520
if (signal_name && signal_meaning && thread_id && frame &&
521
addr && func && file && fullname)
523
if (gdbio_setup.signal_func)
526
si.signal_name = signal_name;
527
si.signal_meaning = signal_meaning;
531
si.fullname = fullname;
534
gdbio_setup.signal_func(&si);
539
(_("Program received signal %s (%s) at %s in function %s() at %s:%s"),
540
signal_name, signal_meaning, addr, func, file,
548
if (reason_is("end-stepping-range"))
550
return do_step_func(h, reason);
552
if (reason_is("function-finished"))
554
return do_step_func(h, reason);
556
if (reason_is("location-reached"))
558
return do_step_func(h, reason);
560
if (reason_is("watchpoint-trigger"))
565
watchpoint_trigger(h, wpt, reason);
567
return do_step_func(h, reason);
569
if (reason_is("access-watchpoint-trigger"))
574
watchpoint_trigger(h, hw_awpt, reason);
576
return do_step_func(h, reason);
578
if (reason_is("read-watchpoint-trigger"))
583
watchpoint_trigger(h, hw_rwpt, reason);
585
return do_step_func(h, reason);
587
if (reason_is("watchpoint-scope"))
590
gdbio_info_func(_("Watchpoint #%s out of scope"), wpnum ? wpnum : "?");
591
gdbio_send_cmd("-exec-continue\n");
592
return do_step_func(h, reason);
595
if (reason_is("exited-signalled"))
597
HSTR(h, signal_name);
598
HSTR(h, signal_meaning);
599
gdbio_info_func(_("Program exited on signal %s (%s).\n"),
600
signal_name ? signal_name : "UNKNOWN",
601
signal_meaning ? signal_meaning : _("Unknown signal"));
602
gdbio_target_exited(signal_name);
605
if (reason_is("exited"))
612
ec = strtoull(exit_code, &tail, 8);
613
if ((!tail) || (*tail))
618
gdbio_info_func(_("Program exited with code %d [%s]\n"), ec,
619
exit_code ? exit_code : _("(unknown)"));
620
gdbio_target_exited(exit_code);
623
if (g_str_equal(reason, "exited-normally"))
625
gdbio_info_func(_("Program exited normally.\n"));
626
gdbio_target_exited("0");
636
handle_response_line(gchar * str, gchar ** list)
652
gint len = strlen(rv);
653
memmove(rv, rv + 1, len);
654
if (rv[len - 2] == '"')
659
rv = g_strcompress(rv);
667
gchar *comma = strchr(rv, ',');
670
GHashTable *h = gdblx_parse_results(comma + 1);
672
if (g_str_equal(rv, "*stopped"))
674
gdbio_do_status(GdbStopped);
676
if (!handle_results_hash(h, rv, list))
680
g_hash_table_destroy(h);
684
if (g_str_equal(rv, "^running"))
692
gdbio_do_status(GdbRunning);
694
gdbio_set_running(TRUE);
709
#define prompt "\n(gdb) \n"
713
#define starts_with_token(resp) \
714
( IsDigit(resp[0]) && IsDigit(resp[1]) && \
715
IsDigit(resp[2]) && IsDigit(resp[3]) && \
716
IsDigit(resp[4]) && IsDigit(resp[5]) && \
717
strchr("^*=+", resp[6]) )
721
gdbio_consume_response(GString * recv_buf)
728
eos = strstr(recv_buf->str, prompt);
737
gchar seqbuf[SEQ_LEN + 2];
738
ResponseHandler handler = NULL;
742
lines = g_strsplit(recv_buf->str, "\n", 0);
744
len = g_strv_length(lines);
745
g_string_erase(recv_buf, 0, (eos - recv_buf->str) + 8);
748
gchar *resp = lines[len - 1];
749
if (starts_with_token(resp))
751
strncpy(seqbuf, resp, SEQ_LEN);
752
seqbuf[SEQ_LEN] = '\0';
753
seq = gdbio_atoi(seqbuf);
756
handler = gdbio_seq_lookup(seq);
759
memmove(resp, resp + SEQ_LEN,
760
strlen(resp + SEQ_LEN) + 1);
762
handler(seq, lines, resp);
770
("***Error: Could not find handler for token #%s\n",
778
handle_response_lines(lines);
794
gdbio_send_cmd("-exec-continue\n");
803
gdbio_send_seq_cmd(return_function, "-exec-return\n");
810
gdbio_send_seq_cmd(finish_function, "-exec-finish\n");