You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

523 lines
15 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2011 by Broadcom Corporation *
  3. * Evan Hunter - ehunter@broadcom.com *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include "rtos.h"
  24. #include "target/target.h"
  25. #include "helper/log.h"
  26. #include "server/gdb_server.h"
  27. static void hex_to_str(char *dst, char *hex_src);
  28. /* RTOSs */
  29. extern struct rtos_type FreeRTOS_rtos;
  30. extern struct rtos_type ThreadX_rtos;
  31. extern struct rtos_type eCos_rtos;
  32. extern struct rtos_type Linux_os;
  33. static struct rtos_type *rtos_types[] = {
  34. &ThreadX_rtos,
  35. &FreeRTOS_rtos,
  36. &eCos_rtos,
  37. &Linux_os,
  38. NULL
  39. };
  40. int rtos_thread_packet(struct connection *connection, char *packet, int packet_size);
  41. int rtos_smp_init(struct target *target)
  42. {
  43. if (target->rtos->type->smp_init)
  44. return target->rtos->type->smp_init(target);
  45. return ERROR_TARGET_INIT_FAILED;
  46. }
  47. static int os_alloc(struct target *target, struct rtos_type *ostype)
  48. {
  49. struct rtos *os = target->rtos = calloc(1, sizeof(struct rtos));
  50. if (!os)
  51. return JIM_ERR;
  52. os->type = ostype;
  53. os->current_threadid = -1;
  54. os->current_thread = 0;
  55. os->symbols = NULL;
  56. os->target = target;
  57. /* RTOS drivers can override the packet handler in _create(). */
  58. os->gdb_thread_packet = rtos_thread_packet;
  59. return JIM_OK;
  60. }
  61. static void os_free(struct target *target)
  62. {
  63. if (!target->rtos)
  64. return;
  65. if (target->rtos->symbols)
  66. free(target->rtos->symbols);
  67. free(target->rtos);
  68. target->rtos = NULL;
  69. }
  70. static int os_alloc_create(struct target *target, struct rtos_type *ostype)
  71. {
  72. int ret = os_alloc(target, ostype);
  73. if (JIM_OK == ret) {
  74. ret = target->rtos->type->create(target);
  75. if (ret != JIM_OK)
  76. os_free(target);
  77. }
  78. return ret;
  79. }
  80. int rtos_create(Jim_GetOptInfo *goi, struct target *target)
  81. {
  82. int x;
  83. char *cp;
  84. struct Jim_Obj *res;
  85. if (!goi->isconfigure && goi->argc != 0) {
  86. Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "NO PARAMS");
  87. return JIM_ERR;
  88. }
  89. os_free(target);
  90. Jim_GetOpt_String(goi, &cp, NULL);
  91. if (0 == strcmp(cp, "auto")) {
  92. /* Auto detect tries to look up all symbols for each RTOS,
  93. * and runs the RTOS driver's _detect() function when GDB
  94. * finds all symbols for any RTOS. See rtos_qsymbol(). */
  95. target->rtos_auto_detect = true;
  96. /* rtos_qsymbol() will iterate over all RTOSes. Allocate
  97. * target->rtos here, and set it to the first RTOS type. */
  98. return os_alloc(target, rtos_types[0]);
  99. }
  100. for (x = 0; rtos_types[x]; x++)
  101. if (0 == strcmp(cp, rtos_types[x]->name))
  102. return os_alloc_create(target, rtos_types[x]);
  103. Jim_SetResultFormatted(goi->interp, "Unknown RTOS type %s, try one of: ", cp);
  104. res = Jim_GetResult(goi->interp);
  105. for (x = 0; rtos_types[x]; x++)
  106. Jim_AppendStrings(goi->interp, res, rtos_types[x]->name, ", ", NULL);
  107. Jim_AppendStrings(goi->interp, res, " or auto", NULL);
  108. return JIM_ERR;
  109. }
  110. int gdb_thread_packet(struct connection *connection, char *packet, int packet_size)
  111. {
  112. struct target *target = get_target_from_connection(connection);
  113. if (target->rtos == NULL)
  114. return rtos_thread_packet(connection, packet, packet_size); /* thread not
  115. *found*/
  116. return target->rtos->gdb_thread_packet(connection, packet, packet_size);
  117. }
  118. static char *next_symbol(struct rtos *os, char *cur_symbol, uint64_t cur_addr)
  119. {
  120. symbol_table_elem_t *s;
  121. if (!os->symbols)
  122. os->type->get_symbol_list_to_lookup(&os->symbols);
  123. if (!cur_symbol[0])
  124. return os->symbols[0].symbol_name;
  125. for (s = os->symbols; s->symbol_name; s++)
  126. if (!strcmp(s->symbol_name, cur_symbol)) {
  127. s->address = cur_addr;
  128. s++;
  129. return s->symbol_name;
  130. }
  131. return NULL;
  132. }
  133. /* rtos_qsymbol() processes and replies to all qSymbol packets from GDB.
  134. *
  135. * GDB sends a qSymbol:: packet (empty address, empty name) to notify
  136. * that it can now answer qSymbol::hexcodedname queries, to look up symbols.
  137. *
  138. * If the qSymbol packet has no address that means GDB did not find the
  139. * symbol, in which case auto-detect will move on to try the next RTOS.
  140. *
  141. * rtos_qsymbol() then calls the next_symbol() helper function, which
  142. * iterates over symbol names for the current RTOS until it finds the
  143. * symbol in the received GDB packet, and then returns the next entry
  144. * in the list of symbols.
  145. *
  146. * If GDB replied about the last symbol for the RTOS and the RTOS was
  147. * specified explicitly, then no further symbol lookup is done. When
  148. * auto-detecting, the RTOS driver _detect() function must return success.
  149. *
  150. * rtos_qsymbol() returns 1 if an RTOS has been detected, or 0 otherwise.
  151. */
  152. int rtos_qsymbol(struct connection *connection, char *packet, int packet_size)
  153. {
  154. int rtos_detected = 0;
  155. uint64_t addr;
  156. size_t reply_len;
  157. char reply[GDB_BUFFER_SIZE], cur_sym[GDB_BUFFER_SIZE / 2] = "", *next_sym;
  158. struct target *target = get_target_from_connection(connection);
  159. struct rtos *os = target->rtos;
  160. reply_len = sprintf(reply, "OK");
  161. if (sscanf(packet, "qSymbol:%" SCNx64 ":", &addr))
  162. hex_to_str(cur_sym, strchr(packet + 8, ':') + 1);
  163. else if (target->rtos_auto_detect && !rtos_try_next(target))
  164. goto done;
  165. next_sym = next_symbol(os, cur_sym, addr);
  166. if (!next_sym) {
  167. if (!target->rtos_auto_detect) {
  168. rtos_detected = 1;
  169. goto done;
  170. }
  171. if (os->type->detect_rtos(target)) {
  172. LOG_OUTPUT("Auto-detected RTOS: %s\r\n", os->type->name);
  173. rtos_detected = 1;
  174. goto done;
  175. }
  176. if (!rtos_try_next(target))
  177. goto done;
  178. os->type->get_symbol_list_to_lookup(&os->symbols);
  179. next_sym = os->symbols[0].symbol_name;
  180. if (!next_sym)
  181. goto done;
  182. }
  183. if (8 + (strlen(next_sym) * 2) + 1 > sizeof(reply)) {
  184. LOG_OUTPUT("ERROR: RTOS symbol '%s' name is too long for GDB!", next_sym);
  185. goto done;
  186. }
  187. reply_len = sprintf(reply, "qSymbol:");
  188. reply_len += str_to_hex(reply + reply_len, next_sym);
  189. done:
  190. gdb_put_packet(connection, reply, reply_len);
  191. return rtos_detected;
  192. }
  193. int rtos_thread_packet(struct connection *connection, char *packet, int packet_size)
  194. {
  195. struct target *target = get_target_from_connection(connection);
  196. if (strstr(packet, "qThreadExtraInfo,")) {
  197. if ((target->rtos != NULL) && (target->rtos->thread_details != NULL) &&
  198. (target->rtos->thread_count != 0)) {
  199. threadid_t threadid = 0;
  200. int found = -1;
  201. sscanf(packet, "qThreadExtraInfo,%" SCNx64, &threadid);
  202. if ((target->rtos != NULL) && (target->rtos->thread_details != NULL)) {
  203. int thread_num;
  204. for (thread_num = 0; thread_num < target->rtos->thread_count; thread_num++) {
  205. if (target->rtos->thread_details[thread_num].threadid == threadid) {
  206. if (target->rtos->thread_details[thread_num].exists)
  207. found = thread_num;
  208. }
  209. }
  210. }
  211. if (found == -1) {
  212. gdb_put_packet(connection, "E01", 3); /* thread not found */
  213. return ERROR_OK;
  214. }
  215. struct thread_detail *detail = &target->rtos->thread_details[found];
  216. int str_size = 0;
  217. if (detail->display_str != NULL)
  218. str_size += strlen(detail->display_str);
  219. if (detail->thread_name_str != NULL)
  220. str_size += strlen(detail->thread_name_str);
  221. if (detail->extra_info_str != NULL)
  222. str_size += strlen(detail->extra_info_str);
  223. char *tmp_str = (char *) malloc(str_size + 7);
  224. char *tmp_str_ptr = tmp_str;
  225. if (detail->display_str != NULL)
  226. tmp_str_ptr += sprintf(tmp_str_ptr, "%s", detail->display_str);
  227. if (detail->thread_name_str != NULL) {
  228. if (tmp_str_ptr != tmp_str)
  229. tmp_str_ptr += sprintf(tmp_str_ptr, " : ");
  230. tmp_str_ptr += sprintf(tmp_str_ptr, "%s", detail->thread_name_str);
  231. }
  232. if (detail->extra_info_str != NULL) {
  233. if (tmp_str_ptr != tmp_str)
  234. tmp_str_ptr += sprintf(tmp_str_ptr, " : ");
  235. tmp_str_ptr +=
  236. sprintf(tmp_str_ptr, " : %s", detail->extra_info_str);
  237. }
  238. assert(strlen(tmp_str) ==
  239. (size_t) (tmp_str_ptr - tmp_str));
  240. char *hex_str = (char *) malloc(strlen(tmp_str)*2 + 1);
  241. str_to_hex(hex_str, tmp_str);
  242. gdb_put_packet(connection, hex_str, strlen(hex_str));
  243. free(hex_str);
  244. free(tmp_str);
  245. return ERROR_OK;
  246. }
  247. gdb_put_packet(connection, "", 0);
  248. return ERROR_OK;
  249. } else if (strstr(packet, "qSymbol")) {
  250. if (rtos_qsymbol(connection, packet, packet_size) == 1) {
  251. target->rtos_auto_detect = false;
  252. target->rtos->type->create(target);
  253. target->rtos->type->update_threads(target->rtos);
  254. }
  255. return ERROR_OK;
  256. } else if (strstr(packet, "qfThreadInfo")) {
  257. int i;
  258. if ((target->rtos != NULL) && (target->rtos->thread_count != 0)) {
  259. char *out_str = (char *) malloc(17 * target->rtos->thread_count + 5);
  260. char *tmp_str = out_str;
  261. tmp_str += sprintf(tmp_str, "m");
  262. for (i = 0; i < target->rtos->thread_count; i++) {
  263. if (i != 0)
  264. tmp_str += sprintf(tmp_str, ",");
  265. tmp_str += sprintf(tmp_str, "%016" PRIx64,
  266. target->rtos->thread_details[i].threadid);
  267. }
  268. tmp_str[0] = 0;
  269. gdb_put_packet(connection, out_str, strlen(out_str));
  270. } else
  271. gdb_put_packet(connection, "", 0);
  272. return ERROR_OK;
  273. } else if (strstr(packet, "qsThreadInfo")) {
  274. gdb_put_packet(connection, "l", 1);
  275. return ERROR_OK;
  276. } else if (strstr(packet, "qAttached")) {
  277. gdb_put_packet(connection, "1", 1);
  278. return ERROR_OK;
  279. } else if (strstr(packet, "qOffsets")) {
  280. char offsets[] = "Text=0;Data=0;Bss=0";
  281. gdb_put_packet(connection, offsets, sizeof(offsets)-1);
  282. return ERROR_OK;
  283. } else if (strstr(packet, "qC")) {
  284. if (target->rtos != NULL) {
  285. char buffer[15];
  286. int size;
  287. size = snprintf(buffer, 15, "QC%08X", (int)target->rtos->current_thread);
  288. gdb_put_packet(connection, buffer, size);
  289. } else
  290. gdb_put_packet(connection, "QC0", 3);
  291. return ERROR_OK;
  292. } else if (packet[0] == 'T') { /* Is thread alive? */
  293. threadid_t threadid;
  294. int found = -1;
  295. sscanf(packet, "T%" SCNx64, &threadid);
  296. if ((target->rtos != NULL) && (target->rtos->thread_details != NULL)) {
  297. int thread_num;
  298. for (thread_num = 0; thread_num < target->rtos->thread_count; thread_num++) {
  299. if (target->rtos->thread_details[thread_num].threadid == threadid) {
  300. if (target->rtos->thread_details[thread_num].exists)
  301. found = thread_num;
  302. }
  303. }
  304. }
  305. if (found != -1)
  306. gdb_put_packet(connection, "OK", 2); /* thread alive */
  307. else
  308. gdb_put_packet(connection, "E01", 3); /* thread not found */
  309. return ERROR_OK;
  310. } else if (packet[0] == 'H') { /* Set current thread ( 'c' for step and continue, 'g' for
  311. * all other operations ) */
  312. if ((packet[1] == 'g') && (target->rtos != NULL))
  313. sscanf(packet, "Hg%16" SCNx64, &target->rtos->current_threadid);
  314. gdb_put_packet(connection, "OK", 2);
  315. return ERROR_OK;
  316. }
  317. return GDB_THREAD_PACKET_NOT_CONSUMED;
  318. }
  319. int rtos_get_gdb_reg_list(struct connection *connection)
  320. {
  321. struct target *target = get_target_from_connection(connection);
  322. int64_t current_threadid = target->rtos->current_threadid;
  323. if ((target->rtos != NULL) && (current_threadid != -1) &&
  324. (current_threadid != 0) &&
  325. ((current_threadid != target->rtos->current_thread) ||
  326. (target->smp))) { /* in smp several current thread are possible */
  327. char *hex_reg_list;
  328. target->rtos->type->get_thread_reg_list(target->rtos,
  329. current_threadid,
  330. &hex_reg_list);
  331. if (hex_reg_list != NULL) {
  332. gdb_put_packet(connection, hex_reg_list, strlen(hex_reg_list));
  333. free(hex_reg_list);
  334. return ERROR_OK;
  335. }
  336. }
  337. return ERROR_FAIL;
  338. }
  339. int rtos_generic_stack_read(struct target *target,
  340. const struct rtos_register_stacking *stacking,
  341. int64_t stack_ptr,
  342. char **hex_reg_list)
  343. {
  344. int list_size = 0;
  345. char *tmp_str_ptr;
  346. int64_t new_stack_ptr;
  347. int i;
  348. int retval;
  349. if (stack_ptr == 0) {
  350. LOG_OUTPUT("Error: null stack pointer in thread\r\n");
  351. return -5;
  352. }
  353. /* Read the stack */
  354. uint8_t *stack_data = (uint8_t *) malloc(stacking->stack_registers_size);
  355. uint32_t address = stack_ptr;
  356. if (stacking->stack_growth_direction == 1)
  357. address -= stacking->stack_registers_size;
  358. retval = target_read_buffer(target, address, stacking->stack_registers_size, stack_data);
  359. if (retval != ERROR_OK) {
  360. LOG_OUTPUT("Error reading stack frame from FreeRTOS thread\r\n");
  361. return retval;
  362. }
  363. #if 0
  364. LOG_OUTPUT("Stack Data :");
  365. for (i = 0; i < stacking->stack_registers_size; i++)
  366. LOG_OUTPUT("%02X", stack_data[i]);
  367. LOG_OUTPUT("\r\n");
  368. #endif
  369. for (i = 0; i < stacking->num_output_registers; i++)
  370. list_size += stacking->register_offsets[i].width_bits/8;
  371. *hex_reg_list = (char *)malloc(list_size*2 + 1);
  372. tmp_str_ptr = *hex_reg_list;
  373. new_stack_ptr = stack_ptr - stacking->stack_growth_direction *
  374. stacking->stack_registers_size;
  375. if (stacking->stack_alignment != 0) {
  376. /* Align new stack pointer to x byte boundary */
  377. new_stack_ptr =
  378. (new_stack_ptr & (~((int64_t) stacking->stack_alignment - 1))) +
  379. ((stacking->stack_growth_direction == -1) ? stacking->stack_alignment : 0);
  380. }
  381. for (i = 0; i < stacking->num_output_registers; i++) {
  382. int j;
  383. for (j = 0; j < stacking->register_offsets[i].width_bits/8; j++) {
  384. if (stacking->register_offsets[i].offset == -1)
  385. tmp_str_ptr += sprintf(tmp_str_ptr, "%02x", 0);
  386. else if (stacking->register_offsets[i].offset == -2)
  387. tmp_str_ptr += sprintf(tmp_str_ptr, "%02x",
  388. ((uint8_t *)&new_stack_ptr)[j]);
  389. else
  390. tmp_str_ptr += sprintf(tmp_str_ptr, "%02x",
  391. stack_data[stacking->register_offsets[i].offset + j]);
  392. }
  393. }
  394. /* LOG_OUTPUT("Output register string: %s\r\n", *hex_reg_list); */
  395. return ERROR_OK;
  396. }
  397. int rtos_try_next(struct target *target)
  398. {
  399. struct rtos *os = target->rtos;
  400. struct rtos_type **type = rtos_types;
  401. if (!os)
  402. return 0;
  403. while (*type && os->type != *type)
  404. type++;
  405. if (!*type || !*(++type))
  406. return 0;
  407. os->type = *type;
  408. if (os->symbols) {
  409. free(os->symbols);
  410. os->symbols = NULL;
  411. }
  412. return 1;
  413. }
  414. static void hex_to_str(char *dst, char *hex_src)
  415. {
  416. int src_pos = 0;
  417. int dst_pos = 0;
  418. while (hex_src[src_pos] != '\x00') {
  419. char hex_char = hex_src[src_pos];
  420. char hex_digit_val =
  421. (hex_char >=
  422. 'a') ? hex_char-'a'+
  423. 10 : (hex_char >= 'A') ? hex_char-'A'+10 : hex_char-'0';
  424. if (0 == (src_pos & 0x01)) {
  425. dst[dst_pos] = hex_digit_val;
  426. dst[dst_pos+1] = 0;
  427. } else {
  428. ((unsigned char *)dst)[dst_pos] <<= 4;
  429. ((unsigned char *)dst)[dst_pos] += hex_digit_val;
  430. dst_pos++;
  431. }
  432. src_pos++;
  433. }
  434. }
  435. int str_to_hex(char *hex_dst, char *src)
  436. {
  437. char *posptr = hex_dst;
  438. unsigned i;
  439. for (i = 0; i < strlen(src); i++)
  440. posptr += sprintf(posptr, "%02x", (unsigned char)src[i]);
  441. return posptr - hex_dst;
  442. }
  443. int rtos_update_threads(struct target *target)
  444. {
  445. if ((target->rtos != NULL) && (target->rtos->type != NULL))
  446. target->rtos->type->update_threads(target->rtos);
  447. return ERROR_OK;
  448. }