~ubuntu-branches/debian/sid/geany-plugins/sid

« back to all changes in this revision

Viewing changes to geanygdb/src/gdb-io-run.c

  • Committer: Bazaar Package Importer
  • Author(s): Chow Loong Jin
  • Date: 2009-07-10 22:56:41 UTC
  • Revision ID: james.westby@ubuntu.com-20090710225641-xc1126t7pq0jmpos
Tags: upstream-0.17.1
ImportĀ upstreamĀ versionĀ 0.17.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/*
 
3
 * gdb-io-run.c - Process execution and input functions for GDB wrapper library.
 
4
 *
 
5
 * See the file "gdb-io.h" for license information.
 
6
 *
 
7
 */
 
8
 
 
9
 
 
10
#include <unistd.h>
 
11
#include <signal.h>
 
12
#include <string.h>
 
13
#include <glib.h>
 
14
#include "gdb-io-priv.h"
 
15
#include "support.h"
 
16
 
 
17
 
 
18
extern gint g_unlink(const gchar * filename);
 
19
 
 
20
 
 
21
GdbIoSetup gdbio_setup;
 
22
 
 
23
 
 
24
static gchar *gdbio_args[] = { "gdb", "--interpreter=mi", "-nx", NULL };
 
25
 
 
26
static GPid gdbio_pid = 0;
 
27
static GPid target_pid = 0;
 
28
static GPid xterm_pid = 0;
 
29
 
 
30
static GSource *gdbio_src;
 
31
static gint gdbio_in;
 
32
static gint gdbio_out;
 
33
static GIOChannel *gdbio_ch_in;
 
34
static GIOChannel *gdbio_ch_out;
 
35
static guint gdbio_id_in;
 
36
static guint gdbio_id_out;
 
37
 
 
38
static GString send_buf = { NULL, 0, 0 };
 
39
static GString recv_buf = { NULL, 0, 0 };
 
40
 
 
41
static gchar *xterm_tty_file = NULL;
 
42
 
 
43
 
 
44
static gint sequence = SEQ_MIN;
 
45
static gboolean is_running = FALSE;
 
46
static gint process_token = 0;
 
47
 
 
48
 
 
49
/*
 
50
  Hash table to associate a "tokenized" GDB command with a function call.
 
51
  This stores a list of key-value pairs where the unique sequence-number
 
52
  (GDB token) is the key, and a ResponseHandler function pointer is the value.
 
53
*/
 
54
static GHashTable *sequencer;
 
55
 
 
56
 
 
57
 
 
58
#if !GLIB_CHECK_VERSION(2, 14, 0)
 
59
static void
 
60
g_string_append_vprintf(GString *str, const gchar *fmt, va_list args)
 
61
{
 
62
        gchar *tmp = g_strdup_vprintf(fmt, args);
 
63
        g_string_append(str, tmp);
 
64
        g_free(tmp);
 
65
}
 
66
#endif
 
67
 
 
68
 
 
69
/* Add a handler function to the sequencer */
 
70
gint
 
71
gdbio_send_seq_cmd(ResponseHandler func, const gchar * fmt, ...)
 
72
{
 
73
        va_list args;
 
74
        if (!gdbio_pid)
 
75
        {
 
76
                return 0;
 
77
        }
 
78
        if (sequence >= SEQ_MAX)
 
79
        {
 
80
                sequence = SEQ_MIN;
 
81
        }
 
82
        else
 
83
        {
 
84
                sequence++;
 
85
        }
 
86
        if (!sequencer)
 
87
        {
 
88
                sequencer = g_hash_table_new(g_direct_hash, g_direct_equal);
 
89
        }
 
90
        g_hash_table_insert(sequencer, GINT_TO_POINTER(sequence), func);
 
91
        g_string_append_printf(&send_buf, "%d", sequence);
 
92
        va_start(args, fmt);
 
93
        g_string_append_vprintf(&send_buf, fmt, args);
 
94
        va_end(args);
 
95
        return sequence;
 
96
}
 
97
 
 
98
 
 
99
ResponseHandler
 
100
gdbio_seq_lookup(gint seq)
 
