added global gdb breakpoint override configuration command
[fw/openocd] / src / server / gdb_server.c
index 665e3507af179a0d832504aaf71cd7516d5a8156..7be05acf34110ef4f886c0a19988eb4d9eb58de9 100644 (file)
@@ -2,6 +2,9 @@
  *   Copyright (C) 2005 by Dominic Rath                                    *
  *   Dominic.Rath@gmx.de                                                   *
  *                                                                         *
+ *   Copyright (C) 2007,2008 Ã˜yvind Harboe                                      *
+ *   oyvind.harboe@zylin.com                                               *
+ *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
  *   it under the terms of the GNU General Public License as published by  *
  *   the Free Software Foundation; either version 2 of the License, or     *
@@ -43,6 +46,9 @@
 #define _DEBUG_GDB_IO_
 #endif
 
+static int gdb_breakpoint_override;
+static enum breakpoint_type gdb_breakpoint_override_type;
+
 extern int gdb_error(connection_t *connection, int retval);
 static unsigned short gdb_port;
 static const char *DIGITS = "0123456789abcdef";
@@ -287,7 +293,6 @@ int gdb_put_packet_inner(connection_t *connection, char *buffer, int len)
         * however sometimes '-' is sent even though we've already received
         * an ACK (+) for everything we've sent off.
         */
-#ifndef _WIN32
        int gotdata;
        for (;;)
        {
@@ -299,7 +304,6 @@ int gdb_put_packet_inner(connection_t *connection, char *buffer, int len)
                        return retval;
                LOG_WARNING("Discard unexpected char %c", reply);
        }
-#endif
 #endif
 
        while (1)
@@ -413,6 +417,8 @@ int gdb_get_packet_inner(connection_t *connection, char *buffer, int *len)
                                case '$':
                                        break;
                                case '+':
+                                       /* gdb sends a dummy ack '+' at every remote connect - see remote_start_remote (remote.c)
+                                        * incase anyone tries to debug why they receive this warning every time */
                                        LOG_WARNING("acknowledgment received, but no packet pending");
                                        break;
                                case '-':
@@ -585,44 +591,50 @@ int gdb_program_handler(struct target_s *target, enum target_event event, void *
        return ERROR_OK;
 }
 
