* plugins/gdb/debugger.c:
[anjuta-git-plugin.git] / plugins / gdb / debugger.c
blobba40bf013d40fcaaf0638ddcca322a54d846c79f
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 * debugger.c Copyright (C) 2000 Kh. Naba Kumar Singh
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc., 59
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
23 /*#define DEBUG*/
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28 #include <signal.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <ctype.h>
32 #include <sys/wait.h>
33 #include <errno.h>
34 #include <assert.h>
35 #include <fcntl.h>
37 #include <glib.h>
39 #include <libanjuta/anjuta-launcher.h>
40 #include <libanjuta/anjuta-debug.h>
41 #include <libanjuta/anjuta-marshal.h>
42 #include <libanjuta/interfaces/ianjuta-debugger-breakpoint.h>
43 #include <libanjuta/interfaces/ianjuta-debugger-register.h>
44 #include <libanjuta/interfaces/ianjuta-debugger-memory.h>
45 #include <libanjuta/interfaces/ianjuta-debugger-instruction.h>
46 #include <libanjuta/interfaces/ianjuta-debugger-variable.h>
48 #include "debugger.h"
49 #include "utilities.h"
51 #define ANJUTA_LOG_ENV "ANJUTA_LOG"
52 #define DEBUGGER_LOG_LEVEL 1
54 #define GDB_PROMPT "(gdb)"
55 #define FILE_BUFFER_SIZE 1024
56 #define GDB_PATH "gdb"
57 #define SUMMARY_MAX_LENGTH 90 /* Should be smaller than 4K to be displayed
58 * in GtkCellRendererCell */
60 enum {
61 DEBUGGER_NONE,
62 DEBUGGER_EXIT,
63 DEBUGGER_RERUN_PROGRAM
66 enum
68 PROGRAM_LOADED_SIGNAL,
69 PROGRAM_RUNNING_SIGNAL,
70 PROGRAM_EXITED_SIGNAL,
71 PROGRAM_STOPPED_SIGNAL,
72 RESULTS_ARRIVED_SIGNAL,
73 LOCATION_CHANGED_SIGNAL,
74 BREAKPOINT_CHANGED_SIGNAL,
75 VARIABLE_CHANGED_SIGNAL,
76 LAST_SIGNAL
79 struct _DebuggerPriv
81 GtkWindow *parent_win;
83 IAnjutaDebuggerOutputCallback output_callback;
84 gpointer output_user_data;
86 GList *search_dirs;
88 gboolean prog_is_running;
89 gboolean prog_is_attached;
90 gboolean prog_is_loaded;
91 gboolean debugger_is_started;
92 guint debugger_is_busy;
93 gint post_execution_flag;
95 AnjutaLauncher *launcher;
96 GString *stdo_line;
97 GString *stdo_acc;
98 GString *stde_line;
100 GList *cli_lines;
102 /* State */
103 gboolean solib_event;
104 gboolean stopping;
105 gboolean exiting;
106 gboolean starting;
107 gboolean terminating;
108 gboolean loading;
110 /* GDB command queue */
111 GList *cmd_queqe;
112 DebuggerCommand current_cmd;
113 gboolean skip_next_prompt;
114 gboolean command_output_sent;
116 pid_t inferior_pid;
117 gint current_thread;
118 guint current_frame;
120 GObject* instance;
122 IAnjutaMessageView *log;
123 gboolean gdb_log;
126 static gpointer parent_class;
128 static void debugger_queue_clear (Debugger *debugger);
129 static void debugger_queue_execute_command (Debugger *debugger);
131 static void gdb_stdout_line_arrived (Debugger *debugger, const gchar * line);
132 static void gdb_stderr_line_arrived (Debugger *debugger, const gchar * line);
133 static void debugger_stdo_flush (Debugger *debugger);
134 static void debugger_stde_flush (Debugger *debugger);
135 static void on_gdb_output_arrived (AnjutaLauncher *launcher,
136 AnjutaLauncherOutputType output_type,
137 const gchar *chars, gpointer data);
138 static void on_gdb_terminated (AnjutaLauncher *launcher,
139 gint child_pid, gint status,
140 gulong t, gpointer data);
142 static void debugger_class_init (DebuggerClass *klass);
143 static void debugger_instance_init (Debugger *debugger);
145 typedef struct _GdbGListPacket
147 GList* list;
148 gint tag;
149 } GdbGListPacket;
151 /* Useful functions
152 *---------------------------------------------------------------------------*/
154 static gchar * gdb_quote (const gchar *unquoted_string)
156 const char *p;
157 g_return_val_if_fail (unquoted_string != NULL, NULL);
160 p = strpbrk (unquoted_string, "\"\\");
161 if (p == NULL)
163 /* No need to quote anything */
164 return g_strdup (unquoted_string);
166 else
168 GString *dest;
170 dest = g_string_new_len (unquoted_string, p - unquoted_string);
171 for (;;)
173 g_string_append_c (dest, '\\');
174 unquoted_string = p;
175 p = strpbrk (unquoted_string + 1, "\"\\");
176 if (p == NULL)
178 g_string_append (dest, unquoted_string);
179 break;
181 else
183 g_string_append_len (dest, unquoted_string, p - unquoted_string);
186 return g_string_free (dest, FALSE);
190 typedef struct _GdbMessageCode GdbMessageCode;
192 struct _GdbMessageCode
194 const gchar *msg;
195 guint code;
198 const static GdbMessageCode GdbErrorMessage[] =
199 {{"mi_cmd_var_create: unable to create variable object",
200 IANJUTA_DEBUGGER_UNABLE_TO_CREATE_VARIABLE},
201 {"Cannot access memory at address ",
202 IANJUTA_DEBUGGER_UNABLE_TO_ACCESS_MEMORY},
203 {"No source file named ",
204 IANJUTA_DEBUGGER_UNABLE_TO_OPEN_FILE},
205 {NULL, 0}};
207 static guint
208 gdb_match_error(const gchar *message)
210 const GdbMessageCode* msg;
212 for (msg = GdbErrorMessage; msg->msg != NULL; msg++)
214 gsize len = strlen (msg->msg);
216 if (!isspace (msg->msg[len - 1]))
218 /* Match the whole string */
219 len++;
222 if (memcmp (msg->msg, message, len) == 0)
224 return msg->code;
228 return IANJUTA_DEBUGGER_UNKNOWN_ERROR;
231 static void
232 debugger_message_view_append (Debugger *debugger, IAnjutaMessageViewType type, const char *message)
234 guint len = strlen(message);
235 gchar buf[SUMMARY_MAX_LENGTH];
236 const gchar* summary = message;
237 const gchar* detail = "";
240 if (len > SUMMARY_MAX_LENGTH)
243 memcpy(buf, message, SUMMARY_MAX_LENGTH - 4);
244 memcpy(buf + SUMMARY_MAX_LENGTH - 4, "...", 4);
245 summary = buf;
246 detail = message;
249 ianjuta_message_view_append (debugger->priv->log, type, summary, detail, NULL);
252 static void
253 debugger_initialize (Debugger *debugger)
255 const gchar* anjuta_log;
257 DEBUG_PRINT ("In function: debugger_init()");
259 debugger->priv = g_new0 (DebuggerPriv, 1);
261 debugger->priv->output_callback = NULL;
262 debugger->priv->parent_win = NULL;
263 debugger->priv->search_dirs = NULL;
264 debugger->priv->launcher = anjuta_launcher_new ();
266 debugger->priv->debugger_is_started = FALSE;
267 debugger->priv->prog_is_running = FALSE;
268 debugger->priv->debugger_is_busy = 0;
269 debugger->priv->starting = FALSE;
270 debugger->priv->terminating = FALSE;
271 debugger->priv->skip_next_prompt = FALSE;
272 debugger->priv->command_output_sent = FALSE;
274 debugger->priv->current_cmd.cmd = NULL;
275 debugger->priv->current_cmd.parser = NULL;
277 debugger->priv->cmd_queqe = NULL;
278 debugger->priv->cli_lines = NULL;
279 debugger->priv->solib_event = FALSE;
281 debugger->priv->stdo_line = g_string_sized_new (FILE_BUFFER_SIZE);
282 g_string_assign (debugger->priv->stdo_line, "");
283 debugger->priv->stdo_acc = g_string_new ("");
285 debugger->priv->stde_line = g_string_sized_new (FILE_BUFFER_SIZE);
286 g_string_assign (debugger->priv->stde_line, "");
288 debugger->priv->post_execution_flag = DEBUGGER_NONE;
290 anjuta_log = g_getenv (ANJUTA_LOG_ENV);
291 debugger->priv->gdb_log = anjuta_log && (atoi(anjuta_log) > DEBUGGER_LOG_LEVEL);
294 static void
295 debugger_instance_init (Debugger *debugger)
297 debugger_initialize (debugger);
300 Debugger*
301 debugger_new (GtkWindow *parent_win, GObject* instance)
303 Debugger *debugger;
305 debugger = g_object_new (DEBUGGER_TYPE, NULL);
306 debugger->priv->parent_win = parent_win;
307 debugger->priv->instance = instance;
309 return debugger;
312 void
313 debugger_free (Debugger *debugger)
315 g_return_if_fail (IS_DEBUGGER (debugger));
317 g_object_unref (debugger);
320 gboolean
321 debugger_is_ready (Debugger *debugger)
323 g_return_val_if_fail (IS_DEBUGGER (debugger), FALSE);
324 return !debugger->priv->debugger_is_busy;
327 gboolean
328 debugger_program_is_running (Debugger *debugger)
330 g_return_val_if_fail (IS_DEBUGGER (debugger), FALSE);
331 return debugger->priv->prog_is_running;
334 gboolean
335 debugger_program_is_attached (Debugger *debugger)
337 g_return_val_if_fail (IS_DEBUGGER (debugger), FALSE);
338 return debugger->priv->prog_is_attached;
341 gboolean
342 debugger_program_is_loaded (Debugger *debugger)
344 g_return_val_if_fail (IS_DEBUGGER (debugger), FALSE);
345 return debugger->priv->prog_is_loaded;
348 static void
349 debugger_log_command (Debugger *debugger, const gchar *command)
351 gchar* str;
352 gsize len;
354 if (debugger->priv->log == NULL) return;
356 if (*command == '-')
358 str = g_strdup (command);
359 len = strlen (command);
361 /* Remove trailing carriage return */
362 if (str[len - 1] == '\n') str[len - 1] = '\0';
364 /* Log only MI command as other are echo */
365 #ifdef DEBUG
366 DEBUG_PRINT ("GDB:> %s", str);
367 #else
368 if (debugger->priv->gdb_log) g_message ("GDB:> %s", str);
369 #endif
370 debugger_message_view_append (debugger, IANJUTA_MESSAGE_VIEW_TYPE_NORMAL, str);
371 g_free (str);
375 static void
376 debugger_log_output (Debugger *debugger, const gchar *line)
378 gchar* str;
379 const gchar* start;
380 IAnjutaMessageViewType type;
381 gsize len;
383 if (debugger->priv->log == NULL) return;
385 type = IANJUTA_MESSAGE_VIEW_TYPE_NORMAL;
386 switch (*line)
388 case '~':
389 type = IANJUTA_MESSAGE_VIEW_TYPE_INFO;
390 /* Go through */
391 case '&':
392 len = strlen(line);
393 start = line + 1;
395 /* Remove double quote if necessary */
396 if ((line[1] == '\"') && (line[len - 1] == '\"')) start++;
397 str = g_strcompress (line + 2);
398 len = strlen (str);
399 if (start == line + 2)
401 str[len - 1] = '\0';
402 len--;
405 /* Remove trailing carriage return */
406 if (str[len - 1] == '\n') str[len - 1] = '\0';
408 debugger_message_view_append (debugger, type, str);
409 g_free (str);
410 break;
411 case '^':
412 if (strncmp(line + 1, "error", 5) == 0)
414 debugger_message_view_append (debugger, IANJUTA_MESSAGE_VIEW_TYPE_ERROR, line + 1);
416 else
418 debugger_message_view_append (debugger, IANJUTA_MESSAGE_VIEW_TYPE_WARNING, line + 1);
420 break;
421 case '@':
422 debugger_message_view_append (debugger, IANJUTA_MESSAGE_VIEW_TYPE_NORMAL, line + 1);
423 break;
424 default:
425 debugger_message_view_append (debugger, IANJUTA_MESSAGE_VIEW_TYPE_NORMAL, line);
426 break;
430 static void
431 debugger_emit_ready (Debugger *debugger)
433 if (!debugger->priv->debugger_is_busy)
435 if (debugger->priv->loading)
437 debugger->priv->starting = FALSE;
438 debugger->priv->loading = FALSE;
439 debugger->priv->exiting = FALSE;
440 debugger->priv->stopping = FALSE;
441 debugger->priv->solib_event = FALSE;
442 g_signal_emit_by_name (debugger->priv->instance, "debugger-ready", IANJUTA_DEBUGGER_PROGRAM_LOADED);
444 else if (debugger->priv->starting)
446 debugger->priv->starting = FALSE;
447 debugger->priv->loading = FALSE;
448 debugger->priv->exiting = FALSE;
449 debugger->priv->stopping = FALSE;
450 debugger->priv->solib_event = FALSE;
451 g_signal_emit_by_name (debugger->priv->instance, "debugger-ready", IANJUTA_DEBUGGER_STARTED);
453 else if (debugger->priv->exiting)
455 debugger->priv->exiting = FALSE;
456 debugger->priv->stopping = FALSE;
457 debugger->priv->solib_event = FALSE;
458 g_signal_emit_by_name (debugger->priv->instance, "debugger-ready", IANJUTA_DEBUGGER_PROGRAM_LOADED);
460 else if (debugger->priv->solib_event)
462 debugger->priv->exiting = FALSE;
463 debugger->priv->stopping = FALSE;
464 debugger->priv->solib_event = FALSE;
465 g_signal_emit_by_name (debugger->priv->instance, "sharedlib-event");
467 else if (debugger->priv->stopping)
469 debugger->priv->exiting = FALSE;
470 debugger->priv->stopping = FALSE;
471 debugger->priv->solib_event = FALSE;
472 g_signal_emit_by_name (debugger->priv->instance, "debugger-ready", IANJUTA_DEBUGGER_PROGRAM_STOPPED);
474 else
476 if (debugger->priv->prog_is_running || debugger->priv->prog_is_attached)
478 g_signal_emit_by_name (debugger->priv->instance, "debugger-ready", IANJUTA_DEBUGGER_PROGRAM_STOPPED);
480 else if (debugger->priv->prog_is_loaded)
482 g_signal_emit_by_name (debugger->priv->instance, "debugger-ready", IANJUTA_DEBUGGER_PROGRAM_LOADED);
484 else
486 g_signal_emit_by_name (debugger->priv->instance, "debugger-ready", IANJUTA_DEBUGGER_STARTED);
492 IAnjutaDebuggerState
493 debugger_get_state (Debugger *debugger)
495 if (debugger->priv->debugger_is_busy)
497 return IANJUTA_DEBUGGER_BUSY;
499 else
501 if (debugger->priv->prog_is_running || debugger->priv->prog_is_attached)
503 return IANJUTA_DEBUGGER_PROGRAM_STOPPED;
505 else if (debugger->priv->prog_is_loaded)
507 return IANJUTA_DEBUGGER_PROGRAM_LOADED;
509 else if (debugger->priv->debugger_is_started)
511 return IANJUTA_DEBUGGER_STARTED;
513 else
515 return IANJUTA_DEBUGGER_STOPPED;
520 static void
521 debugger_clear_buffers (Debugger *debugger)
523 DEBUG_PRINT ("In function: debugger_clear_buffers()");
525 /* Clear the output line buffer */
526 g_string_assign (debugger->priv->stdo_line, "");
527 if (!debugger->priv->current_cmd.keep_result)
528 g_string_assign (debugger->priv->stdo_acc, "");
530 /* Clear the error line buffer */
531 g_string_assign (debugger->priv->stde_line, "");
533 /* Clear CLI output lines */
534 g_list_foreach (debugger->priv->cli_lines, (GFunc)g_free, NULL);
535 g_list_free (debugger->priv->cli_lines);
536 debugger->priv->cli_lines = NULL;
539 static DebuggerCommand *
540 debugger_queue_get_next_command (Debugger *debugger)
542 DebuggerCommand *dc;
544 DEBUG_PRINT ("In function: debugger_get_next_command()");
546 if (debugger->priv->cmd_queqe)
548 dc = g_list_nth_data (debugger->priv->cmd_queqe, 0);
549 debugger->priv->cmd_queqe = g_list_remove (debugger->priv->cmd_queqe, dc);
551 else
552 dc = NULL;
553 return dc;
556 static gboolean
557 debugger_queue_set_next_command (Debugger *debugger)
559 DebuggerCommand *dc;
561 DEBUG_PRINT ("In function: debugger_set_next_command()");
563 dc = debugger_queue_get_next_command (debugger);
564 if (!dc)
566 debugger->priv->current_cmd.cmd = NULL;
567 debugger->priv->current_cmd.parser = NULL;
568 debugger->priv->current_cmd.callback = NULL;
569 debugger->priv->current_cmd.user_data = NULL;
570 debugger->priv->current_cmd.suppress_error = FALSE;
571 debugger->priv->current_cmd.keep_result = FALSE;
573 return FALSE;
575 g_free (debugger->priv->current_cmd.cmd);
576 debugger->priv->current_cmd.cmd = dc->cmd;
577 debugger->priv->current_cmd.parser = dc->parser;
578 debugger->priv->current_cmd.callback = dc->callback;
579 debugger->priv->current_cmd.user_data = dc->user_data;
580 debugger->priv->current_cmd.suppress_error = dc->suppress_error;
581 debugger->priv->current_cmd.keep_result = dc->keep_result;
582 g_free (dc);
584 return TRUE;
587 static void
588 debugger_queue_command (Debugger *debugger, const gchar *cmd,
589 gboolean suppress_error, gboolean keep_result,
590 DebuggerParserFunc parser,
591 IAnjutaDebuggerCallback callback, gpointer user_data)
593 DebuggerCommand *dc;
596 DEBUG_PRINT ("In function: debugger_queue_command (%s)", cmd);
598 dc = g_malloc (sizeof (DebuggerCommand));
599 if (dc)
601 dc->cmd = g_strdup(cmd);
602 dc->parser = parser;
603 dc->callback = callback;
604 dc->user_data = user_data;
605 dc->suppress_error = suppress_error;
606 dc->keep_result = keep_result;
608 debugger->priv->cmd_queqe = g_list_append (debugger->priv->cmd_queqe, dc);
609 debugger_queue_execute_command (debugger);
612 static void
613 debugger_queue_clear (Debugger *debugger)
615 GList *node;
617 DEBUG_PRINT ("In function: debugger_queue_clear()");
619 node = debugger->priv->cmd_queqe;
620 while (node)
622 g_free (((DebuggerCommand *)node->data)->cmd);
623 g_free (node->data);
624 node = g_list_next (node);
626 g_list_free (debugger->priv->cmd_queqe);
627 debugger->priv->cmd_queqe = NULL;
628 g_free (debugger->priv->current_cmd.cmd);
629 debugger->priv->current_cmd.cmd = NULL;
630 debugger->priv->current_cmd.parser = NULL;
631 debugger->priv->current_cmd.callback = NULL;
632 debugger->priv->current_cmd.user_data = NULL;
633 debugger->priv->current_cmd.suppress_error = FALSE;
634 debugger->priv->current_cmd.keep_result = FALSE;
635 debugger_clear_buffers (debugger);
638 static void
639 debugger_execute_command (Debugger *debugger, const gchar *command)
641 gchar *cmd;
643 DEBUG_PRINT ("In function: debugger_execute_command(%s) %d\n",command, debugger->priv->debugger_is_busy);
644 debugger->priv->debugger_is_busy++;
645 debugger->priv->command_output_sent = FALSE;
646 cmd = g_strconcat (command, "\n", NULL);
647 debugger_log_command (debugger, cmd);
648 anjuta_launcher_send_stdin (debugger->priv->launcher, cmd);
649 g_free (cmd);
652 static void
653 debugger_queue_execute_command (Debugger *debugger)
655 DEBUG_PRINT ("In function: debugger_queue_execute_command()");
657 if (!debugger->priv->debugger_is_busy &&
658 !debugger->priv->starting &&
659 g_list_length (debugger->priv->cmd_queqe) >= 1)
661 debugger_clear_buffers (debugger);
662 if (debugger_queue_set_next_command (debugger))
663 debugger_execute_command (debugger, debugger->priv->current_cmd.cmd);
667 static void
668 debugger_load_executable_finish (Debugger *debugger, const GDBMIValue *mi_results,
669 const GList *cli_results, GError *error)
671 DEBUG_PRINT ("Program loaded");
672 debugger->priv->prog_is_loaded = TRUE;
674 g_signal_emit_by_name (debugger->priv->instance, "program-loaded");
677 void
678 debugger_load_executable (Debugger *debugger, const gchar *prog)
680 gchar *command, *dir, *msg;
682 g_return_if_fail (IS_DEBUGGER (debugger));
683 g_return_if_fail (prog != NULL);
685 DEBUG_PRINT ("In function: debugger_load_executable(%s)", prog);
687 if (debugger->priv->output_callback)
689 msg = g_strconcat (_("Loading Executable: "), prog, "\n", NULL);
690 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT, msg,
691 debugger->priv->output_user_data);
692 g_free (msg);
695 command = g_strconcat ("-file-exec-and-symbols ", prog, NULL);
696 dir = g_path_get_dirname (prog);
697 /* TODO
698 anjuta_set_execution_dir(dir);
700 g_free (dir);
701 debugger_queue_command (debugger, command, FALSE, FALSE, debugger_load_executable_finish, NULL, NULL);
702 g_free (command);
703 debugger->priv->starting = TRUE;
704 debugger->priv->terminating = FALSE;
707 void
708 debugger_load_core (Debugger *debugger, const gchar *core)
710 gchar *command, *dir, *msg;
712 g_return_if_fail (IS_DEBUGGER (debugger));
713 g_return_if_fail (core != NULL);
715 DEBUG_PRINT ("In function: debugger_load_core(%s)", core);
717 if (debugger->priv->output_callback)
719 msg = g_strconcat (_("Loading Core: "), core, "\n", NULL);
720 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT, msg,
721 debugger->priv->output_user_data);
722 g_free (msg);
725 command = g_strconcat ("core ", core, NULL);
726 dir = g_path_get_dirname (core);
727 debugger->priv->search_dirs =
728 g_list_prepend (debugger->priv->search_dirs, dir);
729 debugger_queue_command (debugger, command, FALSE, FALSE, NULL, NULL, NULL);
730 g_free (command);
733 gboolean
734 debugger_start (Debugger *debugger, const GList *search_dirs,
735 const gchar *prog, gboolean is_libtool_prog)
737 gchar *command_str, *dir, *tmp, *text, *msg;
738 gchar *exec_dir;
739 gboolean ret;
740 const GList *node;
741 AnjutaLauncher *launcher;
742 GList *dir_list = NULL;
743 gchar *term = NULL;
745 DEBUG_PRINT ("In function: debugger_start(%s) libtool %d", prog == NULL ? "(null)" : prog, is_libtool_prog);
747 if (anjuta_util_prog_is_installed ("gdb", TRUE) == FALSE)
748 return FALSE;
750 debugger_queue_clear (debugger);
752 tmp = g_strconcat (PACKAGE_DATA_DIR, "/", "gdb.init", NULL);
753 if (g_file_test (tmp, G_FILE_TEST_IS_REGULAR) == FALSE)
755 anjuta_util_dialog_error (debugger->priv->parent_win,
756 _("Unable to find: %s.\n"
757 "Unable to initialize debugger.\n"
758 "Make sure Anjuta is installed correctly."),
759 tmp);
760 g_free (tmp);
761 return FALSE;
763 g_free (tmp);
765 /* Prepare source search directories */
766 exec_dir = NULL;
767 if (prog)
768 exec_dir = g_path_get_dirname (prog);
770 if (exec_dir)
772 gchar *quoted_exec_dir = gdb_quote (exec_dir);
773 dir = g_strconcat (" -directory=\"", quoted_exec_dir, "\"", NULL);
774 g_free (quoted_exec_dir);
775 dir_list = g_list_prepend (dir_list, exec_dir);
777 else
779 dir = g_strdup (" ");
782 node = search_dirs;
783 while (node)
785 text = node->data;
786 if (strncmp (text, "file://", 7) == 0)
788 text += 7;
790 else
792 g_warning ("Debugger source search uri '%s' is not a local uri", text);
795 if (text[0] == '/')
797 tmp = g_strconcat (dir, " -directory=", text, NULL);
798 g_free (dir);
799 dir = tmp;
801 dir_list = g_list_prepend (dir_list, g_strdup (text));
803 else
805 g_warning ("Debugger source search dir '%s' is not absolute",
806 text);
808 node = g_list_next (node);
811 /* Now save the dir list. Order is automatically revesed */
812 node = dir_list;
813 while (node)
815 debugger->priv->search_dirs =
816 g_list_prepend (debugger->priv->search_dirs, node->data);
817 node = g_list_next (node);
819 g_list_free (dir_list);
821 if (prog && strlen(prog) > 0)
823 gchar *quoted_prog = gdb_quote (prog);
824 if (exec_dir)
825 chdir (exec_dir);
826 if (is_libtool_prog == FALSE)
828 command_str = g_strdup_printf (GDB_PATH " -f -n -i=mi2 %s %s "
829 "-x %s/gdb.init \"%s\"", dir, term == NULL ? "" : term,
830 PACKAGE_DATA_DIR, quoted_prog);
832 else
834 command_str = g_strdup_printf ("libtool --mode=execute " GDB_PATH
835 " -f -n -i=mi2 %s %s "
836 "-x %s/gdb.init \"%s\"", dir, term == NULL ? "" : term,
837 PACKAGE_DATA_DIR, quoted_prog);
839 g_free (quoted_prog);
841 else
843 if (is_libtool_prog == FALSE)
845 command_str = g_strdup_printf (GDB_PATH " -f -n -i=mi2 %s %s "
846 "-x %s/gdb.init ", term == NULL ? "" : term,
847 dir, PACKAGE_DATA_DIR);
849 else
851 command_str = g_strdup_printf ("libtool --mode=execute " GDB_PATH
852 " -f -n -i=mi2 %s %s -x "
853 "%s/gdb.init ",
854 dir, term == NULL ? "" : term, PACKAGE_DATA_DIR);
857 g_free (dir);
858 g_free (term);
859 debugger->priv->starting = TRUE;
860 debugger->priv->terminating = FALSE;
861 debugger->priv->loading = prog != NULL ? TRUE : FALSE;
862 debugger->priv->debugger_is_busy = 1;
864 /* Prepare for launch. */
865 launcher = debugger->priv->launcher;
866 anjuta_launcher_set_terminate_on_exit (launcher, TRUE);
867 g_signal_connect (G_OBJECT (launcher), "child-exited",
868 G_CALLBACK (on_gdb_terminated), debugger);
869 ret = anjuta_launcher_execute (launcher, command_str,
870 on_gdb_output_arrived, debugger);
872 if (ret)
874 debugger->priv->debugger_is_started = TRUE;
875 debugger->priv->prog_is_loaded = prog != NULL;
877 anjuta_launcher_set_encoding (launcher, "ISO-8859-1");
879 if (debugger->priv->output_callback != NULL)
881 if (ret == TRUE)
883 /* TODO anjuta_update_app_status (TRUE, _("Debugger")); */
884 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
885 _("Getting ready to start debugging "
886 "session...\n"),
887 debugger->priv->output_user_data);
889 if (prog)
891 msg = g_strconcat (_("Loading Executable: "), prog, "\n", NULL);
892 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
893 msg,
894 debugger->priv->output_user_data);
895 g_free (msg);
897 else
899 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
900 _("No executable specified.\n"),
901 debugger->priv->output_user_data);
902 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
903 _("Open an executable or attach "
904 "to a process to start "
905 "debugging.\n"),
906 debugger->priv->output_user_data);
909 else
911 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
912 _("There was an error whilst "
913 "launching the debugger.\n"),
914 debugger->priv->output_user_data);
915 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
916 _("Make sure 'gdb' is installed "
917 "on the system.\n"),
918 debugger->priv->output_user_data);
921 g_free (command_str);
923 return TRUE;
926 static void
927 gdb_stdout_line_arrived (Debugger *debugger, const gchar * chars)
929 gint i = 0;
931 while (chars[i])
933 if (chars[i] == '\n')
935 debugger_stdo_flush (debugger);
937 else
939 g_string_append_c (debugger->priv->stdo_line, chars[i]);
941 i++;
945 static void
946 gdb_stderr_line_arrived (Debugger *debugger, const gchar * chars)
948 gint i;
950 for (i = 0; i < strlen (chars); i++)
952 if (chars[i] == '\n')
953 debugger_stde_flush (debugger);
954 else
955 g_string_append_c (debugger->priv->stde_line, chars[i]);
959 static void
960 on_gdb_output_arrived (AnjutaLauncher *launcher,
961 AnjutaLauncherOutputType output_type,
962 const gchar *chars, gpointer data)
964 Debugger *debugger = DEBUGGER (data);
965 DEBUG_PRINT ("on gdb output arrived");
967 /* Do not emit signal when the debugger is destroyed */
968 if (debugger->priv->instance == NULL) return;
970 switch (output_type)
972 case ANJUTA_LAUNCHER_OUTPUT_STDERR:
973 gdb_stderr_line_arrived (debugger, chars);
974 break;
975 case ANJUTA_LAUNCHER_OUTPUT_STDOUT:
976 gdb_stdout_line_arrived (debugger, chars);
977 break;
978 default:
979 break;
983 static void
984 debugger_handle_post_execution (Debugger *debugger)
986 switch (debugger->priv->post_execution_flag)
988 case DEBUGGER_NONE:
989 break;
990 case DEBUGGER_EXIT:
991 DEBUG_PRINT ("debugger stop in handle post execution\n");
992 debugger_stop (debugger);
993 break;
994 case DEBUGGER_RERUN_PROGRAM:
995 DEBUG_PRINT ("debugger run in handle post execution\n");
996 debugger_run (debugger);
997 break;
998 default:
999 g_warning ("Execution should not reach here");
1003 static void
1004 debugger_process_frame (Debugger *debugger, const GDBMIValue *val)
1006 const GDBMIValue *file, *line, *frame, *addr, *fullname, *thread;
1007 const gchar *file_str = NULL;
1008 guint line_num = 0;
1009 gulong addr_num = 0;
1011 g_return_if_fail (val != NULL);
1013 thread = gdbmi_value_hash_lookup (val, "thread-id");
1014 if (thread)
1016 debugger->priv->current_thread = strtoul (gdbmi_value_literal_get (thread), NULL, 0);
1018 debugger->priv->current_frame = 0;
1020 frame = gdbmi_value_hash_lookup (val, "frame");
1021 if (frame)
1023 fullname = gdbmi_value_hash_lookup (frame, "fullname");
1024 if (fullname)
1026 file_str = gdbmi_value_literal_get (fullname);
1027 if (*file_str == '\0') file_str = NULL;
1029 else
1031 file = gdbmi_value_hash_lookup (frame, "file");
1032 if (file)
1034 file_str = gdbmi_value_literal_get (file);
1035 if (*file_str == '\0') file_str = NULL;
1039 if (file_str != NULL)
1041 line = gdbmi_value_hash_lookup (frame, "line");
1042 if (line)
1044 line_num = strtoul (gdbmi_value_literal_get (line), NULL, 0);
1048 addr = gdbmi_value_hash_lookup (frame, "addr");
1049 if (addr)
1051 addr_num = strtoul (gdbmi_value_literal_get (addr), NULL, 0);
1053 debugger_program_moved (debugger, file_str, line_num, addr_num);
1057 static GError*
1058 gdb_parse_error (Debugger *debugger, const GDBMIValue *mi_results)
1060 const GDBMIValue *message;
1061 const gchar *literal;
1062 guint code = IANJUTA_DEBUGGER_UNKNOWN_ERROR;
1064 message = gdbmi_value_hash_lookup (mi_results, "msg");
1065 literal = gdbmi_value_literal_get (message);
1067 if ((mi_results != NULL)
1068 && ((message = gdbmi_value_hash_lookup (mi_results, "msg")) != NULL)
1069 && ((literal = gdbmi_value_literal_get (message)) != NULL)
1070 && (*literal != '\0'))
1072 code = gdb_match_error (literal);
1073 DEBUG_PRINT ("error code %d", code);
1075 else
1077 /* No error message */
1078 literal = "Error without a message";
1081 return g_error_new_literal (IANJUTA_DEBUGGER_ERROR, code, literal);
1085 /* Parsing output
1086 *---------------------------------------------------------------------------*/
1088 static void
1089 debugger_parse_output (Debugger *debugger)
1091 gchar *line;
1093 line = debugger->priv->stdo_line->str;
1095 if (line[0] == '\032' && line[1] == '\032')
1097 gchar *filename;
1098 guint lineno;
1100 gdb_util_parse_error_line (&(line[2]), &filename, &lineno);
1101 if (filename)
1103 debugger_program_moved (debugger, filename, lineno, 0);
1104 g_free (filename);
1107 else
1109 gchar *proper_msg;
1110 gsize len;
1112 len = strlen (line);
1113 if (line[1] == '\"' && line [strlen(line) - 1] == '\"')
1115 line[1] = line[0];
1116 /* Reserve space for an additional carriage return */
1117 line[strlen(line) - 1] = ' ';
1118 proper_msg = g_strcompress (line + 1);
1119 len = strlen (proper_msg) - 1;
1120 proper_msg[len] = '\0';
1122 else
1124 /* Reserve space for an additional carriage return */
1125 proper_msg = g_strndup (line, len + 1);
1128 if (strcmp(proper_msg, "~Stopped due to shared library event\n") == 0)
1130 /* Recognize a solib event */
1131 debugger->priv->solib_event = TRUE;
1132 g_free (proper_msg);
1134 else if (debugger->priv->current_cmd.parser)
1136 /* Save GDB CLI output */
1137 debugger->priv->cli_lines = g_list_prepend (debugger->priv->cli_lines,
1138 proper_msg);
1140 else
1142 /* Discard CLI output */
1143 g_free (proper_msg);
1148 static void
1149 debugger_parse_stopped (Debugger *debugger)
1151 gchar *line = debugger->priv->stdo_line->str;
1154 if (!debugger->priv->solib_event)
1156 gboolean program_exited = FALSE;
1157 GDBMIValue *val;
1159 /* Check if program has exited */
1160 val = gdbmi_value_parse (line);
1161 if (val)
1163 const GDBMIValue *reason;
1164 const gchar *str = NULL;
1166 debugger_process_frame (debugger, val);
1168 reason = gdbmi_value_hash_lookup (val, "reason");
1169 if (reason)
1170 str = gdbmi_value_literal_get (reason);
1172 if (str && (strncmp (str, "exited", 6) == 0))
1174 program_exited = TRUE;
1177 /* Emit signal received if necessary */
1178 if (str && strcmp (str, "exited-signalled") == 0)
1180 const GDBMIValue *signal_name, *signal_meaning;
1181 const gchar *signal_str, *signal_meaning_str;
1183 signal_name = gdbmi_value_hash_lookup (val, "signal-name");
1184 signal_str = gdbmi_value_literal_get (signal_name);
1185 signal_meaning = gdbmi_value_hash_lookup (val, "signal-meaning");
1186 signal_meaning_str = gdbmi_value_literal_get (signal_meaning);
1187 g_signal_emit_by_name (debugger->priv->instance, "signal-received", signal_str, signal_meaning_str);
1189 else if (str && strcmp (str, "signal-received") == 0)
1191 const GDBMIValue *signal_name, *signal_meaning;
1192 const gchar *signal_str, *signal_meaning_str;
1194 signal_name = gdbmi_value_hash_lookup (val, "signal-name");
1195 signal_str = gdbmi_value_literal_get (signal_name);
1196 signal_meaning = gdbmi_value_hash_lookup (val, "signal-meaning");
1197 signal_meaning_str = gdbmi_value_literal_get (signal_meaning);
1199 g_signal_emit_by_name (debugger->priv->instance, "signal-received", signal_str, signal_meaning_str);
1202 if (debugger->priv->output_callback)
1204 if (str && strcmp (str, "exited-normally") == 0)
1206 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1207 _("Program exited normally\n"),
1208 debugger->priv->output_user_data);
1210 else if (str && strcmp (str, "exited") == 0)
1212 const GDBMIValue *errcode;
1213 const gchar *errcode_str;
1214 gchar *msg;
1216 errcode = gdbmi_value_hash_lookup (val, "exit-code");
1217 errcode_str = gdbmi_value_literal_get (errcode);
1218 msg = g_strdup_printf (_("Program exited with error code %s\n"),
1219 errcode_str);
1220 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1221 msg, debugger->priv->output_user_data);
1222 g_free (msg);
1224 else if (str && strcmp (str, "breakpoint-hit") == 0)
1226 const GDBMIValue *bkptno;
1227 const gchar *bkptno_str;
1228 gchar *msg;
1230 bkptno = gdbmi_value_hash_lookup (val, "bkptno");
1231 bkptno_str = gdbmi_value_literal_get (bkptno);
1232 /* The program has reached one breakpoint and will stop */
1233 msg = g_strdup_printf (_("Breakpoint number %s hit\n"),
1234 bkptno_str);
1235 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1236 msg, debugger->priv->output_user_data);
1237 g_free (msg);
1239 else if (str && strcmp (str, "function-finished") == 0)
1241 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1242 _("Function finished\n"),
1243 debugger->priv->output_user_data);
1245 else if (str && strcmp (str, "end-stepping-range") == 0)
1247 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1248 _("Stepping finished\n"),
1249 debugger->priv->output_user_data);
1251 else if (str && strcmp (str, "location-reached") == 0)
1253 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1254 _("Location reached\n"),
1255 debugger->priv->output_user_data);
1260 if (program_exited)
1262 debugger->priv->prog_is_running = FALSE;
1263 debugger->priv->prog_is_attached = FALSE;
1264 debugger_handle_post_execution (debugger);
1265 debugger->priv->exiting = TRUE;
1267 else
1269 // g_signal_emit_by_name (debugger->priv->instance, "program-stopped");
1270 debugger->priv->stopping = TRUE;
1273 debugger->priv->cli_lines = g_list_reverse (debugger->priv->cli_lines);
1274 if ((debugger->priv->current_cmd.cmd != NULL) &&
1275 (debugger->priv->current_cmd.parser != NULL))
1277 debugger->priv->current_cmd.parser (debugger, val,
1278 debugger->priv->cli_lines, FALSE);
1279 debugger->priv->command_output_sent = TRUE;
1280 DEBUG_PRINT ("In function: Sending output...");
1283 if (val)
1284 gdbmi_value_free (val);
1288 static void
1289 debugger_parse_prompt (Debugger *debugger)
1291 /* If the parser has not yet been called, call it now. */
1292 if (debugger->priv->command_output_sent == FALSE &&
1293 debugger->priv->current_cmd.parser)
1295 debugger->priv->current_cmd.parser (debugger, NULL,
1296 debugger->priv->cli_lines, FALSE);
1297 debugger->priv->command_output_sent = TRUE;
1300 debugger->priv->debugger_is_busy--;
1301 debugger_queue_execute_command (debugger); /* Next command. Go. */
1302 debugger_emit_ready (debugger);
1305 static gboolean
1306 parse_breakpoint (IAnjutaDebuggerBreakpointItem* bp, const GDBMIValue *brkpnt)
1308 const GDBMIValue *literal;
1309 const gchar* value;
1311 memset (bp, 0, sizeof (*bp));
1313 literal = gdbmi_value_hash_lookup (brkpnt, "number");
1314 if (literal)
1316 value = gdbmi_value_literal_get (literal);
1317 bp->id = strtoul (value, NULL, 10);
1320 literal = gdbmi_value_hash_lookup (brkpnt, "fullname");
1321 if (literal == NULL) literal = gdbmi_value_hash_lookup (brkpnt, "file");
1322 if (literal)
1324 value = gdbmi_value_literal_get (literal);
1325 bp->file = (gchar *)value;
1328 literal = gdbmi_value_hash_lookup (brkpnt, "line");
1329 if (literal)
1331 value = gdbmi_value_literal_get (literal);
1332 bp->line = strtoul (value, NULL, 10);
1333 bp->type |= IANJUTA_DEBUGGER_BREAKPOINT_ON_LINE;
1336 literal = gdbmi_value_hash_lookup (brkpnt, "type");
1337 if (literal)
1339 value = gdbmi_value_literal_get (literal);
1342 literal = gdbmi_value_hash_lookup (brkpnt, "disp");
1343 if (literal)
1345 value = gdbmi_value_literal_get (literal);
1346 if (strcmp (value, "keep") == 0)
1348 bp->type |= IANJUTA_DEBUGGER_BREAKPOINT_WITH_TEMPORARY;
1349 bp->temporary = FALSE;
1351 else if ((strcmp (value, "nokeep") == 0) || (strcmp (value, "del") == 0))
1353 bp->type |= IANJUTA_DEBUGGER_BREAKPOINT_WITH_TEMPORARY;
1354 bp->temporary = TRUE;
1358 literal = gdbmi_value_hash_lookup (brkpnt, "enabled");
1359 if (literal)
1361 value = gdbmi_value_literal_get (literal);
1362 if (strcmp (value, "n") == 0)
1364 bp->type |= IANJUTA_DEBUGGER_BREAKPOINT_WITH_ENABLE;
1365 bp->enable = FALSE;
1367 else if (strcmp (value, "y") == 0)
1369 bp->type |= IANJUTA_DEBUGGER_BREAKPOINT_WITH_ENABLE;
1370 bp->enable = TRUE;
1374 literal = gdbmi_value_hash_lookup (brkpnt, "addr");
1375 if (literal)
1377 value = gdbmi_value_literal_get (literal);
1378 bp->address = strtoul (value, NULL, 16);
1379 bp->type |= IANJUTA_DEBUGGER_BREAKPOINT_ON_ADDRESS;
1382 literal = gdbmi_value_hash_lookup (brkpnt, "func");
1383 if (literal)
1385 value = gdbmi_value_literal_get (literal);
1386 bp->function = (gchar *)value;
1387 bp->type |= IANJUTA_DEBUGGER_BREAKPOINT_ON_FUNCTION;
1390 literal = gdbmi_value_hash_lookup (brkpnt, "times");
1391 if (literal)
1393 value = gdbmi_value_literal_get (literal);
1394 bp->times = strtoul (value, NULL, 10);
1395 bp->type |= IANJUTA_DEBUGGER_BREAKPOINT_WITH_TIME;
1397 DEBUG_PRINT("parse time %d", bp->times);
1399 literal = gdbmi_value_hash_lookup (brkpnt, "ignore");
1400 if (literal)
1402 value = gdbmi_value_literal_get (literal);
1403 bp->ignore = strtoul (value, NULL, 10);
1404 bp->type |= IANJUTA_DEBUGGER_BREAKPOINT_WITH_IGNORE;
1407 literal = gdbmi_value_hash_lookup (brkpnt, "cond");
1408 if (literal)
1410 value = gdbmi_value_literal_get (literal);
1411 bp->condition = (gchar *)value;
1412 bp->type |= IANJUTA_DEBUGGER_BREAKPOINT_WITH_CONDITION;
1415 return TRUE;
1418 static void
1419 debugger_stdo_flush (Debugger *debugger)
1421 gchar *line;
1423 line = debugger->priv->stdo_line->str;
1425 #ifdef DEBUG
1426 DEBUG_PRINT ("GDB:< %s", line);
1427 #else
1428 if (debugger->priv->gdb_log) g_message ("GDB:< %s", line);
1429 #endif
1430 debugger_log_output (debugger, line);
1431 if (strlen (line) == 0)
1433 return;
1435 if (strncasecmp (line, "^error", 6) == 0)
1437 /* GDB reported error */
1438 if ((debugger->priv->current_cmd.keep_result) || (debugger->priv->stdo_acc->len != 0))
1440 /* Keep result for next command */
1442 if (debugger->priv->stdo_acc->len == 0)
1444 g_string_append (debugger->priv->stdo_acc, line);
1446 else
1448 line = strchr (line, ',');
1449 if (line != NULL)
1451 g_string_append (debugger->priv->stdo_acc, line);
1454 line = debugger->priv->stdo_acc->str;
1457 GDBMIValue *val = gdbmi_value_parse (line);
1458 GError *error;
1460 error = gdb_parse_error (debugger, val);
1462 if (debugger->priv->current_cmd.parser != NULL)
1464 debugger->priv->current_cmd.parser (debugger, val, debugger->priv->cli_lines, error);
1465 debugger->priv->command_output_sent = TRUE; }
1466 DEBUG_PRINT("GDB: error %s", error->message);
1467 /*else
1469 anjuta_util_dialog_error (debugger->priv->parent_win, "%s", error->message);
1471 g_error_free (error);
1472 gdbmi_value_free (val);
1474 else if (strncasecmp(line, "^running", 8) == 0)
1476 /* Program started running */
1477 debugger->priv->prog_is_running = TRUE;
1478 /* debugger->priv->skip_next_prompt = TRUE; Replaced by debugger_is_busy++ */
1479 debugger->priv->debugger_is_busy++;
1480 g_signal_emit_by_name (debugger->priv->instance, "program-running");
1482 else if (strncasecmp (line, "*stopped", 8) == 0)
1484 /* Process has stopped */
1485 debugger_parse_stopped (debugger);
1487 else if (strncasecmp (line, "^done", 5) == 0)
1489 if ((debugger->priv->current_cmd.keep_result) || (debugger->priv->stdo_acc->len != 0))
1491 /* Keep result for next command */
1493 if (debugger->priv->stdo_acc->len == 0)
1495 g_string_append (debugger->priv->stdo_acc, line);
1497 else
1499 line = strchr (line, ',');
1500 if (line != NULL)
1502 g_string_append (debugger->priv->stdo_acc, line);
1505 line = debugger->priv->stdo_acc->str;
1508 if (!debugger->priv->current_cmd.keep_result)
1510 /* GDB command has reported output */
1511 GDBMIValue *val = gdbmi_value_parse (line);
1513 debugger->priv->cli_lines = g_list_reverse (debugger->priv->cli_lines);
1514 if ((debugger->priv->current_cmd.cmd != NULL) &&
1515 (debugger->priv->current_cmd.parser != NULL))
1517 debugger->priv->current_cmd.parser (debugger, val,
1518 debugger->priv->cli_lines, FALSE);
1519 debugger->priv->command_output_sent = TRUE;
1520 DEBUG_PRINT ("In function: Sending output...");
1522 else /* if (val) */
1524 /*g_signal_emit_by_name (debugger, "results-arrived",
1525 debugger->priv->current_cmd.cmd, val);*/
1528 if (val)
1530 /*debugger_process_frame (debugger, val);*/
1531 gdbmi_value_free (val);
1535 if (!debugger->priv->current_cmd.keep_result)
1537 g_string_assign (debugger->priv->stdo_acc, "");
1540 else if (strncasecmp (line, GDB_PROMPT, strlen (GDB_PROMPT)) == 0)
1542 debugger_parse_prompt (debugger);
1544 else
1546 debugger_parse_output (debugger);
1549 /* Clear the line buffer */
1550 g_string_assign (debugger->priv->stdo_line, "");
1553 void
1554 debugger_stde_flush (Debugger *debugger)
1556 if ((debugger->priv->output_callback) && (strlen (debugger->priv->stde_line->str) > 0))
1558 debugger->priv->output_callback (IANJUTA_DEBUGGER_ERROR_OUTPUT,
1559 debugger->priv->stde_line->str,
1560 debugger->priv->output_user_data);
1562 /* Clear the line buffer */
1563 g_string_assign (debugger->priv->stde_line, "");
1566 static void
1567 on_gdb_terminated (AnjutaLauncher *launcher,
1568 gint child_pid, gint status, gulong t,
1569 gpointer data)
1571 Debugger *debugger = DEBUGGER (data);
1572 GError *err = NULL;
1574 g_signal_handlers_disconnect_by_func (G_OBJECT (launcher),
1575 G_CALLBACK (on_gdb_terminated),
1576 debugger);
1578 DEBUG_PRINT ("In function: gdb_terminated()");
1580 /* Clear the command queue & Buffer */
1581 debugger_clear_buffers (debugger);
1583 /* Good Bye message */
1584 /*if (!debugger->priv->terminating)
1586 anjuta_util_dialog_error (debugger->priv->parent_win,
1587 _("gdb terminated unexpectedly with status %d\n"), status);
1589 debugger->priv->prog_is_running = FALSE;
1590 debugger->priv->prog_is_attached = FALSE;
1591 debugger->priv->prog_is_loaded = FALSE;
1592 debugger->priv->debugger_is_busy = 0;
1593 debugger->priv->skip_next_prompt = FALSE;
1594 if (!debugger->priv->terminating)
1596 err = g_error_new (IANJUTA_DEBUGGER_ERROR,
1597 IANJUTA_DEBUGGER_OTHER_ERROR,
1598 "gdb terminated with status %d", status);
1600 debugger->priv->terminating = FALSE;
1601 debugger->priv->debugger_is_started = FALSE;
1602 if (debugger->priv->instance)
1604 g_signal_emit_by_name (debugger->priv->instance, "debugger-stopped", err);
1606 if (err != NULL) g_error_free (err);
1609 static void
1610 debugger_stop_real (Debugger *debugger)
1612 DEBUG_PRINT ("In function: debugger_stop_real()");
1614 /* if program is attached - detach from it before quiting */
1615 if (debugger->priv->prog_is_attached == TRUE)
1617 debugger_detach_process(debugger);
1620 debugger->priv->terminating = TRUE;
1621 debugger_queue_command (debugger, "-gdb-exit", FALSE, FALSE, NULL, NULL, NULL);
1624 gboolean
1625 debugger_stop (Debugger *debugger)
1627 #if 0
1628 gboolean ret = TRUE;
1630 if (debugger->priv->prog_is_running == TRUE)
1632 GtkWidget *dialog;
1633 gchar *mesg;
1635 if (debugger->priv->prog_is_attached == TRUE)
1636 mesg = _("The program is attached.\n"
1637 "Do you still want to stop the debugger?");
1638 else
1639 mesg = _("The program is running.\n"
1640 "Do you still want to stop the debugger?");
1641 dialog = gtk_message_dialog_new (debugger->priv->parent_win,
1642 GTK_DIALOG_DESTROY_WITH_PARENT,
1643 GTK_MESSAGE_QUESTION,
1644 GTK_BUTTONS_NONE, mesg);
1645 gtk_dialog_add_buttons (GTK_DIALOG (dialog),
1646 GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
1647 GTK_STOCK_STOP, GTK_RESPONSE_YES,
1648 NULL);
1649 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_YES)
1650 debugger_stop_real (debugger);
1651 else
1652 ret = FALSE;
1653 gtk_widget_destroy (dialog);
1655 else
1656 debugger_stop_real (debugger);
1657 return ret;
1658 #endif
1659 debugger_stop_real (debugger);
1661 return TRUE;
1664 gboolean
1665 debugger_abort (Debugger *debugger)
1667 DEBUG_PRINT ("In function: debugger_abort()");
1669 /* Stop inferior */
1670 if ((debugger->priv->prog_is_attached == FALSE) && (debugger->priv->inferior_pid != 0))
1672 kill (debugger->priv->inferior_pid, SIGTERM);
1673 debugger->priv->inferior_pid = 0;
1676 /* Stop gdb */
1677 debugger->priv->terminating = TRUE;
1678 g_signal_handlers_disconnect_by_func (G_OBJECT (debugger->priv->launcher), G_CALLBACK (on_gdb_terminated), debugger);
1679 anjuta_launcher_reset (debugger->priv->launcher);
1681 /* Free memory */
1682 debugger_queue_clear (debugger);
1683 g_list_foreach (debugger->priv->search_dirs, (GFunc)g_free, NULL);
1684 g_list_free (debugger->priv->search_dirs);
1685 debugger->priv->search_dirs = NULL;
1687 /* Emit signal, state of the debugger must be DEBUGGER_STOPPED */
1688 debugger->priv->prog_is_running = FALSE;
1689 debugger->priv->prog_is_attached = FALSE;
1690 debugger->priv->inferior_pid = 0;
1691 debugger->priv->prog_is_loaded = FALSE;
1692 debugger->priv->debugger_is_busy = 0;
1693 debugger->priv->debugger_is_started = FALSE;
1694 if (debugger->priv->instance != NULL)
1696 g_signal_emit_by_name (debugger->priv->instance, "debugger-stopped", NULL);
1697 debugger->priv->instance = NULL;
1700 return TRUE;
1703 gchar*
1704 debugger_get_source_path (Debugger *debugger, const gchar *file)
1706 GList *node;
1707 gchar *path = NULL;
1709 if (g_path_is_absolute (file))
1710 return g_strdup (file);
1712 node = debugger->priv->search_dirs;
1713 while (node)
1715 path = g_build_filename (node->data, file, NULL);
1716 if (g_file_test (path, G_FILE_TEST_EXISTS))
1717 break;
1718 g_free (path);
1719 path = NULL;
1720 node = g_list_next (node);
1723 if (path == NULL)
1725 /* The file could be found nowhere. Use current directory */
1726 gchar *cwd;
1727 cwd = g_get_current_dir ();
1728 path = g_build_filename (cwd, file, NULL);
1729 g_free (cwd);
1731 return path;
1734 void
1735 debugger_set_output_callback (Debugger *debugger, IAnjutaDebuggerOutputCallback callback, gpointer user_data)
1737 debugger->priv->output_callback = callback;
1738 debugger->priv->output_user_data = user_data;
1741 void
1742 debugger_program_moved (Debugger *debugger, const gchar *file,
1743 gint line, gulong address)
1745 gchar *src_path;
1747 if ((file != NULL) && (*file != G_DIR_SEPARATOR))
1749 src_path = debugger_get_source_path (debugger, file);
1750 g_signal_emit_by_name (debugger->priv->instance, "program-moved", debugger->priv->inferior_pid, debugger->priv->current_thread, address, src_path, line);
1751 g_free (src_path);
1753 else
1755 g_signal_emit_by_name (debugger->priv->instance, "program-moved", debugger->priv->inferior_pid, debugger->priv->current_thread, address, file, line);
1759 static void
1760 debugger_info_program_finish (Debugger *debugger, const GDBMIValue *mi_results,
1761 const GList *cli_results, GError *error)
1763 DEBUG_PRINT ("In function: debugger_info_program()");
1765 /* Hack: find message string giving inferior pid */
1766 while (cli_results != NULL)
1768 gchar* child_proc;
1770 child_proc = strstr(cli_results->data, " child process ");
1771 if (child_proc != NULL)
1773 debugger->priv->inferior_pid = strtoul (child_proc + 15, NULL, 10);
1774 break;
1776 cli_results = g_list_next (cli_results);
1780 void
1781 debugger_start_program (Debugger *debugger, const gchar* args, const gchar* tty, gboolean stop)
1783 gchar *cmd;
1785 DEBUG_PRINT ("In function: debugger_start_program()");
1787 g_return_if_fail (IS_DEBUGGER (debugger));
1788 g_return_if_fail (debugger->priv->prog_is_running == FALSE);
1790 /* Without a terminal, the output of the debugged program
1791 * are lost */
1792 if (tty)
1794 cmd = g_strdup_printf ("-inferior-tty-set %s", tty);
1795 debugger_queue_command (debugger, cmd, FALSE, FALSE, NULL, NULL, NULL);
1796 g_free (cmd);
1799 debugger->priv->inferior_pid = 0;
1800 if (stop)
1802 debugger_queue_command (debugger, "-break-insert -t main", FALSE, FALSE, NULL, NULL, NULL);
1804 if (args && (*args))
1806 cmd = g_strconcat ("-exec-arguments ", args, NULL);
1807 debugger_queue_command (debugger, cmd, FALSE, FALSE, NULL, NULL, NULL);
1808 g_free (cmd);
1811 debugger_queue_command (debugger, "-exec-run", FALSE, FALSE, NULL, NULL, NULL);
1813 /* Get pid of program on next stop */
1814 debugger_queue_command (debugger, "info program", FALSE, FALSE, debugger_info_program_finish, NULL, NULL);
1815 debugger->priv->post_execution_flag = DEBUGGER_NONE;
1818 static void
1819 debugger_attach_process_finish (Debugger *debugger, const GDBMIValue *mi_results,
1820 const GList *cli_results, GError *error)
1822 DEBUG_PRINT ("Program attach finished");
1823 if (debugger->priv->output_callback)
1825 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1826 _("Program attached\n"),
1827 debugger->priv->output_user_data);
1829 debugger->priv->prog_is_attached = TRUE;
1830 debugger->priv->prog_is_running = TRUE;
1831 /* It is not really a shared lib event, but it allows to restart
1832 * the program after setting breakpoints. It is better to restart
1833 * it because we don't have the normal stop frame that tell where
1834 * the program is stopped */
1835 debugger->priv->solib_event = TRUE;
1838 static void
1839 debugger_attach_process_real (Debugger *debugger, pid_t pid)
1841 gchar *buff;
1843 DEBUG_PRINT ("In function: debugger_attach_process_real()");
1845 if (debugger->priv->output_callback)
1847 buff = g_strdup_printf (_("Attaching to process: %d...\n"), pid);
1848 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1849 buff, debugger->priv->output_user_data);
1850 g_free (buff);
1853 debugger->priv->inferior_pid = pid;
1854 buff = g_strdup_printf ("attach %d", pid);
1855 debugger_queue_command (debugger, buff, FALSE, FALSE,
1856 debugger_attach_process_finish, NULL, NULL);
1857 g_free (buff);
1860 void
1861 debugger_attach_process (Debugger *debugger, pid_t pid)
1863 DEBUG_PRINT ("In function: debugger_attach_process()");
1865 g_return_if_fail (IS_DEBUGGER (debugger));
1867 if (debugger->priv->prog_is_running == TRUE)
1869 // TODO: Dialog to be made HIG compliant.
1870 gchar *mesg;
1871 GtkWidget *dialog;
1873 mesg = _("A process is already running.\n"
1874 "Would you like to terminate it and attach the new process?"),
1875 dialog = gtk_message_dialog_new (debugger->priv->parent_win,
1876 GTK_DIALOG_DESTROY_WITH_PARENT,
1877 GTK_MESSAGE_QUESTION,
1878 GTK_BUTTONS_YES_NO, mesg);
1879 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_YES)
1881 debugger_stop_program (debugger);
1882 debugger_attach_process_real (debugger, pid);
1884 gtk_widget_destroy (dialog);
1886 else if (getpid () == pid ||
1887 anjuta_launcher_get_child_pid (debugger->priv->launcher) == pid)
1889 anjuta_util_dialog_error (debugger->priv->parent_win,
1890 _("Anjuta is unable to attach to itself."));
1891 return;
1893 else
1894 debugger_attach_process_real (debugger, pid);
1897 void
1898 debugger_restart_program (Debugger *debugger)
1900 DEBUG_PRINT ("In function: debugger_restart_program()");
1902 g_return_if_fail (debugger->priv->prog_is_attached == FALSE);
1905 debugger->priv->post_execution_flag = DEBUGGER_RERUN_PROGRAM;
1906 debugger_stop_program (debugger);
1908 debugger_put_cmd_in_queqe ("tbreak main", DB_CMD_NONE, NULL, NULL);
1909 debugger_put_cmd_in_queqe ("run >/dev/null 2>/dev/null", DB_CMD_ALL,
1910 NULL, NULL);
1911 debugger_put_cmd_in_queqe ("info program", DB_CMD_NONE,
1912 on_debugger_update_prog_status, NULL);
1913 debugger_put_cmd_in_queqe ("continue", DB_CMD_NONE, NULL, NULL);
1914 debugger_execute_cmd_in_queqe ();
1918 void
1919 debugger_stop_program (Debugger *debugger)
1921 DEBUG_PRINT ("In function: debugger_stop_program()");
1923 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
1925 if (debugger->priv->prog_is_attached == TRUE)
1927 debugger_detach_process (debugger);
1929 else
1931 /* FIXME: Why doesn't -exec-abort work??? */
1932 /* debugger_queue_command (debugger, "-exec-abort", NULL, NULL); */
1933 debugger_queue_command (debugger, "kill", FALSE, FALSE, NULL, NULL, NULL);
1934 debugger->priv->prog_is_running = FALSE;
1935 debugger->priv->prog_is_attached = FALSE;
1936 g_signal_emit_by_name (debugger->priv->instance, "program-exited");
1937 if (debugger->priv->output_callback)
1939 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1940 _("Program terminated\n"),
1941 debugger->priv->output_user_data);
1943 debugger_handle_post_execution (debugger);
1947 static void
1948 debugger_detach_process_finish (Debugger *debugger, const GDBMIValue *mi_results,
1949 const GList *cli_results, GError *error)
1951 DEBUG_PRINT ("Program detach finished");
1952 if (debugger->priv->output_callback)
1954 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1955 _("Program detached\n"),
1956 debugger->priv->output_user_data);
1958 debugger->priv->inferior_pid = 0;
1959 debugger->priv->prog_is_attached = FALSE;
1960 debugger->priv->prog_is_running = FALSE;
1961 g_signal_emit_by_name (debugger->priv->instance, "program-exited");
1964 void
1965 debugger_detach_process (Debugger *debugger)
1967 gchar *buff;
1969 DEBUG_PRINT ("In function: debugger_detach_process()");
1971 g_return_if_fail (debugger->priv->prog_is_attached == TRUE);
1973 if (debugger->priv->output_callback)
1975 buff = g_strdup_printf (_("Detaching the process...\n"));
1976 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1977 buff, debugger->priv->output_user_data);
1978 g_free (buff);
1981 debugger_queue_command (debugger, "detach", FALSE, FALSE,
1982 debugger_detach_process_finish, NULL, NULL);
1985 void
1986 debugger_interrupt (Debugger *debugger)
1988 DEBUG_PRINT ("In function: debugger_interrupt()");
1990 g_return_if_fail (IS_DEBUGGER (debugger));
1991 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
1993 if (debugger->priv->output_callback)
1995 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1996 _("Interrupting the process\n"),
1997 debugger->priv->output_user_data);
2000 if (debugger->priv->inferior_pid == 0)
2002 /* In case we do not have the inferior pid, send signal to gdb */
2003 anjuta_launcher_signal (debugger->priv->launcher, SIGINT);
2005 else
2007 /* Send signal directly to inferior */
2008 kill (debugger->priv->inferior_pid, SIGINT);
2010 //g_signal_emit_by_name (debugger->priv->instance, "program-running");
2013 void
2014 debugger_run (Debugger *debugger)
2016 DEBUG_PRINT ("In function: debugger_run()");
2018 g_return_if_fail (IS_DEBUGGER (debugger));
2019 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
2021 /* Program running - continue */
2022 debugger_queue_command (debugger, "-exec-continue", FALSE, FALSE, NULL, NULL, NULL);
2025 void
2026 debugger_step_in (Debugger *debugger)
2028 DEBUG_PRINT ("In function: debugger_step_in()");
2030 g_return_if_fail (IS_DEBUGGER (debugger));
2031 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
2033 debugger_queue_command (debugger, "-exec-step", FALSE, FALSE, NULL, NULL, NULL);
2036 void
2037 debugger_step_over (Debugger *debugger)
2039 DEBUG_PRINT ("In function: debugger_step_over()");
2041 g_return_if_fail (IS_DEBUGGER (debugger));
2042 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
2044 debugger_queue_command (debugger, "-exec-next", FALSE, FALSE, NULL, NULL, NULL);
2047 void
2048 debugger_step_out (Debugger *debugger)
2050 DEBUG_PRINT ("In function: debugger_step_out()");
2052 g_return_if_fail (IS_DEBUGGER (debugger));
2053 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
2055 debugger_queue_command (debugger, "-exec-finish", FALSE, FALSE, NULL, NULL, NULL);
2058 void
2059 debugger_stepi_in (Debugger *debugger)
2061 DEBUG_PRINT ("In function: debugger_step_in()");
2063 g_return_if_fail (IS_DEBUGGER (debugger));
2064 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
2066 debugger_queue_command (debugger, "-exec-step-instruction", FALSE, FALSE, NULL, NULL, NULL);
2069 void
2070 debugger_stepi_over (Debugger *debugger)
2072 DEBUG_PRINT ("In function: debugger_step_over()");
2074 g_return_if_fail (IS_DEBUGGER (debugger));
2075 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
2077 debugger_queue_command (debugger, "-exec-next-instruction", FALSE, FALSE, NULL, NULL, NULL);
2080 void
2081 debugger_run_to_location (Debugger *debugger, const gchar *loc)
2083 gchar *buff;
2085 DEBUG_PRINT ("In function: debugger_run_to_location()");
2087 g_return_if_fail (IS_DEBUGGER (debugger));
2088 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
2090 buff = g_strdup_printf ("-exec-until %s", loc);
2091 debugger_queue_command (debugger, buff, FALSE, FALSE, NULL, NULL, NULL);
2092 g_free (buff);
2095 void
2096 debugger_run_to_position (Debugger *debugger, const gchar *file, guint line)
2098 gchar *buff;
2099 gchar *quoted_file;
2101 DEBUG_PRINT ("In function: debugger_run_to_position()");
2103 g_return_if_fail (IS_DEBUGGER (debugger));
2104 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
2106 quoted_file = gdb_quote (file);
2107 buff = g_strdup_printf ("-break-insert -t \"\\\"%s\\\":%u\"", quoted_file, line);
2108 g_free (quoted_file);
2109 debugger_queue_command (debugger, buff, FALSE, FALSE, NULL, NULL, NULL);
2110 g_free (buff);
2111 debugger_queue_command (debugger, "-exec-continue", FALSE, FALSE, NULL, NULL, NULL);
2114 void
2115 debugger_run_to_address (Debugger *debugger, gulong address)
2117 gchar *buff;
2119 DEBUG_PRINT ("In function: debugger_run_to_address()");
2121 g_return_if_fail (IS_DEBUGGER (debugger));
2122 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
2124 buff = g_strdup_printf ("-break-insert -t *0x%lx", address);
2125 debugger_queue_command (debugger, buff, FALSE, FALSE, NULL, NULL, NULL);
2126 g_free (buff);
2127 debugger_queue_command (debugger, "-exec-continue", FALSE, FALSE, NULL, NULL, NULL);
2130 void
2131 debugger_command (Debugger *debugger, const gchar *command,
2132 gboolean suppress_error, DebuggerParserFunc parser,
2133 gpointer user_data)
2135 if (strncasecmp (command, "-exec-run",
2136 strlen ("-exec-run")) == 0 ||
2137 strncasecmp (command, "run", strlen ("run")) == 0)
2139 /* FIXME: The user might have passed args to the command */
2140 debugger_run (debugger);
2142 else if (strncasecmp (command, "-exec-step",
2143 strlen ("-exec-step")) == 0 ||
2144 strncasecmp (command, "step", strlen ("step")) == 0)
2146 debugger_step_in (debugger);
2148 else if (strncasecmp (command, "-exec-next",
2149 strlen ("-exec-next")) == 0 ||
2150 strncasecmp (command, "next", strlen ("next")) == 0)
2152 debugger_step_over (debugger);
2154 else if (strncasecmp (command, "-exec-finish",
2155 strlen ("-exec-finish")) == 0 ||
2156 strncasecmp (command, "finish", strlen ("finish")) == 0)
2158 debugger_step_out (debugger);
2160 else if (strncasecmp (command, "-exec-continue",
2161 strlen ("-exec-continue")) == 0 ||
2162 strncasecmp (command, "continue", strlen ("continue")) == 0)
2164 debugger_run (debugger);
2166 else if (strncasecmp (command, "-exec-until",
2167 strlen ("-exec-until")) == 0 ||
2168 strncasecmp (command, "until", strlen ("until")) == 0)
2170 debugger_run_to_location (debugger, strchr (command, ' '));
2172 else if (strncasecmp (command, "-exec-abort",
2173 strlen ("-exec-abort")) == 0 ||
2174 strncasecmp (command, "kill", strlen ("kill")) == 0)
2176 debugger_stop_program (debugger);
2178 else if (strncasecmp (command, "-target-attach",
2179 strlen ("-target-attach")) == 0 ||
2180 strncasecmp (command, "attach", strlen ("attach")) == 0)
2182 pid_t pid = 0;
2183 gchar *pid_str = strchr (command, ' ');
2184 if (pid_str)
2185 pid = atoi (pid_str);
2186 debugger_attach_process (debugger, pid);
2188 else if (strncasecmp (command, "-target-detach",
2189 strlen ("-target-detach")) == 0 ||
2190 strncasecmp (command, "detach", strlen ("detach")) == 0)
2192 debugger_detach_process (debugger);
2194 else if (strncasecmp (command, "-file-exec-and-symbols",
2195 strlen ("-file-exec-and-symbols")) == 0 ||
2196 strncasecmp (command, "file", strlen ("file")) == 0)
2198 debugger_load_executable (debugger, strchr (command, ' '));
2200 else if (strncasecmp (command, "core", strlen ("core")) == 0)
2202 debugger_load_core (debugger, strchr (command, ' '));
2204 else
2206 debugger_queue_command (debugger, command, suppress_error, FALSE,
2207 parser, user_data, NULL);
2211 static void
2212 debugger_add_breakpoint_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2214 IAnjutaDebuggerBreakpointItem bp;
2215 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2216 gpointer user_data = debugger->priv->current_cmd.user_data;
2218 if ((error != NULL) || (mi_results == NULL))
2220 /* Call callback in all case (useful for enable that doesn't return
2221 * anything */
2222 if (callback != NULL)
2223 callback (NULL, user_data, error);
2225 else if (callback != NULL)
2227 const GDBMIValue *brkpnt;
2229 brkpnt = gdbmi_value_hash_lookup (mi_results, "bkpt");
2230 parse_breakpoint (&bp, brkpnt);
2232 /* Call callback in all case (useful for enable that doesn't return
2233 * anything */
2234 callback (&bp, user_data, error);
2239 void
2240 debugger_add_breakpoint_at_line (Debugger *debugger, const gchar *file, guint line, IAnjutaDebuggerCallback callback, gpointer user_data)
2242 gchar *buff;
2243 gchar *quoted_file;
2245 DEBUG_PRINT ("In function: debugger_add_breakpoint()");
2247 g_return_if_fail (IS_DEBUGGER (debugger));
2249 quoted_file = gdb_quote (file);
2251 buff = g_strdup_printf ("-break-insert \"\\\"%s\\\":%u\"", quoted_file, line);
2252 g_free (quoted_file);
2253 debugger_queue_command (debugger, buff, FALSE, FALSE, debugger_add_breakpoint_finish, callback, user_data);
2254 g_free (buff);
2257 void
2258 debugger_add_breakpoint_at_function (Debugger *debugger, const gchar *file, const gchar *function, IAnjutaDebuggerCallback callback, gpointer user_data)
2260 gchar *buff;
2262 DEBUG_PRINT ("In function: debugger_add_breakpoint()");
2264 g_return_if_fail (IS_DEBUGGER (debugger));
2266 buff = g_strdup_printf ("-break-insert %s%s%s", file == NULL ? "" : file, file == NULL ? "" : ":" , function);
2267 debugger_queue_command (debugger, buff, FALSE, FALSE, debugger_add_breakpoint_finish, callback, user_data);
2268 g_free (buff);
2271 void
2272 debugger_add_breakpoint_at_address (Debugger *debugger, gulong address, IAnjutaDebuggerCallback callback, gpointer user_data)
2274 gchar *buff;
2276 DEBUG_PRINT ("In function: debugger_add_breakpoint()");
2278 g_return_if_fail (IS_DEBUGGER (debugger));
2280 buff = g_strdup_printf ("-break-insert *0x%lx", address);
2281 debugger_queue_command (debugger, buff, FALSE, FALSE, debugger_add_breakpoint_finish, callback, user_data);
2282 g_free (buff);
2285 void
2286 debugger_enable_breakpoint (Debugger *debugger, guint id, gboolean enable, IAnjutaDebuggerCallback callback, gpointer user_data)
2289 gchar *buff;
2291 DEBUG_PRINT ("In function: debugger_enable_breakpoint()");
2293 g_return_if_fail (IS_DEBUGGER (debugger));
2295 buff = g_strdup_printf (enable ? "-break-enable %d" : "-break-disable %d",id);
2296 debugger_queue_command (debugger, buff, FALSE, FALSE, debugger_add_breakpoint_finish, callback, user_data);
2297 g_free (buff);
2300 void
2301 debugger_ignore_breakpoint (Debugger *debugger, guint id, guint ignore, IAnjutaDebuggerCallback callback, gpointer user_data)
2303 gchar *buff;
2305 DEBUG_PRINT ("In function: debugger_ignore_breakpoint()");
2307 g_return_if_fail (IS_DEBUGGER (debugger));
2309 buff = g_strdup_printf ("-break-after %d %d", id, ignore);
2310 debugger_queue_command (debugger, buff, FALSE, FALSE, debugger_add_breakpoint_finish, callback, user_data);
2311 g_free (buff);
2314 void
2315 debugger_condition_breakpoint (Debugger *debugger, guint id, const gchar *condition, IAnjutaDebuggerCallback callback, gpointer user_data)
2317 gchar *buff;
2319 DEBUG_PRINT ("In function: debugger_ignore_breakpoint()");
2321 g_return_if_fail (IS_DEBUGGER (debugger));
2323 buff = g_strdup_printf ("-break-condition %d %s", id, condition == NULL ? "" : condition);
2324 debugger_queue_command (debugger, buff, FALSE, FALSE, debugger_add_breakpoint_finish, callback, user_data);
2325 g_free (buff);
2328 static void
2329 debugger_remove_breakpoint_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2331 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2332 gpointer user_data = debugger->priv->current_cmd.user_data;
2333 IAnjutaDebuggerBreakpointItem bp;
2335 bp.type = IANJUTA_DEBUGGER_BREAKPOINT_REMOVED;
2336 bp.id = atoi (debugger->priv->current_cmd.cmd + 14);
2337 if (callback != NULL)
2338 callback (&bp, user_data, NULL);
2341 void
2342 debugger_remove_breakpoint (Debugger *debugger, guint id, IAnjutaDebuggerCallback callback, gpointer user_data)
2344 gchar *buff;
2346 DEBUG_PRINT ("In function: debugger_delete_breakpoint()");
2348 g_return_if_fail (IS_DEBUGGER (debugger));
2350 buff = g_strdup_printf ("-break-delete %d", id);
2351 debugger_queue_command (debugger, buff, FALSE, FALSE, debugger_remove_breakpoint_finish, callback, user_data);
2352 g_free (buff);
2355 static void
2356 debugger_list_breakpoint_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2358 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2359 gpointer user_data = debugger->priv->current_cmd.user_data;
2360 IAnjutaDebuggerBreakpointItem* bp;
2361 const GDBMIValue *table;
2362 GList *list = NULL;
2364 if ((error != NULL) || (mi_results == NULL))
2366 /* Call callback in all case (useful for enable that doesn't return
2367 * anything */
2368 if (callback != NULL)
2369 callback (NULL, user_data, error);
2372 table = gdbmi_value_hash_lookup (mi_results, "BreakpointTable");
2373 if (table)
2375 table = gdbmi_value_hash_lookup (table, "body");
2377 if (table)
2379 int i;
2381 for (i = 0; i < gdbmi_value_get_size (table); i++)
2383 const GDBMIValue *brkpnt;
2385 bp = g_new0 (IAnjutaDebuggerBreakpointItem, 1);
2387 brkpnt = gdbmi_value_list_get_nth (table, i);
2388 parse_breakpoint(bp, brkpnt);
2389 list = g_list_prepend (list, bp);
2394 /* Call callback in all case (useful for enable that doesn't return
2395 * anything */
2396 if (callback != NULL)
2398 list = g_list_reverse (list);
2399 callback (list, user_data, error);
2402 g_list_foreach (list, (GFunc)g_free, NULL);
2403 g_list_free (list);
2406 void
2407 debugger_list_breakpoint (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2409 DEBUG_PRINT ("In function: debugger_list_breakpoint()");
2411 g_return_if_fail (IS_DEBUGGER (debugger));
2413 debugger_queue_command (debugger, "-break-list", FALSE, FALSE, debugger_list_breakpoint_finish, callback, user_data);
2416 static void
2417 debugger_print_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2419 gchar *ptr = NULL;
2420 gchar *tmp;
2421 GList *list, *node;
2422 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2423 gpointer user_data = debugger->priv->current_cmd.user_data;
2426 list = gdb_util_remove_blank_lines (cli_results);
2427 if (g_list_length (list) < 1)
2429 tmp = NULL;
2431 else
2433 tmp = strchr ((gchar *) list->data, '=');
2435 if (tmp != NULL)
2437 ptr = g_strdup (tmp);
2438 for (node = list ? list->next : NULL; node != NULL; node = node->next)
2440 tmp = ptr;
2441 ptr = g_strconcat (tmp, (gchar *) node->data, NULL);
2442 g_free (tmp);
2446 callback (ptr, user_data, NULL);
2447 g_free (ptr);
2450 void
2451 debugger_print (Debugger *debugger, const gchar* variable, IAnjutaDebuggerCallback callback, gpointer user_data)
2453 gchar *buff;
2455 DEBUG_PRINT ("In function: debugger_print()");
2457 g_return_if_fail (IS_DEBUGGER (debugger));
2459 buff = g_strdup_printf ("print %s", variable);
2460 debugger_queue_command (debugger, buff, TRUE, FALSE, debugger_print_finish, callback, user_data);
2461 g_free (buff);
2464 static void
2465 debugger_evaluate_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2468 const GDBMIValue *value = NULL;
2469 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2470 gpointer user_data = debugger->priv->current_cmd.user_data;
2472 if (mi_results)
2473 value = gdbmi_value_hash_lookup (mi_results, "value");
2475 /* Call user function */
2476 if (callback != NULL)
2477 callback (value == NULL ? "?" : (char *)gdbmi_value_literal_get (value), user_data, NULL);
2480 void
2481 debugger_evaluate (Debugger *debugger, const gchar* name, IAnjutaDebuggerCallback callback, gpointer user_data)
2483 gchar *buff;
2484 DEBUG_PRINT ("In function: debugger_add_watch()");
2486 g_return_if_fail (IS_DEBUGGER (debugger));
2488 buff = g_strdup_printf ("-data-evaluate-expression %s", name);
2489 debugger_queue_command (debugger, buff, TRUE, FALSE, debugger_evaluate_finish, callback, user_data);
2490 g_free (buff);
2493 static void
2494 debugger_list_local_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2497 const GDBMIValue *local, *var, *frame, *args, *stack;
2498 const gchar * name;
2499 GList* list;
2500 guint i;
2501 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2502 gpointer user_data = debugger->priv->current_cmd.user_data;
2505 list = NULL;
2506 /* Add arguments */
2507 stack = gdbmi_value_hash_lookup (mi_results, "stack-args");
2508 if (stack)
2510 frame = gdbmi_value_list_get_nth (stack, 0);
2511 if (frame)
2513 args = gdbmi_value_hash_lookup (frame, "args");
2514 if (args)
2516 for (i = 0; i < gdbmi_value_get_size (args); i++)
2518 var = gdbmi_value_list_get_nth (args, i);
2519 if (var)
2521 name = gdbmi_value_literal_get (var);
2522 list = g_list_prepend (list, (gchar *)name);
2530 /* List local variables */
2531 local = gdbmi_value_hash_lookup (mi_results, "locals");
2532 if (local)
2534 for (i = 0; i < gdbmi_value_get_size (local); i++)
2536 var = gdbmi_value_list_get_nth (local, i);
2537 if (var)
2539 name = gdbmi_value_literal_get (var);
2540 list = g_list_prepend (list, (gchar *)name);
2544 list = g_list_reverse (list);
2545 callback (list, user_data, NULL);
2546 g_list_free (list);
2549 void
2550 debugger_list_local (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2552 gchar *buff;
2553 DEBUG_PRINT ("In function: debugger_list_local()");
2555 g_return_if_fail (IS_DEBUGGER (debugger));
2557 buff = g_strdup_printf("-stack-list-arguments 0 %d %d", debugger->priv->current_frame, debugger->priv->current_frame);
2558 debugger_queue_command (debugger, buff, TRUE, TRUE, NULL, NULL, NULL);
2559 g_free (buff);
2560 debugger_queue_command (debugger, "-stack-list-locals 0", TRUE, FALSE, debugger_list_local_finish, callback, user_data);
2563 static void
2564 debugger_list_argument_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2567 const GDBMIValue *frame, *var, *args, *stack;
2568 const gchar * name;
2569 GList* list;
2570 guint i;
2571 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2572 gpointer user_data = debugger->priv->current_cmd.user_data;
2575 list = NULL;
2576 args = NULL;
2577 stack = gdbmi_value_hash_lookup (mi_results, "stack-args");
2578 if (stack)
2580 frame = gdbmi_value_list_get_nth (stack, 0);
2581 if (frame)
2583 args = gdbmi_value_hash_lookup (frame, "args");
2587 if (args)
2589 for (i = 0; i < gdbmi_value_get_size (args); i++)
2591 var = gdbmi_value_list_get_nth (args, i);
2592 if (var)
2594 name = gdbmi_value_literal_get (var);
2595 list = g_list_prepend (list, (gchar *)name);
2599 list = g_list_reverse (list);
2600 callback (list, user_data, NULL);
2601 g_list_free (list);
2604 void
2605 debugger_list_argument (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2607 gchar *buff;
2609 DEBUG_PRINT ("In function: debugger_list_argument()");
2611 g_return_if_fail (IS_DEBUGGER (debugger));
2613 buff = g_strdup_printf("-stack-list-arguments 0 %d %d", debugger->priv->current_frame, debugger->priv->current_frame);
2614 debugger_queue_command (debugger, buff, TRUE, FALSE, debugger_list_argument_finish, callback, user_data);
2615 g_free (buff);
2618 static void
2619 debugger_info_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2622 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2623 gpointer user_data = debugger->priv->current_cmd.user_data;
2625 if (callback != NULL)
2626 callback ((GList *)cli_results, user_data, NULL);
2629 void
2630 debugger_info_frame (Debugger *debugger, guint frame, IAnjutaDebuggerCallback callback, gpointer user_data)
2632 gchar *buff;
2634 DEBUG_PRINT ("In function: debugger_info_frame()");
2636 g_return_if_fail (IS_DEBUGGER (debugger));
2638 if (frame == 0)
2640 buff = g_strdup_printf("info frame");
2642 else
2644 buff = g_strdup_printf ("info frame %d", frame);
2646 debugger_queue_command (debugger, buff, TRUE, FALSE, (DebuggerParserFunc)debugger_info_finish, callback, user_data);
2647 g_free (buff);
2650 void
2651 debugger_info_signal (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2653 DEBUG_PRINT ("In function: debugger_info_signal()");
2655 g_return_if_fail (IS_DEBUGGER (debugger));
2657 debugger_queue_command (debugger, "info signals", TRUE, FALSE, (DebuggerParserFunc)debugger_info_finish, callback, user_data);
2660 void
2661 debugger_info_sharedlib (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2663 gchar *buff;
2665 DEBUG_PRINT ("In function: debugger_info_sharedlib()");
2667 g_return_if_fail (IS_DEBUGGER (debugger));
2669 buff = g_strdup_printf ("info sharedlib");
2670 debugger_queue_command (debugger, buff, TRUE, FALSE, (DebuggerParserFunc)debugger_info_finish, callback, user_data); g_free (buff);
2673 void
2674 debugger_info_args (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2676 DEBUG_PRINT ("In function: debugger_info_args()");
2678 g_return_if_fail (IS_DEBUGGER (debugger));
2680 debugger_queue_command (debugger, "info args", TRUE, FALSE, (DebuggerParserFunc)debugger_info_finish, callback, user_data);
2683 void
2684 debugger_info_target (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2686 DEBUG_PRINT ("In function: debugger_info_target()");
2688 g_return_if_fail (IS_DEBUGGER (debugger));
2690 debugger_queue_command (debugger, "info target", TRUE, FALSE, (DebuggerParserFunc)debugger_info_finish, callback, user_data);
2693 void
2694 debugger_info_program (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2696 DEBUG_PRINT ("In function: debugger_info_program()");
2698 g_return_if_fail (IS_DEBUGGER (debugger));
2700 debugger_queue_command (debugger, "info program", TRUE, FALSE, (DebuggerParserFunc)debugger_info_finish, callback, user_data);
2703 void
2704 debugger_info_udot (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2706 DEBUG_PRINT ("In function: debugger_info_udot()");
2708 g_return_if_fail (IS_DEBUGGER (debugger));
2710 debugger_queue_command (debugger, "info udot", TRUE, FALSE, (DebuggerParserFunc)debugger_info_finish, callback, user_data);
2713 void
2714 debugger_info_variables (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2716 DEBUG_PRINT ("In function: debugger_info_variables()");
2718 g_return_if_fail (IS_DEBUGGER (debugger));
2720 debugger_queue_command (debugger, "info variables", TRUE, FALSE, (DebuggerParserFunc)debugger_info_finish, callback, user_data);
2723 static void
2724 debugger_read_memory_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2727 const GDBMIValue *literal;
2728 const GDBMIValue *mem;
2729 const gchar *value;
2730 gchar *data;
2731 gchar *ptr;
2732 gulong address;
2733 guint len;
2734 guint i;
2735 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2736 gpointer user_data = debugger->priv->current_cmd.user_data;
2737 IAnjutaDebuggerMemoryBlock read = {0,};
2739 literal = gdbmi_value_hash_lookup (mi_results, "total-bytes");
2740 if (literal)
2742 guint size;
2744 len = strtoul (gdbmi_value_literal_get (literal), NULL, 10);
2745 data = g_new (gchar, len * 2);
2746 memset (data + len, 0, len);
2748 literal = gdbmi_value_hash_lookup (mi_results, "addr");
2749 address = strtoul (gdbmi_value_literal_get (literal), NULL, 0);
2751 ptr = data;
2752 size = 0;
2753 mem = gdbmi_value_hash_lookup (mi_results, "memory");
2754 if (mem)
2756 mem = gdbmi_value_list_get_nth (mem, 0);
2757 if (mem)
2759 mem = gdbmi_value_hash_lookup (mem, "data");
2760 if (mem)
2762 size = gdbmi_value_get_size (mem);
2767 if (size < len) len = size;
2768 for (i = 0; i < len; i++)
2770 literal = gdbmi_value_list_get_nth (mem, i);
2771 if (literal)
2773 gchar *endptr;
2774 value = gdbmi_value_literal_get (literal);
2775 *ptr = strtoul (value, &endptr, 16);
2776 if ((*value != '\0') && (*endptr == '\0'))
2778 /* valid data */
2779 ptr[len] = 1;
2781 ptr++;
2784 read.address = address;
2785 read.length = len;
2786 read.data = data;
2787 callback (&read, user_data, NULL);
2789 g_free (data);
2791 else
2793 callback (NULL, user_data, NULL);
2797 void
2798 debugger_inspect_memory (Debugger *debugger, gulong address, guint length, IAnjutaDebuggerCallback callback, gpointer user_data)
2800 gchar *buff;
2802 DEBUG_PRINT ("In function: debugger_inspect_memory()");
2804 g_return_if_fail (IS_DEBUGGER (debugger));
2806 buff = g_strdup_printf ("-data-read-memory 0x%lx x 1 1 %d", address, length);
2807 debugger_queue_command (debugger, buff, FALSE, FALSE, debugger_read_memory_finish, callback, user_data);
2808 g_free (buff);
2811 static void
2812 debugger_disassemble_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2815 const GDBMIValue *literal;
2816 const GDBMIValue *line;
2817 const GDBMIValue *mem;
2818 const gchar *value;
2819 guint i;
2820 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2821 gpointer user_data = debugger->priv->current_cmd.user_data;
2822 IAnjutaDebuggerInstructionDisassembly *read = NULL;
2824 if (error != NULL)
2826 /* Command fail */
2827 callback (NULL, user_data, error);
2829 return;
2833 mem = gdbmi_value_hash_lookup (mi_results, "asm_insns");
2834 if (mem)
2836 guint size;
2838 size = gdbmi_value_get_size (mem);
2839 read = (IAnjutaDebuggerInstructionDisassembly *)g_malloc0(sizeof (IAnjutaDebuggerInstructionDisassembly) + sizeof(IAnjutaDebuggerInstructionALine) * size);
2840 read->size = size;
2842 for (i = 0; i < size; i++)
2844 line = gdbmi_value_list_get_nth (mem, i);
2845 if (line)
2847 /* Get address */
2848 literal = gdbmi_value_hash_lookup (line, "address");
2849 if (literal)
2851 value = gdbmi_value_literal_get (literal);
2852 read->data[i].address = strtoul (value, NULL, 0);
2855 /* Get label if one exist */
2856 literal = gdbmi_value_hash_lookup (line, "offset");
2857 if (literal)
2859 value = gdbmi_value_literal_get (literal);
2860 if ((value != NULL) && (strtoul (value, NULL, 0) == 0))
2862 literal = gdbmi_value_hash_lookup (line, "func-name");
2863 if (literal)
2865 read->data[i].label = gdbmi_value_literal_get (literal);
2872 /* Get disassembly line */
2873 literal = gdbmi_value_hash_lookup (line, "inst");
2874 if (literal)
2876 read->data[i].text = gdbmi_value_literal_get (literal);
2881 /* Remove last line to mark end */
2882 read->data[i - 1].text = NULL;
2884 callback (read, user_data, NULL);
2886 g_free (read);
2888 else
2890 callback (NULL, user_data, NULL);
2894 void
2895 debugger_disassemble (Debugger *debugger, gulong address, guint length, IAnjutaDebuggerCallback callback, gpointer user_data)
2897 gchar *buff;
2898 gulong end;
2900 DEBUG_PRINT ("In function: debugger_disassemble()");
2902 g_return_if_fail (IS_DEBUGGER (debugger));
2905 /* Handle overflow */
2906 end = (address + length < address) ? G_MAXULONG : address + length;
2907 buff = g_strdup_printf ("-data-disassemble -s 0x%lx -e 0x%lx -- 0", address, end);
2908 debugger_queue_command (debugger, buff, FALSE, FALSE, debugger_disassemble_finish, callback, user_data);
2909 g_free (buff);
2912 static void
2913 parse_frame (IAnjutaDebuggerFrame *frame, const GDBMIValue *frame_hash)
2915 const GDBMIValue *literal;
2917 literal = gdbmi_value_hash_lookup (frame_hash, "level");
2918 if (literal)
2919 frame->level = strtoul (gdbmi_value_literal_get (literal), NULL, 10);
2920 else
2921 frame->level = 0;
2923 literal = gdbmi_value_hash_lookup (frame_hash, "fullname");
2924 if (literal == NULL)
2925 literal = gdbmi_value_hash_lookup (frame_hash, "file");
2926 if (literal)
2927 frame->file = (gchar *)gdbmi_value_literal_get (literal);
2928 else
2929 frame->file = NULL;
2931 literal = gdbmi_value_hash_lookup (frame_hash, "line");
2932 if (literal)
2933 frame->line = strtoul (gdbmi_value_literal_get (literal), NULL, 10);
2934 else
2935 frame->line = 0;
2937 literal = gdbmi_value_hash_lookup (frame_hash, "func");
2938 if (literal)
2939 frame->function = (gchar *)gdbmi_value_literal_get (literal);
2940 else
2941 frame->function = NULL;
2943 literal = gdbmi_value_hash_lookup (frame_hash, "from");
2944 if (literal)
2945 frame->library = (gchar *)gdbmi_value_literal_get (literal);
2946 else
2947 frame->library = NULL;
2949 literal = gdbmi_value_hash_lookup (frame_hash, "addr");
2950 if (literal)
2951 frame->address = strtoul (gdbmi_value_literal_get (literal), NULL, 16);
2952 else
2953 frame->address = 0;
2958 static void
2959 add_frame (const GDBMIValue *frame_hash, GdbGListPacket* pack)
2961 IAnjutaDebuggerFrame* frame;
2963 frame = g_new0 (IAnjutaDebuggerFrame, 1);
2964 pack->list = g_list_prepend (pack->list, frame);
2966 frame->thread = pack->tag;
2967 parse_frame (frame, frame_hash);
2970 static void
2971 set_func_args (const GDBMIValue *frame_hash, GList** node)
2973 const gchar *level;
2974 const GDBMIValue *literal, *args_list, *arg_hash;
2975 gint i;
2976 GString *args_str;
2977 IAnjutaDebuggerFrame* frame;
2979 literal = gdbmi_value_hash_lookup (frame_hash, "level");
2980 if (!literal)
2981 return;
2983 level = gdbmi_value_literal_get (literal);
2984 if (!level)
2985 return;
2987 frame = (IAnjutaDebuggerFrame *)(*node)->data;
2989 args_list = gdbmi_value_hash_lookup (frame_hash, "args");
2990 if (args_list)
2992 args_str = g_string_new ("(");
2993 for (i = 0; i < gdbmi_value_get_size (args_list); i++)
2995 const gchar *name, *value;
2997 arg_hash = gdbmi_value_list_get_nth (args_list, i);
2998 if (!arg_hash)
2999 continue;
3001 literal = gdbmi_value_hash_lookup (arg_hash, "name");
3002 if (!literal)
3003 continue;
3004 name = gdbmi_value_literal_get (literal);
3005 if (!name)
3006 continue;
3008 literal = gdbmi_value_hash_lookup (arg_hash, "value");
3009 if (!literal)
3010 continue;
3011 value = gdbmi_value_literal_get (literal);
3012 if (!value)
3013 continue;
3014 args_str = g_string_append (args_str, name);
3015 args_str = g_string_append (args_str, "=");
3016 args_str = g_string_append (args_str, value);
3017 if (i < (gdbmi_value_get_size (args_list) - 1))
3018 args_str = g_string_append (args_str, ", ");
3020 args_str = g_string_append (args_str, ")");
3021 frame->args = args_str->str;
3022 g_string_free (args_str, FALSE);
3024 *node = g_list_next (*node);
3027 static void
3028 debugger_stack_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
3031 GdbGListPacket pack = {NULL, 0};
3032 GList* node;
3033 const GDBMIValue *stack_list;
3034 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
3035 gpointer user_data = debugger->priv->current_cmd.user_data;
3037 if (!mi_results)
3038 return;
3040 stack_list = gdbmi_value_hash_lookup (mi_results, "stack");
3041 if (stack_list)
3043 pack.tag = debugger->priv->current_thread;
3044 gdbmi_value_foreach (stack_list, (GFunc)add_frame, &pack);
3047 if (pack.list)
3049 pack.list = g_list_reverse (pack.list);
3050 node = g_list_first (pack.list);
3051 stack_list = gdbmi_value_hash_lookup (mi_results, "stack-args");
3052 if (stack_list)
3053 gdbmi_value_foreach (stack_list, (GFunc)set_func_args, &node);
3055 // Call call back function
3056 if (callback != NULL)
3057 callback (pack.list, user_data, NULL);
3059 // Free data
3060 for (node = g_list_first (pack.list); node != NULL; node = g_list_next (node))
3062 g_free ((gchar *)((IAnjutaDebuggerFrame *)node->data)->args);
3063 g_free (node->data);
3066 g_list_free (pack.list);
3068 else
3070 // Call call back function
3071 if (callback != NULL)
3072 callback (NULL, user_data, NULL);
3076 void
3077 debugger_list_frame (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
3079 DEBUG_PRINT ("In function: debugger_list_frame()");
3081 g_return_if_fail (IS_DEBUGGER (debugger));
3083 debugger_queue_command (debugger, "-stack-list-frames", TRUE, TRUE, NULL, NULL, NULL);
3084 debugger_queue_command (debugger, "-stack-list-arguments 1", TRUE, FALSE, debugger_stack_finish, callback, user_data);
3087 /* Thread functions
3088 *---------------------------------------------------------------------------*/
3090 static void
3091 debugger_set_thread_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
3093 const GDBMIValue *literal;
3094 guint id;
3096 if (mi_results == NULL) return;
3098 literal = gdbmi_value_hash_lookup (mi_results, "new-thread-id");
3099 if (literal == NULL) return;
3101 id = strtoul (gdbmi_value_literal_get (literal), NULL, 10);
3102 if (id == 0) return;
3104 debugger->priv->current_thread = id;
3105 g_signal_emit_by_name (debugger->priv->instance, "frame-changed", 0, debugger->priv->current_thread);
3107 return;
3110 void
3111 debugger_set_thread (Debugger *debugger, gint thread)
3113 gchar *buff;
3115 DEBUG_PRINT ("In function: debugger_set_thread()");
3117 g_return_if_fail (IS_DEBUGGER (debugger));
3119 buff = g_strdup_printf ("-thread-select %d", thread);
3121 debugger_queue_command (debugger, buff, FALSE, FALSE, (DebuggerParserFunc)debugger_set_thread_finish, NULL, NULL);
3122 g_free (buff);
3125 static void
3126 add_thread_id (const GDBMIValue *thread_hash, GList** list)
3128 IAnjutaDebuggerFrame* frame;
3129 gint thread;
3131 thread = strtoul (gdbmi_value_literal_get (thread_hash), NULL, 10);
3132 if (thread == 0) return;
3134 frame = g_new0 (IAnjutaDebuggerFrame, 1);
3135 *list = g_list_prepend (*list, frame);
3137 frame->thread = thread;
3140 static void
3141 debugger_list_thread_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
3144 const GDBMIValue *id_list;
3145 gpointer user_data = debugger->priv->current_cmd.user_data;
3146 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
3147 GList* thread_list = NULL;
3149 for (;;)
3151 if (mi_results == NULL) break;
3153 id_list = gdbmi_value_hash_lookup (mi_results, "thread-ids");
3154 if (id_list == NULL) break;
3156 gdbmi_value_foreach (id_list, (GFunc)add_thread_id, &thread_list);
3157 thread_list = g_list_reverse (thread_list);
3158 break;
3161 if (callback != NULL)
3162 callback (thread_list, user_data, error);
3164 if (thread_list != NULL)
3166 g_list_foreach (thread_list, (GFunc)g_free, NULL);
3167 g_list_free (thread_list);
3171 void
3172 debugger_list_thread (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
3174 DEBUG_PRINT ("In function: debugger_list_thread()");
3176 g_return_if_fail (IS_DEBUGGER (debugger));
3178 debugger_queue_command (debugger, "-thread-list-ids", TRUE, FALSE, debugger_list_thread_finish, callback, user_data);
3181 static void
3182 debugger_info_set_thread_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
3184 const GDBMIValue *literal;
3185 guint id;
3187 if (mi_results == NULL) return;
3189 literal = gdbmi_value_hash_lookup (mi_results, "new-thread-id");
3190 if (literal == NULL) return;
3192 id = strtoul (gdbmi_value_literal_get (literal), NULL, 10);
3193 if (id == 0) return;
3195 debugger->priv->current_thread = id;
3196 /* Do not emit a signal notification as the current thread will
3197 * be restored when needed */
3199 return;
3202 static void
3203 debugger_info_thread_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
3205 const GDBMIValue *frame;
3206 IAnjutaDebuggerFrame top_frame;
3207 IAnjutaDebuggerFrame *top;
3208 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
3209 gpointer user_data = debugger->priv->current_cmd.user_data;
3211 for (top = NULL;;)
3213 DEBUG_PRINT("look for stack %p", mi_results);
3214 if (mi_results == NULL) break;
3216 frame = gdbmi_value_hash_lookup (mi_results, "stack");
3217 if (frame == NULL) break;
3219 DEBUG_PRINT("get stack");
3221 frame = gdbmi_value_list_get_nth (frame, 0);
3222 if (frame == NULL) break;
3224 DEBUG_PRINT("get nth element");
3226 /*frame = gdbmi_value_hash_lookup (frame, "frame");
3227 DEBUG_PRINT("get frame %p", frame);
3228 if (frame == NULL) break;*/
3230 DEBUG_PRINT("get frame");
3232 top = &top_frame;
3233 top->thread = debugger->priv->current_thread;
3235 parse_frame (top, frame);
3236 break;
3239 if (callback != NULL)
3240 callback (top, user_data, error);
3242 return;
3245 void
3246 debugger_info_thread (Debugger *debugger, gint thread, IAnjutaDebuggerCallback callback, gpointer user_data)
3248 gchar *buff;
3249 guint orig_thread;
3251 DEBUG_PRINT ("In function: debugger_info_thread()");
3253 g_return_if_fail (IS_DEBUGGER (debugger));
3255 orig_thread = debugger->priv->current_thread;
3256 buff = g_strdup_printf ("-thread-select %d", thread);
3257 debugger_queue_command (debugger, buff, FALSE, FALSE, (DebuggerParserFunc)debugger_info_set_thread_finish, NULL, NULL);
3258 g_free (buff);
3259 debugger_queue_command (debugger, "-stack-list-frames 0 0", FALSE, FALSE, (DebuggerParserFunc)debugger_info_thread_finish, callback, user_data);
3261 buff = g_strdup_printf ("-thread-select %d", orig_thread);
3262 debugger_queue_command (debugger, buff, FALSE, FALSE, (DebuggerParserFunc)debugger_info_set_thread_finish, NULL, NULL);
3263 g_free (buff);
3266 static void
3267 add_register_name (const GDBMIValue *reg_literal, GList** list)
3269 IAnjutaDebuggerRegisterData* reg;
3270 GList *prev = *list;
3272 reg = g_new0 (IAnjutaDebuggerRegisterData, 1);
3273 *list = g_list_prepend (prev, reg);
3274 reg->name = (gchar *)gdbmi_value_literal_get (reg_literal);
3275 reg->num = prev == NULL ? 0 : ((IAnjutaDebuggerRegisterData *)prev->data)->num + 1;
3278 static void
3279 add_register_value (const GDBMIValue *reg_hash, GList** list)
3281 const GDBMIValue *literal;
3282 const gchar *val;
3283 IAnjutaDebuggerRegisterData* reg;
3284 guint num;
3285 GList* prev = *list;
3287 literal = gdbmi_value_hash_lookup (reg_hash, "number");
3288 if (!literal)
3289 return;
3290 val = gdbmi_value_literal_get (literal);
3291 num = strtoul (val, NULL, 10);
3293 literal = gdbmi_value_hash_lookup (reg_hash, "value");
3294 if (!literal)
3295 return;
3297 reg = g_new0 (IAnjutaDebuggerRegisterData, 1);
3298 *list = g_list_prepend (prev, reg);
3299 reg->num = num;
3300 reg->value = (gchar *)gdbmi_value_literal_get (literal);
3303 static void
3304 debugger_register_name_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
3307 GList* list = NULL;
3308 GList* node;
3309 const GDBMIValue *reg_list;
3310 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
3311 gpointer user_data = debugger->priv->current_cmd.user_data;
3313 if (!mi_results)
3314 return;
3316 reg_list = gdbmi_value_hash_lookup (mi_results, "register-names");
3317 if (reg_list)
3318 gdbmi_value_foreach (reg_list, (GFunc)add_register_name, &list);
3319 list = g_list_reverse (list);
3321 // Call call back function
3322 if (callback != NULL)
3323 callback (list, user_data, NULL);
3325 // Free data
3326 for (node = g_list_first (list); node != NULL; node = g_list_next (node))
3328 g_free (node->data);
3330 g_list_free (list);
3333 static void
3334 debugger_register_value_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
3337 GList* list = NULL;
3338 GList* node;
3339 const GDBMIValue *reg_list;
3340 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
3341 gpointer user_data = debugger->priv->current_cmd.user_data;
3343 if (!mi_results)
3344 return;
3346 reg_list = gdbmi_value_hash_lookup (mi_results, "register-values");
3347 if (reg_list)
3348 gdbmi_value_foreach (reg_list, (GFunc)add_register_value, &list);
3349 list = g_list_reverse (list);
3351 // Call call back function
3352 if (callback != NULL)
3353 callback (list, user_data, NULL);
3355 // Free data
3356 for (node = g_list_first (list); node != NULL; node = g_list_next (node))
3358 g_free (node->data);
3360 g_list_free (list);
3363 void
3364 debugger_list_register (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
3366 DEBUG_PRINT ("In function: debugger_list_register()");
3368 g_return_if_fail (IS_DEBUGGER (debugger));
3370 debugger_queue_command (debugger, "-data-list-register-names", TRUE, FALSE, debugger_register_name_finish, callback, user_data);
3373 void
3374 debugger_update_register (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
3376 DEBUG_PRINT ("In function: debugger_update_register()");
3378 g_return_if_fail (IS_DEBUGGER (debugger));
3380 debugger_queue_command (debugger, "-data-list-register-values r", TRUE, FALSE, (DebuggerParserFunc)debugger_register_value_finish, callback, user_data);
3383 void
3384 debugger_write_register (Debugger *debugger, const gchar *name, const gchar *value)
3386 gchar *buf;
3388 DEBUG_PRINT ("In function: debugger_write_register()");
3390 g_return_if_fail (IS_DEBUGGER (debugger));
3392 buf = g_strdup_printf ("-data-evaluate-expression \"$%s=%s\"", name, value);
3393 debugger_queue_command (debugger, buf, TRUE, FALSE, NULL, NULL, NULL);
3394 g_free (buf);
3397 static void
3398 debugger_set_frame_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
3401 guint frame = (guint)debugger->priv->current_cmd.user_data;
3402 debugger->priv->current_frame = frame;
3404 g_signal_emit_by_name (debugger->priv->instance, "frame-changed", frame, debugger->priv->current_thread);
3407 void
3408 debugger_set_frame (Debugger *debugger, guint frame)
3410 gchar *buff;
3412 DEBUG_PRINT ("In function: debugger_set_frame()");
3414 g_return_if_fail (IS_DEBUGGER (debugger));
3416 buff = g_strdup_printf ("-stack-select-frame %d", frame);
3418 debugger_queue_command (debugger, buff, FALSE, FALSE, (DebuggerParserFunc)debugger_set_frame_finish, NULL, (gpointer)frame);
3419 g_free (buff);
3422 void
3423 debugger_set_log (Debugger *debugger, IAnjutaMessageView *log)
3425 debugger->priv->log = log;
3428 /* Variable objects functions
3429 *---------------------------------------------------------------------------*/
3431 void
3432 debugger_delete_variable (Debugger *debugger, const gchar* name)
3434 gchar *buff;
3436 DEBUG_PRINT ("In function: delete_variable()");
3438 g_return_if_fail (IS_DEBUGGER (debugger));
3440 buff = g_strdup_printf ("-var-delete %s", name);
3441 debugger_queue_command (debugger, buff, FALSE, FALSE, NULL, NULL, NULL);
3442 g_free (buff);
3445 static void
3446 gdb_var_evaluate_expression (Debugger *debugger,
3447 const GDBMIValue *mi_results, const GList *cli_results,
3448 GError *error)
3450 const gchar *value = NULL;
3451 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
3452 gpointer user_data = debugger->priv->current_cmd.user_data;
3454 if (mi_results != NULL)
3456 const GDBMIValue *const gdbmi_value =
3457 gdbmi_value_hash_lookup (mi_results, "value");
3459 if (gdbmi_value != NULL)
3460 value = gdbmi_value_literal_get (gdbmi_value);
3462 callback ((const gpointer)value, user_data, NULL);
3465 void
3466 debugger_evaluate_variable (Debugger *debugger, const gchar* name, IAnjutaDebuggerCallback callback, gpointer user_data)
3468 gchar *buff;
3470 DEBUG_PRINT ("In function: evaluate_variable()");
3472 g_return_if_fail (IS_DEBUGGER (debugger));
3474 buff = g_strdup_printf ("-var-evaluate-expression %s", name);
3475 debugger_queue_command (debugger, buff, FALSE, FALSE, gdb_var_evaluate_expression, callback, user_data);
3476 g_free (buff);
3479 void
3480 debugger_assign_variable (Debugger *debugger, const gchar* name, const gchar *value)
3482 gchar *buff;
3484 DEBUG_PRINT ("In function: assign_variable()");
3486 g_return_if_fail (IS_DEBUGGER (debugger));
3488 buff = g_strdup_printf ("-var-assign %s %s", name, value);
3489 debugger_queue_command (debugger, buff, FALSE, FALSE, NULL, NULL, NULL);
3490 g_free (buff);
3493 static void
3494 gdb_var_list_children (Debugger *debugger,
3495 const GDBMIValue *mi_results, const GList *cli_results,
3496 GError *error)
3498 GList* list = NULL;
3499 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
3500 gpointer user_data = debugger->priv->current_cmd.user_data;
3502 if (mi_results != NULL)
3504 const GDBMIValue *literal;
3505 const GDBMIValue *children;
3506 glong numchild = 0;
3507 glong i = 0;
3509 literal = gdbmi_value_hash_lookup (mi_results, "numchild");
3511 if (literal)
3512 numchild = strtoul(gdbmi_value_literal_get (literal), NULL, 0);
3513 children = gdbmi_value_hash_lookup (mi_results, "children");
3515 for(i = 0 ; i < numchild; ++i)
3517 const GDBMIValue *const gdbmi_chl =
3518 gdbmi_value_list_get_nth (children, i);
3519 IAnjutaDebuggerVariableObject *var;
3521 var = g_new0 (IAnjutaDebuggerVariableObject, 1);
3523 literal = gdbmi_value_hash_lookup (gdbmi_chl, "name");
3524 if (literal)
3525 var->name = (gchar *)gdbmi_value_literal_get (literal);
3527 literal = gdbmi_value_hash_lookup (gdbmi_chl, "exp");
3528 if (literal)
3529 var->expression = (gchar *)gdbmi_value_literal_get(literal);
3531 literal = gdbmi_value_hash_lookup (gdbmi_chl, "type");
3532 if (literal)
3533 var->type = (gchar *)gdbmi_value_literal_get(literal);
3535 literal = gdbmi_value_hash_lookup (gdbmi_chl, "value");
3536 if (literal)
3537 var->value = (gchar *)gdbmi_value_literal_get(literal);
3539 literal = gdbmi_value_hash_lookup (gdbmi_chl, "numchild");
3540 if (literal)
3541 var->children = strtoul(gdbmi_value_literal_get(literal), NULL, 10);
3543 list = g_list_prepend (list, var);
3545 list = g_list_reverse (list);
3548 callback (list, user_data, NULL);
3549 g_list_foreach (list, (GFunc)g_free, NULL);
3550 g_list_free (list);
3553 void debugger_list_variable_children (Debugger *debugger, const gchar* name, IAnjutaDebuggerCallback callback, gpointer user_data)
3555 gchar *buff;
3557 DEBUG_PRINT ("In function: list_variable_children()");
3559 g_return_if_fail (IS_DEBUGGER (debugger));
3561 buff = g_strdup_printf ("-var-list-children --all-values %s", name);
3562 debugger_queue_command (debugger, buff, FALSE, FALSE, gdb_var_list_children, callback, user_data);
3563 g_free (buff);
3566 static void
3567 gdb_var_create (Debugger *debugger,
3568 const GDBMIValue *mi_results, const GList *cli_results,
3569 GError *error)
3571 const GDBMIValue * result;
3572 IAnjutaDebuggerVariableObject var = {0,};
3573 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
3574 gpointer user_data = debugger->priv->current_cmd.user_data;
3576 if ((error == NULL) && (mi_results != NULL))
3578 result = gdbmi_value_hash_lookup (mi_results, "name");
3579 var.name = (gchar *)gdbmi_value_literal_get(result);
3581 result = gdbmi_value_hash_lookup (mi_results, "type");
3582 var.type = (gchar *)gdbmi_value_literal_get (result);
3584 result = gdbmi_value_hash_lookup (mi_results, "numchild");
3585 var.children = strtoul (gdbmi_value_literal_get(result), NULL, 10);
3587 callback (&var, user_data, error);
3591 void debugger_create_variable (Debugger *debugger, const gchar* name, IAnjutaDebuggerCallback callback, gpointer user_data)
3593 gchar *buff;
3595 DEBUG_PRINT ("In function: create_variable()");
3597 g_return_if_fail (IS_DEBUGGER (debugger));
3599 buff = g_strdup_printf ("-var-create - * %s", name);
3600 debugger_queue_command (debugger, buff, FALSE, FALSE, gdb_var_create, callback, user_data);
3601 g_free (buff);
3604 static void
3605 gdb_var_update (Debugger *debugger,
3606 const GDBMIValue *mi_results, const GList *cli_results,
3607 GError *error)
3609 GList* list = NULL;
3610 glong idx = 0, changed_count = 0;
3611 const GDBMIValue *const gdbmi_changelist =
3612 gdbmi_value_hash_lookup (mi_results, "changelist");
3613 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
3614 gpointer user_data = debugger->priv->current_cmd.user_data;
3617 changed_count = gdbmi_value_get_size (gdbmi_changelist);
3618 for(; idx<changed_count; ++idx)
3620 const GDBMIValue *const gdbmi_change =
3621 gdbmi_value_list_get_nth (gdbmi_changelist, idx);
3622 const GDBMIValue * value;
3625 value = gdbmi_value_hash_lookup (gdbmi_change, "name");
3626 if (value)
3628 IAnjutaDebuggerVariableObject *var = g_new0 (IAnjutaDebuggerVariableObject, 1);
3629 var->changed = TRUE;
3630 var->name = (gchar *)gdbmi_value_literal_get(value);
3631 list = g_list_prepend (list, var);
3633 value = gdbmi_value_hash_lookup (gdbmi_change, "type_changed");
3634 if (value != NULL)
3636 const gchar *type_changed = gdbmi_value_literal_get (value);
3638 if (strcmp (type_changed, "true"))
3640 var->deleted = TRUE;
3644 value = gdbmi_value_hash_lookup (gdbmi_change, "in_scope");
3645 if (value != NULL)
3647 const gchar *in_scope = gdbmi_value_literal_get(value);
3649 if (strcmp (in_scope, "false") == 0)
3651 var->exited = TRUE;
3653 else if (strcmp (in_scope, "invalid") == 0)
3655 var->deleted = TRUE;
3660 list = g_list_reverse (list);
3661 callback (list, user_data, NULL);
3662 g_list_foreach (list, (GFunc)g_free, NULL);
3663 g_list_free (list);
3666 void debugger_update_variable (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
3668 DEBUG_PRINT ("In function: update_variable()");
3670 g_return_if_fail (IS_DEBUGGER (debugger));
3672 debugger_queue_command (debugger, "-var-update *", FALSE, FALSE, gdb_var_update, callback, user_data);
3675 GType
3676 debugger_get_type (void)
3678 static GType obj_type = 0;
3680 if (!obj_type)
3682 static const GTypeInfo obj_info =
3684 sizeof (DebuggerClass),
3685 (GBaseInitFunc) NULL,
3686 (GBaseFinalizeFunc) NULL,
3687 (GClassInitFunc) debugger_class_init,
3688 (GClassFinalizeFunc) NULL,
3689 NULL, /* class_data */
3690 sizeof (Debugger),
3691 0, /* n_preallocs */
3692 (GInstanceInitFunc) debugger_instance_init,
3693 NULL /* value_table */
3695 obj_type = g_type_register_static (G_TYPE_OBJECT,
3696 "Debugger", &obj_info, 0);
3698 return obj_type;
3701 static void
3702 debugger_dispose (GObject *obj)
3704 Debugger *debugger = DEBUGGER (obj);
3706 DEBUG_PRINT ("In function: debugger_shutdown()");
3708 /* Do not emit signal when the debugger is destroyed */
3709 debugger->priv->instance = NULL;
3710 debugger_abort (debugger);
3712 /* Good Bye message */
3713 if (debugger->priv->output_callback)
3715 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
3716 "Debugging session completed.\n",
3717 debugger->priv->output_user_data);
3720 if (debugger->priv->launcher)
3722 anjuta_launcher_reset (debugger->priv->launcher);
3723 g_object_unref (debugger->priv->launcher);
3724 debugger->priv->launcher = NULL;
3727 GNOME_CALL_PARENT (G_OBJECT_CLASS, dispose, (obj));
3730 static void
3731 debugger_finalize (GObject *obj)
3733 Debugger *debugger = DEBUGGER (obj);
3734 g_string_free (debugger->priv->stdo_line, TRUE);
3735 g_string_free (debugger->priv->stdo_acc, TRUE);
3736 g_string_free (debugger->priv->stde_line, TRUE);
3737 g_free (debugger->priv);
3738 GNOME_CALL_PARENT (G_OBJECT_CLASS, finalize, (obj));
3741 static void
3742 debugger_class_init (DebuggerClass * klass)
3744 GObjectClass *object_class;
3746 g_return_if_fail (klass != NULL);
3747 object_class = G_OBJECT_CLASS (klass);
3749 DEBUG_PRINT ("Initializing debugger class");
3751 parent_class = g_type_class_peek_parent (klass);
3752 object_class->dispose = debugger_dispose;
3753 object_class->finalize = debugger_finalize;
3757 #if 0 /* FIXME */
3758 void
3759 debugger_signal (const gchar *sig, gboolean show_msg)
3761 /* eg:- "SIGTERM" */
3762 gchar *buff;
3763 gchar *cmd;
3765 DEBUG_PRINT ("In function: debugger_signal()");
3767 if (debugger_is_active () == FALSE)
3768 return;
3769 if (debugger.prog_is_running == FALSE)
3770 return;
3771 if (debugger.child_pid < 1)
3773 DEBUG_PRINT ("Not sending signal - pid not known\n");
3774 return;
3777 if (show_msg)
3779 buff = g_strdup_printf (_("Sending signal %s to the process: %d"),
3780 sig, (int) debugger.child_pid);
3781 gdb_util_append_message (ANJUTA_PLUGIN (debugger.plugin), buff);
3782 g_free (buff);
3785 if (debugger_is_ready ())
3787 cmd = g_strconcat ("signal ", sig, NULL);
3788 stack_trace_set_frame (debugger.stack, 0);
3789 debugger_put_cmd_in_queqe (cmd, DB_CMD_ALL, NULL, NULL);
3790 debugger_put_cmd_in_queqe ("info program", DB_CMD_NONE,
3791 on_debugger_update_prog_status,
3792 NULL);
3793 g_free (cmd);
3794 debugger_execute_cmd_in_queqe ();
3796 else
3798 GtkWindow *parent;
3799 int status;
3801 parent = GTK_WINDOW (ANJUTA_PLUGIN (debugger.plugin)->shell);
3802 status = gdb_util_kill_process (debugger.child_pid, sig);
3803 if (status != 0 && show_msg)
3804 anjuta_util_dialog_error (parent,
3805 _("Error whilst signaling the process."));
3809 static void
3810 query_set_cmd (const gchar *cmd, gboolean state)
3812 gchar buffer[50];
3813 gchar *tmp = g_stpcpy (buffer, cmd);
3814 strcpy (tmp, state ? "on" : "off");
3815 debugger_put_cmd_in_queqe (buffer, DB_CMD_NONE, NULL, NULL);
3818 static void
3819 query_set_verbose (gboolean state)
3821 query_set_cmd ("set verbose ", state);
3824 static void
3825 query_set_print_staticmembers (gboolean state)
3827 query_set_cmd ("set print static-members ", state);
3830 static void
3831 query_set_print_pretty (gboolean state)
3833 query_set_cmd ("set print pretty ", state);
3836 void debugger_query_evaluate_expr_tip (const gchar *expr,
3837 DebuggerCLIFunc parser, gpointer data)
3839 query_set_verbose (FALSE);
3840 query_set_print_staticmembers (FALSE);
3841 gchar *printcmd = g_strconcat ("print ", expr, NULL);
3842 debugger_put_cmd_in_queqe (printcmd, DB_CMD_NONE, parser, data);
3843 query_set_verbose (TRUE);
3844 query_set_print_staticmembers (TRUE);
3845 g_free (printcmd);
3848 void
3849 debugger_query_evaluate_expression (const gchar *expr, DebuggerFunc parser,
3850 gpointer data)
3852 query_set_print_pretty (TRUE);
3853 query_set_verbose (FALSE);
3854 gchar *printcmd = g_strconcat ("print ", expr, NULL);
3855 debugger_put_cmd_in_queqe (printcmd, DB_CMD_SE_MESG | DB_CMD_SE_DIALOG,
3856 parser, data);
3857 query_set_print_pretty (FALSE);
3858 query_set_verbose (TRUE);
3859 g_free (printcmd);
3862 #endif