101
{
 
102
        return g_hash_table_lookup(sequencer, GINT_TO_POINTER(seq));
 
103
}
 
104
 
 
105
 
 
106
void
 
107
gdbio_pop_seq(gint seq)
 
108
{
 
109
        g_hash_table_remove(sequencer, GINT_TO_POINTER(seq));
 
110
}
 
111
 
 
112
static gboolean
 
113
gerror(gchar * msg, GError ** err)
 
114
{
 
115
        if (*err)
 
116
        {
 
117
                if (msg)
 
118
                {
 
119
                        gdbio_error_func("%s\n%s\n", msg, (*err)->message);
 
120
                }
 
121
                else
 
122
                {
 
123
                        gdbio_error_func("%s\n", (*err)->message);
 
124
                }
 
125
                g_error_free(*err);
 
126
                *err = NULL;
 
127
                return TRUE;
 
128
        }
 
129
        else
 
130
        {
 
131
                return FALSE;
 
132
        }
 
133
}
 
134
 
 
135
 
 
136
gint
 
137
gdbio_atoi(gchar * str)
 
138
{
 
139
        gchar *tail = NULL;
 
140
        gint rv = strtol(str, &tail, 10);
 
141
        return (tail && !*tail) ? rv : -1;
 
142
}
 
143
 
 
144
 
 
145
void
 
146
gdbio_error_func(gchar * fmt, ...)
 
147
{
 
148
        va_list args;
 
149
        gchar *msg;
 
150
        va_start(args, fmt);
 
151
        msg = g_strdup_vprintf(fmt, args);
 
152
        if (gdbio_setup.error_func)
 
153
        {
 
154
                gdbio_setup.error_func(g_strstrip(msg));
 
155
        }
 
156
        else
 
157
        {
 
158
                g_printerr("%s", msg);
 
159
        }
 
160
        g_free(msg);
 
161
        va_end(args);
 
162
}
 
163
 
 
164
 
 
165
void
 
166
gdbio_info_func(gchar * fmt, ...)
 
167
{
 
168
        va_list args;
 
169
        gchar *msg;
 
170
        va_start(args, fmt);
 
171
        msg = g_strdup_vprintf(fmt, args);
 
172
        if (gdbio_setup.info_func)
 
173
        {
 
174
                gdbio_setup.info_func(g_strstrip(msg));
 
175
        }
 
176
        else
 
177
        {
 
178
                g_printerr("%s", msg);
 
179
        }
 
180
        g_free(msg);
 
181
        va_end(args);
 
182
}
 
183
 
 
184
 
 
185
gint
 
186
gdbio_wait(gint ms)
 
187
{
 
188
        struct timespec req = { 0, 0 }, rem =
 
189
        {
 
190
        0, 0};
 
191
        gint rms = ms;
 
192
        if (ms >= 1000)
 
193
        {
 
194
                req.tv_sec = ms / 1000;
 
195
                rms = ms % 1000;
 
196
        }
 
197
        req.tv_nsec = rms * 1000000;    /* 1 millisecond = 1,000,000 nanoseconds */
 
198
        do
 
199
        {
 
200
                nanosleep(&req, &rem);
 
201
                if ((rem.tv_sec || rem.tv_nsec))
 
202
                {
 
203
                        memcpy(&req, &rem, sizeof(req));
 
204
                        memset(&rem, 0, sizeof(rem));
 
205
                }
 
206
                else
 
207
                {
 
208
                        break;
 
209
                }
 
210
 
 
211
        }
 
212
        while (1);
 
213
        return ms;
 
214
}
 
215
 
 
216
 
 
217
 
 
218
void
 
219
gdbio_send_cmd(const gchar * fmt, ...)
 
220
{
 
221
        va_list args;
 
222
        if (!gdbio_pid)
 
223
        {
 
224
                return;
 
225
        }
 
226
        va_start(args, fmt);
 
227
        g_string_append_vprintf(&send_buf, fmt, args);
 
228
        va_end(args);
 
229
}
 
230
 
 
231
 
 
232
 
 
233
void
 
234
gdbio_set_running(gboolean running)
 
235
{
 
236
        is_running = running;
 
237
}
 
238
 
 
239
 
 
240
 
 
241
static void
 
