2007-02-02 Sebastien Granjoux <seb.sfo@free.fr>
[anjuta-git-plugin.git] / plugins / gdb / debugger.c
blob9cb93ffe83c283614da3aaf14fcc3503552984aa
1 /*
2 * debugger.c Copyright (C) 2000 Kh. Naba Kumar Singh
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; either version 2 of the License, or (at your option)
7 * any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc., 59
16 * Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
22 /*#define DEBUG*/
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27 #include <signal.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <ctype.h>
31 #include <sys/wait.h>
32 #include <errno.h>
33 #include <assert.h>
34 #include <fcntl.h>
36 #include <glib.h>
38 #include <libanjuta/anjuta-launcher.h>
39 #include <libanjuta/anjuta-debug.h>
40 #include <libanjuta/anjuta-children.h>
41 #include <libanjuta/anjuta-marshal.h>
43 #include "debugger.h"
44 #include "utilities.h"
46 #define GDB_PROMPT "(gdb)"
47 #define PREF_TERMINAL_COMMAND "anjuta.command.terminal"
48 #define FILE_BUFFER_SIZE 1024
49 #define GDB_PATH "gdb"
51 enum {
52 DEBUGGER_NONE,
53 DEBUGGER_EXIT,
54 DEBUGGER_RERUN_PROGRAM
57 enum
59 PROGRAM_LOADED_SIGNAL,
60 PROGRAM_RUNNING_SIGNAL,
61 PROGRAM_EXITED_SIGNAL,
62 PROGRAM_STOPPED_SIGNAL,
63 RESULTS_ARRIVED_SIGNAL,
64 LOCATION_CHANGED_SIGNAL,
65 BREAKPOINT_CHANGED_SIGNAL,
66 VARIABLE_CHANGED_SIGNAL,
67 LAST_SIGNAL
70 struct _DebuggerPriv
72 GtkWindow *parent_win;
74 IAnjutaDebuggerOutputCallback output_callback;
75 gpointer output_user_data;
77 GList *search_dirs;
79 gboolean prog_is_running;
80 gboolean prog_is_attached;
81 gboolean prog_is_loaded;
82 guint debugger_is_busy;
83 gint post_execution_flag;
85 AnjutaLauncher *launcher;
86 GString *stdo_line;
87 GString *stdo_acc;
88 GString *stde_line;
90 GList *cli_lines;
92 /* State */
93 gboolean solib_event;
94 gboolean stopping;
95 gboolean exiting;
96 gboolean starting;
97 gboolean terminating;
98 gboolean loading;
100 /* GDB command queue */
101 GList *cmd_queqe;
102 DebuggerCommand current_cmd;
103 gboolean skip_next_prompt;
104 gboolean command_output_sent;
106 gboolean term_is_running;
107 pid_t term_pid;
108 pid_t inferior_pid;
109 gint gnome_terminal_type;
111 GObject* instance;
113 IAnjutaMessageView *log;
116 gpointer parent_class;
118 static gchar *debugger_start_terminal (Debugger *debugger);
119 static void debugger_stop_terminal (Debugger *debugger);
121 static void debugger_queue_clear (Debugger *debugger);
122 static void debugger_queue_execute_command (Debugger *debugger);
124 static void gdb_stdout_line_arrived (Debugger *debugger, const gchar * line);
125 static void gdb_stderr_line_arrived (Debugger *debugger, const gchar * line);
126 static void debugger_stdo_flush (Debugger *debugger);
127 static void debugger_stde_flush (Debugger *debugger);
128 static void on_gdb_output_arrived (AnjutaLauncher *launcher,
129 AnjutaLauncherOutputType output_type,
130 const gchar *chars, gpointer data);
131 static void on_gdb_terminated (AnjutaLauncher *launcher,
132 gint child_pid, gint status,
133 gulong t, gpointer data);
135 static void debugger_class_init (DebuggerClass *klass);
136 static void debugger_instance_init (Debugger *debugger);
138 /* Useful functions
139 *---------------------------------------------------------------------------*/
141 typedef struct _GdbMessageCode GdbMessageCode;
143 struct _GdbMessageCode
145 const gchar *msg;
146 guint code;
149 const static GdbMessageCode GdbErrorMessage[] =
150 {{"mi_cmd_var_create: unable to create variable object",
151 IANJUTA_DEBUGGER_UNABLE_TO_CREATE_VARIABLE},
152 {"Cannot access memory at address ",
153 IANJUTA_DEBUGGER_UNABLE_TO_ACCESS_MEMORY},
154 {NULL, 0}};
156 static guint
157 gdb_match_error(const gchar *message)
159 GdbMessageCode* msg;
160 guint code;
162 for (msg = GdbErrorMessage; msg->msg != NULL; msg++)
164 gsize len = strlen (msg->msg);
166 if (!isspace (msg->msg[len - 1]))
168 /* Match the whole string */
169 len++;
172 if (memcmp (msg->msg, message, len) == 0)
174 return msg->code;
178 return IANJUTA_DEBUGGER_UNKNOWN_ERROR;
181 static void
182 debugger_initialize (Debugger *debugger)
184 DEBUG_PRINT ("In function: debugger_init()");
186 debugger->priv = g_new0 (DebuggerPriv, 1);
188 debugger->priv->output_callback = NULL;
189 debugger->priv->parent_win = NULL;
190 debugger->priv->search_dirs = NULL;
191 debugger->priv->launcher = anjuta_launcher_new ();
193 debugger->priv->prog_is_running = FALSE;
194 debugger->priv->debugger_is_busy = 0;
195 debugger->priv->starting = FALSE;
196 debugger->priv->terminating = FALSE;
197 debugger->priv->skip_next_prompt = FALSE;
198 debugger->priv->command_output_sent = FALSE;
200 strcpy (debugger->priv->current_cmd.cmd, "");
201 debugger->priv->current_cmd.parser = NULL;
203 debugger->priv->cmd_queqe = NULL;
204 debugger->priv->cli_lines = NULL;
205 debugger->priv->solib_event = FALSE;
207 debugger->priv->stdo_line = g_string_sized_new (FILE_BUFFER_SIZE);
208 g_string_assign (debugger->priv->stdo_line, "");
209 debugger->priv->stdo_acc = g_string_new ("");
211 debugger->priv->stde_line = g_string_sized_new (FILE_BUFFER_SIZE);
212 g_string_assign (debugger->priv->stde_line, "");
214 debugger->priv->term_is_running = FALSE;
215 debugger->priv->term_pid = -1;
217 debugger->priv->post_execution_flag = DEBUGGER_NONE;
218 debugger->priv->gnome_terminal_type = gdb_util_check_gnome_terminal();
220 DEBUG_PRINT ("gnome-terminal type %d found.",
221 debugger->priv->gnome_terminal_type);
224 static void
225 debugger_instance_init (Debugger *debugger)
227 debugger_initialize (debugger);
230 Debugger*
231 debugger_new (GtkWindow *parent_win, GObject* instance)
233 Debugger *debugger;
235 debugger = g_object_new (DEBUGGER_TYPE, NULL);
236 debugger->priv->parent_win = parent_win;
237 debugger->priv->instance = instance;
238 g_object_add_weak_pointer (instance, (gpointer *)&debugger->priv->instance);
240 return debugger;
243 void
244 debugger_free (Debugger *debugger)
246 g_return_if_fail (IS_DEBUGGER (debugger));
248 g_object_unref (debugger);
251 gboolean
252 debugger_is_ready (Debugger *debugger)
254 g_return_val_if_fail (IS_DEBUGGER (debugger), FALSE);
255 return !debugger->priv->debugger_is_busy;
258 gboolean
259 debugger_program_is_running (Debugger *debugger)
261 g_return_val_if_fail (IS_DEBUGGER (debugger), FALSE);
262 return debugger->priv->prog_is_running;
265 gboolean
266 debugger_program_is_attached (Debugger *debugger)
268 g_return_val_if_fail (IS_DEBUGGER (debugger), FALSE);
269 return debugger->priv->prog_is_attached;
272 gboolean
273 debugger_program_is_loaded (Debugger *debugger)
275 g_return_val_if_fail (IS_DEBUGGER (debugger), FALSE);
276 return debugger->priv->prog_is_loaded;
279 static void
280 debugger_log_command (Debugger *debugger, const gchar *command)
282 gchar* str;
283 gsize len;
285 if (debugger->priv->log == NULL) return;
287 if (*command == '-')
289 str = g_strdup (command);
290 len = strlen (command);
292 /* Remove trailing carriage return */
293 if (str[len - 1] == '\n') str[len - 1] = '\0';
295 /* Log only MI command as other are echo */
296 DEBUG_PRINT ("Cmd: %s", str);
297 ianjuta_message_view_append (debugger->priv->log, IANJUTA_MESSAGE_VIEW_TYPE_NORMAL, str, "", NULL);
298 g_free (str);
302 static void
303 debugger_log_output (Debugger *debugger, const gchar *line)
305 gchar* str;
306 const gchar* start;
307 IAnjutaMessageViewType type;
308 gsize len;
310 if (debugger->priv->log == NULL) return;
312 type = IANJUTA_MESSAGE_VIEW_TYPE_NORMAL;
313 switch (*line)
315 case '~':
316 type = IANJUTA_MESSAGE_VIEW_TYPE_INFO;
317 /* Go through */
318 case '&':
319 len = strlen(line);
320 start = line + 1;
322 /* Remove double quote if necessary */
323 if ((line[1] == '\"') && (line[len - 1] == '\"')) start++;
324 str = g_strcompress (line + 2);
325 len = strlen (str);
326 if (start == line + 2)
328 str[len - 1] = '\0';
329 len--;
332 /* Remove trailing carriage return */
333 if (str[len - 1] == '\n') str[len - 1] = '\0';
335 ianjuta_message_view_append (debugger->priv->log, type, str, "", NULL);
336 g_free (str);
337 break;
338 case '^':
339 if (strncmp(line + 1, "error", 5) == 0)
341 ianjuta_message_view_append (debugger->priv->log, IANJUTA_MESSAGE_VIEW_TYPE_ERROR, line + 1, "", NULL);
343 else
345 ianjuta_message_view_append (debugger->priv->log, IANJUTA_MESSAGE_VIEW_TYPE_WARNING, line + 1, "", NULL);
347 break;
348 case '@':
349 ianjuta_message_view_append (debugger->priv->log, IANJUTA_MESSAGE_VIEW_TYPE_NORMAL, line + 1, "", NULL);
350 break;
351 default:
352 ianjuta_message_view_append (debugger->priv->log, IANJUTA_MESSAGE_VIEW_TYPE_NORMAL, line, "", NULL);
353 break;
357 static void
358 debugger_emit_status (Debugger *debugger)
360 if (!debugger->priv->debugger_is_busy)
362 if (debugger->priv->loading)
364 debugger->priv->starting = FALSE;
365 debugger->priv->loading = FALSE;
366 debugger->priv->exiting = FALSE;
367 debugger->priv->stopping = FALSE;
368 debugger->priv->solib_event = FALSE;
369 g_signal_emit_by_name (debugger->priv->instance, "program-loaded");
371 else if (debugger->priv->starting)
373 debugger->priv->starting = FALSE;
374 debugger->priv->loading = FALSE;
375 debugger->priv->exiting = FALSE;
376 debugger->priv->stopping = FALSE;
377 debugger->priv->solib_event = FALSE;
378 g_signal_emit_by_name (debugger->priv->instance, "debugger-started");
380 else if (debugger->priv->exiting)
382 debugger->priv->exiting = FALSE;
383 debugger->priv->stopping = FALSE;
384 debugger->priv->solib_event = FALSE;
385 g_signal_emit_by_name (debugger->priv->instance, "program-exited");
387 else if (debugger->priv->solib_event)
389 debugger->priv->exiting = FALSE;
390 debugger->priv->stopping = FALSE;
391 debugger->priv->solib_event = FALSE;
392 g_signal_emit_by_name (debugger->priv->instance, "sharedlib-event");
394 else if (debugger->priv->stopping)
396 debugger->priv->exiting = FALSE;
397 debugger->priv->stopping = FALSE;
398 debugger->priv->solib_event = FALSE;
399 g_signal_emit_by_name (debugger->priv->instance, "program-stopped");
401 else
403 if (debugger->priv->prog_is_running || debugger->priv->prog_is_attached)
405 g_signal_emit_by_name (debugger->priv->instance, "debugger-ready", IANJUTA_DEBUGGER_PROGRAM_STOPPED);
407 else if (debugger->priv->prog_is_loaded)
409 g_signal_emit_by_name (debugger->priv->instance, "debugger-ready", IANJUTA_DEBUGGER_PROGRAM_LOADED);
411 else
413 g_signal_emit_by_name (debugger->priv->instance, "debugger-ready", IANJUTA_DEBUGGER_STARTED);
419 IAnjutaDebuggerStatus
420 debugger_get_status (Debugger *debugger)
422 if (debugger->priv->debugger_is_busy)
424 return IANJUTA_DEBUGGER_BUSY;
426 else
428 if (debugger->priv->prog_is_running || debugger->priv->prog_is_attached)
430 return IANJUTA_DEBUGGER_PROGRAM_STOPPED;
432 else if (debugger->priv->prog_is_loaded)
434 return IANJUTA_DEBUGGER_PROGRAM_LOADED;
436 else
438 return IANJUTA_DEBUGGER_STARTED;
443 static void
444 debugger_clear_buffers (Debugger *debugger)
446 DEBUG_PRINT ("In function: debugger_clear_buffers()");
448 /* Clear the output line buffer */
449 g_string_assign (debugger->priv->stdo_line, "");
450 if (!debugger->priv->current_cmd.keep_result)
451 g_string_assign (debugger->priv->stdo_acc, "");
453 /* Clear the error line buffer */
454 g_string_assign (debugger->priv->stde_line, "");
456 /* Clear CLI output lines */
457 g_list_foreach (debugger->priv->cli_lines, (GFunc)g_free, NULL);
458 g_list_free (debugger->priv->cli_lines);
459 debugger->priv->cli_lines = NULL;
462 static DebuggerCommand *
463 debugger_queue_get_next_command (Debugger *debugger)
465 DebuggerCommand *dc;
467 DEBUG_PRINT ("In function: debugger_get_next_command()");
469 if (debugger->priv->cmd_queqe)
471 dc = g_list_nth_data (debugger->priv->cmd_queqe, 0);
472 debugger->priv->cmd_queqe = g_list_remove (debugger->priv->cmd_queqe, dc);
474 else
475 dc = NULL;
476 return dc;
479 static gboolean
480 debugger_queue_set_next_command (Debugger *debugger)
482 DebuggerCommand *dc;
484 DEBUG_PRINT ("In function: debugger_set_next_command()");
486 dc = debugger_queue_get_next_command (debugger);
487 if (!dc)
489 strcpy (debugger->priv->current_cmd.cmd, "");
490 debugger->priv->current_cmd.parser = NULL;
491 debugger->priv->current_cmd.callback = NULL;
492 debugger->priv->current_cmd.user_data = NULL;
493 debugger->priv->current_cmd.suppress_error = FALSE;
494 debugger->priv->current_cmd.keep_result = FALSE;
496 return FALSE;
498 else
501 strcpy (debugger->priv->current_cmd.cmd, dc->cmd);
502 debugger->priv->current_cmd.parser = dc->parser;
503 debugger->priv->current_cmd.callback = dc->callback;
504 debugger->priv->current_cmd.user_data = dc->user_data;
505 debugger->priv->current_cmd.suppress_error = dc->suppress_error;
506 debugger->priv->current_cmd.keep_result = dc->keep_result;
507 g_free (dc);
509 return TRUE;
512 static void
513 debugger_queue_command (Debugger *debugger, const gchar *cmd,
514 gboolean suppress_error, gboolean keep_result,
515 DebuggerParserFunc parser,
516 IAnjutaDebuggerCallback callback, gpointer user_data)
518 DebuggerCommand *dc;
521 DEBUG_PRINT ("In function: debugger_queue_command (%s)", cmd);
523 dc = g_malloc (sizeof (DebuggerCommand));
524 if (dc)
526 strcpy (dc->cmd, cmd);
527 dc->parser = parser;
528 dc->callback = callback;
529 dc->user_data = user_data;
530 dc->suppress_error = suppress_error;
531 dc->keep_result = keep_result;
533 debugger->priv->cmd_queqe = g_list_append (debugger->priv->cmd_queqe, dc);
534 if (!debugger->priv->debugger_is_busy &&
535 g_list_length (debugger->priv->cmd_queqe) >= 1)
537 debugger_queue_execute_command (debugger);
541 static void
542 debugger_queue_clear (Debugger *debugger)
544 GList *node;
546 DEBUG_PRINT ("In function: debugger_queue_clear()");
548 node = debugger->priv->cmd_queqe;
549 while (node)
551 g_free (node->data);
552 node = g_list_next (node);
554 g_list_free (debugger->priv->cmd_queqe);
555 debugger->priv->cmd_queqe = NULL;
556 strcpy (debugger->priv->current_cmd.cmd, "");
557 debugger->priv->current_cmd.parser = NULL;
558 debugger->priv->current_cmd.callback = NULL;
559 debugger->priv->current_cmd.user_data = NULL;
560 debugger->priv->current_cmd.suppress_error = FALSE;
561 debugger->priv->current_cmd.keep_result = FALSE;
562 debugger_clear_buffers (debugger);
565 static void
566 debugger_execute_command (Debugger *debugger, const gchar *command)
568 gchar *cmd;
570 DEBUG_PRINT ("In function: debugger_execute_command(%s) %d\n",command, debugger->priv->debugger_is_busy);
571 debugger->priv->debugger_is_busy++;
572 debugger->priv->command_output_sent = FALSE;
573 cmd = g_strconcat (command, "\n", NULL);
574 debugger_log_command (debugger, cmd);
575 anjuta_launcher_send_stdin (debugger->priv->launcher, cmd);
576 g_free (cmd);
579 static void
580 debugger_queue_execute_command (Debugger *debugger)
582 DEBUG_PRINT ("In function: debugger_queue_execute_command()");
584 debugger_clear_buffers (debugger);
585 if (debugger_queue_set_next_command (debugger))
586 // if (strlen (debugger->priv->current_cmd.cmd))
587 debugger_execute_command (debugger, debugger->priv->current_cmd.cmd);
590 static void
591 debugger_load_executable_finish (Debugger *debugger, const GDBMIValue *mi_results,
592 const GList *cli_results, GError *error)
594 DEBUG_PRINT ("Program loaded");
595 debugger->priv->prog_is_loaded = TRUE;
597 g_signal_emit_by_name (debugger->priv->instance, "program-loaded");
600 void
601 debugger_load_executable (Debugger *debugger, const gchar *prog)
603 gchar *command, *dir, *msg;
605 g_return_if_fail (IS_DEBUGGER (debugger));
606 g_return_if_fail (prog != NULL);
608 DEBUG_PRINT ("In function: debugger_load_executable(%s)", prog);
610 if (debugger->priv->output_callback)
612 msg = g_strconcat (_("Loading Executable: "), prog, "\n", NULL);
613 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT, msg,
614 debugger->priv->output_user_data);
615 g_free (msg);
618 command = g_strconcat ("-file-exec-and-symbols ", prog, NULL);
619 dir = g_path_get_dirname (prog);
620 /* TODO
621 anjuta_set_execution_dir(dir);
623 g_free (dir);
624 debugger_queue_command (debugger, command, FALSE, FALSE, debugger_load_executable_finish, NULL, NULL);
625 g_free (command);
626 debugger->priv->starting = TRUE;
627 debugger->priv->terminating = FALSE;
630 void
631 debugger_load_core (Debugger *debugger, const gchar *core)
633 gchar *command, *dir, *msg;
635 g_return_if_fail (IS_DEBUGGER (debugger));
636 g_return_if_fail (core != NULL);
638 DEBUG_PRINT ("In function: debugger_load_core(%s)", core);
640 if (debugger->priv->output_callback)
642 msg = g_strconcat (_("Loading Core: "), core, "\n", NULL);
643 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT, msg,
644 debugger->priv->output_user_data);
645 g_free (msg);
648 command = g_strconcat ("core ", core, NULL);
649 dir = g_path_get_dirname (core);
650 debugger->priv->search_dirs =
651 g_list_prepend (debugger->priv->search_dirs, dir);
652 debugger_queue_command (debugger, command, FALSE, FALSE, NULL, NULL, NULL);
653 g_free (command);
656 gboolean
657 debugger_start (Debugger *debugger, const GList *search_dirs,
658 const gchar *prog, gboolean is_libtool_prog,
659 gboolean terminal)
661 gchar *command_str, *dir, *tmp, *text, *msg;
662 gchar *exec_dir;
663 gboolean ret;
664 const GList *node;
665 AnjutaLauncher *launcher;
666 GList *dir_list = NULL;
667 gchar *term = NULL;
669 DEBUG_PRINT ("In function: debugger_start(%s) libtool %d", prog == NULL ? "(null)" : prog, is_libtool_prog);
671 /* Without a terminal, the output of the debugged program
672 * are lost */
673 if (terminal)
675 term = debugger_start_terminal (debugger);
676 if (term)
678 tmp = g_strconcat (" -tty=", term, NULL);
679 g_free(term);
680 term = tmp;
684 if (anjuta_util_prog_is_installed ("gdb", TRUE) == FALSE)
685 return FALSE;
687 debugger_queue_clear (debugger);
689 tmp = g_strconcat (PACKAGE_DATA_DIR, "/", "gdb.init", NULL);
690 if (g_file_test (tmp, G_FILE_TEST_IS_REGULAR) == FALSE)
692 anjuta_util_dialog_error (debugger->priv->parent_win,
693 _("Unable to find: %s.\n"
694 "Unable to initialize debugger.\n"
695 "Make sure Anjuta is installed correctly."),
696 tmp);
697 g_free (tmp);
698 return FALSE;
700 g_free (tmp);
702 /* Prepare source search directories */
703 exec_dir = NULL;
704 if (prog)
705 exec_dir = g_path_get_dirname (prog);
707 if (exec_dir)
709 dir = g_strconcat (" -directory=", exec_dir, NULL);
710 dir_list = g_list_prepend (dir_list, exec_dir);
712 else
714 dir = g_strdup (" ");
717 node = search_dirs;
718 while (node)
720 text = node->data;
721 if (strncmp (text, "file://", 7) == 0)
723 text += 7;
725 else
727 g_warning ("Debugger source search uri '%s' is not a local uri", text);
730 if (text[0] == '/')
732 tmp = g_strconcat (dir, " -directory=", text, NULL);
733 g_free (dir);
734 dir = tmp;
736 dir_list = g_list_prepend (dir_list, g_strdup (text));
738 else
740 g_warning ("Debugger source search dir '%s' is not absolute",
741 text);
743 node = g_list_next (node);
746 /* Now save the dir list. Order is automatically revesed */
747 node = dir_list;
748 while (node)
750 debugger->priv->search_dirs =
751 g_list_prepend (debugger->priv->search_dirs, node->data);
752 node = g_list_next (node);
754 g_list_free (dir_list);
756 if (prog && strlen(prog) > 0)
758 if (exec_dir)
759 chdir (exec_dir);
760 if (is_libtool_prog == FALSE)
762 command_str = g_strdup_printf (GDB_PATH " -f -n -i=mi2 %s %s "
763 "-x %s/gdb.init %s", dir, term == NULL ? "" : term,
764 PACKAGE_DATA_DIR, prog);
766 else
768 command_str = g_strdup_printf ("libtool --mode=execute " GDB_PATH
769 " -f -n -i=mi2 %s %s "
770 "-x %s/gdb.init %s", dir, term == NULL ? "" : term,
771 PACKAGE_DATA_DIR, prog);
774 else
776 if (is_libtool_prog == FALSE)
778 command_str = g_strdup_printf (GDB_PATH " -f -n -i=mi2 %s %s "
779 "-x %s/gdb.init ", term == NULL ? "" : term,
780 dir, PACKAGE_DATA_DIR);
782 else
784 command_str = g_strdup_printf ("libtool --mode=execute " GDB_PATH
785 " -f -n -i=mi2 %s %s -x "
786 "%s/gdb.init ",
787 dir, term == NULL ? "" : term, PACKAGE_DATA_DIR);
790 g_free (dir);
791 g_free (term);
792 debugger->priv->starting = TRUE;
793 debugger->priv->terminating = FALSE;
794 debugger->priv->loading = prog != NULL ? TRUE : FALSE;
795 debugger->priv->debugger_is_busy = 1;
797 /* Prepare for launch. */
798 launcher = debugger->priv->launcher;
799 anjuta_launcher_set_terminate_on_exit (launcher, TRUE);
800 g_signal_connect (G_OBJECT (launcher), "child-exited",
801 G_CALLBACK (on_gdb_terminated), debugger);
802 ret = anjuta_launcher_execute (launcher, command_str,
803 on_gdb_output_arrived, debugger);
805 if (ret)
806 debugger->priv->prog_is_loaded = prog != NULL;
807 anjuta_launcher_set_encoding (launcher, "ISO-8859-1");
809 if (debugger->priv->output_callback != NULL)
811 if (ret == TRUE)
813 /* TODO anjuta_update_app_status (TRUE, _("Debugger")); */
814 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
815 _("Getting ready to start debugging "
816 "session...\n"),
817 debugger->priv->output_user_data);
819 if (prog)
821 msg = g_strconcat (_("Loading Executable: "), prog, "\n", NULL);
822 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
823 msg,
824 debugger->priv->output_user_data);
825 g_free (msg);
827 else
829 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
830 _("No executable specified.\n"),
831 debugger->priv->output_user_data);
832 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
833 _("Open an executable or attach "
834 "to a process to start "
835 "debugging.\n"),
836 debugger->priv->output_user_data);
839 else
841 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
842 _("There was an error whilst "
843 "launching the debugger.\n"),
844 debugger->priv->output_user_data);
845 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
846 _("Make sure 'gdb' is installed "
847 "on the system.\n"),
848 debugger->priv->output_user_data);
851 g_free (command_str);
853 return TRUE;
856 static void
857 gdb_stdout_line_arrived (Debugger *debugger, const gchar * chars)
859 gint i = 0;
861 while (chars[i])
863 if (chars[i] == '\n')
865 debugger_stdo_flush (debugger);
867 else
869 g_string_append_c (debugger->priv->stdo_line, chars[i]);
871 i++;
875 static void
876 gdb_stderr_line_arrived (Debugger *debugger, const gchar * chars)
878 gint i;
880 for (i = 0; i < strlen (chars); i++)
882 if (chars[i] == '\n')
883 debugger_stde_flush (debugger);
884 else
885 g_string_append_c (debugger->priv->stde_line, chars[i]);
889 static void
890 on_gdb_output_arrived (AnjutaLauncher *launcher,
891 AnjutaLauncherOutputType output_type,
892 const gchar *chars, gpointer data)
894 Debugger *debugger = DEBUGGER (data);
895 DEBUG_PRINT ("on gdb output arrived");
897 switch (output_type)
899 case ANJUTA_LAUNCHER_OUTPUT_STDERR:
900 gdb_stderr_line_arrived (debugger, chars);
901 break;
902 case ANJUTA_LAUNCHER_OUTPUT_STDOUT:
903 gdb_stdout_line_arrived (debugger, chars);
904 break;
905 default:
906 break;
910 static void
911 debugger_handle_post_execution (Debugger *debugger)
913 switch (debugger->priv->post_execution_flag)
915 case DEBUGGER_NONE:
916 break;
917 case DEBUGGER_EXIT:
918 DEBUG_PRINT ("debugger stop in handle post execution\n");
919 debugger_stop (debugger);
920 break;
921 case DEBUGGER_RERUN_PROGRAM:
922 DEBUG_PRINT ("debugger run in handle post execution\n");
923 debugger_run (debugger);
924 break;
925 default:
926 g_warning ("Execution should not reach here");
930 static void
931 debugger_process_frame (Debugger *debugger, const GDBMIValue *val)
933 const GDBMIValue *file, *line, *frame, *addr, *fullname;
934 const gchar *file_str, *fullname_str;
935 gint line_num;
936 guint addr_num;
938 if (!val)
939 return;
941 g_return_if_fail (val != NULL);
943 file = gdbmi_value_hash_lookup (val, "file");
944 line = gdbmi_value_hash_lookup (val, "line");
945 frame = gdbmi_value_hash_lookup (val, "frame");
946 addr = gdbmi_value_hash_lookup (val, "addr");
947 fullname = gdbmi_value_hash_lookup (val, "fullname");
949 if (file && line)
951 file_str = gdbmi_value_literal_get (file);
952 line_num = atoi(gdbmi_value_literal_get (line));
954 fullname_str = fullname ? gdbmi_value_literal_get (fullname) : NULL;
955 addr_num = addr ? strtoul (gdbmi_value_literal_get (addr), NULL, 0) : 0;
957 debugger_change_location (debugger, fullname_str ? fullname_str : file_str,
958 line_num, addr_num);
960 else if (frame)
962 file = gdbmi_value_hash_lookup (frame, "file");
963 line = gdbmi_value_hash_lookup (frame, "line");
964 fullname = gdbmi_value_hash_lookup (frame, "fullname");
966 fullname_str = fullname ? gdbmi_value_literal_get (fullname) : NULL;
968 addr_num = addr ? strtoul (gdbmi_value_literal_get (addr), NULL, 0) : 0;
969 file_str = file ? gdbmi_value_literal_get (file) : NULL;
970 line_num = line ? atoi(gdbmi_value_literal_get (line)) : 0;
971 debugger_change_location (debugger, fullname_str ? fullname_str : file_str,
972 line_num, addr_num);
976 static GError*
977 gdb_parse_error (Debugger *debugger, const GDBMIValue *mi_results)
979 const GDBMIValue *message;
980 const gchar *literal;
981 guint code = IANJUTA_DEBUGGER_UNKNOWN_ERROR;
983 message = gdbmi_value_hash_lookup (mi_results, "msg");
984 literal = gdbmi_value_literal_get (message);
986 if ((mi_results != NULL)
987 && ((message = gdbmi_value_hash_lookup (mi_results, "msg")) != NULL)
988 && ((literal = gdbmi_value_literal_get (message)) != NULL)
989 && (*literal != '\0'))
991 code = gdb_match_error (literal);
992 DEBUG_PRINT ("error code %d", code);
994 else
996 /* No error message */
997 literal = "Error without a message";
1000 return g_error_new_literal (IANJUTA_DEBUGGER_ERROR, code, literal);
1004 /* Parsing output
1005 *---------------------------------------------------------------------------*/
1007 static void
1008 debugger_parse_output (Debugger *debugger)
1010 gchar *line;
1012 line = debugger->priv->stdo_line->str;
1014 if (line[0] == '\032' && line[1] == '\032')
1016 gchar *filename;
1017 guint lineno;
1019 gdb_util_parse_error_line (&(line[2]), &filename, &lineno);
1020 if (filename)
1022 debugger_change_location (debugger, filename, lineno, NULL);
1023 g_free (filename);
1026 else
1028 gchar *proper_msg;
1029 gsize len;
1031 len = strlen (line);
1032 if (line[1] == '\"' && line [strlen(line) - 1] == '\"')
1034 line[1] = line[0];
1035 /* Reserve space for an additional carriage return */
1036 line[strlen(line) - 1] = ' ';
1037 proper_msg = g_strcompress (line + 1);
1038 len = strlen (proper_msg) - 1;
1039 proper_msg[len] = '\0';
1041 else
1043 /* Reserve space for an additional carriage return */
1044 proper_msg = g_strndup (line, len + 1);
1047 if (strcmp(proper_msg, "~Stopped due to shared library event\n") == 0)
1049 /* Recognize a solib event */
1050 debugger->priv->solib_event = TRUE;
1051 g_free (proper_msg);
1053 else if (debugger->priv->current_cmd.parser)
1055 /* Save GDB CLI output */
1056 debugger->priv->cli_lines = g_list_prepend (debugger->priv->cli_lines,
1057 proper_msg);
1059 else
1061 /* Discard CLI output */
1062 g_free (proper_msg);
1067 static void
1068 debugger_parse_stopped (Debugger *debugger)
1070 gchar *line = debugger->priv->stdo_line->str;
1073 if (!debugger->priv->solib_event)
1075 gboolean program_exited = FALSE;
1076 GDBMIValue *val;
1078 /* Check if program has exited */
1079 val = gdbmi_value_parse (line);
1080 if (val)
1082 const GDBMIValue *reason;
1083 const gchar *str = NULL;
1085 debugger_process_frame (debugger, val);
1087 reason = gdbmi_value_hash_lookup (val, "reason");
1088 if (reason)
1089 str = gdbmi_value_literal_get (reason);
1091 if (str && (strncmp (str, "exited", 6) == 0))
1093 program_exited = TRUE;
1096 if (debugger->priv->output_callback)
1098 if (str && strcmp (str, "exited-normally") == 0)
1100 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1101 _("Program exited normally\n"),
1102 debugger->priv->output_user_data);
1104 else if (str && strcmp (str, "exited") == 0)
1106 const GDBMIValue *errcode;
1107 const gchar *errcode_str;
1108 gchar *msg;
1110 errcode = gdbmi_value_hash_lookup (val, "exit-code");
1111 errcode_str = gdbmi_value_literal_get (errcode);
1112 msg = g_strdup_printf (_("Program exited with error code %s\n"),
1113 errcode_str);
1114 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1115 msg, debugger->priv->output_user_data);
1116 g_free (msg);
1118 else if (str && strcmp (str, "exited-signalled") == 0)
1120 const GDBMIValue *signal_name, *signal_meaning;
1121 const gchar *signal_str, *signal_meaning_str;
1122 gchar *msg;
1124 signal_name = gdbmi_value_hash_lookup (val, "signal-name");
1125 signal_str = gdbmi_value_literal_get (signal_name);
1126 signal_meaning = gdbmi_value_hash_lookup (val,
1127 "signal-meaning");
1128 signal_meaning_str = gdbmi_value_literal_get (signal_meaning);
1129 msg = g_strdup_printf (_("Program received signal %s (%s) and exited\n"),
1130 signal_str, signal_meaning_str);
1131 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1132 msg, debugger->priv->output_user_data);
1133 g_free (msg);
1135 else if (str && strcmp (str, "signal-received") == 0)
1137 const GDBMIValue *signal_name, *signal_meaning;
1138 const gchar *signal_str, *signal_meaning_str;
1139 gchar *msg;
1141 signal_name = gdbmi_value_hash_lookup (val, "signal-name");
1142 signal_str = gdbmi_value_literal_get (signal_name);
1143 signal_meaning = gdbmi_value_hash_lookup (val,
1144 "signal-meaning");
1145 signal_meaning_str = gdbmi_value_literal_get (signal_meaning);
1147 msg = g_strdup_printf (_("Program received signal %s (%s)\n"),
1148 signal_str, signal_meaning_str);
1149 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1150 msg, debugger->priv->output_user_data);
1151 g_free (msg);
1153 else if (str && strcmp (str, "breakpoint-hit") == 0)
1155 const GDBMIValue *bkptno;
1156 const gchar *bkptno_str;
1157 gchar *msg;
1159 bkptno = gdbmi_value_hash_lookup (val, "bkptno");
1160 bkptno_str = gdbmi_value_literal_get (bkptno);
1162 msg = g_strdup_printf (_("Breakpoint number %s hit\n"),
1163 bkptno_str);
1164 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1165 msg, debugger->priv->output_user_data);
1166 g_free (msg);
1168 else if (str && strcmp (str, "function-finished") == 0)
1170 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1171 _("Function finished\n"),
1172 debugger->priv->output_user_data);
1174 else if (str && strcmp (str, "end-stepping-range") == 0)
1176 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1177 _("Stepping finished\n"),
1178 debugger->priv->output_user_data);
1180 else if (str && strcmp (str, "location-reached") == 0)
1182 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1183 _("Location reached\n"),
1184 debugger->priv->output_user_data);
1189 if (program_exited)
1191 debugger->priv->prog_is_running = FALSE;
1192 debugger->priv->prog_is_attached = FALSE;
1193 DEBUG_PRINT ("stop terminal in parse stopped");
1194 debugger_stop_terminal (debugger);
1195 // g_signal_emit_by_name (debugger->priv->instance, "program-exited");
1196 debugger_handle_post_execution (debugger);
1197 debugger->priv->exiting = TRUE;
1199 else
1201 // g_signal_emit_by_name (debugger->priv->instance, "program-stopped");
1202 debugger->priv->stopping = TRUE;
1205 debugger->priv->cli_lines = g_list_reverse (debugger->priv->cli_lines);
1206 if (debugger->priv->current_cmd.cmd[0] != '\0' &&
1207 debugger->priv->current_cmd.parser != NULL)
1209 debugger->priv->current_cmd.parser (debugger, val,
1210 debugger->priv->cli_lines, FALSE);
1211 debugger->priv->command_output_sent = TRUE;
1212 DEBUG_PRINT ("In function: Sending output...");
1215 if (val)
1216 gdbmi_value_free (val);
1220 static void
1221 debugger_parse_prompt (Debugger *debugger)
1223 /* If the parser has not yet been called, call it now. */
1224 if (debugger->priv->command_output_sent == FALSE &&
1225 debugger->priv->current_cmd.parser)
1227 debugger->priv->current_cmd.parser (debugger, NULL,
1228 debugger->priv->cli_lines, FALSE);
1229 debugger->priv->command_output_sent = TRUE;
1232 debugger->priv->debugger_is_busy--;
1234 if (!debugger->priv->debugger_is_busy &&
1235 g_list_length (debugger->priv->cmd_queqe) >= 1)
1237 debugger_queue_execute_command (debugger); /* Next command. Go. */
1239 debugger_emit_status (debugger);
1241 #if 0
1242 if (debugger->priv->skip_next_prompt)
1244 // Gdb is running in synchronous mode
1245 // new command are accepted only after executing
1246 // the current one.
1247 debugger->priv->skip_next_prompt = FALSE;
1249 else
1251 if (debugger->priv->starting)
1253 debugger->priv->starting = FALSE;
1254 /* Debugger has just started */
1255 if (debugger->priv->output_callback)
1257 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1258 _("Debugger is ready.\n"),
1259 debugger->priv->output_user_data);
1261 g_signal_emit_by_name (debugger->priv->instance, "debugger-started");
1263 else
1265 debugger->priv->debugger_is_busy--;
1267 if (strcmp (debugger->priv->current_cmd.cmd, "-exec-run") == 0 &&
1268 debugger->priv->prog_is_running == FALSE)
1270 /* Program has failed to run */
1271 debugger_stop_terminal (debugger);
1274 /* If the parser has not yet been called, call it now. */
1275 if (debugger->priv->command_output_sent == FALSE &&
1276 debugger->priv->current_cmd.parser)
1278 debugger->priv->current_cmd.parser (debugger, NULL,
1279 debugger->priv->cli_lines,
1280 debugger->priv->current_cmd.callback,
1281 debugger->priv->current_cmd.user_data);
1282 debugger->priv->command_output_sent = TRUE;
1286 if (!debugger->priv->debugger_is_busy &&
1287 g_list_length (debugger->priv->cmd_queqe) >= 1)
1289 static int ctag = 0;
1290 int tag = ctag++;
1291 debugger_queue_execute_command (debugger); /* Next command. Go. */
1293 debugger_emit_status (debugger);
1294 // return;
1296 #endif
1299 static void
1300 debugger_stdo_flush (Debugger *debugger)
1302 gchar *line;
1304 line = debugger->priv->stdo_line->str;
1306 DEBUG_PRINT ("Log: %s\n", line);
1307 debugger_log_output (debugger, line);
1308 if (strlen (line) == 0)
1310 return;
1312 if (strncasecmp (line, "^error", 6) == 0)
1314 /* GDB reported error */
1315 if (debugger->priv->current_cmd.suppress_error == FALSE)
1317 GDBMIValue *val = gdbmi_value_parse (line);
1318 GError *error;
1320 error = gdb_parse_error (debugger, val);
1322 if (debugger->priv->current_cmd.parser != NULL)
1324 debugger->priv->current_cmd.parser (debugger, val, debugger->priv->cli_lines, error);
1325 debugger->priv->command_output_sent = TRUE; }
1326 else
1328 anjuta_util_dialog_error (debugger->priv->parent_win,
1329 "%s",
1330 error->message);
1332 g_error_free (error);
1333 gdbmi_value_free (val);
1336 else if (strncasecmp(line, "^running", 8) == 0)
1338 /* Program started running */
1339 debugger->priv->prog_is_running = TRUE;
1340 /* debugger->priv->skip_next_prompt = TRUE; Replaced by debugger_is_busy++ */
1341 debugger->priv->debugger_is_busy++;
1342 g_signal_emit_by_name (debugger->priv->instance, "program-running");
1344 else if (strncasecmp (line, "*stopped", 8) == 0)
1346 /* Process has stopped */
1347 debugger_parse_stopped (debugger);
1349 else if (strncasecmp (line, "^done", 5) == 0)
1351 if ((debugger->priv->current_cmd.keep_result) || (debugger->priv->stdo_acc->len != 0))
1353 /* Keep result for next command */
1355 if (debugger->priv->stdo_acc->len == 0)
1357 g_string_append (debugger->priv->stdo_acc, line);
1359 else
1361 line = strchr (line, ',');
1362 if (line != NULL)
1364 g_string_append (debugger->priv->stdo_acc, line);
1367 line = debugger->priv->stdo_acc->str;
1370 if (!debugger->priv->current_cmd.keep_result)
1372 /* GDB command has reported output */
1373 GDBMIValue *val = gdbmi_value_parse (line);
1375 debugger->priv->cli_lines = g_list_reverse (debugger->priv->cli_lines);
1376 if (debugger->priv->current_cmd.cmd[0] != '\0' &&
1377 debugger->priv->current_cmd.parser != NULL)
1379 debugger->priv->current_cmd.parser (debugger, val,
1380 debugger->priv->cli_lines, FALSE);
1381 debugger->priv->command_output_sent = TRUE;
1382 DEBUG_PRINT ("In function: Sending output...");
1384 else /* if (val) */
1386 /*g_signal_emit_by_name (debugger, "results-arrived",
1387 debugger->priv->current_cmd.cmd, val);*/
1390 if (val)
1392 debugger_process_frame (debugger, val);
1393 gdbmi_value_free (val);
1397 if (!debugger->priv->current_cmd.keep_result)
1399 g_string_assign (debugger->priv->stdo_acc, "");
1402 else if (strncasecmp (line, GDB_PROMPT, strlen (GDB_PROMPT)) == 0)
1404 debugger_parse_prompt (debugger);
1406 else
1408 debugger_parse_output (debugger);
1411 /* Clear the line buffer */
1412 g_string_assign (debugger->priv->stdo_line, "");
1415 void
1416 debugger_stde_flush (Debugger *debugger)
1418 if ((debugger->priv->output_callback) && (strlen (debugger->priv->stde_line->str) > 0))
1420 debugger->priv->output_callback (IANJUTA_DEBUGGER_ERROR_OUTPUT,
1421 debugger->priv->stde_line->str,
1422 debugger->priv->output_user_data);
1424 /* Clear the line buffer */
1425 g_string_assign (debugger->priv->stde_line, "");
1428 static void
1429 on_gdb_terminated (AnjutaLauncher *launcher,
1430 gint child_pid, gint status, gulong t,
1431 gpointer data)
1433 Debugger *debugger = DEBUGGER (data);
1435 g_signal_handlers_disconnect_by_func (G_OBJECT (launcher),
1436 G_CALLBACK (on_gdb_terminated),
1437 debugger);
1439 DEBUG_PRINT ("In function: gdb_terminated()");
1441 /* Clear the command queue & Buffer */
1442 debugger_clear_buffers (debugger);
1443 //debugger_stop_terminal (debugger);
1445 /* Good Bye message */
1446 if (!debugger->priv->terminating)
1448 anjuta_util_dialog_error (debugger->priv->parent_win,
1449 _("gdb terminated unexpectedly with status %d\n"), status);
1451 debugger_stop_terminal (debugger);
1452 debugger->priv->prog_is_running = FALSE;
1453 debugger->priv->term_is_running = FALSE;
1454 debugger->priv->term_pid = -1;
1455 debugger->priv->debugger_is_busy = 0;
1456 debugger->priv->skip_next_prompt = FALSE;
1457 debugger->priv->terminating = FALSE;
1458 if (debugger->priv->instance)
1459 g_signal_emit_by_name (debugger->priv->instance, "debugger-stopped");
1462 static void
1463 debugger_stop_real (Debugger *debugger)
1465 DEBUG_PRINT ("In function: debugger_stop_real()");
1467 /* if program is attached - detach from it before quiting */
1468 if (debugger->priv->prog_is_attached == TRUE)
1469 debugger_queue_command (debugger, "detach", FALSE, FALSE, NULL, NULL, NULL);
1471 debugger_stop_terminal (debugger);
1472 debugger->priv->terminating = TRUE;
1473 debugger_queue_command (debugger, "-gdb-exit", FALSE, FALSE, NULL, NULL, NULL);
1476 gboolean
1477 debugger_stop (Debugger *debugger)
1479 gboolean ret = TRUE;
1481 if (debugger->priv->prog_is_running == TRUE)
1483 GtkWidget *dialog;
1484 gchar *mesg;
1486 if (debugger->priv->prog_is_attached == TRUE)
1487 mesg = _("The program is attached.\n"
1488 "Do you still want to stop the debugger?");
1489 else
1490 mesg = _("The program is running.\n"
1491 "Do you still want to stop the debugger?");
1492 dialog = gtk_message_dialog_new (debugger->priv->parent_win,
1493 GTK_DIALOG_DESTROY_WITH_PARENT,
1494 GTK_MESSAGE_QUESTION,
1495 GTK_BUTTONS_NONE, mesg);
1496 gtk_dialog_add_buttons (GTK_DIALOG (dialog),
1497 GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
1498 GTK_STOCK_STOP, GTK_RESPONSE_YES,
1499 NULL);
1500 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_YES)
1501 debugger_stop_real (debugger);
1502 else
1503 ret = FALSE;
1504 gtk_widget_destroy (dialog);
1506 else
1507 debugger_stop_real (debugger);
1508 return ret;
1511 gboolean
1512 debugger_abort (Debugger *debugger)
1514 DEBUG_PRINT ("In function: debugger_abort()");
1516 /* Stop terminal */
1517 debugger_stop_terminal (debugger);
1518 debugger->priv->terminating = TRUE;
1520 /* Stop gdb */
1521 anjuta_launcher_reset (debugger->priv->launcher);
1523 /* Stop inferior */
1524 if ((debugger->priv->prog_is_attached == FALSE) && (debugger->priv->inferior_pid != 0))
1526 kill (debugger->priv->inferior_pid, SIGTERM);
1529 /* Free memory */
1530 debugger_queue_clear (debugger);
1531 g_list_foreach (debugger->priv->search_dirs, (GFunc)g_free, NULL);
1532 g_list_free (debugger->priv->search_dirs);
1533 debugger->priv->search_dirs = NULL;
1535 /* Disconnect */
1536 if (debugger->priv->instance != NULL)
1538 /* Signal end of debugger */
1539 g_signal_emit_by_name (debugger->priv->instance, "debugger-stopped");
1541 g_object_remove_weak_pointer (debugger->priv->instance, (gpointer *)&debugger->priv->instance);
1542 debugger->priv->instance = NULL;
1545 return TRUE;
1548 static void
1549 on_debugger_terminal_terminated (int status, gpointer user_data)
1551 Debugger *debugger = DEBUGGER (user_data);
1553 DEBUG_PRINT ("In function: on_debugger_terminal_terminated()");
1555 debugger->priv->term_is_running = FALSE;
1556 debugger->priv->term_pid = -1;
1558 if (debugger->priv->prog_is_running)
1559 debugger_stop_program (debugger);
1562 gchar*
1563 debugger_get_source_path (Debugger *debugger, const gchar *file)
1565 GList *node;
1566 gchar *path = NULL;
1568 if (g_path_is_absolute (file))
1569 return g_strdup (file);
1571 node = debugger->priv->search_dirs;
1572 while (node)
1574 path = g_build_filename (node->data, file, NULL);
1575 if (g_file_test (path, G_FILE_TEST_EXISTS))
1576 break;
1577 g_free (path);
1578 path = NULL;
1579 node = g_list_next (node);
1582 if (path == NULL)
1584 /* The file could be found nowhere. Use current directory */
1585 gchar *cwd;
1586 cwd = g_get_current_dir ();
1587 path = g_build_filename (cwd, file, NULL);
1588 g_free (cwd);
1590 return path;
1593 void
1594 debugger_set_output_callback (Debugger *debugger, IAnjutaDebuggerOutputCallback callback, gpointer user_data)
1596 debugger->priv->output_callback = callback;
1597 debugger->priv->output_user_data = user_data;
1600 void
1601 debugger_change_location (Debugger *debugger, const gchar *file,
1602 gint line, const gchar *address)
1604 gchar *src_path;
1606 if ((file != NULL) && (*file != G_DIR_SEPARATOR))
1608 src_path = debugger_get_source_path (debugger, file);
1609 g_signal_emit_by_name (debugger->priv->instance, "location-changed", src_path, line, address);
1610 g_free (src_path);
1612 else
1614 g_signal_emit_by_name (debugger->priv->instance, "location-changed", file, line, address);
1618 static gchar *
1619 debugger_start_terminal (Debugger *debugger)
1621 gchar *file, *cmd, *encoded_cmd;
1622 gchar dev_name[100];
1623 gint count, idx;
1624 FILE *fp;
1625 pid_t pid;
1626 gchar *argv[20];
1627 GList *args, *node;
1629 DEBUG_PRINT ("In function: debugger_start_terminal()");
1631 if (debugger->priv->prog_is_running == TRUE)
1632 return NULL;
1633 if (debugger->priv->term_is_running == TRUE)
1634 return NULL;
1635 if (anjuta_util_prog_is_installed ("anjuta_launcher", TRUE) == FALSE)
1636 return NULL;
1638 count = 0;
1639 file = anjuta_util_get_a_tmp_file ();
1641 while (g_file_test (file, G_FILE_TEST_IS_REGULAR))
1643 g_free (file);
1644 file = anjuta_util_get_a_tmp_file ();
1645 if (count++ > 100)
1646 goto error;
1648 if (mkfifo (file, 0664) < 0)
1649 goto error;
1651 debugger->priv->term_is_running = TRUE;
1652 cmd = g_strconcat ("anjuta_launcher --__debug_terminal ", file, NULL);
1653 encoded_cmd = anjuta_util_escape_quotes (cmd);
1654 g_free (cmd);
1656 cmd = g_strdup ("gnome-terminal -e \"%s\"");
1657 if (cmd)
1659 gchar *final_cmd;
1660 if (strstr (cmd, "%s") != NULL)
1662 final_cmd = g_strdup_printf (cmd, encoded_cmd);
1664 else
1666 final_cmd = g_strconcat (cmd, " ", encoded_cmd, NULL);
1668 g_free (cmd);
1669 cmd = final_cmd;
1671 g_free (encoded_cmd);
1673 args = anjuta_util_parse_args_from_string (cmd);
1675 /* Fix gnome-terminal1 and gnome-terminal2 confusion */
1676 if (g_list_length(args) > 0 &&
1677 strcmp ((gchar*)args->data, "gnome-terminal") == 0)
1679 GList* node;
1680 node = g_list_next(args);
1681 /* Terminal command is gnome-terminal */
1682 if (debugger->priv->gnome_terminal_type == 1) {
1683 /* Remove any --disable-factory option, if present */
1684 while (node) {
1685 if (strcmp ((gchar*)args->data, "--disable-factory") == 0) {
1686 g_free (node->data);
1687 args = g_list_remove (args, node);
1688 break;
1690 node = g_list_next (node);
1692 } else if (debugger->priv->gnome_terminal_type == 2) {
1693 /* Add --disable-factory option, if not present */
1694 gboolean found = 0;
1695 while (node) {
1696 if (strcmp ((gchar*)args->data, "--disable-factory") == 0) {
1697 found = 1;
1698 break;
1700 node = g_list_next (node);
1702 if (!found) {
1703 gchar* arg = g_strdup ("--disable-factory");
1704 args = g_list_insert (args, arg, 1);
1709 DEBUG_PRINT ("Terminal command: [%s]\n", cmd);
1711 g_free (cmd);
1713 /* Rearrange the args to be passed to fork */
1714 node = args;
1715 idx = 0;
1716 while (node) {
1717 argv[idx++] = (gchar*)node->data;
1718 node = g_list_next (node);
1721 #ifdef DEBUG
1723 int i;
1724 GList *node = args;
1725 i=0;
1726 while (node) {
1727 g_print ("%d) [%s]\n", i++, (char*)node->data);
1728 node = g_list_next (node);
1731 #endif
1733 argv[idx] = NULL;
1734 if (idx < 1)
1735 goto error;
1737 /* With this command SIGCHILD is not emitted */
1738 /* pid = gnome_execute_async (app->dirs->home, 6, av); */
1739 /* so using fork instead */
1740 if ((pid = fork ()) == 0)
1742 execvp (argv[0], argv);
1743 g_error (_("Cannot execute gnome-terminal"));
1745 g_list_foreach (args, (GFunc)g_free, NULL);
1746 g_list_free (args);
1748 debugger->priv->term_pid = pid;
1749 if (pid < 1)
1750 goto error;
1751 DEBUG_PRINT ("terminal pid = %d\n", pid);
1752 anjuta_children_register (pid, on_debugger_terminal_terminated, debugger);
1755 * May be the terminal is not started properly.
1756 * Callback will reset this flag
1758 if (debugger->priv->term_is_running == FALSE)
1759 goto error;
1762 * Warning: call to fopen() may be blocked if the terminal is not properly started.
1763 * I don't know how to handle this. May be opening as non-blocking will solve this .
1765 fp = fopen (file, "r"); /* Ok, take the risk. */
1767 if (!fp)
1768 goto error;
1769 if (fscanf (fp, "%s", dev_name) < 1)
1770 goto error;
1771 fclose (fp);
1772 remove (file);
1773 if (strcmp (dev_name, "__ERROR__") == 0)
1774 goto error;
1775 g_free (file);
1776 return g_strdup (dev_name);
1777 error:
1778 anjuta_util_dialog_error (debugger->priv->parent_win,
1779 _("Cannot start terminal for debugging."));
1780 debugger_stop_terminal (debugger);
1781 remove (file);
1782 g_free (file);
1783 return NULL;
1786 static void
1787 debugger_stop_terminal (Debugger *debugger)
1789 DEBUG_PRINT ("In function: debugger_stop_terminal()");
1791 if (debugger->priv->term_is_running == FALSE)
1792 return;
1793 if (debugger->priv->term_pid > 0) {
1794 anjuta_children_unregister (debugger->priv->term_pid);
1795 if (kill (debugger->priv->term_pid, SIGTERM) == -1) {
1796 switch (errno) {
1797 case EINVAL:
1798 g_warning ("Invalid signal applied to kill");
1799 break;
1800 case ESRCH:
1801 g_warning ("No such pid [%d] or process has already died",
1802 debugger->priv->term_pid);
1803 break;
1804 case EPERM:
1805 g_warning ("No permission to send signal to the process");
1806 break;
1807 default:
1808 g_warning ("Unknow error while kill");
1812 debugger->priv->term_pid = -1;
1813 debugger->priv->term_is_running = FALSE;
1816 static void
1817 debugger_info_program_finish (Debugger *debugger, const GDBMIValue *mi_results,
1818 const GList *cli_results, GError *error)
1820 DEBUG_PRINT ("In function: debugger_info_program()");
1822 /* Hack: find message string giving inferior pid */
1823 while (cli_results != NULL)
1825 gchar* child_proc;
1827 child_proc = strstr(cli_results->data, " child process ");
1828 if (child_proc != NULL)
1830 debugger->priv->inferior_pid = strtoul (child_proc + 15, NULL, 10);
1831 break;
1833 cli_results = g_list_next (cli_results);
1837 void
1838 debugger_start_program (Debugger *debugger, const gchar* args)
1840 gchar *cmd;
1842 DEBUG_PRINT ("In function: debugger_start_program()");
1844 g_return_if_fail (IS_DEBUGGER (debugger));
1845 g_return_if_fail (debugger->priv->prog_is_running == FALSE);
1847 debugger->priv->inferior_pid = 0;
1848 debugger_queue_command (debugger, "-break-insert -t main", FALSE, FALSE, NULL, NULL, NULL);
1849 if (args && (*args))
1851 cmd = g_strconcat ("-exec-arguments ", args, NULL);
1852 debugger_queue_command (debugger, cmd, FALSE, FALSE, NULL, NULL, NULL);
1853 g_free (cmd);
1856 debugger_queue_command (debugger, "-exec-run", FALSE, FALSE, NULL, NULL, NULL);
1858 /* Get pid of program on next stop */
1859 debugger_queue_command (debugger, "info program", FALSE, FALSE, debugger_info_program_finish, NULL, NULL);
1860 debugger->priv->post_execution_flag = DEBUGGER_NONE;
1863 static void
1864 debugger_attach_process_finish (Debugger *debugger, const GDBMIValue *mi_results,
1865 const GList *cli_results, GError *error)
1867 DEBUG_PRINT ("Program attach finished");
1868 if (debugger->priv->output_callback)
1870 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1871 _("Program attached\n"),
1872 debugger->priv->output_user_data);
1874 debugger->priv->prog_is_attached = TRUE;
1875 debugger->priv->prog_is_running = TRUE;
1876 g_signal_emit_by_name (debugger->priv->instance, "program-exited");
1879 static void
1880 debugger_attach_process_real (Debugger *debugger, pid_t pid)
1882 gchar *buff;
1884 DEBUG_PRINT ("In function: debugger_attach_process_real()");
1886 if (debugger->priv->output_callback)
1888 buff = g_strdup_printf (_("Attaching to process: %d...\n"), pid);
1889 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1890 buff, debugger->priv->output_user_data);
1891 g_free (buff);
1894 debugger->priv->inferior_pid = pid;
1895 buff = g_strdup_printf ("attach %d", pid);
1896 debugger_queue_command (debugger, buff, FALSE, FALSE,
1897 debugger_attach_process_finish, NULL, NULL);
1898 g_free (buff);
1901 void
1902 debugger_attach_process (Debugger *debugger, pid_t pid)
1904 DEBUG_PRINT ("In function: debugger_attach_process()");
1906 g_return_if_fail (IS_DEBUGGER (debugger));
1908 if (debugger->priv->prog_is_running == TRUE)
1910 // TODO: Dialog to be made HIG compliant.
1911 gchar *mesg;
1912 GtkWidget *dialog;
1914 mesg = _("A process is already running.\n"
1915 "Would you like to terminate it and attach the new process?"),
1916 dialog = gtk_message_dialog_new (debugger->priv->parent_win,
1917 GTK_DIALOG_DESTROY_WITH_PARENT,
1918 GTK_MESSAGE_QUESTION,
1919 GTK_BUTTONS_YES_NO, mesg);
1920 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_YES)
1922 debugger_stop_program (debugger);
1923 debugger_attach_process_real (debugger, pid);
1925 gtk_widget_destroy (dialog);
1927 else if (getpid () == pid ||
1928 anjuta_launcher_get_child_pid (debugger->priv->launcher) == pid)
1930 anjuta_util_dialog_error (debugger->priv->parent_win,
1931 _("Anjuta is unable to attach to itself."));
1932 return;
1934 else
1935 debugger_attach_process_real (debugger, pid);
1938 void
1939 debugger_restart_program (Debugger *debugger)
1941 DEBUG_PRINT ("In function: debugger_restart_program()");
1943 g_return_if_fail (debugger->priv->prog_is_attached == FALSE);
1946 debugger->priv->post_execution_flag = DEBUGGER_RERUN_PROGRAM;
1947 debugger_stop_program (debugger);
1949 debugger_put_cmd_in_queqe ("tbreak main", DB_CMD_NONE, NULL, NULL);
1950 debugger_put_cmd_in_queqe ("run >/dev/null 2>/dev/null", DB_CMD_ALL,
1951 NULL, NULL);
1952 debugger_put_cmd_in_queqe ("info program", DB_CMD_NONE,
1953 on_debugger_update_prog_status, NULL);
1954 debugger_put_cmd_in_queqe ("continue", DB_CMD_NONE, NULL, NULL);
1955 debugger_execute_cmd_in_queqe ();
1959 void
1960 debugger_stop_program (Debugger *debugger)
1962 DEBUG_PRINT ("In function: debugger_stop_program()");
1964 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
1966 if (debugger->priv->prog_is_attached == TRUE)
1967 debugger_queue_command (debugger, "detach", FALSE, FALSE, NULL, NULL, NULL);
1968 else
1970 /* FIXME: Why doesn't -exec-abort work??? */
1971 /* debugger_queue_command (debugger, "-exec-abort", NULL, NULL); */
1972 debugger_queue_command (debugger, "kill", FALSE, FALSE, NULL, NULL, NULL);
1973 debugger->priv->prog_is_running = FALSE;
1974 debugger->priv->prog_is_attached = FALSE;
1975 DEBUG_PRINT ("stop terminal in stop program");
1976 debugger_stop_terminal (debugger);
1977 g_signal_emit_by_name (debugger->priv->instance, "program-exited");
1978 if (debugger->priv->output_callback)
1980 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1981 _("Program terminated\n"),
1982 debugger->priv->output_user_data);
1984 debugger_handle_post_execution (debugger);
1988 static void
1989 debugger_detach_process_finish (Debugger *debugger, const GDBMIValue *mi_results,
1990 const GList *cli_results, GError *error)
1992 DEBUG_PRINT ("Program detach finished");
1993 if (debugger->priv->output_callback)
1995 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1996 _("Program detached\n"),
1997 debugger->priv->output_user_data);
1999 debugger->priv->prog_is_attached = FALSE;
2000 debugger->priv->prog_is_running = FALSE;
2001 g_signal_emit_by_name (debugger->priv->instance, "program-exited");
2004 void
2005 debugger_detach_process (Debugger *debugger)
2007 gchar *buff;
2009 DEBUG_PRINT ("In function: debugger_detach_process()");
2011 g_return_if_fail (debugger->priv->prog_is_attached == TRUE);
2013 if (debugger->priv->output_callback)
2015 buff = g_strdup_printf (_("Detaching the process...\n"));
2016 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
2017 buff, debugger->priv->output_user_data);
2018 g_free (buff);
2021 debugger_queue_command (debugger, "detach", FALSE, FALSE,
2022 debugger_detach_process_finish, NULL, NULL);
2023 debugger->priv->prog_is_attached = FALSE;
2026 void
2027 debugger_interrupt (Debugger *debugger)
2029 DEBUG_PRINT ("In function: debugger_interrupt()");
2031 g_return_if_fail (IS_DEBUGGER (debugger));
2032 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
2034 if (debugger->priv->output_callback)
2036 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
2037 _("Interrupting the process\n"),
2038 debugger->priv->output_user_data);
2041 if (debugger->priv->inferior_pid == 0)
2043 /* In case we do not have the inferior pid, send signal to gdb */
2044 anjuta_launcher_signal (debugger->priv->launcher, SIGINT);
2046 else
2048 /* Send signal directly to inferior */
2049 kill (debugger->priv->inferior_pid, SIGINT);
2051 //g_signal_emit_by_name (debugger->priv->instance, "program-running");
2054 void
2055 debugger_run (Debugger *debugger)
2057 DEBUG_PRINT ("In function: debugger_run()");
2059 g_return_if_fail (IS_DEBUGGER (debugger));
2060 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
2062 /* Program running - continue */
2063 debugger_queue_command (debugger, "-exec-continue", FALSE, FALSE, NULL, NULL, NULL);
2066 void
2067 debugger_step_in (Debugger *debugger)
2069 DEBUG_PRINT ("In function: debugger_step_in()");
2071 g_return_if_fail (IS_DEBUGGER (debugger));
2072 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
2074 debugger_queue_command (debugger, "-exec-step", FALSE, FALSE, NULL, NULL, NULL);
2077 void
2078 debugger_step_over (Debugger *debugger)
2080 DEBUG_PRINT ("In function: debugger_step_over()");
2082 g_return_if_fail (IS_DEBUGGER (debugger));
2083 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
2085 debugger_queue_command (debugger, "-exec-next", FALSE, FALSE, NULL, NULL, NULL);
2088 void
2089 debugger_step_out (Debugger *debugger)
2091 DEBUG_PRINT ("In function: debugger_step_out()");
2093 g_return_if_fail (IS_DEBUGGER (debugger));
2094 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
2096 debugger_queue_command (debugger, "-exec-finish", FALSE, FALSE, NULL, NULL, NULL);
2099 void
2100 debugger_run_to_location (Debugger *debugger, const gchar *loc)
2102 gchar *buff;
2104 DEBUG_PRINT ("In function: debugger_run_to_location()");
2106 g_return_if_fail (IS_DEBUGGER (debugger));
2107 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
2109 buff = g_strdup_printf ("-exec-until %s", loc);
2110 debugger_queue_command (debugger, buff, FALSE, FALSE, NULL, NULL, NULL);
2111 g_free (buff);
2114 void
2115 debugger_run_to_position (Debugger *debugger, const gchar *file, guint line)
2117 gchar *buff;
2119 DEBUG_PRINT ("In function: debugger_run_to_position()");
2121 g_return_if_fail (IS_DEBUGGER (debugger));
2122 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
2124 buff = g_strdup_printf ("-exec-until %s:%d", file, line);
2125 debugger_queue_command (debugger, buff, FALSE, FALSE, NULL, NULL, NULL);
2126 g_free (buff);
2129 void
2130 debugger_command (Debugger *debugger, const gchar *command,
2131 gboolean suppress_error, DebuggerParserFunc parser,
2132 gpointer user_data)
2134 if (strncasecmp (command, "-exec-run",
2135 strlen ("-exec-run")) == 0 ||
2136 strncasecmp (command, "run", strlen ("run")) == 0)
2138 /* FIXME: The user might have passed args to the command */
2139 debugger_run (debugger);
2141 else if (strncasecmp (command, "-exec-step",
2142 strlen ("-exec-step")) == 0 ||
2143 strncasecmp (command, "step", strlen ("step")) == 0)
2145 debugger_step_in (debugger);
2147 else if (strncasecmp (command, "-exec-next",
2148 strlen ("-exec-next")) == 0 ||
2149 strncasecmp (command, "next", strlen ("next")) == 0)
2151 debugger_step_over (debugger);
2153 else if (strncasecmp (command, "-exec-finish",
2154 strlen ("-exec-finish")) == 0 ||
2155 strncasecmp (command, "finish", strlen ("finish")) == 0)
2157 debugger_step_out (debugger);
2159 else if (strncasecmp (command, "-exec-continue",
2160 strlen ("-exec-continue")) == 0 ||
2161 strncasecmp (command, "continue", strlen ("continue")) == 0)
2163 debugger_run (debugger);
2165 else if (strncasecmp (command, "-exec-until",
2166 strlen ("-exec-until")) == 0 ||
2167 strncasecmp (command, "until", strlen ("until")) == 0)
2169 debugger_run_to_location (debugger, strchr (command, ' '));
2171 else if (strncasecmp (command, "-exec-abort",
2172 strlen ("-exec-abort")) == 0 ||
2173 strncasecmp (command, "kill", strlen ("kill")) == 0)
2175 debugger_stop_program (debugger);
2177 else if (strncasecmp (command, "-target-attach",
2178 strlen ("-target-attach")) == 0 ||
2179 strncasecmp (command, "attach", strlen ("attach")) == 0)
2181 pid_t pid = 0;
2182 gchar *pid_str = strchr (command, ' ');
2183 if (pid_str)
2184 pid = atoi (pid_str);
2185 debugger_attach_process (debugger, pid);
2187 else if (strncasecmp (command, "-target-detach",
2188 strlen ("-target-detach")) == 0 ||
2189 strncasecmp (command, "detach", strlen ("detach")) == 0)
2191 debugger_detach_process (debugger);
2193 else if (strncasecmp (command, "-file-exec-and-symbols",
2194 strlen ("-file-exec-and-symbols")) == 0 ||
2195 strncasecmp (command, "file", strlen ("file")) == 0)
2197 debugger_load_executable (debugger, strchr (command, ' '));
2199 else if (strncasecmp (command, "core", strlen ("core")) == 0)
2201 debugger_load_core (debugger, strchr (command, ' '));
2203 else
2205 debugger_queue_command (debugger, command, suppress_error, FALSE,
2206 parser, user_data, NULL);
2210 static void
2211 debugger_add_breakpoint_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2213 const GDBMIValue *brkpnt;
2214 const GDBMIValue *literal;
2215 const gchar* value;
2216 IAnjutaDebuggerBreakpoint bp;
2217 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2218 gpointer user_data = debugger->priv->current_cmd.user_data;
2220 memset (&bp, 0, sizeof (bp));
2222 bp.enable = IANJUTA_DEBUGGER_UNDEFINED;
2223 bp.keep = IANJUTA_DEBUGGER_UNDEFINED;
2224 if (mi_results == NULL)
2226 if (strncmp((const gchar *)cli_results->data,"&No source file named ", 22) == 0)
2228 /* Breakpoint cannot be set, dynamic library not loaded ? */
2230 /* Useful for enable that doesn't return anything */
2231 if (callback != NULL)
2232 callback (NULL, user_data, NULL);
2234 else
2236 brkpnt = gdbmi_value_hash_lookup (mi_results, "bkpt");
2238 literal = gdbmi_value_hash_lookup (brkpnt, "number");
2239 if (literal)
2241 value = gdbmi_value_literal_get (literal);
2242 bp.id = strtoul (value, NULL, 10);
2245 literal = gdbmi_value_hash_lookup (brkpnt, "file");
2246 if (literal)
2248 value = gdbmi_value_literal_get (literal);
2249 bp.file = (gchar *)value;
2252 literal = gdbmi_value_hash_lookup (brkpnt, "line");
2253 if (literal)
2255 value = gdbmi_value_literal_get (literal);
2256 bp.line = strtoul (value, NULL, 10);
2259 literal = gdbmi_value_hash_lookup (brkpnt, "type");
2260 if (literal)
2262 value = gdbmi_value_literal_get (literal);
2265 literal = gdbmi_value_hash_lookup (brkpnt, "disp");
2266 if (literal)
2268 value = gdbmi_value_literal_get (literal);
2269 if (strcmp (value, "keep") == 0)
2271 bp.keep = IANJUTA_DEBUGGER_YES;
2273 else if (strcmp (value, "nokeep") == 0)
2275 bp.keep = IANJUTA_DEBUGGER_NO;
2279 literal = gdbmi_value_hash_lookup (brkpnt, "enabled");
2280 if (literal)
2282 value = gdbmi_value_literal_get (literal);
2283 if (strcmp (value, "n") == 0)
2285 bp.enable = IANJUTA_DEBUGGER_NO;
2287 else if (strcmp (value, "y") == 0)
2289 bp.enable = IANJUTA_DEBUGGER_YES;
2293 literal = gdbmi_value_hash_lookup (brkpnt, "addr");
2294 if (literal)
2296 value = gdbmi_value_literal_get (literal);
2297 bp.address = strtoul (value, NULL, 16);
2300 literal = gdbmi_value_hash_lookup (brkpnt, "func");
2301 if (literal)
2303 value = gdbmi_value_literal_get (literal);
2304 bp.function = (gchar *)value;
2307 literal = gdbmi_value_hash_lookup (brkpnt, "times");
2308 if (literal)
2310 value = gdbmi_value_literal_get (literal);
2311 bp.times = strtoul (value, NULL, 10);
2313 else
2315 bp.times = G_MAXUINT32;
2317 literal = gdbmi_value_hash_lookup (brkpnt, "ignore");
2318 if (literal)
2320 value = gdbmi_value_literal_get (literal);
2321 bp.ignore = strtoul (value, NULL, 10);
2323 else
2325 bp.ignore = G_MAXUINT32;
2328 literal = gdbmi_value_hash_lookup (brkpnt, "cond");
2329 if (literal)
2331 value = gdbmi_value_literal_get (literal);
2334 /* Call callback in all case (useful for enable that doesn't return
2335 * anything */
2336 if (callback != NULL)
2337 callback (&bp, user_data, NULL);
2341 void
2342 debugger_add_breakpoint_at_line (Debugger *debugger, const gchar *file, guint line, IAnjutaDebuggerCallback callback, gpointer user_data)
2344 gchar *buff;
2346 DEBUG_PRINT ("In function: debugger_add_breakpoint()");
2348 g_return_if_fail (IS_DEBUGGER (debugger));
2350 buff = g_strdup_printf ("-break-insert %s:%u", file, line);
2351 debugger_queue_command (debugger, buff, FALSE, FALSE, debugger_add_breakpoint_finish, callback, user_data);
2352 g_free (buff);
2355 void
2356 debugger_add_breakpoint_at_function (Debugger *debugger, const gchar *file, const gchar *function, IAnjutaDebuggerCallback callback, gpointer user_data)
2358 gchar *buff;
2360 DEBUG_PRINT ("In function: debugger_add_breakpoint()");
2362 g_return_if_fail (IS_DEBUGGER (debugger));
2364 buff = g_strdup_printf ("-break-insert %s%s%s", file == NULL ? "" : file, file == NULL ? "" : ":" , function);
2365 debugger_queue_command (debugger, buff, FALSE, FALSE, debugger_add_breakpoint_finish, callback, user_data);
2366 g_free (buff);
2369 void
2370 debugger_add_breakpoint_at_address (Debugger *debugger, guint address, IAnjutaDebuggerCallback callback, gpointer user_data)
2372 gchar *buff;
2374 DEBUG_PRINT ("In function: debugger_add_breakpoint()");
2376 g_return_if_fail (IS_DEBUGGER (debugger));
2378 buff = g_strdup_printf ("-break-insert *0x%x", address);
2379 debugger_queue_command (debugger, buff, FALSE, FALSE, debugger_add_breakpoint_finish, callback, user_data);
2380 g_free (buff);
2383 void
2384 debugger_enable_breakpoint (Debugger *debugger, guint id, gboolean enable, IAnjutaDebuggerCallback callback, gpointer user_data)
2387 gchar *buff;
2389 DEBUG_PRINT ("In function: debugger_enable_breakpoint()");
2391 g_return_if_fail (IS_DEBUGGER (debugger));
2393 buff = g_strdup_printf (enable ? "-break-enable %d" : "-break-disable %d",id);
2394 debugger_queue_command (debugger, buff, FALSE, FALSE, debugger_add_breakpoint_finish, callback, user_data);
2395 g_free (buff);
2398 void
2399 debugger_ignore_breakpoint (Debugger *debugger, guint id, guint ignore, IAnjutaDebuggerCallback callback, gpointer user_data)
2401 gchar *buff;
2403 DEBUG_PRINT ("In function: debugger_ignore_breakpoint()");
2405 g_return_if_fail (IS_DEBUGGER (debugger));
2407 buff = g_strdup_printf ("-break-after %d %d", id, ignore);
2408 debugger_queue_command (debugger, buff, FALSE, FALSE, debugger_add_breakpoint_finish, callback, user_data);
2409 g_free (buff);
2412 void
2413 debugger_condition_breakpoint (Debugger *debugger, guint id, const gchar *condition, IAnjutaDebuggerCallback callback, gpointer user_data)
2415 gchar *buff;
2417 DEBUG_PRINT ("In function: debugger_ignore_breakpoint()");
2419 g_return_if_fail (IS_DEBUGGER (debugger));
2421 buff = g_strdup_printf ("-break-condition %d %s", id, condition);
2422 debugger_queue_command (debugger, buff, FALSE, FALSE, debugger_add_breakpoint_finish, callback, user_data);
2423 g_free (buff);
2426 static void
2427 debugger_remove_breakpoint_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2429 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2430 gpointer user_data = debugger->priv->current_cmd.user_data;
2432 if (callback != NULL)
2433 callback (NULL, user_data, NULL);
2436 void
2437 debugger_remove_breakpoint (Debugger *debugger, guint id, IAnjutaDebuggerCallback callback, gpointer user_data)
2439 gchar *buff;
2441 DEBUG_PRINT ("In function: debugger_delete_breakpoint()");
2443 g_return_if_fail (IS_DEBUGGER (debugger));
2445 buff = g_strdup_printf ("-break-delete %d", id);
2446 debugger_queue_command (debugger, buff, FALSE, FALSE, debugger_remove_breakpoint_finish, callback, user_data);
2447 g_free (buff);
2450 static void
2451 debugger_print_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2453 gchar *ptr = NULL;
2454 gchar *tmp;
2455 GList *list, *node;
2456 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2457 gpointer user_data = debugger->priv->current_cmd.user_data;
2460 list = gdb_util_remove_blank_lines (cli_results);
2461 if (g_list_length (list) < 1)
2463 tmp = NULL;
2465 else
2467 tmp = strchr ((gchar *) list->data, '=');
2469 if (tmp != NULL)
2471 ptr = g_strdup (tmp);
2472 for (node = list ? list->next : NULL; node != NULL; node = node->next)
2474 tmp = ptr;
2475 ptr = g_strconcat (tmp, (gchar *) node->data, NULL);
2476 g_free (tmp);
2480 callback (ptr, user_data, NULL);
2481 g_free (ptr);
2484 void
2485 debugger_print (Debugger *debugger, const gchar* variable, IAnjutaDebuggerCallback callback, gpointer user_data)
2487 gchar *buff;
2489 DEBUG_PRINT ("In function: debugger_print()");
2491 g_return_if_fail (IS_DEBUGGER (debugger));
2493 buff = g_strdup_printf ("print %s", variable);
2494 debugger_queue_command (debugger, buff, TRUE, FALSE, debugger_print_finish, callback, user_data);
2495 g_free (buff);
2498 static void
2499 debugger_evaluate_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2502 const GDBMIValue *value = NULL;
2503 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2504 gpointer user_data = debugger->priv->current_cmd.user_data;
2506 if (mi_results)
2507 value = gdbmi_value_hash_lookup (mi_results, "value");
2509 /* Call user function */
2510 if (callback != NULL)
2511 callback (value == NULL ? "?" : (char *)gdbmi_value_literal_get (value), user_data, NULL);
2514 void
2515 debugger_evaluate (Debugger *debugger, const gchar* name, IAnjutaDebuggerCallback callback, gpointer user_data)
2517 gchar *buff;
2518 DEBUG_PRINT ("In function: debugger_add_watch()");
2520 g_return_if_fail (IS_DEBUGGER (debugger));
2522 buff = g_strdup_printf ("-data-evaluate-expression %s", name);
2523 debugger_queue_command (debugger, buff, TRUE, FALSE, debugger_evaluate_finish, callback, user_data);
2524 g_free (buff);
2527 static void
2528 debugger_list_local_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2531 const GDBMIValue *local, *var, *frame, *args, *stack;
2532 const gchar * name;
2533 GList* list;
2534 guint i;
2535 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2536 gpointer user_data = debugger->priv->current_cmd.user_data;
2539 list = NULL;
2540 /* Add arguments */
2541 stack = gdbmi_value_hash_lookup (mi_results, "stack-args");
2542 if (stack)
2544 frame = gdbmi_value_list_get_nth (stack, 0);
2545 if (frame)
2547 args = gdbmi_value_hash_lookup (frame, "args");
2548 if (args)
2550 for (i = 0; i < gdbmi_value_get_size (args); i++)
2552 var = gdbmi_value_list_get_nth (args, i);
2553 if (var)
2555 name = gdbmi_value_literal_get (var);
2556 list = g_list_prepend (list, (gchar *)name);
2564 /* List local variables */
2565 local = gdbmi_value_hash_lookup (mi_results, "locals");
2566 if (local)
2568 for (i = 0; i < gdbmi_value_get_size (local); i++)
2570 var = gdbmi_value_list_get_nth (local, i);
2571 if (var)
2573 name = gdbmi_value_literal_get (var);
2574 list = g_list_prepend (list, (gchar *)name);
2578 list = g_list_reverse (list);
2579 callback (list, user_data, NULL);
2580 g_list_free (list);
2583 void
2584 debugger_list_local (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2586 DEBUG_PRINT ("In function: debugger_list_local()");
2588 g_return_if_fail (IS_DEBUGGER (debugger));
2590 debugger_queue_command (debugger, "-stack-list-arguments 0 0 0", TRUE, TRUE, NULL, NULL, NULL);
2591 debugger_queue_command (debugger, "-stack-list-locals 0", TRUE, FALSE, debugger_list_local_finish, callback, user_data);
2594 static void
2595 debugger_list_argument_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2598 const GDBMIValue *frame, *var, *args, *stack;
2599 const gchar * name;
2600 GList* list;
2601 guint i;
2602 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2603 gpointer user_data = debugger->priv->current_cmd.user_data;
2606 list = NULL;
2607 args = NULL;
2608 stack = gdbmi_value_hash_lookup (mi_results, "stack-args");
2609 if (stack)
2611 frame = gdbmi_value_list_get_nth (stack, 0);
2612 if (frame)
2614 args = gdbmi_value_hash_lookup (frame, "args");
2618 if (args)
2620 for (i = 0; i < gdbmi_value_get_size (args); i++)
2622 var = gdbmi_value_list_get_nth (args, i);
2623 if (var)
2625 name = gdbmi_value_literal_get (var);
2626 list = g_list_prepend (list, (gchar *)name);
2630 list = g_list_reverse (list);
2631 callback (list, user_data, NULL);
2632 g_list_free (list);
2635 void
2636 debugger_list_argument (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2638 DEBUG_PRINT ("In function: debugger_list_argument()");
2640 g_return_if_fail (IS_DEBUGGER (debugger));
2642 debugger_queue_command (debugger, "-stack-list-argument 0 0 0", TRUE, FALSE, debugger_list_argument_finish, callback, user_data);
2645 static void
2646 debugger_info_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2649 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2650 gpointer user_data = debugger->priv->current_cmd.user_data;
2652 if (callback != NULL)
2653 callback ((GList *)cli_results, user_data, NULL);
2656 void
2657 debugger_info_frame (Debugger *debugger, guint frame, IAnjutaDebuggerCallback callback, gpointer user_data)
2659 gchar *buff;
2661 DEBUG_PRINT ("In function: debugger_info_frame()");
2663 g_return_if_fail (IS_DEBUGGER (debugger));
2665 if (frame == 0)
2667 buff = g_strdup_printf("info frame");
2669 else
2671 buff = g_strdup_printf ("info frame %d", frame);
2673 debugger_queue_command (debugger, buff, TRUE, FALSE, (DebuggerParserFunc)debugger_info_finish, callback, user_data);
2674 g_free (buff);
2677 void
2678 debugger_info_signal (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2680 DEBUG_PRINT ("In function: debugger_info_signal()");
2682 g_return_if_fail (IS_DEBUGGER (debugger));
2684 debugger_queue_command (debugger, "info signals", TRUE, FALSE, (DebuggerParserFunc)debugger_info_finish, callback, user_data);
2687 void
2688 debugger_info_sharedlib (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2690 gchar *buff;
2692 DEBUG_PRINT ("In function: debugger_info_sharedlib()");
2694 g_return_if_fail (IS_DEBUGGER (debugger));
2696 buff = g_strdup_printf ("info sharedlib");
2697 debugger_queue_command (debugger, buff, TRUE, FALSE, (DebuggerParserFunc)debugger_info_finish, callback, user_data); g_free (buff);
2700 void
2701 debugger_info_args (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2703 DEBUG_PRINT ("In function: debugger_info_args()");
2705 g_return_if_fail (IS_DEBUGGER (debugger));
2707 debugger_queue_command (debugger, "info args", TRUE, FALSE, (DebuggerParserFunc)debugger_info_finish, callback, user_data);
2710 void
2711 debugger_info_target (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2713 DEBUG_PRINT ("In function: debugger_info_target()");
2715 g_return_if_fail (IS_DEBUGGER (debugger));
2717 debugger_queue_command (debugger, "info target", TRUE, FALSE, (DebuggerParserFunc)debugger_info_finish, callback, user_data);
2720 void
2721 debugger_info_program (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2723 DEBUG_PRINT ("In function: debugger_info_program()");
2725 g_return_if_fail (IS_DEBUGGER (debugger));
2727 debugger_queue_command (debugger, "info program", TRUE, FALSE, (DebuggerParserFunc)debugger_info_finish, callback, user_data);
2730 void
2731 debugger_info_udot (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2733 DEBUG_PRINT ("In function: debugger_info_udot()");
2735 g_return_if_fail (IS_DEBUGGER (debugger));
2737 debugger_queue_command (debugger, "info udot", TRUE, FALSE, (DebuggerParserFunc)debugger_info_finish, callback, user_data);
2740 void
2741 debugger_info_threads (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2743 DEBUG_PRINT ("In function: debugger_info_threads()");
2745 g_return_if_fail (IS_DEBUGGER (debugger));
2747 debugger_queue_command (debugger, "info threads", TRUE, FALSE, (DebuggerParserFunc)debugger_info_finish, callback, user_data);
2750 void
2751 debugger_info_variables (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2753 DEBUG_PRINT ("In function: debugger_info_variables()");
2755 g_return_if_fail (IS_DEBUGGER (debugger));
2757 debugger_queue_command (debugger, "info variables", TRUE, FALSE, (DebuggerParserFunc)debugger_info_finish, callback, user_data);
2760 static void
2761 debugger_read_memory_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2764 const GDBMIValue *literal;
2765 const GDBMIValue *mem;
2766 const gchar *value;
2767 gchar *data;
2768 gchar *ptr;
2769 gchar *address;
2770 guint len;
2771 guint i;
2772 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2773 gpointer user_data = debugger->priv->current_cmd.user_data;
2774 IAnjutaDebuggerMemory read = {0,};
2776 literal = gdbmi_value_hash_lookup (mi_results, "total-bytes");
2777 if (literal)
2779 guint size;
2781 len = strtoul (gdbmi_value_literal_get (literal), NULL, 10);
2782 data = g_new (gchar, len * 2);
2783 memset (data + len, 0, len);
2785 literal = gdbmi_value_hash_lookup (mi_results, "addr");
2786 address = (char *)strtoul (gdbmi_value_literal_get (literal), NULL, 0);
2788 ptr = data;
2789 size = 0;
2790 mem = gdbmi_value_hash_lookup (mi_results, "memory");
2791 if (mem)
2793 mem = gdbmi_value_list_get_nth (mem, 0);
2794 if (mem)
2796 mem = gdbmi_value_hash_lookup (mem, "data");
2797 if (mem)
2799 size = gdbmi_value_get_size (mem);
2804 if (size < len) len = size;
2805 for (i = 0; i < len; i++)
2807 literal = gdbmi_value_list_get_nth (mem, i);
2808 if (literal)
2810 gchar *endptr;
2811 value = gdbmi_value_literal_get (literal);
2812 *ptr = strtoul (value, &endptr, 16);
2813 if ((*value != '\0') && (*endptr == '\0'))
2815 /* valid data */
2816 ptr[len] = 1;
2818 ptr++;
2821 read.address = address;
2822 read.length = len;
2823 read.data = data;
2824 callback (&read, user_data, NULL);
2826 g_free (data);
2828 else
2830 callback (NULL, user_data, NULL);
2834 void
2835 debugger_inspect_memory (Debugger *debugger, guint address, guint length, IAnjutaDebuggerCallback callback, gpointer user_data)
2837 gchar *buff;
2839 DEBUG_PRINT ("In function: debugger_inspect_memory()");
2841 g_return_if_fail (IS_DEBUGGER (debugger));
2843 buff = g_strdup_printf ("-data-read-memory 0x%x x 1 1 %d", address, length);
2844 debugger_queue_command (debugger, buff, FALSE, FALSE, debugger_read_memory_finish, callback, user_data);
2845 g_free (buff);
2848 static void
2849 debugger_disassemble_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2852 const GDBMIValue *literal;
2853 const GDBMIValue *line;
2854 const GDBMIValue *mem;
2855 const gchar *value;
2856 guint i;
2857 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2858 gpointer user_data = debugger->priv->current_cmd.user_data;
2859 IAnjutaDebuggerDisassembly *read = NULL;
2861 if (error != NULL)
2863 /* Command fail */
2864 callback (NULL, user_data, error);
2866 return;
2870 mem = gdbmi_value_hash_lookup (mi_results, "asm_insns");
2871 if (mem)
2873 guint size;
2875 size = gdbmi_value_get_size (mem);
2876 read = (IAnjutaDebuggerDisassembly *)g_malloc0(sizeof (IAnjutaDebuggerDisassembly) + sizeof(IAnjutaDebuggerALine) * size);
2877 read->size = size;
2879 for (i = 0; i < size; i++)
2881 line = gdbmi_value_list_get_nth (mem, i);
2882 if (line)
2884 /* Get address */
2885 literal = gdbmi_value_hash_lookup (line, "address");
2886 if (literal)
2888 value = gdbmi_value_literal_get (literal);
2889 read->data[i].address = strtoul (value, NULL, 0);
2892 /* Get disassembly line */
2893 literal = gdbmi_value_hash_lookup (line, "inst");
2894 if (literal)
2896 read->data[i].text = gdbmi_value_literal_get (literal);
2901 /* Remove last line to mark end */
2902 read->data[i - 1].text = NULL;
2904 callback (read, user_data, NULL);
2906 g_free (read);
2908 else
2910 callback (NULL, user_data, NULL);
2914 void
2915 debugger_disassemble (Debugger *debugger, guint address, guint length, IAnjutaDebuggerCallback callback, gpointer user_data)
2917 gchar *buff;
2918 guint end;
2920 DEBUG_PRINT ("In function: debugger_disassemble()");
2922 g_return_if_fail (IS_DEBUGGER (debugger));
2925 /* Handle overflow */
2926 if (address + length < address) length = G_MAXUINT - address;
2927 buff = g_strdup_printf ("-data-disassemble -s 0x%x -e 0x%x -- 0", address, address + length);
2928 debugger_queue_command (debugger, buff, FALSE, FALSE, debugger_disassemble_finish, callback, user_data);
2929 g_free (buff);
2932 static void
2933 add_frame (const GDBMIValue *frame_hash, GList** stack)
2935 const GDBMIValue *literal;
2936 IAnjutaDebuggerFrame* frame;
2938 frame = g_new0 (IAnjutaDebuggerFrame, 1);
2939 *stack = g_list_prepend (*stack, frame);
2941 literal = gdbmi_value_hash_lookup (frame_hash, "level");
2942 if (literal)
2943 frame->level = strtoul (gdbmi_value_literal_get (literal), NULL, 10);
2945 literal = gdbmi_value_hash_lookup (frame_hash, "fullname");
2946 if (literal == NULL)
2947 literal = gdbmi_value_hash_lookup (frame_hash, "file");
2948 if (literal)
2949 frame->file = (gchar *)gdbmi_value_literal_get (literal);
2951 literal = gdbmi_value_hash_lookup (frame_hash, "line");
2952 if (literal)
2953 frame->line = strtoul (gdbmi_value_literal_get (literal), NULL, 10);
2955 literal = gdbmi_value_hash_lookup (frame_hash, "func");
2956 if (literal)
2957 frame->function = (gchar *)gdbmi_value_literal_get (literal);
2959 literal = gdbmi_value_hash_lookup (frame_hash, "addr");
2960 if (literal)
2961 frame->address = strtoul (gdbmi_value_literal_get (literal), NULL, 16);
2965 static void
2966 set_func_args (const GDBMIValue *frame_hash, GList** node)
2968 const gchar *level;
2969 const GDBMIValue *literal, *args_list, *arg_hash;
2970 gint i;
2971 GString *args_str;
2972 IAnjutaDebuggerFrame* frame;
2974 literal = gdbmi_value_hash_lookup (frame_hash, "level");
2975 if (!literal)
2976 return;
2978 level = gdbmi_value_literal_get (literal);
2979 if (!level)
2980 return;
2982 frame = (IAnjutaDebuggerFrame *)(*node)->data;
2984 args_list = gdbmi_value_hash_lookup (frame_hash, "args");
2985 if (args_list)
2987 args_str = g_string_new ("(");
2988 for (i = 0; i < gdbmi_value_get_size (args_list); i++)
2990 const gchar *name, *value;
2992 arg_hash = gdbmi_value_list_get_nth (args_list, i);
2993 if (!arg_hash)
2994 continue;
2996 literal = gdbmi_value_hash_lookup (arg_hash, "name");
2997 if (!literal)
2998 continue;
2999 name = gdbmi_value_literal_get (literal);
3000 if (!name)
3001 continue;
3003 literal = gdbmi_value_hash_lookup (arg_hash, "value");
3004 if (!literal)
3005 continue;
3006 value = gdbmi_value_literal_get (literal);
3007 if (!value)
3008 continue;
3009 args_str = g_string_append (args_str, name);
3010 args_str = g_string_append (args_str, "=");
3011 args_str = g_string_append (args_str, value);
3012 if (i < (gdbmi_value_get_size (args_list) - 1))
3013 args_str = g_string_append (args_str, ", ");
3015 args_str = g_string_append (args_str, ")");
3016 frame->args = args_str->str;
3017 g_string_free (args_str, FALSE);
3019 *node = g_list_next (*node);
3022 static void
3023 debugger_stack_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
3026 GList* stack = NULL;
3027 GList* node;
3028 const GDBMIValue *stack_list;
3029 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
3030 gpointer user_data = debugger->priv->current_cmd.user_data;
3032 if (!mi_results)
3033 return;
3035 stack_list = gdbmi_value_hash_lookup (mi_results, "stack");
3036 if (stack_list)
3037 gdbmi_value_foreach (stack_list, (GFunc)add_frame, &stack);
3039 if (stack)
3041 stack = g_list_reverse (stack);
3042 node = g_list_first (stack);
3043 stack_list = gdbmi_value_hash_lookup (mi_results, "stack-args");
3044 if (stack_list)
3045 gdbmi_value_foreach (stack_list, (GFunc)set_func_args, &node);
3047 // Call call back function
3048 if (callback != NULL)
3049 callback (stack, user_data, NULL);
3051 // Free data
3052 for (node = g_list_first (stack); node != NULL; node = g_list_next (node))
3054 g_free ((gchar *)((IAnjutaDebuggerFrame *)node->data)->args);
3055 g_free (node->data);
3058 g_list_free (stack);
3060 else
3062 // Call call back function
3063 if (callback != NULL)
3064 callback (NULL, user_data, NULL);
3068 void
3069 debugger_list_frame (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
3071 DEBUG_PRINT ("In function: debugger_list_frame()");
3073 g_return_if_fail (IS_DEBUGGER (debugger));
3075 debugger_queue_command (debugger, "-stack-list-frames", TRUE, TRUE, NULL, NULL, NULL);
3076 debugger_queue_command (debugger, "-stack-list-arguments 1", TRUE, FALSE, debugger_stack_finish, callback, user_data);
3079 static void
3080 add_register_name (const GDBMIValue *reg_literal, GList** list)
3082 IAnjutaDebuggerRegister* reg;
3083 GList *prev = *list;
3085 reg = g_new0 (IAnjutaDebuggerRegister, 1);
3086 *list = g_list_prepend (prev, reg);
3087 reg->name = (gchar *)gdbmi_value_literal_get (reg_literal);
3088 reg->num = prev == NULL ? 0 : ((IAnjutaDebuggerRegister *)prev->data)->num + 1;
3091 static void
3092 add_register_value (const GDBMIValue *reg_hash, GList** list)
3094 const GDBMIValue *literal;
3095 const gchar *val;
3096 IAnjutaDebuggerRegister* reg;
3097 guint num;
3098 GList* prev = *list;
3100 literal = gdbmi_value_hash_lookup (reg_hash, "number");
3101 if (!literal)
3102 return;
3103 val = gdbmi_value_literal_get (literal);
3104 num = strtoul (val, NULL, 10);
3106 literal = gdbmi_value_hash_lookup (reg_hash, "value");
3107 if (!literal)
3108 return;
3110 reg = g_new0 (IAnjutaDebuggerRegister, 1);
3111 *list = g_list_prepend (prev, reg);
3112 reg->num = num;
3113 reg->value = (gchar *)gdbmi_value_literal_get (literal);
3116 static void
3117 debugger_register_name_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
3120 GList* list = NULL;
3121 GList* node;
3122 const GDBMIValue *reg_list;
3123 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
3124 gpointer user_data = debugger->priv->current_cmd.user_data;
3126 if (!mi_results)
3127 return;
3129 reg_list = gdbmi_value_hash_lookup (mi_results, "register-names");
3130 if (reg_list)
3131 gdbmi_value_foreach (reg_list, (GFunc)add_register_name, &list);
3132 list = g_list_reverse (list);
3134 // Call call back function
3135 if (callback != NULL)
3136 callback (list, user_data, NULL);
3138 // Free data
3139 for (node = g_list_first (list); node != NULL; node = g_list_next (node))
3141 g_free (node->data);
3143 g_list_free (list);
3146 static void
3147 debugger_register_value_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
3150 GList* list = NULL;
3151 GList* node;
3152 const GDBMIValue *reg_list;
3153 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
3154 gpointer user_data = debugger->priv->current_cmd.user_data;
3156 if (!mi_results)
3157 return;
3159 reg_list = gdbmi_value_hash_lookup (mi_results, "register-values");
3160 if (reg_list)
3161 gdbmi_value_foreach (reg_list, (GFunc)add_register_value, &list);
3162 list = g_list_reverse (list);
3164 // Call call back function
3165 if (callback != NULL)
3166 callback (list, user_data, NULL);
3168 // Free data
3169 for (node = g_list_first (list); node != NULL; node = g_list_next (node))
3171 g_free (node->data);
3173 g_list_free (list);
3176 void
3177 debugger_list_register (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
3179 DEBUG_PRINT ("In function: debugger_list_register()");
3181 g_return_if_fail (IS_DEBUGGER (debugger));
3183 debugger_queue_command (debugger, "-data-list-register-names", TRUE, FALSE, debugger_register_name_finish, callback, user_data);
3186 void
3187 debugger_update_register (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
3189 DEBUG_PRINT ("In function: debugger_update_register()");
3191 g_return_if_fail (IS_DEBUGGER (debugger));
3193 debugger_queue_command (debugger, "-data-list-register-values r", TRUE, FALSE, (DebuggerParserFunc)debugger_register_value_finish, callback, user_data);
3196 void
3197 debugger_write_register (Debugger *debugger, const gchar *name, const gchar *value)
3199 gchar *buf;
3201 DEBUG_PRINT ("In function: debugger_write_register()");
3203 g_return_if_fail (IS_DEBUGGER (debugger));
3205 buf = g_strdup_printf ("-data-evaluate-expression \"$%s=%s\"", name, value);
3206 debugger_queue_command (debugger, buf, TRUE, FALSE, NULL, NULL, NULL);
3207 g_free (buf);
3210 static void
3211 debugger_set_frame_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
3214 guint frame = (guint)debugger->priv->current_cmd.user_data;
3216 g_signal_emit_by_name (debugger->priv->instance, "frame-changed", frame);
3219 void
3220 debugger_set_frame (Debugger *debugger, guint frame)
3222 gchar *buff;
3224 DEBUG_PRINT ("In function: debugger_set_frame()");
3226 g_return_if_fail (IS_DEBUGGER (debugger));
3228 buff = g_strdup_printf ("-stack-select-frame %d", frame);
3230 debugger_queue_command (debugger, buff, FALSE, FALSE, (DebuggerParserFunc)debugger_set_frame_finish, NULL, (gpointer)frame);
3231 g_free (buff);
3234 void
3235 debugger_set_log (Debugger *debugger, IAnjutaMessageView *log)
3237 debugger->priv->log = log;
3240 /* Variable objects functions
3241 *---------------------------------------------------------------------------*/
3243 void
3244 debugger_delete_variable (Debugger *debugger, const gchar* name)
3246 gchar *buff;
3248 DEBUG_PRINT ("In function: delete_variable()");
3250 g_return_if_fail (IS_DEBUGGER (debugger));
3252 buff = g_strdup_printf ("-var-delete %s", name);
3253 debugger_queue_command (debugger, buff, FALSE, FALSE, NULL, NULL, NULL);
3254 g_free (buff);
3257 static void
3258 gdb_var_evaluate_expression (Debugger *debugger,
3259 const GDBMIValue *mi_results, const GList *cli_results,
3260 GError *error)
3262 const gchar *value = NULL;
3263 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
3264 gpointer user_data = debugger->priv->current_cmd.user_data;
3266 if (mi_results != NULL)
3268 const GDBMIValue *const gdbmi_value =
3269 gdbmi_value_hash_lookup (mi_results, "value");
3271 if (gdbmi_value != NULL)
3272 value = gdbmi_value_literal_get (gdbmi_value);
3274 callback ((const gpointer)value, user_data, NULL);
3277 void
3278 debugger_evaluate_variable (Debugger *debugger, const gchar* name, IAnjutaDebuggerCallback callback, gpointer user_data)
3280 gchar *buff;
3282 DEBUG_PRINT ("In function: evaluate_variable()");
3284 g_return_if_fail (IS_DEBUGGER (debugger));
3286 buff = g_strdup_printf ("-var-evaluate-expression %s", name);
3287 debugger_queue_command (debugger, buff, FALSE, FALSE, gdb_var_evaluate_expression, callback, user_data);
3288 g_free (buff);
3291 void
3292 debugger_assign_variable (Debugger *debugger, const gchar* name, const gchar *value)
3294 gchar *buff;
3296 DEBUG_PRINT ("In function: assign_variable()");
3298 g_return_if_fail (IS_DEBUGGER (debugger));
3300 buff = g_strdup_printf ("-var-assign %s %s", name, value);
3301 debugger_queue_command (debugger, buff, FALSE, FALSE, NULL, NULL, NULL);
3302 g_free (buff);
3305 static void
3306 gdb_var_list_children (Debugger *debugger,
3307 const GDBMIValue *mi_results, const GList *cli_results,
3308 GError *error)
3310 GList* list = NULL;
3311 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
3312 gpointer user_data = debugger->priv->current_cmd.user_data;
3314 if (mi_results != NULL)
3316 const GDBMIValue *literal;
3317 const GDBMIValue *children;
3318 glong numchild = 0;
3319 glong i = 0;
3321 literal = gdbmi_value_hash_lookup (mi_results, "numchild");
3323 if (literal)
3324 numchild = strtoul(gdbmi_value_literal_get (literal), NULL, 0);
3325 children = gdbmi_value_hash_lookup (mi_results, "children");
3327 for(i = 0 ; i < numchild; ++i)
3329 const GDBMIValue *const gdbmi_chl =
3330 gdbmi_value_list_get_nth (children, i);
3331 IAnjutaDebuggerVariable *var;
3333 var = g_new0 (IAnjutaDebuggerVariable, 1);
3335 literal = gdbmi_value_hash_lookup (gdbmi_chl, "name");
3336 if (literal)
3337 var->name = (gchar *)gdbmi_value_literal_get (literal);
3339 literal = gdbmi_value_hash_lookup (gdbmi_chl, "exp");
3340 if (literal)
3341 var->expression = (gchar *)gdbmi_value_literal_get(literal);
3343 literal = gdbmi_value_hash_lookup (gdbmi_chl, "type");
3344 if (literal)
3345 var->type = (gchar *)gdbmi_value_literal_get(literal);
3347 literal = gdbmi_value_hash_lookup (gdbmi_chl, "value");
3348 if (literal)
3349 var->value = (gchar *)gdbmi_value_literal_get(literal);
3351 literal = gdbmi_value_hash_lookup (gdbmi_chl, "numchild");
3352 if (literal)
3353 var->children = strtoul(gdbmi_value_literal_get(literal), NULL, 10);
3355 list = g_list_prepend (list, var);
3357 list = g_list_reverse (list);
3360 callback (list, user_data, NULL);
3361 g_list_foreach (list, (GFunc)g_free, NULL);
3362 g_list_free (list);
3365 void debugger_list_variable_children (Debugger *debugger, const gchar* name, IAnjutaDebuggerCallback callback, gpointer user_data)
3367 gchar *buff;
3369 DEBUG_PRINT ("In function: list_variable_children()");
3371 g_return_if_fail (IS_DEBUGGER (debugger));
3373 buff = g_strdup_printf ("-var-list-children --all-values %s", name);
3374 debugger_queue_command (debugger, buff, FALSE, FALSE, gdb_var_list_children, callback, user_data);
3375 g_free (buff);
3378 static void
3379 gdb_var_create (Debugger *debugger,
3380 const GDBMIValue *mi_results, const GList *cli_results,
3381 GError *error)
3383 const GDBMIValue * result;
3384 IAnjutaDebuggerVariable var = {0,};
3385 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
3386 gpointer user_data = debugger->priv->current_cmd.user_data;
3388 if ((error == NULL) && (mi_results != NULL))
3390 result = gdbmi_value_hash_lookup (mi_results, "name");
3391 var.name = (gchar *)gdbmi_value_literal_get(result);
3393 result = gdbmi_value_hash_lookup (mi_results, "type");
3394 var.type = (gchar *)gdbmi_value_literal_get (result);
3396 result = gdbmi_value_hash_lookup (mi_results, "numchild");
3397 var.children = strtoul (gdbmi_value_literal_get(result), NULL, 10);
3399 callback (&var, user_data, error);
3403 void debugger_create_variable (Debugger *debugger, const gchar* name, IAnjutaDebuggerCallback callback, gpointer user_data)
3405 gchar *buff;
3407 DEBUG_PRINT ("In function: create_variable()");
3409 g_return_if_fail (IS_DEBUGGER (debugger));
3411 buff = g_strdup_printf ("-var-create - * %s", name);
3412 debugger_queue_command (debugger, buff, FALSE, FALSE, gdb_var_create, callback, user_data);
3413 g_free (buff);
3416 static void
3417 gdb_var_update (Debugger *debugger,
3418 const GDBMIValue *mi_results, const GList *cli_results,
3419 GError *error)
3421 GList* list = NULL;
3422 glong idx = 0, changed_count = 0;
3423 const GDBMIValue *const gdbmi_changelist =
3424 gdbmi_value_hash_lookup (mi_results, "changelist");
3425 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
3426 gpointer user_data = debugger->priv->current_cmd.user_data;
3429 changed_count = gdbmi_value_get_size (gdbmi_changelist);
3430 for(; idx<changed_count; ++idx)
3432 const GDBMIValue *const gdbmi_change =
3433 gdbmi_value_list_get_nth (gdbmi_changelist, idx);
3434 const GDBMIValue *gdbmi_val =
3435 gdbmi_value_hash_lookup (gdbmi_change, "in_scope");
3436 IAnjutaDebuggerVariable *var;
3438 if(0 != strcmp(gdbmi_value_literal_get(gdbmi_val), "false"))
3440 gdbmi_val = gdbmi_value_hash_lookup (gdbmi_change, "name");
3441 var = g_new0 (IAnjutaDebuggerVariable, 1);
3442 var->changed = TRUE;
3443 var->name = (gchar *)gdbmi_value_literal_get(gdbmi_val);
3445 list = g_list_prepend (list, var);
3448 list = g_list_reverse (list);
3449 callback (list, user_data, NULL);
3450 g_list_foreach (list, (GFunc)g_free, NULL);
3451 g_list_free (list);
3454 void debugger_update_variable (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
3456 DEBUG_PRINT ("In function: update_variable()");
3458 g_return_if_fail (IS_DEBUGGER (debugger));
3460 debugger_queue_command (debugger, "-var-update *", FALSE, FALSE, gdb_var_update, callback, user_data);
3463 GType
3464 debugger_get_type (void)
3466 static GType obj_type = 0;
3468 if (!obj_type)
3470 static const GTypeInfo obj_info =
3472 sizeof (DebuggerClass),
3473 (GBaseInitFunc) NULL,
3474 (GBaseFinalizeFunc) NULL,
3475 (GClassInitFunc) debugger_class_init,
3476 (GClassFinalizeFunc) NULL,
3477 NULL, /* class_data */
3478 sizeof (Debugger),
3479 0, /* n_preallocs */
3480 (GInstanceInitFunc) debugger_instance_init,
3481 NULL /* value_table */
3483 obj_type = g_type_register_static (G_TYPE_OBJECT,
3484 "Debugger", &obj_info, 0);
3486 return obj_type;
3489 static void
3490 debugger_dispose (GObject *obj)
3492 Debugger *debugger = DEBUGGER (obj);
3494 DEBUG_PRINT ("In function: debugger_shutdown()");
3496 debugger_abort (debugger);
3498 /* Good Bye message */
3499 if (debugger->priv->output_callback)
3501 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
3502 "Debugging session completed.\n",
3503 debugger->priv->output_user_data);
3506 if (debugger->priv->launcher)
3508 anjuta_launcher_reset (debugger->priv->launcher);
3509 g_object_unref (debugger->priv->launcher);
3510 debugger->priv->launcher = NULL;
3513 GNOME_CALL_PARENT (G_OBJECT_CLASS, dispose, (obj));
3516 static void
3517 debugger_finalize (GObject *obj)
3519 Debugger *debugger = DEBUGGER (obj);
3520 g_string_free (debugger->priv->stdo_line, TRUE);
3521 g_string_free (debugger->priv->stdo_acc, TRUE);
3522 g_string_free (debugger->priv->stde_line, TRUE);
3523 g_free (debugger->priv);
3524 GNOME_CALL_PARENT (G_OBJECT_CLASS, finalize, (obj));
3527 static void
3528 debugger_class_init (DebuggerClass * klass)
3530 GObjectClass *object_class;
3532 g_return_if_fail (klass != NULL);
3533 object_class = G_OBJECT_CLASS (klass);
3535 DEBUG_PRINT ("Initializing debugger class");
3537 parent_class = g_type_class_peek_parent (klass);
3538 object_class->dispose = debugger_dispose;
3539 object_class->finalize = debugger_finalize;
3543 #if 0 /* FIXME */
3544 void
3545 debugger_signal (const gchar *sig, gboolean show_msg)
3547 /* eg:- "SIGTERM" */
3548 gchar *buff;
3549 gchar *cmd;
3551 DEBUG_PRINT ("In function: debugger_signal()");
3553 if (debugger_is_active () == FALSE)
3554 return;
3555 if (debugger.prog_is_running == FALSE)
3556 return;
3557 if (debugger.child_pid < 1)
3559 DEBUG_PRINT ("Not sending signal - pid not known\n");
3560 return;
3563 if (show_msg)
3565 buff = g_strdup_printf (_("Sending signal %s to the process: %d"),
3566 sig, (int) debugger.child_pid);
3567 gdb_util_append_message (ANJUTA_PLUGIN (debugger.plugin), buff);
3568 g_free (buff);
3571 if (debugger_is_ready ())
3573 cmd = g_strconcat ("signal ", sig, NULL);
3574 stack_trace_set_frame (debugger.stack, 0);
3575 debugger_put_cmd_in_queqe (cmd, DB_CMD_ALL, NULL, NULL);
3576 debugger_put_cmd_in_queqe ("info program", DB_CMD_NONE,
3577 on_debugger_update_prog_status,
3578 NULL);
3579 g_free (cmd);
3580 debugger_execute_cmd_in_queqe ();
3582 else
3584 GtkWindow *parent;
3585 int status;
3587 parent = GTK_WINDOW (ANJUTA_PLUGIN (debugger.plugin)->shell);
3588 status = gdb_util_kill_process (debugger.child_pid, sig);
3589 if (status != 0 && show_msg)
3590 anjuta_util_dialog_error (parent,
3591 _("Error whilst signaling the process."));
3595 static void
3596 query_set_cmd (const gchar *cmd, gboolean state)
3598 gchar buffer[50];
3599 gchar *tmp = g_stpcpy (buffer, cmd);
3600 strcpy (tmp, state ? "on" : "off");
3601 debugger_put_cmd_in_queqe (buffer, DB_CMD_NONE, NULL, NULL);
3604 static void
3605 query_set_verbose (gboolean state)
3607 query_set_cmd ("set verbose ", state);
3610 static void
3611 query_set_print_staticmembers (gboolean state)
3613 query_set_cmd ("set print static-members ", state);
3616 static void
3617 query_set_print_pretty (gboolean state)
3619 query_set_cmd ("set print pretty ", state);
3622 void debugger_query_evaluate_expr_tip (const gchar *expr,
3623 DebuggerCLIFunc parser, gpointer data)
3625 query_set_verbose (FALSE);
3626 query_set_print_staticmembers (FALSE);
3627 gchar *printcmd = g_strconcat ("print ", expr, NULL);
3628 debugger_put_cmd_in_queqe (printcmd, DB_CMD_NONE, parser, data);
3629 query_set_verbose (TRUE);
3630 query_set_print_staticmembers (TRUE);
3631 g_free (printcmd);
3634 void
3635 debugger_query_evaluate_expression (const gchar *expr, DebuggerFunc parser,
3636 gpointer data)
3638 query_set_print_pretty (TRUE);
3639 query_set_verbose (FALSE);
3640 gchar *printcmd = g_strconcat ("print ", expr, NULL);
3641 debugger_put_cmd_in_queqe (printcmd, DB_CMD_SE_MESG | DB_CMD_SE_DIALOG,
3642 parser, data);
3643 query_set_print_pretty (FALSE);
3644 query_set_verbose (TRUE);
3645 g_free (printcmd);
3648 #endif