* plugins/gdb/debugger.c:
[anjuta-git-plugin.git] / plugins / gdb / debugger.c
blobf6851e8bbc6811230137647b11f5d5ffa5e73384
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 typedef struct _GdbMessageCode GdbMessageCode;
156 struct _GdbMessageCode
158 const gchar *msg;
159 guint code;
162 const static GdbMessageCode GdbErrorMessage[] =
163 {{"mi_cmd_var_create: unable to create variable object",
164 IANJUTA_DEBUGGER_UNABLE_TO_CREATE_VARIABLE},
165 {"Cannot access memory at address ",
166 IANJUTA_DEBUGGER_UNABLE_TO_ACCESS_MEMORY},
167 {"No source file named ",
168 IANJUTA_DEBUGGER_UNABLE_TO_OPEN_FILE},
169 {NULL, 0}};
171 static guint
172 gdb_match_error(const gchar *message)
174 const GdbMessageCode* msg;
176 for (msg = GdbErrorMessage; msg->msg != NULL; msg++)
178 gsize len = strlen (msg->msg);
180 if (!isspace (msg->msg[len - 1]))
182 /* Match the whole string */
183 len++;
186 if (memcmp (msg->msg, message, len) == 0)
188 return msg->code;
192 return IANJUTA_DEBUGGER_UNKNOWN_ERROR;
195 static void
196 debugger_message_view_append (Debugger *debugger, IAnjutaMessageViewType type, const char *message)
198 guint len = strlen(message);
199 gchar buf[SUMMARY_MAX_LENGTH];
200 const gchar* summary = message;
201 const gchar* detail = "";
204 if (len > SUMMARY_MAX_LENGTH)
207 memcpy(buf, message, SUMMARY_MAX_LENGTH - 4);
208 memcpy(buf + SUMMARY_MAX_LENGTH - 4, "...", 4);
209 summary = buf;
210 detail = message;
213 ianjuta_message_view_append (debugger->priv->log, type, summary, detail, NULL);
216 static void
217 debugger_initialize (Debugger *debugger)
219 const gchar* anjuta_log;
221 DEBUG_PRINT ("In function: debugger_init()");
223 debugger->priv = g_new0 (DebuggerPriv, 1);
225 debugger->priv->output_callback = NULL;
226 debugger->priv->parent_win = NULL;
227 debugger->priv->search_dirs = NULL;
228 debugger->priv->launcher = anjuta_launcher_new ();
230 debugger->priv->debugger_is_started = FALSE;
231 debugger->priv->prog_is_running = FALSE;
232 debugger->priv->debugger_is_busy = 0;
233 debugger->priv->starting = FALSE;
234 debugger->priv->terminating = FALSE;
235 debugger->priv->skip_next_prompt = FALSE;
236 debugger->priv->command_output_sent = FALSE;
238 debugger->priv->current_cmd.cmd = NULL;
239 debugger->priv->current_cmd.parser = NULL;
241 debugger->priv->cmd_queqe = NULL;
242 debugger->priv->cli_lines = NULL;
243 debugger->priv->solib_event = FALSE;
245 debugger->priv->stdo_line = g_string_sized_new (FILE_BUFFER_SIZE);
246 g_string_assign (debugger->priv->stdo_line, "");
247 debugger->priv->stdo_acc = g_string_new ("");
249 debugger->priv->stde_line = g_string_sized_new (FILE_BUFFER_SIZE);
250 g_string_assign (debugger->priv->stde_line, "");
252 debugger->priv->post_execution_flag = DEBUGGER_NONE;
254 anjuta_log = g_getenv (ANJUTA_LOG_ENV);
255 debugger->priv->gdb_log = anjuta_log && (atoi(anjuta_log) > DEBUGGER_LOG_LEVEL);
258 static void
259 debugger_instance_init (Debugger *debugger)
261 debugger_initialize (debugger);
264 Debugger*
265 debugger_new (GtkWindow *parent_win, GObject* instance)
267 Debugger *debugger;
269 debugger = g_object_new (DEBUGGER_TYPE, NULL);
270 debugger->priv->parent_win = parent_win;
271 debugger->priv->instance = instance;
273 return debugger;
276 void
277 debugger_free (Debugger *debugger)
279 g_return_if_fail (IS_DEBUGGER (debugger));
281 g_object_unref (debugger);
284 gboolean
285 debugger_is_ready (Debugger *debugger)
287 g_return_val_if_fail (IS_DEBUGGER (debugger), FALSE);
288 return !debugger->priv->debugger_is_busy;
291 gboolean
292 debugger_program_is_running (Debugger *debugger)
294 g_return_val_if_fail (IS_DEBUGGER (debugger), FALSE);
295 return debugger->priv->prog_is_running;
298 gboolean
299 debugger_program_is_attached (Debugger *debugger)
301 g_return_val_if_fail (IS_DEBUGGER (debugger), FALSE);
302 return debugger->priv->prog_is_attached;
305 gboolean
306 debugger_program_is_loaded (Debugger *debugger)
308 g_return_val_if_fail (IS_DEBUGGER (debugger), FALSE);
309 return debugger->priv->prog_is_loaded;
312 static void
313 debugger_log_command (Debugger *debugger, const gchar *command)
315 gchar* str;
316 gsize len;
318 if (debugger->priv->log == NULL) return;
320 if (*command == '-')
322 str = g_strdup (command);
323 len = strlen (command);
325 /* Remove trailing carriage return */
326 if (str[len - 1] == '\n') str[len - 1] = '\0';
328 /* Log only MI command as other are echo */
329 #ifdef DEBUG
330 DEBUG_PRINT ("GDB:> %s", str);
331 #else
332 if (debugger->priv->gdb_log) g_message ("GDB:> %s", str);
333 #endif
334 debugger_message_view_append (debugger, IANJUTA_MESSAGE_VIEW_TYPE_NORMAL, str);
335 g_free (str);
339 static void
340 debugger_log_output (Debugger *debugger, const gchar *line)
342 gchar* str;
343 const gchar* start;
344 IAnjutaMessageViewType type;
345 gsize len;
347 if (debugger->priv->log == NULL) return;
349 type = IANJUTA_MESSAGE_VIEW_TYPE_NORMAL;
350 switch (*line)
352 case '~':
353 type = IANJUTA_MESSAGE_VIEW_TYPE_INFO;
354 /* Go through */
355 case '&':
356 len = strlen(line);
357 start = line + 1;
359 /* Remove double quote if necessary */
360 if ((line[1] == '\"') && (line[len - 1] == '\"')) start++;
361 str = g_strcompress (line + 2);
362 len = strlen (str);
363 if (start == line + 2)
365 str[len - 1] = '\0';
366 len--;
369 /* Remove trailing carriage return */
370 if (str[len - 1] == '\n') str[len - 1] = '\0';
372 debugger_message_view_append (debugger, type, str);
373 g_free (str);
374 break;
375 case '^':
376 if (strncmp(line + 1, "error", 5) == 0)
378 debugger_message_view_append (debugger, IANJUTA_MESSAGE_VIEW_TYPE_ERROR, line + 1);
380 else
382 debugger_message_view_append (debugger, IANJUTA_MESSAGE_VIEW_TYPE_WARNING, line + 1);
384 break;
385 case '@':
386 debugger_message_view_append (debugger, IANJUTA_MESSAGE_VIEW_TYPE_NORMAL, line + 1);
387 break;
388 default:
389 debugger_message_view_append (debugger, IANJUTA_MESSAGE_VIEW_TYPE_NORMAL, line);
390 break;
394 static void
395 debugger_emit_ready (Debugger *debugger)
397 if (!debugger->priv->debugger_is_busy)
399 if (debugger->priv->loading)
401 debugger->priv->starting = FALSE;
402 debugger->priv->loading = FALSE;
403 debugger->priv->exiting = FALSE;
404 debugger->priv->stopping = FALSE;
405 debugger->priv->solib_event = FALSE;
406 g_signal_emit_by_name (debugger->priv->instance, "debugger-ready", IANJUTA_DEBUGGER_PROGRAM_LOADED);
408 else if (debugger->priv->starting)
410 debugger->priv->starting = FALSE;
411 debugger->priv->loading = FALSE;
412 debugger->priv->exiting = FALSE;
413 debugger->priv->stopping = FALSE;
414 debugger->priv->solib_event = FALSE;
415 g_signal_emit_by_name (debugger->priv->instance, "debugger-ready", IANJUTA_DEBUGGER_STARTED);
417 else if (debugger->priv->exiting)
419 debugger->priv->exiting = FALSE;
420 debugger->priv->stopping = FALSE;
421 debugger->priv->solib_event = FALSE;
422 g_signal_emit_by_name (debugger->priv->instance, "debugger-ready", IANJUTA_DEBUGGER_PROGRAM_LOADED);
424 else if (debugger->priv->solib_event)
426 debugger->priv->exiting = FALSE;
427 debugger->priv->stopping = FALSE;
428 debugger->priv->solib_event = FALSE;
429 g_signal_emit_by_name (debugger->priv->instance, "sharedlib-event");
431 else if (debugger->priv->stopping)
433 debugger->priv->exiting = FALSE;
434 debugger->priv->stopping = FALSE;
435 debugger->priv->solib_event = FALSE;
436 g_signal_emit_by_name (debugger->priv->instance, "debugger-ready", IANJUTA_DEBUGGER_PROGRAM_STOPPED);
438 else
440 if (debugger->priv->prog_is_running || debugger->priv->prog_is_attached)
442 g_signal_emit_by_name (debugger->priv->instance, "debugger-ready", IANJUTA_DEBUGGER_PROGRAM_STOPPED);
444 else if (debugger->priv->prog_is_loaded)
446 g_signal_emit_by_name (debugger->priv->instance, "debugger-ready", IANJUTA_DEBUGGER_PROGRAM_LOADED);
448 else
450 g_signal_emit_by_name (debugger->priv->instance, "debugger-ready", IANJUTA_DEBUGGER_STARTED);
456 IAnjutaDebuggerState
457 debugger_get_state (Debugger *debugger)
459 if (debugger->priv->debugger_is_busy)
461 return IANJUTA_DEBUGGER_BUSY;
463 else
465 if (debugger->priv->prog_is_running || debugger->priv->prog_is_attached)
467 return IANJUTA_DEBUGGER_PROGRAM_STOPPED;
469 else if (debugger->priv->prog_is_loaded)
471 return IANJUTA_DEBUGGER_PROGRAM_LOADED;
473 else if (debugger->priv->debugger_is_started)
475 return IANJUTA_DEBUGGER_STARTED;
477 else
479 return IANJUTA_DEBUGGER_STOPPED;
484 static void
485 debugger_clear_buffers (Debugger *debugger)
487 DEBUG_PRINT ("In function: debugger_clear_buffers()");
489 /* Clear the output line buffer */
490 g_string_assign (debugger->priv->stdo_line, "");
491 if (!debugger->priv->current_cmd.keep_result)
492 g_string_assign (debugger->priv->stdo_acc, "");
494 /* Clear the error line buffer */
495 g_string_assign (debugger->priv->stde_line, "");
497 /* Clear CLI output lines */
498 g_list_foreach (debugger->priv->cli_lines, (GFunc)g_free, NULL);
499 g_list_free (debugger->priv->cli_lines);
500 debugger->priv->cli_lines = NULL;
503 static DebuggerCommand *
504 debugger_queue_get_next_command (Debugger *debugger)
506 DebuggerCommand *dc;
508 DEBUG_PRINT ("In function: debugger_get_next_command()");
510 if (debugger->priv->cmd_queqe)
512 dc = g_list_nth_data (debugger->priv->cmd_queqe, 0);
513 debugger->priv->cmd_queqe = g_list_remove (debugger->priv->cmd_queqe, dc);
515 else
516 dc = NULL;
517 return dc;
520 static gboolean
521 debugger_queue_set_next_command (Debugger *debugger)
523 DebuggerCommand *dc;
525 DEBUG_PRINT ("In function: debugger_set_next_command()");
527 dc = debugger_queue_get_next_command (debugger);
528 if (!dc)
530 debugger->priv->current_cmd.cmd = NULL;
531 debugger->priv->current_cmd.parser = NULL;
532 debugger->priv->current_cmd.callback = NULL;
533 debugger->priv->current_cmd.user_data = NULL;
534 debugger->priv->current_cmd.suppress_error = FALSE;
535 debugger->priv->current_cmd.keep_result = FALSE;
537 return FALSE;
539 g_free (debugger->priv->current_cmd.cmd);
540 debugger->priv->current_cmd.cmd = dc->cmd;
541 debugger->priv->current_cmd.parser = dc->parser;
542 debugger->priv->current_cmd.callback = dc->callback;
543 debugger->priv->current_cmd.user_data = dc->user_data;
544 debugger->priv->current_cmd.suppress_error = dc->suppress_error;
545 debugger->priv->current_cmd.keep_result = dc->keep_result;
546 g_free (dc);
548 return TRUE;
551 static void
552 debugger_queue_command (Debugger *debugger, const gchar *cmd,
553 gboolean suppress_error, gboolean keep_result,
554 DebuggerParserFunc parser,
555 IAnjutaDebuggerCallback callback, gpointer user_data)
557 DebuggerCommand *dc;
560 DEBUG_PRINT ("In function: debugger_queue_command (%s)", cmd);
562 dc = g_malloc (sizeof (DebuggerCommand));
563 if (dc)
565 dc->cmd = g_strdup(cmd);
566 dc->parser = parser;
567 dc->callback = callback;
568 dc->user_data = user_data;
569 dc->suppress_error = suppress_error;
570 dc->keep_result = keep_result;
572 debugger->priv->cmd_queqe = g_list_append (debugger->priv->cmd_queqe, dc);
573 debugger_queue_execute_command (debugger);
576 static void
577 debugger_queue_clear (Debugger *debugger)
579 GList *node;
581 DEBUG_PRINT ("In function: debugger_queue_clear()");
583 node = debugger->priv->cmd_queqe;
584 while (node)
586 g_free (((DebuggerCommand *)node->data)->cmd);
587 g_free (node->data);
588 node = g_list_next (node);
590 g_list_free (debugger->priv->cmd_queqe);
591 debugger->priv->cmd_queqe = NULL;
592 g_free (debugger->priv->current_cmd.cmd);
593 debugger->priv->current_cmd.cmd = NULL;
594 debugger->priv->current_cmd.parser = NULL;
595 debugger->priv->current_cmd.callback = NULL;
596 debugger->priv->current_cmd.user_data = NULL;
597 debugger->priv->current_cmd.suppress_error = FALSE;
598 debugger->priv->current_cmd.keep_result = FALSE;
599 debugger_clear_buffers (debugger);
602 static void
603 debugger_execute_command (Debugger *debugger, const gchar *command)
605 gchar *cmd;
607 DEBUG_PRINT ("In function: debugger_execute_command(%s) %d\n",command, debugger->priv->debugger_is_busy);
608 debugger->priv->debugger_is_busy++;
609 debugger->priv->command_output_sent = FALSE;
610 cmd = g_strconcat (command, "\n", NULL);
611 debugger_log_command (debugger, cmd);
612 anjuta_launcher_send_stdin (debugger->priv->launcher, cmd);
613 g_free (cmd);
616 static void
617 debugger_queue_execute_command (Debugger *debugger)
619 DEBUG_PRINT ("In function: debugger_queue_execute_command()");
621 if (!debugger->priv->debugger_is_busy &&
622 !debugger->priv->starting &&
623 g_list_length (debugger->priv->cmd_queqe) >= 1)
625 debugger_clear_buffers (debugger);
626 if (debugger_queue_set_next_command (debugger))
627 debugger_execute_command (debugger, debugger->priv->current_cmd.cmd);
631 static void
632 debugger_load_executable_finish (Debugger *debugger, const GDBMIValue *mi_results,
633 const GList *cli_results, GError *error)
635 DEBUG_PRINT ("Program loaded");
636 debugger->priv->prog_is_loaded = TRUE;
638 g_signal_emit_by_name (debugger->priv->instance, "program-loaded");
641 void
642 debugger_load_executable (Debugger *debugger, const gchar *prog)
644 gchar *command, *dir, *msg;
646 g_return_if_fail (IS_DEBUGGER (debugger));
647 g_return_if_fail (prog != NULL);
649 DEBUG_PRINT ("In function: debugger_load_executable(%s)", prog);
651 if (debugger->priv->output_callback)
653 msg = g_strconcat (_("Loading Executable: "), prog, "\n", NULL);
654 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT, msg,
655 debugger->priv->output_user_data);
656 g_free (msg);
659 command = g_strconcat ("-file-exec-and-symbols ", prog, NULL);
660 dir = g_path_get_dirname (prog);
661 /* TODO
662 anjuta_set_execution_dir(dir);
664 g_free (dir);
665 debugger_queue_command (debugger, command, FALSE, FALSE, debugger_load_executable_finish, NULL, NULL);
666 g_free (command);
667 debugger->priv->starting = TRUE;
668 debugger->priv->terminating = FALSE;
671 void
672 debugger_load_core (Debugger *debugger, const gchar *core)
674 gchar *command, *dir, *msg;
676 g_return_if_fail (IS_DEBUGGER (debugger));
677 g_return_if_fail (core != NULL);
679 DEBUG_PRINT ("In function: debugger_load_core(%s)", core);
681 if (debugger->priv->output_callback)
683 msg = g_strconcat (_("Loading Core: "), core, "\n", NULL);
684 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT, msg,
685 debugger->priv->output_user_data);
686 g_free (msg);
689 command = g_strconcat ("core ", core, NULL);
690 dir = g_path_get_dirname (core);
691 debugger->priv->search_dirs =
692 g_list_prepend (debugger->priv->search_dirs, dir);
693 debugger_queue_command (debugger, command, FALSE, FALSE, NULL, NULL, NULL);
694 g_free (command);
697 gboolean
698 debugger_start (Debugger *debugger, const GList *search_dirs,
699 const gchar *prog, gboolean is_libtool_prog)
701 gchar *command_str, *dir, *tmp, *text, *msg;
702 gchar *exec_dir;
703 gboolean ret;
704 const GList *node;
705 AnjutaLauncher *launcher;
706 GList *dir_list = NULL;
707 gchar *term = NULL;
709 DEBUG_PRINT ("In function: debugger_start(%s) libtool %d", prog == NULL ? "(null)" : prog, is_libtool_prog);
711 if (anjuta_util_prog_is_installed ("gdb", TRUE) == FALSE)
712 return FALSE;
714 debugger_queue_clear (debugger);
716 tmp = g_strconcat (PACKAGE_DATA_DIR, "/", "gdb.init", NULL);
717 if (g_file_test (tmp, G_FILE_TEST_IS_REGULAR) == FALSE)
719 anjuta_util_dialog_error (debugger->priv->parent_win,
720 _("Unable to find: %s.\n"
721 "Unable to initialize debugger.\n"
722 "Make sure Anjuta is installed correctly."),
723 tmp);
724 g_free (tmp);
725 return FALSE;
727 g_free (tmp);
729 /* Prepare source search directories */
730 exec_dir = NULL;
731 if (prog)
732 exec_dir = g_path_get_dirname (prog);
734 if (exec_dir)
736 dir = g_strconcat (" -directory=", exec_dir, NULL);
737 dir_list = g_list_prepend (dir_list, exec_dir);
739 else
741 dir = g_strdup (" ");
744 node = search_dirs;
745 while (node)
747 text = node->data;
748 if (strncmp (text, "file://", 7) == 0)
750 text += 7;
752 else
754 g_warning ("Debugger source search uri '%s' is not a local uri", text);
757 if (text[0] == '/')
759 tmp = g_strconcat (dir, " -directory=", text, NULL);
760 g_free (dir);
761 dir = tmp;
763 dir_list = g_list_prepend (dir_list, g_strdup (text));
765 else
767 g_warning ("Debugger source search dir '%s' is not absolute",
768 text);
770 node = g_list_next (node);
773 /* Now save the dir list. Order is automatically revesed */
774 node = dir_list;
775 while (node)
777 debugger->priv->search_dirs =
778 g_list_prepend (debugger->priv->search_dirs, node->data);
779 node = g_list_next (node);
781 g_list_free (dir_list);
783 if (prog && strlen(prog) > 0)
785 if (exec_dir)
786 chdir (exec_dir);
787 if (is_libtool_prog == FALSE)
789 command_str = g_strdup_printf (GDB_PATH " -f -n -i=mi2 %s %s "
790 "-x %s/gdb.init %s", dir, term == NULL ? "" : term,
791 PACKAGE_DATA_DIR, prog);
793 else
795 command_str = g_strdup_printf ("libtool --mode=execute " GDB_PATH
796 " -f -n -i=mi2 %s %s "
797 "-x %s/gdb.init %s", dir, term == NULL ? "" : term,
798 PACKAGE_DATA_DIR, prog);
801 else
803 if (is_libtool_prog == FALSE)
805 command_str = g_strdup_printf (GDB_PATH " -f -n -i=mi2 %s %s "
806 "-x %s/gdb.init ", term == NULL ? "" : term,
807 dir, PACKAGE_DATA_DIR);
809 else
811 command_str = g_strdup_printf ("libtool --mode=execute " GDB_PATH
812 " -f -n -i=mi2 %s %s -x "
813 "%s/gdb.init ",
814 dir, term == NULL ? "" : term, PACKAGE_DATA_DIR);
817 g_free (dir);
818 g_free (term);
819 debugger->priv->starting = TRUE;
820 debugger->priv->terminating = FALSE;
821 debugger->priv->loading = prog != NULL ? TRUE : FALSE;
822 debugger->priv->debugger_is_busy = 1;
824 /* Prepare for launch. */
825 launcher = debugger->priv->launcher;
826 anjuta_launcher_set_terminate_on_exit (launcher, TRUE);
827 g_signal_connect (G_OBJECT (launcher), "child-exited",
828 G_CALLBACK (on_gdb_terminated), debugger);
829 ret = anjuta_launcher_execute (launcher, command_str,
830 on_gdb_output_arrived, debugger);
832 if (ret)
834 debugger->priv->debugger_is_started = TRUE;
835 debugger->priv->prog_is_loaded = prog != NULL;
837 anjuta_launcher_set_encoding (launcher, "ISO-8859-1");
839 if (debugger->priv->output_callback != NULL)
841 if (ret == TRUE)
843 /* TODO anjuta_update_app_status (TRUE, _("Debugger")); */
844 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
845 _("Getting ready to start debugging "
846 "session...\n"),
847 debugger->priv->output_user_data);
849 if (prog)
851 msg = g_strconcat (_("Loading Executable: "), prog, "\n", NULL);
852 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
853 msg,
854 debugger->priv->output_user_data);
855 g_free (msg);
857 else
859 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
860 _("No executable specified.\n"),
861 debugger->priv->output_user_data);
862 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
863 _("Open an executable or attach "
864 "to a process to start "
865 "debugging.\n"),
866 debugger->priv->output_user_data);
869 else
871 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
872 _("There was an error whilst "
873 "launching the debugger.\n"),
874 debugger->priv->output_user_data);
875 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
876 _("Make sure 'gdb' is installed "
877 "on the system.\n"),
878 debugger->priv->output_user_data);
881 g_free (command_str);
883 return TRUE;
886 static void
887 gdb_stdout_line_arrived (Debugger *debugger, const gchar * chars)
889 gint i = 0;
891 while (chars[i])
893 if (chars[i] == '\n')
895 debugger_stdo_flush (debugger);
897 else
899 g_string_append_c (debugger->priv->stdo_line, chars[i]);
901 i++;
905 static void
906 gdb_stderr_line_arrived (Debugger *debugger, const gchar * chars)
908 gint i;
910 for (i = 0; i < strlen (chars); i++)
912 if (chars[i] == '\n')
913 debugger_stde_flush (debugger);
914 else
915 g_string_append_c (debugger->priv->stde_line, chars[i]);
919 static void
920 on_gdb_output_arrived (AnjutaLauncher *launcher,
921 AnjutaLauncherOutputType output_type,
922 const gchar *chars, gpointer data)
924 Debugger *debugger = DEBUGGER (data);
925 DEBUG_PRINT ("on gdb output arrived");
927 /* Do not emit signal when the debugger is destroyed */
928 if (debugger->priv->instance == NULL) return;
930 switch (output_type)
932 case ANJUTA_LAUNCHER_OUTPUT_STDERR:
933 gdb_stderr_line_arrived (debugger, chars);
934 break;
935 case ANJUTA_LAUNCHER_OUTPUT_STDOUT:
936 gdb_stdout_line_arrived (debugger, chars);
937 break;
938 default:
939 break;
943 static void
944 debugger_handle_post_execution (Debugger *debugger)
946 switch (debugger->priv->post_execution_flag)
948 case DEBUGGER_NONE:
949 break;
950 case DEBUGGER_EXIT:
951 DEBUG_PRINT ("debugger stop in handle post execution\n");
952 debugger_stop (debugger);
953 break;
954 case DEBUGGER_RERUN_PROGRAM:
955 DEBUG_PRINT ("debugger run in handle post execution\n");
956 debugger_run (debugger);
957 break;
958 default:
959 g_warning ("Execution should not reach here");
963 static void
964 debugger_process_frame (Debugger *debugger, const GDBMIValue *val)
966 const GDBMIValue *file, *line, *frame, *addr, *fullname, *thread;
967 const gchar *file_str = NULL;
968 guint line_num = 0;
969 gulong addr_num = 0;
971 g_return_if_fail (val != NULL);
973 thread = gdbmi_value_hash_lookup (val, "thread-id");
974 if (thread)
976 debugger->priv->current_thread = strtoul (gdbmi_value_literal_get (thread), NULL, 0);
978 debugger->priv->current_frame = 0;
980 frame = gdbmi_value_hash_lookup (val, "frame");
981 if (frame)
983 fullname = gdbmi_value_hash_lookup (frame, "fullname");
984 if (fullname)
986 file_str = gdbmi_value_literal_get (fullname);
987 if (*file_str == '\0') file_str = NULL;
989 else
991 file = gdbmi_value_hash_lookup (frame, "file");
992 if (file)
994 file_str = gdbmi_value_literal_get (file);
995 if (*file_str == '\0') file_str = NULL;
999 if (file_str != NULL)
1001 line = gdbmi_value_hash_lookup (frame, "line");
1002 if (line)
1004 line_num = strtoul (gdbmi_value_literal_get (line), NULL, 0);
1008 addr = gdbmi_value_hash_lookup (frame, "addr");
1009 if (addr)
1011 addr_num = strtoul (gdbmi_value_literal_get (addr), NULL, 0);
1013 debugger_program_moved (debugger, file_str, line_num, addr_num);
1017 static GError*
1018 gdb_parse_error (Debugger *debugger, const GDBMIValue *mi_results)
1020 const GDBMIValue *message;
1021 const gchar *literal;
1022 guint code = IANJUTA_DEBUGGER_UNKNOWN_ERROR;
1024 message = gdbmi_value_hash_lookup (mi_results, "msg");
1025 literal = gdbmi_value_literal_get (message);
1027 if ((mi_results != NULL)
1028 && ((message = gdbmi_value_hash_lookup (mi_results, "msg")) != NULL)
1029 && ((literal = gdbmi_value_literal_get (message)) != NULL)
1030 && (*literal != '\0'))
1032 code = gdb_match_error (literal);
1033 DEBUG_PRINT ("error code %d", code);
1035 else
1037 /* No error message */
1038 literal = "Error without a message";
1041 return g_error_new_literal (IANJUTA_DEBUGGER_ERROR, code, literal);
1045 /* Parsing output
1046 *---------------------------------------------------------------------------*/
1048 static void
1049 debugger_parse_output (Debugger *debugger)
1051 gchar *line;
1053 line = debugger->priv->stdo_line->str;
1055 if (line[0] == '\032' && line[1] == '\032')
1057 gchar *filename;
1058 guint lineno;
1060 gdb_util_parse_error_line (&(line[2]), &filename, &lineno);
1061 if (filename)
1063 debugger_program_moved (debugger, filename, lineno, 0);
1064 g_free (filename);
1067 else
1069 gchar *proper_msg;
1070 gsize len;
1072 len = strlen (line);
1073 if (line[1] == '\"' && line [strlen(line) - 1] == '\"')
1075 line[1] = line[0];
1076 /* Reserve space for an additional carriage return */
1077 line[strlen(line) - 1] = ' ';
1078 proper_msg = g_strcompress (line + 1);
1079 len = strlen (proper_msg) - 1;
1080 proper_msg[len] = '\0';
1082 else
1084 /* Reserve space for an additional carriage return */
1085 proper_msg = g_strndup (line, len + 1);
1088 if (strcmp(proper_msg, "~Stopped due to shared library event\n") == 0)
1090 /* Recognize a solib event */
1091 debugger->priv->solib_event = TRUE;
1092 g_free (proper_msg);
1094 else if (debugger->priv->current_cmd.parser)
1096 /* Save GDB CLI output */
1097 debugger->priv->cli_lines = g_list_prepend (debugger->priv->cli_lines,
1098 proper_msg);
1100 else
1102 /* Discard CLI output */
1103 g_free (proper_msg);
1108 static void
1109 debugger_parse_stopped (Debugger *debugger)
1111 gchar *line = debugger->priv->stdo_line->str;
1114 if (!debugger->priv->solib_event)
1116 gboolean program_exited = FALSE;
1117 GDBMIValue *val;
1119 /* Check if program has exited */
1120 val = gdbmi_value_parse (line);
1121 if (val)
1123 const GDBMIValue *reason;
1124 const gchar *str = NULL;
1126 debugger_process_frame (debugger, val);
1128 reason = gdbmi_value_hash_lookup (val, "reason");
1129 if (reason)
1130 str = gdbmi_value_literal_get (reason);
1132 if (str && (strncmp (str, "exited", 6) == 0))
1134 program_exited = TRUE;
1137 /* Emit signal received if necessary */
1138 if (str && strcmp (str, "exited-signalled") == 0)
1140 const GDBMIValue *signal_name, *signal_meaning;
1141 const gchar *signal_str, *signal_meaning_str;
1143 signal_name = gdbmi_value_hash_lookup (val, "signal-name");
1144 signal_str = gdbmi_value_literal_get (signal_name);
1145 signal_meaning = gdbmi_value_hash_lookup (val, "signal-meaning");
1146 signal_meaning_str = gdbmi_value_literal_get (signal_meaning);
1147 g_signal_emit_by_name (debugger->priv->instance, "signal-received", signal_str, signal_meaning_str);
1149 else if (str && strcmp (str, "signal-received") == 0)
1151 const GDBMIValue *signal_name, *signal_meaning;
1152 const gchar *signal_str, *signal_meaning_str;
1154 signal_name = gdbmi_value_hash_lookup (val, "signal-name");
1155 signal_str = gdbmi_value_literal_get (signal_name);
1156 signal_meaning = gdbmi_value_hash_lookup (val, "signal-meaning");
1157 signal_meaning_str = gdbmi_value_literal_get (signal_meaning);
1159 g_signal_emit_by_name (debugger->priv->instance, "signal-received", signal_str, signal_meaning_str);
1162 if (debugger->priv->output_callback)
1164 if (str && strcmp (str, "exited-normally") == 0)
1166 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1167 _("Program exited normally\n"),
1168 debugger->priv->output_user_data);
1170 else if (str && strcmp (str, "exited") == 0)
1172 const GDBMIValue *errcode;
1173 const gchar *errcode_str;
1174 gchar *msg;
1176 errcode = gdbmi_value_hash_lookup (val, "exit-code");
1177 errcode_str = gdbmi_value_literal_get (errcode);
1178 msg = g_strdup_printf (_("Program exited with error code %s\n"),
1179 errcode_str);
1180 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1181 msg, debugger->priv->output_user_data);
1182 g_free (msg);
1184 else if (str && strcmp (str, "breakpoint-hit") == 0)
1186 const GDBMIValue *bkptno;
1187 const gchar *bkptno_str;
1188 gchar *msg;
1190 bkptno = gdbmi_value_hash_lookup (val, "bkptno");
1191 bkptno_str = gdbmi_value_literal_get (bkptno);
1193 msg = g_strdup_printf (_("Breakpoint number %s hit\n"),
1194 bkptno_str);
1195 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1196 msg, debugger->priv->output_user_data);
1197 g_free (msg);
1199 else if (str && strcmp (str, "function-finished") == 0)
1201 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1202 _("Function finished\n"),
1203 debugger->priv->output_user_data);
1205 else if (str && strcmp (str, "end-stepping-range") == 0)
1207 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1208 _("Stepping finished\n"),
1209 debugger->priv->output_user_data);
1211 else if (str && strcmp (str, "location-reached") == 0)
1213 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1214 _("Location reached\n"),
1215 debugger->priv->output_user_data);
1220 if (program_exited)
1222 debugger->priv->prog_is_running = FALSE;
1223 debugger->priv->prog_is_attached = FALSE;
1224 debugger_handle_post_execution (debugger);
1225 debugger->priv->exiting = TRUE;
1227 else
1229 // g_signal_emit_by_name (debugger->priv->instance, "program-stopped");
1230 debugger->priv->stopping = TRUE;
1233 debugger->priv->cli_lines = g_list_reverse (debugger->priv->cli_lines);
1234 if ((debugger->priv->current_cmd.cmd != NULL) &&
1235 (debugger->priv->current_cmd.parser != NULL))
1237 debugger->priv->current_cmd.parser (debugger, val,
1238 debugger->priv->cli_lines, FALSE);
1239 debugger->priv->command_output_sent = TRUE;
1240 DEBUG_PRINT ("In function: Sending output...");
1243 if (val)
1244 gdbmi_value_free (val);
1248 static void
1249 debugger_parse_prompt (Debugger *debugger)
1251 /* If the parser has not yet been called, call it now. */
1252 if (debugger->priv->command_output_sent == FALSE &&
1253 debugger->priv->current_cmd.parser)
1255 debugger->priv->current_cmd.parser (debugger, NULL,
1256 debugger->priv->cli_lines, FALSE);
1257 debugger->priv->command_output_sent = TRUE;
1260 debugger->priv->debugger_is_busy--;
1261 debugger_queue_execute_command (debugger); /* Next command. Go. */
1262 debugger_emit_ready (debugger);
1265 static gboolean
1266 parse_breakpoint (IAnjutaDebuggerBreakpointItem* bp, const GDBMIValue *brkpnt)
1268 const GDBMIValue *literal;
1269 const gchar* value;
1271 memset (bp, 0, sizeof (*bp));
1273 literal = gdbmi_value_hash_lookup (brkpnt, "number");
1274 if (literal)
1276 value = gdbmi_value_literal_get (literal);
1277 bp->id = strtoul (value, NULL, 10);
1280 literal = gdbmi_value_hash_lookup (brkpnt, "fullname");
1281 if (literal == NULL) literal = gdbmi_value_hash_lookup (brkpnt, "file");
1282 if (literal)
1284 value = gdbmi_value_literal_get (literal);
1285 bp->file = (gchar *)value;
1288 literal = gdbmi_value_hash_lookup (brkpnt, "line");
1289 if (literal)
1291 value = gdbmi_value_literal_get (literal);
1292 bp->line = strtoul (value, NULL, 10);
1293 bp->type |= IANJUTA_DEBUGGER_BREAKPOINT_ON_LINE;
1296 literal = gdbmi_value_hash_lookup (brkpnt, "type");
1297 if (literal)
1299 value = gdbmi_value_literal_get (literal);
1302 literal = gdbmi_value_hash_lookup (brkpnt, "disp");
1303 if (literal)
1305 value = gdbmi_value_literal_get (literal);
1306 if (strcmp (value, "keep") == 0)
1308 bp->type |= IANJUTA_DEBUGGER_BREAKPOINT_WITH_TEMPORARY;
1309 bp->temporary = FALSE;
1311 else if ((strcmp (value, "nokeep") == 0) || (strcmp (value, "del") == 0))
1313 bp->type |= IANJUTA_DEBUGGER_BREAKPOINT_WITH_TEMPORARY;
1314 bp->temporary = TRUE;
1318 literal = gdbmi_value_hash_lookup (brkpnt, "enabled");
1319 if (literal)
1321 value = gdbmi_value_literal_get (literal);
1322 if (strcmp (value, "n") == 0)
1324 bp->type |= IANJUTA_DEBUGGER_BREAKPOINT_WITH_ENABLE;
1325 bp->enable = FALSE;
1327 else if (strcmp (value, "y") == 0)
1329 bp->type |= IANJUTA_DEBUGGER_BREAKPOINT_WITH_ENABLE;
1330 bp->enable = TRUE;
1334 literal = gdbmi_value_hash_lookup (brkpnt, "addr");
1335 if (literal)
1337 value = gdbmi_value_literal_get (literal);
1338 bp->address = strtoul (value, NULL, 16);
1339 bp->type |= IANJUTA_DEBUGGER_BREAKPOINT_ON_ADDRESS;
1342 literal = gdbmi_value_hash_lookup (brkpnt, "func");
1343 if (literal)
1345 value = gdbmi_value_literal_get (literal);
1346 bp->function = (gchar *)value;
1347 bp->type |= IANJUTA_DEBUGGER_BREAKPOINT_ON_FUNCTION;
1350 literal = gdbmi_value_hash_lookup (brkpnt, "times");
1351 if (literal)
1353 value = gdbmi_value_literal_get (literal);
1354 bp->times = strtoul (value, NULL, 10);
1355 bp->type |= IANJUTA_DEBUGGER_BREAKPOINT_WITH_TIME;
1357 DEBUG_PRINT("parse time %d", bp->times);
1359 literal = gdbmi_value_hash_lookup (brkpnt, "ignore");
1360 if (literal)
1362 value = gdbmi_value_literal_get (literal);
1363 bp->ignore = strtoul (value, NULL, 10);
1364 bp->type |= IANJUTA_DEBUGGER_BREAKPOINT_WITH_IGNORE;
1367 literal = gdbmi_value_hash_lookup (brkpnt, "cond");
1368 if (literal)
1370 value = gdbmi_value_literal_get (literal);
1371 bp->condition = (gchar *)value;
1372 bp->type |= IANJUTA_DEBUGGER_BREAKPOINT_WITH_CONDITION;
1375 return TRUE;
1378 static void
1379 debugger_stdo_flush (Debugger *debugger)
1381 gchar *line;
1383 line = debugger->priv->stdo_line->str;
1385 #ifdef DEBUG
1386 DEBUG_PRINT ("GDB:< %s", line);
1387 #else
1388 if (debugger->priv->gdb_log) g_message ("GDB:< %s", line);
1389 #endif
1390 debugger_log_output (debugger, line);
1391 if (strlen (line) == 0)
1393 return;
1395 if (strncasecmp (line, "^error", 6) == 0)
1397 /* GDB reported error */
1398 if ((debugger->priv->current_cmd.keep_result) || (debugger->priv->stdo_acc->len != 0))
1400 /* Keep result for next command */
1402 if (debugger->priv->stdo_acc->len == 0)
1404 g_string_append (debugger->priv->stdo_acc, line);
1406 else
1408 line = strchr (line, ',');
1409 if (line != NULL)
1411 g_string_append (debugger->priv->stdo_acc, line);
1414 line = debugger->priv->stdo_acc->str;
1417 GDBMIValue *val = gdbmi_value_parse (line);
1418 GError *error;
1420 error = gdb_parse_error (debugger, val);
1422 if (debugger->priv->current_cmd.parser != NULL)
1424 debugger->priv->current_cmd.parser (debugger, val, debugger->priv->cli_lines, error);
1425 debugger->priv->command_output_sent = TRUE; }
1426 DEBUG_PRINT("GDB: error %s", error->message);
1427 /*else
1429 anjuta_util_dialog_error (debugger->priv->parent_win, "%s", error->message);
1431 g_error_free (error);
1432 gdbmi_value_free (val);
1434 else if (strncasecmp(line, "^running", 8) == 0)
1436 /* Program started running */
1437 debugger->priv->prog_is_running = TRUE;
1438 /* debugger->priv->skip_next_prompt = TRUE; Replaced by debugger_is_busy++ */
1439 debugger->priv->debugger_is_busy++;
1440 g_signal_emit_by_name (debugger->priv->instance, "program-running");
1442 else if (strncasecmp (line, "*stopped", 8) == 0)
1444 /* Process has stopped */
1445 debugger_parse_stopped (debugger);
1447 else if (strncasecmp (line, "^done", 5) == 0)
1449 if ((debugger->priv->current_cmd.keep_result) || (debugger->priv->stdo_acc->len != 0))
1451 /* Keep result for next command */
1453 if (debugger->priv->stdo_acc->len == 0)
1455 g_string_append (debugger->priv->stdo_acc, line);
1457 else
1459 line = strchr (line, ',');
1460 if (line != NULL)
1462 g_string_append (debugger->priv->stdo_acc, line);
1465 line = debugger->priv->stdo_acc->str;
1468 if (!debugger->priv->current_cmd.keep_result)
1470 /* GDB command has reported output */
1471 GDBMIValue *val = gdbmi_value_parse (line);
1473 debugger->priv->cli_lines = g_list_reverse (debugger->priv->cli_lines);
1474 if ((debugger->priv->current_cmd.cmd != NULL) &&
1475 (debugger->priv->current_cmd.parser != NULL))
1477 debugger->priv->current_cmd.parser (debugger, val,
1478 debugger->priv->cli_lines, FALSE);
1479 debugger->priv->command_output_sent = TRUE;
1480 DEBUG_PRINT ("In function: Sending output...");
1482 else /* if (val) */
1484 /*g_signal_emit_by_name (debugger, "results-arrived",
1485 debugger->priv->current_cmd.cmd, val);*/
1488 if (val)
1490 /*debugger_process_frame (debugger, val);*/
1491 gdbmi_value_free (val);
1495 if (!debugger->priv->current_cmd.keep_result)
1497 g_string_assign (debugger->priv->stdo_acc, "");
1500 else if (strncasecmp (line, GDB_PROMPT, strlen (GDB_PROMPT)) == 0)
1502 debugger_parse_prompt (debugger);
1504 else
1506 debugger_parse_output (debugger);
1509 /* Clear the line buffer */
1510 g_string_assign (debugger->priv->stdo_line, "");
1513 void
1514 debugger_stde_flush (Debugger *debugger)
1516 if ((debugger->priv->output_callback) && (strlen (debugger->priv->stde_line->str) > 0))
1518 debugger->priv->output_callback (IANJUTA_DEBUGGER_ERROR_OUTPUT,
1519 debugger->priv->stde_line->str,
1520 debugger->priv->output_user_data);
1522 /* Clear the line buffer */
1523 g_string_assign (debugger->priv->stde_line, "");
1526 static void
1527 on_gdb_terminated (AnjutaLauncher *launcher,
1528 gint child_pid, gint status, gulong t,
1529 gpointer data)
1531 Debugger *debugger = DEBUGGER (data);
1532 GError *err = NULL;
1534 g_signal_handlers_disconnect_by_func (G_OBJECT (launcher),
1535 G_CALLBACK (on_gdb_terminated),
1536 debugger);
1538 DEBUG_PRINT ("In function: gdb_terminated()");
1540 /* Clear the command queue & Buffer */
1541 debugger_clear_buffers (debugger);
1543 /* Good Bye message */
1544 /*if (!debugger->priv->terminating)
1546 anjuta_util_dialog_error (debugger->priv->parent_win,
1547 _("gdb terminated unexpectedly with status %d\n"), status);
1549 debugger->priv->prog_is_running = FALSE;
1550 debugger->priv->prog_is_attached = FALSE;
1551 debugger->priv->prog_is_loaded = FALSE;
1552 debugger->priv->debugger_is_busy = 0;
1553 debugger->priv->skip_next_prompt = FALSE;
1554 if (!debugger->priv->terminating)
1556 err = g_error_new (IANJUTA_DEBUGGER_ERROR,
1557 IANJUTA_DEBUGGER_OTHER_ERROR,
1558 "gdb terminated with status %d", status);
1560 debugger->priv->terminating = FALSE;
1561 debugger->priv->debugger_is_started = FALSE;
1562 if (debugger->priv->instance)
1564 g_signal_emit_by_name (debugger->priv->instance, "debugger-stopped", err);
1566 if (err != NULL) g_error_free (err);
1569 static void
1570 debugger_stop_real (Debugger *debugger)
1572 DEBUG_PRINT ("In function: debugger_stop_real()");
1574 /* if program is attached - detach from it before quiting */
1575 if (debugger->priv->prog_is_attached == TRUE)
1576 debugger_queue_command (debugger, "detach", FALSE, FALSE, NULL, NULL, NULL);
1578 debugger->priv->terminating = TRUE;
1579 debugger_queue_command (debugger, "-gdb-exit", FALSE, FALSE, NULL, NULL, NULL);
1582 gboolean
1583 debugger_stop (Debugger *debugger)
1585 #if 0
1586 gboolean ret = TRUE;
1588 if (debugger->priv->prog_is_running == TRUE)
1590 GtkWidget *dialog;
1591 gchar *mesg;
1593 if (debugger->priv->prog_is_attached == TRUE)
1594 mesg = _("The program is attached.\n"
1595 "Do you still want to stop the debugger?");
1596 else
1597 mesg = _("The program is running.\n"
1598 "Do you still want to stop the debugger?");
1599 dialog = gtk_message_dialog_new (debugger->priv->parent_win,
1600 GTK_DIALOG_DESTROY_WITH_PARENT,
1601 GTK_MESSAGE_QUESTION,
1602 GTK_BUTTONS_NONE, mesg);
1603 gtk_dialog_add_buttons (GTK_DIALOG (dialog),
1604 GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
1605 GTK_STOCK_STOP, GTK_RESPONSE_YES,
1606 NULL);
1607 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_YES)
1608 debugger_stop_real (debugger);
1609 else
1610 ret = FALSE;
1611 gtk_widget_destroy (dialog);
1613 else
1614 debugger_stop_real (debugger);
1615 return ret;
1616 #endif
1617 debugger_stop_real (debugger);
1619 return TRUE;
1622 gboolean
1623 debugger_abort (Debugger *debugger)
1625 DEBUG_PRINT ("In function: debugger_abort()");
1627 /* Stop inferior */
1628 if ((debugger->priv->prog_is_attached == FALSE) && (debugger->priv->inferior_pid != 0))
1630 kill (debugger->priv->inferior_pid, SIGTERM);
1631 debugger->priv->inferior_pid = 0;
1634 /* Stop gdb */
1635 debugger->priv->terminating = TRUE;
1636 g_signal_handlers_disconnect_by_func (G_OBJECT (debugger->priv->launcher), G_CALLBACK (on_gdb_terminated), debugger);
1637 anjuta_launcher_reset (debugger->priv->launcher);
1639 /* Free memory */
1640 debugger_queue_clear (debugger);
1641 g_list_foreach (debugger->priv->search_dirs, (GFunc)g_free, NULL);
1642 g_list_free (debugger->priv->search_dirs);
1643 debugger->priv->search_dirs = NULL;
1645 /* Emit signal, state of the debugger must be DEBUGGER_STOPPED */
1646 debugger->priv->prog_is_running = FALSE;
1647 debugger->priv->prog_is_attached = FALSE;
1648 debugger->priv->prog_is_loaded = FALSE;
1649 debugger->priv->debugger_is_busy = 0;
1650 debugger->priv->debugger_is_started = FALSE;
1651 if (debugger->priv->instance != NULL)
1653 g_signal_emit_by_name (debugger->priv->instance, "debugger-stopped", NULL);
1654 debugger->priv->instance = NULL;
1657 return TRUE;
1660 gchar*
1661 debugger_get_source_path (Debugger *debugger, const gchar *file)
1663 GList *node;
1664 gchar *path = NULL;
1666 if (g_path_is_absolute (file))
1667 return g_strdup (file);
1669 node = debugger->priv->search_dirs;
1670 while (node)
1672 path = g_build_filename (node->data, file, NULL);
1673 if (g_file_test (path, G_FILE_TEST_EXISTS))
1674 break;
1675 g_free (path);
1676 path = NULL;
1677 node = g_list_next (node);
1680 if (path == NULL)
1682 /* The file could be found nowhere. Use current directory */
1683 gchar *cwd;
1684 cwd = g_get_current_dir ();
1685 path = g_build_filename (cwd, file, NULL);
1686 g_free (cwd);
1688 return path;
1691 void
1692 debugger_set_output_callback (Debugger *debugger, IAnjutaDebuggerOutputCallback callback, gpointer user_data)
1694 debugger->priv->output_callback = callback;
1695 debugger->priv->output_user_data = user_data;
1698 void
1699 debugger_program_moved (Debugger *debugger, const gchar *file,
1700 gint line, gulong address)
1702 gchar *src_path;
1704 if ((file != NULL) && (*file != G_DIR_SEPARATOR))
1706 src_path = debugger_get_source_path (debugger, file);
1707 g_signal_emit_by_name (debugger->priv->instance, "program-moved", debugger->priv->inferior_pid, debugger->priv->current_thread, address, src_path, line);
1708 g_free (src_path);
1710 else
1712 g_signal_emit_by_name (debugger->priv->instance, "program-moved", debugger->priv->inferior_pid, debugger->priv->current_thread, address, file, line);
1716 static void
1717 debugger_info_program_finish (Debugger *debugger, const GDBMIValue *mi_results,
1718 const GList *cli_results, GError *error)
1720 DEBUG_PRINT ("In function: debugger_info_program()");
1722 /* Hack: find message string giving inferior pid */
1723 while (cli_results != NULL)
1725 gchar* child_proc;
1727 child_proc = strstr(cli_results->data, " child process ");
1728 if (child_proc != NULL)
1730 debugger->priv->inferior_pid = strtoul (child_proc + 15, NULL, 10);
1731 break;
1733 cli_results = g_list_next (cli_results);
1737 void
1738 debugger_start_program (Debugger *debugger, const gchar* args, const gchar* tty, gboolean stop)
1740 gchar *cmd;
1742 DEBUG_PRINT ("In function: debugger_start_program()");
1744 g_return_if_fail (IS_DEBUGGER (debugger));
1745 g_return_if_fail (debugger->priv->prog_is_running == FALSE);
1747 /* Without a terminal, the output of the debugged program
1748 * are lost */
1749 if (tty)
1751 cmd = g_strdup_printf ("-inferior-tty-set %s", tty);
1752 debugger_queue_command (debugger, cmd, FALSE, FALSE, NULL, NULL, NULL);
1753 g_free (cmd);
1756 debugger->priv->inferior_pid = 0;
1757 if (stop)
1759 debugger_queue_command (debugger, "-break-insert -t main", FALSE, FALSE, NULL, NULL, NULL);
1761 if (args && (*args))
1763 cmd = g_strconcat ("-exec-arguments ", args, NULL);
1764 debugger_queue_command (debugger, cmd, FALSE, FALSE, NULL, NULL, NULL);
1765 g_free (cmd);
1768 debugger_queue_command (debugger, "-exec-run", FALSE, FALSE, NULL, NULL, NULL);
1770 /* Get pid of program on next stop */
1771 debugger_queue_command (debugger, "info program", FALSE, FALSE, debugger_info_program_finish, NULL, NULL);
1772 debugger->priv->post_execution_flag = DEBUGGER_NONE;
1775 static void
1776 debugger_attach_process_finish (Debugger *debugger, const GDBMIValue *mi_results,
1777 const GList *cli_results, GError *error)
1779 DEBUG_PRINT ("Program attach finished");
1780 if (debugger->priv->output_callback)
1782 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1783 _("Program attached\n"),
1784 debugger->priv->output_user_data);
1786 debugger->priv->prog_is_attached = TRUE;
1787 debugger->priv->prog_is_running = TRUE;
1788 //debugger_emit_status (debugger);
1791 static void
1792 debugger_attach_process_real (Debugger *debugger, pid_t pid)
1794 gchar *buff;
1796 DEBUG_PRINT ("In function: debugger_attach_process_real()");
1798 if (debugger->priv->output_callback)
1800 buff = g_strdup_printf (_("Attaching to process: %d...\n"), pid);
1801 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1802 buff, debugger->priv->output_user_data);
1803 g_free (buff);
1806 debugger->priv->inferior_pid = pid;
1807 buff = g_strdup_printf ("attach %d", pid);
1808 debugger_queue_command (debugger, buff, FALSE, FALSE,
1809 debugger_attach_process_finish, NULL, NULL);
1810 g_free (buff);
1813 void
1814 debugger_attach_process (Debugger *debugger, pid_t pid)
1816 DEBUG_PRINT ("In function: debugger_attach_process()");
1818 g_return_if_fail (IS_DEBUGGER (debugger));
1820 if (debugger->priv->prog_is_running == TRUE)
1822 // TODO: Dialog to be made HIG compliant.
1823 gchar *mesg;
1824 GtkWidget *dialog;
1826 mesg = _("A process is already running.\n"
1827 "Would you like to terminate it and attach the new process?"),
1828 dialog = gtk_message_dialog_new (debugger->priv->parent_win,
1829 GTK_DIALOG_DESTROY_WITH_PARENT,
1830 GTK_MESSAGE_QUESTION,
1831 GTK_BUTTONS_YES_NO, mesg);
1832 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_YES)
1834 debugger_stop_program (debugger);
1835 debugger_attach_process_real (debugger, pid);
1837 gtk_widget_destroy (dialog);
1839 else if (getpid () == pid ||
1840 anjuta_launcher_get_child_pid (debugger->priv->launcher) == pid)
1842 anjuta_util_dialog_error (debugger->priv->parent_win,
1843 _("Anjuta is unable to attach to itself."));
1844 return;
1846 else
1847 debugger_attach_process_real (debugger, pid);
1850 void
1851 debugger_restart_program (Debugger *debugger)
1853 DEBUG_PRINT ("In function: debugger_restart_program()");
1855 g_return_if_fail (debugger->priv->prog_is_attached == FALSE);
1858 debugger->priv->post_execution_flag = DEBUGGER_RERUN_PROGRAM;
1859 debugger_stop_program (debugger);
1861 debugger_put_cmd_in_queqe ("tbreak main", DB_CMD_NONE, NULL, NULL);
1862 debugger_put_cmd_in_queqe ("run >/dev/null 2>/dev/null", DB_CMD_ALL,
1863 NULL, NULL);
1864 debugger_put_cmd_in_queqe ("info program", DB_CMD_NONE,
1865 on_debugger_update_prog_status, NULL);
1866 debugger_put_cmd_in_queqe ("continue", DB_CMD_NONE, NULL, NULL);
1867 debugger_execute_cmd_in_queqe ();
1871 void
1872 debugger_stop_program (Debugger *debugger)
1874 DEBUG_PRINT ("In function: debugger_stop_program()");
1876 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
1878 if (debugger->priv->prog_is_attached == TRUE)
1879 debugger_queue_command (debugger, "detach", FALSE, FALSE, NULL, NULL, NULL);
1880 else
1882 /* FIXME: Why doesn't -exec-abort work??? */
1883 /* debugger_queue_command (debugger, "-exec-abort", NULL, NULL); */
1884 debugger_queue_command (debugger, "kill", FALSE, FALSE, NULL, NULL, NULL);
1885 debugger->priv->prog_is_running = FALSE;
1886 debugger->priv->prog_is_attached = FALSE;
1887 g_signal_emit_by_name (debugger->priv->instance, "program-exited");
1888 if (debugger->priv->output_callback)
1890 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1891 _("Program terminated\n"),
1892 debugger->priv->output_user_data);
1894 debugger_handle_post_execution (debugger);
1898 static void
1899 debugger_detach_process_finish (Debugger *debugger, const GDBMIValue *mi_results,
1900 const GList *cli_results, GError *error)
1902 DEBUG_PRINT ("Program detach finished");
1903 if (debugger->priv->output_callback)
1905 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1906 _("Program detached\n"),
1907 debugger->priv->output_user_data);
1909 debugger->priv->prog_is_attached = FALSE;
1910 debugger->priv->prog_is_running = FALSE;
1911 g_signal_emit_by_name (debugger->priv->instance, "program-exited");
1914 void
1915 debugger_detach_process (Debugger *debugger)
1917 gchar *buff;
1919 DEBUG_PRINT ("In function: debugger_detach_process()");
1921 g_return_if_fail (debugger->priv->prog_is_attached == TRUE);
1923 if (debugger->priv->output_callback)
1925 buff = g_strdup_printf (_("Detaching the process...\n"));
1926 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1927 buff, debugger->priv->output_user_data);
1928 g_free (buff);
1931 debugger_queue_command (debugger, "detach", FALSE, FALSE,
1932 debugger_detach_process_finish, NULL, NULL);
1933 debugger->priv->prog_is_attached = FALSE;
1936 void
1937 debugger_interrupt (Debugger *debugger)
1939 DEBUG_PRINT ("In function: debugger_interrupt()");
1941 g_return_if_fail (IS_DEBUGGER (debugger));
1942 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
1944 if (debugger->priv->output_callback)
1946 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
1947 _("Interrupting the process\n"),
1948 debugger->priv->output_user_data);
1951 if (debugger->priv->inferior_pid == 0)
1953 /* In case we do not have the inferior pid, send signal to gdb */
1954 anjuta_launcher_signal (debugger->priv->launcher, SIGINT);
1956 else
1958 /* Send signal directly to inferior */
1959 kill (debugger->priv->inferior_pid, SIGINT);
1961 //g_signal_emit_by_name (debugger->priv->instance, "program-running");
1964 void
1965 debugger_run (Debugger *debugger)
1967 DEBUG_PRINT ("In function: debugger_run()");
1969 g_return_if_fail (IS_DEBUGGER (debugger));
1970 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
1972 /* Program running - continue */
1973 debugger_queue_command (debugger, "-exec-continue", FALSE, FALSE, NULL, NULL, NULL);
1976 void
1977 debugger_step_in (Debugger *debugger)
1979 DEBUG_PRINT ("In function: debugger_step_in()");
1981 g_return_if_fail (IS_DEBUGGER (debugger));
1982 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
1984 debugger_queue_command (debugger, "-exec-step", FALSE, FALSE, NULL, NULL, NULL);
1987 void
1988 debugger_step_over (Debugger *debugger)
1990 DEBUG_PRINT ("In function: debugger_step_over()");
1992 g_return_if_fail (IS_DEBUGGER (debugger));
1993 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
1995 debugger_queue_command (debugger, "-exec-next", FALSE, FALSE, NULL, NULL, NULL);
1998 void
1999 debugger_step_out (Debugger *debugger)
2001 DEBUG_PRINT ("In function: debugger_step_out()");
2003 g_return_if_fail (IS_DEBUGGER (debugger));
2004 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
2006 debugger_queue_command (debugger, "-exec-finish", FALSE, FALSE, NULL, NULL, NULL);
2009 void
2010 debugger_stepi_in (Debugger *debugger)
2012 DEBUG_PRINT ("In function: debugger_step_in()");
2014 g_return_if_fail (IS_DEBUGGER (debugger));
2015 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
2017 debugger_queue_command (debugger, "-exec-step-instruction", FALSE, FALSE, NULL, NULL, NULL);
2020 void
2021 debugger_stepi_over (Debugger *debugger)
2023 DEBUG_PRINT ("In function: debugger_step_over()");
2025 g_return_if_fail (IS_DEBUGGER (debugger));
2026 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
2028 debugger_queue_command (debugger, "-exec-next-instruction", FALSE, FALSE, NULL, NULL, NULL);
2031 void
2032 debugger_run_to_location (Debugger *debugger, const gchar *loc)
2034 gchar *buff;
2036 DEBUG_PRINT ("In function: debugger_run_to_location()");
2038 g_return_if_fail (IS_DEBUGGER (debugger));
2039 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
2041 buff = g_strdup_printf ("-exec-until %s", loc);
2042 debugger_queue_command (debugger, buff, FALSE, FALSE, NULL, NULL, NULL);
2043 g_free (buff);
2046 void
2047 debugger_run_to_position (Debugger *debugger, const gchar *file, guint line)
2049 gchar *buff;
2051 DEBUG_PRINT ("In function: debugger_run_to_position()");
2053 g_return_if_fail (IS_DEBUGGER (debugger));
2054 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
2056 buff = g_strdup_printf ("-break-insert -t %s:%d", file, line);
2057 debugger_queue_command (debugger, buff, FALSE, FALSE, NULL, NULL, NULL);
2058 g_free (buff);
2059 debugger_queue_command (debugger, "-exec-continue", FALSE, FALSE, NULL, NULL, NULL);
2062 void
2063 debugger_run_to_address (Debugger *debugger, gulong address)
2065 gchar *buff;
2067 DEBUG_PRINT ("In function: debugger_run_to_address()");
2069 g_return_if_fail (IS_DEBUGGER (debugger));
2070 g_return_if_fail (debugger->priv->prog_is_running == TRUE);
2072 buff = g_strdup_printf ("-break-insert -t *0x%lx", address);
2073 debugger_queue_command (debugger, buff, FALSE, FALSE, NULL, NULL, NULL);
2074 g_free (buff);
2075 debugger_queue_command (debugger, "-exec-continue", FALSE, FALSE, NULL, NULL, NULL);
2078 void
2079 debugger_command (Debugger *debugger, const gchar *command,
2080 gboolean suppress_error, DebuggerParserFunc parser,
2081 gpointer user_data)
2083 if (strncasecmp (command, "-exec-run",
2084 strlen ("-exec-run")) == 0 ||
2085 strncasecmp (command, "run", strlen ("run")) == 0)
2087 /* FIXME: The user might have passed args to the command */
2088 debugger_run (debugger);
2090 else if (strncasecmp (command, "-exec-step",
2091 strlen ("-exec-step")) == 0 ||
2092 strncasecmp (command, "step", strlen ("step")) == 0)
2094 debugger_step_in (debugger);
2096 else if (strncasecmp (command, "-exec-next",
2097 strlen ("-exec-next")) == 0 ||
2098 strncasecmp (command, "next", strlen ("next")) == 0)
2100 debugger_step_over (debugger);
2102 else if (strncasecmp (command, "-exec-finish",
2103 strlen ("-exec-finish")) == 0 ||
2104 strncasecmp (command, "finish", strlen ("finish")) == 0)
2106 debugger_step_out (debugger);
2108 else if (strncasecmp (command, "-exec-continue",
2109 strlen ("-exec-continue")) == 0 ||
2110 strncasecmp (command, "continue", strlen ("continue")) == 0)
2112 debugger_run (debugger);
2114 else if (strncasecmp (command, "-exec-until",
2115 strlen ("-exec-until")) == 0 ||
2116 strncasecmp (command, "until", strlen ("until")) == 0)
2118 debugger_run_to_location (debugger, strchr (command, ' '));
2120 else if (strncasecmp (command, "-exec-abort",
2121 strlen ("-exec-abort")) == 0 ||
2122 strncasecmp (command, "kill", strlen ("kill")) == 0)
2124 debugger_stop_program (debugger);
2126 else if (strncasecmp (command, "-target-attach",
2127 strlen ("-target-attach")) == 0 ||
2128 strncasecmp (command, "attach", strlen ("attach")) == 0)
2130 pid_t pid = 0;
2131 gchar *pid_str = strchr (command, ' ');
2132 if (pid_str)
2133 pid = atoi (pid_str);
2134 debugger_attach_process (debugger, pid);
2136 else if (strncasecmp (command, "-target-detach",
2137 strlen ("-target-detach")) == 0 ||
2138 strncasecmp (command, "detach", strlen ("detach")) == 0)
2140 debugger_detach_process (debugger);
2142 else if (strncasecmp (command, "-file-exec-and-symbols",
2143 strlen ("-file-exec-and-symbols")) == 0 ||
2144 strncasecmp (command, "file", strlen ("file")) == 0)
2146 debugger_load_executable (debugger, strchr (command, ' '));
2148 else if (strncasecmp (command, "core", strlen ("core")) == 0)
2150 debugger_load_core (debugger, strchr (command, ' '));
2152 else
2154 debugger_queue_command (debugger, command, suppress_error, FALSE,
2155 parser, user_data, NULL);
2159 static void
2160 debugger_add_breakpoint_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2162 IAnjutaDebuggerBreakpointItem bp;
2163 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2164 gpointer user_data = debugger->priv->current_cmd.user_data;
2166 if ((error != NULL) || (mi_results == NULL))
2168 /* Call callback in all case (useful for enable that doesn't return
2169 * anything */
2170 if (callback != NULL)
2171 callback (NULL, user_data, error);
2173 else if (callback != NULL)
2175 const GDBMIValue *brkpnt;
2177 brkpnt = gdbmi_value_hash_lookup (mi_results, "bkpt");
2178 parse_breakpoint (&bp, brkpnt);
2180 /* Call callback in all case (useful for enable that doesn't return
2181 * anything */
2182 callback (&bp, user_data, error);
2187 void
2188 debugger_add_breakpoint_at_line (Debugger *debugger, const gchar *file, guint line, IAnjutaDebuggerCallback callback, gpointer user_data)
2190 gchar *buff;
2192 DEBUG_PRINT ("In function: debugger_add_breakpoint()");
2194 g_return_if_fail (IS_DEBUGGER (debugger));
2196 buff = g_strdup_printf ("-break-insert %s:%u", file, line);
2197 debugger_queue_command (debugger, buff, FALSE, FALSE, debugger_add_breakpoint_finish, callback, user_data);
2198 g_free (buff);
2201 void
2202 debugger_add_breakpoint_at_function (Debugger *debugger, const gchar *file, const gchar *function, IAnjutaDebuggerCallback callback, gpointer user_data)
2204 gchar *buff;
2206 DEBUG_PRINT ("In function: debugger_add_breakpoint()");
2208 g_return_if_fail (IS_DEBUGGER (debugger));
2210 buff = g_strdup_printf ("-break-insert %s%s%s", file == NULL ? "" : file, file == NULL ? "" : ":" , function);
2211 debugger_queue_command (debugger, buff, FALSE, FALSE, debugger_add_breakpoint_finish, callback, user_data);
2212 g_free (buff);
2215 void
2216 debugger_add_breakpoint_at_address (Debugger *debugger, gulong address, IAnjutaDebuggerCallback callback, gpointer user_data)
2218 gchar *buff;
2220 DEBUG_PRINT ("In function: debugger_add_breakpoint()");
2222 g_return_if_fail (IS_DEBUGGER (debugger));
2224 buff = g_strdup_printf ("-break-insert *0x%lx", address);
2225 debugger_queue_command (debugger, buff, FALSE, FALSE, debugger_add_breakpoint_finish, callback, user_data);
2226 g_free (buff);
2229 void
2230 debugger_enable_breakpoint (Debugger *debugger, guint id, gboolean enable, IAnjutaDebuggerCallback callback, gpointer user_data)
2233 gchar *buff;
2235 DEBUG_PRINT ("In function: debugger_enable_breakpoint()");
2237 g_return_if_fail (IS_DEBUGGER (debugger));
2239 buff = g_strdup_printf (enable ? "-break-enable %d" : "-break-disable %d",id);
2240 debugger_queue_command (debugger, buff, FALSE, FALSE, debugger_add_breakpoint_finish, callback, user_data);
2241 g_free (buff);
2244 void
2245 debugger_ignore_breakpoint (Debugger *debugger, guint id, guint ignore, IAnjutaDebuggerCallback callback, gpointer user_data)
2247 gchar *buff;
2249 DEBUG_PRINT ("In function: debugger_ignore_breakpoint()");
2251 g_return_if_fail (IS_DEBUGGER (debugger));
2253 buff = g_strdup_printf ("-break-after %d %d", id, ignore);
2254 debugger_queue_command (debugger, buff, FALSE, FALSE, debugger_add_breakpoint_finish, callback, user_data);
2255 g_free (buff);
2258 void
2259 debugger_condition_breakpoint (Debugger *debugger, guint id, const gchar *condition, IAnjutaDebuggerCallback callback, gpointer user_data)
2261 gchar *buff;
2263 DEBUG_PRINT ("In function: debugger_ignore_breakpoint()");
2265 g_return_if_fail (IS_DEBUGGER (debugger));
2267 buff = g_strdup_printf ("-break-condition %d %s", id, condition == NULL ? "" : condition);
2268 debugger_queue_command (debugger, buff, FALSE, FALSE, debugger_add_breakpoint_finish, callback, user_data);
2269 g_free (buff);
2272 static void
2273 debugger_remove_breakpoint_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2275 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2276 gpointer user_data = debugger->priv->current_cmd.user_data;
2277 IAnjutaDebuggerBreakpointItem bp;
2279 bp.type = IANJUTA_DEBUGGER_BREAKPOINT_REMOVED;
2280 bp.id = atoi (debugger->priv->current_cmd.cmd + 14);
2281 if (callback != NULL)
2282 callback (&bp, user_data, NULL);
2285 void
2286 debugger_remove_breakpoint (Debugger *debugger, guint id, IAnjutaDebuggerCallback callback, gpointer user_data)
2288 gchar *buff;
2290 DEBUG_PRINT ("In function: debugger_delete_breakpoint()");
2292 g_return_if_fail (IS_DEBUGGER (debugger));
2294 buff = g_strdup_printf ("-break-delete %d", id);
2295 debugger_queue_command (debugger, buff, FALSE, FALSE, debugger_remove_breakpoint_finish, callback, user_data);
2296 g_free (buff);
2299 static void
2300 debugger_list_breakpoint_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2302 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2303 gpointer user_data = debugger->priv->current_cmd.user_data;
2304 IAnjutaDebuggerBreakpointItem* bp;
2305 const GDBMIValue *table;
2306 GList *list = NULL;
2308 if ((error != NULL) || (mi_results == NULL))
2310 /* Call callback in all case (useful for enable that doesn't return
2311 * anything */
2312 if (callback != NULL)
2313 callback (NULL, user_data, error);
2316 table = gdbmi_value_hash_lookup (mi_results, "BreakpointTable");
2317 if (table)
2319 table = gdbmi_value_hash_lookup (table, "body");
2321 if (table)
2323 int i;
2325 for (i = 0; i < gdbmi_value_get_size (table); i++)
2327 const GDBMIValue *brkpnt;
2329 bp = g_new0 (IAnjutaDebuggerBreakpointItem, 1);
2331 brkpnt = gdbmi_value_list_get_nth (table, i);
2332 parse_breakpoint(bp, brkpnt);
2333 list = g_list_prepend (list, bp);
2338 /* Call callback in all case (useful for enable that doesn't return
2339 * anything */
2340 if (callback != NULL)
2342 list = g_list_reverse (list);
2343 callback (list, user_data, error);
2346 g_list_foreach (list, (GFunc)g_free, NULL);
2347 g_list_free (list);
2350 void
2351 debugger_list_breakpoint (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2353 DEBUG_PRINT ("In function: debugger_list_breakpoint()");
2355 g_return_if_fail (IS_DEBUGGER (debugger));
2357 debugger_queue_command (debugger, "-break-list", FALSE, FALSE, debugger_list_breakpoint_finish, callback, user_data);
2360 static void
2361 debugger_print_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2363 gchar *ptr = NULL;
2364 gchar *tmp;
2365 GList *list, *node;
2366 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2367 gpointer user_data = debugger->priv->current_cmd.user_data;
2370 list = gdb_util_remove_blank_lines (cli_results);
2371 if (g_list_length (list) < 1)
2373 tmp = NULL;
2375 else
2377 tmp = strchr ((gchar *) list->data, '=');
2379 if (tmp != NULL)
2381 ptr = g_strdup (tmp);
2382 for (node = list ? list->next : NULL; node != NULL; node = node->next)
2384 tmp = ptr;
2385 ptr = g_strconcat (tmp, (gchar *) node->data, NULL);
2386 g_free (tmp);
2390 callback (ptr, user_data, NULL);
2391 g_free (ptr);
2394 void
2395 debugger_print (Debugger *debugger, const gchar* variable, IAnjutaDebuggerCallback callback, gpointer user_data)
2397 gchar *buff;
2399 DEBUG_PRINT ("In function: debugger_print()");
2401 g_return_if_fail (IS_DEBUGGER (debugger));
2403 buff = g_strdup_printf ("print %s", variable);
2404 debugger_queue_command (debugger, buff, TRUE, FALSE, debugger_print_finish, callback, user_data);
2405 g_free (buff);
2408 static void
2409 debugger_evaluate_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2412 const GDBMIValue *value = NULL;
2413 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2414 gpointer user_data = debugger->priv->current_cmd.user_data;
2416 if (mi_results)
2417 value = gdbmi_value_hash_lookup (mi_results, "value");
2419 /* Call user function */
2420 if (callback != NULL)
2421 callback (value == NULL ? "?" : (char *)gdbmi_value_literal_get (value), user_data, NULL);
2424 void
2425 debugger_evaluate (Debugger *debugger, const gchar* name, IAnjutaDebuggerCallback callback, gpointer user_data)
2427 gchar *buff;
2428 DEBUG_PRINT ("In function: debugger_add_watch()");
2430 g_return_if_fail (IS_DEBUGGER (debugger));
2432 buff = g_strdup_printf ("-data-evaluate-expression %s", name);
2433 debugger_queue_command (debugger, buff, TRUE, FALSE, debugger_evaluate_finish, callback, user_data);
2434 g_free (buff);
2437 static void
2438 debugger_list_local_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2441 const GDBMIValue *local, *var, *frame, *args, *stack;
2442 const gchar * name;
2443 GList* list;
2444 guint i;
2445 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2446 gpointer user_data = debugger->priv->current_cmd.user_data;
2449 list = NULL;
2450 /* Add arguments */
2451 stack = gdbmi_value_hash_lookup (mi_results, "stack-args");
2452 if (stack)
2454 frame = gdbmi_value_list_get_nth (stack, 0);
2455 if (frame)
2457 args = gdbmi_value_hash_lookup (frame, "args");
2458 if (args)
2460 for (i = 0; i < gdbmi_value_get_size (args); i++)
2462 var = gdbmi_value_list_get_nth (args, i);
2463 if (var)
2465 name = gdbmi_value_literal_get (var);
2466 list = g_list_prepend (list, (gchar *)name);
2474 /* List local variables */
2475 local = gdbmi_value_hash_lookup (mi_results, "locals");
2476 if (local)
2478 for (i = 0; i < gdbmi_value_get_size (local); i++)
2480 var = gdbmi_value_list_get_nth (local, i);
2481 if (var)
2483 name = gdbmi_value_literal_get (var);
2484 list = g_list_prepend (list, (gchar *)name);
2488 list = g_list_reverse (list);
2489 callback (list, user_data, NULL);
2490 g_list_free (list);
2493 void
2494 debugger_list_local (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2496 gchar *buff;
2497 DEBUG_PRINT ("In function: debugger_list_local()");
2499 g_return_if_fail (IS_DEBUGGER (debugger));
2501 buff = g_strdup_printf("-stack-list-arguments 0 %d %d", debugger->priv->current_frame, debugger->priv->current_frame);
2502 debugger_queue_command (debugger, buff, TRUE, TRUE, NULL, NULL, NULL);
2503 g_free (buff);
2504 debugger_queue_command (debugger, "-stack-list-locals 0", TRUE, FALSE, debugger_list_local_finish, callback, user_data);
2507 static void
2508 debugger_list_argument_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2511 const GDBMIValue *frame, *var, *args, *stack;
2512 const gchar * name;
2513 GList* list;
2514 guint i;
2515 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2516 gpointer user_data = debugger->priv->current_cmd.user_data;
2519 list = NULL;
2520 args = NULL;
2521 stack = gdbmi_value_hash_lookup (mi_results, "stack-args");
2522 if (stack)
2524 frame = gdbmi_value_list_get_nth (stack, 0);
2525 if (frame)
2527 args = gdbmi_value_hash_lookup (frame, "args");
2531 if (args)
2533 for (i = 0; i < gdbmi_value_get_size (args); i++)
2535 var = gdbmi_value_list_get_nth (args, i);
2536 if (var)
2538 name = gdbmi_value_literal_get (var);
2539 list = g_list_prepend (list, (gchar *)name);
2543 list = g_list_reverse (list);
2544 callback (list, user_data, NULL);
2545 g_list_free (list);
2548 void
2549 debugger_list_argument (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2551 gchar *buff;
2553 DEBUG_PRINT ("In function: debugger_list_argument()");
2555 g_return_if_fail (IS_DEBUGGER (debugger));
2557 buff = g_strdup_printf("-stack-list-arguments 0 %d %d", debugger->priv->current_frame, debugger->priv->current_frame);
2558 debugger_queue_command (debugger, buff, TRUE, FALSE, debugger_list_argument_finish, callback, user_data);
2559 g_free (buff);
2562 static void
2563 debugger_info_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2566 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2567 gpointer user_data = debugger->priv->current_cmd.user_data;
2569 if (callback != NULL)
2570 callback ((GList *)cli_results, user_data, NULL);
2573 void
2574 debugger_info_frame (Debugger *debugger, guint frame, IAnjutaDebuggerCallback callback, gpointer user_data)
2576 gchar *buff;
2578 DEBUG_PRINT ("In function: debugger_info_frame()");
2580 g_return_if_fail (IS_DEBUGGER (debugger));
2582 if (frame == 0)
2584 buff = g_strdup_printf("info frame");
2586 else
2588 buff = g_strdup_printf ("info frame %d", frame);
2590 debugger_queue_command (debugger, buff, TRUE, FALSE, (DebuggerParserFunc)debugger_info_finish, callback, user_data);
2591 g_free (buff);
2594 void
2595 debugger_info_signal (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2597 DEBUG_PRINT ("In function: debugger_info_signal()");
2599 g_return_if_fail (IS_DEBUGGER (debugger));
2601 debugger_queue_command (debugger, "info signals", TRUE, FALSE, (DebuggerParserFunc)debugger_info_finish, callback, user_data);
2604 void
2605 debugger_info_sharedlib (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2607 gchar *buff;
2609 DEBUG_PRINT ("In function: debugger_info_sharedlib()");
2611 g_return_if_fail (IS_DEBUGGER (debugger));
2613 buff = g_strdup_printf ("info sharedlib");
2614 debugger_queue_command (debugger, buff, TRUE, FALSE, (DebuggerParserFunc)debugger_info_finish, callback, user_data); g_free (buff);
2617 void
2618 debugger_info_args (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2620 DEBUG_PRINT ("In function: debugger_info_args()");
2622 g_return_if_fail (IS_DEBUGGER (debugger));
2624 debugger_queue_command (debugger, "info args", TRUE, FALSE, (DebuggerParserFunc)debugger_info_finish, callback, user_data);
2627 void
2628 debugger_info_target (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2630 DEBUG_PRINT ("In function: debugger_info_target()");
2632 g_return_if_fail (IS_DEBUGGER (debugger));
2634 debugger_queue_command (debugger, "info target", TRUE, FALSE, (DebuggerParserFunc)debugger_info_finish, callback, user_data);
2637 void
2638 debugger_info_program (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2640 DEBUG_PRINT ("In function: debugger_info_program()");
2642 g_return_if_fail (IS_DEBUGGER (debugger));
2644 debugger_queue_command (debugger, "info program", TRUE, FALSE, (DebuggerParserFunc)debugger_info_finish, callback, user_data);
2647 void
2648 debugger_info_udot (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2650 DEBUG_PRINT ("In function: debugger_info_udot()");
2652 g_return_if_fail (IS_DEBUGGER (debugger));
2654 debugger_queue_command (debugger, "info udot", TRUE, FALSE, (DebuggerParserFunc)debugger_info_finish, callback, user_data);
2657 void
2658 debugger_info_variables (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
2660 DEBUG_PRINT ("In function: debugger_info_variables()");
2662 g_return_if_fail (IS_DEBUGGER (debugger));
2664 debugger_queue_command (debugger, "info variables", TRUE, FALSE, (DebuggerParserFunc)debugger_info_finish, callback, user_data);
2667 static void
2668 debugger_read_memory_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2671 const GDBMIValue *literal;
2672 const GDBMIValue *mem;
2673 const gchar *value;
2674 gchar *data;
2675 gchar *ptr;
2676 gulong address;
2677 guint len;
2678 guint i;
2679 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2680 gpointer user_data = debugger->priv->current_cmd.user_data;
2681 IAnjutaDebuggerMemoryBlock read = {0,};
2683 literal = gdbmi_value_hash_lookup (mi_results, "total-bytes");
2684 if (literal)
2686 guint size;
2688 len = strtoul (gdbmi_value_literal_get (literal), NULL, 10);
2689 data = g_new (gchar, len * 2);
2690 memset (data + len, 0, len);
2692 literal = gdbmi_value_hash_lookup (mi_results, "addr");
2693 address = strtoul (gdbmi_value_literal_get (literal), NULL, 0);
2695 ptr = data;
2696 size = 0;
2697 mem = gdbmi_value_hash_lookup (mi_results, "memory");
2698 if (mem)
2700 mem = gdbmi_value_list_get_nth (mem, 0);
2701 if (mem)
2703 mem = gdbmi_value_hash_lookup (mem, "data");
2704 if (mem)
2706 size = gdbmi_value_get_size (mem);
2711 if (size < len) len = size;
2712 for (i = 0; i < len; i++)
2714 literal = gdbmi_value_list_get_nth (mem, i);
2715 if (literal)
2717 gchar *endptr;
2718 value = gdbmi_value_literal_get (literal);
2719 *ptr = strtoul (value, &endptr, 16);
2720 if ((*value != '\0') && (*endptr == '\0'))
2722 /* valid data */
2723 ptr[len] = 1;
2725 ptr++;
2728 read.address = address;
2729 read.length = len;
2730 read.data = data;
2731 callback (&read, user_data, NULL);
2733 g_free (data);
2735 else
2737 callback (NULL, user_data, NULL);
2741 void
2742 debugger_inspect_memory (Debugger *debugger, gulong address, guint length, IAnjutaDebuggerCallback callback, gpointer user_data)
2744 gchar *buff;
2746 DEBUG_PRINT ("In function: debugger_inspect_memory()");
2748 g_return_if_fail (IS_DEBUGGER (debugger));
2750 buff = g_strdup_printf ("-data-read-memory 0x%lx x 1 1 %d", address, length);
2751 debugger_queue_command (debugger, buff, FALSE, FALSE, debugger_read_memory_finish, callback, user_data);
2752 g_free (buff);
2755 static void
2756 debugger_disassemble_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2759 const GDBMIValue *literal;
2760 const GDBMIValue *line;
2761 const GDBMIValue *mem;
2762 const gchar *value;
2763 guint i;
2764 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2765 gpointer user_data = debugger->priv->current_cmd.user_data;
2766 IAnjutaDebuggerInstructionDisassembly *read = NULL;
2768 if (error != NULL)
2770 /* Command fail */
2771 callback (NULL, user_data, error);
2773 return;
2777 mem = gdbmi_value_hash_lookup (mi_results, "asm_insns");
2778 if (mem)
2780 guint size;
2782 size = gdbmi_value_get_size (mem);
2783 read = (IAnjutaDebuggerInstructionDisassembly *)g_malloc0(sizeof (IAnjutaDebuggerInstructionDisassembly) + sizeof(IAnjutaDebuggerInstructionALine) * size);
2784 read->size = size;
2786 for (i = 0; i < size; i++)
2788 line = gdbmi_value_list_get_nth (mem, i);
2789 if (line)
2791 /* Get address */
2792 literal = gdbmi_value_hash_lookup (line, "address");
2793 if (literal)
2795 value = gdbmi_value_literal_get (literal);
2796 read->data[i].address = strtoul (value, NULL, 0);
2799 /* Get label if one exist */
2800 literal = gdbmi_value_hash_lookup (line, "offset");
2801 if (literal)
2803 value = gdbmi_value_literal_get (literal);
2804 if ((value != NULL) && (strtoul (value, NULL, 0) == 0))
2806 literal = gdbmi_value_hash_lookup (line, "func-name");
2807 if (literal)
2809 read->data[i].label = gdbmi_value_literal_get (literal);
2816 /* Get disassembly line */
2817 literal = gdbmi_value_hash_lookup (line, "inst");
2818 if (literal)
2820 read->data[i].text = gdbmi_value_literal_get (literal);
2825 /* Remove last line to mark end */
2826 read->data[i - 1].text = NULL;
2828 callback (read, user_data, NULL);
2830 g_free (read);
2832 else
2834 callback (NULL, user_data, NULL);
2838 void
2839 debugger_disassemble (Debugger *debugger, gulong address, guint length, IAnjutaDebuggerCallback callback, gpointer user_data)
2841 gchar *buff;
2842 gulong end;
2844 DEBUG_PRINT ("In function: debugger_disassemble()");
2846 g_return_if_fail (IS_DEBUGGER (debugger));
2849 /* Handle overflow */
2850 end = (address + length < address) ? G_MAXULONG : address + length;
2851 buff = g_strdup_printf ("-data-disassemble -s 0x%lx -e 0x%lx -- 0", address, end);
2852 debugger_queue_command (debugger, buff, FALSE, FALSE, debugger_disassemble_finish, callback, user_data);
2853 g_free (buff);
2856 static void
2857 parse_frame (IAnjutaDebuggerFrame *frame, const GDBMIValue *frame_hash)
2859 const GDBMIValue *literal;
2861 literal = gdbmi_value_hash_lookup (frame_hash, "level");
2862 if (literal)
2863 frame->level = strtoul (gdbmi_value_literal_get (literal), NULL, 10);
2864 else
2865 frame->level = 0;
2867 literal = gdbmi_value_hash_lookup (frame_hash, "fullname");
2868 if (literal == NULL)
2869 literal = gdbmi_value_hash_lookup (frame_hash, "file");
2870 if (literal)
2871 frame->file = (gchar *)gdbmi_value_literal_get (literal);
2872 else
2873 frame->file = NULL;
2875 literal = gdbmi_value_hash_lookup (frame_hash, "line");
2876 if (literal)
2877 frame->line = strtoul (gdbmi_value_literal_get (literal), NULL, 10);
2878 else
2879 frame->line = 0;
2881 literal = gdbmi_value_hash_lookup (frame_hash, "func");
2882 if (literal)
2883 frame->function = (gchar *)gdbmi_value_literal_get (literal);
2884 else
2885 frame->function = NULL;
2887 literal = gdbmi_value_hash_lookup (frame_hash, "from");
2888 if (literal)
2889 frame->library = (gchar *)gdbmi_value_literal_get (literal);
2890 else
2891 frame->library = NULL;
2893 literal = gdbmi_value_hash_lookup (frame_hash, "addr");
2894 if (literal)
2895 frame->address = strtoul (gdbmi_value_literal_get (literal), NULL, 16);
2896 else
2897 frame->address = 0;
2902 static void
2903 add_frame (const GDBMIValue *frame_hash, GdbGListPacket* pack)
2905 IAnjutaDebuggerFrame* frame;
2907 frame = g_new0 (IAnjutaDebuggerFrame, 1);
2908 pack->list = g_list_prepend (pack->list, frame);
2910 frame->thread = pack->tag;
2911 parse_frame (frame, frame_hash);
2914 static void
2915 set_func_args (const GDBMIValue *frame_hash, GList** node)
2917 const gchar *level;
2918 const GDBMIValue *literal, *args_list, *arg_hash;
2919 gint i;
2920 GString *args_str;
2921 IAnjutaDebuggerFrame* frame;
2923 literal = gdbmi_value_hash_lookup (frame_hash, "level");
2924 if (!literal)
2925 return;
2927 level = gdbmi_value_literal_get (literal);
2928 if (!level)
2929 return;
2931 frame = (IAnjutaDebuggerFrame *)(*node)->data;
2933 args_list = gdbmi_value_hash_lookup (frame_hash, "args");
2934 if (args_list)
2936 args_str = g_string_new ("(");
2937 for (i = 0; i < gdbmi_value_get_size (args_list); i++)
2939 const gchar *name, *value;
2941 arg_hash = gdbmi_value_list_get_nth (args_list, i);
2942 if (!arg_hash)
2943 continue;
2945 literal = gdbmi_value_hash_lookup (arg_hash, "name");
2946 if (!literal)
2947 continue;
2948 name = gdbmi_value_literal_get (literal);
2949 if (!name)
2950 continue;
2952 literal = gdbmi_value_hash_lookup (arg_hash, "value");
2953 if (!literal)
2954 continue;
2955 value = gdbmi_value_literal_get (literal);
2956 if (!value)
2957 continue;
2958 args_str = g_string_append (args_str, name);
2959 args_str = g_string_append (args_str, "=");
2960 args_str = g_string_append (args_str, value);
2961 if (i < (gdbmi_value_get_size (args_list) - 1))
2962 args_str = g_string_append (args_str, ", ");
2964 args_str = g_string_append (args_str, ")");
2965 frame->args = args_str->str;
2966 g_string_free (args_str, FALSE);
2968 *node = g_list_next (*node);
2971 static void
2972 debugger_stack_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
2975 GdbGListPacket pack = {NULL, 0};
2976 GList* node;
2977 const GDBMIValue *stack_list;
2978 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
2979 gpointer user_data = debugger->priv->current_cmd.user_data;
2981 if (!mi_results)
2982 return;
2984 stack_list = gdbmi_value_hash_lookup (mi_results, "stack");
2985 if (stack_list)
2987 pack.tag = debugger->priv->current_thread;
2988 gdbmi_value_foreach (stack_list, (GFunc)add_frame, &pack);
2991 if (pack.list)
2993 pack.list = g_list_reverse (pack.list);
2994 node = g_list_first (pack.list);
2995 stack_list = gdbmi_value_hash_lookup (mi_results, "stack-args");
2996 if (stack_list)
2997 gdbmi_value_foreach (stack_list, (GFunc)set_func_args, &node);
2999 // Call call back function
3000 if (callback != NULL)
3001 callback (pack.list, user_data, NULL);
3003 // Free data
3004 for (node = g_list_first (pack.list); node != NULL; node = g_list_next (node))
3006 g_free ((gchar *)((IAnjutaDebuggerFrame *)node->data)->args);
3007 g_free (node->data);
3010 g_list_free (pack.list);
3012 else
3014 // Call call back function
3015 if (callback != NULL)
3016 callback (NULL, user_data, NULL);
3020 void
3021 debugger_list_frame (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
3023 DEBUG_PRINT ("In function: debugger_list_frame()");
3025 g_return_if_fail (IS_DEBUGGER (debugger));
3027 debugger_queue_command (debugger, "-stack-list-frames", TRUE, TRUE, NULL, NULL, NULL);
3028 debugger_queue_command (debugger, "-stack-list-arguments 1", TRUE, FALSE, debugger_stack_finish, callback, user_data);
3031 /* Thread functions
3032 *---------------------------------------------------------------------------*/
3034 static void
3035 debugger_set_thread_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
3037 const GDBMIValue *literal;
3038 guint id;
3040 if (mi_results == NULL) return;
3042 literal = gdbmi_value_hash_lookup (mi_results, "new-thread-id");
3043 if (literal == NULL) return;
3045 id = strtoul (gdbmi_value_literal_get (literal), NULL, 10);
3046 if (id == 0) return;
3048 debugger->priv->current_thread = id;
3049 g_signal_emit_by_name (debugger->priv->instance, "frame-changed", 0, debugger->priv->current_thread);
3051 return;
3054 void
3055 debugger_set_thread (Debugger *debugger, gint thread)
3057 gchar *buff;
3059 DEBUG_PRINT ("In function: debugger_set_thread()");
3061 g_return_if_fail (IS_DEBUGGER (debugger));
3063 buff = g_strdup_printf ("-thread-select %d", thread);
3065 debugger_queue_command (debugger, buff, FALSE, FALSE, (DebuggerParserFunc)debugger_set_thread_finish, NULL, NULL);
3066 g_free (buff);
3069 static void
3070 add_thread_id (const GDBMIValue *thread_hash, GList** list)
3072 IAnjutaDebuggerFrame* frame;
3073 gint thread;
3075 thread = strtoul (gdbmi_value_literal_get (thread_hash), NULL, 10);
3076 if (thread == 0) return;
3078 frame = g_new0 (IAnjutaDebuggerFrame, 1);
3079 *list = g_list_prepend (*list, frame);
3081 frame->thread = thread;
3084 static void
3085 debugger_list_thread_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
3088 const GDBMIValue *id_list;
3089 gpointer user_data = debugger->priv->current_cmd.user_data;
3090 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
3091 GList* thread_list = NULL;
3093 for (;;)
3095 if (mi_results == NULL) break;
3097 id_list = gdbmi_value_hash_lookup (mi_results, "thread-ids");
3098 if (id_list == NULL) break;
3100 gdbmi_value_foreach (id_list, (GFunc)add_thread_id, &thread_list);
3101 thread_list = g_list_reverse (thread_list);
3102 break;
3105 if (callback != NULL)
3106 callback (thread_list, user_data, error);
3108 if (thread_list != NULL)
3110 g_list_foreach (thread_list, (GFunc)g_free, NULL);
3111 g_list_free (thread_list);
3115 void
3116 debugger_list_thread (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
3118 DEBUG_PRINT ("In function: debugger_list_thread()");
3120 g_return_if_fail (IS_DEBUGGER (debugger));
3122 debugger_queue_command (debugger, "-thread-list-ids", TRUE, FALSE, debugger_list_thread_finish, callback, user_data);
3125 static void
3126 debugger_info_set_thread_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
3128 const GDBMIValue *literal;
3129 guint id;
3131 if (mi_results == NULL) return;
3133 literal = gdbmi_value_hash_lookup (mi_results, "new-thread-id");
3134 if (literal == NULL) return;
3136 id = strtoul (gdbmi_value_literal_get (literal), NULL, 10);
3137 if (id == 0) return;
3139 debugger->priv->current_thread = id;
3140 /* Do not emit a signal notification as the current thread will
3141 * be restored when needed */
3143 return;
3146 static void
3147 debugger_info_thread_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
3149 const GDBMIValue *frame;
3150 IAnjutaDebuggerFrame top_frame;
3151 IAnjutaDebuggerFrame *top;
3152 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
3153 gpointer user_data = debugger->priv->current_cmd.user_data;
3155 for (top = NULL;;)
3157 DEBUG_PRINT("look for stack %p", mi_results);
3158 if (mi_results == NULL) break;
3160 frame = gdbmi_value_hash_lookup (mi_results, "stack");
3161 if (frame == NULL) break;
3163 DEBUG_PRINT("get stack");
3165 frame = gdbmi_value_list_get_nth (frame, 0);
3166 if (frame == NULL) break;
3168 DEBUG_PRINT("get nth element");
3170 /*frame = gdbmi_value_hash_lookup (frame, "frame");
3171 DEBUG_PRINT("get frame %p", frame);
3172 if (frame == NULL) break;*/
3174 DEBUG_PRINT("get frame");
3176 top = &top_frame;
3177 top->thread = debugger->priv->current_thread;
3179 parse_frame (top, frame);
3180 break;
3183 if (callback != NULL)
3184 callback (top, user_data, error);
3186 return;
3189 void
3190 debugger_info_thread (Debugger *debugger, gint thread, IAnjutaDebuggerCallback callback, gpointer user_data)
3192 gchar *buff;
3193 guint orig_thread;
3195 DEBUG_PRINT ("In function: debugger_info_thread()");
3197 g_return_if_fail (IS_DEBUGGER (debugger));
3199 orig_thread = debugger->priv->current_thread;
3200 buff = g_strdup_printf ("-thread-select %d", thread);
3201 debugger_queue_command (debugger, buff, FALSE, FALSE, (DebuggerParserFunc)debugger_info_set_thread_finish, NULL, NULL);
3202 g_free (buff);
3203 debugger_queue_command (debugger, "-stack-list-frames 0 0", FALSE, FALSE, (DebuggerParserFunc)debugger_info_thread_finish, callback, user_data);
3205 buff = g_strdup_printf ("-thread-select %d", orig_thread);
3206 debugger_queue_command (debugger, buff, FALSE, FALSE, (DebuggerParserFunc)debugger_info_set_thread_finish, NULL, NULL);
3207 g_free (buff);
3210 static void
3211 add_register_name (const GDBMIValue *reg_literal, GList** list)
3213 IAnjutaDebuggerRegisterData* reg;
3214 GList *prev = *list;
3216 reg = g_new0 (IAnjutaDebuggerRegisterData, 1);
3217 *list = g_list_prepend (prev, reg);
3218 reg->name = (gchar *)gdbmi_value_literal_get (reg_literal);
3219 reg->num = prev == NULL ? 0 : ((IAnjutaDebuggerRegisterData *)prev->data)->num + 1;
3222 static void
3223 add_register_value (const GDBMIValue *reg_hash, GList** list)
3225 const GDBMIValue *literal;
3226 const gchar *val;
3227 IAnjutaDebuggerRegisterData* reg;
3228 guint num;
3229 GList* prev = *list;
3231 literal = gdbmi_value_hash_lookup (reg_hash, "number");
3232 if (!literal)
3233 return;
3234 val = gdbmi_value_literal_get (literal);
3235 num = strtoul (val, NULL, 10);
3237 literal = gdbmi_value_hash_lookup (reg_hash, "value");
3238 if (!literal)
3239 return;
3241 reg = g_new0 (IAnjutaDebuggerRegisterData, 1);
3242 *list = g_list_prepend (prev, reg);
3243 reg->num = num;
3244 reg->value = (gchar *)gdbmi_value_literal_get (literal);
3247 static void
3248 debugger_register_name_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
3251 GList* list = NULL;
3252 GList* node;
3253 const GDBMIValue *reg_list;
3254 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
3255 gpointer user_data = debugger->priv->current_cmd.user_data;
3257 if (!mi_results)
3258 return;
3260 reg_list = gdbmi_value_hash_lookup (mi_results, "register-names");
3261 if (reg_list)
3262 gdbmi_value_foreach (reg_list, (GFunc)add_register_name, &list);
3263 list = g_list_reverse (list);
3265 // Call call back function
3266 if (callback != NULL)
3267 callback (list, user_data, NULL);
3269 // Free data
3270 for (node = g_list_first (list); node != NULL; node = g_list_next (node))
3272 g_free (node->data);
3274 g_list_free (list);
3277 static void
3278 debugger_register_value_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
3281 GList* list = NULL;
3282 GList* node;
3283 const GDBMIValue *reg_list;
3284 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
3285 gpointer user_data = debugger->priv->current_cmd.user_data;
3287 if (!mi_results)
3288 return;
3290 reg_list = gdbmi_value_hash_lookup (mi_results, "register-values");
3291 if (reg_list)
3292 gdbmi_value_foreach (reg_list, (GFunc)add_register_value, &list);
3293 list = g_list_reverse (list);
3295 // Call call back function
3296 if (callback != NULL)
3297 callback (list, user_data, NULL);
3299 // Free data
3300 for (node = g_list_first (list); node != NULL; node = g_list_next (node))
3302 g_free (node->data);
3304 g_list_free (list);
3307 void
3308 debugger_list_register (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
3310 DEBUG_PRINT ("In function: debugger_list_register()");
3312 g_return_if_fail (IS_DEBUGGER (debugger));
3314 debugger_queue_command (debugger, "-data-list-register-names", TRUE, FALSE, debugger_register_name_finish, callback, user_data);
3317 void
3318 debugger_update_register (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
3320 DEBUG_PRINT ("In function: debugger_update_register()");
3322 g_return_if_fail (IS_DEBUGGER (debugger));
3324 debugger_queue_command (debugger, "-data-list-register-values r", TRUE, FALSE, (DebuggerParserFunc)debugger_register_value_finish, callback, user_data);
3327 void
3328 debugger_write_register (Debugger *debugger, const gchar *name, const gchar *value)
3330 gchar *buf;
3332 DEBUG_PRINT ("In function: debugger_write_register()");
3334 g_return_if_fail (IS_DEBUGGER (debugger));
3336 buf = g_strdup_printf ("-data-evaluate-expression \"$%s=%s\"", name, value);
3337 debugger_queue_command (debugger, buf, TRUE, FALSE, NULL, NULL, NULL);
3338 g_free (buf);
3341 static void
3342 debugger_set_frame_finish (Debugger *debugger, const GDBMIValue *mi_results, const GList *cli_results, GError *error)
3345 guint frame = (guint)debugger->priv->current_cmd.user_data;
3346 debugger->priv->current_frame = frame;
3348 g_signal_emit_by_name (debugger->priv->instance, "frame-changed", frame, debugger->priv->current_thread);
3351 void
3352 debugger_set_frame (Debugger *debugger, guint frame)
3354 gchar *buff;
3356 DEBUG_PRINT ("In function: debugger_set_frame()");
3358 g_return_if_fail (IS_DEBUGGER (debugger));
3360 buff = g_strdup_printf ("-stack-select-frame %d", frame);
3362 debugger_queue_command (debugger, buff, FALSE, FALSE, (DebuggerParserFunc)debugger_set_frame_finish, NULL, (gpointer)frame);
3363 g_free (buff);
3366 void
3367 debugger_set_log (Debugger *debugger, IAnjutaMessageView *log)
3369 debugger->priv->log = log;
3372 /* Variable objects functions
3373 *---------------------------------------------------------------------------*/
3375 void
3376 debugger_delete_variable (Debugger *debugger, const gchar* name)
3378 gchar *buff;
3380 DEBUG_PRINT ("In function: delete_variable()");
3382 g_return_if_fail (IS_DEBUGGER (debugger));
3384 buff = g_strdup_printf ("-var-delete %s", name);
3385 debugger_queue_command (debugger, buff, FALSE, FALSE, NULL, NULL, NULL);
3386 g_free (buff);
3389 static void
3390 gdb_var_evaluate_expression (Debugger *debugger,
3391 const GDBMIValue *mi_results, const GList *cli_results,
3392 GError *error)
3394 const gchar *value = NULL;
3395 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
3396 gpointer user_data = debugger->priv->current_cmd.user_data;
3398 if (mi_results != NULL)
3400 const GDBMIValue *const gdbmi_value =
3401 gdbmi_value_hash_lookup (mi_results, "value");
3403 if (gdbmi_value != NULL)
3404 value = gdbmi_value_literal_get (gdbmi_value);
3406 callback ((const gpointer)value, user_data, NULL);
3409 void
3410 debugger_evaluate_variable (Debugger *debugger, const gchar* name, IAnjutaDebuggerCallback callback, gpointer user_data)
3412 gchar *buff;
3414 DEBUG_PRINT ("In function: evaluate_variable()");
3416 g_return_if_fail (IS_DEBUGGER (debugger));
3418 buff = g_strdup_printf ("-var-evaluate-expression %s", name);
3419 debugger_queue_command (debugger, buff, FALSE, FALSE, gdb_var_evaluate_expression, callback, user_data);
3420 g_free (buff);
3423 void
3424 debugger_assign_variable (Debugger *debugger, const gchar* name, const gchar *value)
3426 gchar *buff;
3428 DEBUG_PRINT ("In function: assign_variable()");
3430 g_return_if_fail (IS_DEBUGGER (debugger));
3432 buff = g_strdup_printf ("-var-assign %s %s", name, value);
3433 debugger_queue_command (debugger, buff, FALSE, FALSE, NULL, NULL, NULL);
3434 g_free (buff);
3437 static void
3438 gdb_var_list_children (Debugger *debugger,
3439 const GDBMIValue *mi_results, const GList *cli_results,
3440 GError *error)
3442 GList* list = NULL;
3443 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
3444 gpointer user_data = debugger->priv->current_cmd.user_data;
3446 if (mi_results != NULL)
3448 const GDBMIValue *literal;
3449 const GDBMIValue *children;
3450 glong numchild = 0;
3451 glong i = 0;
3453 literal = gdbmi_value_hash_lookup (mi_results, "numchild");
3455 if (literal)
3456 numchild = strtoul(gdbmi_value_literal_get (literal), NULL, 0);
3457 children = gdbmi_value_hash_lookup (mi_results, "children");
3459 for(i = 0 ; i < numchild; ++i)
3461 const GDBMIValue *const gdbmi_chl =
3462 gdbmi_value_list_get_nth (children, i);
3463 IAnjutaDebuggerVariableObject *var;
3465 var = g_new0 (IAnjutaDebuggerVariableObject, 1);
3467 literal = gdbmi_value_hash_lookup (gdbmi_chl, "name");
3468 if (literal)
3469 var->name = (gchar *)gdbmi_value_literal_get (literal);
3471 literal = gdbmi_value_hash_lookup (gdbmi_chl, "exp");
3472 if (literal)
3473 var->expression = (gchar *)gdbmi_value_literal_get(literal);
3475 literal = gdbmi_value_hash_lookup (gdbmi_chl, "type");
3476 if (literal)
3477 var->type = (gchar *)gdbmi_value_literal_get(literal);
3479 literal = gdbmi_value_hash_lookup (gdbmi_chl, "value");
3480 if (literal)
3481 var->value = (gchar *)gdbmi_value_literal_get(literal);
3483 literal = gdbmi_value_hash_lookup (gdbmi_chl, "numchild");
3484 if (literal)
3485 var->children = strtoul(gdbmi_value_literal_get(literal), NULL, 10);
3487 list = g_list_prepend (list, var);
3489 list = g_list_reverse (list);
3492 callback (list, user_data, NULL);
3493 g_list_foreach (list, (GFunc)g_free, NULL);
3494 g_list_free (list);
3497 void debugger_list_variable_children (Debugger *debugger, const gchar* name, IAnjutaDebuggerCallback callback, gpointer user_data)
3499 gchar *buff;
3501 DEBUG_PRINT ("In function: list_variable_children()");
3503 g_return_if_fail (IS_DEBUGGER (debugger));
3505 buff = g_strdup_printf ("-var-list-children --all-values %s", name);
3506 debugger_queue_command (debugger, buff, FALSE, FALSE, gdb_var_list_children, callback, user_data);
3507 g_free (buff);
3510 static void
3511 gdb_var_create (Debugger *debugger,
3512 const GDBMIValue *mi_results, const GList *cli_results,
3513 GError *error)
3515 const GDBMIValue * result;
3516 IAnjutaDebuggerVariableObject var = {0,};
3517 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
3518 gpointer user_data = debugger->priv->current_cmd.user_data;
3520 if ((error == NULL) && (mi_results != NULL))
3522 result = gdbmi_value_hash_lookup (mi_results, "name");
3523 var.name = (gchar *)gdbmi_value_literal_get(result);
3525 result = gdbmi_value_hash_lookup (mi_results, "type");
3526 var.type = (gchar *)gdbmi_value_literal_get (result);
3528 result = gdbmi_value_hash_lookup (mi_results, "numchild");
3529 var.children = strtoul (gdbmi_value_literal_get(result), NULL, 10);
3531 callback (&var, user_data, error);
3535 void debugger_create_variable (Debugger *debugger, const gchar* name, IAnjutaDebuggerCallback callback, gpointer user_data)
3537 gchar *buff;
3539 DEBUG_PRINT ("In function: create_variable()");
3541 g_return_if_fail (IS_DEBUGGER (debugger));
3543 buff = g_strdup_printf ("-var-create - * %s", name);
3544 debugger_queue_command (debugger, buff, FALSE, FALSE, gdb_var_create, callback, user_data);
3545 g_free (buff);
3548 static void
3549 gdb_var_update (Debugger *debugger,
3550 const GDBMIValue *mi_results, const GList *cli_results,
3551 GError *error)
3553 GList* list = NULL;
3554 glong idx = 0, changed_count = 0;
3555 const GDBMIValue *const gdbmi_changelist =
3556 gdbmi_value_hash_lookup (mi_results, "changelist");
3557 IAnjutaDebuggerCallback callback = debugger->priv->current_cmd.callback;
3558 gpointer user_data = debugger->priv->current_cmd.user_data;
3561 changed_count = gdbmi_value_get_size (gdbmi_changelist);
3562 for(; idx<changed_count; ++idx)
3564 const GDBMIValue *const gdbmi_change =
3565 gdbmi_value_list_get_nth (gdbmi_changelist, idx);
3566 const GDBMIValue *gdbmi_val =
3567 gdbmi_value_hash_lookup (gdbmi_change, "in_scope");
3568 IAnjutaDebuggerVariableObject *var;
3570 if(0 != strcmp(gdbmi_value_literal_get(gdbmi_val), "false"))
3572 gdbmi_val = gdbmi_value_hash_lookup (gdbmi_change, "name");
3573 var = g_new0 (IAnjutaDebuggerVariableObject, 1);
3574 var->changed = TRUE;
3575 var->name = (gchar *)gdbmi_value_literal_get(gdbmi_val);
3577 list = g_list_prepend (list, var);
3580 list = g_list_reverse (list);
3581 callback (list, user_data, NULL);
3582 g_list_foreach (list, (GFunc)g_free, NULL);
3583 g_list_free (list);
3586 void debugger_update_variable (Debugger *debugger, IAnjutaDebuggerCallback callback, gpointer user_data)
3588 DEBUG_PRINT ("In function: update_variable()");
3590 g_return_if_fail (IS_DEBUGGER (debugger));
3592 debugger_queue_command (debugger, "-var-update *", FALSE, FALSE, gdb_var_update, callback, user_data);
3595 GType
3596 debugger_get_type (void)
3598 static GType obj_type = 0;
3600 if (!obj_type)
3602 static const GTypeInfo obj_info =
3604 sizeof (DebuggerClass),
3605 (GBaseInitFunc) NULL,
3606 (GBaseFinalizeFunc) NULL,
3607 (GClassInitFunc) debugger_class_init,
3608 (GClassFinalizeFunc) NULL,
3609 NULL, /* class_data */
3610 sizeof (Debugger),
3611 0, /* n_preallocs */
3612 (GInstanceInitFunc) debugger_instance_init,
3613 NULL /* value_table */
3615 obj_type = g_type_register_static (G_TYPE_OBJECT,
3616 "Debugger", &obj_info, 0);
3618 return obj_type;
3621 static void
3622 debugger_dispose (GObject *obj)
3624 Debugger *debugger = DEBUGGER (obj);
3626 DEBUG_PRINT ("In function: debugger_shutdown()");
3628 /* Do not emit signal when the debugger is destroyed */
3629 debugger->priv->instance = NULL;
3630 debugger_abort (debugger);
3632 /* Good Bye message */
3633 if (debugger->priv->output_callback)
3635 debugger->priv->output_callback (IANJUTA_DEBUGGER_OUTPUT,
3636 "Debugging session completed.\n",
3637 debugger->priv->output_user_data);
3640 if (debugger->priv->launcher)
3642 anjuta_launcher_reset (debugger->priv->launcher);
3643 g_object_unref (debugger->priv->launcher);
3644 debugger->priv->launcher = NULL;
3647 GNOME_CALL_PARENT (G_OBJECT_CLASS, dispose, (obj));
3650 static void
3651 debugger_finalize (GObject *obj)
3653 Debugger *debugger = DEBUGGER (obj);
3654 g_string_free (debugger->priv->stdo_line, TRUE);
3655 g_string_free (debugger->priv->stdo_acc, TRUE);
3656 g_string_free (debugger->priv->stde_line, TRUE);
3657 g_free (debugger->priv);
3658 GNOME_CALL_PARENT (G_OBJECT_CLASS, finalize, (obj));
3661 static void
3662 debugger_class_init (DebuggerClass * klass)
3664 GObjectClass *object_class;
3666 g_return_if_fail (klass != NULL);
3667 object_class = G_OBJECT_CLASS (klass);
3669 DEBUG_PRINT ("Initializing debugger class");
3671 parent_class = g_type_class_peek_parent (klass);
3672 object_class->dispose = debugger_dispose;
3673 object_class->finalize = debugger_finalize;
3677 #if 0 /* FIXME */
3678 void
3679 debugger_signal (const gchar *sig, gboolean show_msg)
3681 /* eg:- "SIGTERM" */
3682 gchar *buff;
3683 gchar *cmd;
3685 DEBUG_PRINT ("In function: debugger_signal()");
3687 if (debugger_is_active () == FALSE)
3688 return;
3689 if (debugger.prog_is_running == FALSE)
3690 return;
3691 if (debugger.child_pid < 1)
3693 DEBUG_PRINT ("Not sending signal - pid not known\n");
3694 return;
3697 if (show_msg)
3699 buff = g_strdup_printf (_("Sending signal %s to the process: %d"),
3700 sig, (int) debugger.child_pid);
3701 gdb_util_append_message (ANJUTA_PLUGIN (debugger.plugin), buff);
3702 g_free (buff);
3705 if (debugger_is_ready ())
3707 cmd = g_strconcat ("signal ", sig, NULL);
3708 stack_trace_set_frame (debugger.stack, 0);
3709 debugger_put_cmd_in_queqe (cmd, DB_CMD_ALL, NULL, NULL);
3710 debugger_put_cmd_in_queqe ("info program", DB_CMD_NONE,
3711 on_debugger_update_prog_status,
3712 NULL);
3713 g_free (cmd);
3714 debugger_execute_cmd_in_queqe ();
3716 else
3718 GtkWindow *parent;
3719 int status;
3721 parent = GTK_WINDOW (ANJUTA_PLUGIN (debugger.plugin)->shell);
3722 status = gdb_util_kill_process (debugger.child_pid, sig);
3723 if (status != 0 && show_msg)
3724 anjuta_util_dialog_error (parent,
3725 _("Error whilst signaling the process."));
3729 static void
3730 query_set_cmd (const gchar *cmd, gboolean state)
3732 gchar buffer[50];
3733 gchar *tmp = g_stpcpy (buffer, cmd);
3734 strcpy (tmp, state ? "on" : "off");
3735 debugger_put_cmd_in_queqe (buffer, DB_CMD_NONE, NULL, NULL);
3738 static void
3739 query_set_verbose (gboolean state)
3741 query_set_cmd ("set verbose ", state);
3744 static void
3745 query_set_print_staticmembers (gboolean state)
3747 query_set_cmd ("set print static-members ", state);
3750 static void
3751 query_set_print_pretty (gboolean state)
3753 query_set_cmd ("set print pretty ", state);
3756 void debugger_query_evaluate_expr_tip (const gchar *expr,
3757 DebuggerCLIFunc parser, gpointer data)
3759 query_set_verbose (FALSE);
3760 query_set_print_staticmembers (FALSE);
3761 gchar *printcmd = g_strconcat ("print ", expr, NULL);
3762 debugger_put_cmd_in_queqe (printcmd, DB_CMD_NONE, parser, data);
3763 query_set_verbose (TRUE);
3764 query_set_print_staticmembers (TRUE);
3765 g_free (printcmd);
3768 void
3769 debugger_query_evaluate_expression (const gchar *expr, DebuggerFunc parser,
3770 gpointer data)
3772 query_set_print_pretty (TRUE);
3773 query_set_verbose (FALSE);
3774 gchar *printcmd = g_strconcat ("print ", expr, NULL);
3775 debugger_put_cmd_in_queqe (printcmd, DB_CMD_SE_MESG | DB_CMD_SE_DIALOG,
3776 parser, data);
3777 query_set_print_pretty (FALSE);
3778 query_set_verbose (TRUE);
3779 g_free (printcmd);
3782 #endif