242
kill_xterm()
 
243
{
 
244
        if (xterm_pid)
 
245
        {
 
246
                kill(xterm_pid, SIGKILL);
 
247
                xterm_pid = 0;
 
248
        }
 
249
}
 
250
 
 
251
 
 
252
 
 
253
static gchar *
 
254
start_xterm(gchar * term_cmd)
 
255
{
 
256
        gchar *term_args[] = { "xterm", "-title", "Debug terminal", "-e", NULL, NULL, NULL };
 
257
        GError *err = NULL;
 
258
        gint i = 0;
 
259
        gchar *tty_name = NULL;
 
260
        const gchar *exe_name = basename(term_cmd);
 
261
        gchar *all;
 
262
        if (!gdbio_setup.temp_dir)
 
263
        {
 
264
                gdbio_error_func(_("tty temporary directory not specified!\n"));
 
265
                return NULL;
 
266
        }
 
267
        if (!g_file_test(gdbio_setup.temp_dir, G_FILE_TEST_IS_DIR))
 
268
        {
 
269
                gdbio_error_func(_("tty temporary directory not found!\n"));
 
270
                return NULL;
 
271
        }
 
272
        if (!xterm_tty_file)
 
273
        {
 
274
                xterm_tty_file = g_strdup_printf("%s/%d.tty", gdbio_setup.temp_dir, getpid());
 
275
        }
 
276
        if (g_file_set_contents(xterm_tty_file, "", -1, &err))
 
277
        {
 
278
                g_unlink(xterm_tty_file);
 
279
        }
 
280
        else
 
281
        {
 
282
                gerror("writing ttyname logfile", &err);
 
283
                g_unlink(xterm_tty_file);
 
284
                return FALSE;
 
285
        }
 
286
        if (!gdbio_setup.tty_helper)
 
287
        {
 
288
                gdbio_error_func(_("tty helper program not specified!\n"));
 
289
                return NULL;
 
290
        }
 
291
        if (!
 
292
            (g_file_test(gdbio_setup.tty_helper, G_FILE_TEST_IS_EXECUTABLE) &&
 
293
             g_file_test(gdbio_setup.tty_helper, G_FILE_TEST_IS_REGULAR)))
 
294
        {
 
295
                gdbio_error_func(_("tty helper program not found!\n"));
 
296
                return NULL;
 
297
        }
 
298
        term_args[0] = term_cmd;
 
299
        if (g_str_equal(exe_name, "xterm") || g_str_equal(exe_name, "konsole"))
 
300
        {
 
301
                term_args[1] = "-T";
 
302
        }
 
303
        else
 
304
        {
 
305
                if (g_str_equal(exe_name, "gnome-terminal"))
 
306
                {
 
307
                        term_args[1] = "--title";
 
308
                        term_args[3] = "-x";
 
309
                }
 
310
                else
 
311
                {
 
312
                        if (g_str_equal(exe_name, "rxvt") || g_str_equal(exe_name, "urxvt"))
 
313
                        {
 
314
                                term_args[1] = "-title";
 
315
                        }
 
316
                        else
 
317
                        {
 
318
                                term_args[1] = "-e";
 
319
                                term_args[2] = NULL;
 
320
                        }
 
321
                }
 
322
        }
 
323
        i = 0;
 
324
        while (term_args[i])
 
325
        {
 
326
                i++;
 
327
        }
 
328
        term_args[i] = gdbio_setup.tty_helper;
 
329
        term_args[i + 1] = xterm_tty_file;
 
330
        all = g_strjoinv("\"  \"", term_args);
 
331
        gdbio_info_func("\"%s\"\n", all);
 
332
        g_free(all);
 
333
        if (g_spawn_async(NULL, term_args, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, &xterm_pid, &err))
 
334
        {
 
335
                gchar *contents = NULL;
 
336
                gsize len;
 
337
                gint ms = 0;
 
338
                do
 
339
                {
 
340
                        if (g_file_test(xterm_tty_file, G_FILE_TEST_EXISTS))
 
341
                        {
 
342
                                if (g_file_get_contents(xterm_tty_file, &contents, &len, &err))
 
343
                                {
 
344
                                        g_strstrip(contents);
 
345
                                        if (strlen(contents))
 
346
                                        {
 
347
                                                tty_name = g_strdup(contents);
 
348
                                                gdbio_info_func(_("Attaching to terminal %s\n"),
 
349
                                                                tty_name);
 
350
                                        }
 
351
                                        break;
 
352
                                }
 
353
                                else
 
354
                                {
 
355
                                        gerror("Error getting tty name:", &err);
 
356
                                }
 
357
                        }
 
358
                        ms += gdbio_wait(250);
 
359
                }
 
360
                while (ms <= 10000);
 
361
                if (ms > 10000)
 
362
                {
 
363
                        gdbio_error_func(_("Timeout waiting for TTY name.\n"));
 
364
                        kill_xterm();
 
365
                }
 
366
        }
 
367
        else
 
368
        {
 
369
                gerror("Error starting terminal: ", &err);
 
370
        }
 
371
        g_unlink(xterm_tty_file);
 
372
        return tty_name;
 
373
}
 
