Skip to content

Commit 2b6fe8f

Browse files
committed
target: fix assert in 'monitor profile' on constant PC
When target is stopped in WFI/WFE or is in an infinite loop, the sampled PC will always return the same value. Command 'profile' requires that distance between min and max PC should be at least 2, which is not the case for constant PC, and incorrectly enforces the check through as assert(). Move the code that reads the optional parameters 'start' and 'end' and check the gap 'end - start' before running the profile. For self-computed min and max, increase max (or decrease min) to match the required constraint. Drop the assert(). Change-Id: I2be8df8568ce8c889923888c492e4f7ce354b16b Signed-off-by: Antonio Borneo <[email protected]> Fixes: https://sourceforge.net/p/openocd/tickets/370/ Reviewed-on: https://review.openocd.org/c/openocd/+/7400 Tested-by: jenkins
1 parent a51ac96 commit 2b6fe8f

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

src/target/target.c

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4253,10 +4253,17 @@ static void write_gmon(uint32_t *samples, uint32_t sample_num, const char *filen
42534253
* Refer to binutils/gprof/hist.c (find_histogram_for_pc) */
42544254
if (max < UINT32_MAX)
42554255
max++;
4256+
4257+
/* gprof requires (max - min) >= 2 */
4258+
while ((max - min) < 2) {
4259+
if (max < UINT32_MAX)
4260+
max++;
4261+
else
4262+
min--;
4263+
}
42564264
}
42574265

42584266
uint32_t address_space = max - min;
4259-
assert(address_space >= 2);
42604267

42614268
/* FIXME: What is the reasonable number of buckets?
42624269
* The profiling result will be more accurate if there are enough buckets. */
@@ -4332,6 +4339,19 @@ COMMAND_HANDLER(handle_profile_command)
43324339

43334340
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], offset);
43344341

4342+
uint32_t start_address = 0;
4343+
uint32_t end_address = 0;
4344+
bool with_range = false;
4345+
if (CMD_ARGC == 4) {
4346+
with_range = true;
4347+
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], start_address);
4348+
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], end_address);
4349+
if (start_address > end_address || (end_address - start_address) < 2) {
4350+
command_print(CMD, "Error: end - start < 2");
4351+
return ERROR_COMMAND_ARGUMENT_INVALID;
4352+
}
4353+
}
4354+
43354355
uint32_t *samples = malloc(sizeof(uint32_t) * MAX_PROFILE_SAMPLE_NUM);
43364356
if (!samples) {
43374357
LOG_ERROR("No memory to store samples.");
@@ -4384,15 +4404,6 @@ COMMAND_HANDLER(handle_profile_command)
43844404
return retval;
43854405
}
43864406

4387-
uint32_t start_address = 0;
4388-
uint32_t end_address = 0;
4389-
bool with_range = false;
4390-
if (CMD_ARGC == 4) {
4391-
with_range = true;
4392-
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], start_address);
4393-
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], end_address);
4394-
}
4395-
43964407
write_gmon(samples, num_of_samples, CMD_ARGV[1],
43974408
with_range, start_address, end_address, target, duration_ms);
43984409
command_print(CMD, "Wrote %s", CMD_ARGV[1]);

0 commit comments

Comments
 (0)