* plugins/debug-manager/registers.c:
[anjuta-git-plugin.git] / plugins / gdb / debugger.c
blob7161831ab0576dca58cb71a6c61e074e867015ca
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 gboolean
730 debugger_start (Debugger *debugger, const GList *search_dirs,
731 const gchar *prog, gboolean is_libtool_prog)
733 gchar *command_str, *dir, *tmp, *text, *msg;
734 gchar *exec_dir;
735 gboolean ret;
736 const GList *node;
737 AnjutaLauncher *launcher;
738 GList *dir_list = NULL;
739 gchar *term = NULL;
741 DEBUG_PRINT ("In function: debugger_start(%s) libtool %d", prog == NULL ? "(null)" : prog, is_libtool_prog);
743 if (anjuta_util_prog_is_installed ("gdb", TRUE) == FALSE)
744 return FALSE;
746 debugger_queue_clear (debugger);
748 tmp = g_strconcat (PACKAGE_DATA_DIR, "/", "gdb.init", NULL);
749 if (g_file_test (tmp, G_FILE_TEST_IS_REGULAR) == FALSE)
751 anjuta_util_dialog_error (debugger->priv->parent_win,
752 _("Unable to find: %s.\n"
753 "Unable to initialize debugger.\n"
754 "Make sure Anjuta is installed correctly."),
755 tmp);
756 g_free (tmp);
757 return FALSE;
759 g_free (tmp);
761 /* Prepare source search directories */
762 exec_dir = NULL;
763 if (prog)
764 exec_dir = g_path_get_dirname (prog);
766 if (exec_dir)
768 gchar *quoted_exec_dir = gdb_quote (exec_dir);
769 dir = g_strconcat (" -directory=\"", quoted_exec_dir, "\"", NULL);
770 g_free (quoted_exec_dir);
771 dir_list = g_list_prepend (dir_list, exec_dir);
773 else
775 dir = g_strdup (" ");
778 node = search_dirs;
779 while (node)
781 text = node->data;
782 if (strncmp (text, "file://", 7) == 0)
784 text += 7;
786 else
788 g_warning ("Debugger source search uri '%s' is not a local uri", text);
791 if (text[0] == '/')
793 tmp = g_strconcat (dir, " -directory=", text, NULL);
794 g_free (dir);
795 dir = tmp;
797 dir_list = g_list_prepend (dir_list, g_strdup (text));
799 else
801 g_warning ("Debugger source search dir '%s' is not absolute",
802 text);
804 node = g_list_next (node);
807 /* Now save the dir list. Order is automatically revesed */
808 node = dir_list;
809 while (node)
811 debugger->priv->search_dirs =
812 g_list_prepend (debugger->priv->search_dirs, node->data);
813 node = g_list_next (node);
815 g_list_free (dir_list);
817 if (prog && strlen(prog) > 0)
819 gchar *quoted_prog = gdb_quote (prog);
820 if (exec_dir)
821 chdir (exec_dir);
822 if (is_libtool_prog == FALSE)
824 command_str = g_strdup_printf (GDB_PATH " -f -n -i=mi2 %s %s "
825 "-x %s/gdb.init \"%s\"", dir, term == NULL ? "" : term,
826 PACKAGE_DATA_DIR, quoted_prog);
828 else
830 command_str = g_strdup_printf ("libtool --mode=execute " GDB_PATH
831 " -f -n -i=mi2 %s %s "
832 "-x %s/gdb.init \"%s\"", dir, term == NULL ? "" : term,
833 PACKAGE_DATA_DIR, quoted_prog);
835 g_free (quoted_prog);
837 else
839 if (is_libtool_prog == FALSE)
841 command_str = g_strdup_printf (GDB_PATH " -f -n -i=mi2 %s %s "
842 "-x %s/gdb.init ", term == NULL ? "" : term,
843 dir, PACKAGE_DATA_DIR);
845 else
847 command_str = g_strdup_printf ("libtool --mode=execute " GDB_PATH
848 " -f -n -i=mi2 %s %s -x "
849 "%s/gdb.init ",
850 dir, term == NULL ? "" : term, PACKAGE_DATA_DIR);
853 g_free (dir);
854 g_free (term);
855 debugger->priv->starting = TRUE;
856 debugger->priv->terminating = FALSE;
857 debugger->priv->loading = prog != NULL ? TRUE : FALSE;
858 debugger->priv->debugger_is_busy = 1;
860 /* Prepare for launch. */
861 launcher = debugger->priv->launcher;
862 anjuta_launcher_set_terminate_on_exit (launcher, TRUE);
863 g_signal_connect (G_OBJECT (launcher), "child-exited",
864 G_CALLBACK (on_gdb_terminated), debugger);
865 ret = anjuta_launcher_execute (launcher, command_str,
866 on_gdb_output_arrived, debugger);
868 if (ret)
870 debugger->priv->debugger_is_started = TRUE;
871 debugger->priv->prog_is_loaded = prog != NULL;
873 anjuta_launcher_set_encoding (launcher, "ISO-8859-1");
875 if (debugger->priv->output_callback != NULL)
877 if (ret == TRUE)
879 /* TODO anjuta_update_app_status (TRUE, _("Debugger")); */
880 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
881 _("Getting ready to start debugging "
882 "session...\n"),
883 debugger->priv->output_user_data);
885 if (prog)
887 msg = g_strconcat (_("Loading Executable: "), prog, "\n", NULL);
888 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
889 msg,
890 debugger->priv->output_user_data);
891 g_free (msg);
893 else
895 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
896 _("No executable specified.\n"),
897 debugger->priv->output_user_data);
898 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
899 _("Open an executable or attach "
900 "to a process to start "
901 "debugging.\n"),
902 debugger->priv->output_user_data);
905 else
907 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
908 _("There was an error whilst "
909 "launching the debugger.\n"),
910 debugger->priv->output_user_data);
911 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
912 _("Make sure 'gdb' is installed "
913 "on the system.\n"),
914 debugger->priv->output_user_data);
917 g_free (command_str);
919 return TRUE;
922 static void
923 gdb_stdout_line_arrived (Debugger *debugger, const gchar * chars)
925 gint i = 0;
927 while (chars[i])
929 if (chars[i] == '\n')
931 debugger_stdo_flush (debugger);
933 else
935 g_string_append_c (debugger->priv->stdo_line, chars[i]);
937 i++;
941 static void
942 gdb_stderr_line_arrived (Debugger *debugger, const gchar * chars)
944 gint i;
946 for (i = 0; i < strlen (chars); i++)
948 if (chars[i] == '\n')
949 debugger_stde_flush (debugger);
950 else
951 g_string_append_c (debugger->priv->stde_line, chars[i]);
955 static void
956 on_gdb_output_arrived (AnjutaLauncher *launcher,
957 AnjutaLauncherOutputType output_type,
958 const gchar *chars, gpointer data)
960 Debugger *debugger = DEBUGGER (data);
961 DEBUG_PRINT ("on gdb output arrived");
963 /* Do not emit signal when the debugger is destroyed */
964 if (debugger->priv->instance == NULL) return;
966 switch (output_type)
968 case ANJUTA_LAUNCHER_OUTPUT_STDERR:
969 gdb_stderr_line_arrived (debugger, chars);
970 break;
971 case ANJUTA_LAUNCHER_OUTPUT_STDOUT:
972 gdb_stdout_line_arrived (debugger, chars);
973 break;
974 default:
975 break;
979 static void
980 debugger_handle_post_execution (Debugger *debugger)
982 switch (debugger->priv->post_execution_flag)
984 case DEBUGGER_NONE:
985 break;
986 case DEBUGGER_EXIT:
987 DEBUG_PRINT ("debugger stop in handle post execution\n");
988 debugger_stop (debugger);
989 break;
990 case DEBUGGER_RERUN_PROGRAM:
991 DEBUG_PRINT ("debugger run in handle post execution\n");
992 debugger_run (debugger);
993 break;
994 default:
995 g_warning ("Execution should not reach here");
999 static void
1000 debugger_process_frame (Debugger *debugger, const GDBMIValue *val)
1002 const GDBMIValue *file, *line, *frame, *addr, *fullname, *thread;
1003 const gchar *file_str = NULL;
1004 guint line_num = 0;
1005 gulong addr_num = 0;
1007 g_return_if_fail (val != NULL);
1009 thread = gdbmi_value_hash_lookup (val, "thread-id");
1010 if (thread)
1012 debugger->priv->current_thread = strtoul (gdbmi_value_literal_get (thread), NULL, 0);
1014 debugger->priv->current_frame = 0;
1016 frame = gdbmi_value_hash_lookup (val, "frame");
1017 if (frame)
1019 fullname = gdbmi_value_hash_lookup (frame, "fullname");
1020 if (fullname)
1022 file_str = gdbmi_value_literal_get (fullname);
1023 if (*file_str == '\0') file_str = NULL;
1025 else
1027 file = gdbmi_value_hash_lookup (frame, "file");
1028 if (file)
1030 file_str = gdbmi_value_literal_get (file);
1031 if (*file_str == '\0') file_str = NULL;
1035 if (file_str != NULL)
1037 line = gdbmi_value_hash_lookup (frame, "line");
1038 if (line)
1040 line_num = strtoul (gdbmi_value_literal_get (line), NULL, 0);
1044 addr = gdbmi_value_hash_lookup (frame, "addr");
1045 if (addr)
1047 addr_num = strtoul (gdbmi_value_literal_get (addr), NULL, 0);
1049 debugger_program_moved (debugger, file_str, line_num, addr_num);
1053 static GError*
1054 gdb_parse_error (Debugger *debugger, const GDBMIValue *mi_results)
1056 const GDBMIValue *message;
1057 const gchar *literal;
1058 guint code = IANJUTA_DEBUGGER_UNKNOWN_ERROR;
1060 message = gdbmi_value_hash_lookup (mi_results, "msg");
1061 literal = gdbmi_value_literal_get (message);
1063 if ((mi_results != NULL)
1064 && ((message = gdbmi_value_hash_lookup (mi_results, "msg")) != NULL)
1065 && ((literal = gdbmi_value_literal_get (message)) != NULL)
1066 && (*literal != '\0'))
1068 code = gdb_match_error (literal);
1069 DEBUG_PRINT ("error code %d", code);
1071 else
1073 /* No error message */
1074 literal = "Error without a message";
1077 return g_error_new_literal (IANJUTA_DEBUGGER_ERROR, code, literal);
1081 /* Parsing output
1082 *---------------------------------------------------------------------------*/
1084 static void
1085 debugger_parse_output (Debugger *debugger)
1087 gchar *line;
1089 line = debugger->priv->stdo_line->str;
1091 if (line[0] == '\032' && line[1] == '\032')
1093 gchar *filename;
1094 guint lineno;
1096 gdb_util_parse_error_line (&(line[2]), &filename, &lineno);
1097 if (filename)
1099 debugger_program_moved (debugger, filename, lineno, 0);
1100 g_free (filename);
1103 else
1105 gchar *proper_msg;
1106 gsize len;
1108 len = strlen (line);
1109 if (line[1] == '\"' && line [strlen(line) - 1] == '\"')
1111 line[1] = line[0];
1112 /* Reserve space for an additional carriage return */
1113 line[strlen(line) - 1] = ' ';
1114 proper_msg = g_strcompress (line + 1);
1115 len = strlen (proper_msg) - 1;
1116 proper_msg[len] = '\0';
1118 else
1120 /* Reserve space for an additional carriage return */
1121 proper_msg = g_strndup (line, len + 1);
1124 if (strcmp(proper_msg, "~Stopped due to shared library event\n") == 0)
1126 /* Recognize a solib event */
1127 debugger->priv->solib_event = TRUE;
1128 g_free (proper_msg);
1130 else if (debugger->priv->current_cmd.parser)
1132 /* Save GDB CLI output */
1133 debugger->priv->cli_lines = g_list_prepend (debugger->priv->cli_lines,
1134 proper_msg);
1136 else
1138 /* Discard CLI output */
1139 g_free (proper_msg);
1144 static void
1145 debugger_parse_stopped (Debugger *debugger)
1147 gchar *line = debugger->priv->stdo_line->str;
1150 if (!debugger->priv->solib_event)
1152 gboolean program_exited = FALSE;
1153 GDBMIValue *val;
1155 /* Check if program has exited */
1156 val = gdbmi_value_parse (line);
1157 if (val)
1159 const GDBMIValue *reason;
1160 const gchar *str = NULL;
1162 debugger_process_frame (debugger, val);
1164 reason = gdbmi_value_hash_lookup (val, "reason");
1165 if (reason)
1166 str = gdbmi_value_literal_get (reason);
1168 if (str && (strncmp (str, "exited", 6) == 0))
1170 program_exited = TRUE;
1173 /* Emit signal received if necessary */
1174 if (str && strcmp (str, "exited-signalled") == 0)
1176 const GDBMIValue *signal_name, *signal_meaning;
1177 const gchar *signal_str, *signal_meaning_str;
1179 signal_name = gdbmi_value_hash_lookup (val, "signal-name");
1180 signal_str = gdbmi_value_literal_get (signal_name);
1181 signal_meaning = gdbmi_value_hash_lookup (val, "signal-meaning");
1182 signal_meaning_str = gdbmi_value_literal_get (signal_meaning);
1183 g_signal_emit_by_name (debugger->priv->instance, "signal-received", signal_str, signal_meaning_str);
1185 else if (str && strcmp (str, "signal-received") == 0)
1187 const GDBMIValue *signal_name, *signal_meaning;
1188 const gchar *signal_str, *signal_meaning_str;
1190 signal_name = gdbmi_value_hash_lookup (val, "signal-name");
1191 signal_str = gdbmi_value_literal_get (signal_name);
1192 signal_meaning = gdbmi_value_hash_lookup (val, "signal-meaning");
1193 signal_meaning_str = gdbmi_value_literal_get (signal_meaning);
1195 g_signal_emit_by_name (debugger->priv->instance, "signal-received", signal_str, signal_meaning_str);
1198 if (debugger->priv->output_callback)
1200 if (str && strcmp (str, "exited-normally") == 0)
1202 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1203 _("Program exited normally\n"),
1204 debugger->priv->output_user_data);
1206 else if (str && strcmp (str, "exited") == 0)
1208 const GDBMIValue *errcode;
1209 const gchar *errcode_str;
1210 gchar *msg;
1212 errcode = gdbmi_value_hash_lookup (val, "exit-code");
1213 errcode_str = gdbmi_value_literal_get (errcode);
1214 msg = g_strdup_printf (_("Program exited with error code %s\n"),
1215 errcode_str);
1216 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1217 msg, debugger->priv->output_user_data);
1218 g_free (msg);
1220 else if (str && strcmp (str, "breakpoint-hit") == 0)
1222 const GDBMIValue *bkptno;
1223 const gchar *bkptno_str;
1224 gchar *msg;
1226 bkptno = gdbmi_value_hash_lookup (val, "bkptno");
1227 bkptno_str = gdbmi_value_literal_get (bkptno);
1228 /* The program has reached one breakpoint and will stop */
1229 msg = g_strdup_printf (_("Breakpoint number %s hit\n"),
1230 bkptno_str);
1231 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1232 msg, debugger->priv->output_user_data);
1233 g_free (msg);
1235 else if (str && strcmp (str, "function-finished") == 0)
1237 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1238 _("Function finished\n"),
1239 debugger->priv->output_user_data);
1241 else if (str && strcmp (str, "end-stepping-range") == 0)
1243 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1244 _("Stepping finished\n"),
1245 debugger->priv->output_user_data);
1247 else if (str && strcmp (str, "location-reached") == 0)
1249 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1250 _("Location reached\n"),
1251 debugger->priv->output_user_data);
1256 if (program_exited)
1258 debugger->priv->prog_is_running = FALSE;
1259 debugger->priv->prog_is_attached = FALSE;
1260 debugger_handle_post_execution (debugger);
1261 debugger->priv->exiting = TRUE;
1263 else
1265 // g_signal_emit_by_name (debugger->priv->instance, "program-stopped");
1266 debugger->priv->stopping = TRUE;
1269 debugger->priv->cli_lines = g_list_reverse (debugger->priv->cli_lines);
1270 if ((debugger->priv->current_cmd.cmd != NULL) &&
1271 (debugger->priv->current_cmd.parser != NULL))
1273 debugger->priv->current_cmd.parser (debugger, val,
1274 debugger->priv->cli_lines, FALSE);
1275 debugger->priv->command_output_sent = TRUE;
1276 DEBUG_PRINT ("In function: Sending output...");
1279 if (val)
1280 gdbmi_value_free (val);
1284 static void
1285 debugger_parse_prompt (Debugger *debugger)
1287 /* If the parser has not yet been called, call it now. */
1288 if (debugger->priv->command_output_sent == FALSE &&
1289 debugger->priv->current_cmd.parser)
1291 debugger->priv->current_cmd.parser (debugger, NULL,
1292 debugger->priv->cli_lines, FALSE);
1293 debugger->priv->command_output_sent = TRUE;
1296 debugger->priv->debugger_is_busy--;
1297 debugger_queue_execute_command (debugger); /* Next command. Go. */
1298 debugger_emit_ready (debugger);
1301 static gboolean
1302 parse_breakpoint (IAnjutaDebuggerBreakpointItem* bp, const GDBMIValue *brkpnt)
1304 const GDBMIValue *literal;
1305 const gchar* value;
1307 memset (bp, 0, sizeof (*bp));
1309 literal = gdbmi_value_hash_lookup (brkpnt, "number");
1310 if (literal)
1312 value = gdbmi_value_literal_get (literal);
1313 bp->id = strtoul (value, NULL, 10);
1316 literal = gdbmi_value_hash_lookup (brkpnt, "fullname");
1317 if (literal == NULL) literal = gdbmi_value_hash_lookup (brkpnt, "file");
1318 if (literal)
1320 value = gdbmi_value_literal_get (literal);
1321 bp->file = (gchar *)value;
1324 literal = gdbmi_value_hash_lookup (brkpnt, "line");
1325 if (literal)
1327 value = gdbmi_value_literal_get (literal);
1328 bp->line = strtoul (value, NULL, 10);
1329 bp->type |= IANJUTA_DEBUGGER_BREAKPOINT_ON_LINE;
1332 literal = gdbmi_value_hash_lookup (brkpnt, "type");
1333 if (literal)
1335 value = gdbmi_value_literal_get (literal);
1338 literal = gdbmi_value_hash_lookup (brkpnt, "disp");
1339 if (literal)
1341 value = gdbmi_value_literal_get (literal);
1342 if (strcmp (value, "keep") == 0)
1344 bp->type |= IANJUTA_DEBUGGER_BREAKPOINT_WITH_TEMPORARY;
1345 bp->temporary = FALSE;
1347 else if ((strcmp (value, "nokeep") == 0) || (strcmp (value, "del") == 0))
1349 bp->type |= IANJUTA_DEBUGGER_BREAKPOINT_WITH_TEMPORARY;
1350 bp->temporary = TRUE;
1354 literal = gdbmi_value_hash_lookup (brkpnt, "enabled");
1355 if (literal)
1357 value = gdbmi_value_literal_get (literal);
1358 if (strcmp (value, "n") == 0)
1360 bp->type |= IANJUTA_DEBUGGER_BREAKPOINT_WITH_ENABLE;
1361 bp->enable = FALSE;
1363 else if (strcmp (value, "y") == 0)
1365 bp->type |= IANJUTA_DEBUGGER_BREAKPOINT_WITH_ENABLE;
1366 bp->enable = TRUE;
1370 literal = gdbmi_value_hash_lookup (brkpnt, "addr");
1371 if (literal)
1373 value = gdbmi_value_literal_get (literal);
1374 bp->address = strtoul (value, NULL, 16);
1375 bp->type |= IANJUTA_DEBUGGER_BREAKPOINT_ON_ADDRESS;
1378 literal = gdbmi_value_hash_lookup (brkpnt, "func");
1379 if (literal)
1381 value = gdbmi_value_literal_get (literal);
1382 bp->function = (gchar *)value;
1383 bp->type |= IANJUTA_DEBUGGER_BREAKPOINT_ON_FUNCTION;
1386 literal = gdbmi_value_hash_lookup (brkpnt, "times");
1387 if (literal)
1389 value = gdbmi_value_literal_get (literal);
1390 bp->times = strtoul (value, NULL, 10);
1391 bp->type |= IANJUTA_DEBUGGER_BREAKPOINT_WITH_TIME;
1393 DEBUG_PRINT("parse time %d", bp->times);
1395 literal = gdbmi_value_hash_lookup (brkpnt, "ignore");
1396 if (literal)
1398 value = gdbmi_value_literal_get (literal);
1399 bp->ignore = strtoul (value, NULL, 10);
1400 bp->type |= IANJUTA_DEBUGGER_BREAKPOINT_WITH_IGNORE;
1403 literal = gdbmi_value_hash_lookup (brkpnt, "cond");
1404 if (literal)
1406 value = gdbmi_value_literal_get (literal);
1407 bp->condition = (gchar *)value;
1408 bp->type |= IANJUTA_DEBUGGER_BREAKPOINT_WITH_CONDITION;
1411 return TRUE;
1414 static void
1415 debugger_stdo_flush (Debugger *debugger)
1417 gchar *line;
1419 line = debugger->priv->stdo_line->str;
1421 #ifdef DEBUG
1422 DEBUG_PRINT ("GDB:< %s", line);
1423 #else
1424 if (debugger->priv->gdb_log) g_message ("GDB:< %s", line);
1425 #endif
1426 debugger_log_output (debugger, line);
1427 if (strlen (line) == 0)
1429 return;
1431 if (strncasecmp (line, "^error", 6) == 0)
1433 /* GDB reported error */
1434 if ((debugger->priv->current_cmd.keep_result) || (debugger->priv->stdo_acc->len != 0))
1436 /* Keep result for next command */
1438 if (debugger->priv->stdo_acc->len == 0)
1440 g_string_append (debugger->priv->stdo_acc, line);
1442 else
1444 line = strchr (line, ',');
1445 if (line != NULL)
1447 g_string_append (debugger->priv->stdo_acc, line);
1450 line = debugger->priv->stdo_acc->str;
1453 GDBMIValue *val = gdbmi_value_parse (line);
1454 GError *error;
1456 error = gdb_parse_error (debugger, val);
1458 /* Trap state error */
1459 if ((error != NULL) && (error->code == IANJUTA_DEBUGGER_PROGRAM_NOT_FOUND))
1461 debugger->priv->prog_is_running = FALSE;
1462 debugger->priv->prog_is_attached = FALSE;
1463 debugger->priv->prog_is_loaded = FALSE;
1466 if (debugger->priv->current_cmd.parser != NULL)
1468 debugger->priv->current_cmd.parser (debugger, val, debugger->priv->cli_lines, error);
1469 debugger->priv->command_output_sent = TRUE; }
1470 DEBUG_PRINT("GDB: error %s", error->message);
1471 /*else
1473 anjuta_util_dialog_error (debugger->priv->parent_win, "%s", error->message);
1475 g_error_free (error);
1476 gdbmi_value_free (val);
1478 else if (strncasecmp(line, "^running", 8) == 0)
1480 /* Program started running */
1481 debugger->priv->prog_is_running = TRUE;
1482 /* debugger->priv->skip_next_prompt = TRUE; Replaced by debugger_is_busy++ */
1483 debugger->priv->debugger_is_busy++;
1484 g_signal_emit_by_name (debugger->priv->instance, "program-running");
1486 else if (strncasecmp (line, "*stopped", 8) == 0)
1488 /* Process has stopped */
1489 debugger_parse_stopped (debugger);
1491 else if (strncasecmp (line, "^done", 5) == 0)
1493 if ((debugger->priv->current_cmd.keep_result) || (debugger->priv->stdo_acc->len != 0))
1495 /* Keep result for next command */
1497 if (debugger->priv->stdo_acc->len == 0)
1499 g_string_append (debugger->priv->stdo_acc, line);
1501 else
1503 line = strchr (line, ',');
1504 if (line != NULL)
1506 g_string_append (debugger->priv->stdo_acc, line);
1509 line = debugger->priv->stdo_acc->str;
1512 if (!debugger->priv->current_cmd.keep_result)
1514 /* GDB command has reported output */
1515 GDBMIValue *val = gdbmi_value_parse (line);
1517 debugger->priv->cli_lines = g_list_reverse (debugger->priv->cli_lines);
1518 if ((debugger->priv->current_cmd.cmd != NULL) &&
1519 (debugger->priv->current_cmd.parser != NULL))
1521 debugger->priv->current_cmd.parser (debugger, val,
1522 debugger->priv->cli_lines, FALSE);
1523 debugger->priv->command_output_sent = TRUE;
1524 DEBUG_PRINT ("In function: Sending output...");
1526 else /* if (val) */
1528 /*g_signal_emit_by_name (debugger, "results-arrived",
1529 debugger->priv->current_cmd.cmd, val);*/
1532 if (val)
1534 /*debugger_process_frame (debugger, val);*/
1535 gdbmi_value_free (val);
1539 if (!debugger->priv->current_cmd.keep_result)
1541 g_string_assign (debugger->priv->stdo_acc, "");
1544 else if (strncasecmp (line, GDB_PROMPT, strlen (GDB_PROMPT)) == 0)
1546 debugger_parse_prompt (debugger);
1548 else
1550 debugger_parse_output (debugger);
1553 /* Clear the line buffer */
1554 g_string_assign (debugger->priv->stdo_line, "");
1557 void
1558 debugger_stde_flush (Debugger *debugger)
1560 if ((debugger->priv->output_callback) && (strlen (debugger->priv->stde_line->str) > 0))
1562 debugger->priv->output_callback (IANJUTA_DEBUGGER_ERROR_OUTPUT,
1563 debugger->priv->stde_line->str,
1564 debugger->priv->output_user_data);
1566 /* Clear the line buffer */
1567 g_string_assign (debugger->priv->stde_line, "");
1570 static void
1571 on_gdb_terminated (AnjutaLauncher *launcher,
1572 gint child_pid, gint status, gulong t,
1573 gpointer data)
1575 Debugger *debugger = DEBUGGER (data);
1576 GError *err = NULL;
1578 g_signal_handlers_disconnect_by_func (G_OBJECT (launcher),
1579 G_CALLBACK (on_gdb_terminated),
1580 debugger);
1582 DEBUG_PRINT ("In function: gdb_terminated()");
1584 /* Clear the command queue & Buffer */
1585 debugger_clear_buffers (debugger);
1587 /* Good Bye message */
1588 /*if (!debugger->priv->terminating)
1590 anjuta_util_dialog_error (debugger->priv->parent_win,
1591 _("gdb terminated unexpectedly with status %d\n"), status);
1593 debugger->priv->prog_is_running = FALSE;
1594 debugger->priv->prog_is_attached = FALSE;
1595 debugger->priv->prog_is_loaded = FALSE;
1596 debugger->priv->debugger_is_busy = 0;
1597 debugger->priv->skip_next_prompt = FALSE;
1598 if (!debugger->priv->terminating)
1600 err = g_error_new (IANJUTA_DEBUGGER_ERROR,
1601 IANJUTA_DEBUGGER_OTHER_ERROR,
1602 "gdb terminated with status %d", status);
1604 debugger->priv->terminating = FALSE;
1605 debugger->priv->debugger_is_started = FALSE;
1606 if (debugger->priv->instance)
1608 g_signal_emit_by_name (debugger->priv->instance, "debugger-stopped", err);
1610 if (err != NULL) g_error_free (err);
1613 static void
1614 debugger_stop_real (Debugger *debugger)
1616 DEBUG_PRINT ("In function: debugger_stop_real()");
1618 /* if program is attached - detach from it before quiting */
1619 if (debugger->priv->prog_is_attached == TRUE)
1621 debugger_detach_process(debugger);
1624 debugger->priv->terminating = TRUE;
1625 debugger_queue_command (debugger, "-gdb-exit", FALSE, FALSE, NULL, NULL, NULL);
1628 gboolean
1629 debugger_stop (Debugger *debugger)
1631 #if 0
1632 gboolean ret = TRUE;
1634 if (debugger->priv->prog_is_running == TRUE)
1636 GtkWidget *dialog;
1637 gchar *mesg;
1639 if (debugger->priv->prog_is_attached == TRUE)
1640 mesg = _("The program is attached.\n"
1641 "Do you still want to stop the debugger?");
1642 else
1643 mesg = _("The program is running.\n"
1644 "Do you still want to stop the debugger?");
1645 dialog = gtk_message_dialog_new (debugger->priv->parent_win,
1646 GTK_DIALOG_DESTROY_WITH_PARENT,
1647 GTK_MESSAGE_QUESTION,
1648 GTK_BUTTONS_NONE, mesg);
1649 gtk_dialog_add_buttons (GTK_DIALOG (dialog),
1650 GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
1651 GTK_STOCK_STOP, GTK_RESPONSE_YES,
1652 NULL);
1653 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_YES)
1654 debugger_stop_real (debugger);
1655 else
1656 ret = FALSE;
1657 gtk_widget_destroy (dialog);
1659 else
1660 debugger_stop_real (debugger);
1661 return ret;
1662 #endif
1663 debugger_stop_real (debugger);
1665 return TRUE;
1668 gboolean
1669 debugger_abort (Debugger *debugger)
1671 DEBUG_PRINT ("In function: debugger_abort()");
1673 /* Stop inferior */
1674 if ((debugger->priv->prog_is_attached == FALSE) && (debugger->priv->inferior_pid != 0))
1676 kill (debugger->priv->inferior_pid, SIGTERM);
1677 debugger->priv->inferior_pid = 0;
1680 /* Stop gdb */
1681 debugger->priv->terminating = TRUE;
1682 g_signal_handlers_disconnect_by_func (G_OBJECT (debugger->priv->launcher), G_CALLBACK (on_gdb_terminated), debugger);
1683 anjuta_launcher_reset (debugger->priv->launcher);
1685 /* Free memory */
1686 debugger_queue_clear (debugger);
1687 g_list_foreach (debugger->priv->search_dirs, (GFunc)g_free, NULL);
1688 g_list_free (debugger->priv->search_dirs);
1689 debugger->priv->search_dirs = NULL;
1691 /* Emit signal, state of the debugger must be DEBUGGER_STOPPED */
1692 debugger->priv->prog_is_running = FALSE;
1693 debugger->priv->prog_is_attached = FALSE;
1694 debugger->priv->inferior_pid = 0;
1695 debugger->priv->prog_is_loaded = FALSE;
1696 debugger->priv->debugger_is_busy = 0;
1697 debugger->priv->debugger_is_started = FALSE;
1698 if (debugger->priv->instance != NULL)
1700 g_signal_emit_by_name (debugger->priv->instance, "debugger-stopped", NULL);
1701 debugger->priv->instance = NULL;
1704 return TRUE;
1707 gchar*
1708 debugger_get_source_path (Debugger *debugger, const gchar *file)
1710 GList *node;
1711 gchar *path = NULL;
1713 if (g_path_is_absolute (file))
1714 return g_strdup (file);
1716 node = debugger->priv->search_dirs;
1717 while (node)
1719 path = g_build_filename (node->data, file, NULL);
1720 if (g_file_test (path, G_FILE_TEST_EXISTS))
1721 break;
1722 g_free (path);
1723 path = NULL;
1724 node = g_list_next (node);
1727 if (path == NULL)
1729 /* The file could be found nowhere. Use current directory */
1730 gchar *cwd;
1731 cwd = g_get_current_dir ();
1732 path = g_build_filename (cwd, file, NULL);
1733 g_free (cwd);
1735 return path;
1738 void
1739 debugger_set_output_callback (Debugger *debugger, IAnjutaDebuggerOutputCallback callback, gpointer user_data)
1741 debugger->priv->output_callback = callback;
1742 debugger->priv->output_user_data = user_data;
1745 void
1746 debugger_program_moved (Debugger *debugger, const gchar *file,
1747 gint line, gulong address)
1749 gchar *src_path;
1751 if ((file != NULL) && (*file != G_DIR_SEPARATOR))
1753 src_path = debugger_get_source_path (debugger, file);
1754 g_signal_emit_by_name (debugger->priv->instance, "program-moved", debugger->priv->inferior_pid, debugger->priv->current_thread, address, src_path, line);
1755 g_free (src_path);
1757 else
1759 g_signal_emit_by_name (debugger->priv->instance, "program-moved", debugger->priv->inferior_pid, debugger->priv->current_thread, address, file, line);
1763 static void
1764 debugger_info_program_finish (Debugger *debugger, const GDBMIValue *mi_results,
1765 const GList *cli_results, GError *error)
1767 DEBUG_PRINT ("In function: debugger_info_program()");
1769 /* Hack: find message string giving inferior pid */
1770 while (cli_results != NULL)
1772 gchar* child_proc;
1774 child_proc = strstr(cli_results->data, " child process ");
1775 if (child_proc != NULL)
1777 debugger->priv->inferior_pid = strtoul (child_proc + 15, NULL, 10);
1778 break;
1780 cli_results = g_list_next (cli_results);
1784 void
1785 debugger_start_program (Debugger *debugger, const gchar* args, const gchar* tty, gboolean stop)
1787 gchar *cmd;
1789 DEBUG_PRINT ("In function: debugger_start_program()");
1791 g_return_if_fail (IS_DEBUGGER (debugger));
1792 g_return_if_fail (debugger->priv->prog_is_running == FALSE);
1794 /* Without a terminal, the output of the debugged program
1795 * are lost */
1796 if (tty)
1798 cmd = g_strdup_printf ("-inferior-tty-set %s", tty);
1799 debugger_queue_command (debugger, cmd, FALSE, FALSE, NULL, NULL, NULL);
1800 g_free (cmd);
1803 debugger->priv->inferior_pid = 0;
1804 if (stop)
1806 debugger_queue_command (debugger, "-break-insert -t main", FALSE, FALSE, NULL, NULL, NULL);
1808 if (args && (*args))
1810 cmd = g_strconcat ("-exec-arguments ", args, NULL);
1811 debugger_queue_command (debugger, cmd, FALSE, FALSE, NULL, NULL, NULL);
1812 g_free (cmd);
1815 debugger_queue_command (debugger, "-exec-run", FALSE, FALSE, NULL, NULL, NULL);
1817 /* Get pid of program on next stop */
1818 debugger_queue_command (debugger, "info program", FALSE, FALSE, debugger_info_program_finish, NULL, NULL);
1819 debugger->priv->post_execution_flag = DEBUGGER_NONE;
1822 static void
1823 debugger_attach_process_finish (Debugger *debugger, const GDBMIValue *mi_results,
1824 const GList *cli_results, GError *error)
1826 DEBUG_PRINT ("Program attach finished");
1827 if (debugger->priv->output_callback)
1829 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1830 _("Program attached\n"),
1831 debugger->priv->output_user_data);
1833 debugger->priv->prog_is_attached = TRUE;
1834 debugger->priv->prog_is_running = TRUE;
1835 /* It is not really a shared lib event, but it allows to restart
1836 * the program after setting breakpoints. It is better to restart
1837 * it because we don't have the normal stop frame that tell where
1838 * the program is stopped */
1839 debugger->priv->solib_event = TRUE;
1842 static void
1843 debugger_attach_process_real (Debugger *debugger, pid_t pid)
1845 gchar *buff;
1847 DEBUG_PRINT ("In function: debugger_attach_process_real()");
1849 if (debugger->priv->output_callback)
1851 buff = g_strdup_printf (_("Attaching to process: %d...\n"), pid);
1852 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1853 buff, debugger->priv->output_user_data);
1854 g_free (buff);
1857 debugger->priv->inferior_pid = pid;
1858 buff = g_strdup_printf ("attach %d", pid);
1859 debugger_queue_command (debugger, buff, FALSE, FALSE,
1860 debugger_attach_process_finish, NULL, NULL);
1861 g_free (buff);
1864 void
1865 debugger_attach_process (Debugger *debugger, pid_t pid)
1867 DEBUG_PRINT ("In function: debugger_attach_process()");
1869 g_return_if_fail (IS_DEBUGGER (debugger));
1871 if (debugger->priv->prog_is_running == TRUE)
1873 // TODO: Dialog to be made HIG compliant.
1874 gchar *mesg;
1875 GtkWidget *dialog;
1877 mesg = _("A process is already running.\n"
1878 "Would you like to terminate it and attach the new process?"),
1879 dialog = gtk_message_dialog_new (debugger->priv->parent_win,
1880 GTK_DIALOG_DESTROY_WITH_PARENT,
1881 GTK_MESSAGE_QUESTION,
1882 GTK_BUTTONS_YES_NO, mesg);
1883 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_YES)
1885 debugger_stop_program (debugger);
1886 debugger_attach_process_real (debugger, pid);
1888 gtk_widget_destroy (dialog);
1890 else if (getpid () == pid ||
1891 anjuta_launcher_get_child_pid (debugger->priv->launcher) == pid)
1893 anjuta_util_dialog_error (debugger->priv->parent_win,
1894 _("Anjuta is unable to attach to itself."));
1895 return;
1897 else
1898 debugger_attach_process_real (debugger, pid);
1901 void
1902 debugger_restart_program (Debugger *debugger)
1904 DEBUG_PRINT ("In function: debugger_restart_program()");
1906 g_return_if_fail (debugger->priv->prog_is_attached == FALSE);
1909 debugger->priv->post_execution_flag = DEBUGGER_RERUN_PROGRAM;
1910 debugger_stop_program (debugger);
1912 debugger_put_cmd_in_queqe ("tbreak main", DB_CMD_NONE, NULL, NULL);
1913 debugger_put_cmd_in_queqe ("run >/dev/null 2>/dev/null", DB_CMD_ALL,
1914 NULL, NULL);
1915 debugger_put_cmd_in_queqe ("info program", DB_CMD_NONE,
1916 on_debugger_update_prog_status, NULL);
1917 debugger_put_cmd_in_queqe ("continue", DB_CMD_NONE, NULL, NULL);
1918 debugger_execute_cmd_in_queqe ();
1922 void
1923 debugger_stop_program (Debugger *debugger)
1925 DEBUG_PRINT ("In function: debugger_stop_program()");
1927 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
1929 if (debugger->priv->prog_is_attached == TRUE)
1931 debugger_detach_process (debugger);
1933 else
1935 /* FIXME: Why doesn't -exec-abort work??? */
1936 /* debugger_queue_command (debugger, "-exec-abort", NULL, NULL); */
1937 debugger_queue_command (debugger, "kill", FALSE, FALSE, NULL, NULL, NULL);
1938 debugger->priv->prog_is_running = FALSE;
1939 debugger->priv->prog_is_attached = FALSE;
1940 g_signal_emit_by_name (debugger->priv->instance, "program-exited");
1941 if (debugger->priv->output_callback)
1943 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1944 _("Program terminated\n"),
1945 debugger->priv->output_user_data);
1947 debugger_handle_post_execution (debugger);
1951 static void
1952 debugger_detach_process_finish (Debugger *debugger, const GDBMIValue *mi_results,
1953 const GList *cli_results, GError *error)
1955 DEBUG_PRINT ("Program detach finished");
1956 if (debugger->priv->output_callback)
1958 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1959 _("Program detached\n"),
1960 debugger->priv->output_user_data);
1962 debugger->priv->inferior_pid = 0;
1963 debugger->priv->prog_is_attached = FALSE;
1964 debugger->priv->prog_is_running = FALSE;
1965 g_signal_emit_by_name (debugger->priv->instance, "program-exited");
1968 void
1969 debugger_detach_process (Debugger *debugger)
1971 gchar *buff;
1973 DEBUG_PRINT ("In function: debugger_detach_process()");
1975 g_return_if_fail (debugger->priv->prog_is_attached == TRUE);
1977 if (debugger->priv->output_callback)
1979 buff = g_strdup_printf (_("Detaching the process...\n"));
1980 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1981 buff, debugger->priv->output_user_data);
1982 g_free (buff);
1985 debugger_queue_command (debugger, "detach", FALSE, FALSE,
1986 debugger_detach_process_finish, NULL, NULL);
1989 void
1990 debugger_interrupt (Debugger *debugger)
1992 DEBUG_PRINT ("In function: debugger_interrupt()");
1994 g_return_if_fail (IS_DEBUGGER (debugger));
1995 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
1997 if (debugger->priv->output_callback)
1999 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
2000 _("Interrupting the process\n"),
2001 debugger->priv->output_user_data);
2004 if (debugger->priv->inferior_pid == 0)
2006 /* In case we do not have the inferior pid, send signal to gdb */
2007 anjuta_launcher_signal (debugger->priv->launcher, SIGINT);
2009 else
2011 /* Send signal directly to inferior */
2012 kill (debugger->priv->inferior_pid, SIGINT);
2014 //g_signal_emit_by_name (debugger->priv->instance, "program-running");
2017 void
2018 debugger_run (Debugger *debugger)
2020 DEBUG_PRINT ("In function: debugger_run()");
2022 g_return_if_fail (IS_DEBUGGER (debugger));
2023 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
2025 /* Program running - continue */
2026 debugger_queue_command (debugger, "-exec-continue", FALSE, FALSE, NULL, NULL, NULL);
2029 void
2030 debugger_step_in (Debugger *debugger)
2032 DEBUG_PRINT ("In function: debugger_step_in()");
2034 g_return_if_fail (IS_DEBUGGER (debugger));
2035 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
2037 debugger_queue_command (debugger, "-exec-step", FALSE, FALSE, NULL, NULL, NULL);
2040 void
2041 debugger_step_over (Debugger *debugger)
2043 DEBUG_PRINT ("In function: debugger_step_over()");
2045 g_return_if_fail (IS_DEBUGGER (debugger));
2046 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
2048 debugger_queue_command (debugger, "-exec-next", FALSE, FALSE, NULL, NULL, NULL);
2051 void
2052 debugger_step_out (Debugger *debugger)
2054 DEBUG_PRINT ("In function: debugger_step_out()");
2056 g_return_if_fail (IS_DEBUGGER (debugger));
2057 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
2059 debugger_queue_command (debugger, "-exec-finish", FALSE, FALSE, NULL, NULL, NULL);
2062 void
2063 debugger_stepi_in (Debugger *debugger)
2065 DEBUG_PRINT ("In function: debugger_step_in()");
2067 g_return_if_fail (IS_DEBUGGER (debugger));
2068 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
2070 debugger_queue_command (debugger, "-exec-step-instruction", FALSE, FALSE, NULL, NULL, NULL);
2073 void
2074 debugger_stepi_over (Debugger *debugger)
2076 DEBUG_PRINT ("In function: debugger_step_over()");
2078 g_return_if_fail (IS_DEBUGGER (debugger));
2079 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
2081 debugger_queue_command (debugger, "-exec-next-instruction", FALSE, FALSE, NULL, NULL, NULL);
2084 void
2085 debugger_run_to_location (Debugger *debugger, const gchar *loc)
2087 gchar *buff;
2089 DEBUG_PRINT ("In function: debugger_run_to_location()");
2091 g_return_if_fail (IS_DEBUGGER (debugger));
2092 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
2094 buff = g_strdup_printf ("-exec-until %s", loc);
2095 debugger_queue_command (debugger, buff, FALSE, FALSE, NULL, NULL, NULL);
2096 g_free (buff);
2099 void
2100 debugger_run_to_position (Debugger *debugger, const gchar *file, guint line)
2102 gchar *buff;
2103 gchar *quoted_file;
2105 DEBUG_PRINT ("In function: debugger_run_to_position()");
2107 g_return_if_fail (IS_DEBUGGER (debugger));
2108 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
2110 quoted_file = gdb_quote (file);
2111 buff = g_strdup_printf ("-break-insert -t \"\\\"%s\\\":%u\"", quoted_file, line);
2112 g_free (quoted_file);
2113 debugger_queue_command (debugger, buff, FALSE, FALSE, NULL, NULL, NULL);
2114 g_free (buff);
2115 debugger_queue_command (debugger, "-exec-continue", FALSE, FALSE, NULL, NULL, NULL);
2118 void
2119 debugger_run_to_address (Debugger *debugger, gulong address)
2121 gchar *buff;
2123 DEBUG_PRINT ("In function: debugger_run_to_address()");
2125 g_return_if_fail (IS_DEBUGGER (debugger));
2126 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
2128 buff = g_strdup_printf ("-break-insert -t *0x%lx", address);
2129 debugger_queue_command (debugger, buff, FALSE, FALSE, NULL, NULL, NULL);
2130 g_free (buff);
2131 debugger_queue_command (debugger, "-exec-continue", FALSE, FALSE, NULL, NULL, NULL);
2134 void
2135 debugger_command (Debugger *debugger, const gchar *command,
2136 gboolean suppress_error, DebuggerParserFunc parser,
2137 gpointer user_data)
2139 if (strncasecmp (command, "-exec-run",
2140 strlen ("-exec-run")) == 0 ||
2141 strncasecmp (command, "run", strlen ("run")) == 0)
2143 /* FIXME: The user might have passed args to the command */
2144 debugger_run (debugger);
2146 else if (strncasecmp (command, "-exec-step",
2147 strlen ("-exec-step")) == 0 ||
2148 strncasecmp (command, "step", strlen ("step")) == 0)
2150 debugger_step_in (debugger);
2152 else if (strncasecmp (command, "-exec-next",
2153 strlen ("-exec-next")) == 0 ||
2154 strncasecmp (command, "next", strlen ("next")) == 0)
2156 debugger_step_over (debugger);
2158 else if (strncasecmp (command, "-exec-finish",
2159 strlen ("-exec-finish")) == 0 ||
2160 strncasecmp (command, "finish", strlen ("finish")) == 0)
2162 debugger_step_out (debugger);
2164 else if (strncasecmp (command, "-exec-continue",
2165 strlen ("-exec-continue")) == 0 ||
2166 strncasecmp (command, "continue", strlen ("continue")) == 0)
2168 debugger_run (debugger);
2170 else if (strncasecmp (command, "-exec-until",
2171 strlen ("-exec-until")) == 0 ||
2172 strncasecmp (command, "until", strlen ("until")) == 0)
2174 debugger_run_to_location (debugger, strchr (command, ' '));
2176 else if (strncasecmp (command, "-exec-abort",
2177 strlen ("-exec-abort")) == 0 ||
2178 strncasecmp (command, "kill", strlen ("kill")) == 0)
2180 debugger_stop_program (debugger);
2182 else if (strncasecmp (command, "-target-attach",
2183 strlen ("-target-attach")) == 0 ||
2184 strncasecmp (command, "attach", strlen ("attach")) == 0)
2186 pid_t pid = 0;
2187 gchar *pid_str = strchr (command, ' ');
2188 if (pid_str)
2189 pid = atoi (pid_str);
2190 debugger_attach_process (debugger, pid);
2192 else if (strncasecmp (command, "-target-detach",
2193 strlen ("-target-detach")) == 0 ||
2194 strncasecmp (command, "detach", strlen ("detach")) == 0)
2196 debugger_detach_process (debugger);
2198 else if (strncasecmp (command, "-file-exec-and-symbols",
2199 strlen ("-file-exec-and-symbols")) == 0 ||
2200 strncasecmp (command, "file", strlen ("file")) == 0)
2202 debugger_load_executable (debugger, strchr (command, ' '));
2204 else if (strncasecmp (command, "core", strlen ("core")) == 0)
2206 debugger_load_core (debugger, strchr (command, ' '));
2208 else
2210 debugger_queue_command (debugger, command, suppress_error, FALSE,
2211 parser, user_data, NULL);
2215 static void
2216 debugger_add_breakpoint_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2218 IAnjutaDebuggerBreakpointItem bp;
2219 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2220 gpointer user_data = debugger->priv->current_cmd.user_data;
2222 if ((error != NULL) || (mi_results == NULL))
2224 /* Call callback in all case (useful for enable that doesn't return
2225 * anything */
2226 if (callback != NULL)
2227 callback (NULL, user_data, error);
2229 else if (callback != NULL)
2231 const GDBMIValue *brkpnt;
2233 brkpnt = gdbmi_value_hash_lookup (mi_results, "bkpt");
2234 parse_breakpoint (&bp, brkpnt);
2236 /* Call callback in all case (useful for enable that doesn't return
2237 * anything */
2238 callback (&bp, user_data, error);
2243 void
2244 debugger_add_breakpoint_at_line (Debugger *debugger, const gchar *file, guint line, IAnjutaDebuggerCallback callback, gpointer user_data)
2246 gchar *buff;
2247 gchar *quoted_file;
2249 DEBUG_PRINT ("In function: debugger_add_breakpoint()");
2251 g_return_if_fail (IS_DEBUGGER (debugger));
2253 quoted_file = gdb_quote (file);
2255 buff = g_strdup_printf ("-break-insert \"\\\"%s\\\":%u\"", quoted_file, line);
2256 g_free (quoted_file);
2257 debugger_queue_command (debugger, buff, FALSE, FALSE, debugger_add_breakpoint_finish, callback, user_data);
2258 g_free (buff);
2261 void
2262 debugger_add_breakpoint_at_function (Debugger *debugger, const gchar *file, const gchar *function, IAnjutaDebuggerCallback callback, gpointer user_data)
2264 gchar *buff;
2266 DEBUG_PRINT ("In function: debugger_add_breakpoint()");
2268 g_return_if_fail (IS_DEBUGGER (debugger));
2270 buff = g_strdup_printf ("-break-insert %s%s%s", file == NULL ? "" : file, file == NULL ? "" : ":" , function);
2271 debugger_queue_command (debugger, buff, FALSE, FALSE, debugger_add_breakpoint_finish, callback, user_data);
2272 g_free (buff);
2275 void
2276 debugger_add_breakpoint_at_address (Debugger *debugger, gulong address, IAnjutaDebuggerCallback callback, gpointer user_data)
2278 gchar *buff;
2280 DEBUG_PRINT ("In function: debugger_add_breakpoint()");
2282 g_return_if_fail (IS_DEBUGGER (debugger));
2284 buff = g_strdup_printf ("-break-insert *0x%lx", address);
2285 debugger_queue_command (debugger, buff, FALSE, FALSE, debugger_add_breakpoint_finish, callback, user_data);
2286 g_free (buff);
2289 void
2290 debugger_enable_breakpoint (Debugger *debugger, guint id, gboolean enable, IAnjutaDebuggerCallback callback, gpointer user_data)
2293 gchar *buff;
2295 DEBUG_PRINT ("In function: debugger_enable_breakpoint()");
2297 g_return_if_fail (IS_DEBUGGER (debugger));
2299 buff = g_strdup_printf (enable ? "-break-enable %d" : "-break-disable %d",id);
2300 debugger_queue_command (debugger, buff, FALSE, FALSE, debugger_add_breakpoint_finish, callback, user_data);
2301 g_free (buff);
2304 void
2305 debugger_ignore_breakpoint (Debugger *debugger, guint id, guint ignore, IAnjutaDebuggerCallback callback, gpointer user_data)
2307 gchar *buff;
2309 DEBUG_PRINT ("In function: debugger_ignore_breakpoint()");
2311 g_return_if_fail (IS_DEBUGGER (debugger));
2313 buff = g_strdup_printf ("-break-after %d %d", id, ignore);
2314 debugger_queue_command (debugger, buff, FALSE, FALSE, debugger_add_breakpoint_finish, callback, user_data);
2315 g_free (buff);
2318 void
2319 debugger_condition_breakpoint (Debugger *debugger, guint id, const gchar *condition, IAnjutaDebuggerCallback callback, gpointer user_data)
2321 gchar *buff;
2323 DEBUG_PRINT ("In function: debugger_ignore_breakpoint()");
2325 g_return_if_fail (IS_DEBUGGER (debugger));
2327 buff = g_strdup_printf ("-break-condition %d %s", id, condition == NULL ? "" : condition);
2328 debugger_queue_command (debugger, buff, FALSE, FALSE, debugger_add_breakpoint_finish, callback, user_data);
2329 g_free (buff);
2332 static void
2333 debugger_remove_breakpoint_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2335 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2336 gpointer user_data = debugger->priv->current_cmd.user_data;
2337 IAnjutaDebuggerBreakpointItem bp;
2339 bp.type = IANJUTA_DEBUGGER_BREAKPOINT_REMOVED;
2340 bp.id = atoi (debugger->priv->current_cmd.cmd + 14);
2341 if (callback != NULL)
2342 callback (&bp, user_data, NULL);
2345 void
2346 debugger_remove_breakpoint (Debugger *debugger, guint id, IAnjutaDebuggerCallback callback, gpointer user_data)
2348 gchar *buff;
2350 DEBUG_PRINT ("In function: debugger_delete_breakpoint()");
2352 g_return_if_fail (IS_DEBUGGER (debugger));
2354 buff = g_strdup_printf ("-break-delete %d", id);
2355 debugger_queue_command (debugger, buff, FALSE, FALSE, debugger_remove_breakpoint_finish, callback, user_data);
2356 g_free (buff);
2359 static void
2360 debugger_list_breakpoint_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2362 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2363 gpointer user_data = debugger->priv->current_cmd.user_data;
2364 IAnjutaDebuggerBreakpointItem* bp;
2365 const GDBMIValue *table;
2366 GList *list = NULL;
2368 if ((error != NULL) || (mi_results == NULL))
2370 /* Call callback in all case (useful for enable that doesn't return
2371 * anything */
2372 if (callback != NULL)
2373 callback (NULL, user_data, error);
2376 table = gdbmi_value_hash_lookup (mi_results, "BreakpointTable");
2377 if (table)
2379 table = gdbmi_value_hash_lookup (table, "body");
2381 if (table)
2383 int i;
2385 for (i = 0; i < gdbmi_value_get_size (table); i++)
2387 const GDBMIValue *brkpnt;
2389 bp = g_new0 (IAnjutaDebuggerBreakpointItem, 1);
2391 brkpnt = gdbmi_value_list_get_nth (table, i);
2392 parse_breakpoint(bp, brkpnt);
2393 list = g_list_prepend (list, bp);
2398 /* Call callback in all case (useful for enable that doesn't return
2399 * anything */
2400 if (callback != NULL)
2402 list = g_list_reverse (list);
2403 callback (list, user_data, error);
2406 g_list_foreach (list, (GFunc)g_free, NULL);
2407 g_list_free (list);
2410 void
2411 debugger_list_breakpoint (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2413 DEBUG_PRINT ("In function: debugger_list_breakpoint()");
2415 g_return_if_fail (IS_DEBUGGER (debugger));
2417 debugger_queue_command (debugger, "-break-list", FALSE, FALSE, debugger_list_breakpoint_finish, callback, user_data);
2420 static void
2421 debugger_print_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2423 gchar *ptr = NULL;
2424 gchar *tmp;
2425 GList *list, *node;
2426 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2427 gpointer user_data = debugger->priv->current_cmd.user_data;
2430 list = gdb_util_remove_blank_lines (cli_results);
2431 if (g_list_length (list) < 1)
2433 tmp = NULL;
2435 else
2437 tmp = strchr ((gchar *) list->data, '=');
2439 if (tmp != NULL)
2441 ptr = g_strdup (tmp);
2442 for (node = list ? list->next : NULL; node != NULL; node = node->next)
2444 tmp = ptr;
2445 ptr = g_strconcat (tmp, (gchar *) node->data, NULL);
2446 g_free (tmp);
2450 callback (ptr, user_data, NULL);
2451 g_free (ptr);
2454 void
2455 debugger_print (Debugger *debugger, const gchar* variable, IAnjutaDebuggerCallback callback, gpointer user_data)
2457 gchar *buff;
2459 DEBUG_PRINT ("In function: debugger_print()");
2461 g_return_if_fail (IS_DEBUGGER (debugger));
2463 buff = g_strdup_printf ("print %s", variable);
2464 debugger_queue_command (debugger, buff, TRUE, FALSE, debugger_print_finish, callback, user_data);
2465 g_free (buff);
2468 static void
2469 debugger_evaluate_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2472 const GDBMIValue *value = NULL;
2473 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2474 gpointer user_data = debugger->priv->current_cmd.user_data;
2476 if (mi_results)
2477 value = gdbmi_value_hash_lookup (mi_results, "value");
2479 /* Call user function */
2480 if (callback != NULL)
2481 callback (value == NULL ? "?" : (char *)gdbmi_value_literal_get (value), user_data, NULL);
2484 void
2485 debugger_evaluate (Debugger *debugger, const gchar* name, IAnjutaDebuggerCallback callback, gpointer user_data)
2487 gchar *buff;
2488 DEBUG_PRINT ("In function: debugger_add_watch()");
2490 g_return_if_fail (IS_DEBUGGER (debugger));
2492 buff = g_strdup_printf ("-data-evaluate-expression %s", name);
2493 debugger_queue_command (debugger, buff, TRUE, FALSE, debugger_evaluate_finish, callback, user_data);
2494 g_free (buff);
2497 static void
2498 debugger_list_local_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2501 const GDBMIValue *local, *var, *frame, *args, *stack;
2502 const gchar * name;
2503 GList* list;
2504 guint i;
2505 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2506 gpointer user_data = debugger->priv->current_cmd.user_data;
2509 list = NULL;
2510 /* Add arguments */
2511 stack = gdbmi_value_hash_lookup (mi_results, "stack-args");
2512 if (stack)
2514 frame = gdbmi_value_list_get_nth (stack, 0);
2515 if (frame)
2517 args = gdbmi_value_hash_lookup (frame, "args");
2518 if (args)
2520 for (i = 0; i < gdbmi_value_get_size (args); i++)
2522 var = gdbmi_value_list_get_nth (args, i);
2523 if (var)
2525 name = gdbmi_value_literal_get (var);
2526 list = g_list_prepend (list, (gchar *)name);
2534 /* List local variables */
2535 local = gdbmi_value_hash_lookup (mi_results, "locals");
2536 if (local)
2538 for (i = 0; i < gdbmi_value_get_size (local); i++)
2540 var = gdbmi_value_list_get_nth (local, i);
2541 if (var)
2543 name = gdbmi_value_literal_get (var);
2544 list = g_list_prepend (list, (gchar *)name);
2548 list = g_list_reverse (list);
2549 callback (list, user_data, NULL);
2550 g_list_free (list);
2553 void
2554 debugger_list_local (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2556 gchar *buff;
2557 DEBUG_PRINT ("In function: debugger_list_local()");
2559 g_return_if_fail (IS_DEBUGGER (debugger));
2561 buff = g_strdup_printf("-stack-list-arguments 0 %d %d", debugger->priv->current_frame, debugger->priv->current_frame);
2562 debugger_queue_command (debugger, buff, TRUE, TRUE, NULL, NULL, NULL);
2563 g_free (buff);
2564 debugger_queue_command (debugger, "-stack-list-locals 0", TRUE, FALSE, debugger_list_local_finish, callback, user_data);
2567 static void
2568 debugger_list_argument_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2571 const GDBMIValue *frame, *var, *args, *stack;
2572 const gchar * name;
2573 GList* list;
2574 guint i;
2575 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2576 gpointer user_data = debugger->priv->current_cmd.user_data;
2579 list = NULL;
2580 args = NULL;
2581 stack = gdbmi_value_hash_lookup (mi_results, "stack-args");
2582 if (stack)
2584 frame = gdbmi_value_list_get_nth (stack, 0);
2585 if (frame)
2587 args = gdbmi_value_hash_lookup (frame, "args");
2591 if (args)
2593 for (i = 0; i < gdbmi_value_get_size (args); i++)
2595 var = gdbmi_value_list_get_nth (args, i);
2596 if (var)
2598 name = gdbmi_value_literal_get (var);
2599 list = g_list_prepend (list, (gchar *)name);
2603 list = g_list_reverse (list);
2604 callback (list, user_data, NULL);
2605 g_list_free (list);
2608 void
2609 debugger_list_argument (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2611 gchar *buff;
2613 DEBUG_PRINT ("In function: debugger_list_argument()");
2615 g_return_if_fail (IS_DEBUGGER (debugger));
2617 buff = g_strdup_printf("-stack-list-arguments 0 %d %d", debugger->priv->current_frame, debugger->priv->current_frame);
2618 debugger_queue_command (debugger, buff, TRUE, FALSE, debugger_list_argument_finish, callback, user_data);
2619 g_free (buff);
2622 static void
2623 debugger_info_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2626 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2627 gpointer user_data = debugger->priv->current_cmd.user_data;
2629 if (callback != NULL)
2630 callback ((GList *)cli_results, user_data, NULL);
2633 void
2634 debugger_info_frame (Debugger *debugger, guint frame, IAnjutaDebuggerCallback callback, gpointer user_data)
2636 gchar *buff;
2638 DEBUG_PRINT ("In function: debugger_info_frame()");
2640 g_return_if_fail (IS_DEBUGGER (debugger));
2642 if (frame == 0)
2644 buff = g_strdup_printf("info frame");
2646 else
2648 buff = g_strdup_printf ("info frame %d", frame);
2650 debugger_queue_command (debugger, buff, TRUE, FALSE, (DebuggerParserFunc)debugger_info_finish, callback, user_data);
2651 g_free (buff);
2654 void
2655 debugger_info_signal (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2657 DEBUG_PRINT ("In function: debugger_info_signal()");
2659 g_return_if_fail (IS_DEBUGGER (debugger));
2661 debugger_queue_command (debugger, "info signals", TRUE, FALSE, (DebuggerParserFunc)debugger_info_finish, callback, user_data);
2664 void
2665 debugger_info_sharedlib (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2667 gchar *buff;
2669 DEBUG_PRINT ("In function: debugger_info_sharedlib()");
2671 g_return_if_fail (IS_DEBUGGER (debugger));
2673 buff = g_strdup_printf ("info sharedlib");
2674 debugger_queue_command (debugger, buff, TRUE, FALSE, (DebuggerParserFunc)debugger_info_finish, callback, user_data); g_free (buff);
2677 void
2678 debugger_info_args (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2680 DEBUG_PRINT ("In function: debugger_info_args()");
2682 g_return_if_fail (IS_DEBUGGER (debugger));
2684 debugger_queue_command (debugger, "info args", TRUE, FALSE, (DebuggerParserFunc)debugger_info_finish, callback, user_data);
2687 void
2688 debugger_info_target (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2690 DEBUG_PRINT ("In function: debugger_info_target()");
2692 g_return_if_fail (IS_DEBUGGER (debugger));
2694 debugger_queue_command (debugger, "info target", TRUE, FALSE, (DebuggerParserFunc)debugger_info_finish, callback, user_data);
2697 void
2698 debugger_info_program (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2700 DEBUG_PRINT ("In function: debugger_info_program()");
2702 g_return_if_fail (IS_DEBUGGER (debugger));
2704 debugger_queue_command (debugger, "info program", TRUE, FALSE, (DebuggerParserFunc)debugger_info_finish, callback, user_data);
2707 void
2708 debugger_info_udot (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2710 DEBUG_PRINT ("In function: debugger_info_udot()");
2712 g_return_if_fail (IS_DEBUGGER (debugger));
2714 debugger_queue_command (debugger, "info udot", TRUE, FALSE, (DebuggerParserFunc)debugger_info_finish, callback, user_data);
2717 void
2718 debugger_info_variables (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2720 DEBUG_PRINT ("In function: debugger_info_variables()");
2722 g_return_if_fail (IS_DEBUGGER (debugger));
2724 debugger_queue_command (debugger, "info variables", TRUE, FALSE, (DebuggerParserFunc)debugger_info_finish, callback, user_data);
2727 static void
2728 debugger_read_memory_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2731 const GDBMIValue *literal;
2732 const GDBMIValue *mem;
2733 const gchar *value;
2734 gchar *data;
2735 gchar *ptr;
2736 gulong address;
2737 guint len;
2738 guint i;
2739 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2740 gpointer user_data = debugger->priv->current_cmd.user_data;
2741 IAnjutaDebuggerMemoryBlock read = {0,};
2743 literal = gdbmi_value_hash_lookup (mi_results, "total-bytes");
2744 if (literal)
2746 guint size;
2748 len = strtoul (gdbmi_value_literal_get (literal), NULL, 10);
2749 data = g_new (gchar, len * 2);
2750 memset (data + len, 0, len);
2752 literal = gdbmi_value_hash_lookup (mi_results, "addr");
2753 address = strtoul (gdbmi_value_literal_get (literal), NULL, 0);
2755 ptr = data;
2756 size = 0;
2757 mem = gdbmi_value_hash_lookup (mi_results, "memory");
2758 if (mem)
2760 mem = gdbmi_value_list_get_nth (mem, 0);
2761 if (mem)
2763 mem = gdbmi_value_hash_lookup (mem, "data");
2764 if (mem)
2766 size = gdbmi_value_get_size (mem);
2771 if (size < len) len = size;
2772 for (i = 0; i < len; i++)
2774 literal = gdbmi_value_list_get_nth (mem, i);
2775 if (literal)
2777 gchar *endptr;
2778 value = gdbmi_value_literal_get (literal);
2779 *ptr = strtoul (value, &endptr, 16);
2780 if ((*value != '\0') && (*endptr == '\0'))
2782 /* valid data */
2783 ptr[len] = 1;
2785 ptr++;
2788 read.address = address;
2789 read.length = len;
2790 read.data = data;
2791 callback (&read, user_data, NULL);
2793 g_free (data);
2795 else
2797 callback (NULL, user_data, NULL);
2801 void
2802 debugger_inspect_memory (Debugger *debugger, gulong address, guint length, IAnjutaDebuggerCallback callback, gpointer user_data)
2804 gchar *buff;
2806 DEBUG_PRINT ("In function: debugger_inspect_memory()");
2808 g_return_if_fail (IS_DEBUGGER (debugger));
2810 buff = g_strdup_printf ("-data-read-memory 0x%lx x 1 1 %d", address, length);
2811 debugger_queue_command (debugger, buff, FALSE, FALSE, debugger_read_memory_finish, callback, user_data);
2812 g_free (buff);
2815 static void
2816 debugger_disassemble_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2819 const GDBMIValue *literal;
2820 const GDBMIValue *line;
2821 const GDBMIValue *mem;
2822 const gchar *value;
2823 guint i;
2824 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2825 gpointer user_data = debugger->priv->current_cmd.user_data;
2826 IAnjutaDebuggerInstructionDisassembly *read = NULL;
2828 if (error != NULL)
2830 /* Command fail */
2831 callback (NULL, user_data, error);
2833 return;
2837 mem = gdbmi_value_hash_lookup (mi_results, "asm_insns");
2838 if (mem)
2840 guint size;
2842 size = gdbmi_value_get_size (mem);
2843 read = (IAnjutaDebuggerInstructionDisassembly *)g_malloc0(sizeof (IAnjutaDebuggerInstructionDisassembly) + sizeof(IAnjutaDebuggerInstructionALine) * size);
2844 read->size = size;
2846 for (i = 0; i < size; i++)
2848 line = gdbmi_value_list_get_nth (mem, i);
2849 if (line)
2851 /* Get address */
2852 literal = gdbmi_value_hash_lookup (line, "address");
2853 if (literal)
2855 value = gdbmi_value_literal_get (literal);
2856 read->data[i].address = strtoul (value, NULL, 0);
2859 /* Get label if one exist */
2860 literal = gdbmi_value_hash_lookup (line, "offset");
2861 if (literal)
2863 value = gdbmi_value_literal_get (literal);
2864 if ((value != NULL) && (strtoul (value, NULL, 0) == 0))
2866 literal = gdbmi_value_hash_lookup (line, "func-name");
2867 if (literal)
2869 read->data[i].label = gdbmi_value_literal_get (literal);
2876 /* Get disassembly line */
2877 literal = gdbmi_value_hash_lookup (line, "inst");
2878 if (literal)
2880 read->data[i].text = gdbmi_value_literal_get (literal);
2885 /* Remove last line to mark end */
2886 read->data[i - 1].text = NULL;
2888 callback (read, user_data, NULL);
2890 g_free (read);
2892 else
2894 callback (NULL, user_data, NULL);
2898 void
2899 debugger_disassemble (Debugger *debugger, gulong address, guint length, IAnjutaDebuggerCallback callback, gpointer user_data)
2901 gchar *buff;
2902 gulong end;
2904 DEBUG_PRINT ("In function: debugger_disassemble()");
2906 g_return_if_fail (IS_DEBUGGER (debugger));
2909 /* Handle overflow */
2910 end = (address + length < address) ? G_MAXULONG : address + length;
2911 buff = g_strdup_printf ("-data-disassemble -s 0x%lx -e 0x%lx -- 0", address, end);
2912 debugger_queue_command (debugger, buff, FALSE, FALSE, debugger_disassemble_finish, callback, user_data);
2913 g_free (buff);
2916 static void
2917 parse_frame (IAnjutaDebuggerFrame *frame, const GDBMIValue *frame_hash)
2919 const GDBMIValue *literal;
2921 literal = gdbmi_value_hash_lookup (frame_hash, "level");
2922 if (literal)
2923 frame->level = strtoul (gdbmi_value_literal_get (literal), NULL, 10);
2924 else
2925 frame->level = 0;
2927 literal = gdbmi_value_hash_lookup (frame_hash, "fullname");
2928 if (literal == NULL)
2929 literal = gdbmi_value_hash_lookup (frame_hash, "file");
2930 if (literal)
2931 frame->file = (gchar *)gdbmi_value_literal_get (literal);
2932 else
2933 frame->file = NULL;
2935 literal = gdbmi_value_hash_lookup (frame_hash, "line");
2936 if (literal)
2937 frame->line = strtoul (gdbmi_value_literal_get (literal), NULL, 10);
2938 else
2939 frame->line = 0;
2941 literal = gdbmi_value_hash_lookup (frame_hash, "func");
2942 if (literal)
2943 frame->function = (gchar *)gdbmi_value_literal_get (literal);
2944 else
2945 frame->function = NULL;
2947 literal = gdbmi_value_hash_lookup (frame_hash, "from");
2948 if (literal)
2949 frame->library = (gchar *)gdbmi_value_literal_get (literal);
2950 else
2951 frame->library = NULL;
2953 literal = gdbmi_value_hash_lookup (frame_hash, "addr");
2954 if (literal)
2955 frame->address = strtoul (gdbmi_value_literal_get (literal), NULL, 16);
2956 else
2957 frame->address = 0;
2962 static void
2963 add_frame (const GDBMIValue *frame_hash, GdbGListPacket* pack)
2965 IAnjutaDebuggerFrame* frame;
2967 frame = g_new0 (IAnjutaDebuggerFrame, 1);
2968 pack->list = g_list_prepend (pack->list, frame);
2970 frame->thread = pack->tag;
2971 parse_frame (frame, frame_hash);
2974 static void
2975 set_func_args (const GDBMIValue *frame_hash, GList** node)
2977 const gchar *level;
2978 const GDBMIValue *literal, *args_list, *arg_hash;
2979 gint i;
2980 GString *args_str;
2981 IAnjutaDebuggerFrame* frame;
2983 literal = gdbmi_value_hash_lookup (frame_hash, "level");
2984 if (!literal)
2985 return;
2987 level = gdbmi_value_literal_get (literal);
2988 if (!level)
2989 return;
2991 frame = (IAnjutaDebuggerFrame *)(*node)->data;
2993 args_list = gdbmi_value_hash_lookup (frame_hash, "args");
2994 if (args_list)
2996 args_str = g_string_new ("(");
2997 for (i = 0; i < gdbmi_value_get_size (args_list); i++)
2999 const gchar *name, *value;
3001 arg_hash = gdbmi_value_list_get_nth (args_list, i);
3002 if (!arg_hash)
3003 continue;
3005 literal = gdbmi_value_hash_lookup (arg_hash, "name");
3006 if (!literal)
3007 continue;
3008 name = gdbmi_value_literal_get (literal);
3009 if (!name)
3010 continue;
3012 literal = gdbmi_value_hash_lookup (arg_hash, "value");
3013 if (!literal)
3014 continue;
3015 value = gdbmi_value_literal_get (literal);
3016 if (!value)
3017 continue;
3018 args_str = g_string_append (args_str, name);
3019 args_str = g_string_append (args_str, "=");
3020 args_str = g_string_append (args_str, value);
3021 if (i < (gdbmi_value_get_size (args_list) - 1))
3022 args_str = g_string_append (args_str, ", ");
3024 args_str = g_string_append (args_str, ")");
3025 frame->args = args_str->str;
3026 g_string_free (args_str, FALSE);
3028 *node = g_list_next (*node);
3031 static void
3032 debugger_stack_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
3035 GdbGListPacket pack = {NULL, 0};
3036 GList* node;
3037 const GDBMIValue *stack_list;
3038 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
3039 gpointer user_data = debugger->priv->current_cmd.user_data;
3041 if (!mi_results)
3042 return;
3044 stack_list = gdbmi_value_hash_lookup (mi_results, "stack");
3045 if (stack_list)
3047 pack.tag = debugger->priv->current_thread;
3048 gdbmi_value_foreach (stack_list, (GFunc)add_frame, &pack);
3051 if (pack.list)
3053 pack.list = g_list_reverse (pack.list);
3054 node = g_list_first (pack.list);
3055 stack_list = gdbmi_value_hash_lookup (mi_results, "stack-args");
3056 if (stack_list)
3057 gdbmi_value_foreach (stack_list, (GFunc)set_func_args, &node);
3059 // Call call back function
3060 if (callback != NULL)
3061 callback (pack.list, user_data, NULL);
3063 // Free data
3064 for (node = g_list_first (pack.list); node != NULL; node = g_list_next (node))
3066 g_free ((gchar *)((IAnjutaDebuggerFrame *)node->data)->args);
3067 g_free (node->data);
3070 g_list_free (pack.list);
3072 else
3074 // Call call back function
3075 if (callback != NULL)
3076 callback (NULL, user_data, NULL);
3080 void
3081 debugger_list_frame (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
3083 DEBUG_PRINT ("In function: debugger_list_frame()");
3085 g_return_if_fail (IS_DEBUGGER (debugger));
3087 debugger_queue_command (debugger, "-stack-list-frames", TRUE, TRUE, NULL, NULL, NULL);
3088 debugger_queue_command (debugger, "-stack-list-arguments 1", TRUE, FALSE, debugger_stack_finish, callback, user_data);
3091 /* Thread functions
3092 *---------------------------------------------------------------------------*/
3094 static void
3095 debugger_set_thread_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
3097 const GDBMIValue *literal;
3098 guint id;
3100 if (mi_results == NULL) return;
3102 literal = gdbmi_value_hash_lookup (mi_results, "new-thread-id");
3103 if (literal == NULL) return;
3105 id = strtoul (gdbmi_value_literal_get (literal), NULL, 10);
3106 if (id == 0) return;
3108 debugger->priv->current_thread = id;
3109 g_signal_emit_by_name (debugger->priv->instance, "frame-changed", 0, debugger->priv->current_thread);
3111 return;
3114 void
3115 debugger_set_thread (Debugger *debugger, gint thread)
3117 gchar *buff;
3119 DEBUG_PRINT ("In function: debugger_set_thread()");
3121 g_return_if_fail (IS_DEBUGGER (debugger));
3123 buff = g_strdup_printf ("-thread-select %d", thread);
3125 debugger_queue_command (debugger, buff, FALSE, FALSE, (DebuggerParserFunc)debugger_set_thread_finish, NULL, NULL);
3126 g_free (buff);
3129 static void
3130 add_thread_id (const GDBMIValue *thread_hash, GList** list)
3132 IAnjutaDebuggerFrame* frame;
3133 gint thread;
3135 thread = strtoul (gdbmi_value_literal_get (thread_hash), NULL, 10);
3136 if (thread == 0) return;
3138 frame = g_new0 (IAnjutaDebuggerFrame, 1);
3139 *list = g_list_prepend (*list, frame);
3141 frame->thread = thread;
3144 static void
3145 debugger_list_thread_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
3148 const GDBMIValue *id_list;
3149 gpointer user_data = debugger->priv->current_cmd.user_data;
3150 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
3151 GList* thread_list = NULL;
3153 for (;;)
3155 if (mi_results == NULL) break;
3157 id_list = gdbmi_value_hash_lookup (mi_results, "thread-ids");
3158 if (id_list == NULL) break;
3160 gdbmi_value_foreach (id_list, (GFunc)add_thread_id, &thread_list);
3161 thread_list = g_list_reverse (thread_list);
3162 break;
3165 if (callback != NULL)
3166 callback (thread_list, user_data, error);
3168 if (thread_list != NULL)
3170 g_list_foreach (thread_list, (GFunc)g_free, NULL);
3171 g_list_free (thread_list);
3175 void
3176 debugger_list_thread (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
3178 DEBUG_PRINT ("In function: debugger_list_thread()");
3180 g_return_if_fail (IS_DEBUGGER (debugger));
3182 debugger_queue_command (debugger, "-thread-list-ids", TRUE, FALSE, debugger_list_thread_finish, callback, user_data);
3185 static void
3186 debugger_info_set_thread_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
3188 const GDBMIValue *literal;
3189 guint id;
3191 if (mi_results == NULL) return;
3193 literal = gdbmi_value_hash_lookup (mi_results, "new-thread-id");
3194 if (literal == NULL) return;
3196 id = strtoul (gdbmi_value_literal_get (literal), NULL, 10);
3197 if (id == 0) return;
3199 debugger->priv->current_thread = id;
3200 /* Do not emit a signal notification as the current thread will
3201 * be restored when needed */
3203 return;
3206 static void
3207 debugger_info_thread_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
3209 const GDBMIValue *frame;
3210 IAnjutaDebuggerFrame top_frame;
3211 IAnjutaDebuggerFrame *top;
3212 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
3213 gpointer user_data = debugger->priv->current_cmd.user_data;
3215 for (top = NULL;;)
3217 DEBUG_PRINT("look for stack %p", mi_results);
3218 if (mi_results == NULL) break;
3220 frame = gdbmi_value_hash_lookup (mi_results, "stack");
3221 if (frame == NULL) break;
3223 DEBUG_PRINT("get stack");
3225 frame = gdbmi_value_list_get_nth (frame, 0);
3226 if (frame == NULL) break;
3228 DEBUG_PRINT("get nth element");
3230 /*frame = gdbmi_value_hash_lookup (frame, "frame");
3231 DEBUG_PRINT("get frame %p", frame);
3232 if (frame == NULL) break;*/
3234 DEBUG_PRINT("get frame");
3236 top = &top_frame;
3237 top->thread = debugger->priv->current_thread;
3239 parse_frame (top, frame);
3240 break;
3243 if (callback != NULL)
3244 callback (top, user_data, error);
3246 return;
3249 void
3250 debugger_info_thread (Debugger *debugger, gint thread, IAnjutaDebuggerCallback callback, gpointer user_data)
3252 gchar *buff;
3253 guint orig_thread;
3255 DEBUG_PRINT ("In function: debugger_info_thread()");
3257 g_return_if_fail (IS_DEBUGGER (debugger));
3259 orig_thread = debugger->priv->current_thread;
3260 buff = g_strdup_printf ("-thread-select %d", thread);
3261 debugger_queue_command (debugger, buff, FALSE, FALSE, (DebuggerParserFunc)debugger_info_set_thread_finish, NULL, NULL);
3262 g_free (buff);
3263 debugger_queue_command (debugger, "-stack-list-frames 0 0", FALSE, FALSE, (DebuggerParserFunc)debugger_info_thread_finish, callback, user_data);
3265 buff = g_strdup_printf ("-thread-select %d", orig_thread);
3266 debugger_queue_command (debugger, buff, FALSE, FALSE, (DebuggerParserFunc)debugger_info_set_thread_finish, NULL, NULL);
3267 g_free (buff);
3270 static void
3271 add_register_name (const GDBMIValue *reg_literal, GList** list)
3273 IAnjutaDebuggerRegisterData* reg;
3274 GList *prev = *list;
3276 reg = g_new0 (IAnjutaDebuggerRegisterData, 1);
3277 *list = g_list_prepend (prev, reg);
3278 reg->name = (gchar *)gdbmi_value_literal_get (reg_literal);
3279 reg->num = prev == NULL ? 0 : ((IAnjutaDebuggerRegisterData *)prev->data)->num + 1;
3282 static void
3283 add_register_value (const GDBMIValue *reg_hash, GList** list)
3285 const GDBMIValue *literal;
3286 const gchar *val;
3287 IAnjutaDebuggerRegisterData* reg;
3288 guint num;
3289 GList* prev = *list;
3291 literal = gdbmi_value_hash_lookup (reg_hash, "number");
3292 if (!literal)
3293 return;
3294 val = gdbmi_value_literal_get (literal);
3295 num = strtoul (val, NULL, 10);
3297 literal = gdbmi_value_hash_lookup (reg_hash, "value");
3298 if (!literal)
3299 return;
3301 reg = g_new0 (IAnjutaDebuggerRegisterData, 1);
3302 *list = g_list_prepend (prev, reg);
3303 reg->num = num;
3304 reg->value = (gchar *)gdbmi_value_literal_get (literal);
3307 static void
3308 debugger_register_name_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
3311 GList* list = NULL;
3312 GList* node;
3313 const GDBMIValue *reg_list;
3314 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
3315 gpointer user_data = debugger->priv->current_cmd.user_data;
3317 if (!mi_results)
3318 return;
3320 reg_list = gdbmi_value_hash_lookup (mi_results, "register-names");
3321 if (reg_list)
3322 gdbmi_value_foreach (reg_list, (GFunc)add_register_name, &list);
3323 list = g_list_reverse (list);
3325 // Call call back function
3326 if (callback != NULL)
3327 callback (list, user_data, NULL);
3329 // Free data
3330 for (node = g_list_first (list); node != NULL; node = g_list_next (node))
3332 g_free (node->data);
3334 g_list_free (list);
3337 static void
3338 debugger_register_value_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
3341 GList* list = NULL;
3342 GList* node;
3343 const GDBMIValue *reg_list;
3344 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
3345 gpointer user_data = debugger->priv->current_cmd.user_data;
3347 if (!mi_results)
3348 return;
3350 reg_list = gdbmi_value_hash_lookup (mi_results, "register-values");
3351 if (reg_list)
3352 gdbmi_value_foreach (reg_list, (GFunc)add_register_value, &list);
3353 list = g_list_reverse (list);
3355 // Call call back function
3356 if (callback != NULL)
3357 callback (list, user_data, NULL);
3359 // Free data
3360 for (node = g_list_first (list); node != NULL; node = g_list_next (node))
3362 g_free (node->data);
3364 g_list_free (list);
3367 void
3368 debugger_list_register (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
3370 DEBUG_PRINT ("In function: debugger_list_register()");
3372 g_return_if_fail (IS_DEBUGGER (debugger));
3374 debugger_queue_command (debugger, "-data-list-register-names", TRUE, FALSE, debugger_register_name_finish, callback, user_data);
3377 void
3378 debugger_update_register (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
3380 DEBUG_PRINT ("In function: debugger_update_register()");
3382 g_return_if_fail (IS_DEBUGGER (debugger));
3384 debugger_queue_command (debugger, "-data-list-register-values r", TRUE, FALSE, (DebuggerParserFunc)debugger_register_value_finish, callback, user_data);
3387 void
3388 debugger_write_register (Debugger *debugger, const gchar *name, const gchar *value)
3390 gchar *buf;
3392 DEBUG_PRINT ("In function: debugger_write_register()");
3394 g_return_if_fail (IS_DEBUGGER (debugger));
3396 buf = g_strdup_printf ("-data-evaluate-expression \"$%s=%s\"", name, value);
3397 debugger_queue_command (debugger, buf, TRUE, FALSE, NULL, NULL, NULL);
3398 g_free (buf);
3401 static void
3402 debugger_set_frame_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
3405 guint frame = (guint)debugger->priv->current_cmd.user_data;
3406 debugger->priv->current_frame = frame;
3408 g_signal_emit_by_name (debugger->priv->instance, "frame-changed", frame, debugger->priv->current_thread);
3411 void
3412 debugger_set_frame (Debugger *debugger, guint frame)
3414 gchar *buff;
3416 DEBUG_PRINT ("In function: debugger_set_frame()");
3418 g_return_if_fail (IS_DEBUGGER (debugger));
3420 buff = g_strdup_printf ("-stack-select-frame %d", frame);
3422 debugger_queue_command (debugger, buff, FALSE, FALSE, (DebuggerParserFunc)debugger_set_frame_finish, NULL, (gpointer)frame);
3423 g_free (buff);
3426 void
3427 debugger_set_log (Debugger *debugger, IAnjutaMessageView *log)
3429 debugger->priv->log = log;
3432 /* Variable objects functions
3433 *---------------------------------------------------------------------------*/
3435 void
3436 debugger_delete_variable (Debugger *debugger, const gchar* name)
3438 gchar *buff;
3440 DEBUG_PRINT ("In function: delete_variable()");
3442 g_return_if_fail (IS_DEBUGGER (debugger));
3444 buff = g_strdup_printf ("-var-delete %s", name);
3445 debugger_queue_command (debugger, buff, FALSE, FALSE, NULL, NULL, NULL);
3446 g_free (buff);
3449 static void
3450 gdb_var_evaluate_expression (Debugger *debugger,
3451 const GDBMIValue *mi_results, const GList *cli_results,
3452 GError *error)
3454 const gchar *value = NULL;
3455 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
3456 gpointer user_data = debugger->priv->current_cmd.user_data;
3458 if (mi_results != NULL)
3460 const GDBMIValue *const gdbmi_value =
3461 gdbmi_value_hash_lookup (mi_results, "value");
3463 if (gdbmi_value != NULL)
3464 value = gdbmi_value_literal_get (gdbmi_value);
3466 callback ((const gpointer)value, user_data, NULL);
3469 void
3470 debugger_evaluate_variable (Debugger *debugger, const gchar* name, IAnjutaDebuggerCallback callback, gpointer user_data)
3472 gchar *buff;
3474 DEBUG_PRINT ("In function: evaluate_variable()");
3476 g_return_if_fail (IS_DEBUGGER (debugger));
3478 buff = g_strdup_printf ("-var-evaluate-expression %s", name);
3479 debugger_queue_command (debugger, buff, FALSE, FALSE, gdb_var_evaluate_expression, callback, user_data);
3480 g_free (buff);
3483 void
3484 debugger_assign_variable (Debugger *debugger, const gchar* name, const gchar *value)
3486 gchar *buff;
3488 DEBUG_PRINT ("In function: assign_variable()");
3490 g_return_if_fail (IS_DEBUGGER (debugger));
3492 buff = g_strdup_printf ("-var-assign %s %s", name, value);
3493 debugger_queue_command (debugger, buff, FALSE, FALSE, NULL, NULL, NULL);
3494 g_free (buff);
3497 static void
3498 gdb_var_list_children (Debugger *debugger,
3499 const GDBMIValue *mi_results, const GList *cli_results,
3500 GError *error)
3502 GList* list = NULL;
3503 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
3504 gpointer user_data = debugger->priv->current_cmd.user_data;
3506 if (mi_results != NULL)
3508 const GDBMIValue *literal;
3509 const GDBMIValue *children;
3510 glong numchild = 0;
3511 glong i = 0;
3513 literal = gdbmi_value_hash_lookup (mi_results, "numchild");
3515 if (literal)
3516 numchild = strtoul(gdbmi_value_literal_get (literal), NULL, 0);
3517 children = gdbmi_value_hash_lookup (mi_results, "children");
3519 for(i = 0 ; i < numchild; ++i)
3521 const GDBMIValue *const gdbmi_chl =
3522 gdbmi_value_list_get_nth (children, i);
3523 IAnjutaDebuggerVariableObject *var;
3525 var = g_new0 (IAnjutaDebuggerVariableObject, 1);
3527 literal = gdbmi_value_hash_lookup (gdbmi_chl, "name");
3528 if (literal)
3529 var->name = (gchar *)gdbmi_value_literal_get (literal);
3531 literal = gdbmi_value_hash_lookup (gdbmi_chl, "exp");
3532 if (literal)
3533 var->expression = (gchar *)gdbmi_value_literal_get(literal);
3535 literal = gdbmi_value_hash_lookup (gdbmi_chl, "type");
3536 if (literal)
3537 var->type = (gchar *)gdbmi_value_literal_get(literal);
3539 literal = gdbmi_value_hash_lookup (gdbmi_chl, "value");
3540 if (literal)
3541 var->value = (gchar *)gdbmi_value_literal_get(literal);
3543 literal = gdbmi_value_hash_lookup (gdbmi_chl, "numchild");
3544 if (literal)
3545 var->children = strtoul(gdbmi_value_literal_get(literal), NULL, 10);
3547 list = g_list_prepend (list, var);
3549 list = g_list_reverse (list);
3552 callback (list, user_data, NULL);
3553 g_list_foreach (list, (GFunc)g_free, NULL);
3554 g_list_free (list);
3557 void debugger_list_variable_children (Debugger *debugger, const gchar* name, IAnjutaDebuggerCallback callback, gpointer user_data)
3559 gchar *buff;
3561 DEBUG_PRINT ("In function: list_variable_children()");
3563 g_return_if_fail (IS_DEBUGGER (debugger));
3565 buff = g_strdup_printf ("-var-list-children --all-values %s", name);
3566 debugger_queue_command (debugger, buff, FALSE, FALSE, gdb_var_list_children, callback, user_data);
3567 g_free (buff);
3570 static void
3571 gdb_var_create (Debugger *debugger,
3572 const GDBMIValue *mi_results, const GList *cli_results,
3573 GError *error)
3575 const GDBMIValue * result;
3576 IAnjutaDebuggerVariableObject var = {0,};
3577 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
3578 gpointer user_data = debugger->priv->current_cmd.user_data;
3580 if ((error == NULL) && (mi_results != NULL))
3582 result = gdbmi_value_hash_lookup (mi_results, "name");
3583 var.name = (gchar *)gdbmi_value_literal_get(result);
3585 result = gdbmi_value_hash_lookup (mi_results, "type");
3586 var.type = (gchar *)gdbmi_value_literal_get (result);
3588 result = gdbmi_value_hash_lookup (mi_results, "numchild");
3589 var.children = strtoul (gdbmi_value_literal_get(result), NULL, 10);
3591 callback (&var, user_data, error);
3595 void debugger_create_variable (Debugger *debugger, const gchar* name, IAnjutaDebuggerCallback callback, gpointer user_data)
3597 gchar *buff;
3599 DEBUG_PRINT ("In function: create_variable()");
3601 g_return_if_fail (IS_DEBUGGER (debugger));
3603 buff = g_strdup_printf ("-var-create - * %s", name);
3604 debugger_queue_command (debugger, buff, FALSE, FALSE, gdb_var_create, callback, user_data);
3605 g_free (buff);
3608 static void
3609 gdb_var_update (Debugger *debugger,
3610 const GDBMIValue *mi_results, const GList *cli_results,
3611 GError *error)
3613 GList* list = NULL;
3614 glong idx = 0, changed_count = 0;
3615 const GDBMIValue *const gdbmi_changelist =
3616 gdbmi_value_hash_lookup (mi_results, "changelist");
3617 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
3618 gpointer user_data = debugger->priv->current_cmd.user_data;
3621 changed_count = gdbmi_value_get_size (gdbmi_changelist);
3622 for(; idx<changed_count; ++idx)
3624 const GDBMIValue *const gdbmi_change =
3625 gdbmi_value_list_get_nth (gdbmi_changelist, idx);
3626 const GDBMIValue * value;
3629 value = gdbmi_value_hash_lookup (gdbmi_change, "name");
3630 if (value)
3632 IAnjutaDebuggerVariableObject *var = g_new0 (IAnjutaDebuggerVariableObject, 1);
3633 var->changed = TRUE;
3634 var->name = (gchar *)gdbmi_value_literal_get(value);
3635 list = g_list_prepend (list, var);
3637 value = gdbmi_value_hash_lookup (gdbmi_change, "type_changed");
3638 if (value != NULL)
3640 const gchar *type_changed = gdbmi_value_literal_get (value);
3642 if (strcmp (type_changed, "true"))
3644 var->deleted = TRUE;
3648 value = gdbmi_value_hash_lookup (gdbmi_change, "in_scope");
3649 if (value != NULL)
3651 const gchar *in_scope = gdbmi_value_literal_get(value);
3653 if (strcmp (in_scope, "false") == 0)
3655 var->exited = TRUE;
3657 else if (strcmp (in_scope, "invalid") == 0)
3659 var->deleted = TRUE;
3664 list = g_list_reverse (list);
3665 callback (list, user_data, NULL);
3666 g_list_foreach (list, (GFunc)g_free, NULL);
3667 g_list_free (list);
3670 void debugger_update_variable (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
3672 DEBUG_PRINT ("In function: update_variable()");
3674 g_return_if_fail (IS_DEBUGGER (debugger));
3676 debugger_queue_command (debugger, "-var-update *", FALSE, FALSE, gdb_var_update, callback, user_data);
3679 GType
3680 debugger_get_type (void)
3682 static GType obj_type = 0;
3684 if (!obj_type)
3686 static const GTypeInfo obj_info =
3688 sizeof (DebuggerClass),
3689 (GBaseInitFunc) NULL,
3690 (GBaseFinalizeFunc) NULL,
3691 (GClassInitFunc) debugger_class_init,
3692 (GClassFinalizeFunc) NULL,
3693 NULL, /* class_data */
3694 sizeof (Debugger),
3695 0, /* n_preallocs */
3696 (GInstanceInitFunc) debugger_instance_init,
3697 NULL /* value_table */
3699 obj_type = g_type_register_static (G_TYPE_OBJECT,
3700 "Debugger", &obj_info, 0);
3702 return obj_type;
3705 static void
3706 debugger_dispose (GObject *obj)
3708 Debugger *debugger = DEBUGGER (obj);
3710 DEBUG_PRINT ("In function: debugger_shutdown()");
3712 /* Do not emit signal when the debugger is destroyed */
3713 debugger->priv->instance = NULL;
3714 debugger_abort (debugger);
3716 /* Good Bye message */
3717 if (debugger->priv->output_callback)
3719 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
3720 "Debugging session completed.\n",
3721 debugger->priv->output_user_data);
3724 if (debugger->priv->launcher)
3726 anjuta_launcher_reset (debugger->priv->launcher);
3727 g_object_unref (debugger->priv->launcher);
3728 debugger->priv->launcher = NULL;
3731 G_OBJECT_CLASS (parent_class)->dispose (obj);
3734 static void
3735 debugger_finalize (GObject *obj)
3737 Debugger *debugger = DEBUGGER (obj);
3738 g_string_free (debugger->priv->stdo_line, TRUE);
3739 g_string_free (debugger->priv->stdo_acc, TRUE);
3740 g_string_free (debugger->priv->stde_line, TRUE);
3741 g_free (debugger->priv);
3742 G_OBJECT_CLASS (parent_class)->finalize (obj);
3745 static void
3746 debugger_class_init (DebuggerClass * klass)
3748 GObjectClass *object_class;
3750 g_return_if_fail (klass != NULL);
3751 object_class = G_OBJECT_CLASS (klass);
3753 DEBUG_PRINT ("Initializing debugger class");
3755 parent_class = g_type_class_peek_parent (klass);
3756 object_class->dispose = debugger_dispose;
3757 object_class->finalize = debugger_finalize;
3761 #if 0 /* FIXME */
3762 void
3763 debugger_signal (const gchar *sig, gboolean show_msg)
3765 /* eg:- "SIGTERM" */
3766 gchar *buff;
3767 gchar *cmd;
3769 DEBUG_PRINT ("In function: debugger_signal()");
3771 if (debugger_is_active () == FALSE)
3772 return;
3773 if (debugger.prog_is_running == FALSE)
3774 return;
3775 if (debugger.child_pid < 1)
3777 DEBUG_PRINT ("Not sending signal - pid not known\n");
3778 return;
3781 if (show_msg)
3783 buff = g_strdup_printf (_("Sending signal %s to the process: %d"),
3784 sig, (int) debugger.child_pid);
3785 gdb_util_append_message (ANJUTA_PLUGIN (debugger.plugin), buff);
3786 g_free (buff);
3789 if (debugger_is_ready ())
3791 cmd = g_strconcat ("signal ", sig, NULL);
3792 stack_trace_set_frame (debugger.stack, 0);
3793 debugger_put_cmd_in_queqe (cmd, DB_CMD_ALL, NULL, NULL);
3794 debugger_put_cmd_in_queqe ("info program", DB_CMD_NONE,
3795 on_debugger_update_prog_status,
3796 NULL);
3797 g_free (cmd);
3798 debugger_execute_cmd_in_queqe ();
3800 else
3802 GtkWindow *parent;
3803 int status;
3805 parent = GTK_WINDOW (ANJUTA_PLUGIN (debugger.plugin)->shell);
3806 status = gdb_util_kill_process (debugger.child_pid, sig);
3807 if (status != 0 && show_msg)
3808 anjuta_util_dialog_error (parent,
3809 _("Error whilst signaling the process."));
3813 static void
3814 query_set_cmd (const gchar *cmd, gboolean state)
3816 gchar buffer[50];
3817 gchar *tmp = g_stpcpy (buffer, cmd);
3818 strcpy (tmp, state ? "on" : "off");
3819 debugger_put_cmd_in_queqe (buffer, DB_CMD_NONE, NULL, NULL);
3822 static void
3823 query_set_verbose (gboolean state)
3825 query_set_cmd ("set verbose ", state);
3828 static void
3829 query_set_print_staticmembers (gboolean state)
3831 query_set_cmd ("set print static-members ", state);
3834 static void
3835 query_set_print_pretty (gboolean state)
3837 query_set_cmd ("set print pretty ", state);
3840 void debugger_query_evaluate_expr_tip (const gchar *expr,
3841 DebuggerCLIFunc parser, gpointer data)
3843 query_set_verbose (FALSE);
3844 query_set_print_staticmembers (FALSE);
3845 gchar *printcmd = g_strconcat ("print ", expr, NULL);
3846 debugger_put_cmd_in_queqe (printcmd, DB_CMD_NONE, parser, data);
3847 query_set_verbose (TRUE);
3848 query_set_print_staticmembers (TRUE);
3849 g_free (printcmd);
3852 void
3853 debugger_query_evaluate_expression (const gchar *expr, DebuggerFunc parser,
3854 gpointer data)
3856 query_set_print_pretty (TRUE);
3857 query_set_verbose (FALSE);
3858 gchar *printcmd = g_strconcat ("print ", expr, NULL);
3859 debugger_put_cmd_in_queqe (printcmd, DB_CMD_SE_MESG | DB_CMD_SE_DIALOG,
3860 parser, data);
3861 query_set_print_pretty (FALSE);
3862 query_set_verbose (TRUE);
3863 g_free (printcmd);
3866 #endif