374
 
 
375
 
 
376
 
 
377
static void
 
378
free_buf(GString * buf)
 
379
{
 
380
        if (buf->str)
 
381
        {
 
382
                g_free(buf->str);
 
383
                buf->str = NULL;
 
384
                buf->len = 0;
 
385
                buf->allocated_len = 0;
 
386
        }
 
387
}
 
388
 
 
389
static void
 
390
shutdown_channel(GIOChannel ** ch)
 
391
{
 
392
        if (*ch)
 
393
        {
 
394
                GError *err = NULL;
 
395
                gint fd = g_io_channel_unix_get_fd(*ch);
 
396
                g_io_channel_shutdown(*ch, TRUE, &err);
 
397
                gerror("Shutting down channel", &err);
 
398
                g_io_channel_unref(*ch);
 
399
                *ch = NULL;
 
400
                if (fd >= 0)
 
401
                {
 
402
                        close(fd);
 
403
                }
 
404
        }
 
405
}
 
406
 
 
407
 
 
408
static void
 
409
on_gdb_exit(GPid pid, gint status, gpointer data)
 
410
{
 
411
        gdbio_pid = 0;
 
412
        gdbio_info_func(_("GDB exited (pid=%d)\n"), pid);
 
413
        g_spawn_close_pid(pid);
 
414
 
 
415
 
 
416
        g_source_remove(gdbio_id_in);
 
417
        shutdown_channel(&gdbio_ch_in);
 
418
 
 
419
        g_source_remove(gdbio_id_out);
 
420
        shutdown_channel(&gdbio_ch_out);
 
421
 
 
422
        free_buf(&send_buf);
 
423
        if (recv_buf.len)
 
424
        {
 
425
                gdbio_info_func("%s\n", recv_buf.str);
 
426
        }
 
427
        free_buf(&recv_buf);
 
428
 
 
429
        if (target_pid)
 
430
        {
 
431
                kill(target_pid, SIGKILL);
 
432
                target_pid = 0;
 
433
        }
 
434
        gdbio_set_running(FALSE);
 
435
        gdblx_scanner_done();
 
436
        gdbio_do_status(GdbDead);
 
437
}
 
438
 
 
439
 
 
440
 
 
441
static gboolean
 
442
on_send_to_gdb(GIOChannel * src, GIOCondition cond, gpointer data)
 
443
{
 
444
        GIOStatus st;
 
445
        GError *err = NULL;
 
446
        gsize count;
 
447
        if (send_buf.len)
 
448
        {
 
449
                while (send_buf.len)
 
450
                {
 
451
                        st = g_io_channel_write_chars(src, send_buf.str, send_buf.len, &count,
 
452
                                                      &err);
 
453
                        g_string_erase(&send_buf, 0, count);
 
454
                        if (err || (st == G_IO_STATUS_ERROR) || (st == G_IO_STATUS_EOF))
 
455
                        {
 
456
                                gerror("Error sending command", &err);
 
457
                                break;
 
458
                        }
 
459
                }
 
460
                st = g_io_channel_flush(src, &err);
 
461
                gerror("Error pushing command", &err);
 
462
        }
 
463
        do_loop();
 
464
        gdbio_wait(10);
 
465
        return TRUE;
 
466
}
 
