target: Correct profiling calculation
[openocd.git] / src / target / target.c
index c5b80d6476745325fc854ce372d3d1426ea3dbe3..1ea1a61668c9d059299e6225778138ba92b2f8c8 100644 (file)
 #define DEFAULT_HALT_TIMEOUT 5000
 
 static int target_read_buffer_default(struct target *target, uint32_t address,
-               uint32_t size, uint8_t *buffer);
+               uint32_t count, uint8_t *buffer);
 static int target_write_buffer_default(struct target *target, uint32_t address,
-               uint32_t size, const uint8_t *buffer);
+               uint32_t count, const uint8_t *buffer);
 static int target_array2mem(Jim_Interp *interp, struct target *target,
                int argc, Jim_Obj * const *argv);
 static int target_mem2array(Jim_Interp *interp, struct target *target,
                int argc, Jim_Obj * const *argv);
 static int target_register_user_commands(struct command_context *cmd_ctx);
+static int target_get_gdb_fileio_info_default(struct target *target,
+               struct gdb_fileio_info *fileio_info);
+static int target_gdb_fileio_end_default(struct target *target, int retcode,
+               int fileio_errno, bool ctrl_c);
 
 /* targets */
 extern struct target_type arm7tdmi_target;
@@ -224,6 +228,7 @@ static const Jim_Nvp nvp_target_debug_reason[] = {
        { .name = "watchpoint-and-breakpoint", .value = DBG_REASON_WPTANDBKPT },
        { .name = "single-step"              , .value = DBG_REASON_SINGLESTEP },
        { .name = "target-not-halted"        , .value = DBG_REASON_NOTHALTED  },
+       { .name = "program-exit"             , .value = DBG_REASON_EXIT },
        { .name = "undefined"                , .value = DBG_REASON_UNDEFINED },
        { .name = NULL, .value = -1 },
 };
@@ -578,8 +583,10 @@ static int target_process_reset(struct command_context *cmd_ctx, enum target_res
        retval = target_call_timer_callbacks_now();
 
        struct target *target;
-       for (target = all_targets; target; target = target->next)
+       for (target = all_targets; target; target = target->next) {
                target->type->check_reset(target);
+               target->running_alg = false;
+       }
 
        return retval;
 }
@@ -979,12 +986,6 @@ int target_write_phys_memory(struct target *target,
        return target->type->write_phys_memory(target, address, size, count, buffer);
 }
 
