1
2
3
4
5
6
7
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
145
152
164
172
187
189
190
191
192
193
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
217
218
219
220
221
222
223
224
225
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
261
262
263
267
268
269
270
271
272
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
305
306
310
311
312
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
374
375
376
377
378
379
380
381
382
383
384
385
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <helper/time_support.h>
#include <jtag/jtag.h>
#include "target/target.h"
#include "target/register.h"
#include <target/smp.h>
#include "rtos.h"
#include "helper/log.h"
#include "helper/types.h"
#include "server/gdb_server.h"
9 includes
static bool hwthread_detect_rtos(struct target *target);
static int hwthread_create(struct target *target);
static int hwthread_update_threads(struct rtos *rtos);
static int hwthread_get_thread_reg(struct rtos *rtos, int64_t thread_id,
uint32_t reg_num, struct rtos_reg *rtos_reg);
static int hwthread_get_thread_reg_list(struct rtos *rtos, int64_t thread_id,
struct rtos_reg **reg_list, int *num_regs);
static int hwthread_get_symbol_list_to_lookup(struct symbol_table_elem *symbol_list[]);
static int hwthread_smp_init(struct target *target);
static int hwthread_set_reg(struct rtos *rtos, uint32_t reg_num, uint8_t *reg_value);
static int hwthread_read_buffer(struct rtos *rtos, target_addr_t address,
uint32_t size, uint8_t *buffer);
static int hwthread_write_buffer(struct rtos *rtos, target_addr_t address,
uint32_t size, const uint8_t *buffer);
#define HW_THREAD_NAME_STR_SIZE (32)
static inline threadid_t threadid_from_target(const struct target *target)
{
if (!target->smp)
return 1;
threadid_t threadid = 1;
struct target_list *head;
foreach_smp_target(head, target->smp_targets) {
if (target == head->target)
return threadid;
++threadid;
}foreach_smp_target (head, target->smp_targets) { ... }
assert(0 && "Target is not found in it's own SMP group!");
return -1;
}{ ... }
const struct rtos_type hwthread_rtos = {
.name = "hwthread",
.detect_rtos = hwthread_detect_rtos,
.create = hwthread_create,
.update_threads = hwthread_update_threads,
.get_thread_reg_list = hwthread_get_thread_reg_list,
.get_thread_reg = hwthread_get_thread_reg,
.get_symbol_list_to_lookup = hwthread_get_symbol_list_to_lookup,
.smp_init = hwthread_smp_init,
.set_reg = hwthread_set_reg,
.read_buffer = hwthread_read_buffer,
.write_buffer = hwthread_write_buffer,
...};
struct hwthread_params {
int dummy_param;
...};
static int hwthread_fill_thread(struct rtos *rtos, struct target *curr, int thread_num, threadid_t tid)
{
char tmp_str[HW_THREAD_NAME_STR_SIZE];
memset(tmp_str, 0, HW_THREAD_NAME_STR_SIZE);
rtos->thread_details[thread_num].threadid = tid;
rtos->thread_details[thread_num].exists = true;
rtos->thread_details[thread_num].thread_name_str = strdup(target_name(curr));
snprintf(tmp_str, HW_THREAD_NAME_STR_SIZE-1, "state: %s", debug_reason_name(curr));
rtos->thread_details[thread_num].extra_info_str = strdup(tmp_str);
return ERROR_OK;
}{ ... }
static int hwthread_update_threads(struct rtos *rtos)
{
int threads_found = 0;
int thread_list_size = 0;
struct target_list *head;
struct target *target;
int64_t current_thread = 0;
int64_t current_threadid = rtos->current_threadid;
enum target_debug_reason current_reason = DBG_REASON_UNDEFINED;
if (!rtos)
return -1;
target = rtos->target;
rtos_free_threadlist(rtos);
if (target->smp) {
foreach_smp_target(head, target->smp_targets) {
struct target *curr = head->target;
if (!target_was_examined(curr))
continue;
++thread_list_size;
}foreach_smp_target (head, target->smp_targets) { ... }
}if (target->smp) { ... } else
thread_list_size = 1;
/* ... */
if (current_threadid <= thread_list_size)
rtos->current_threadid = current_threadid;
else
LOG_WARNING("SMP node change, disconnect GDB from core/thread %" PRId64,
current_threadid);
rtos->thread_details = malloc(sizeof(struct thread_detail) * thread_list_size);
if (target->smp) {
foreach_smp_target(head, target->smp_targets) {
struct target *curr = head->target;
if (!target_was_examined(curr))
continue;
threadid_t tid = threadid_from_target(curr);
hwthread_fill_thread(rtos, curr, threads_found, tid);
switch (current_reason) {
case DBG_REASON_UNDEFINED:
current_reason = curr->debug_reason;
current_thread = tid;
break;case DBG_REASON_UNDEFINED:
case DBG_REASON_SINGLESTEP:
if (curr->debug_reason == DBG_REASON_SINGLESTEP) {
if (tid == rtos->current_threadid)
current_thread = tid;
}if (curr->debug_reason == DBG_REASON_SINGLESTEP) { ... }
break;case DBG_REASON_SINGLESTEP:
case DBG_REASON_BREAKPOINT:
if (curr->debug_reason == DBG_REASON_SINGLESTEP) {
current_reason = curr->debug_reason;
current_thread = tid;
}if (curr->debug_reason == DBG_REASON_SINGLESTEP) { ... } else
if (curr->debug_reason == DBG_REASON_BREAKPOINT) {
if (tid == rtos->current_threadid)
current_thread = tid;
}else if (curr->debug_reason == DBG_REASON_BREAKPOINT) { ... }
break;case DBG_REASON_BREAKPOINT:
case DBG_REASON_WATCHPOINT:
if (curr->debug_reason == DBG_REASON_SINGLESTEP ||
curr->debug_reason == DBG_REASON_BREAKPOINT) {
current_reason = curr->debug_reason;
current_thread = tid;
}if (curr->debug_reason == DBG_REASON_SINGLESTEP || curr->debug_reason == DBG_REASON_BREAKPOINT) { ... }
break;case DBG_REASON_WATCHPOINT:
case DBG_REASON_DBGRQ:
if (curr->debug_reason == DBG_REASON_SINGLESTEP ||
curr->debug_reason == DBG_REASON_WATCHPOINT ||
curr->debug_reason == DBG_REASON_BREAKPOINT) {
current_reason = curr->debug_reason;
current_thread = tid;
}if (curr->debug_reason == DBG_REASON_SINGLESTEP || curr->debug_reason == DBG_REASON_WATCHPOINT || curr->debug_reason == DBG_REASON_BREAKPOINT) { ... } else
if (curr->debug_reason == DBG_REASON_DBGRQ) {
if (tid == rtos->current_threadid)
current_thread = tid;
}else if (curr->debug_reason == DBG_REASON_DBGRQ) { ... }
break;
case DBG_REASON_DBGRQ:
default:
break;default
}switch (current_reason) { ... }
threads_found++;
}foreach_smp_target (head, target->smp_targets) { ... }
}if (target->smp) { ... } else {
current_thread = 1;
hwthread_fill_thread(rtos, target, threads_found, current_thread);
threads_found++;
}else { ... }
rtos->thread_count = threads_found;
if (current_thread != 0)
rtos->current_thread = current_thread;
else if (rtos->current_threadid != 0)
rtos->current_thread = rtos->current_threadid;
else
rtos->current_thread = threadid_from_target(target);
LOG_DEBUG("%s current_thread=%i", __func__, (int)rtos->current_thread);
return 0;
}{ ... }
static int hwthread_smp_init(struct target *target)
{
return hwthread_update_threads(target->rtos);
}{ ... }
static struct target *hwthread_find_thread(struct target *target, threadid_t thread_id)
{
if (!(target && target->smp))
return target;
struct target_list *head;
threadid_t tid = 1;
foreach_smp_target(head, target->smp_targets) {
if (thread_id == tid)
return head->target;
++tid;
}foreach_smp_target (head, target->smp_targets) { ... }
return NULL;
}{ ... }
static int hwthread_get_thread_reg_list(struct rtos *rtos, int64_t thread_id,
struct rtos_reg **rtos_reg_list, int *rtos_reg_list_size)
{
if (!rtos)
return ERROR_FAIL;
struct target *target = rtos->target;
struct target *curr = hwthread_find_thread(target, thread_id);
if (!curr)
return ERROR_FAIL;
if (!target_was_examined(curr))
return ERROR_FAIL;
int reg_list_size;
struct reg **reg_list;
int retval = target_get_gdb_reg_list(curr, ®_list, ®_list_size,
REG_CLASS_GENERAL);
if (retval != ERROR_OK)
return retval;
int j = 0;
for (int i = 0; i < reg_list_size; i++) {
if (!reg_list[i] || reg_list[i]->exist == false || reg_list[i]->hidden)
continue;
j++;
}for (int i = 0; i < reg_list_size; i++) { ... }
*rtos_reg_list_size = j;
*rtos_reg_list = calloc(*rtos_reg_list_size, sizeof(struct rtos_reg));
if (!*rtos_reg_list) {
free(reg_list);
return ERROR_FAIL;
}if (!*rtos_reg_list) { ... }
j = 0;
for (int i = 0; i < reg_list_size; i++) {
if (!reg_list[i] || reg_list[i]->exist == false || reg_list[i]->hidden)
continue;
if (!reg_list[i]->valid) {
retval = reg_list[i]->type->get(reg_list[i]);
if (retval != ERROR_OK) {
LOG_ERROR("Couldn't get register %s.", reg_list[i]->name);
free(reg_list);
free(*rtos_reg_list);
return retval;
}if (retval != ERROR_OK) { ... }
}if (!reg_list[i]->valid) { ... }
(*rtos_reg_list)[j].number = reg_list[i]->number;
(*rtos_reg_list)[j].size = reg_list[i]->size;
memcpy((*rtos_reg_list)[j].value, reg_list[i]->value,
DIV_ROUND_UP(reg_list[i]->size, 8));
j++;
}for (int i = 0; i < reg_list_size; i++) { ... }
free(reg_list);
return ERROR_OK;
}{ ... }
static int hwthread_get_thread_reg(struct rtos *rtos, int64_t thread_id,
uint32_t reg_num, struct rtos_reg *rtos_reg)
{
if (!rtos)
return ERROR_FAIL;
struct target *target = rtos->target;
struct target *curr = hwthread_find_thread(target, thread_id);
if (!curr) {
LOG_ERROR("Couldn't find RTOS thread for id %" PRId64 ".", thread_id);
return ERROR_FAIL;
}if (!curr) { ... }
if (!target_was_examined(curr)) {
LOG_TARGET_ERROR(curr, "Target hasn't been examined yet.");
return ERROR_FAIL;
}if (!target_was_examined(curr)) { ... }
struct reg *reg = register_get_by_number(curr->reg_cache, reg_num, true);
if (!reg) {
LOG_ERROR("Couldn't find register %" PRIu32 " in thread %" PRId64 ".", reg_num,
thread_id);
return ERROR_FAIL;
}if (!reg) { ... }
if (reg->type->get(reg) != ERROR_OK)
return ERROR_FAIL;
rtos_reg->number = reg->number;
rtos_reg->size = reg->size;
unsigned bytes = (reg->size + 7) / 8;
assert(bytes <= sizeof(rtos_reg->value));
memcpy(rtos_reg->value, reg->value, bytes);
return ERROR_OK;
}{ ... }
static int hwthread_set_reg(struct rtos *rtos, uint32_t reg_num, uint8_t *reg_value)
{
if (!rtos)
return ERROR_FAIL;
struct target *target = rtos->target;
struct target *curr = hwthread_find_thread(target, rtos->current_thread);
if (!curr)
return ERROR_FAIL;
struct reg *reg = register_get_by_number(curr->reg_cache, reg_num, true);
if (!reg)
return ERROR_FAIL;
return reg->type->set(reg, reg_value);
}{ ... }
static int hwthread_get_symbol_list_to_lookup(struct symbol_table_elem *symbol_list[])
{
*symbol_list = calloc(1, sizeof(struct symbol_table_elem));
(*symbol_list)[0].symbol_name = NULL;
return 0;
}{ ... }
static int hwthread_target_for_threadid(struct connection *connection, int64_t thread_id, struct target **p_target)
{
struct target *target = get_target_from_connection(connection);
struct target *curr = hwthread_find_thread(target, thread_id);
if (!curr)
return ERROR_FAIL;
*p_target = curr;
return ERROR_OK;
}{ ... }
static bool hwthread_detect_rtos(struct target *target)
{
return false;
}{ ... }
static int hwthread_thread_packet(struct connection *connection, const char *packet, int packet_size)
{
struct target *target = get_target_from_connection(connection);
struct target *curr = NULL;
int64_t current_threadid;
if (packet[0] == 'H' && packet[1] == 'g') {
sscanf(packet, "Hg%16" SCNx64, ¤t_threadid);
if (current_threadid > 0) {
if (hwthread_target_for_threadid(connection, current_threadid, &curr) != ERROR_OK) {
LOG_ERROR("hwthread: cannot find thread id %"PRId64, current_threadid);
gdb_put_packet(connection, "E01", 3);
return ERROR_FAIL;
}if (hwthread_target_for_threadid(connection, current_threadid, &curr) != ERROR_OK) { ... }
target->rtos->current_thread = current_threadid;
}if (current_threadid > 0) { ... } else if (current_threadid == 0 || current_threadid == -1) {
target->rtos->current_thread = threadid_from_target(target);
}else if (current_threadid == 0 || current_threadid == -1) { ... }
target->rtos->current_threadid = current_threadid;
gdb_put_packet(connection, "OK", 2);
return ERROR_OK;
}if (packet[0] == 'H' && packet[1] == 'g') { ... }
return rtos_thread_packet(connection, packet, packet_size);
}{ ... }
static int hwthread_create(struct target *target)
{
LOG_INFO("Hardware thread awareness created");
target->rtos->rtos_specific_params = NULL;
target->rtos->current_thread = 0;
target->rtos->thread_details = NULL;
target->rtos->gdb_target_for_threadid = hwthread_target_for_threadid;
target->rtos->gdb_thread_packet = hwthread_thread_packet;
return 0;
}{ ... }
static int hwthread_read_buffer(struct rtos *rtos, target_addr_t address,
uint32_t size, uint8_t *buffer)
{
if (!rtos)
return ERROR_FAIL;
struct target *target = rtos->target;
struct target *curr = hwthread_find_thread(target, rtos->current_thread);
if (!curr)
return ERROR_FAIL;
return target_read_buffer(curr, address, size, buffer);
}{ ... }
static int hwthread_write_buffer(struct rtos *rtos, target_addr_t address,
uint32_t size, const uint8_t *buffer)
{
if (!rtos)
return ERROR_FAIL;
struct target *target = rtos->target;
struct target *curr = hwthread_find_thread(target, rtos->current_thread);
if (!curr)
return ERROR_FAIL;
return target_write_buffer(curr, address, size, buffer);
}{ ... }