+static void gdb_frontend_halted(struct target_s *target, connection_t *connection)
+{
+       gdb_connection_t *gdb_connection = connection->priv;
+       
+       /* In the GDB protocol when we are stepping or coninuing execution,
+        * we have a lingering reply. Upon receiving a halted event
+        * when we have that lingering packet, we reply to the original
+        * step or continue packet.
+        *
+        * Executing monitor commands can bring the target in and
+        * out of the running state so we'll see lots of TARGET_EVENT_XXX
+        * that are to be ignored.
+        */
+       if (gdb_connection->frontend_state == TARGET_RUNNING)
+       {
+               char sig_reply[4];
+               int signal;
+               /* stop forwarding log packets! */
+               log_remove_callback(gdb_log_callback, connection);
+
+               if (gdb_connection->ctrl_c)
+               {
+                       signal = 0x2;
+                       gdb_connection->ctrl_c = 0;
+               }
+               else
+               {
+                       signal = gdb_last_signal(target);
+               }
+
+               snprintf(sig_reply, 4, "T%2.2x", signal);
+               gdb_put_packet(connection, sig_reply, 3);
+               gdb_connection->frontend_state = TARGET_HALTED;
+       }
+}
+
 int gdb_target_callback_event_handler(struct target_s *target, enum target_event event, void *priv)
 {
        connection_t *connection = priv;
-       gdb_connection_t *gdb_connection = connection->priv;
-       char sig_reply[4];
-       int signal;
 
        switch (event)
        {
                case TARGET_EVENT_HALTED:
-                       /* In the GDB protocol when we are stepping or coninuing execution,
-                        * we have a lingering reply. Upon receiving a halted event
-                        * when we have that lingering packet, we reply to the original
-                        * step or continue packet.
-                        *
-                        * Executing monitor commands can bring the target in and
-                        * out of the running state so we'll see lots of TARGET_EVENT_XXX
-                        * that are to be ignored.
-                        */
-                       if (gdb_connection->frontend_state == TARGET_RUNNING)
-                       {
-                               /* stop forwarding log packets! */
-                               log_remove_callback(gdb_log_callback, connection);
-
-                               if (gdb_connection->ctrl_c)
-                               {
-                                       signal = 0x2;
-                                       gdb_connection->ctrl_c = 0;
-                               }
-                               else
-                               {
-                                       signal = gdb_last_signal(target);
-                               }
-
-                               snprintf(sig_reply, 4, "T%2.2x", signal);
-                               gdb_put_packet(connection, sig_reply, 3);
-                               gdb_connection->frontend_state = TARGET_HALTED;
-                       }
+                       gdb_frontend_halted(target, connection);
                        break;
                case TARGET_EVENT_GDB_PROGRAM:
                        gdb_program_handler(target, event, connection->cmd_ctx);
@@ -659,6 +671,13 @@ int gdb_new_connection(connection_t *connection)
        /* output goes through gdb connection */
        command_set_output_handler(connection->cmd_ctx, gdb_output, connection);
 
+       /* we must remove all breakpoints registered to the target as a previous
+        * GDB session could leave dangling breakpoints if e.g. communication 
+        * timed out.
+        */
+       breakpoint_clear_target(gdb_service->target);
+       watchpoint_clear_target(gdb_service->target);
+       
        /* register callback to be informed about target events */
        target_register_event_callback(gdb_target_callback_event_handler, connection);
 
@@ -679,6 +698,12 @@ int gdb_new_connection(connection_t *connection)
         * connect. 
         */
        target_halt(gdb_service->target);
+       /* FIX!!!! could extended-remote work better here?
+        * 
+        *  wait a tiny bit for halted state or we just continue. The
+        * GDB register packet will then contain garbage 
+        */
+       target_wait_state(gdb_service->target, TARGET_HALTED, 500);
        
        /* remove the initial ACK from the incoming buffer */
        if ((retval = gdb_get_char(connection, &initial_ack)) != ERROR_OK)
@@ -1199,10 +1224,11 @@ int gdb_write_memory_binary_packet(connection_t *connection, target_t *target, c
        return ERROR_OK;
 }
 
-void gdb_step_continue_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
+int gdb_step_continue_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
 {
        int current = 0;
        u32 address = 0x0;
+       int retval=ERROR_OK;
 
        LOG_DEBUG("-");
 
@@ -1220,13 +1246,14 @@ void gdb_step_continue_packet(connection_t *connection, target_t *target, char *
        {
                LOG_DEBUG("continue");
                target_invoke_script(connection->cmd_ctx, target, "pre_resume");
-               target_resume(target, current, address, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
+               retval=target_resume(target, current, address, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
        }
        else if (packet[0] == 's')
        {
                LOG_DEBUG("step");
-               target->type->step(target, current, address, 0); /* step at current or address, don't handle breakpoints */
+               retval=target->type->step(target, current, address, 0); /* step at current or address, don't handle breakpoints */
        }
+       return retval;
 }
 
 int gdb_breakpoint_watchpoint_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
@@ -1253,6 +1280,11 @@ int gdb_breakpoint_watchpoint_packet(connection_t *connection, target_t *target,
                wp_type = WPT_READ;
        else if (type == 4) /* access watchpoint */
                wp_type = WPT_ACCESS;
+       
+       if (gdb_breakpoint_override&&((bp_type==BKPT_SOFT)||(bp_type==BKPT_SOFT)))
+       {
+               bp_type=gdb_breakpoint_override_type;
+       }
 
        if (*separator != ',')
        {
@@ -1505,7 +1537,7 @@ int gdb_query_packet(connection_t *connection, target_t *target, char *packet, i
 
                xml_printf(&retval, &buffer, &pos, &size,
                                "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-",
-                               (GDB_BUFFER_SIZE - 1), gdb_use_memory_map == 1 ? '+' : '-');
+                               (GDB_BUFFER_SIZE - 1), ((gdb_use_memory_map == 1)&&(flash_get_bank_count()>0)) ? '+' : '-');
 
                if (retval != ERROR_OK)
                {
@@ -1518,7 +1550,7 @@ int gdb_query_packet(connection_t *connection, target_t *target, char *packet, i
 
                return ERROR_OK;
        }
-       else if (strstr(packet, "qXfer:memory-map:read::"))
+       else if (strstr(packet, "qXfer:memory-map:read::")&&(flash_get_bank_count()>0))
        {
                /* We get away with only specifying flash here. Regions that are not
                 * specified are treated as if we provided no memory map(if not we
@@ -1842,6 +1874,14 @@ static void gdb_log_callback(void *priv, const char *file, int line,
 /* Do not allocate this on the stack */
 char gdb_packet_buffer[GDB_BUFFER_SIZE];
 
+static void gdb_sig_halted(connection_t *connection)
+{
+       char sig_reply[4];
+       snprintf(sig_reply, 4, "T%2.2x", 2);
+       gdb_put_packet(connection, sig_reply, 3);
+
+}
+
 int gdb_input_inner(connection_t *connection)
 {
        gdb_service_t *gdb_service = connection->service->priv;
@@ -1912,9 +1952,7 @@ int gdb_input_inner(connection_t *connection)
                                                        /* If the target isn't in the halted state, then we can't
                                                         * step/continue. This might be early setup, etc.
                                                         */
-                                                       char sig_reply[4];
-                                                       snprintf(sig_reply, 4, "T%2.2x", 2);
-                                                       gdb_put_packet(connection, sig_reply, 3);
+                                                       gdb_sig_halted(connection);
                                                } else
                                                {
                                                        /* We're running/stepping, in which case we can
@@ -1923,7 +1961,12 @@ int gdb_input_inner(connection_t *connection)
                                                        gdb_connection_t *gdb_con = connection->priv;
                                                        gdb_con->frontend_state = TARGET_RUNNING;
                                                        log_add_callback(gdb_log_callback, connection);
-                                                       gdb_step_continue_packet(connection, target, packet, packet_size);
+                                                       int retval=gdb_step_continue_packet(connection, target, packet, packet_size);
+                                                       if (retval!=ERROR_OK)
+                                                       {
+                                                               /* we'll never receive a halted condition... issue a false one.. */
+                                                               gdb_frontend_halted(target, connection); 
+                                                       }
                                                }
                                        }
                                        break;
@@ -1950,8 +1993,7 @@ int gdb_input_inner(connection_t *connection)
                                        break;
                                case 'R':
                                        /* handle extended restart packet */
-                                       /* fix?? make this configurable? */
-                                       target_process_reset(connection->cmd_ctx, RESET_HALT);
+                                       command_run_linef(connection->cmd_ctx, "ocd_gdb_restart %d", get_num_by_target(target));
                                        break;
                                default:
                                        /* ignore unkown packets */
@@ -1994,7 +2036,7 @@ int gdb_input(connection_t *connection)
        return ERROR_OK;
 }
 
-int gdb_init()
+int gdb_init(void)
 {
        gdb_service_t *gdb_service;
        target_t *target = targets;
@@ -2135,6 +2177,41 @@ int handle_gdb_report_data_abort_command(struct command_context_s *cmd_ctx, char
        return ERROR_OK;
 }
 
+/* daemon configuration command gdb_port */
+int handle_gdb_breakpoint_override_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
+{
+       if (argc == 0)
+       {
+               
+       } else if (argc==1)
+       {
+               gdb_breakpoint_override = 1;
+               if (strcmp(args[0], "hard")==0)
+               {
+                       gdb_breakpoint_override_type=BKPT_HARD;
+               } else if (strcmp(args[0], "soft")==0)
+               {
+                       gdb_breakpoint_override_type=BKPT_SOFT;
+               } else if (strcmp(args[0], "disable") == 0)
+               {
+                       gdb_breakpoint_override = 0;
+               }
+       } else
+       {
+               return ERROR_COMMAND_SYNTAX_ERROR;
+       }
+       if (gdb_breakpoint_override)
+       {
+               LOG_USER("force %s breakpoints", (gdb_breakpoint_override_type==BKPT_HARD)?"hard":"soft");
+       } else
+       {
+               LOG_USER("breakpoint type is not overriden");
+       }
+       
+       return ERROR_OK;
+}
+
+
 int gdb_register_commands(command_context_t *command_context)
 {
        register_command(command_context, NULL, "gdb_port", handle_gdb_port_command,
@@ -2147,5 +2224,10 @@ int gdb_register_commands(command_context_t *command_context)
                        COMMAND_CONFIG, "");
        register_command(command_context, NULL, "gdb_report_data_abort", handle_gdb_report_data_abort_command,
                        COMMAND_CONFIG, "");
+       register_command(command_context, NULL, "gdb_breakpoint_override", handle_gdb_breakpoint_override_command,
+                       COMMAND_EXEC, "hard/soft/disabled - force breakpoint type for gdb 'break' commands."
+                       "The raison d'etre for this option is to support GDB GUI's without "
+                       "a hard/soft breakpoint concept where the default OpenOCD behaviour "
+                       "is not sufficient");
        return ERROR_OK;
 }