-
-
Notifications
You must be signed in to change notification settings - Fork 781
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: memwatch: read memory while running
- Loading branch information
Showing
5 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#ifndef MEMWATCH_H | ||
#define MEMWATCH_H | ||
|
||
#include <stdint.h> | ||
#include <stdbool.h> | ||
#include <target.h> | ||
|
||
#define MEMWATCH_NUM 8 | ||
/* string length has to be long enough to store an address 0x20000000 */ | ||
#define MEMWATCH_STRLEN 12 | ||
|
||
typedef enum memwatch_format { | ||
MEMWATCH_FMT_SIGNED, | ||
MEMWATCH_FMT_UNSIGNED, | ||
MEMWATCH_FMT_HEX | ||
} memwatch_format_e; | ||
|
||
typedef struct { | ||
uint32_t addr; | ||
uint32_t value; | ||
char name[MEMWATCH_STRLEN]; | ||
memwatch_format_e format; | ||
} memwatch_s; | ||
|
||
extern memwatch_s memwatch_table[MEMWATCH_NUM]; | ||
extern uint32_t memwatch_cnt; | ||
extern bool memwatch_timestamp; | ||
extern void poll_memwatch(target_s *cur_target); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#include "general.h" | ||
#include "gdb_packet.h" | ||
#include "memwatch.h" | ||
#ifdef ENABLE_RTT | ||
#include "rtt_if.h" | ||
#endif | ||
#if PC_HOSTED == 1 | ||
#include <unistd.h> | ||
#else | ||
#include "usb_serial.h" | ||
#endif | ||
|
||
memwatch_s memwatch_table[MEMWATCH_NUM]; | ||
uint32_t memwatch_cnt = 0; | ||
bool memwatch_timestamp = false; | ||
|
||
#ifndef ENABLE_RTT | ||
static uint32_t rtt_write(const char *buf, uint32_t len) | ||
{ | ||
#if PC_HOSTED == 1 | ||
return write(1, buf, len); | ||
#else | ||
uint32_t start_ms = platform_time_ms(); | ||
while (usbd_ep_write_packet(usbdev, CDCACM_UART_ENDPOINT, buf, len) <= 0) { | ||
if (platform_time_ms() - start_ms >= 25) | ||
return 0; /* drop silently */ | ||
} | ||
return len; | ||
#endif | ||
} | ||
#endif | ||
|
||
void poll_memwatch(target_s *cur_target) | ||
{ | ||
uint32_t val; | ||
char buf[64]; | ||
char timestamp[64]; | ||
uint32_t len; | ||
if (!cur_target || (memwatch_cnt == 0)) | ||
return; | ||
|
||
for (uint32_t i = 0; i < memwatch_cnt; i++) { | ||
if (!target_mem_read(cur_target, &val, memwatch_table[i].addr, sizeof(val)) && | ||
(val != memwatch_table[i].value)) { | ||
if (memwatch_timestamp) | ||
snprintf(timestamp, sizeof(timestamp), "%" PRIu32 " ", platform_time_ms()); | ||
else | ||
timestamp[0] = '\0'; | ||
switch (memwatch_table[i].format) { | ||
case MEMWATCH_FMT_SIGNED: | ||
len = snprintf(buf, sizeof(buf), "%s%s %" PRId32 "\r\n", timestamp, memwatch_table[i].name, val); | ||
break; | ||
case MEMWATCH_FMT_UNSIGNED: | ||
len = snprintf(buf, sizeof(buf), "%s%s %" PRIu32 "\r\n", timestamp, memwatch_table[i].name, val); | ||
break; | ||
case MEMWATCH_FMT_HEX: | ||
default: | ||
len = snprintf(buf, sizeof(buf), "%s%s 0x%" PRIx32 "\r\n", timestamp, memwatch_table[i].name, val); | ||
break; | ||
} | ||
buf[sizeof(buf) - 1] = '\0'; | ||
rtt_write(buf, len); | ||
memwatch_table[i].value = val; | ||
} | ||
} | ||
return; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters