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.
 
 
 
 
 
 

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