1
2
3
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
30
31
32
37
38
42
43
44
45
46
47
48
49
50
51
52
53
54
58
59
60
66
67
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
97
102
106
110
114
118
119
120
121
122
123
124
125
126
127
128
129
133
134
135
136
137
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
163
164
168
169
170
171
172
173
174
175
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
202
203
204
205
206
210
211
215
216
217
218
219
223
224
225
226
227
231
232
233
237
238
241
242
243
247
248
249
250
251
252
253
254
255
256
259
260
261
265
266
267
268
269
270
271
272
273
274
275
276
281
282
283
288
289
290
291
295
296
297
298
299
300
301
302
303
304
314
315
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
346
347
348
352
353
354
359
360
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
387
388
389
390
391
395
396
397
401
402
403
404
405
406
407
408
412
413
414
415
416
417
418
419
420
421
425
426
430
431
432
433
434
435
436
437
438
439
440
441
442
/* ... */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <jtag/jtag.h>
#include "target/target.h"
#include "target/armv7m.h"
#include "target/cortex_m.h"
#include "rtos.h"
#include "helper/log.h"
#include "helper/types.h"
#include "target/register.h"
#include "rtos_nuttx_stackings.h"
9 includes
#define NAME_SIZE 32
#define EXTRAINFO_SIZE 256
/* ... */
#define PTR_WIDTH 4
struct nuttx_params {
const char *target_name;
const struct rtos_register_stacking *stacking;
const struct rtos_register_stacking *(*select_stackinfo)(struct target *target);
...};
/* ... */
#define TCBINFO_TARGET_SIZE 22
struct tcbinfo {
uint16_t pid_off;
uint16_t state_off;
uint16_t pri_off;
uint16_t name_off;
uint16_t regs_off;
uint16_t basic_num;
uint16_t total_num;
target_addr_t xcpreg_off;
...};
struct symbols {
const char *name;
bool optional;
...};
enum nuttx_symbol_vals {
NX_SYM_READYTORUN = 0,
NX_SYM_PIDHASH,
NX_SYM_NPIDHASH,
NX_SYM_TCB_INFO,
...};
static const struct symbols nuttx_symbol_list[] = {
{ "g_readytorun", false },
{ "g_pidhash", false },
{ "g_npidhash", false },
{ "g_tcbinfo", false },
{ NULL, false }
...};
static char *task_state_str[] = {
"INVALID",
"PENDING",
"READYTORUN",
"RUNNING",
"INACTIVE",
"WAIT_SEM",
"WAIT_SIG",
"WAIT_MQNOTEMPTY",
"WAIT_MQNOTFULL",
"WAIT_PAGEFILL",
"STOPPED",
...};
static const struct rtos_register_stacking *cortexm_select_stackinfo(struct target *target);
static const struct nuttx_params nuttx_params_list[] = {
{
.target_name = "cortex_m",
.stacking = NULL,
.select_stackinfo = cortexm_select_stackinfo,
...},
{
.target_name = "hla_target",
.stacking = NULL,
.select_stackinfo = cortexm_select_stackinfo,
...},
{
.target_name = "esp32",
.stacking = &nuttx_esp32_stacking,
...},
{
.target_name = "esp32s2",
.stacking = &nuttx_esp32s2_stacking,
...},
{
.target_name = "esp32s3",
.stacking = &nuttx_esp32s3_stacking,
...},
{
.target_name = "esp32c3",
.stacking = &nuttx_riscv_stacking,
...},
...};
static bool cortexm_hasfpu(struct target *target)
{
uint32_t cpacr;
struct armv7m_common *armv7m_target = target_to_armv7m(target);
if (!is_armv7m(armv7m_target) || armv7m_target->fp_feature == FP_NONE)
return false;
int retval = target_read_u32(target, FPU_CPACR, &cpacr);
if (retval != ERROR_OK) {
LOG_ERROR("Could not read CPACR register to check FPU state");
return false;
}if (retval != ERROR_OK) { ... }
return cpacr & 0x00F00000;
}{ ... }
static const struct rtos_register_stacking *cortexm_select_stackinfo(struct target *target)
{
return cortexm_hasfpu(target) ? &nuttx_stacking_cortex_m_fpu : &nuttx_stacking_cortex_m;
}{ ... }
static bool nuttx_detect_rtos(struct target *target)
{
if (target->rtos->symbols &&
target->rtos->symbols[NX_SYM_READYTORUN].address != 0 &&
target->rtos->symbols[NX_SYM_PIDHASH].address != 0)
return true;
return false;
}{ ... }
static int nuttx_create(struct target *target)
{
const struct nuttx_params *param;
unsigned int i;
for (i = 0; i < ARRAY_SIZE(nuttx_params_list); i++) {
param = &nuttx_params_list[i];
if (strcmp(target_type_name(target), param->target_name) == 0) {
LOG_INFO("Detected target \"%s\"", param->target_name);
break;
}if (strcmp(target_type_name(target), param->target_name) == 0) { ... }
}for (i = 0; i < ARRAY_SIZE(nuttx_params_list); i++) { ... }
if (i >= ARRAY_SIZE(nuttx_params_list)) {
LOG_ERROR("Could not find \"%s\" target in NuttX compatibility list", target_type_name(target));
return JIM_ERR;
}if (i >= ARRAY_SIZE(nuttx_params_list)) { ... }
target->rtos->rtos_specific_params = (void *)param;
return JIM_OK;
}{ ... }
static int nuttx_smp_init(struct target *target)
{
/* ... */
return ERROR_OK;
}{ ... }
static target_addr_t target_buffer_get_addr(struct target *target, const uint8_t *buffer)
{
#if PTR_WIDTH == 8
return target_buffer_get_u64(target, buffer);
#else
return target_buffer_get_u32(target, buffer);
#endif
}{ ... }
static int nuttx_update_threads(struct rtos *rtos)
{
struct tcbinfo tcbinfo;
uint32_t pidhashaddr, npidhash, tcbaddr;
uint16_t pid;
uint8_t state;
if (!rtos->symbols) {
LOG_ERROR("No symbols for nuttx");
return ERROR_FAIL;
}if (!rtos->symbols) { ... }
rtos_free_threadlist(rtos);
/* ... */
int ret = target_read_u32(rtos->target, rtos->symbols[NX_SYM_NPIDHASH].address, &npidhash);
if (ret != ERROR_OK) {
LOG_ERROR("Failed to read g_npidhash: ret = %d", ret);
return ERROR_FAIL;
}if (ret != ERROR_OK) { ... }
LOG_DEBUG("Hash table size (g_npidhash) = %" PRId32, npidhash);
ret = target_read_u32(rtos->target, rtos->symbols[NX_SYM_PIDHASH].address, &pidhashaddr);
if (ret != ERROR_OK) {
LOG_ERROR("Failed to read g_pidhash address: ret = %d", ret);
return ERROR_FAIL;
}if (ret != ERROR_OK) { ... }
LOG_DEBUG("Hash table address (g_pidhash) = %" PRIx32, pidhashaddr);
uint8_t *pidhash = malloc(npidhash * PTR_WIDTH);
if (!pidhash) {
LOG_ERROR("Failed to allocate pidhash");
return ERROR_FAIL;
}if (!pidhash) { ... }
ret = target_read_buffer(rtos->target, pidhashaddr, PTR_WIDTH * npidhash, pidhash);
if (ret != ERROR_OK) {
LOG_ERROR("Failed to read tcbhash: ret = %d", ret);
goto errout;
}if (ret != ERROR_OK) { ... }
/* ... */
uint8_t buff[TCBINFO_TARGET_SIZE];
ret = target_read_buffer(rtos->target, rtos->symbols[NX_SYM_TCB_INFO].address, sizeof(buff), buff);
if (ret != ERROR_OK) {
LOG_ERROR("Failed to read tcbinfo: ret = %d", ret);
goto errout;
}if (ret != ERROR_OK) { ... }
tcbinfo.pid_off = target_buffer_get_u16(rtos->target, buff);
tcbinfo.state_off = target_buffer_get_u16(rtos->target, buff + 2);
tcbinfo.pri_off = target_buffer_get_u16(rtos->target, buff + 4);
tcbinfo.name_off = target_buffer_get_u16(rtos->target, buff + 6);
tcbinfo.regs_off = target_buffer_get_u16(rtos->target, buff + 8);
tcbinfo.basic_num = target_buffer_get_u16(rtos->target, buff + 10);
tcbinfo.total_num = target_buffer_get_u16(rtos->target, buff + 12);
tcbinfo.xcpreg_off = target_buffer_get_addr(rtos->target, buff + 14);
/* ... */
uint32_t current_thread;
ret = target_read_u32(rtos->target, rtos->symbols[NX_SYM_READYTORUN].address, ¤t_thread);
if (ret != ERROR_OK) {
LOG_ERROR("Failed to read g_readytorun: ret = %d", ret);
goto errout;
}if (ret != ERROR_OK) { ... }
rtos->current_thread = current_thread;
uint32_t thread_count = 0;
for (unsigned int i = 0; i < npidhash; i++) {
tcbaddr = target_buffer_get_u32(rtos->target, &pidhash[i * PTR_WIDTH]);
if (!tcbaddr)
continue;
ret = target_read_u16(rtos->target, tcbaddr + tcbinfo.pid_off, &pid);
if (ret != ERROR_OK) {
LOG_ERROR("Failed to read PID of TCB@0x%x from pidhash[%d]: ret = %d",
tcbaddr, i, ret);
goto errout;
}if (ret != ERROR_OK) { ... }
ret = target_read_u8(rtos->target, tcbaddr + tcbinfo.state_off, &state);
if (ret != ERROR_OK) {
LOG_ERROR("Failed to read state of TCB@0x%x from pidhash[%d]: ret = %d",
tcbaddr, i, ret);
goto errout;
}if (ret != ERROR_OK) { ... }
struct thread_detail *new_thread_details = realloc(rtos->thread_details,
sizeof(struct thread_detail) * (thread_count + 1));
if (!new_thread_details) {
ret = ERROR_FAIL;
goto errout;
}if (!new_thread_details) { ... }
struct thread_detail *thread = &new_thread_details[thread_count];
thread->threadid = tcbaddr;
thread->exists = true;
thread->extra_info_str = NULL;
rtos->thread_details = new_thread_details;
thread_count++;
if (state < ARRAY_SIZE(task_state_str)) {
thread->extra_info_str = malloc(EXTRAINFO_SIZE);
if (!thread->extra_info_str) {
ret = ERROR_FAIL;
goto errout;
}if (!thread->extra_info_str) { ... }
snprintf(thread->extra_info_str, EXTRAINFO_SIZE, "pid:%d, %s",
pid,
task_state_str[state]);
}if (state < ARRAY_SIZE(task_state_str)) { ... }
if (tcbinfo.name_off) {
thread->thread_name_str = calloc(NAME_SIZE + 1, sizeof(char));
if (!thread->thread_name_str) {
ret = ERROR_FAIL;
goto errout;
}if (!thread->thread_name_str) { ... }
ret = target_read_buffer(rtos->target, tcbaddr + tcbinfo.name_off,
sizeof(char) * NAME_SIZE, (uint8_t *)thread->thread_name_str);
if (ret != ERROR_OK) {
LOG_ERROR("Failed to read thread's name: ret = %d", ret);
goto errout;
}if (ret != ERROR_OK) { ... }
}if (tcbinfo.name_off) { ... } else {
thread->thread_name_str = strdup("None");
}else { ... }
}for (unsigned int i = 0; i < npidhash; i++) { ... }
ret = ERROR_OK;
rtos->thread_count = thread_count;
errout:
free(pidhash);
return ret;
}{ ... }
static int nuttx_getreg_current_thread(struct rtos *rtos,
struct rtos_reg **reg_list, int *num_regs)
{
struct reg **gdb_reg_list;
/* ... */
int ret = target_get_gdb_reg_list(rtos->target, &gdb_reg_list, num_regs,
REG_CLASS_GENERAL);
if (ret != ERROR_OK) {
LOG_ERROR("target_get_gdb_reg_list failed %d", ret);
return ret;
}if (ret != ERROR_OK) { ... }
*reg_list = calloc(*num_regs, sizeof(struct rtos_reg));
if (!(*reg_list)) {
LOG_ERROR("Failed to alloc memory for %d", *num_regs);
free(gdb_reg_list);
return ERROR_FAIL;
}if (!(*reg_list)) { ... }
for (int i = 0; i < *num_regs; i++) {
(*reg_list)[i].number = gdb_reg_list[i]->number;
(*reg_list)[i].size = gdb_reg_list[i]->size;
memcpy((*reg_list)[i].value, gdb_reg_list[i]->value, ((*reg_list)[i].size + 7) / 8);
}for (int i = 0; i < *num_regs; i++) { ... }
free(gdb_reg_list);
return ERROR_OK;
}{ ... }
static int nuttx_getregs_fromstack(struct rtos *rtos, int64_t thread_id,
struct rtos_reg **reg_list, int *num_regs)
{
uint16_t xcpreg_off;
uint32_t regsaddr;
const struct nuttx_params *priv = rtos->rtos_specific_params;
const struct rtos_register_stacking *stacking = priv->stacking;
if (!stacking) {
if (priv->select_stackinfo) {
stacking = priv->select_stackinfo(rtos->target);
}if (priv->select_stackinfo) { ... } else {
LOG_ERROR("Can't find a way to get stacking info");
return ERROR_FAIL;
}else { ... }
}if (!stacking) { ... }
int ret = target_read_u16(rtos->target,
rtos->symbols[NX_SYM_TCB_INFO].address + offsetof(struct tcbinfo, regs_off),
&xcpreg_off);
if (ret != ERROR_OK) {
LOG_ERROR("Failed to read registers' offset: ret = %d", ret);
return ERROR_FAIL;
}if (ret != ERROR_OK) { ... }
ret = target_read_u32(rtos->target, thread_id + xcpreg_off, ®saddr);
if (ret != ERROR_OK) {
LOG_ERROR("Failed to read registers' address: ret = %d", ret);
return ERROR_FAIL;
}if (ret != ERROR_OK) { ... }
return rtos_generic_stack_read(rtos->target, stacking, regsaddr, reg_list, num_regs);
}{ ... }
static int nuttx_get_thread_reg_list(struct rtos *rtos, int64_t thread_id,
struct rtos_reg **reg_list, int *num_regs)
{
if (!rtos) {
LOG_ERROR("NUTTX: out of memory");
return ERROR_FAIL;
}if (!rtos) { ... }
if (thread_id == rtos->current_thread)
return nuttx_getreg_current_thread(rtos, reg_list, num_regs);
return nuttx_getregs_fromstack(rtos, thread_id, reg_list, num_regs);
}{ ... }
static int nuttx_get_symbol_list_to_lookup(struct symbol_table_elem *symbol_list[])
{
*symbol_list = calloc(ARRAY_SIZE(nuttx_symbol_list), sizeof(**symbol_list));
if (!*symbol_list) {
LOG_ERROR("NUTTX: out of memory");
return ERROR_FAIL;
}if (!*symbol_list) { ... }
for (unsigned int i = 0; i < ARRAY_SIZE(nuttx_symbol_list); i++) {
(*symbol_list)[i].symbol_name = nuttx_symbol_list[i].name;
(*symbol_list)[i].optional = nuttx_symbol_list[i].optional;
}for (unsigned int i = 0; i < ARRAY_SIZE(nuttx_symbol_list); i++) { ... }
return ERROR_OK;
}{ ... }
const struct rtos_type nuttx_rtos = {
.name = "nuttx",
.detect_rtos = nuttx_detect_rtos,
.create = nuttx_create,
.smp_init = nuttx_smp_init,
.update_threads = nuttx_update_threads,
.get_thread_reg_list = nuttx_get_thread_reg_list,
.get_symbol_list_to_lookup = nuttx_get_symbol_list_to_lookup,
...};