-static int target_bulk_write_memory_default(struct target *target,
-               uint32_t address, uint32_t count, const uint8_t *buffer)
-{
-       return target_write_memory(target, address, 4, count, buffer);
-}
-
 int target_add_breakpoint(struct target *target,
                struct breakpoint *breakpoint)
 {
@@ -1035,6 +1036,23 @@ int target_remove_watchpoint(struct target *target,
 {
        return target->type->remove_watchpoint(target, watchpoint);
 }
+int target_hit_watchpoint(struct target *target,
+               struct watchpoint **hit_watchpoint)
+{
+       if (target->state != TARGET_HALTED) {
+               LOG_WARNING("target %s is not halted", target->cmd_name);
+               return ERROR_TARGET_NOT_HALTED;
+       }
+
+       if (target->type->hit_watchpoint == NULL) {
+               /* For backward compatible, if hit_watchpoint is not implemented,
+                * return ERROR_FAIL such that gdb_server will not take the nonsense
+                * information. */
+               return ERROR_FAIL;
+       }
+
+       return target->type->hit_watchpoint(target, hit_watchpoint);
+}
 
 int target_get_gdb_reg_list(struct target *target,
                struct reg **reg_list[], int *reg_list_size,
@@ -1048,6 +1066,24 @@ int target_step(struct target *target,
        return target->type->step(target, current, address, handle_breakpoints);
 }
 
+int target_get_gdb_fileio_info(struct target *target, struct gdb_fileio_info *fileio_info)
+{
+       if (target->state != TARGET_HALTED) {
+               LOG_WARNING("target %s is not halted", target->cmd_name);
+               return ERROR_TARGET_NOT_HALTED;
+       }
+       return target->type->get_gdb_fileio_info(target, fileio_info);
+}
+
+int target_gdb_fileio_end(struct target *target, int retcode, int fileio_errno, bool ctrl_c)
+{
+       if (target->state != TARGET_HALTED) {
+               LOG_WARNING("target %s is not halted", target->cmd_name);
+               return ERROR_TARGET_NOT_HALTED;
+       }
+       return target->type->gdb_fileio_end(target, retcode, fileio_errno, ctrl_c);
+}
+
 /**
  * Reset the @c examined flag for the given target.
  * Pure paranoia -- targets are zeroed on allocation.
@@ -1131,8 +1167,11 @@ static int target_init_one(struct command_context *cmd_ctx,
        if (target->type->write_buffer == NULL)
                target->type->write_buffer = target_write_buffer_default;
 
-       if (target->type->bulk_write_memory == NULL)
-               target->type->bulk_write_memory = target_bulk_write_memory_default;
+       if (target->type->get_gdb_fileio_info == NULL)
+               target->type->get_gdb_fileio_info = target_get_gdb_fileio_info_default;
+
+       if (target->type->gdb_fileio_end == NULL)
+               target->type->gdb_fileio_end = target_gdb_fileio_end_default;
 
        return ERROR_OK;
 }
@@ -1683,6 +1722,22 @@ int target_arch_state(struct target *target)
        return retval;
 }
 
+static int target_get_gdb_fileio_info_default(struct target *target,
+               struct gdb_fileio_info *fileio_info)
+{
+       /* If target does not support semi-hosting function, target
+          has no need to provide .get_gdb_fileio_info callback.
+          It just return ERROR_FAIL and gdb_server will return "Txx"
+          as target halted every time.  */
+       return ERROR_FAIL;
+}
+
+static int target_gdb_fileio_end_default(struct target *target,
+               int retcode, int fileio_errno, bool ctrl_c)
+{
+       return ERROR_OK;
+}
+
 /* Single aligned words are guaranteed to use 16 or 32 bit access
  * mode respectively, otherwise data is handled as quickly as
  * possible
@@ -1711,57 +1766,37 @@ int target_write_buffer(struct target *target, uint32_t address, uint32_t size,
        return target->type->write_buffer(target, address, size, buffer);
 }
 
-static int target_write_buffer_default(struct target *target, uint32_t address, uint32_t size, const uint8_t *buffer)
+static int target_write_buffer_default(struct target *target, uint32_t address, uint32_t count, const uint8_t *buffer)
 {
-       int retval = ERROR_OK;
+       uint32_t size;
 
-       if (((address % 2) == 0) && (size == 2))
-               return target_write_memory(target, address, 2, 1, buffer);
-
-       /* handle unaligned head bytes */
-       if (address % 4) {
-               uint32_t unaligned = 4 - (address % 4);
-
-               if (unaligned > size)
-                       unaligned = size;
-
-               retval = target_write_memory(target, address, 1, unaligned, buffer);
-               if (retval != ERROR_OK)
-                       return retval;
-
-               buffer += unaligned;
-               address += unaligned;
-               size -= unaligned;
-       }
-
-       /* handle aligned words */
-       if (size >= 4) {
-               int aligned = size - (size % 4);
-
-               /* use bulk writes above a certain limit. This may have to be changed */
-               if (aligned > 128) {
-                       retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer);
-                       if (retval != ERROR_OK)
-                               return retval;
-               } else {
-                       retval = target_write_memory(target, address, 4, aligned / 4, buffer);
+       /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
+        * will have something to do with the size we leave to it. */
+       for (size = 1; size < 4 && count >= size * 2 + (address & size); size *= 2) {
+               if (address & size) {
+                       int retval = target_write_memory(target, address, size, 1, buffer);
                        if (retval != ERROR_OK)
                                return retval;
+                       address += size;
+                       count -= size;
+                       buffer += size;
                }
-
-               buffer += aligned;
-               address += aligned;
-               size -= aligned;
        }
 
-       /* handle tail writes of less than 4 bytes */
-       if (size > 0) {
-               retval = target_write_memory(target, address, 1, size, buffer);
-               if (retval != ERROR_OK)
-                       return retval;
+       /* Write the data with as large access size as possible. */
+       for (; size > 0; size /= 2) {
+               uint32_t aligned = count - count % size;
+               if (aligned > 0) {
+                       int retval = target_write_memory(target, address, size, aligned / size, buffer);
+                       if (retval != ERROR_OK)
+                               return retval;
+                       address += aligned;
+                       count -= aligned;
+                       buffer += aligned;
+               }
        }
 
-       return retval;
+       return ERROR_OK;
 }
 
 /* Single aligned words are guaranteed to use 16 or 32 bit access
@@ -1792,58 +1827,34 @@ int target_read_buffer(struct target *target, uint32_t address, uint32_t size, u
        return target->type->read_buffer(target, address, size, buffer);
 }
 
-static int target_read_buffer_default(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
+static int target_read_buffer_default(struct target *target, uint32_t address, uint32_t count, uint8_t *buffer)
 {
-       int retval = ERROR_OK;
+       uint32_t size;
 
-       if (((address % 2) == 0) && (size == 2))
-               return target_read_memory(target, address, 2, 1, buffer);
-
-       /* handle unaligned head bytes */
-       if (address % 4) {
-               uint32_t unaligned = 4 - (address % 4);
-
-               if (unaligned > size)
-                       unaligned = size;
-
-               retval = target_read_memory(target, address, 1, unaligned, buffer);
-               if (retval != ERROR_OK)
-                       return retval;
-
-               buffer += unaligned;
-               address += unaligned;
-               size -= unaligned;
-       }
-
-       /* handle aligned words */
-       if (size >= 4) {
-               int aligned = size - (size % 4);
-
-               retval = target_read_memory(target, address, 4, aligned / 4, buffer);
-               if (retval != ERROR_OK)
-                       return retval;
-
-               buffer += aligned;
-               address += aligned;
-               size -= aligned;
+       /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
+        * will have something to do with the size we leave to it. */
+       for (size = 1; size < 4 && count >= size * 2 + (address & size); size *= 2) {
+               if (address & size) {
+                       int retval = target_read_memory(target, address, size, 1, buffer);
+                       if (retval != ERROR_OK)
+                               return retval;
+                       address += size;
+                       count -= size;
+                       buffer += size;
+               }
        }
 
-       /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
-       if (size        >= 2) {
-               int aligned = size - (size % 2);
-               retval = target_read_memory(target, address, 2, aligned / 2, buffer);
-               if (retval != ERROR_OK)
-                       return retval;
-
-               buffer += aligned;
-               address += aligned;
-               size -= aligned;
-       }
-       /* handle tail writes of less than 4 bytes */
-       if (size > 0) {
-               retval = target_read_memory(target, address, 1, size, buffer);
-               if (retval != ERROR_OK)
-                       return retval;
+       /* Read the data with as large access size as possible. */
+       for (; size > 0; size /= 2) {
+               uint32_t aligned = count - count % size;
+               if (aligned > 0) {
+                       int retval = target_read_memory(target, address, size, aligned / size, buffer);
+                       if (retval != ERROR_OK)
+                               return retval;
+                       address += aligned;
+                       count -= aligned;
+                       buffer += aligned;
+               }
        }
 
        return ERROR_OK;
@@ -3376,24 +3387,28 @@ static void writeGmon(uint32_t *samples, uint32_t sampleNum, const char *filenam
                        max = samples[i];
        }
 
-       int addressSpace = (max - min + 1);
+       /* max should be (largest sample + 1)
+        * Refer to binutils/gprof/hist.c (find_histogram_for_pc) */
+       max++;
+
+       int addressSpace = max - min;
        assert(addressSpace >= 2);
 
        static const uint32_t maxBuckets = 16 * 1024; /* maximum buckets. */
-       uint32_t length = addressSpace;
-       if (length > maxBuckets)
-               length = maxBuckets;
-       int *buckets = malloc(sizeof(int)*length);
+       uint32_t numBuckets = addressSpace;
+       if (numBuckets > maxBuckets)
+               numBuckets = maxBuckets;
+       int *buckets = malloc(sizeof(int) * numBuckets);
        if (buckets == NULL) {
                fclose(f);
                return;
        }
-       memset(buckets, 0, sizeof(int) * length);
+       memset(buckets, 0, sizeof(int) * numBuckets);
        for (i = 0; i < sampleNum; i++) {
                uint32_t address = samples[i];
                long long a = address - min;
-               long long b = length - 1;
-               long long c = addressSpace - 1;
+               long long b = numBuckets;
+               long long c = addressSpace;
                int index_t = (a * b) / c; /* danger!!!! int32 overflows */
                buckets[index_t]++;
        }
@@ -3401,7 +3416,7 @@ static void writeGmon(uint32_t *samples, uint32_t sampleNum, const char *filenam
        /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
        writeLong(f, min);                      /* low_pc */
        writeLong(f, max);                      /* high_pc */
-       writeLong(f, length);           /* # of samples */
+       writeLong(f, numBuckets);       /* # of buckets */
        writeLong(f, 100);                      /* KLUDGE! We lie, ca. 100Hz best case. */
        writeString(f, "seconds");
        for (i = 0; i < (15-strlen("seconds")); i++)
@@ -3410,9 +3425,9 @@ static void writeGmon(uint32_t *samples, uint32_t sampleNum, const char *filenam
 
        /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
 
-       char *data = malloc(2 * length);
+       char *data = malloc(2 * numBuckets);
        if (data != NULL) {
-               for (i = 0; i < length; i++) {
+               for (i = 0; i < numBuckets; i++) {
                        int val;
                        val = buckets[i];
                        if (val > 65535)
@@ -3421,7 +3436,7 @@ static void writeGmon(uint32_t *samples, uint32_t sampleNum, const char *filenam
                        data[i * 2 + 1] = (val >> 8) & 0xff;
                }
                free(buckets);
-               writeData(f, data, length * 2);
+               writeData(f, data, numBuckets * 2);
                free(data);
        } else
                free(buckets);

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)