467
 
 
468
 
 
469
 
 
470
void
 
471
gdbio_target_exited(gchar * reason)
 
472
{
 
473
        gdbio_info_func(_("Target process exited. (pid=%d; %s%s)\n"), target_pid,
 
474
                        reason
 
475
                        && g_ascii_isdigit(reason[0]) ? _("code=") : _("reason:"),
 
476
                        reason ? reason : "unknown");
 
477
        target_pid = 0;
 
478
        kill_xterm();
 
479
        gdbio_set_running(FALSE);
 
480
        gdbio_do_status(GdbFinished);
 
481
        if (process_token)
 
482
        {
 
483
                gdbio_pop_seq(process_token);
 
484
                process_token = 0;
 
485
        }
 
486
}
 
487
 
 
488
static GdbStatus gdbio_status = GdbDead;
 
489
 
 
490
void
 
491
gdbio_do_status(GdbStatus s)
 
492
{
 
493
        gdbio_status = s;
 
494
        if (gdbio_setup.status_func)
 
495
        {
 
496
                gdbio_setup.status_func(s);
 
497
        }
 
498
}
 
499
 
 
500
 
 
501
 
 
502
void
 
503
gdbio_pause_target()
 
504
{
 
505
        if (target_pid)
 
506
        {
 
507
                kill(target_pid, SIGINT);
 
508
        }
 
509
}
 
510
 
 
511
 
 
512
 
 
513
static void
 
514
target_killed(gint seq, gchar ** list, gchar * resp)
 
515
{
 
516
        GHashTable *h = gdbio_get_results(resp, list);
 
517
        gdbio_pop_seq(seq);
 
518
        if (h)
 
519
        {
 
520
                g_hash_table_destroy(h);
 
521
        }
 
522
        if (strncmp(resp, "^done", 5) == 0)
 
523
        {
 
524
                gdbio_target_exited("killed by GDB");
 
525
        }
 
526
}
 
527
 
 
528
 
 
529
 
 
530
void
 
531
gdbio_kill_target(gboolean force)
 
532
{
 
533
        if (target_pid)
 
534
        {
 
535
                gchar pidstr[64];
 
536
                GPid this_pid = target_pid;
 
537
                gint ms = 0;
 
538
                snprintf(pidstr, sizeof(pidstr) - 1, "/proc/%d", target_pid);
 
539
                if (!g_file_test(pidstr, G_FILE_TEST_IS_DIR))
 
540
                {
 
541
                        gdbio_info_func(_("Directory %s not found!\n"), pidstr);
 
542
                        pidstr[0] = '\0';
 
543
                }
 
544
                if (!force)
 
545
                {
 
546
                        gdbio_info_func(_("Shutting down target program.\n"));
 
547
                        gdbio_send_seq_cmd(target_killed, "kill SIGKILL\n");
 
548
                        gdbio_wait(250);
 
549
                        do_loop();
 
550
                }
 
551
                else
 
552
                {
 
553
                        gdbio_info_func(_("Killing target program.\n"));
 
554
                        kill(this_pid, SIGKILL);
 
555
                }
 
556
                while (1)
 
557
                {
 
558
                        do_loop();
 
559
                        if (ms >= 2000)
 
560
                        {
 
561
                                gdbio_info_func(_("Timeout waiting for target process.\n"));
 
562
                                if (!force)
 
563
                                {
 
564
                                        gdbio_info_func(_("Using a bigger hammer!\n"));
 
565
                                        gdbio_kill_target(TRUE);
 
566
                                }
 
567
                                break;
 
568
                        }
 
569
                        if (target_pid != this_pid)
 
570
                        {
 
571
                                break;
 
572
                        }
 
573
                        if ((pidstr[0]) && !g_file_test(pidstr, G_FILE_TEST_EXISTS))
 
574
                        {
 
575
                                break;
 
576
                        }
 
577
                        if (!(ms % 1000))
 
578
                                gdbio_info_func(_("Waiting for target process to exit.\n"));
 
579
                        ms += gdbio_wait(250);
 
580
                }
 
581
        }
 
582
        kill_xterm();
 
583
}
 
