Updated Spanish translation
[anjuta-git-plugin.git] / plugins / gdb / debugger.c
blob1333dfc591b93c65a03e0d8d504255aad540e97b
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 * debugger.c Copyright (C) 2000 Kh. Naba Kumar Singh
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc., 59
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
23 /*#define DEBUG*/
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28 #include <signal.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <ctype.h>
32 #include <sys/wait.h>
33 #include <errno.h>
34 #include <assert.h>
35 #include <fcntl.h>
37 #include <glib.h>
39 #include <libanjuta/anjuta-launcher.h>
40 #include <libanjuta/anjuta-debug.h>
41 #include <libanjuta/anjuta-marshal.h>
42 #include <libanjuta/interfaces/ianjuta-debugger-breakpoint.h>
43 #include <libanjuta/interfaces/ianjuta-debugger-register.h>
44 #include <libanjuta/interfaces/ianjuta-debugger-memory.h>
45 #include <libanjuta/interfaces/ianjuta-debugger-instruction.h>
46 #include <libanjuta/interfaces/ianjuta-debugger-variable.h>
48 #include "debugger.h"
49 #include "utilities.h"
51 #define ANJUTA_LOG_ENV "ANJUTA_LOG"
52 #define DEBUGGER_LOG_LEVEL 1
54 #define GDB_PROMPT "(gdb)"
55 #define FILE_BUFFER_SIZE 1024
56 #define GDB_PATH "gdb"
57 #define SUMMARY_MAX_LENGTH 90 /* Should be smaller than 4K to be displayed
58 * in GtkCellRendererCell */
60 enum {
61 DEBUGGER_NONE,
62 DEBUGGER_EXIT,
63 DEBUGGER_RERUN_PROGRAM
66 enum
68 PROGRAM_LOADED_SIGNAL,
69 PROGRAM_RUNNING_SIGNAL,
70 PROGRAM_EXITED_SIGNAL,
71 PROGRAM_STOPPED_SIGNAL,
72 RESULTS_ARRIVED_SIGNAL,
73 LOCATION_CHANGED_SIGNAL,
74 BREAKPOINT_CHANGED_SIGNAL,
75 VARIABLE_CHANGED_SIGNAL,
76 LAST_SIGNAL
79 struct _DebuggerPriv
81 GtkWindow *parent_win;
83 IAnjutaDebuggerOutputCallback output_callback;
84 gpointer output_user_data;
86 GList *search_dirs;
88 gboolean prog_is_running;
89 gboolean prog_is_attached;
90 gboolean prog_is_loaded;
91 gboolean debugger_is_started;
92 guint debugger_is_busy;
93 gint post_execution_flag;
95 AnjutaLauncher *launcher;
96 GString *stdo_line;
97 GString *stdo_acc;
98 GString *stde_line;
100 GList *cli_lines;
102 /* State */
103 gboolean solib_event;
104 gboolean stopping;
105 gboolean exiting;
106 gboolean starting;
107 gboolean terminating;
108 gboolean loading;
110 /* GDB command queue */
111 GList *cmd_queqe;
112 DebuggerCommand current_cmd;
113 gboolean skip_next_prompt;
114 gboolean command_output_sent;
116 pid_t inferior_pid;
117 gint current_thread;
118 guint current_frame;
120 GObject* instance;
122 IAnjutaMessageView *log;
123 gboolean gdb_log;
126 static gpointer parent_class;
128 static void debugger_queue_clear (Debugger *debugger);
129 static void debugger_queue_execute_command (Debugger *debugger);
131 static void gdb_stdout_line_arrived (Debugger *debugger, const gchar * line);
132 static void gdb_stderr_line_arrived (Debugger *debugger, const gchar * line);
133 static void debugger_stdo_flush (Debugger *debugger);
134 static void debugger_stde_flush (Debugger *debugger);
135 static void on_gdb_output_arrived (AnjutaLauncher *launcher,
136 AnjutaLauncherOutputType output_type,
137 const gchar *chars, gpointer data);
138 static void on_gdb_terminated (AnjutaLauncher *launcher,
139 gint child_pid, gint status,
140 gulong t, gpointer data);
142 static void debugger_class_init (DebuggerClass *klass);
143 static void debugger_instance_init (Debugger *debugger);
145 typedef struct _GdbGListPacket
147 GList* list;
148 gint tag;
149 } GdbGListPacket;
151 /* Useful functions
152 *---------------------------------------------------------------------------*/
154 static gchar * gdb_quote (const gchar *unquoted_string)
156 const char *p;
157 g_return_val_if_fail (unquoted_string != NULL, NULL);
160 p = strpbrk (unquoted_string, "\"\\");
161 if (p == NULL)
163 /* No need to quote anything */
164 return g_strdup (unquoted_string);
166 else
168 GString *dest;
170 dest = g_string_new_len (unquoted_string, p - unquoted_string);
171 for (;;)
173 g_string_append_c (dest, '\\');
174 unquoted_string = p;
175 p = strpbrk (unquoted_string + 1, "\"\\");
176 if (p == NULL)
178 g_string_append (dest, unquoted_string);
179 break;
181 else
183 g_string_append_len (dest, unquoted_string, p - unquoted_string);
186 return g_string_free (dest, FALSE);
190 typedef struct _GdbMessageCode GdbMessageCode;
192 struct _GdbMessageCode
194 const gchar *msg;
195 guint code;
198 const static GdbMessageCode GdbErrorMessage[] =
199 {{"mi_cmd_var_create: unable to create variable object",
200 IANJUTA_DEBUGGER_UNABLE_TO_CREATE_VARIABLE},
201 {"Cannot access memory at address ",
202 IANJUTA_DEBUGGER_UNABLE_TO_ACCESS_MEMORY},
203 {"No source file named ",
204 IANJUTA_DEBUGGER_UNABLE_TO_OPEN_FILE},
205 {"No executable file specified.",
206 IANJUTA_DEBUGGER_PROGRAM_NOT_FOUND},
207 {NULL, 0}};
209 static guint
210 gdb_match_error(const gchar *message)
212 const GdbMessageCode* msg;
214 for (msg = GdbErrorMessage; msg->msg != NULL; msg++)
216 gsize len = strlen (msg->msg);
218 if (memcmp (msg->msg, message, len) == 0)
220 return msg->code;
224 return IANJUTA_DEBUGGER_UNKNOWN_ERROR;
227 static void
228 debugger_message_view_append (Debugger *debugger, IAnjutaMessageViewType type, const char *message)
230 guint len = strlen(message);
231 gchar buf[SUMMARY_MAX_LENGTH];
232 const gchar* summary = message;
233 const gchar* detail = "";
236 if (len > SUMMARY_MAX_LENGTH)
239 memcpy(buf, message, SUMMARY_MAX_LENGTH - 4);
240 memcpy(buf + SUMMARY_MAX_LENGTH - 4, "...", 4);
241 summary = buf;
242 detail = message;
245 ianjuta_message_view_append (debugger->priv->log, type, summary, detail, NULL);
248 static void
249 debugger_initialize (Debugger *debugger)
251 const gchar* anjuta_log;
253 DEBUG_PRINT ("In function: debugger_init()");
255 debugger->priv = g_new0 (DebuggerPriv, 1);
257 debugger->priv->output_callback = NULL;
258 debugger->priv->parent_win = NULL;
259 debugger->priv->search_dirs = NULL;
260 debugger->priv->launcher = anjuta_launcher_new ();
262 debugger->priv->debugger_is_started = FALSE;
263 debugger->priv->prog_is_running = FALSE;
264 debugger->priv->debugger_is_busy = 0;
265 debugger->priv->starting = FALSE;
266 debugger->priv->terminating = FALSE;
267 debugger->priv->skip_next_prompt = FALSE;
268 debugger->priv->command_output_sent = FALSE;
270 debugger->priv->current_cmd.cmd = NULL;
271 debugger->priv->current_cmd.parser = NULL;
273 debugger->priv->cmd_queqe = NULL;
274 debugger->priv->cli_lines = NULL;
275 debugger->priv->solib_event = FALSE;
277 debugger->priv->stdo_line = g_string_sized_new (FILE_BUFFER_SIZE);
278 g_string_assign (debugger->priv->stdo_line, "");
279 debugger->priv->stdo_acc = g_string_new ("");
281 debugger->priv->stde_line = g_string_sized_new (FILE_BUFFER_SIZE);
282 g_string_assign (debugger->priv->stde_line, "");
284 debugger->priv->post_execution_flag = DEBUGGER_NONE;
286 anjuta_log = g_getenv (ANJUTA_LOG_ENV);
287 debugger->priv->gdb_log = anjuta_log && (atoi(anjuta_log) > DEBUGGER_LOG_LEVEL);
290 static void
291 debugger_instance_init (Debugger *debugger)
293 debugger_initialize (debugger);
296 Debugger*
297 debugger_new (GtkWindow *parent_win, GObject* instance)
299 Debugger *debugger;
301 debugger = g_object_new (DEBUGGER_TYPE, NULL);
302 debugger->priv->parent_win = parent_win;
303 debugger->priv->instance = instance;
305 return debugger;
308 void
309 debugger_free (Debugger *debugger)
311 g_return_if_fail (IS_DEBUGGER (debugger));
313 g_object_unref (debugger);
316 gboolean
317 debugger_is_ready (Debugger *debugger)
319 g_return_val_if_fail (IS_DEBUGGER (debugger), FALSE);
320 return !debugger->priv->debugger_is_busy;
323 gboolean
324 debugger_program_is_running (Debugger *debugger)
326 g_return_val_if_fail (IS_DEBUGGER (debugger), FALSE);
327 return debugger->priv->prog_is_running;
330 gboolean
331 debugger_program_is_attached (Debugger *debugger)
333 g_return_val_if_fail (IS_DEBUGGER (debugger), FALSE);
334 return debugger->priv->prog_is_attached;
337 gboolean
338 debugger_program_is_loaded (Debugger *debugger)
340 g_return_val_if_fail (IS_DEBUGGER (debugger), FALSE);
341 return debugger->priv->prog_is_loaded;
344 static void
345 debugger_log_command (Debugger *debugger, const gchar *command)
347 gchar* str;
348 gsize len;
350 if (debugger->priv->log == NULL) return;
352 if (*command == '-')
354 str = g_strdup (command);
355 len = strlen (command);
357 /* Remove trailing carriage return */
358 if (str[len - 1] == '\n') str[len - 1] = '\0';
360 /* Log only MI command as other are echo */
361 #ifdef DEBUG
362 DEBUG_PRINT ("GDB:> %s", str);
363 #else
364 if (debugger->priv->gdb_log) g_message ("GDB:> %s", str);
365 #endif
366 debugger_message_view_append (debugger, IANJUTA_MESSAGE_VIEW_TYPE_NORMAL, str);
367 g_free (str);
371 static void
372 debugger_log_output (Debugger *debugger, const gchar *line)
374 gchar* str;
375 const gchar* start;
376 IAnjutaMessageViewType type;
377 gsize len;
379 if (debugger->priv->log == NULL) return;
381 type = IANJUTA_MESSAGE_VIEW_TYPE_NORMAL;
382 switch (*line)
384 case '~':
385 type = IANJUTA_MESSAGE_VIEW_TYPE_INFO;
386 /* Go through */
387 case '&':
388 len = strlen(line);
389 start = line + 1;
391 /* Remove double quote if necessary */
392 if ((line[1] == '\"') && (line[len - 1] == '\"')) start++;
393 str = g_strcompress (line + 2);
394 len = strlen (str);
395 if (start == line + 2)
397 str[len - 1] = '\0';
398 len--;
401 /* Remove trailing carriage return */
402 if (str[len - 1] == '\n') str[len - 1] = '\0';
404 debugger_message_view_append (debugger, type, str);
405 g_free (str);
406 break;
407 case '^':
408 if (strncmp(line + 1, "error", 5) == 0)
410 debugger_message_view_append (debugger, IANJUTA_MESSAGE_VIEW_TYPE_ERROR, line + 1);
412 else
414 debugger_message_view_append (debugger, IANJUTA_MESSAGE_VIEW_TYPE_WARNING, line + 1);
416 break;
417 case '@':
418 debugger_message_view_append (debugger, IANJUTA_MESSAGE_VIEW_TYPE_NORMAL, line + 1);
419 break;
420 default:
421 debugger_message_view_append (debugger, IANJUTA_MESSAGE_VIEW_TYPE_NORMAL, line);
422 break;
426 static void
427 debugger_emit_ready (Debugger *debugger)
429 if (!debugger->priv->debugger_is_busy)
431 if (debugger->priv->loading)
433 debugger->priv->starting = FALSE;
434 debugger->priv->loading = FALSE;
435 debugger->priv->exiting = FALSE;
436 debugger->priv->stopping = FALSE;
437 debugger->priv->solib_event = FALSE;
438 g_signal_emit_by_name (debugger->priv->instance, "debugger-ready", IANJUTA_DEBUGGER_PROGRAM_LOADED);
440 else if (debugger->priv->starting)
442 debugger->priv->starting = FALSE;
443 debugger->priv->loading = FALSE;
444 debugger->priv->exiting = FALSE;
445 debugger->priv->stopping = FALSE;
446 debugger->priv->solib_event = FALSE;
447 g_signal_emit_by_name (debugger->priv->instance, "debugger-ready", IANJUTA_DEBUGGER_STARTED);
449 else if (debugger->priv->exiting)
451 debugger->priv->exiting = FALSE;
452 debugger->priv->stopping = FALSE;
453 debugger->priv->solib_event = FALSE;
454 g_signal_emit_by_name (debugger->priv->instance, "debugger-ready", IANJUTA_DEBUGGER_PROGRAM_LOADED);
456 else if (debugger->priv->solib_event)
458 debugger->priv->exiting = FALSE;
459 debugger->priv->stopping = FALSE;
460 debugger->priv->solib_event = FALSE;
461 g_signal_emit_by_name (debugger->priv->instance, "sharedlib-event");
463 else if (debugger->priv->stopping)
465 debugger->priv->exiting = FALSE;
466 debugger->priv->stopping = FALSE;
467 debugger->priv->solib_event = FALSE;
468 g_signal_emit_by_name (debugger->priv->instance, "debugger-ready", IANJUTA_DEBUGGER_PROGRAM_STOPPED);
470 else
472 if (debugger->priv->prog_is_running || debugger->priv->prog_is_attached)
474 g_signal_emit_by_name (debugger->priv->instance, "debugger-ready", IANJUTA_DEBUGGER_PROGRAM_STOPPED);
476 else if (debugger->priv->prog_is_loaded)
478 g_signal_emit_by_name (debugger->priv->instance, "debugger-ready", IANJUTA_DEBUGGER_PROGRAM_LOADED);
480 else
482 g_signal_emit_by_name (debugger->priv->instance, "debugger-ready", IANJUTA_DEBUGGER_STARTED);
488 IAnjutaDebuggerState
489 debugger_get_state (Debugger *debugger)
491 if (debugger->priv->debugger_is_busy)
493 return IANJUTA_DEBUGGER_BUSY;
495 else
497 if (debugger->priv->prog_is_running || debugger->priv->prog_is_attached)
499 return IANJUTA_DEBUGGER_PROGRAM_STOPPED;
501 else if (debugger->priv->prog_is_loaded)
503 return IANJUTA_DEBUGGER_PROGRAM_LOADED;
505 else if (debugger->priv->debugger_is_started)
507 return IANJUTA_DEBUGGER_STARTED;
509 else
511 return IANJUTA_DEBUGGER_STOPPED;
516 static void
517 debugger_clear_buffers (Debugger *debugger)
519 DEBUG_PRINT ("In function: debugger_clear_buffers()");
521 /* Clear the output line buffer */
522 g_string_assign (debugger->priv->stdo_line, "");
523 if (!debugger->priv->current_cmd.keep_result)
524 g_string_assign (debugger->priv->stdo_acc, "");
526 /* Clear the error line buffer */
527 g_string_assign (debugger->priv->stde_line, "");
529 /* Clear CLI output lines */
530 g_list_foreach (debugger->priv->cli_lines, (GFunc)g_free, NULL);
531 g_list_free (debugger->priv->cli_lines);
532 debugger->priv->cli_lines = NULL;
535 static DebuggerCommand *
536 debugger_queue_get_next_command (Debugger *debugger)
538 DebuggerCommand *dc;
540 DEBUG_PRINT ("In function: debugger_get_next_command()");
542 if (debugger->priv->cmd_queqe)
544 dc = g_list_nth_data (debugger->priv->cmd_queqe, 0);
545 debugger->priv->cmd_queqe = g_list_remove (debugger->priv->cmd_queqe, dc);
547 else
548 dc = NULL;
549 return dc;
552 static gboolean
553 debugger_queue_set_next_command (Debugger *debugger)
555 DebuggerCommand *dc;
557 DEBUG_PRINT ("In function: debugger_set_next_command()");
559 dc = debugger_queue_get_next_command (debugger);
560 if (!dc)
562 debugger->priv->current_cmd.cmd = NULL;
563 debugger->priv->current_cmd.parser = NULL;
564 debugger->priv->current_cmd.callback = NULL;
565 debugger->priv->current_cmd.user_data = NULL;
566 debugger->priv->current_cmd.suppress_error = FALSE;
567 debugger->priv->current_cmd.keep_result = FALSE;
569 return FALSE;
571 g_free (debugger->priv->current_cmd.cmd);
572 debugger->priv->current_cmd.cmd = dc->cmd;
573 debugger->priv->current_cmd.parser = dc->parser;
574 debugger->priv->current_cmd.callback = dc->callback;
575 debugger->priv->current_cmd.user_data = dc->user_data;
576 debugger->priv->current_cmd.suppress_error = dc->suppress_error;
577 debugger->priv->current_cmd.keep_result = dc->keep_result;
578 g_free (dc);
580 return TRUE;
583 static void
584 debugger_queue_command (Debugger *debugger, const gchar *cmd,
585 gboolean suppress_error, gboolean keep_result,
586 DebuggerParserFunc parser,
587 IAnjutaDebuggerCallback callback, gpointer user_data)
589 DebuggerCommand *dc;
592 DEBUG_PRINT ("In function: debugger_queue_command (%s)", cmd);
594 dc = g_malloc (sizeof (DebuggerCommand));
595 if (dc)
597 dc->cmd = g_strdup(cmd);
598 dc->parser = parser;
599 dc->callback = callback;
600 dc->user_data = user_data;
601 dc->suppress_error = suppress_error;
602 dc->keep_result = keep_result;
604 debugger->priv->cmd_queqe = g_list_append (debugger->priv->cmd_queqe, dc);
605 debugger_queue_execute_command (debugger);
608 static void
609 debugger_queue_clear (Debugger *debugger)
611 GList *node;
613 DEBUG_PRINT ("In function: debugger_queue_clear()");
615 node = debugger->priv->cmd_queqe;
616 while (node)
618 g_free (((DebuggerCommand *)node->data)->cmd);
619 g_free (node->data);
620 node = g_list_next (node);
622 g_list_free (debugger->priv->cmd_queqe);
623 debugger->priv->cmd_queqe = NULL;
624 g_free (debugger->priv->current_cmd.cmd);
625 debugger->priv->current_cmd.cmd = NULL;
626 debugger->priv->current_cmd.parser = NULL;
627 debugger->priv->current_cmd.callback = NULL;
628 debugger->priv->current_cmd.user_data = NULL;
629 debugger->priv->current_cmd.suppress_error = FALSE;
630 debugger->priv->current_cmd.keep_result = FALSE;
631 debugger_clear_buffers (debugger);
634 static void
635 debugger_execute_command (Debugger *debugger, const gchar *command)
637 gchar *cmd;
639 DEBUG_PRINT ("In function: debugger_execute_command(%s) %d\n",command, debugger->priv->debugger_is_busy);
640 debugger->priv->debugger_is_busy++;
641 debugger->priv->command_output_sent = FALSE;
642 cmd = g_strconcat (command, "\n", NULL);
643 debugger_log_command (debugger, cmd);
644 anjuta_launcher_send_stdin (debugger->priv->launcher, cmd);
645 g_free (cmd);
648 static void
649 debugger_queue_execute_command (Debugger *debugger)
651 DEBUG_PRINT ("In function: debugger_queue_execute_command()");
653 if (!debugger->priv->debugger_is_busy &&
654 !debugger->priv->starting &&
655 g_list_length (debugger->priv->cmd_queqe) >= 1)
657 debugger_clear_buffers (debugger);
658 if (debugger_queue_set_next_command (debugger))
659 debugger_execute_command (debugger, debugger->priv->current_cmd.cmd);
663 static void
664 debugger_load_executable_finish (Debugger *debugger, const GDBMIValue *mi_results,
665 const GList *cli_results, GError *error)
667 DEBUG_PRINT ("Program loaded");
668 debugger->priv->prog_is_loaded = TRUE;
670 g_signal_emit_by_name (debugger->priv->instance, "program-loaded");
673 void
674 debugger_load_executable (Debugger *debugger, const gchar *prog)
676 gchar *command, *dir, *msg;
678 g_return_if_fail (IS_DEBUGGER (debugger));
679 g_return_if_fail (prog != NULL);
681 DEBUG_PRINT ("In function: debugger_load_executable(%s)", prog);
683 if (debugger->priv->output_callback)
685 msg = g_strconcat (_("Loading Executable: "), prog, "\n", NULL);
686 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT, msg,
687 debugger->priv->output_user_data);
688 g_free (msg);
691 command = g_strconcat ("-file-exec-and-symbols ", prog, NULL);
692 dir = g_path_get_dirname (prog);
693 /* TODO
694 anjuta_set_execution_dir(dir);
696 g_free (dir);
697 debugger_queue_command (debugger, command, FALSE, FALSE, debugger_load_executable_finish, NULL, NULL);
698 g_free (command);
699 debugger->priv->starting = TRUE;
700 debugger->priv->terminating = FALSE;
703 void
704 debugger_load_core (Debugger *debugger, const gchar *core)
706 gchar *command, *dir, *msg;
708 g_return_if_fail (IS_DEBUGGER (debugger));
709 g_return_if_fail (core != NULL);
711 DEBUG_PRINT ("In function: debugger_load_core(%s)", core);
713 if (debugger->priv->output_callback)
715 msg = g_strconcat (_("Loading Core: "), core, "\n", NULL);
716 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT, msg,
717 debugger->priv->output_user_data);
718 g_free (msg);
721 command = g_strconcat ("core ", core, NULL);
722 dir = g_path_get_dirname (core);
723 debugger->priv->search_dirs =
724 g_list_prepend (debugger->priv->search_dirs, dir);
725 debugger_queue_command (debugger, command, FALSE, FALSE, NULL, NULL, NULL);
726 g_free (command);
729 /* Set environment
730 *---------------------------------------------------------------------------*/
732 gboolean
733 debugger_set_working_directory (Debugger *debugger, const gchar *directory)
735 gchar *buff;
737 DEBUG_PRINT ("In function: set_working_directory()");
739 g_return_val_if_fail (IS_DEBUGGER (debugger), FALSE);
741 buff = g_strdup_printf ("-environment-cd %s", directory);
742 debugger_queue_command (debugger, buff, FALSE, FALSE, NULL, NULL, NULL);
743 g_free (buff);
745 return TRUE;
748 gboolean
749 debugger_set_environment (Debugger *debugger, gchar **variables)
751 gchar *buff;
752 GList *node;
754 DEBUG_PRINT ("In function: set_environment()");
756 g_return_val_if_fail (IS_DEBUGGER (debugger), FALSE);
758 if (variables != NULL)
760 for (; *variables != NULL; variables++)
762 buff = g_strdup_printf("set environment %s", *variables);
763 debugger_queue_command (debugger, buff, FALSE, FALSE, NULL, NULL, NULL);
764 g_free (buff);
768 return TRUE;
771 gboolean
772 debugger_start (Debugger *debugger, const GList *search_dirs,
773 const gchar *prog, gboolean is_libtool_prog)
775 gchar *command_str, *dir, *tmp, *text, *msg;
776 gchar *exec_dir;
777 gboolean ret;
778 const GList *node;
779 AnjutaLauncher *launcher;
780 GList *dir_list = NULL;
781 gchar *term = NULL;
783 DEBUG_PRINT ("In function: debugger_start(%s) libtool %d", prog == NULL ? "(null)" : prog, is_libtool_prog);
785 if (anjuta_util_prog_is_installed ("gdb", TRUE) == FALSE)
786 return FALSE;
788 debugger_queue_clear (debugger);
790 tmp = g_strconcat (PACKAGE_DATA_DIR, "/", "gdb.init", NULL);
791 if (g_file_test (tmp, G_FILE_TEST_IS_REGULAR) == FALSE)
793 anjuta_util_dialog_error (debugger->priv->parent_win,
794 _("Unable to find: %s.\n"
795 "Unable to initialize debugger.\n"
796 "Make sure Anjuta is installed correctly."),
797 tmp);
798 g_free (tmp);
799 return FALSE;
801 g_free (tmp);
803 /* Prepare source search directories */
804 exec_dir = NULL;
805 if (prog)
806 exec_dir = g_path_get_dirname (prog);
808 if (exec_dir)
810 gchar *quoted_exec_dir = gdb_quote (exec_dir);
811 dir = g_strconcat (" -directory=\"", quoted_exec_dir, "\"", NULL);
812 g_free (quoted_exec_dir);
813 dir_list = g_list_prepend (dir_list, exec_dir);
815 else
817 dir = g_strdup (" ");
820 node = search_dirs;
821 while (node)
823 text = node->data;
824 if (strncmp (text, "file://", 7) == 0)
826 text += 7;
828 else
830 g_warning ("Debugger source search uri '%s' is not a local uri", text);
833 if (text[0] == '/')
835 tmp = g_strconcat (dir, " -directory=", text, NULL);
836 g_free (dir);
837 dir = tmp;
839 dir_list = g_list_prepend (dir_list, g_strdup (text));
841 else
843 g_warning ("Debugger source search dir '%s' is not absolute",
844 text);
846 node = g_list_next (node);
849 /* Now save the dir list. Order is automatically revesed */
850 node = dir_list;
851 while (node)
853 debugger->priv->search_dirs =
854 g_list_prepend (debugger->priv->search_dirs, node->data);
855 node = g_list_next (node);
857 g_list_free (dir_list);
859 if (prog && strlen(prog) > 0)
861 gchar *quoted_prog = gdb_quote (prog);
862 if (exec_dir)
863 chdir (exec_dir);
864 if (is_libtool_prog == FALSE)
866 command_str = g_strdup_printf (GDB_PATH " -f -n -i=mi2 %s %s "
867 "-x %s/gdb.init \"%s\"", dir, term == NULL ? "" : term,
868 PACKAGE_DATA_DIR, quoted_prog);
870 else
872 command_str = g_strdup_printf ("libtool --mode=execute " GDB_PATH
873 " -f -n -i=mi2 %s %s "
874 "-x %s/gdb.init \"%s\"", dir, term == NULL ? "" : term,
875 PACKAGE_DATA_DIR, quoted_prog);
877 g_free (quoted_prog);
879 else
881 if (is_libtool_prog == FALSE)
883 command_str = g_strdup_printf (GDB_PATH " -f -n -i=mi2 %s %s "
884 "-x %s/gdb.init ", term == NULL ? "" : term,
885 dir, PACKAGE_DATA_DIR);
887 else
889 command_str = g_strdup_printf ("libtool --mode=execute " GDB_PATH
890 " -f -n -i=mi2 %s %s -x "
891 "%s/gdb.init ",
892 dir, term == NULL ? "" : term, PACKAGE_DATA_DIR);
895 g_free (dir);
896 g_free (term);
897 debugger->priv->starting = TRUE;
898 debugger->priv->terminating = FALSE;
899 debugger->priv->loading = prog != NULL ? TRUE : FALSE;
900 debugger->priv->debugger_is_busy = 1;
902 /* Prepare for launch. */
903 launcher = debugger->priv->launcher;
904 anjuta_launcher_set_terminate_on_exit (launcher, TRUE);
905 g_signal_connect (G_OBJECT (launcher), "child-exited",
906 G_CALLBACK (on_gdb_terminated), debugger);
907 ret = anjuta_launcher_execute (launcher, command_str,
908 on_gdb_output_arrived, debugger);
910 if (ret)
912 debugger->priv->debugger_is_started = TRUE;
913 debugger->priv->prog_is_loaded = prog != NULL;
915 anjuta_launcher_set_encoding (launcher, "ISO-8859-1");
917 if (debugger->priv->output_callback != NULL)
919 if (ret == TRUE)
921 /* TODO anjuta_update_app_status (TRUE, _("Debugger")); */
922 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
923 _("Getting ready to start debugging "
924 "session...\n"),
925 debugger->priv->output_user_data);
927 if (prog)
929 msg = g_strconcat (_("Loading Executable: "), prog, "\n", NULL);
930 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
931 msg,
932 debugger->priv->output_user_data);
933 g_free (msg);
935 else
937 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
938 _("No executable specified.\n"),
939 debugger->priv->output_user_data);
940 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
941 _("Open an executable or attach "
942 "to a process to start "
943 "debugging.\n"),
944 debugger->priv->output_user_data);
947 else
949 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
950 _("There was an error whilst "
951 "launching the debugger.\n"),
952 debugger->priv->output_user_data);
953 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
954 _("Make sure 'gdb' is installed "
955 "on the system.\n"),
956 debugger->priv->output_user_data);
959 g_free (command_str);
961 return TRUE;
964 static void
965 gdb_stdout_line_arrived (Debugger *debugger, const gchar * chars)
967 gint i = 0;
969 while (chars[i])
971 if (chars[i] == '\n')
973 debugger_stdo_flush (debugger);
975 else
977 g_string_append_c (debugger->priv->stdo_line, chars[i]);
979 i++;
983 static void
984 gdb_stderr_line_arrived (Debugger *debugger, const gchar * chars)
986 gint i;
988 for (i = 0; i < strlen (chars); i++)
990 if (chars[i] == '\n')
991 debugger_stde_flush (debugger);
992 else
993 g_string_append_c (debugger->priv->stde_line, chars[i]);
997 static void
998 on_gdb_output_arrived (AnjutaLauncher *launcher,
999 AnjutaLauncherOutputType output_type,
1000 const gchar *chars, gpointer data)
1002 Debugger *debugger = DEBUGGER (data);
1003 DEBUG_PRINT ("on gdb output arrived");
1005 /* Do not emit signal when the debugger is destroyed */
1006 if (debugger->priv->instance == NULL) return;
1008 switch (output_type)
1010 case ANJUTA_LAUNCHER_OUTPUT_STDERR:
1011 gdb_stderr_line_arrived (debugger, chars);
1012 break;
1013 case ANJUTA_LAUNCHER_OUTPUT_STDOUT:
1014 gdb_stdout_line_arrived (debugger, chars);
1015 break;
1016 default:
1017 break;
1021 static void
1022 debugger_handle_post_execution (Debugger *debugger)
1024 switch (debugger->priv->post_execution_flag)
1026 case DEBUGGER_NONE:
1027 break;
1028 case DEBUGGER_EXIT:
1029 DEBUG_PRINT ("debugger stop in handle post execution\n");
1030 debugger_stop (debugger);
1031 break;
1032 case DEBUGGER_RERUN_PROGRAM:
1033 DEBUG_PRINT ("debugger run in handle post execution\n");
1034 debugger_run (debugger);
1035 break;
1036 default:
1037 g_warning ("Execution should not reach here");
1041 static void
1042 debugger_process_frame (Debugger *debugger, const GDBMIValue *val)
1044 const GDBMIValue *file, *line, *frame, *addr, *fullname, *thread;
1045 const gchar *file_str = NULL;
1046 guint line_num = 0;
1047 gulong addr_num = 0;
1049 g_return_if_fail (val != NULL);
1051 thread = gdbmi_value_hash_lookup (val, "thread-id");
1052 if (thread)
1054 debugger->priv->current_thread = strtoul (gdbmi_value_literal_get (thread), NULL, 0);
1056 debugger->priv->current_frame = 0;
1058 frame = gdbmi_value_hash_lookup (val, "frame");
1059 if (frame)
1061 fullname = gdbmi_value_hash_lookup (frame, "fullname");
1062 if (fullname)
1064 file_str = gdbmi_value_literal_get (fullname);
1065 if (*file_str == '\0') file_str = NULL;
1067 else
1069 file = gdbmi_value_hash_lookup (frame, "file");
1070 if (file)
1072 file_str = gdbmi_value_literal_get (file);
1073 if (*file_str == '\0') file_str = NULL;
1077 if (file_str != NULL)
1079 line = gdbmi_value_hash_lookup (frame, "line");
1080 if (line)
1082 line_num = strtoul (gdbmi_value_literal_get (line), NULL, 0);
1086 addr = gdbmi_value_hash_lookup (frame, "addr");
1087 if (addr)
1089 addr_num = strtoul (gdbmi_value_literal_get (addr), NULL, 0);
1091 debugger_program_moved (debugger, file_str, line_num, addr_num);
1095 static GError*
1096 gdb_parse_error (Debugger *debugger, const GDBMIValue *mi_results)
1098 const GDBMIValue *message;
1099 const gchar *literal;
1100 guint code = IANJUTA_DEBUGGER_UNKNOWN_ERROR;
1102 message = gdbmi_value_hash_lookup (mi_results, "msg");
1103 literal = gdbmi_value_literal_get (message);
1105 if ((mi_results != NULL)
1106 && ((message = gdbmi_value_hash_lookup (mi_results, "msg")) != NULL)
1107 && ((literal = gdbmi_value_literal_get (message)) != NULL)
1108 && (*literal != '\0'))
1110 code = gdb_match_error (literal);
1111 DEBUG_PRINT ("error code %d", code);
1113 else
1115 /* No error message */
1116 literal = "Error without a message";
1119 return g_error_new_literal (IANJUTA_DEBUGGER_ERROR, code, literal);
1123 /* Parsing output
1124 *---------------------------------------------------------------------------*/
1126 static void
1127 debugger_parse_output (Debugger *debugger)
1129 gchar *line;
1131 line = debugger->priv->stdo_line->str;
1133 if (line[0] == '\032' && line[1] == '\032')
1135 gchar *filename;
1136 guint lineno;
1138 gdb_util_parse_error_line (&(line[2]), &filename, &lineno);
1139 if (filename)
1141 debugger_program_moved (debugger, filename, lineno, 0);
1142 g_free (filename);
1145 else
1147 gchar *proper_msg;
1148 gsize len;
1150 len = strlen (line);
1151 if (line[1] == '\"' && line [strlen(line) - 1] == '\"')
1153 line[1] = line[0];
1154 /* Reserve space for an additional carriage return */
1155 line[strlen(line) - 1] = ' ';
1156 proper_msg = g_strcompress (line + 1);
1157 len = strlen (proper_msg) - 1;
1158 proper_msg[len] = '\0';
1160 else
1162 /* Reserve space for an additional carriage return */
1163 proper_msg = g_strndup (line, len + 1);
1166 if (strcmp(proper_msg, "~Stopped due to shared library event\n") == 0)
1168 /* Recognize a solib event */
1169 debugger->priv->solib_event = TRUE;
1170 g_free (proper_msg);
1172 else if (debugger->priv->current_cmd.parser)
1174 /* Save GDB CLI output */
1175 debugger->priv->cli_lines = g_list_prepend (debugger->priv->cli_lines,
1176 proper_msg);
1178 else
1180 /* Discard CLI output */
1181 g_free (proper_msg);
1186 static void
1187 debugger_parse_stopped (Debugger *debugger)
1189 gchar *line = debugger->priv->stdo_line->str;
1192 if (!debugger->priv->solib_event)
1194 gboolean program_exited = FALSE;
1195 GDBMIValue *val;
1197 /* Check if program has exited */
1198 val = gdbmi_value_parse (line);
1199 if (val)
1201 const GDBMIValue *reason;
1202 const gchar *str = NULL;
1204 debugger_process_frame (debugger, val);
1206 reason = gdbmi_value_hash_lookup (val, "reason");
1207 if (reason)
1208 str = gdbmi_value_literal_get (reason);
1210 if (str && (strncmp (str, "exited", 6) == 0))
1212 program_exited = TRUE;
1215 /* Emit signal received if necessary */
1216 if (str && strcmp (str, "exited-signalled") == 0)
1218 const GDBMIValue *signal_name, *signal_meaning;
1219 const gchar *signal_str, *signal_meaning_str;
1221 signal_name = gdbmi_value_hash_lookup (val, "signal-name");
1222 signal_str = gdbmi_value_literal_get (signal_name);
1223 signal_meaning = gdbmi_value_hash_lookup (val, "signal-meaning");
1224 signal_meaning_str = gdbmi_value_literal_get (signal_meaning);
1225 g_signal_emit_by_name (debugger->priv->instance, "signal-received", signal_str, signal_meaning_str);
1227 else if (str && strcmp (str, "signal-received") == 0)
1229 const GDBMIValue *signal_name, *signal_meaning;
1230 const gchar *signal_str, *signal_meaning_str;
1232 signal_name = gdbmi_value_hash_lookup (val, "signal-name");
1233 signal_str = gdbmi_value_literal_get (signal_name);
1234 signal_meaning = gdbmi_value_hash_lookup (val, "signal-meaning");
1235 signal_meaning_str = gdbmi_value_literal_get (signal_meaning);
1237 g_signal_emit_by_name (debugger->priv->instance, "signal-received", signal_str, signal_meaning_str);
1240 if (debugger->priv->output_callback)
1242 if (str && strcmp (str, "exited-normally") == 0)
1244 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1245 _("Program exited normally\n"),
1246 debugger->priv->output_user_data);
1248 else if (str && strcmp (str, "exited") == 0)
1250 const GDBMIValue *errcode;
1251 const gchar *errcode_str;
1252 gchar *msg;
1254 errcode = gdbmi_value_hash_lookup (val, "exit-code");
1255 errcode_str = gdbmi_value_literal_get (errcode);
1256 msg = g_strdup_printf (_("Program exited with error code %s\n"),
1257 errcode_str);
1258 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1259 msg, debugger->priv->output_user_data);
1260 g_free (msg);
1262 else if (str && strcmp (str, "breakpoint-hit") == 0)
1264 const GDBMIValue *bkptno;
1265 const gchar *bkptno_str;
1266 gchar *msg;
1268 bkptno = gdbmi_value_hash_lookup (val, "bkptno");
1269 bkptno_str = gdbmi_value_literal_get (bkptno);
1270 /* The program has reached one breakpoint and will stop */
1271 msg = g_strdup_printf (_("Breakpoint number %s hit\n"),
1272 bkptno_str);
1273 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1274 msg, debugger->priv->output_user_data);
1275 g_free (msg);
1277 else if (str && strcmp (str, "function-finished") == 0)
1279 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1280 _("Function finished\n"),
1281 debugger->priv->output_user_data);
1283 else if (str && strcmp (str, "end-stepping-range") == 0)
1285 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1286 _("Stepping finished\n"),
1287 debugger->priv->output_user_data);
1289 else if (str && strcmp (str, "location-reached") == 0)
1291 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1292 _("Location reached\n"),
1293 debugger->priv->output_user_data);
1298 if (program_exited)
1300 debugger->priv->prog_is_running = FALSE;
1301 debugger->priv->prog_is_attached = FALSE;
1302 debugger_handle_post_execution (debugger);
1303 debugger->priv->exiting = TRUE;
1305 else
1307 // g_signal_emit_by_name (debugger->priv->instance, "program-stopped");
1308 debugger->priv->stopping = TRUE;
1311 debugger->priv->cli_lines = g_list_reverse (debugger->priv->cli_lines);
1312 if ((debugger->priv->current_cmd.cmd != NULL) &&
1313 (debugger->priv->current_cmd.parser != NULL))
1315 debugger->priv->current_cmd.parser (debugger, val,
1316 debugger->priv->cli_lines, FALSE);
1317 debugger->priv->command_output_sent = TRUE;
1318 DEBUG_PRINT ("In function: Sending output...");
1321 if (val)
1322 gdbmi_value_free (val);
1326 static void
1327 debugger_parse_prompt (Debugger *debugger)
1329 /* If the parser has not yet been called, call it now. */
1330 if (debugger->priv->command_output_sent == FALSE &&
1331 debugger->priv->current_cmd.parser)
1333 debugger->priv->current_cmd.parser (debugger, NULL,
1334 debugger->priv->cli_lines, FALSE);
1335 debugger->priv->command_output_sent = TRUE;
1338 debugger->priv->debugger_is_busy--;
1339 debugger_queue_execute_command (debugger); /* Next command. Go. */
1340 debugger_emit_ready (debugger);
1343 static gboolean
1344 parse_breakpoint (IAnjutaDebuggerBreakpointItem* bp, const GDBMIValue *brkpnt)
1346 const GDBMIValue *literal;
1347 const gchar* value;
1349 memset (bp, 0, sizeof (*bp));
1351 literal = gdbmi_value_hash_lookup (brkpnt, "number");
1352 if (literal)
1354 value = gdbmi_value_literal_get (literal);
1355 bp->id = strtoul (value, NULL, 10);
1358 literal = gdbmi_value_hash_lookup (brkpnt, "fullname");
1359 if (literal == NULL) literal = gdbmi_value_hash_lookup (brkpnt, "file");
1360 if (literal)
1362 value = gdbmi_value_literal_get (literal);
1363 bp->file = (gchar *)value;
1366 literal = gdbmi_value_hash_lookup (brkpnt, "line");
1367 if (literal)
1369 value = gdbmi_value_literal_get (literal);
1370 bp->line = strtoul (value, NULL, 10);
1371 bp->type |= IANJUTA_DEBUGGER_BREAKPOINT_ON_LINE;
1374 literal = gdbmi_value_hash_lookup (brkpnt, "type");
1375 if (literal)
1377 value = gdbmi_value_literal_get (literal);
1380 literal = gdbmi_value_hash_lookup (brkpnt, "disp");
1381 if (literal)
1383 value = gdbmi_value_literal_get (literal);
1384 if (strcmp (value, "keep") == 0)
1386 bp->type |= IANJUTA_DEBUGGER_BREAKPOINT_WITH_TEMPORARY;
1387 bp->temporary = FALSE;
1389 else if ((strcmp (value, "nokeep") == 0) || (strcmp (value, "del") == 0))
1391 bp->type |= IANJUTA_DEBUGGER_BREAKPOINT_WITH_TEMPORARY;
1392 bp->temporary = TRUE;
1396 literal = gdbmi_value_hash_lookup (brkpnt, "enabled");
1397 if (literal)
1399 value = gdbmi_value_literal_get (literal);
1400 if (strcmp (value, "n") == 0)
1402 bp->type |= IANJUTA_DEBUGGER_BREAKPOINT_WITH_ENABLE;
1403 bp->enable = FALSE;
1405 else if (strcmp (value, "y") == 0)
1407 bp->type |= IANJUTA_DEBUGGER_BREAKPOINT_WITH_ENABLE;
1408 bp->enable = TRUE;
1412 literal = gdbmi_value_hash_lookup (brkpnt, "addr");
1413 if (literal)
1415 value = gdbmi_value_literal_get (literal);
1416 bp->address = strtoul (value, NULL, 16);
1417 bp->type |= IANJUTA_DEBUGGER_BREAKPOINT_ON_ADDRESS;
1420 literal = gdbmi_value_hash_lookup (brkpnt, "func");
1421 if (literal)
1423 value = gdbmi_value_literal_get (literal);
1424 bp->function = (gchar *)value;
1425 bp->type |= IANJUTA_DEBUGGER_BREAKPOINT_ON_FUNCTION;
1428 literal = gdbmi_value_hash_lookup (brkpnt, "times");
1429 if (literal)
1431 value = gdbmi_value_literal_get (literal);
1432 bp->times = strtoul (value, NULL, 10);
1433 bp->type |= IANJUTA_DEBUGGER_BREAKPOINT_WITH_TIME;
1435 DEBUG_PRINT("parse time %d", bp->times);
1437 literal = gdbmi_value_hash_lookup (brkpnt, "ignore");
1438 if (literal)
1440 value = gdbmi_value_literal_get (literal);
1441 bp->ignore = strtoul (value, NULL, 10);
1442 bp->type |= IANJUTA_DEBUGGER_BREAKPOINT_WITH_IGNORE;
1445 literal = gdbmi_value_hash_lookup (brkpnt, "cond");
1446 if (literal)
1448 value = gdbmi_value_literal_get (literal);
1449 bp->condition = (gchar *)value;
1450 bp->type |= IANJUTA_DEBUGGER_BREAKPOINT_WITH_CONDITION;
1453 return TRUE;
1456 static void
1457 debugger_stdo_flush (Debugger *debugger)
1459 gchar *line;
1461 line = debugger->priv->stdo_line->str;
1463 #ifdef DEBUG
1464 DEBUG_PRINT ("GDB:< %s", line);
1465 #else
1466 if (debugger->priv->gdb_log) g_message ("GDB:< %s", line);
1467 #endif
1468 debugger_log_output (debugger, line);
1469 if (strlen (line) == 0)
1471 return;
1473 if (strncasecmp (line, "^error", 6) == 0)
1475 /* GDB reported error */
1476 if ((debugger->priv->current_cmd.keep_result) || (debugger->priv->stdo_acc->len != 0))
1478 /* Keep result for next command */
1480 if (debugger->priv->stdo_acc->len == 0)
1482 g_string_append (debugger->priv->stdo_acc, line);
1484 else
1486 line = strchr (line, ',');
1487 if (line != NULL)
1489 g_string_append (debugger->priv->stdo_acc, line);
1492 line = debugger->priv->stdo_acc->str;
1495 GDBMIValue *val = gdbmi_value_parse (line);
1496 GError *error;
1498 error = gdb_parse_error (debugger, val);
1500 /* Trap state error */
1501 if ((error != NULL) && (error->code == IANJUTA_DEBUGGER_PROGRAM_NOT_FOUND))
1503 debugger->priv->prog_is_running = FALSE;
1504 debugger->priv->prog_is_attached = FALSE;
1505 debugger->priv->prog_is_loaded = FALSE;
1508 if (debugger->priv->current_cmd.parser != NULL)
1510 debugger->priv->current_cmd.parser (debugger, val, debugger->priv->cli_lines, error);
1511 debugger->priv->command_output_sent = TRUE; }
1512 DEBUG_PRINT("GDB: error %s", error->message);
1513 /*else
1515 anjuta_util_dialog_error (debugger->priv->parent_win, "%s", error->message);
1517 g_error_free (error);
1518 gdbmi_value_free (val);
1520 else if (strncasecmp(line, "^running", 8) == 0)
1522 /* Program started running */
1523 debugger->priv->prog_is_running = TRUE;
1524 /* debugger->priv->skip_next_prompt = TRUE; Replaced by debugger_is_busy++ */
1525 debugger->priv->debugger_is_busy++;
1526 g_signal_emit_by_name (debugger->priv->instance, "program-running");
1528 else if (strncasecmp (line, "*stopped", 8) == 0)
1530 /* Process has stopped */
1531 debugger_parse_stopped (debugger);
1533 else if (strncasecmp (line, "^done", 5) == 0)
1535 if ((debugger->priv->current_cmd.keep_result) || (debugger->priv->stdo_acc->len != 0))
1537 /* Keep result for next command */
1539 if (debugger->priv->stdo_acc->len == 0)
1541 g_string_append (debugger->priv->stdo_acc, line);
1543 else
1545 line = strchr (line, ',');
1546 if (line != NULL)
1548 g_string_append (debugger->priv->stdo_acc, line);
1551 line = debugger->priv->stdo_acc->str;
1554 if (!debugger->priv->current_cmd.keep_result)
1556 /* GDB command has reported output */
1557 GDBMIValue *val = gdbmi_value_parse (line);
1559 debugger->priv->cli_lines = g_list_reverse (debugger->priv->cli_lines);
1560 if ((debugger->priv->current_cmd.cmd != NULL) &&
1561 (debugger->priv->current_cmd.parser != NULL))
1563 debugger->priv->current_cmd.parser (debugger, val,
1564 debugger->priv->cli_lines, FALSE);
1565 debugger->priv->command_output_sent = TRUE;
1566 DEBUG_PRINT ("In function: Sending output...");
1568 else /* if (val) */
1570 /*g_signal_emit_by_name (debugger, "results-arrived",
1571 debugger->priv->current_cmd.cmd, val);*/
1574 if (val)
1576 /*debugger_process_frame (debugger, val);*/
1577 gdbmi_value_free (val);
1581 if (!debugger->priv->current_cmd.keep_result)
1583 g_string_assign (debugger->priv->stdo_acc, "");
1586 else if (strncasecmp (line, GDB_PROMPT, strlen (GDB_PROMPT)) == 0)
1588 debugger_parse_prompt (debugger);
1590 else
1592 debugger_parse_output (debugger);
1595 /* Clear the line buffer */
1596 g_string_assign (debugger->priv->stdo_line, "");
1599 void
1600 debugger_stde_flush (Debugger *debugger)
1602 if ((debugger->priv->output_callback) && (strlen (debugger->priv->stde_line->str) > 0))
1604 debugger->priv->output_callback (IANJUTA_DEBUGGER_ERROR_OUTPUT,
1605 debugger->priv->stde_line->str,
1606 debugger->priv->output_user_data);
1608 /* Clear the line buffer */
1609 g_string_assign (debugger->priv->stde_line, "");
1612 static void
1613 on_gdb_terminated (AnjutaLauncher *launcher,
1614 gint child_pid, gint status, gulong t,
1615 gpointer data)
1617 Debugger *debugger = DEBUGGER (data);
1618 GError *err = NULL;
1620 g_signal_handlers_disconnect_by_func (G_OBJECT (launcher),
1621 G_CALLBACK (on_gdb_terminated),
1622 debugger);
1624 DEBUG_PRINT ("In function: gdb_terminated()");
1626 /* Clear the command queue & Buffer */
1627 debugger_clear_buffers (debugger);
1629 /* Good Bye message */
1630 /*if (!debugger->priv->terminating)
1632 anjuta_util_dialog_error (debugger->priv->parent_win,
1633 _("gdb terminated unexpectedly with status %d\n"), status);
1635 debugger->priv->prog_is_running = FALSE;
1636 debugger->priv->prog_is_attached = FALSE;
1637 debugger->priv->prog_is_loaded = FALSE;
1638 debugger->priv->debugger_is_busy = 0;
1639 debugger->priv->skip_next_prompt = FALSE;
1640 if (!debugger->priv->terminating)
1642 err = g_error_new (IANJUTA_DEBUGGER_ERROR,
1643 IANJUTA_DEBUGGER_OTHER_ERROR,
1644 "gdb terminated with status %d", status);
1646 debugger->priv->terminating = FALSE;
1647 debugger->priv->debugger_is_started = FALSE;
1648 if (debugger->priv->instance)
1650 g_signal_emit_by_name (debugger->priv->instance, "debugger-stopped", err);
1652 if (err != NULL) g_error_free (err);
1655 static void
1656 debugger_stop_real (Debugger *debugger)
1658 DEBUG_PRINT ("In function: debugger_stop_real()");
1660 /* if program is attached - detach from it before quiting */
1661 if (debugger->priv->prog_is_attached == TRUE)
1663 debugger_detach_process(debugger);
1666 debugger->priv->terminating = TRUE;
1667 debugger_queue_command (debugger, "-gdb-exit", FALSE, FALSE, NULL, NULL, NULL);
1670 gboolean
1671 debugger_stop (Debugger *debugger)
1673 #if 0
1674 gboolean ret = TRUE;
1676 if (debugger->priv->prog_is_running == TRUE)
1678 GtkWidget *dialog;
1679 gchar *mesg;
1681 if (debugger->priv->prog_is_attached == TRUE)
1682 mesg = _("The program is attached.\n"
1683 "Do you still want to stop the debugger?");
1684 else
1685 mesg = _("The program is running.\n"
1686 "Do you still want to stop the debugger?");
1687 dialog = gtk_message_dialog_new (debugger->priv->parent_win,
1688 GTK_DIALOG_DESTROY_WITH_PARENT,
1689 GTK_MESSAGE_QUESTION,
1690 GTK_BUTTONS_NONE, mesg);
1691 gtk_dialog_add_buttons (GTK_DIALOG (dialog),
1692 GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
1693 GTK_STOCK_STOP, GTK_RESPONSE_YES,
1694 NULL);
1695 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_YES)
1696 debugger_stop_real (debugger);
1697 else
1698 ret = FALSE;
1699 gtk_widget_destroy (dialog);
1701 else
1702 debugger_stop_real (debugger);
1703 return ret;
1704 #endif
1705 debugger_stop_real (debugger);
1707 return TRUE;
1710 gboolean
1711 debugger_abort (Debugger *debugger)
1713 DEBUG_PRINT ("In function: debugger_abort()");
1715 /* Stop inferior */
1716 if ((debugger->priv->prog_is_attached == FALSE) && (debugger->priv->inferior_pid != 0))
1718 kill (debugger->priv->inferior_pid, SIGTERM);
1719 debugger->priv->inferior_pid = 0;
1722 /* Stop gdb */
1723 debugger->priv->terminating = TRUE;
1724 g_signal_handlers_disconnect_by_func (G_OBJECT (debugger->priv->launcher), G_CALLBACK (on_gdb_terminated), debugger);
1725 anjuta_launcher_reset (debugger->priv->launcher);
1727 /* Free memory */
1728 debugger_queue_clear (debugger);
1729 g_list_foreach (debugger->priv->search_dirs, (GFunc)g_free, NULL);
1730 g_list_free (debugger->priv->search_dirs);
1731 debugger->priv->search_dirs = NULL;
1733 /* Emit signal, state of the debugger must be DEBUGGER_STOPPED */
1734 debugger->priv->prog_is_running = FALSE;
1735 debugger->priv->prog_is_attached = FALSE;
1736 debugger->priv->inferior_pid = 0;
1737 debugger->priv->prog_is_loaded = FALSE;
1738 debugger->priv->debugger_is_busy = 0;
1739 debugger->priv->debugger_is_started = FALSE;
1740 if (debugger->priv->instance != NULL)
1742 g_signal_emit_by_name (debugger->priv->instance, "debugger-stopped", NULL);
1743 debugger->priv->instance = NULL;
1746 return TRUE;
1749 gchar*
1750 debugger_get_source_path (Debugger *debugger, const gchar *file)
1752 GList *node;
1753 gchar *path = NULL;
1755 if (g_path_is_absolute (file))
1756 return g_strdup (file);
1758 node = debugger->priv->search_dirs;
1759 while (node)
1761 path = g_build_filename (node->data, file, NULL);
1762 if (g_file_test (path, G_FILE_TEST_EXISTS))
1763 break;
1764 g_free (path);
1765 path = NULL;
1766 node = g_list_next (node);
1769 if (path == NULL)
1771 /* The file could be found nowhere. Use current directory */
1772 gchar *cwd;
1773 cwd = g_get_current_dir ();
1774 path = g_build_filename (cwd, file, NULL);
1775 g_free (cwd);
1777 return path;
1780 void
1781 debugger_set_output_callback (Debugger *debugger, IAnjutaDebuggerOutputCallback callback, gpointer user_data)
1783 debugger->priv->output_callback = callback;
1784 debugger->priv->output_user_data = user_data;
1787 void
1788 debugger_program_moved (Debugger *debugger, const gchar *file,
1789 gint line, gulong address)
1791 gchar *src_path;
1793 if ((file != NULL) && (*file != G_DIR_SEPARATOR))
1795 src_path = debugger_get_source_path (debugger, file);
1796 g_signal_emit_by_name (debugger->priv->instance, "program-moved", debugger->priv->inferior_pid, debugger->priv->current_thread, address, src_path, line);
1797 g_free (src_path);
1799 else
1801 g_signal_emit_by_name (debugger->priv->instance, "program-moved", debugger->priv->inferior_pid, debugger->priv->current_thread, address, file, line);
1805 static void
1806 debugger_info_program_finish (Debugger *debugger, const GDBMIValue *mi_results,
1807 const GList *cli_results, GError *error)
1809 DEBUG_PRINT ("In function: debugger_info_program()");
1811 /* Hack: find message string giving inferior pid */
1812 while (cli_results != NULL)
1814 gchar* child_proc;
1816 child_proc = strstr(cli_results->data, " child process ");
1817 if (child_proc != NULL)
1819 debugger->priv->inferior_pid = strtoul (child_proc + 15, NULL, 10);
1820 break;
1822 cli_results = g_list_next (cli_results);
1826 void
1827 debugger_start_program (Debugger *debugger, const gchar* args, const gchar* tty, gboolean stop)
1829 gchar *cmd;
1831 DEBUG_PRINT ("In function: debugger_start_program()");
1833 g_return_if_fail (IS_DEBUGGER (debugger));
1834 g_return_if_fail (debugger->priv->prog_is_running == FALSE);
1836 /* Without a terminal, the output of the debugged program
1837 * are lost */
1838 if (tty)
1840 cmd = g_strdup_printf ("-inferior-tty-set %s", tty);
1841 debugger_queue_command (debugger, cmd, FALSE, FALSE, NULL, NULL, NULL);
1842 g_free (cmd);
1845 debugger->priv->inferior_pid = 0;
1846 if (stop)
1848 debugger_queue_command (debugger, "-break-insert -t main", FALSE, FALSE, NULL, NULL, NULL);
1850 if (args && (*args))
1852 cmd = g_strconcat ("-exec-arguments ", args, NULL);
1853 debugger_queue_command (debugger, cmd, FALSE, FALSE, NULL, NULL, NULL);
1854 g_free (cmd);
1857 debugger_queue_command (debugger, "-exec-run", FALSE, FALSE, NULL, NULL, NULL);
1859 /* Get pid of program on next stop */
1860 debugger_queue_command (debugger, "info program", FALSE, FALSE, debugger_info_program_finish, NULL, NULL);
1861 debugger->priv->post_execution_flag = DEBUGGER_NONE;
1864 static void
1865 debugger_attach_process_finish (Debugger *debugger, const GDBMIValue *mi_results,
1866 const GList *cli_results, GError *error)
1868 DEBUG_PRINT ("Program attach finished");
1869 if (debugger->priv->output_callback)
1871 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1872 _("Program attached\n"),
1873 debugger->priv->output_user_data);
1875 debugger->priv->prog_is_attached = TRUE;
1876 debugger->priv->prog_is_running = TRUE;
1877 /* It is not really a shared lib event, but it allows to restart
1878 * the program after setting breakpoints. It is better to restart
1879 * it because we don't have the normal stop frame that tell where
1880 * the program is stopped */
1881 debugger->priv->solib_event = TRUE;
1884 static void
1885 debugger_attach_process_real (Debugger *debugger, pid_t pid)
1887 gchar *buff;
1889 DEBUG_PRINT ("In function: debugger_attach_process_real()");
1891 if (debugger->priv->output_callback)
1893 buff = g_strdup_printf (_("Attaching to process: %d...\n"), pid);
1894 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1895 buff, debugger->priv->output_user_data);
1896 g_free (buff);
1899 debugger->priv->inferior_pid = pid;
1900 buff = g_strdup_printf ("attach %d", pid);
1901 debugger_queue_command (debugger, buff, FALSE, FALSE,
1902 debugger_attach_process_finish, NULL, NULL);
1903 g_free (buff);
1906 void
1907 debugger_attach_process (Debugger *debugger, pid_t pid)
1909 DEBUG_PRINT ("In function: debugger_attach_process()");
1911 g_return_if_fail (IS_DEBUGGER (debugger));
1913 if (debugger->priv->prog_is_running == TRUE)
1915 // TODO: Dialog to be made HIG compliant.
1916 gchar *mesg;
1917 GtkWidget *dialog;
1919 mesg = _("A process is already running.\n"
1920 "Would you like to terminate it and attach the new process?"),
1921 dialog = gtk_message_dialog_new (debugger->priv->parent_win,
1922 GTK_DIALOG_DESTROY_WITH_PARENT,
1923 GTK_MESSAGE_QUESTION,
1924 GTK_BUTTONS_YES_NO, mesg);
1925 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_YES)
1927 debugger_stop_program (debugger);
1928 debugger_attach_process_real (debugger, pid);
1930 gtk_widget_destroy (dialog);
1932 else if (getpid () == pid ||
1933 anjuta_launcher_get_child_pid (debugger->priv->launcher) == pid)
1935 anjuta_util_dialog_error (debugger->priv->parent_win,
1936 _("Anjuta is unable to attach to itself."));
1937 return;
1939 else
1940 debugger_attach_process_real (debugger, pid);
1943 void
1944 debugger_restart_program (Debugger *debugger)
1946 DEBUG_PRINT ("In function: debugger_restart_program()");
1948 g_return_if_fail (debugger->priv->prog_is_attached == FALSE);
1951 debugger->priv->post_execution_flag = DEBUGGER_RERUN_PROGRAM;
1952 debugger_stop_program (debugger);
1954 debugger_put_cmd_in_queqe ("tbreak main", DB_CMD_NONE, NULL, NULL);
1955 debugger_put_cmd_in_queqe ("run >/dev/null 2>/dev/null", DB_CMD_ALL,
1956 NULL, NULL);
1957 debugger_put_cmd_in_queqe ("info program", DB_CMD_NONE,
1958 on_debugger_update_prog_status, NULL);
1959 debugger_put_cmd_in_queqe ("continue", DB_CMD_NONE, NULL, NULL);
1960 debugger_execute_cmd_in_queqe ();
1964 void
1965 debugger_stop_program (Debugger *debugger)
1967 DEBUG_PRINT ("In function: debugger_stop_program()");
1969 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
1971 if (debugger->priv->prog_is_attached == TRUE)
1973 debugger_detach_process (debugger);
1975 else
1977 /* FIXME: Why doesn't -exec-abort work??? */
1978 /* debugger_queue_command (debugger, "-exec-abort", NULL, NULL); */
1979 debugger_queue_command (debugger, "kill", FALSE, FALSE, NULL, NULL, NULL);
1980 debugger->priv->prog_is_running = FALSE;
1981 debugger->priv->prog_is_attached = FALSE;
1982 g_signal_emit_by_name (debugger->priv->instance, "program-exited");
1983 if (debugger->priv->output_callback)
1985 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1986 _("Program terminated\n"),
1987 debugger->priv->output_user_data);
1989 debugger_handle_post_execution (debugger);
1993 static void
1994 debugger_detach_process_finish (Debugger *debugger, const GDBMIValue *mi_results,
1995 const GList *cli_results, GError *error)
1997 DEBUG_PRINT ("Program detach finished");
1998 if (debugger->priv->output_callback)
2000 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
2001 _("Program detached\n"),
2002 debugger->priv->output_user_data);
2004 debugger->priv->inferior_pid = 0;
2005 debugger->priv->prog_is_attached = FALSE;
2006 debugger->priv->prog_is_running = FALSE;
2007 g_signal_emit_by_name (debugger->priv->instance, "program-exited");
2010 void
2011 debugger_detach_process (Debugger *debugger)
2013 gchar *buff;
2015 DEBUG_PRINT ("In function: debugger_detach_process()");
2017 g_return_if_fail (debugger->priv->prog_is_attached == TRUE);
2019 if (debugger->priv->output_callback)
2021 buff = g_strdup_printf (_("Detaching the process...\n"));
2022 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
2023 buff, debugger->priv->output_user_data);
2024 g_free (buff);
2027 debugger_queue_command (debugger, "detach", FALSE, FALSE,
2028 debugger_detach_process_finish, NULL, NULL);
2031 void
2032 debugger_interrupt (Debugger *debugger)
2034 DEBUG_PRINT ("In function: debugger_interrupt()");
2036 g_return_if_fail (IS_DEBUGGER (debugger));
2037 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
2039 if (debugger->priv->output_callback)
2041 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
2042 _("Interrupting the process\n"),
2043 debugger->priv->output_user_data);
2046 if (debugger->priv->inferior_pid == 0)
2048 /* In case we do not have the inferior pid, send signal to gdb */
2049 anjuta_launcher_signal (debugger->priv->launcher, SIGINT);
2051 else
2053 /* Send signal directly to inferior */
2054 kill (debugger->priv->inferior_pid, SIGINT);
2056 //g_signal_emit_by_name (debugger->priv->instance, "program-running");
2059 void
2060 debugger_run (Debugger *debugger)
2062 DEBUG_PRINT ("In function: debugger_run()");
2064 g_return_if_fail (IS_DEBUGGER (debugger));
2065 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
2067 /* Program running - continue */
2068 debugger_queue_command (debugger, "-exec-continue", FALSE, FALSE, NULL, NULL, NULL);
2071 void
2072 debugger_step_in (Debugger *debugger)
2074 DEBUG_PRINT ("In function: debugger_step_in()");
2076 g_return_if_fail (IS_DEBUGGER (debugger));
2077 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
2079 debugger_queue_command (debugger, "-exec-step", FALSE, FALSE, NULL, NULL, NULL);
2082 void
2083 debugger_step_over (Debugger *debugger)
2085 DEBUG_PRINT ("In function: debugger_step_over()");
2087 g_return_if_fail (IS_DEBUGGER (debugger));
2088 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
2090 debugger_queue_command (debugger, "-exec-next", FALSE, FALSE, NULL, NULL, NULL);
2093 void
2094 debugger_step_out (Debugger *debugger)
2096 DEBUG_PRINT ("In function: debugger_step_out()");
2098 g_return_if_fail (IS_DEBUGGER (debugger));
2099 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
2101 debugger_queue_command (debugger, "-exec-finish", FALSE, FALSE, NULL, NULL, NULL);
2104 void
2105 debugger_stepi_in (Debugger *debugger)
2107 DEBUG_PRINT ("In function: debugger_step_in()");
2109 g_return_if_fail (IS_DEBUGGER (debugger));
2110 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
2112 debugger_queue_command (debugger, "-exec-step-instruction", FALSE, FALSE, NULL, NULL, NULL);
2115 void
2116 debugger_stepi_over (Debugger *debugger)
2118 DEBUG_PRINT ("In function: debugger_step_over()");
2120 g_return_if_fail (IS_DEBUGGER (debugger));
2121 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
2123 debugger_queue_command (debugger, "-exec-next-instruction", FALSE, FALSE, NULL, NULL, NULL);
2126 void
2127 debugger_run_to_location (Debugger *debugger, const gchar *loc)
2129 gchar *buff;
2131 DEBUG_PRINT ("In function: debugger_run_to_location()");
2133 g_return_if_fail (IS_DEBUGGER (debugger));
2134 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
2136 buff = g_strdup_printf ("-exec-until %s", loc);
2137 debugger_queue_command (debugger, buff, FALSE, FALSE, NULL, NULL, NULL);
2138 g_free (buff);
2141 void
2142 debugger_run_to_position (Debugger *debugger, const gchar *file, guint line)
2144 gchar *buff;
2145 gchar *quoted_file;
2147 DEBUG_PRINT ("In function: debugger_run_to_position()");
2149 g_return_if_fail (IS_DEBUGGER (debugger));
2150 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
2152 quoted_file = gdb_quote (file);
2153 buff = g_strdup_printf ("-break-insert -t \"\\\"%s\\\":%u\"", quoted_file, line);
2154 g_free (quoted_file);
2155 debugger_queue_command (debugger, buff, FALSE, FALSE, NULL, NULL, NULL);
2156 g_free (buff);
2157 debugger_queue_command (debugger, "-exec-continue", FALSE, FALSE, NULL, NULL, NULL);
2160 void
2161 debugger_run_to_address (Debugger *debugger, gulong address)
2163 gchar *buff;
2165 DEBUG_PRINT ("In function: debugger_run_to_address()");
2167 g_return_if_fail (IS_DEBUGGER (debugger));
2168 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
2170 buff = g_strdup_printf ("-break-insert -t *0x%lx", address);
2171 debugger_queue_command (debugger, buff, FALSE, FALSE, NULL, NULL, NULL);
2172 g_free (buff);
2173 debugger_queue_command (debugger, "-exec-continue", FALSE, FALSE, NULL, NULL, NULL);
2176 void
2177 debugger_command (Debugger *debugger, const gchar *command,
2178 gboolean suppress_error, DebuggerParserFunc parser,
2179 gpointer user_data)
2181 if (strncasecmp (command, "-exec-run",
2182 strlen ("-exec-run")) == 0 ||
2183 strncasecmp (command, "run", strlen ("run")) == 0)
2185 /* FIXME: The user might have passed args to the command */
2186 debugger_run (debugger);
2188 else if (strncasecmp (command, "-exec-step",
2189 strlen ("-exec-step")) == 0 ||
2190 strncasecmp (command, "step", strlen ("step")) == 0)
2192 debugger_step_in (debugger);
2194 else if (strncasecmp (command, "-exec-next",
2195 strlen ("-exec-next")) == 0 ||
2196 strncasecmp (command, "next", strlen ("next")) == 0)
2198 debugger_step_over (debugger);
2200 else if (strncasecmp (command, "-exec-finish",
2201 strlen ("-exec-finish")) == 0 ||
2202 strncasecmp (command, "finish", strlen ("finish")) == 0)
2204 debugger_step_out (debugger);
2206 else if (strncasecmp (command, "-exec-continue",
2207 strlen ("-exec-continue")) == 0 ||
2208 strncasecmp (command, "continue", strlen ("continue")) == 0)
2210 debugger_run (debugger);
2212 else if (strncasecmp (command, "-exec-until",
2213 strlen ("-exec-until")) == 0 ||
2214 strncasecmp (command, "until", strlen ("until")) == 0)
2216 debugger_run_to_location (debugger, strchr (command, ' '));
2218 else if (strncasecmp (command, "-exec-abort",
2219 strlen ("-exec-abort")) == 0 ||
2220 strncasecmp (command, "kill", strlen ("kill")) == 0)
2222 debugger_stop_program (debugger);
2224 else if (strncasecmp (command, "-target-attach",
2225 strlen ("-target-attach")) == 0 ||
2226 strncasecmp (command, "attach", strlen ("attach")) == 0)
2228 pid_t pid = 0;
2229 gchar *pid_str = strchr (command, ' ');
2230 if (pid_str)
2231 pid = atoi (pid_str);
2232 debugger_attach_process (debugger, pid);
2234 else if (strncasecmp (command, "-target-detach",
2235 strlen ("-target-detach")) == 0 ||
2236 strncasecmp (command, "detach", strlen ("detach")) == 0)
2238 debugger_detach_process (debugger);
2240 else if (strncasecmp (command, "-file-exec-and-symbols",
2241 strlen ("-file-exec-and-symbols")) == 0 ||
2242 strncasecmp (command, "file", strlen ("file")) == 0)
2244 debugger_load_executable (debugger, strchr (command, ' '));
2246 else if (strncasecmp (command, "core", strlen ("core")) == 0)
2248 debugger_load_core (debugger, strchr (command, ' '));
2250 else
2252 debugger_queue_command (debugger, command, suppress_error, FALSE,
2253 parser, user_data, NULL);
2257 static void
2258 debugger_add_breakpoint_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2260 IAnjutaDebuggerBreakpointItem bp;
2261 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2262 gpointer user_data = debugger->priv->current_cmd.user_data;
2264 if ((error != NULL) || (mi_results == NULL))
2266 /* Call callback in all case (useful for enable that doesn't return
2267 * anything */
2268 if (callback != NULL)
2269 callback (NULL, user_data, error);
2271 else if (callback != NULL)
2273 const GDBMIValue *brkpnt;
2275 brkpnt = gdbmi_value_hash_lookup (mi_results, "bkpt");
2276 parse_breakpoint (&bp, brkpnt);
2278 /* Call callback in all case (useful for enable that doesn't return
2279 * anything */
2280 callback (&bp, user_data, error);
2285 void
2286 debugger_add_breakpoint_at_line (Debugger *debugger, const gchar *file, guint line, IAnjutaDebuggerCallback callback, gpointer user_data)
2288 gchar *buff;
2289 gchar *quoted_file;
2291 DEBUG_PRINT ("In function: debugger_add_breakpoint()");
2293 g_return_if_fail (IS_DEBUGGER (debugger));
2295 quoted_file = gdb_quote (file);
2297 buff = g_strdup_printf ("-break-insert \"\\\"%s\\\":%u\"", quoted_file, line);
2298 g_free (quoted_file);
2299 debugger_queue_command (debugger, buff, FALSE, FALSE, debugger_add_breakpoint_finish, callback, user_data);
2300 g_free (buff);
2303 void
2304 debugger_add_breakpoint_at_function (Debugger *debugger, const gchar *file, const gchar *function, IAnjutaDebuggerCallback callback, gpointer user_data)
2306 gchar *buff;
2308 DEBUG_PRINT ("In function: debugger_add_breakpoint()");
2310 g_return_if_fail (IS_DEBUGGER (debugger));
2312 buff = g_strdup_printf ("-break-insert %s%s%s", file == NULL ? "" : file, file == NULL ? "" : ":" , function);
2313 debugger_queue_command (debugger, buff, FALSE, FALSE, debugger_add_breakpoint_finish, callback, user_data);
2314 g_free (buff);
2317 void
2318 debugger_add_breakpoint_at_address (Debugger *debugger, gulong address, IAnjutaDebuggerCallback callback, gpointer user_data)
2320 gchar *buff;
2322 DEBUG_PRINT ("In function: debugger_add_breakpoint()");
2324 g_return_if_fail (IS_DEBUGGER (debugger));
2326 buff = g_strdup_printf ("-break-insert *0x%lx", address);
2327 debugger_queue_command (debugger, buff, FALSE, FALSE, debugger_add_breakpoint_finish, callback, user_data);
2328 g_free (buff);
2331 void
2332 debugger_enable_breakpoint (Debugger *debugger, guint id, gboolean enable, IAnjutaDebuggerCallback callback, gpointer user_data)
2335 gchar *buff;
2337 DEBUG_PRINT ("In function: debugger_enable_breakpoint()");
2339 g_return_if_fail (IS_DEBUGGER (debugger));
2341 buff = g_strdup_printf (enable ? "-break-enable %d" : "-break-disable %d",id);
2342 debugger_queue_command (debugger, buff, FALSE, FALSE, debugger_add_breakpoint_finish, callback, user_data);
2343 g_free (buff);
2346 void
2347 debugger_ignore_breakpoint (Debugger *debugger, guint id, guint ignore, IAnjutaDebuggerCallback callback, gpointer user_data)
2349 gchar *buff;
2351 DEBUG_PRINT ("In function: debugger_ignore_breakpoint()");
2353 g_return_if_fail (IS_DEBUGGER (debugger));
2355 buff = g_strdup_printf ("-break-after %d %d", id, ignore);
2356 debugger_queue_command (debugger, buff, FALSE, FALSE, debugger_add_breakpoint_finish, callback, user_data);
2357 g_free (buff);
2360 void
2361 debugger_condition_breakpoint (Debugger *debugger, guint id, const gchar *condition, IAnjutaDebuggerCallback callback, gpointer user_data)
2363 gchar *buff;
2365 DEBUG_PRINT ("In function: debugger_ignore_breakpoint()");
2367 g_return_if_fail (IS_DEBUGGER (debugger));
2369 buff = g_strdup_printf ("-break-condition %d %s", id, condition == NULL ? "" : condition);
2370 debugger_queue_command (debugger, buff, FALSE, FALSE, debugger_add_breakpoint_finish, callback, user_data);
2371 g_free (buff);
2374 static void
2375 debugger_remove_breakpoint_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2377 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2378 gpointer user_data = debugger->priv->current_cmd.user_data;
2379 IAnjutaDebuggerBreakpointItem bp;
2381 bp.type = IANJUTA_DEBUGGER_BREAKPOINT_REMOVED;
2382 bp.id = atoi (debugger->priv->current_cmd.cmd + 14);
2383 if (callback != NULL)
2384 callback (&bp, user_data, NULL);
2387 void
2388 debugger_remove_breakpoint (Debugger *debugger, guint id, IAnjutaDebuggerCallback callback, gpointer user_data)
2390 gchar *buff;
2392 DEBUG_PRINT ("In function: debugger_delete_breakpoint()");
2394 g_return_if_fail (IS_DEBUGGER (debugger));
2396 buff = g_strdup_printf ("-break-delete %d", id);
2397 debugger_queue_command (debugger, buff, FALSE, FALSE, debugger_remove_breakpoint_finish, callback, user_data);
2398 g_free (buff);
2401 static void
2402 debugger_list_breakpoint_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2404 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2405 gpointer user_data = debugger->priv->current_cmd.user_data;
2406 IAnjutaDebuggerBreakpointItem* bp;
2407 const GDBMIValue *table;
2408 GList *list = NULL;
2410 if ((error != NULL) || (mi_results == NULL))
2412 /* Call callback in all case (useful for enable that doesn't return
2413 * anything */
2414 if (callback != NULL)
2415 callback (NULL, user_data, error);
2418 table = gdbmi_value_hash_lookup (mi_results, "BreakpointTable");
2419 if (table)
2421 table = gdbmi_value_hash_lookup (table, "body");
2423 if (table)
2425 int i;
2427 for (i = 0; i < gdbmi_value_get_size (table); i++)
2429 const GDBMIValue *brkpnt;
2431 bp = g_new0 (IAnjutaDebuggerBreakpointItem, 1);
2433 brkpnt = gdbmi_value_list_get_nth (table, i);
2434 parse_breakpoint(bp, brkpnt);
2435 list = g_list_prepend (list, bp);
2440 /* Call callback in all case (useful for enable that doesn't return
2441 * anything */
2442 if (callback != NULL)
2444 list = g_list_reverse (list);
2445 callback (list, user_data, error);
2448 g_list_foreach (list, (GFunc)g_free, NULL);
2449 g_list_free (list);
2452 void
2453 debugger_list_breakpoint (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2455 DEBUG_PRINT ("In function: debugger_list_breakpoint()");
2457 g_return_if_fail (IS_DEBUGGER (debugger));
2459 debugger_queue_command (debugger, "-break-list", FALSE, FALSE, debugger_list_breakpoint_finish, callback, user_data);
2462 static void
2463 debugger_print_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2465 gchar *ptr = NULL;
2466 gchar *tmp;
2467 GList *list, *node;
2468 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2469 gpointer user_data = debugger->priv->current_cmd.user_data;
2472 list = gdb_util_remove_blank_lines (cli_results);
2473 if (g_list_length (list) < 1)
2475 tmp = NULL;
2477 else
2479 tmp = strchr ((gchar *) list->data, '=');
2481 if (tmp != NULL)
2483 ptr = g_strdup (tmp);
2484 for (node = list ? list->next : NULL; node != NULL; node = node->next)
2486 tmp = ptr;
2487 ptr = g_strconcat (tmp, (gchar *) node->data, NULL);
2488 g_free (tmp);
2492 callback (ptr, user_data, NULL);
2493 g_free (ptr);
2496 void
2497 debugger_print (Debugger *debugger, const gchar* variable, IAnjutaDebuggerCallback callback, gpointer user_data)
2499 gchar *buff;
2501 DEBUG_PRINT ("In function: debugger_print()");
2503 g_return_if_fail (IS_DEBUGGER (debugger));
2505 buff = g_strdup_printf ("print %s", variable);
2506 debugger_queue_command (debugger, buff, TRUE, FALSE, debugger_print_finish, callback, user_data);
2507 g_free (buff);
2510 static void
2511 debugger_evaluate_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2514 const GDBMIValue *value = NULL;
2515 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2516 gpointer user_data = debugger->priv->current_cmd.user_data;
2518 if (mi_results)
2519 value = gdbmi_value_hash_lookup (mi_results, "value");
2521 /* Call user function */
2522 if (callback != NULL)
2523 callback (value == NULL ? "?" : (char *)gdbmi_value_literal_get (value), user_data, NULL);
2526 void
2527 debugger_evaluate (Debugger *debugger, const gchar* name, IAnjutaDebuggerCallback callback, gpointer user_data)
2529 gchar *buff;
2530 DEBUG_PRINT ("In function: debugger_add_watch()");
2532 g_return_if_fail (IS_DEBUGGER (debugger));
2534 buff = g_strdup_printf ("-data-evaluate-expression %s", name);
2535 debugger_queue_command (debugger, buff, TRUE, FALSE, debugger_evaluate_finish, callback, user_data);
2536 g_free (buff);
2539 static void
2540 debugger_list_local_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2543 const GDBMIValue *local, *var, *frame, *args, *stack;
2544 const gchar * name;
2545 GList* list;
2546 guint i;
2547 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2548 gpointer user_data = debugger->priv->current_cmd.user_data;
2551 list = NULL;
2552 /* Add arguments */
2553 stack = gdbmi_value_hash_lookup (mi_results, "stack-args");
2554 if (stack)
2556 frame = gdbmi_value_list_get_nth (stack, 0);
2557 if (frame)
2559 args = gdbmi_value_hash_lookup (frame, "args");
2560 if (args)
2562 for (i = 0; i < gdbmi_value_get_size (args); i++)
2564 var = gdbmi_value_list_get_nth (args, i);
2565 if (var)
2567 name = gdbmi_value_literal_get (var);
2568 list = g_list_prepend (list, (gchar *)name);
2576 /* List local variables */
2577 local = gdbmi_value_hash_lookup (mi_results, "locals");
2578 if (local)
2580 for (i = 0; i < gdbmi_value_get_size (local); i++)
2582 var = gdbmi_value_list_get_nth (local, i);
2583 if (var)
2585 name = gdbmi_value_literal_get (var);
2586 list = g_list_prepend (list, (gchar *)name);
2590 list = g_list_reverse (list);
2591 callback (list, user_data, NULL);
2592 g_list_free (list);
2595 void
2596 debugger_list_local (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2598 gchar *buff;
2599 DEBUG_PRINT ("In function: debugger_list_local()");
2601 g_return_if_fail (IS_DEBUGGER (debugger));
2603 buff = g_strdup_printf("-stack-list-arguments 0 %d %d", debugger->priv->current_frame, debugger->priv->current_frame);
2604 debugger_queue_command (debugger, buff, TRUE, TRUE, NULL, NULL, NULL);
2605 g_free (buff);
2606 debugger_queue_command (debugger, "-stack-list-locals 0", TRUE, FALSE, debugger_list_local_finish, callback, user_data);
2609 static void
2610 debugger_list_argument_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2613 const GDBMIValue *frame, *var, *args, *stack;
2614 const gchar * name;
2615 GList* list;
2616 guint i;
2617 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2618 gpointer user_data = debugger->priv->current_cmd.user_data;
2621 list = NULL;
2622 args = NULL;
2623 stack = gdbmi_value_hash_lookup (mi_results, "stack-args");
2624 if (stack)
2626 frame = gdbmi_value_list_get_nth (stack, 0);
2627 if (frame)
2629 args = gdbmi_value_hash_lookup (frame, "args");
2633 if (args)
2635 for (i = 0; i < gdbmi_value_get_size (args); i++)
2637 var = gdbmi_value_list_get_nth (args, i);
2638 if (var)
2640 name = gdbmi_value_literal_get (var);
2641 list = g_list_prepend (list, (gchar *)name);
2645 list = g_list_reverse (list);
2646 callback (list, user_data, NULL);
2647 g_list_free (list);
2650 void
2651 debugger_list_argument (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2653 gchar *buff;
2655 DEBUG_PRINT ("In function: debugger_list_argument()");
2657 g_return_if_fail (IS_DEBUGGER (debugger));
2659 buff = g_strdup_printf("-stack-list-arguments 0 %d %d", debugger->priv->current_frame, debugger->priv->current_frame);
2660 debugger_queue_command (debugger, buff, TRUE, FALSE, debugger_list_argument_finish, callback, user_data);
2661 g_free (buff);
2664 static void
2665 debugger_info_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2668 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2669 gpointer user_data = debugger->priv->current_cmd.user_data;
2671 if (callback != NULL)
2672 callback ((GList *)cli_results, user_data, NULL);
2675 void
2676 debugger_info_frame (Debugger *debugger, guint frame, IAnjutaDebuggerCallback callback, gpointer user_data)
2678 gchar *buff;
2680 DEBUG_PRINT ("In function: debugger_info_frame()");
2682 g_return_if_fail (IS_DEBUGGER (debugger));
2684 if (frame == 0)
2686 buff = g_strdup_printf("info frame");
2688 else
2690 buff = g_strdup_printf ("info frame %d", frame);
2692 debugger_queue_command (debugger, buff, TRUE, FALSE, (DebuggerParserFunc)debugger_info_finish, callback, user_data);
2693 g_free (buff);
2696 void
2697 debugger_info_signal (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2699 DEBUG_PRINT ("In function: debugger_info_signal()");
2701 g_return_if_fail (IS_DEBUGGER (debugger));
2703 debugger_queue_command (debugger, "info signals", TRUE, FALSE, (DebuggerParserFunc)debugger_info_finish, callback, user_data);
2706 void
2707 debugger_info_sharedlib (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2709 gchar *buff;
2711 DEBUG_PRINT ("In function: debugger_info_sharedlib()");
2713 g_return_if_fail (IS_DEBUGGER (debugger));
2715 buff = g_strdup_printf ("info sharedlib");
2716 debugger_queue_command (debugger, buff, TRUE, FALSE, (DebuggerParserFunc)debugger_info_finish, callback, user_data); g_free (buff);
2719 void
2720 debugger_info_args (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2722 DEBUG_PRINT ("In function: debugger_info_args()");
2724 g_return_if_fail (IS_DEBUGGER (debugger));
2726 debugger_queue_command (debugger, "info args", TRUE, FALSE, (DebuggerParserFunc)debugger_info_finish, callback, user_data);
2729 void
2730 debugger_info_target (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2732 DEBUG_PRINT ("In function: debugger_info_target()");
2734 g_return_if_fail (IS_DEBUGGER (debugger));
2736 debugger_queue_command (debugger, "info target", TRUE, FALSE, (DebuggerParserFunc)debugger_info_finish, callback, user_data);
2739 void
2740 debugger_info_program (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2742 DEBUG_PRINT ("In function: debugger_info_program()");
2744 g_return_if_fail (IS_DEBUGGER (debugger));
2746 debugger_queue_command (debugger, "info program", TRUE, FALSE, (DebuggerParserFunc)debugger_info_finish, callback, user_data);
2749 void
2750 debugger_info_udot (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2752 DEBUG_PRINT ("In function: debugger_info_udot()");
2754 g_return_if_fail (IS_DEBUGGER (debugger));
2756 debugger_queue_command (debugger, "info udot", TRUE, FALSE, (DebuggerParserFunc)debugger_info_finish, callback, user_data);
2759 void
2760 debugger_info_variables (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2762 DEBUG_PRINT ("In function: debugger_info_variables()");
2764 g_return_if_fail (IS_DEBUGGER (debugger));
2766 debugger_queue_command (debugger, "info variables", TRUE, FALSE, (DebuggerParserFunc)debugger_info_finish, callback, user_data);
2769 static void
2770 debugger_read_memory_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2773 const GDBMIValue *literal;
2774 const GDBMIValue *mem;
2775 const gchar *value;
2776 gchar *data;
2777 gchar *ptr;
2778 gulong address;
2779 guint len;
2780 guint i;
2781 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2782 gpointer user_data = debugger->priv->current_cmd.user_data;
2783 IAnjutaDebuggerMemoryBlock read = {0,};
2785 literal = gdbmi_value_hash_lookup (mi_results, "total-bytes");
2786 if (literal)
2788 guint size;
2790 len = strtoul (gdbmi_value_literal_get (literal), NULL, 10);
2791 data = g_new (gchar, len * 2);
2792 memset (data + len, 0, len);
2794 literal = gdbmi_value_hash_lookup (mi_results, "addr");
2795 address = strtoul (gdbmi_value_literal_get (literal), NULL, 0);
2797 ptr = data;
2798 size = 0;
2799 mem = gdbmi_value_hash_lookup (mi_results, "memory");
2800 if (mem)
2802 mem = gdbmi_value_list_get_nth (mem, 0);
2803 if (mem)
2805 mem = gdbmi_value_hash_lookup (mem, "data");
2806 if (mem)
2808 size = gdbmi_value_get_size (mem);
2813 if (size < len) len = size;
2814 for (i = 0; i < len; i++)
2816 literal = gdbmi_value_list_get_nth (mem, i);
2817 if (literal)
2819 gchar *endptr;
2820 value = gdbmi_value_literal_get (literal);
2821 *ptr = strtoul (value, &endptr, 16);
2822 if ((*value != '\0') && (*endptr == '\0'))
2824 /* valid data */
2825 ptr[len] = 1;
2827 ptr++;
2830 read.address = address;
2831 read.length = len;
2832 read.data = data;
2833 callback (&read, user_data, NULL);
2835 g_free (data);
2837 else
2839 callback (NULL, user_data, NULL);
2843 void
2844 debugger_inspect_memory (Debugger *debugger, gulong address, guint length, IAnjutaDebuggerCallback callback, gpointer user_data)
2846 gchar *buff;
2848 DEBUG_PRINT ("In function: debugger_inspect_memory()");
2850 g_return_if_fail (IS_DEBUGGER (debugger));
2852 buff = g_strdup_printf ("-data-read-memory 0x%lx x 1 1 %d", address, length);
2853 debugger_queue_command (debugger, buff, FALSE, FALSE, debugger_read_memory_finish, callback, user_data);
2854 g_free (buff);
2857 static void
2858 debugger_disassemble_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2861 const GDBMIValue *literal;
2862 const GDBMIValue *line;
2863 const GDBMIValue *mem;
2864 const gchar *value;
2865 guint i;
2866 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2867 gpointer user_data = debugger->priv->current_cmd.user_data;
2868 IAnjutaDebuggerInstructionDisassembly *read = NULL;
2870 if (error != NULL)
2872 /* Command fail */
2873 callback (NULL, user_data, error);
2875 return;
2879 mem = gdbmi_value_hash_lookup (mi_results, "asm_insns");
2880 if (mem)
2882 guint size;
2884 size = gdbmi_value_get_size (mem);
2885 read = (IAnjutaDebuggerInstructionDisassembly *)g_malloc0(sizeof (IAnjutaDebuggerInstructionDisassembly) + sizeof(IAnjutaDebuggerInstructionALine) * size);
2886 read->size = size;
2888 for (i = 0; i < size; i++)
2890 line = gdbmi_value_list_get_nth (mem, i);
2891 if (line)
2893 /* Get address */
2894 literal = gdbmi_value_hash_lookup (line, "address");
2895 if (literal)
2897 value = gdbmi_value_literal_get (literal);
2898 read->data[i].address = strtoul (value, NULL, 0);
2901 /* Get label if one exist */
2902 literal = gdbmi_value_hash_lookup (line, "offset");
2903 if (literal)
2905 value = gdbmi_value_literal_get (literal);
2906 if ((value != NULL) && (strtoul (value, NULL, 0) == 0))
2908 literal = gdbmi_value_hash_lookup (line, "func-name");
2909 if (literal)
2911 read->data[i].label = gdbmi_value_literal_get (literal);
2918 /* Get disassembly line */
2919 literal = gdbmi_value_hash_lookup (line, "inst");
2920 if (literal)
2922 read->data[i].text = gdbmi_value_literal_get (literal);
2927 /* Remove last line to mark end */
2928 read->data[i - 1].text = NULL;
2930 callback (read, user_data, NULL);
2932 g_free (read);
2934 else
2936 callback (NULL, user_data, NULL);
2940 void
2941 debugger_disassemble (Debugger *debugger, gulong address, guint length, IAnjutaDebuggerCallback callback, gpointer user_data)
2943 gchar *buff;
2944 gulong end;
2946 DEBUG_PRINT ("In function: debugger_disassemble()");
2948 g_return_if_fail (IS_DEBUGGER (debugger));
2951 /* Handle overflow */
2952 end = (address + length < address) ? G_MAXULONG : address + length;
2953 buff = g_strdup_printf ("-data-disassemble -s 0x%lx -e 0x%lx -- 0", address, end);
2954 debugger_queue_command (debugger, buff, FALSE, FALSE, debugger_disassemble_finish, callback, user_data);
2955 g_free (buff);
2958 static void
2959 parse_frame (IAnjutaDebuggerFrame *frame, const GDBMIValue *frame_hash)
2961 const GDBMIValue *literal;
2963 literal = gdbmi_value_hash_lookup (frame_hash, "level");
2964 if (literal)
2965 frame->level = strtoul (gdbmi_value_literal_get (literal), NULL, 10);
2966 else
2967 frame->level = 0;
2969 literal = gdbmi_value_hash_lookup (frame_hash, "fullname");
2970 if (literal == NULL)
2971 literal = gdbmi_value_hash_lookup (frame_hash, "file");
2972 if (literal)
2973 frame->file = (gchar *)gdbmi_value_literal_get (literal);
2974 else
2975 frame->file = NULL;
2977 literal = gdbmi_value_hash_lookup (frame_hash, "line");
2978 if (literal)
2979 frame->line = strtoul (gdbmi_value_literal_get (literal), NULL, 10);
2980 else
2981 frame->line = 0;
2983 literal = gdbmi_value_hash_lookup (frame_hash, "func");
2984 if (literal)
2985 frame->function = (gchar *)gdbmi_value_literal_get (literal);
2986 else
2987 frame->function = NULL;
2989 literal = gdbmi_value_hash_lookup (frame_hash, "from");
2990 if (literal)
2991 frame->library = (gchar *)gdbmi_value_literal_get (literal);
2992 else
2993 frame->library = NULL;
2995 literal = gdbmi_value_hash_lookup (frame_hash, "addr");
2996 if (literal)
2997 frame->address = strtoul (gdbmi_value_literal_get (literal), NULL, 16);
2998 else
2999 frame->address = 0;
3004 static void
3005 add_frame (const GDBMIValue *frame_hash, GdbGListPacket* pack)
3007 IAnjutaDebuggerFrame* frame;
3009 frame = g_new0 (IAnjutaDebuggerFrame, 1);
3010 pack->list = g_list_prepend (pack->list, frame);
3012 frame->thread = pack->tag;
3013 parse_frame (frame, frame_hash);
3016 static void
3017 set_func_args (const GDBMIValue *frame_hash, GList** node)
3019 const gchar *level;
3020 const GDBMIValue *literal, *args_list, *arg_hash;
3021 gint i;
3022 GString *args_str;
3023 IAnjutaDebuggerFrame* frame;
3025 literal = gdbmi_value_hash_lookup (frame_hash, "level");
3026 if (!literal)
3027 return;
3029 level = gdbmi_value_literal_get (literal);
3030 if (!level)
3031 return;
3033 frame = (IAnjutaDebuggerFrame *)(*node)->data;
3035 args_list = gdbmi_value_hash_lookup (frame_hash, "args");
3036 if (args_list)
3038 args_str = g_string_new ("(");
3039 for (i = 0; i < gdbmi_value_get_size (args_list); i++)
3041 const gchar *name, *value;
3043 arg_hash = gdbmi_value_list_get_nth (args_list, i);
3044 if (!arg_hash)
3045 continue;
3047 literal = gdbmi_value_hash_lookup (arg_hash, "name");
3048 if (!literal)
3049 continue;
3050 name = gdbmi_value_literal_get (literal);
3051 if (!name)
3052 continue;
3054 literal = gdbmi_value_hash_lookup (arg_hash, "value");
3055 if (!literal)
3056 continue;
3057 value = gdbmi_value_literal_get (literal);
3058 if (!value)
3059 continue;
3060 args_str = g_string_append (args_str, name);
3061 args_str = g_string_append (args_str, "=");
3062 args_str = g_string_append (args_str, value);
3063 if (i < (gdbmi_value_get_size (args_list) - 1))
3064 args_str = g_string_append (args_str, ", ");
3066 args_str = g_string_append (args_str, ")");
3067 frame->args = args_str->str;
3068 g_string_free (args_str, FALSE);
3070 *node = g_list_next (*node);
3073 static void
3074 debugger_stack_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
3077 GdbGListPacket pack = {NULL, 0};
3078 GList* node;
3079 const GDBMIValue *stack_list;
3080 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
3081 gpointer user_data = debugger->priv->current_cmd.user_data;
3083 if (!mi_results)
3084 return;
3086 stack_list = gdbmi_value_hash_lookup (mi_results, "stack");
3087 if (stack_list)
3089 pack.tag = debugger->priv->current_thread;
3090 gdbmi_value_foreach (stack_list, (GFunc)add_frame, &pack);
3093 if (pack.list)
3095 pack.list = g_list_reverse (pack.list);
3096 node = g_list_first (pack.list);
3097 stack_list = gdbmi_value_hash_lookup (mi_results, "stack-args");
3098 if (stack_list)
3099 gdbmi_value_foreach (stack_list, (GFunc)set_func_args, &node);
3101 // Call call back function
3102 if (callback != NULL)
3103 callback (pack.list, user_data, NULL);
3105 // Free data
3106 for (node = g_list_first (pack.list); node != NULL; node = g_list_next (node))
3108 g_free ((gchar *)((IAnjutaDebuggerFrame *)node->data)->args);
3109 g_free (node->data);
3112 g_list_free (pack.list);
3114 else
3116 // Call call back function
3117 if (callback != NULL)
3118 callback (NULL, user_data, NULL);
3122 void
3123 debugger_list_frame (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
3125 DEBUG_PRINT ("In function: debugger_list_frame()");
3127 g_return_if_fail (IS_DEBUGGER (debugger));
3129 debugger_queue_command (debugger, "-stack-list-frames", TRUE, TRUE, NULL, NULL, NULL);
3130 debugger_queue_command (debugger, "-stack-list-arguments 1", TRUE, FALSE, debugger_stack_finish, callback, user_data);
3133 /* Thread functions
3134 *---------------------------------------------------------------------------*/
3136 static void
3137 debugger_set_thread_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
3139 const GDBMIValue *literal;
3140 guint id;
3142 if (mi_results == NULL) return;
3144 literal = gdbmi_value_hash_lookup (mi_results, "new-thread-id");
3145 if (literal == NULL) return;
3147 id = strtoul (gdbmi_value_literal_get (literal), NULL, 10);
3148 if (id == 0) return;
3150 debugger->priv->current_thread = id;
3151 g_signal_emit_by_name (debugger->priv->instance, "frame-changed", 0, debugger->priv->current_thread);
3153 return;
3156 void
3157 debugger_set_thread (Debugger *debugger, gint thread)
3159 gchar *buff;
3161 DEBUG_PRINT ("In function: debugger_set_thread()");
3163 g_return_if_fail (IS_DEBUGGER (debugger));
3165 buff = g_strdup_printf ("-thread-select %d", thread);
3167 debugger_queue_command (debugger, buff, FALSE, FALSE, (DebuggerParserFunc)debugger_set_thread_finish, NULL, NULL);
3168 g_free (buff);
3171 static void
3172 add_thread_id (const GDBMIValue *thread_hash, GList** list)
3174 IAnjutaDebuggerFrame* frame;
3175 gint thread;
3177 thread = strtoul (gdbmi_value_literal_get (thread_hash), NULL, 10);
3178 if (thread == 0) return;
3180 frame = g_new0 (IAnjutaDebuggerFrame, 1);
3181 *list = g_list_prepend (*list, frame);
3183 frame->thread = thread;
3186 static void
3187 debugger_list_thread_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
3190 const GDBMIValue *id_list;
3191 gpointer user_data = debugger->priv->current_cmd.user_data;
3192 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
3193 GList* thread_list = NULL;
3195 for (;;)
3197 if (mi_results == NULL) break;
3199 id_list = gdbmi_value_hash_lookup (mi_results, "thread-ids");
3200 if (id_list == NULL) break;
3202 gdbmi_value_foreach (id_list, (GFunc)add_thread_id, &thread_list);
3203 thread_list = g_list_reverse (thread_list);
3204 break;
3207 if (callback != NULL)
3208 callback (thread_list, user_data, error);
3210 if (thread_list != NULL)
3212 g_list_foreach (thread_list, (GFunc)g_free, NULL);
3213 g_list_free (thread_list);
3217 void
3218 debugger_list_thread (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
3220 DEBUG_PRINT ("In function: debugger_list_thread()");
3222 g_return_if_fail (IS_DEBUGGER (debugger));
3224 debugger_queue_command (debugger, "-thread-list-ids", TRUE, FALSE, debugger_list_thread_finish, callback, user_data);
3227 static void
3228 debugger_info_set_thread_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
3230 const GDBMIValue *literal;
3231 guint id;
3233 if (mi_results == NULL) return;
3235 literal = gdbmi_value_hash_lookup (mi_results, "new-thread-id");
3236 if (literal == NULL) return;
3238 id = strtoul (gdbmi_value_literal_get (literal), NULL, 10);
3239 if (id == 0) return;
3241 debugger->priv->current_thread = id;
3242 /* Do not emit a signal notification as the current thread will
3243 * be restored when needed */
3245 return;
3248 static void
3249 debugger_info_thread_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
3251 const GDBMIValue *frame;
3252 IAnjutaDebuggerFrame top_frame;
3253 IAnjutaDebuggerFrame *top;
3254 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
3255 gpointer user_data = debugger->priv->current_cmd.user_data;
3257 for (top = NULL;;)
3259 DEBUG_PRINT("look for stack %p", mi_results);
3260 if (mi_results == NULL) break;
3262 frame = gdbmi_value_hash_lookup (mi_results, "stack");
3263 if (frame == NULL) break;
3265 DEBUG_PRINT("get stack");
3267 frame = gdbmi_value_list_get_nth (frame, 0);
3268 if (frame == NULL) break;
3270 DEBUG_PRINT("get nth element");
3272 /*frame = gdbmi_value_hash_lookup (frame, "frame");
3273 DEBUG_PRINT("get frame %p", frame);
3274 if (frame == NULL) break;*/
3276 DEBUG_PRINT("get frame");
3278 top = &top_frame;
3279 top->thread = debugger->priv->current_thread;
3281 parse_frame (top, frame);
3282 break;
3285 if (callback != NULL)
3286 callback (top, user_data, error);
3288 return;
3291 void
3292 debugger_info_thread (Debugger *debugger, gint thread, IAnjutaDebuggerCallback callback, gpointer user_data)
3294 gchar *buff;
3295 guint orig_thread;
3297 DEBUG_PRINT ("In function: debugger_info_thread()");
3299 g_return_if_fail (IS_DEBUGGER (debugger));
3301 orig_thread = debugger->priv->current_thread;
3302 buff = g_strdup_printf ("-thread-select %d", thread);
3303 debugger_queue_command (debugger, buff, FALSE, FALSE, (DebuggerParserFunc)debugger_info_set_thread_finish, NULL, NULL);
3304 g_free (buff);
3305 debugger_queue_command (debugger, "-stack-list-frames 0 0", FALSE, FALSE, (DebuggerParserFunc)debugger_info_thread_finish, callback, user_data);
3307 buff = g_strdup_printf ("-thread-select %d", orig_thread);
3308 debugger_queue_command (debugger, buff, FALSE, FALSE, (DebuggerParserFunc)debugger_info_set_thread_finish, NULL, NULL);
3309 g_free (buff);
3312 static void
3313 add_register_name (const GDBMIValue *reg_literal, GList** list)
3315 IAnjutaDebuggerRegisterData* reg;
3316 GList *prev = *list;
3318 reg = g_new0 (IAnjutaDebuggerRegisterData, 1);
3319 *list = g_list_prepend (prev, reg);
3320 reg->name = (gchar *)gdbmi_value_literal_get (reg_literal);
3321 reg->num = prev == NULL ? 0 : ((IAnjutaDebuggerRegisterData *)prev->data)->num + 1;
3324 static void
3325 add_register_value (const GDBMIValue *reg_hash, GList** list)
3327 const GDBMIValue *literal;
3328 const gchar *val;
3329 IAnjutaDebuggerRegisterData* reg;
3330 guint num;
3331 GList* prev = *list;
3333 literal = gdbmi_value_hash_lookup (reg_hash, "number");
3334 if (!literal)
3335 return;
3336 val = gdbmi_value_literal_get (literal);
3337 num = strtoul (val, NULL, 10);
3339 literal = gdbmi_value_hash_lookup (reg_hash, "value");
3340 if (!literal)
3341 return;
3343 reg = g_new0 (IAnjutaDebuggerRegisterData, 1);
3344 *list = g_list_prepend (prev, reg);
3345 reg->num = num;
3346 reg->value = (gchar *)gdbmi_value_literal_get (literal);
3349 static void
3350 debugger_register_name_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
3353 GList* list = NULL;
3354 GList* node;
3355 const GDBMIValue *reg_list;
3356 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
3357 gpointer user_data = debugger->priv->current_cmd.user_data;
3359 if (!mi_results)
3360 return;
3362 reg_list = gdbmi_value_hash_lookup (mi_results, "register-names");
3363 if (reg_list)
3364 gdbmi_value_foreach (reg_list, (GFunc)add_register_name, &list);
3365 list = g_list_reverse (list);
3367 // Call call back function
3368 if (callback != NULL)
3369 callback (list, user_data, NULL);
3371 // Free data
3372 for (node = g_list_first (list); node != NULL; node = g_list_next (node))
3374 g_free (node->data);
3376 g_list_free (list);
3379 static void
3380 debugger_register_value_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
3383 GList* list = NULL;
3384 GList* node;
3385 const GDBMIValue *reg_list;
3386 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
3387 gpointer user_data = debugger->priv->current_cmd.user_data;
3389 if (!mi_results)
3390 return;
3392 reg_list = gdbmi_value_hash_lookup (mi_results, "register-values");
3393 if (reg_list)
3394 gdbmi_value_foreach (reg_list, (GFunc)add_register_value, &list);
3395 list = g_list_reverse (list);
3397 // Call call back function
3398 if (callback != NULL)
3399 callback (list, user_data, NULL);
3401 // Free data
3402 for (node = g_list_first (list); node != NULL; node = g_list_next (node))
3404 g_free (node->data);
3406 g_list_free (list);
3409 void
3410 debugger_list_register (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
3412 DEBUG_PRINT ("In function: debugger_list_register()");
3414 g_return_if_fail (IS_DEBUGGER (debugger));
3416 debugger_queue_command (debugger, "-data-list-register-names", TRUE, FALSE, debugger_register_name_finish, callback, user_data);
3419 void
3420 debugger_update_register (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
3422 DEBUG_PRINT ("In function: debugger_update_register()");
3424 g_return_if_fail (IS_DEBUGGER (debugger));
3426 debugger_queue_command (debugger, "-data-list-register-values r", TRUE, FALSE, (DebuggerParserFunc)debugger_register_value_finish, callback, user_data);
3429 void
3430 debugger_write_register (Debugger *debugger, const gchar *name, const gchar *value)
3432 gchar *buf;
3434 DEBUG_PRINT ("In function: debugger_write_register()");
3436 g_return_if_fail (IS_DEBUGGER (debugger));
3438 buf = g_strdup_printf ("-data-evaluate-expression \"$%s=%s\"", name, value);
3439 debugger_queue_command (debugger, buf, TRUE, FALSE, NULL, NULL, NULL);
3440 g_free (buf);
3443 static void
3444 debugger_set_frame_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
3447 guint frame = (guint)debugger->priv->current_cmd.user_data;
3448 debugger->priv->current_frame = frame;
3450 g_signal_emit_by_name (debugger->priv->instance, "frame-changed", frame, debugger->priv->current_thread);
3453 void
3454 debugger_set_frame (Debugger *debugger, guint frame)
3456 gchar *buff;
3458 DEBUG_PRINT ("In function: debugger_set_frame()");
3460 g_return_if_fail (IS_DEBUGGER (debugger));
3462 buff = g_strdup_printf ("-stack-select-frame %d", frame);
3464 debugger_queue_command (debugger, buff, FALSE, FALSE, (DebuggerParserFunc)debugger_set_frame_finish, NULL, (gpointer)frame);
3465 g_free (buff);
3468 void
3469 debugger_set_log (Debugger *debugger, IAnjutaMessageView *log)
3471 debugger->priv->log = log;
3474 /* Variable objects functions
3475 *---------------------------------------------------------------------------*/
3477 void
3478 debugger_delete_variable (Debugger *debugger, const gchar* name)
3480 gchar *buff;
3482 DEBUG_PRINT ("In function: delete_variable()");
3484 g_return_if_fail (IS_DEBUGGER (debugger));
3486 buff = g_strdup_printf ("-var-delete %s", name);
3487 debugger_queue_command (debugger, buff, FALSE, FALSE, NULL, NULL, NULL);
3488 g_free (buff);
3491 static void
3492 gdb_var_evaluate_expression (Debugger *debugger,
3493 const GDBMIValue *mi_results, const GList *cli_results,
3494 GError *error)
3496 const gchar *value = NULL;
3497 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
3498 gpointer user_data = debugger->priv->current_cmd.user_data;
3500 if (mi_results != NULL)
3502 const GDBMIValue *const gdbmi_value =
3503 gdbmi_value_hash_lookup (mi_results, "value");
3505 if (gdbmi_value != NULL)
3506 value = gdbmi_value_literal_get (gdbmi_value);
3508 callback ((const gpointer)value, user_data, NULL);
3511 void
3512 debugger_evaluate_variable (Debugger *debugger, const gchar* name, IAnjutaDebuggerCallback callback, gpointer user_data)
3514 gchar *buff;
3516 DEBUG_PRINT ("In function: evaluate_variable()");
3518 g_return_if_fail (IS_DEBUGGER (debugger));
3520 buff = g_strdup_printf ("-var-evaluate-expression %s", name);
3521 debugger_queue_command (debugger, buff, FALSE, FALSE, gdb_var_evaluate_expression, callback, user_data);
3522 g_free (buff);
3525 void
3526 debugger_assign_variable (Debugger *debugger, const gchar* name, const gchar *value)
3528 gchar *buff;
3530 DEBUG_PRINT ("In function: assign_variable()");
3532 g_return_if_fail (IS_DEBUGGER (debugger));
3534 buff = g_strdup_printf ("-var-assign %s %s", name, value);
3535 debugger_queue_command (debugger, buff, FALSE, FALSE, NULL, NULL, NULL);
3536 g_free (buff);
3539 static void
3540 gdb_var_list_children (Debugger *debugger,
3541 const GDBMIValue *mi_results, const GList *cli_results,
3542 GError *error)
3544 GList* list = NULL;
3545 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
3546 gpointer user_data = debugger->priv->current_cmd.user_data;
3548 if (mi_results != NULL)
3550 const GDBMIValue *literal;
3551 const GDBMIValue *children;
3552 glong numchild = 0;
3553 glong i = 0;
3555 literal = gdbmi_value_hash_lookup (mi_results, "numchild");
3557 if (literal)
3558 numchild = strtoul(gdbmi_value_literal_get (literal), NULL, 0);
3559 children = gdbmi_value_hash_lookup (mi_results, "children");
3561 for(i = 0 ; i < numchild; ++i)
3563 const GDBMIValue *const gdbmi_chl =
3564 gdbmi_value_list_get_nth (children, i);
3565 IAnjutaDebuggerVariableObject *var;
3567 var = g_new0 (IAnjutaDebuggerVariableObject, 1);
3569 literal = gdbmi_value_hash_lookup (gdbmi_chl, "name");
3570 if (literal)
3571 var->name = (gchar *)gdbmi_value_literal_get (literal);
3573 literal = gdbmi_value_hash_lookup (gdbmi_chl, "exp");
3574 if (literal)
3575 var->expression = (gchar *)gdbmi_value_literal_get(literal);
3577 literal = gdbmi_value_hash_lookup (gdbmi_chl, "type");
3578 if (literal)
3579 var->type = (gchar *)gdbmi_value_literal_get(literal);
3581 literal = gdbmi_value_hash_lookup (gdbmi_chl, "value");
3582 if (literal)
3583 var->value = (gchar *)gdbmi_value_literal_get(literal);
3585 literal = gdbmi_value_hash_lookup (gdbmi_chl, "numchild");
3586 if (literal)
3587 var->children = strtoul(gdbmi_value_literal_get(literal), NULL, 10);
3589 list = g_list_prepend (list, var);
3591 list = g_list_reverse (list);
3594 callback (list, user_data, NULL);
3595 g_list_foreach (list, (GFunc)g_free, NULL);
3596 g_list_free (list);
3599 void debugger_list_variable_children (Debugger *debugger, const gchar* name, IAnjutaDebuggerCallback callback, gpointer user_data)
3601 gchar *buff;
3603 DEBUG_PRINT ("In function: list_variable_children()");
3605 g_return_if_fail (IS_DEBUGGER (debugger));
3607 buff = g_strdup_printf ("-var-list-children --all-values %s", name);
3608 debugger_queue_command (debugger, buff, FALSE, FALSE, gdb_var_list_children, callback, user_data);
3609 g_free (buff);
3612 static void
3613 gdb_var_create (Debugger *debugger,
3614 const GDBMIValue *mi_results, const GList *cli_results,
3615 GError *error)
3617 const GDBMIValue * result;
3618 IAnjutaDebuggerVariableObject var = {0,};
3619 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
3620 gpointer user_data = debugger->priv->current_cmd.user_data;
3622 if ((error == NULL) && (mi_results != NULL))
3624 result = gdbmi_value_hash_lookup (mi_results, "name");
3625 var.name = (gchar *)gdbmi_value_literal_get(result);
3627 result = gdbmi_value_hash_lookup (mi_results, "type");
3628 var.type = (gchar *)gdbmi_value_literal_get (result);
3630 result = gdbmi_value_hash_lookup (mi_results, "numchild");
3631 var.children = strtoul (gdbmi_value_literal_get(result), NULL, 10);
3633 callback (&var, user_data, error);
3637 void debugger_create_variable (Debugger *debugger, const gchar* name, IAnjutaDebuggerCallback callback, gpointer user_data)
3639 gchar *buff;
3641 DEBUG_PRINT ("In function: create_variable()");
3643 g_return_if_fail (IS_DEBUGGER (debugger));
3645 buff = g_strdup_printf ("-var-create - * %s", name);
3646 debugger_queue_command (debugger, buff, FALSE, FALSE, gdb_var_create, callback, user_data);
3647 g_free (buff);
3650 static void
3651 gdb_var_update (Debugger *debugger,
3652 const GDBMIValue *mi_results, const GList *cli_results,
3653 GError *error)
3655 GList* list = NULL;
3656 glong idx = 0, changed_count = 0;
3657 const GDBMIValue *const gdbmi_changelist =
3658 gdbmi_value_hash_lookup (mi_results, "changelist");
3659 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
3660 gpointer user_data = debugger->priv->current_cmd.user_data;
3663 changed_count = gdbmi_value_get_size (gdbmi_changelist);
3664 for(; idx<changed_count; ++idx)
3666 const GDBMIValue *const gdbmi_change =
3667 gdbmi_value_list_get_nth (gdbmi_changelist, idx);
3668 const GDBMIValue * value;
3671 value = gdbmi_value_hash_lookup (gdbmi_change, "name");
3672 if (value)
3674 IAnjutaDebuggerVariableObject *var = g_new0 (IAnjutaDebuggerVariableObject, 1);
3675 var->changed = TRUE;
3676 var->name = (gchar *)gdbmi_value_literal_get(value);
3677 list = g_list_prepend (list, var);
3679 value = gdbmi_value_hash_lookup (gdbmi_change, "type_changed");
3680 if (value != NULL)
3682 const gchar *type_changed = gdbmi_value_literal_get (value);
3684 if (strcmp (type_changed, "true"))
3686 var->deleted = TRUE;
3690 value = gdbmi_value_hash_lookup (gdbmi_change, "in_scope");
3691 if (value != NULL)
3693 const gchar *in_scope = gdbmi_value_literal_get(value);
3695 if (strcmp (in_scope, "false") == 0)
3697 var->exited = TRUE;
3699 else if (strcmp (in_scope, "invalid") == 0)
3701 var->deleted = TRUE;
3706 list = g_list_reverse (list);
3707 callback (list, user_data, NULL);
3708 g_list_foreach (list, (GFunc)g_free, NULL);
3709 g_list_free (list);
3712 void debugger_update_variable (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
3714 DEBUG_PRINT ("In function: update_variable()");
3716 g_return_if_fail (IS_DEBUGGER (debugger));
3718 debugger_queue_command (debugger, "-var-update *", FALSE, FALSE, gdb_var_update, callback, user_data);
3721 GType
3722 debugger_get_type (void)
3724 static GType obj_type = 0;
3726 if (!obj_type)
3728 static const GTypeInfo obj_info =
3730 sizeof (DebuggerClass),
3731 (GBaseInitFunc) NULL,
3732 (GBaseFinalizeFunc) NULL,
3733 (GClassInitFunc) debugger_class_init,
3734 (GClassFinalizeFunc) NULL,
3735 NULL, /* class_data */
3736 sizeof (Debugger),
3737 0, /* n_preallocs */
3738 (GInstanceInitFunc) debugger_instance_init,
3739 NULL /* value_table */
3741 obj_type = g_type_register_static (G_TYPE_OBJECT,
3742 "Debugger", &obj_info, 0);
3744 return obj_type;
3747 static void
3748 debugger_dispose (GObject *obj)
3750 Debugger *debugger = DEBUGGER (obj);
3752 DEBUG_PRINT ("In function: debugger_shutdown()");
3754 /* Do not emit signal when the debugger is destroyed */
3755 debugger->priv->instance = NULL;
3756 debugger_abort (debugger);
3758 /* Good Bye message */
3759 if (debugger->priv->output_callback)
3761 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
3762 "Debugging session completed.\n",
3763 debugger->priv->output_user_data);
3766 if (debugger->priv->launcher)
3768 anjuta_launcher_reset (debugger->priv->launcher);
3769 g_object_unref (debugger->priv->launcher);
3770 debugger->priv->launcher = NULL;
3773 G_OBJECT_CLASS (parent_class)->dispose (obj);
3776 static void
3777 debugger_finalize (GObject *obj)
3779 Debugger *debugger = DEBUGGER (obj);
3780 g_string_free (debugger->priv->stdo_line, TRUE);
3781 g_string_free (debugger->priv->stdo_acc, TRUE);
3782 g_string_free (debugger->priv->stde_line, TRUE);
3783 g_free (debugger->priv);
3784 G_OBJECT_CLASS (parent_class)->finalize (obj);
3787 static void
3788 debugger_class_init (DebuggerClass * klass)
3790 GObjectClass *object_class;
3792 g_return_if_fail (klass != NULL);
3793 object_class = G_OBJECT_CLASS (klass);
3795 DEBUG_PRINT ("Initializing debugger class");
3797 parent_class = g_type_class_peek_parent (klass);
3798 object_class->dispose = debugger_dispose;
3799 object_class->finalize = debugger_finalize;
3803 #if 0 /* FIXME */
3804 void
3805 debugger_signal (const gchar *sig, gboolean show_msg)
3807 /* eg:- "SIGTERM" */
3808 gchar *buff;
3809 gchar *cmd;
3811 DEBUG_PRINT ("In function: debugger_signal()");
3813 if (debugger_is_active () == FALSE)
3814 return;
3815 if (debugger.prog_is_running == FALSE)
3816 return;
3817 if (debugger.child_pid < 1)
3819 DEBUG_PRINT ("Not sending signal - pid not known\n");
3820 return;
3823 if (show_msg)
3825 buff = g_strdup_printf (_("Sending signal %s to the process: %d"),
3826 sig, (int) debugger.child_pid);
3827 gdb_util_append_message (ANJUTA_PLUGIN (debugger.plugin), buff);
3828 g_free (buff);
3831 if (debugger_is_ready ())
3833 cmd = g_strconcat ("signal ", sig, NULL);
3834 stack_trace_set_frame (debugger.stack, 0);
3835 debugger_put_cmd_in_queqe (cmd, DB_CMD_ALL, NULL, NULL);
3836 debugger_put_cmd_in_queqe ("info program", DB_CMD_NONE,
3837 on_debugger_update_prog_status,
3838 NULL);
3839 g_free (cmd);
3840 debugger_execute_cmd_in_queqe ();
3842 else
3844 GtkWindow *parent;
3845 int status;
3847 parent = GTK_WINDOW (ANJUTA_PLUGIN (debugger.plugin)->shell);
3848 status = gdb_util_kill_process (debugger.child_pid, sig);
3849 if (status != 0 && show_msg)
3850 anjuta_util_dialog_error (parent,
3851 _("Error whilst signaling the process."));
3855 static void
3856 query_set_cmd (const gchar *cmd, gboolean state)
3858 gchar buffer[50];
3859 gchar *tmp = g_stpcpy (buffer, cmd);
3860 strcpy (tmp, state ? "on" : "off");
3861 debugger_put_cmd_in_queqe (buffer, DB_CMD_NONE, NULL, NULL);
3864 static void
3865 query_set_verbose (gboolean state)
3867 query_set_cmd ("set verbose ", state);
3870 static void
3871 query_set_print_staticmembers (gboolean state)
3873 query_set_cmd ("set print static-members ", state);
3876 static void
3877 query_set_print_pretty (gboolean state)
3879 query_set_cmd ("set print pretty ", state);
3882 void debugger_query_evaluate_expr_tip (const gchar *expr,
3883 DebuggerCLIFunc parser, gpointer data)
3885 query_set_verbose (FALSE);
3886 query_set_print_staticmembers (FALSE);
3887 gchar *printcmd = g_strconcat ("print ", expr, NULL);
3888 debugger_put_cmd_in_queqe (printcmd, DB_CMD_NONE, parser, data);
3889 query_set_verbose (TRUE);
3890 query_set_print_staticmembers (TRUE);
3891 g_free (printcmd);
3894 void
3895 debugger_query_evaluate_expression (const gchar *expr, DebuggerFunc parser,
3896 gpointer data)
3898 query_set_print_pretty (TRUE);
3899 query_set_verbose (FALSE);
3900 gchar *printcmd = g_strconcat ("print ", expr, NULL);
3901 debugger_put_cmd_in_queqe (printcmd, DB_CMD_SE_MESG | DB_CMD_SE_DIALOG,
3902 parser, data);
3903 query_set_print_pretty (FALSE);
3904 query_set_verbose (TRUE);
3905 g_free (printcmd);
3908 #endif