X-Git-Url: https://review.openocd.org/gitweb?p=openocd.git;a=blobdiff_plain;f=src%2Ftarget%2Ftarget.c;h=1ea1a61668c9d059299e6225778138ba92b2f8c8;hp=d34e14418ff608b2dfe805edcc136b6cf6a684f2;hb=d4d62ea8a80502f85f1ca92b231da56ef3b84baf;hpb=3377c164204f8cdaeabdbba13513091b5e04fce0 diff --git a/src/target/target.c b/src/target/target.c index d34e14418f..1ea1a61668 100644 --- a/src/target/target.c +++ b/src/target/target.c @@ -3387,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]++; } @@ -3412,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++) @@ -3421,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) @@ -3432,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);