X-Git-Url: https://review.openocd.org/gitweb?p=openocd.git;a=blobdiff_plain;f=src%2Fserver%2Fgdb_server.c;h=ca8d94489f20b000a48bbc7d950035b8a8fe6e02;hp=bbcff58c58b381b95ec350cfa08ade2595e05658;hb=ef1cfb23947bd32798077c6abb5c25a049460ae9;hpb=06d501a192e36986ba0f81c7ddff9298dd0a389e diff --git a/src/server/gdb_server.c b/src/server/gdb_server.c index bbcff58c58..ca8d94489f 100644 --- a/src/server/gdb_server.c +++ b/src/server/gdb_server.c @@ -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"; @@ -332,7 +338,10 @@ int gdb_put_packet_inner(connection_t *connection, char *buffer, int len) gdb_write(connection, buffer, len); gdb_write(connection, local_buffer+1, 3); } - + + if (gdb_con->noack_mode) + break; + if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK) return retval; @@ -386,13 +395,119 @@ int gdb_put_packet(connection_t *connection, char *buffer, int len) return retval; } -int gdb_get_packet_inner(connection_t *connection, char *buffer, int *len) +static __inline__ int fetch_packet(connection_t *connection, int *checksum_ok, int noack, int *len, char *buffer) { + unsigned char my_checksum = 0; + char checksum[3]; int character; + int retval; + + gdb_connection_t *gdb_con = connection->priv; + my_checksum = 0; int count = 0; + count = 0; + for (;;) + { + /* The common case is that we have an entire packet with no escape chars. + * We need to leave at least 2 bytes in the buffer to have + * gdb_get_char() update various bits and bobs correctly. + */ + if ((gdb_con->buf_cnt > 2) && ((gdb_con->buf_cnt+count) < *len)) + { + /* The compiler will struggle a bit with constant propagation and + * aliasing, so we help it by showing that these values do not + * change inside the loop + */ + int i; + char *buf = gdb_con->buf_p; + int run = gdb_con->buf_cnt - 2; + i = 0; + int done = 0; + while (i < run) + { + character = *buf++; + i++; + if (character == '#') + { + /* Danger! character can be '#' when esc is + * used so we need an explicit boolean for done here. + */ + done = 1; + break; + } + + if (character == '}') + { + /* data transmitted in binary mode (X packet) + * uses 0x7d as escape character */ + my_checksum += character & 0xff; + character = *buf++; + i++; + my_checksum += character & 0xff; + buffer[count++] = (character ^ 0x20) & 0xff; + } + else + { + my_checksum += character & 0xff; + buffer[count++] = character & 0xff; + } + } + gdb_con->buf_p += i; + gdb_con->buf_cnt -= i; + if (done) + break; + } + if (count > *len) + { + LOG_ERROR("packet buffer too small"); + return ERROR_GDB_BUFFER_TOO_SMALL; + } + + if ((retval = gdb_get_char(connection, &character)) != ERROR_OK) + return retval; + + if (character == '#') + break; + + if (character == '}') + { + /* data transmitted in binary mode (X packet) + * uses 0x7d as escape character */ + my_checksum += character & 0xff; + if ((retval = gdb_get_char(connection, &character)) != ERROR_OK) + return retval; + my_checksum += character & 0xff; + buffer[count++] = (character ^ 0x20) & 0xff; + } + else + { + my_checksum += character & 0xff; + buffer[count++] = character & 0xff; + } + } + + *len = count; + + if ((retval = gdb_get_char(connection, &character)) != ERROR_OK) + return retval; + checksum[0] = character; + if ((retval = gdb_get_char(connection, &character)) != ERROR_OK) + return retval; + checksum[1] = character; + checksum[2] = 0; + + if (!noack) + { + *checksum_ok=(my_checksum == strtoul(checksum, NULL, 16)); + } + + return ERROR_OK; +} + +int gdb_get_packet_inner(connection_t *connection, char *buffer, int *len) +{ + int character; int retval; - char checksum[3]; - unsigned char my_checksum = 0; gdb_connection_t *gdb_con = connection->priv; while (1) @@ -428,108 +543,32 @@ int gdb_get_packet_inner(connection_t *connection, char *buffer, int *len) } } while (character != '$'); - my_checksum = 0; - - count = 0; - gdb_connection_t *gdb_con = connection->priv; - for (;;) - { - /* The common case is that we have an entire packet with no escape chars. - * We need to leave at least 2 bytes in the buffer to have - * gdb_get_char() update various bits and bobs correctly. - */ - if ((gdb_con->buf_cnt > 2) && ((gdb_con->buf_cnt+count) < *len)) - { - /* The compiler will struggle a bit with constant propagation and - * aliasing, so we help it by showing that these values do not - * change inside the loop - */ - int i; - char *buf = gdb_con->buf_p; - int run = gdb_con->buf_cnt - 2; - i = 0; - int done = 0; - while (i < run) - { - character = *buf++; - i++; - if (character == '#') - { - /* Danger! character can be '#' when esc is - * used so we need an explicit boolean for done here. - */ - done = 1; - break; - } - if (character == '}') - { - /* data transmitted in binary mode (X packet) - * uses 0x7d as escape character */ - my_checksum += character & 0xff; - character = *buf++; - i++; - my_checksum += character & 0xff; - buffer[count++] = (character ^ 0x20) & 0xff; - } else - { - my_checksum += character & 0xff; - buffer[count++] = character & 0xff; - } - } - gdb_con->buf_p += i; - gdb_con->buf_cnt -= i; - if (done) - break; - } - if (count > *len) - { - LOG_ERROR("packet buffer too small"); - return ERROR_GDB_BUFFER_TOO_SMALL; - } - if ((retval = gdb_get_char(connection, &character)) != ERROR_OK) + int checksum_ok; + /* explicit code expansion here to get faster inlined code in -O3 by not + * calculating checksum + */ + if (gdb_con->noack_mode) + { + if ((retval=fetch_packet(connection, &checksum_ok, 1, len, buffer))!=ERROR_OK) + return retval; + } else + { + if ((retval=fetch_packet(connection, &checksum_ok, 0, len, buffer))!=ERROR_OK) return retval; - - if (character == '#') - break; - - if (character == '}') - { - /* data transmitted in binary mode (X packet) - * uses 0x7d as escape character */ - my_checksum += character & 0xff; - if ((retval = gdb_get_char(connection, &character)) != ERROR_OK) - return retval; - my_checksum += character & 0xff; - buffer[count++] = (character ^ 0x20) & 0xff; - } - else - { - my_checksum += character & 0xff; - buffer[count++] = character & 0xff; - } - } - *len = count; - - if ((retval = gdb_get_char(connection, &character)) != ERROR_OK) - return retval; - checksum[0] = character; - if ((retval = gdb_get_char(connection, &character)) != ERROR_OK) - return retval; - checksum[1] = character; - checksum[2] = 0; - - if (my_checksum == strtoul(checksum, NULL, 16)) + if (gdb_con->noack_mode) + { + /* checksum is not checked in noack mode */ + break; + } + if (checksum_ok) { gdb_write(connection, "+", 1); break; } - - LOG_WARNING("checksum error, requesting retransmission"); - gdb_write(connection, "-", 1); } if (gdb_con->closed) return ERROR_SERVER_REMOTE_CLOSED; @@ -575,57 +614,56 @@ int gdb_output(struct command_context_s *context, const char* line) return ERROR_OK; } -int gdb_program_handler(struct target_s *target, enum target_event event, void *priv) + +static void gdb_frontend_halted(struct target_s *target, connection_t *connection) { - struct command_context_s *cmd_ctx = priv; + 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); - target_invoke_script(cmd_ctx, target, "gdb_program"); - jtag_execute_queue(); + if (gdb_connection->ctrl_c) + { + signal = 0x2; + gdb_connection->ctrl_c = 0; + } + else + { + signal = gdb_last_signal(target); + } - return ERROR_OK; + 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; + target_handle_event( target, event ); 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); + case TARGET_EVENT_GDB_FLASH_ERASE_START: + target_handle_event( target, TARGET_EVENT_OLD_gdb_program_config ); + jtag_execute_queue(); break; default: break; @@ -652,6 +690,7 @@ int gdb_new_connection(connection_t *connection) gdb_connection->vflash_image = NULL; gdb_connection->closed = 0; gdb_connection->busy = 0; + gdb_connection->noack_mode = 0; /* send ACK to GDB for debug request */ gdb_write(connection, "+", 1); @@ -659,6 +698,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); @@ -695,7 +741,7 @@ int gdb_new_connection(connection_t *connection) */ if (initial_ack != '+') gdb_putback_char(connection, initial_ack); - + target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_ATTACH ); return ERROR_OK; } @@ -728,6 +774,7 @@ int gdb_connection_closed(connection_t *connection) target_unregister_event_callback(gdb_target_callback_event_handler, connection); log_remove_callback(gdb_log_callback, connection); + target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_DETACH ); return ERROR_OK; } @@ -1205,10 +1252,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("-"); @@ -1225,14 +1273,15 @@ void gdb_step_continue_packet(connection_t *connection, target_t *target, char * if (packet[0] == 'c') { 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 */ + target_handle_event( target, TARGET_EVENT_OLD_pre_resume ); + 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) @@ -1259,6 +1308,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_HARD))) + { + bp_type=gdb_breakpoint_override_type; + } if (*separator != ',') { @@ -1433,7 +1487,8 @@ static int compare_bank (const void * a, const void * b) int gdb_query_packet(connection_t *connection, target_t *target, char *packet, int packet_size) { command_context_t *cmd_ctx = connection->cmd_ctx; - + gdb_connection_t *gdb_connection = connection->priv; + if (strstr(packet, "qRcmd,")) { if (packet_size > 6) @@ -1510,7 +1565,7 @@ int gdb_query_packet(connection_t *connection, target_t *target, char *packet, i int size = 0; xml_printf(&retval, &buffer, &pos, &size, - "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-", + "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-;QStartNoAckMode+", (GDB_BUFFER_SIZE - 1), ((gdb_use_memory_map == 1)&&(flash_get_bank_count()>0)) ? '+' : '-'); if (retval != ERROR_OK) @@ -1670,6 +1725,12 @@ int gdb_query_packet(connection_t *connection, target_t *target, char *packet, i free(xml); return ERROR_OK; } + else if (strstr(packet, "QStartNoAckMode")) + { + gdb_connection->noack_mode = 1; + gdb_put_packet(connection, "OK", 2); + return ERROR_OK; + } gdb_put_packet(connection, "", 0); return ERROR_OK; @@ -1722,10 +1783,12 @@ int gdb_v_packet(connection_t *connection, target_t *target, char *packet, int p flash_set_dirty(); /* perform any target specific operations before the erase */ - target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_PROGRAM); + target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_ERASE_START); + result = flash_erase_address_range(gdb_service->target, addr, length ); + target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_ERASE_END); /* perform erase */ - if ((result = flash_erase_address_range(gdb_service->target, addr, length)) != ERROR_OK) + if (result != ERROR_OK) { /* GDB doesn't evaluate the actual error number returned, * treat a failed erase as an I/O error @@ -1779,7 +1842,10 @@ int gdb_v_packet(connection_t *connection, target_t *target, char *packet, int p /* process the flashing buffer. No need to erase as GDB * always issues a vFlashErase first. */ - if ((result = flash_write(gdb_service->target, gdb_connection->vflash_image, &written, 0)) != ERROR_OK) + target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_WRITE_START); + result = flash_write(gdb_service->target, gdb_connection->vflash_image, &written, 0); + target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_WRITE_END); + if ( result != ERROR_OK) { if (result == ERROR_FLASH_DST_OUT_OF_BANK) gdb_put_packet(connection, "E.memtype", 9); @@ -1805,10 +1871,11 @@ int gdb_v_packet(connection_t *connection, target_t *target, char *packet, int p int gdb_detach(connection_t *connection, target_t *target) { + switch( detach_mode ) { case GDB_DETACH_RESUME: - target_invoke_script(connection->cmd_ctx, target, "pre_resume"); + target_handle_event( target, TARGET_EVENT_OLD_pre_resume ); target_resume(target, 1, 0, 1, 0); break; @@ -1826,7 +1893,6 @@ int gdb_detach(connection_t *connection, target_t *target) } gdb_put_packet(connection, "OK", 2); - return ERROR_OK; } @@ -1848,6 +1914,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; @@ -1883,6 +1957,7 @@ int gdb_input_inner(connection_t *connection) gdb_put_packet(connection, NULL, 0); break; case 'q': + case 'Q': retval = gdb_query_packet(connection, target, packet, packet_size); break; case 'g': @@ -1918,9 +1993,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 @@ -1929,7 +2002,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; @@ -1956,6 +2034,8 @@ int gdb_input_inner(connection_t *connection) break; case 'R': /* handle extended restart packet */ + breakpoint_clear_target(gdb_service->target); + watchpoint_clear_target(gdb_service->target); command_run_linef(connection->cmd_ctx, "ocd_gdb_restart %d", get_num_by_target(target)); break; default: @@ -1999,11 +2079,10 @@ 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; - int i = 0; + target_t *target = all_targets; if (!target) { @@ -2021,16 +2100,21 @@ int gdb_init() { char service_name[8]; - snprintf(service_name, 8, "gdb-%2.2i", i); + snprintf(service_name, 8, "gdb-%2.2i", target->target_number); gdb_service = malloc(sizeof(gdb_service_t)); gdb_service->target = target; - add_service("gdb", CONNECTION_GDB, gdb_port + i, 1, gdb_new_connection, gdb_input, gdb_connection_closed, gdb_service); + add_service("gdb", CONNECTION_GDB, + gdb_port + target->target_number, + 1, gdb_new_connection, gdb_input, + gdb_connection_closed, + gdb_service); - LOG_DEBUG("gdb service for target %s at port %i", target->type->name, gdb_port + i); + LOG_DEBUG("gdb service for target %s at port %i", + target->type->name, + gdb_port + target->target_number); - i++; target = target->next; } @@ -2140,6 +2224,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, @@ -2152,5 +2271,19 @@ 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; } + + + +/* + * Local Variables: *** + * c-basic-offset: 4 *** + * tab-width: 4 *** + * End: *** + */