openocd: src/rtos: replace the GPL-2.0-or-later license tag
[openocd.git] / src / rtos / rtos.c
blob013080bb0caf46eaf5b86a8d21f9dc2b900115b7
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 /***************************************************************************
4 * Copyright (C) 2011 by Broadcom Corporation *
5 * Evan Hunter - ehunter@broadcom.com *
6 ***************************************************************************/
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
12 #include "rtos.h"
13 #include "target/target.h"
14 #include "helper/log.h"
15 #include "helper/binarybuffer.h"
16 #include "server/gdb_server.h"
18 /* RTOSs */
19 extern struct rtos_type freertos_rtos;
20 extern struct rtos_type threadx_rtos;
21 extern struct rtos_type ecos_rtos;
22 extern struct rtos_type linux_rtos;
23 extern struct rtos_type chibios_rtos;
24 extern struct rtos_type chromium_ec_rtos;
25 extern struct rtos_type embkernel_rtos;
26 extern struct rtos_type mqx_rtos;
27 extern struct rtos_type ucos_iii_rtos;
28 extern struct rtos_type nuttx_rtos;
29 extern struct rtos_type hwthread_rtos;
30 extern struct rtos_type riot_rtos;
31 extern struct rtos_type zephyr_rtos;
33 static struct rtos_type *rtos_types[] = {
34 &threadx_rtos,
35 &freertos_rtos,
36 &ecos_rtos,
37 &linux_rtos,
38 &chibios_rtos,
39 &chromium_ec_rtos,
40 &embkernel_rtos,
41 &mqx_rtos,
42 &ucos_iii_rtos,
43 &nuttx_rtos,
44 &riot_rtos,
45 &zephyr_rtos,
46 /* keep this as last, as it always matches with rtos auto */
47 &hwthread_rtos,
48 NULL
51 static int rtos_try_next(struct target *target);
53 int rtos_thread_packet(struct connection *connection, const char *packet, int packet_size);
55 int rtos_smp_init(struct target *target)
57 if (target->rtos->type->smp_init)
58 return target->rtos->type->smp_init(target);
59 return ERROR_TARGET_INIT_FAILED;
62 static int rtos_target_for_threadid(struct connection *connection, int64_t threadid, struct target **t)
64 struct target *curr = get_target_from_connection(connection);
65 if (t)
66 *t = curr;
68 return ERROR_OK;
71 static int os_alloc(struct target *target, struct rtos_type *ostype)
73 struct rtos *os = target->rtos = calloc(1, sizeof(struct rtos));
75 if (!os)
76 return JIM_ERR;
78 os->type = ostype;
79 os->current_threadid = -1;
80 os->current_thread = 0;
81 os->symbols = NULL;
82 os->target = target;
84 /* RTOS drivers can override the packet handler in _create(). */
85 os->gdb_thread_packet = rtos_thread_packet;
86 os->gdb_target_for_threadid = rtos_target_for_threadid;
88 return JIM_OK;
91 static void os_free(struct target *target)
93 if (!target->rtos)
94 return;
96 free(target->rtos->symbols);
97 free(target->rtos);
98 target->rtos = NULL;
101 static int os_alloc_create(struct target *target, struct rtos_type *ostype)
103 int ret = os_alloc(target, ostype);
105 if (ret == JIM_OK) {
106 ret = target->rtos->type->create(target);
107 if (ret != JIM_OK)
108 os_free(target);
111 return ret;
114 int rtos_create(struct jim_getopt_info *goi, struct target *target)
116 int x;
117 const char *cp;
118 Jim_Obj *res;
119 int e;
121 if (!goi->isconfigure && goi->argc != 0) {
122 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "NO PARAMS");
123 return JIM_ERR;
126 os_free(target);
128 e = jim_getopt_string(goi, &cp, NULL);
129 if (e != JIM_OK)
130 return e;
132 if (strcmp(cp, "auto") == 0) {
133 /* Auto detect tries to look up all symbols for each RTOS,
134 * and runs the RTOS driver's _detect() function when GDB
135 * finds all symbols for any RTOS. See rtos_qsymbol(). */
136 target->rtos_auto_detect = true;
138 /* rtos_qsymbol() will iterate over all RTOSes. Allocate
139 * target->rtos here, and set it to the first RTOS type. */
140 return os_alloc(target, rtos_types[0]);
143 for (x = 0; rtos_types[x]; x++)
144 if (strcmp(cp, rtos_types[x]->name) == 0)
145 return os_alloc_create(target, rtos_types[x]);
147 Jim_SetResultFormatted(goi->interp, "Unknown RTOS type %s, try one of: ", cp);
148 res = Jim_GetResult(goi->interp);
149 for (x = 0; rtos_types[x]; x++)
150 Jim_AppendStrings(goi->interp, res, rtos_types[x]->name, ", ", NULL);
151 Jim_AppendStrings(goi->interp, res, " or auto", NULL);
153 return JIM_ERR;
156 void rtos_destroy(struct target *target)
158 os_free(target);
161 int gdb_thread_packet(struct connection *connection, char const *packet, int packet_size)
163 struct target *target = get_target_from_connection(connection);
164 if (!target->rtos)
165 return rtos_thread_packet(connection, packet, packet_size); /* thread not
166 *found*/
167 return target->rtos->gdb_thread_packet(connection, packet, packet_size);
170 static struct symbol_table_elem *next_symbol(struct rtos *os, char *cur_symbol, uint64_t cur_addr)
172 struct symbol_table_elem *s;
174 if (!os->symbols)
175 os->type->get_symbol_list_to_lookup(&os->symbols);
177 if (!cur_symbol[0])
178 return &os->symbols[0];
180 for (s = os->symbols; s->symbol_name; s++)
181 if (!strcmp(s->symbol_name, cur_symbol)) {
182 s->address = cur_addr;
183 s++;
184 return s;
187 return NULL;
190 /* searches for 'symbol' in the lookup table for 'os' and returns TRUE,
191 * if 'symbol' is not declared optional */
192 static bool is_symbol_mandatory(const struct rtos *os, const char *symbol)
194 for (struct symbol_table_elem *s = os->symbols; s->symbol_name; ++s) {
195 if (!strcmp(s->symbol_name, symbol))
196 return !s->optional;
198 return false;
201 /* rtos_qsymbol() processes and replies to all qSymbol packets from GDB.
203 * GDB sends a qSymbol:: packet (empty address, empty name) to notify
204 * that it can now answer qSymbol::hexcodedname queries, to look up symbols.
206 * If the qSymbol packet has no address that means GDB did not find the
207 * symbol, in which case auto-detect will move on to try the next RTOS.
209 * rtos_qsymbol() then calls the next_symbol() helper function, which
210 * iterates over symbol names for the current RTOS until it finds the
211 * symbol in the received GDB packet, and then returns the next entry
212 * in the list of symbols.
214 * If GDB replied about the last symbol for the RTOS and the RTOS was
215 * specified explicitly, then no further symbol lookup is done. When
216 * auto-detecting, the RTOS driver _detect() function must return success.
218 * rtos_qsymbol() returns 1 if an RTOS has been detected, or 0 otherwise.
220 int rtos_qsymbol(struct connection *connection, char const *packet, int packet_size)
222 int rtos_detected = 0;
223 uint64_t addr = 0;
224 size_t reply_len;
225 char reply[GDB_BUFFER_SIZE + 1], cur_sym[GDB_BUFFER_SIZE / 2 + 1] = ""; /* Extra byte for null-termination */
226 struct symbol_table_elem *next_sym;
227 struct target *target = get_target_from_connection(connection);
228 struct rtos *os = target->rtos;
230 reply_len = sprintf(reply, "OK");
232 if (!os)
233 goto done;
235 /* Decode any symbol name in the packet*/
236 size_t len = unhexify((uint8_t *)cur_sym, strchr(packet + 8, ':') + 1, strlen(strchr(packet + 8, ':') + 1));
237 cur_sym[len] = 0;
239 if ((strcmp(packet, "qSymbol::") != 0) && /* GDB is not offering symbol lookup for the first time */
240 (!sscanf(packet, "qSymbol:%" SCNx64 ":", &addr)) && /* GDB did not find an address for a symbol */
241 is_symbol_mandatory(os, cur_sym)) { /* the symbol is mandatory for this RTOS */
243 /* GDB could not find an address for the previous symbol */
244 if (!target->rtos_auto_detect) {
245 LOG_WARNING("RTOS %s not detected. (GDB could not find symbol \'%s\')", os->type->name, cur_sym);
246 goto done;
247 } else {
248 /* Autodetecting RTOS - try next RTOS */
249 if (!rtos_try_next(target)) {
250 LOG_WARNING("No RTOS could be auto-detected!");
251 goto done;
254 /* Next RTOS selected - invalidate current symbol */
255 cur_sym[0] = '\x00';
259 LOG_DEBUG("RTOS: Address of symbol '%s' is 0x%" PRIx64, cur_sym, addr);
261 next_sym = next_symbol(os, cur_sym, addr);
263 /* Should never happen unless the debugger misbehaves */
264 if (!next_sym) {
265 LOG_WARNING("RTOS: Debugger sent us qSymbol with '%s' that we did not ask for", cur_sym);
266 goto done;
269 if (!next_sym->symbol_name) {
270 /* No more symbols need looking up */
272 if (!target->rtos_auto_detect) {
273 rtos_detected = 1;
274 goto done;
277 if (os->type->detect_rtos(target)) {
278 LOG_INFO("Auto-detected RTOS: %s", os->type->name);
279 rtos_detected = 1;
280 goto done;
281 } else {
282 LOG_WARNING("No RTOS could be auto-detected!");
283 goto done;
287 if (8 + (strlen(next_sym->symbol_name) * 2) + 1 > sizeof(reply)) {
288 LOG_ERROR("ERROR: RTOS symbol '%s' name is too long for GDB!", next_sym->symbol_name);
289 goto done;
292 LOG_DEBUG("RTOS: Requesting symbol lookup of '%s' from the debugger", next_sym->symbol_name);
294 reply_len = snprintf(reply, sizeof(reply), "qSymbol:");
295 reply_len += hexify(reply + reply_len,
296 (const uint8_t *)next_sym->symbol_name, strlen(next_sym->symbol_name),
297 sizeof(reply) - reply_len);
299 done:
300 gdb_put_packet(connection, reply, reply_len);
301 return rtos_detected;
304 int rtos_thread_packet(struct connection *connection, char const *packet, int packet_size)
306 struct target *target = get_target_from_connection(connection);
308 if (strncmp(packet, "qThreadExtraInfo,", 17) == 0) {
309 if ((target->rtos) && (target->rtos->thread_details) &&
310 (target->rtos->thread_count != 0)) {
311 threadid_t threadid = 0;
312 int found = -1;
313 sscanf(packet, "qThreadExtraInfo,%" SCNx64, &threadid);
315 if ((target->rtos) && (target->rtos->thread_details)) {
316 int thread_num;
317 for (thread_num = 0; thread_num < target->rtos->thread_count; thread_num++) {
318 if (target->rtos->thread_details[thread_num].threadid == threadid) {
319 if (target->rtos->thread_details[thread_num].exists)
320 found = thread_num;
324 if (found == -1) {
325 gdb_put_packet(connection, "E01", 3); /* thread not found */
326 return ERROR_OK;
329 struct thread_detail *detail = &target->rtos->thread_details[found];
331 int str_size = 0;
332 if (detail->thread_name_str)
333 str_size += strlen(detail->thread_name_str);
334 if (detail->extra_info_str)
335 str_size += strlen(detail->extra_info_str);
337 char *tmp_str = calloc(str_size + 9, sizeof(char));
338 char *tmp_str_ptr = tmp_str;
340 if (detail->thread_name_str)
341 tmp_str_ptr += sprintf(tmp_str_ptr, "Name: %s", detail->thread_name_str);
342 if (detail->extra_info_str) {
343 if (tmp_str_ptr != tmp_str)
344 tmp_str_ptr += sprintf(tmp_str_ptr, ", ");
345 tmp_str_ptr += sprintf(tmp_str_ptr, "%s", detail->extra_info_str);
348 assert(strlen(tmp_str) ==
349 (size_t) (tmp_str_ptr - tmp_str));
351 char *hex_str = malloc(strlen(tmp_str) * 2 + 1);
352 size_t pkt_len = hexify(hex_str, (const uint8_t *)tmp_str,
353 strlen(tmp_str), strlen(tmp_str) * 2 + 1);
355 gdb_put_packet(connection, hex_str, pkt_len);
356 free(hex_str);
357 free(tmp_str);
358 return ERROR_OK;
361 gdb_put_packet(connection, "", 0);
362 return ERROR_OK;
363 } else if (strncmp(packet, "qSymbol", 7) == 0) {
364 if (rtos_qsymbol(connection, packet, packet_size) == 1) {
365 if (target->rtos_auto_detect == true) {
366 target->rtos_auto_detect = false;
367 target->rtos->type->create(target);
369 target->rtos->type->update_threads(target->rtos);
371 return ERROR_OK;
372 } else if (strncmp(packet, "qfThreadInfo", 12) == 0) {
373 int i;
374 if (target->rtos) {
375 if (target->rtos->thread_count == 0) {
376 gdb_put_packet(connection, "l", 1);
377 } else {
378 /*thread id are 16 char +1 for ',' */
379 char *out_str = malloc(17 * target->rtos->thread_count + 1);
380 char *tmp_str = out_str;
381 for (i = 0; i < target->rtos->thread_count; i++) {
382 tmp_str += sprintf(tmp_str, "%c%016" PRIx64, i == 0 ? 'm' : ',',
383 target->rtos->thread_details[i].threadid);
385 gdb_put_packet(connection, out_str, strlen(out_str));
386 free(out_str);
388 } else
389 gdb_put_packet(connection, "l", 1);
391 return ERROR_OK;
392 } else if (strncmp(packet, "qsThreadInfo", 12) == 0) {
393 gdb_put_packet(connection, "l", 1);
394 return ERROR_OK;
395 } else if (strncmp(packet, "qAttached", 9) == 0) {
396 gdb_put_packet(connection, "1", 1);
397 return ERROR_OK;
398 } else if (strncmp(packet, "qOffsets", 8) == 0) {
399 char offsets[] = "Text=0;Data=0;Bss=0";
400 gdb_put_packet(connection, offsets, sizeof(offsets)-1);
401 return ERROR_OK;
402 } else if (strncmp(packet, "qCRC:", 5) == 0) {
403 /* make sure we check this before "qC" packet below
404 * otherwise it gets incorrectly handled */
405 return GDB_THREAD_PACKET_NOT_CONSUMED;
406 } else if (strncmp(packet, "qC", 2) == 0) {
407 if (target->rtos) {
408 char buffer[19];
409 int size;
410 size = snprintf(buffer, 19, "QC%016" PRIx64, target->rtos->current_thread);
411 gdb_put_packet(connection, buffer, size);
412 } else
413 gdb_put_packet(connection, "QC0", 3);
414 return ERROR_OK;
415 } else if (packet[0] == 'T') { /* Is thread alive? */
416 threadid_t threadid;
417 int found = -1;
418 sscanf(packet, "T%" SCNx64, &threadid);
419 if ((target->rtos) && (target->rtos->thread_details)) {
420 int thread_num;
421 for (thread_num = 0; thread_num < target->rtos->thread_count; thread_num++) {
422 if (target->rtos->thread_details[thread_num].threadid == threadid) {
423 if (target->rtos->thread_details[thread_num].exists)
424 found = thread_num;
428 if (found != -1)
429 gdb_put_packet(connection, "OK", 2); /* thread alive */
430 else
431 gdb_put_packet(connection, "E01", 3); /* thread not found */
432 return ERROR_OK;
433 } else if (packet[0] == 'H') { /* Set current thread ( 'c' for step and continue, 'g' for
434 * all other operations ) */
435 if ((packet[1] == 'g') && (target->rtos)) {
436 threadid_t threadid;
437 sscanf(packet, "Hg%16" SCNx64, &threadid);
438 LOG_DEBUG("RTOS: GDB requested to set current thread to 0x%" PRIx64, threadid);
439 /* threadid of 0 indicates target should choose */
440 if (threadid == 0)
441 target->rtos->current_threadid = target->rtos->current_thread;
442 else
443 target->rtos->current_threadid = threadid;
445 gdb_put_packet(connection, "OK", 2);
446 return ERROR_OK;
449 return GDB_THREAD_PACKET_NOT_CONSUMED;
452 static int rtos_put_gdb_reg_list(struct connection *connection,
453 struct rtos_reg *reg_list, int num_regs)
455 size_t num_bytes = 1; /* NUL */
456 for (int i = 0; i < num_regs; ++i)
457 num_bytes += DIV_ROUND_UP(reg_list[i].size, 8) * 2;
459 char *hex = malloc(num_bytes);
460 char *hex_p = hex;
462 for (int i = 0; i < num_regs; ++i) {
463 size_t count = DIV_ROUND_UP(reg_list[i].size, 8);
464 size_t n = hexify(hex_p, reg_list[i].value, count, num_bytes);
465 hex_p += n;
466 num_bytes -= n;
469 gdb_put_packet(connection, hex, strlen(hex));
470 free(hex);
472 return ERROR_OK;
475 /** Look through all registers to find this register. */
476 int rtos_get_gdb_reg(struct connection *connection, int reg_num)
478 struct target *target = get_target_from_connection(connection);
479 int64_t current_threadid = target->rtos->current_threadid;
480 if ((target->rtos) && (current_threadid != -1) &&
481 (current_threadid != 0) &&
482 ((current_threadid != target->rtos->current_thread) ||
483 (target->smp))) { /* in smp several current thread are possible */
484 struct rtos_reg *reg_list;
485 int num_regs;
487 LOG_DEBUG("getting register %d for thread 0x%" PRIx64
488 ", target->rtos->current_thread=0x%" PRIx64,
489 reg_num,
490 current_threadid,
491 target->rtos->current_thread);
493 int retval;
494 if (target->rtos->type->get_thread_reg) {
495 reg_list = calloc(1, sizeof(*reg_list));
496 num_regs = 1;
497 retval = target->rtos->type->get_thread_reg(target->rtos,
498 current_threadid, reg_num, &reg_list[0]);
499 if (retval != ERROR_OK) {
500 LOG_ERROR("RTOS: failed to get register %d", reg_num);
501 return retval;
503 } else {
504 retval = target->rtos->type->get_thread_reg_list(target->rtos,
505 current_threadid,
506 &reg_list,
507 &num_regs);
508 if (retval != ERROR_OK) {
509 LOG_ERROR("RTOS: failed to get register list");
510 return retval;
514 for (int i = 0; i < num_regs; ++i) {
515 if (reg_list[i].number == (uint32_t)reg_num) {
516 rtos_put_gdb_reg_list(connection, reg_list + i, 1);
517 free(reg_list);
518 return ERROR_OK;
522 free(reg_list);
524 return ERROR_FAIL;
527 /** Return a list of general registers. */
528 int rtos_get_gdb_reg_list(struct connection *connection)
530 struct target *target = get_target_from_connection(connection);
531 int64_t current_threadid = target->rtos->current_threadid;
532 if ((target->rtos) && (current_threadid != -1) &&
533 (current_threadid != 0) &&
534 ((current_threadid != target->rtos->current_thread) ||
535 (target->smp))) { /* in smp several current thread are possible */
536 struct rtos_reg *reg_list;
537 int num_regs;
539 LOG_DEBUG("RTOS: getting register list for thread 0x%" PRIx64
540 ", target->rtos->current_thread=0x%" PRIx64 "\r\n",
541 current_threadid,
542 target->rtos->current_thread);
544 int retval = target->rtos->type->get_thread_reg_list(target->rtos,
545 current_threadid,
546 &reg_list,
547 &num_regs);
548 if (retval != ERROR_OK) {
549 LOG_ERROR("RTOS: failed to get register list");
550 return retval;
553 rtos_put_gdb_reg_list(connection, reg_list, num_regs);
554 free(reg_list);
556 return ERROR_OK;
558 return ERROR_FAIL;
561 int rtos_set_reg(struct connection *connection, int reg_num,
562 uint8_t *reg_value)
564 struct target *target = get_target_from_connection(connection);
565 int64_t current_threadid = target->rtos->current_threadid;
566 if ((target->rtos) &&
567 (target->rtos->type->set_reg) &&
568 (current_threadid != -1) &&
569 (current_threadid != 0)) {
570 return target->rtos->type->set_reg(target->rtos, reg_num, reg_value);
572 return ERROR_FAIL;
575 int rtos_generic_stack_read(struct target *target,
576 const struct rtos_register_stacking *stacking,
577 int64_t stack_ptr,
578 struct rtos_reg **reg_list,
579 int *num_regs)
581 int retval;
583 if (stack_ptr == 0) {
584 LOG_ERROR("Error: null stack pointer in thread");
585 return -5;
587 /* Read the stack */
588 uint8_t *stack_data = malloc(stacking->stack_registers_size);
589 uint32_t address = stack_ptr;
591 if (stacking->stack_growth_direction == 1)
592 address -= stacking->stack_registers_size;
593 retval = target_read_buffer(target, address, stacking->stack_registers_size, stack_data);
594 if (retval != ERROR_OK) {
595 free(stack_data);
596 LOG_ERROR("Error reading stack frame from thread");
597 return retval;
599 LOG_DEBUG("RTOS: Read stack frame at 0x%" PRIx32, address);
601 #if 0
602 LOG_OUTPUT("Stack Data :");
603 for (i = 0; i < stacking->stack_registers_size; i++)
604 LOG_OUTPUT("%02X", stack_data[i]);
605 LOG_OUTPUT("\r\n");
606 #endif
608 target_addr_t new_stack_ptr;
609 if (stacking->calculate_process_stack) {
610 new_stack_ptr = stacking->calculate_process_stack(target,
611 stack_data, stacking, stack_ptr);
612 } else {
613 new_stack_ptr = stack_ptr - stacking->stack_growth_direction *
614 stacking->stack_registers_size;
617 *reg_list = calloc(stacking->num_output_registers, sizeof(struct rtos_reg));
618 *num_regs = stacking->num_output_registers;
620 for (int i = 0; i < stacking->num_output_registers; ++i) {
621 (*reg_list)[i].number = stacking->register_offsets[i].number;
622 (*reg_list)[i].size = stacking->register_offsets[i].width_bits;
624 int offset = stacking->register_offsets[i].offset;
625 if (offset == -2)
626 buf_cpy(&new_stack_ptr, (*reg_list)[i].value, (*reg_list)[i].size);
627 else if (offset != -1)
628 buf_cpy(stack_data + offset, (*reg_list)[i].value, (*reg_list)[i].size);
631 free(stack_data);
632 /* LOG_OUTPUT("Output register string: %s\r\n", *hex_reg_list); */
633 return ERROR_OK;
636 static int rtos_try_next(struct target *target)
638 struct rtos *os = target->rtos;
639 struct rtos_type **type = rtos_types;
641 if (!os)
642 return 0;
644 while (*type && os->type != *type)
645 type++;
647 if (!*type || !*(++type))
648 return 0;
650 os->type = *type;
652 free(os->symbols);
653 os->symbols = NULL;
655 return 1;
658 int rtos_update_threads(struct target *target)
660 if ((target->rtos) && (target->rtos->type))
661 target->rtos->type->update_threads(target->rtos);
662 return ERROR_OK;
665 void rtos_free_threadlist(struct rtos *rtos)
667 if (rtos->thread_details) {
668 int j;
670 for (j = 0; j < rtos->thread_count; j++) {
671 struct thread_detail *current_thread = &rtos->thread_details[j];
672 free(current_thread->thread_name_str);
673 free(current_thread->extra_info_str);
675 free(rtos->thread_details);
676 rtos->thread_details = NULL;
677 rtos->thread_count = 0;
678 rtos->current_threadid = -1;
679 rtos->current_thread = 0;
683 int rtos_read_buffer(struct target *target, target_addr_t address,
684 uint32_t size, uint8_t *buffer)
686 if (target->rtos->type->read_buffer)
687 return target->rtos->type->read_buffer(target->rtos, address, size, buffer);
688 return ERROR_NOT_IMPLEMENTED;
691 int rtos_write_buffer(struct target *target, target_addr_t address,
692 uint32_t size, const uint8_t *buffer)
694 if (target->rtos->type->write_buffer)
695 return target->rtos->type->write_buffer(target->rtos, address, size, buffer);
696 return ERROR_NOT_IMPLEMENTED;