584
 
 
585
static gboolean
 
586
have_console()
 
587
{
 
588
        return (gdbio_status == GdbLoaded) || (gdbio_status == GdbStopped)
 
589
                || (gdbio_status == GdbFinished);
 
590
}
 
591
 
 
592
void
 
593
gdbio_exit()
 
594
{
 
595
        gdbio_kill_target(!have_console());
 
596
        if (gdbio_pid)
 
597
        {
 
598
                GPid this_gdb = gdbio_pid;
 
599
                gint ms = 0;
 
600
                gchar pidstr[64];
 
601
                snprintf(pidstr, sizeof(pidstr) - 1, "/proc/%d", this_gdb);
 
602
                if (is_running)
 
603
                {
 
604
                        if (!g_file_test(pidstr, G_FILE_TEST_IS_DIR))
 
605
                        {
 
606
                                gdbio_info_func(_("Directory %s not found!\n"), pidstr);
 
607
                                pidstr[0] = '\0';
 
608
                        }
 
609
                        do
 
610
                        {
 
611
                                do_loop();
 
612
                                if (gdbio_pid == this_gdb)
 
613
                                {
 
614
                                        gdbio_info_func(_("Killing GDB (pid=%d)\n"), this_gdb);
 
615
                                }
 
616
                                else
 
617
                                {
 
618
                                        break;
 
619
                                }
 
620
                                kill(this_gdb, SIGKILL);
 
621
                                ms += gdbio_wait(500);
 
622
                                if (pidstr[0] && !g_file_test(pidstr, G_FILE_TEST_EXISTS))
 
623
                                {
 
624
                                        break;
 
625
                                }
 
626
                                if (ms > 2000)
 
627
                                {
 
628
                                        gdbio_error_func(_("Timeout trying to kill GDB.\n"));
 
629
                                        break;
 
630
                                }
 
631
                        }
 
632
                        while (1);
 
633
                        free_buf(&send_buf);
 
634
                        gdbio_wait(500);
 
635
                }
 
636
                else
 
637
                {
 
638
                        gdbio_info_func(_("Shutting down GDB\n"));
 
639
                        gdbio_send_cmd("-gdb-exit\n");
 
640
                        while (1)
 
641
                        {
 
642
                                do_loop();
 
643
                                ms += gdbio_wait(250);
 
644
                                if (pidstr[0] && !g_file_test(pidstr, G_FILE_TEST_EXISTS))
 
645
                                {
 
646
                                        break;
 
647
                                }
 
648
                                if (gdbio_pid == this_gdb)
 
649
                                {
 
650
                                        if (!(ms % 1000))
 
651
                                                gdbio_info_func(_("Waiting for GDB to exit.\n"));
 
652
                                }
 
653
                                else
 
654
                                {
 
655
                                        break;
 
656
                                }
 
657
                                if (ms > 2000)
 
658
                                {
 
659
                                        gdbio_info_func(_("Timeout waiting for GDB to exit.\n"));
 
660
                                        gdbio_set_running(TRUE);
 
661
                                        gdbio_exit();
 
662
                                        break;
 
663
                                }
 
664
                        }
 
665
                }
 
666
        }
 
667
        if (sequencer)
 
668
        {
 
669
                g_hash_table_destroy(sequencer);
 
670
                sequencer = NULL;
 
671
        }
 
672
        g_free(xterm_tty_file);
 
673
        xterm_tty_file = NULL;
 
674
}
 
675
 
 
676
 
 
677
 
 
678
void gdbio_parse_file_list(gint seq, gchar ** list, gchar * resp);
 
679
 
 
680
static void
 
681
load_target(const gchar * exe_name)
 
682
{
 
683
        gdbio_set_running(FALSE);
 
684
        gdbio_send_cmd("-file-exec-and-symbols %s\n", exe_name);
 
685
        gdbio_send_seq_cmd(gdbio_parse_file_list, "-file-list-exec-source-files\n");
 
686
 
 
687
}
 
688
 
 
689
 
 
690
 
 
691
static gboolean
 
692
on_read_from_gdb(GIOChannel * src, GIOCondition cond, gpointer data)
 
