target: simplify register get/set ops
[fw/openocd] / src / server / gdb_server.c
index 85855d9140aa0cbd8b3887e88a03d5e1613eddb1..9581ea63aa35e45abba3ca245c52850971374ccd 100644 (file)
 #include "config.h"
 #endif
 
-#include "gdb_server.h"
+#include "breakpoints.h"
 #include "target_request.h"
 #include "register.h"
 #include "server.h"
 #include "flash.h"
+#include "gdb_server.h"
 #include "image.h"
 #include "jtag.h"
 
@@ -67,7 +68,7 @@ int gdb_flash_program = 1;
  * see the code in gdb_read_memory_packet() for further explanations */
 int gdb_report_data_abort = 0;
 
-int gdb_last_signal(target_t *target)
+int gdb_last_signal(struct target *target)
 {
        switch (target->debug_reason)
        {
@@ -650,7 +651,7 @@ int gdb_output_con(struct connection *connection, const char* line)
        return retval;
 }
 
-int gdb_output(struct command_context_s *context, const char* line)
+int gdb_output(struct command_context *context, const char* line)
 {
        /* this will be dumped to the log and also sent as an O packet if possible */
        LOG_USER_N("%s", line);
@@ -658,7 +659,7 @@ int gdb_output(struct command_context_s *context, const char* line)
 }
 
 
-static void gdb_frontend_halted(struct target_s *target, struct connection *connection)
+static void gdb_frontend_halted(struct target *target, struct connection *connection)
 {
        struct gdb_connection *gdb_connection = connection->priv;
 
@@ -695,7 +696,7 @@ static void gdb_frontend_halted(struct target_s *target, struct connection *conn
        }
 }
 
-int gdb_target_callback_event_handler(struct target_s *target, enum target_event event, void *priv)
+int gdb_target_callback_event_handler(struct target *target, enum target_event event, void *priv)
 {
        int retval;
        struct connection *connection = priv;
@@ -833,7 +834,7 @@ void gdb_send_error(struct connection *connection, uint8_t the_error)
        gdb_put_packet(connection, err, 3);
 }
 
-int gdb_last_signal_packet(struct connection *connection, target_t *target, char* packet, int packet_size)
+int gdb_last_signal_packet(struct connection *connection, struct target *target, char* packet, int packet_size)
 {
        char sig_reply[4];
        int signal;
@@ -846,7 +847,7 @@ int gdb_last_signal_packet(struct connection *connection, target_t *target, char
        return ERROR_OK;
 }
 
-static int gdb_reg_pos(target_t *target, int pos, int len)
+static int gdb_reg_pos(struct target *target, int pos, int len)
 {
        if (target->endianness == TARGET_LITTLE_ENDIAN)
                return pos;
@@ -863,14 +864,14 @@ static int gdb_reg_pos(target_t *target, int pos, int len)
  * The format of reg->value is little endian
  *
  */
-void gdb_str_to_target(target_t *target, char *tstr, struct reg *reg)
+void gdb_str_to_target(struct target *target, char *tstr, struct reg *reg)
 {
        int i;
 
        uint8_t *buf;
        int buf_len;
        buf = reg->value;
-       buf_len = CEIL(reg->size, 8);
+       buf_len = DIV_ROUND_UP(reg->size, 8);
 
        for (i = 0; i < buf_len; i++)
        {
@@ -896,7 +897,7 @@ static int hextoint(char c)
 }
 
 /* copy over in register buffer */
-void gdb_target_to_reg(target_t *target, char *tstr, int str_len, uint8_t *bin)
+void gdb_target_to_reg(struct target *target, char *tstr, int str_len, uint8_t *bin)
 {
        if (str_len % 2)
        {
@@ -915,7 +916,7 @@ void gdb_target_to_reg(target_t *target, char *tstr, int str_len, uint8_t *bin)
        }
 }
 
-int gdb_get_registers_packet(struct connection *connection, target_t *target, char* packet, int packet_size)
+int gdb_get_registers_packet(struct connection *connection, struct target *target, char* packet, int packet_size)
 {
        struct reg **reg_list;
        int reg_list_size;
@@ -939,25 +940,25 @@ int gdb_get_registers_packet(struct connection *connection, target_t *target, ch
                reg_packet_size += reg_list[i]->size;
        }
 
-       reg_packet = malloc(CEIL(reg_packet_size, 8) * 2);
+       reg_packet = malloc(DIV_ROUND_UP(reg_packet_size, 8) * 2);
        reg_packet_p = reg_packet;
 
        for (i = 0; i < reg_list_size; i++)
        {
                gdb_str_to_target(target, reg_packet_p, reg_list[i]);
-               reg_packet_p += CEIL(reg_list[i]->size, 8) * 2;
+               reg_packet_p += DIV_ROUND_UP(reg_list[i]->size, 8) * 2;
        }
 
 #ifdef _DEBUG_GDB_IO_
        {
                char *reg_packet_p;
-               reg_packet_p = strndup(reg_packet, CEIL(reg_packet_size, 8) * 2);
+               reg_packet_p = strndup(reg_packet, DIV_ROUND_UP(reg_packet_size, 8) * 2);
                LOG_DEBUG("reg_packet: %s", reg_packet_p);
                free(reg_packet_p);
        }
 #endif
 
-       gdb_put_packet(connection, reg_packet, CEIL(reg_packet_size, 8) * 2);
+       gdb_put_packet(connection, reg_packet, DIV_ROUND_UP(reg_packet_size, 8) * 2);
        free(reg_packet);
 
        free(reg_list);
@@ -965,7 +966,7 @@ int gdb_get_registers_packet(struct connection *connection, target_t *target, ch
        return ERROR_OK;
 }
 
-int gdb_set_registers_packet(struct connection *connection, target_t *target, char *packet, int packet_size)
+int gdb_set_registers_packet(struct connection *connection, struct target *target, char *packet, int packet_size)
 {
        int i;
        struct reg **reg_list;
@@ -996,21 +997,17 @@ int gdb_set_registers_packet(struct connection *connection, target_t *target, ch
        for (i = 0; i < reg_list_size; i++)
        {
                uint8_t *bin_buf;
-               int chars = (CEIL(reg_list[i]->size, 8) * 2);
+               int chars = (DIV_ROUND_UP(reg_list[i]->size, 8) * 2);
 
                if (packet_p + chars > packet + packet_size)
                {
                        LOG_ERROR("BUG: register packet is too small for registers");
                }
 
-               struct reg_arch_type *arch_type;
-               bin_buf = malloc(CEIL(reg_list[i]->size, 8));
+               bin_buf = malloc(DIV_ROUND_UP(reg_list[i]->size, 8));
                gdb_target_to_reg(target, packet_p, chars, bin_buf);
 
-               /* get register arch_type, and call set method */
-               arch_type = register_get_arch_type(reg_list[i]->arch_type);
-
-               arch_type->set(reg_list[i], bin_buf);
+               reg_list[i]->type->set(reg_list[i], bin_buf);
 
                /* advance packet pointer */
                packet_p += chars;
@@ -1027,7 +1024,7 @@ int gdb_set_registers_packet(struct connection *connection, target_t *target, ch
        return ERROR_OK;
 }
 
-int gdb_get_register_packet(struct connection *connection, target_t *target, char *packet, int packet_size)
+int gdb_get_register_packet(struct connection *connection, struct target *target, char *packet, int packet_size)
 {
        char *reg_packet;
        int reg_num = strtoul(packet + 1, NULL, 16);
@@ -1050,11 +1047,11 @@ int gdb_get_register_packet(struct connection *connection, target_t *target, cha
                exit(-1);
        }
 
-       reg_packet = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
+       reg_packet = malloc(DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2);
 
        gdb_str_to_target(target, reg_packet, reg_list[reg_num]);
 
-       gdb_put_packet(connection, reg_packet, CEIL(reg_list[reg_num]->size, 8) * 2);
+       gdb_put_packet(connection, reg_packet, DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2);
 
        free(reg_list);
        free(reg_packet);
@@ -1062,7 +1059,7 @@ int gdb_get_register_packet(struct connection *connection, target_t *target, cha
        return ERROR_OK;
 }
 
-int gdb_set_register_packet(struct connection *connection, target_t *target, char *packet, int packet_size)
+int gdb_set_register_packet(struct connection *connection, struct target *target, char *packet, int packet_size)
 {
        char *separator;
        uint8_t *bin_buf;
@@ -1070,7 +1067,6 @@ int gdb_set_register_packet(struct connection *connection, target_t *target, cha
        struct reg **reg_list;
        int reg_list_size;
        int retval;
-       struct reg_arch_type *arch_type;
 
        LOG_DEBUG("-");
 
@@ -1092,16 +1088,14 @@ int gdb_set_register_packet(struct connection *connection, target_t *target, cha
        }
 
        /* convert from GDB-string (target-endian) to hex-string (big-endian) */
-       bin_buf = malloc(CEIL(reg_list[reg_num]->size, 8));
-       int chars = (CEIL(reg_list[reg_num]->size, 8) * 2);
+       bin_buf = malloc(DIV_ROUND_UP(reg_list[reg_num]->size, 8));
+       int chars = (DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2);
 
        /* fix!!! add some sanity checks on packet size here */
 
        gdb_target_to_reg(target, separator + 1, chars, bin_buf);
 
-               /* get register arch_type, and call set method */
-       arch_type = register_get_arch_type(reg_list[reg_num]->arch_type);
-       arch_type->set(reg_list[reg_num], bin_buf);
+       reg_list[reg_num]->type->set(reg_list[reg_num], bin_buf);
 
        gdb_put_packet(connection, "OK", 2);
 
@@ -1142,7 +1136,7 @@ int gdb_error(struct connection *connection, int retval)
  *
  * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
  */
-int gdb_read_memory_packet(struct connection *connection, target_t *target, char *packet, int packet_size)
+int gdb_read_memory_packet(struct connection *connection, struct target *target, char *packet, int packet_size)
 {
        char *separator;
        uint32_t addr = 0;
@@ -1216,7 +1210,7 @@ int gdb_read_memory_packet(struct connection *connection, target_t *target, char
        return retval;
 }
 
-int gdb_write_memory_packet(struct connection *connection, target_t *target, char *packet, int packet_size)
+int gdb_write_memory_packet(struct connection *connection, struct target *target, char *packet, int packet_size)
 {
        char *separator;
        uint32_t addr = 0;
@@ -1273,7 +1267,7 @@ int gdb_write_memory_packet(struct connection *connection, target_t *target, cha
        return retval;
 }
 
-int gdb_write_memory_binary_packet(struct connection *connection, target_t *target, char *packet, int packet_size)
+int gdb_write_memory_binary_packet(struct connection *connection, struct target *target, char *packet, int packet_size)
 {
        char *separator;
        uint32_t addr = 0;
@@ -1321,7 +1315,7 @@ int gdb_write_memory_binary_packet(struct connection *connection, target_t *targ
        return ERROR_OK;
 }
 
-int gdb_step_continue_packet(struct connection *connection, target_t *target, char *packet, int packet_size)
+int gdb_step_continue_packet(struct connection *connection, struct target *target, char *packet, int packet_size)
 {
        int current = 0;
        uint32_t address = 0x0;
@@ -1354,7 +1348,7 @@ int gdb_step_continue_packet(struct connection *connection, target_t *target, ch
        return retval;
 }
 
-int gdb_breakpoint_watchpoint_packet(struct connection *connection, target_t *target, char *packet, int packet_size)
+int gdb_breakpoint_watchpoint_packet(struct connection *connection, struct target *target, char *packet, int packet_size)
 {
        int type;
        enum breakpoint_type bp_type = BKPT_SOFT /* dummy init to avoid warning */;
@@ -1520,7 +1514,7 @@ static int decode_xfer_read(char *buf, char **annex, int *ofs, unsigned int *len
        return 0;
 }
 
-int gdb_calc_blocksize(flash_bank_t *bank)
+int gdb_calc_blocksize(struct flash_bank *bank)
 {
        uint32_t i;
        uint32_t block_size = 0xffffffff;
@@ -1538,9 +1532,9 @@ int gdb_calc_blocksize(flash_bank_t *bank)
 
 static int compare_bank (const void * a, const void * b)
 {
-       flash_bank_t *b1, *b2;
-       b1=*((flash_bank_t **)a);
-       b2=*((flash_bank_t **)b);
+       struct flash_bank *b1, *b2;
+       b1=*((struct flash_bank **)a);
+       b2=*((struct flash_bank **)b);
 
        if (b1->base == b2->base)
        {
@@ -1554,9 +1548,9 @@ static int compare_bank (const void * a, const void * b)
        }
 }
 
-int gdb_query_packet(struct connection *connection, target_t *target, char *packet, int packet_size)
+int gdb_query_packet(struct connection *connection, struct target *target, char *packet, int packet_size)
 {
-       command_context_t *cmd_ctx = connection->cmd_ctx;
+       struct command_context *cmd_ctx = connection->cmd_ctx;
        struct gdb_connection *gdb_connection = connection->priv;
 
        if (strstr(packet, "qRcmd,"))
@@ -1661,7 +1655,7 @@ int gdb_query_packet(struct connection *connection, target_t *target, char *pack
                 * Normally we only execute this code once, but no big deal if we
                 * have to regenerate it a couple of times. */
 
-               flash_bank_t *p;
+               struct flash_bank *p;
                char *xml = NULL;
                int size = 0;
                int pos = 0;
@@ -1685,7 +1679,7 @@ int gdb_query_packet(struct connection *connection, target_t *target, char *pack
                read/write) by default for GDB.
                GDB does not have a concept of non-cacheable read/write memory.
                 */
-               flash_bank_t **banks = malloc(sizeof(flash_bank_t *)*flash_get_bank_count());
+               struct flash_bank **banks = malloc(sizeof(struct flash_bank *)*flash_get_bank_count());
                int i;
 
                for (i = 0; i < flash_get_bank_count(); i++)
@@ -1701,7 +1695,7 @@ int gdb_query_packet(struct connection *connection, target_t *target, char *pack
                        banks[i]=p;
                }
 
-               qsort(banks, flash_get_bank_count(), sizeof(flash_bank_t *), compare_bank);
+               qsort(banks, flash_get_bank_count(), sizeof(struct flash_bank *), compare_bank);
 
                uint32_t ram_start = 0;
                for (i = 0; i < flash_get_bank_count(); i++)
@@ -1810,7 +1804,7 @@ int gdb_query_packet(struct connection *connection, target_t *target, char *pack
        return ERROR_OK;
 }
 
-int gdb_v_packet(struct connection *connection, target_t *target, char *packet, int packet_size)
+int gdb_v_packet(struct connection *connection, struct target *target, char *packet, int packet_size)
 {
        struct gdb_connection *gdb_connection = connection->priv;
        struct gdb_service *gdb_service = connection->service->priv;
@@ -1947,7 +1941,7 @@ int gdb_v_packet(struct connection *connection, target_t *target, char *packet,
        return ERROR_OK;
 }
 
-int gdb_detach(struct connection *connection, target_t *target)
+int gdb_detach(struct connection *connection, struct target *target)
 {
        struct gdb_service *gdb_service = connection->service->priv;
 
@@ -1985,7 +1979,7 @@ static void gdb_sig_halted(struct connection *connection)
 int gdb_input_inner(struct connection *connection)
 {
        struct gdb_service *gdb_service = connection->service->priv;
-       target_t *target = gdb_service->target;
+       struct target *target = gdb_service->target;
        char *packet = gdb_packet_buffer;
        int packet_size;
        int retval;
@@ -2198,7 +2192,7 @@ int gdb_input(struct connection *connection)
 int gdb_init(void)
 {
        struct gdb_service *gdb_service;
-       target_t *target = all_targets;
+       struct target *target = all_targets;
 
        if (!target)
        {
@@ -2371,7 +2365,7 @@ COMMAND_HANDLER(handle_gdb_breakpoint_override_command)
        return ERROR_OK;
 }
 
-int gdb_register_commands(command_context_t *command_context)
+int gdb_register_commands(struct command_context *command_context)
 {
        register_command(command_context, NULL, "gdb_sync",
                        handle_gdb_sync_command, COMMAND_ANY,