693
{
 
694
        gchar buf[1024];
 
695
        GIOStatus st;
 
696
        GError *err = NULL;
 
697
        gsize count;
 
698
        st = g_io_channel_read_chars(src, buf, sizeof(buf) - 1, &count, &err);
 
699
        buf[count] = '\0';
 
700
        g_string_append_len(&recv_buf, buf, count);
 
701
        gerror("Error reading response", &err);
 
702
        gdbio_consume_response(&recv_buf);
 
703
        gdbio_wait(10);
 
704
        return TRUE;
 
705
}
 
706
 
 
707
 
 
708
#define GDB_SPAWN_FLAGS \
 
709
G_SPAWN_SEARCH_PATH | \
 
710
G_SPAWN_DO_NOT_REAP_CHILD
 
711
 
 
712
 
 
713
void
 
714
gdbio_load(const gchar * exe_name)
 
715
{
 
716
        GError *err = NULL;
 
717
        gdbio_exit();
 
718
        if (g_spawn_async_with_pipes(NULL, gdbio_args, NULL,
 
719
                                     GDB_SPAWN_FLAGS, NULL,
 
720
                                     NULL, &gdbio_pid, &gdbio_in, &gdbio_out, NULL, &err))
 
721
        {
 
722
                gdbio_info_func(_("Starting gdb (pid=%d)\n"), gdbio_pid);
 
723
 
 
724
                g_child_watch_add(gdbio_pid, on_gdb_exit, NULL);
 
725
                gdbio_src = g_child_watch_source_new(gdbio_pid);
 
726
 
 
727
                gdbio_ch_in = g_io_channel_unix_new(gdbio_in);
 
728
                g_io_channel_set_encoding(gdbio_ch_in, NULL, &err);
 
729
                gerror("Error setting encoding", &err);
 
730
                g_io_channel_set_buffered(gdbio_ch_in, FALSE);
 
731
 
 
732
                gdbio_ch_out = g_io_channel_unix_new(gdbio_out);
 
733
                g_io_channel_set_encoding(gdbio_ch_out, NULL, &err);
 
734
                gerror("Error setting encoding", &err);
 
735
                g_io_channel_set_buffered(gdbio_ch_out, FALSE);
 
736
 
 
737
                gdbio_id_in = g_io_add_watch(gdbio_ch_in, G_IO_OUT, on_send_to_gdb, NULL);
 
738
                gdbio_id_out = g_io_add_watch(gdbio_ch_out, G_IO_IN, on_read_from_gdb, NULL);
 
739
 
 
740
                gdbio_send_cmd("-gdb-set width 0\n-gdb-set height 0\n");
 
741
                if (exe_name)
 
742
                {
 
743
                        load_target(exe_name);
 
744
                }
 
745
        }
 
746
        else
 
747
        {
 
748
                gerror("Error starting debugger.", &err);
 
749
        }
 
750
}
 
751
 
 
752
 
 
753
 
 
754
void
 
755
gdbio_exec_target(gchar * terminal_command)
 
756
{
 
757
        if (terminal_command)
 
758
        {
 
759
                gchar *tty_name = start_xterm(terminal_command);
 
760
                if (tty_name)
 
761
                {
 
762
                        gdbio_send_cmd("-inferior-tty-set %s\n", tty_name);
 
763
                        g_free(tty_name);
 
764
                }
 
765
                else
 
766
                        return;
 
767
        }
 
768
        if (process_token)
 
769
        {
 
770
                gdbio_pop_seq(process_token);
 
771
        }
 
772
        gdbio_set_starting(TRUE);
 
773
        gdbio_do_status(GdbStartup);
 
774
        process_token = gdbio_send_seq_cmd(gdbio_target_started, "-exec-run\n");
 
775
}
 
776
 
 
777
 
 
778
 
 
779
void
 
780
gdbio_set_target_pid(GPid pid)
 
781
{
 
782
        gdbio_info_func(_("Started target process. (pid=%d)\n"), pid);
 
783
        target_pid = pid;
 
784
}
 
785
 
 
786
 
 
787
 
 
788
GPid
 
789
gdbio_get_target_pid()
 
790
{
 
791
        return target_pid;
 
792
}