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.
 
 
 
 
 
 

669 lines
17 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 int64_t current_threadid = -1;
  28. static void hex_to_str( char* dst, char * hex_src );
  29. static int str_to_hex( char* hex_dst, char* src );
  30. /* RTOSs */
  31. extern struct rtos_type FreeRTOS_rtos;
  32. extern struct rtos_type ThreadX_rtos;
  33. extern struct rtos_type eCos_rtos;
  34. static struct rtos_type *rtos_types[] =
  35. {
  36. &ThreadX_rtos,
  37. &FreeRTOS_rtos,
  38. &eCos_rtos,
  39. NULL
  40. };
  41. int rtos_create(Jim_GetOptInfo *goi, struct target * target)
  42. {
  43. int x;
  44. char *cp;
  45. if (! goi->isconfigure) {
  46. if (goi->argc != 0) {
  47. if (goi->argc != 0) {
  48. Jim_WrongNumArgs(goi->interp,
  49. goi->argc, goi->argv,
  50. "NO PARAMS");
  51. return JIM_ERR;
  52. }
  53. Jim_SetResultString(goi->interp,
  54. target_type_name(target), -1);
  55. }
  56. }
  57. if (target->rtos) {
  58. free((void *)(target->rtos));
  59. }
  60. // e = Jim_GetOpt_String(goi, &cp, NULL);
  61. // target->rtos = strdup(cp);
  62. Jim_GetOpt_String(goi, &cp, NULL);
  63. /* now does target type exist */
  64. if ( 0 == strcmp( cp, "auto") )
  65. {
  66. // auto detection of RTOS
  67. target->rtos_auto_detect = true;
  68. x = 0;
  69. }
  70. else
  71. {
  72. for (x = 0 ; rtos_types[x] ; x++) {
  73. if (0 == strcmp(cp, rtos_types[x]->name)) {
  74. /* found */
  75. break;
  76. }
  77. }
  78. if (rtos_types[x] == NULL) {
  79. Jim_SetResultFormatted(goi->interp, "Unknown rtos type %s, try one of ", cp);
  80. for (x = 0 ; rtos_types[x] ; x++) {
  81. if (rtos_types[x + 1]) {
  82. Jim_AppendStrings(goi->interp,
  83. Jim_GetResult(goi->interp),
  84. rtos_types[x]->name,
  85. ", ", NULL);
  86. } else {
  87. Jim_AppendStrings(goi->interp,
  88. Jim_GetResult(goi->interp),
  89. " or ",
  90. rtos_types[x]->name,NULL);
  91. }
  92. }
  93. return JIM_ERR;
  94. }
  95. }
  96. /* Create it */
  97. target->rtos = calloc(1,sizeof(struct rtos));
  98. target->rtos->type = rtos_types[x];
  99. target->rtos->current_thread = 0;
  100. target->rtos->symbols = NULL;
  101. target->rtos->target = target;
  102. if ( 0 != strcmp( cp, "auto") )
  103. {
  104. target->rtos->type->create( target );
  105. }
  106. return JIM_OK;
  107. }
  108. int gdb_thread_packet(struct connection *connection, struct target *target, char *packet, int packet_size)
  109. {
  110. if (strstr(packet, "qP"))
  111. {
  112. #define TAG_THREADID 1 /* Echo the thread identifier */
  113. #define TAG_EXISTS 2 /* Is this process defined enough to
  114. fetch registers and its stack */
  115. #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
  116. #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is */
  117. #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about */
  118. // TODO: need to scanf the mode variable (or it with the tags), and the threadid
  119. unsigned long mode;
  120. threadid_t threadid = 0;
  121. struct thread_detail* detail;
  122. sscanf(packet, "qP%8lx%16" SCNx64, &mode, &threadid);
  123. int found = -1;
  124. if ((target->rtos != NULL) && (target->rtos->thread_details
  125. != NULL)) {
  126. int thread_num;
  127. for (thread_num = 0; thread_num
  128. < target->rtos->thread_count; thread_num++) {
  129. if (target->rtos->thread_details[thread_num].threadid
  130. == threadid) {
  131. if (target->rtos->thread_details[thread_num].exists) {
  132. found = thread_num;
  133. }
  134. }
  135. }
  136. }
  137. if (found == -1) {
  138. gdb_put_packet(connection, "E01", 3); // thread not found
  139. return ERROR_OK;
  140. }
  141. detail = &target->rtos->thread_details[found];
  142. if ( detail->display_str != NULL )
  143. {
  144. mode &= TAG_DISPLAY;
  145. }
  146. if ( detail->thread_name_str != NULL )
  147. {
  148. mode &= TAG_THREADNAME;
  149. }
  150. if ( detail->extra_info_str != NULL )
  151. {
  152. mode &= TAG_MOREDISPLAY;
  153. }
  154. mode &= TAG_THREADID | TAG_EXISTS;
  155. char thread_str[1000];
  156. sprintf(thread_str, "%08lx", mode);
  157. sprintf(thread_str, "%016" PRIx64, threadid);
  158. if (mode & TAG_THREADID) {
  159. sprintf(thread_str, "%08" PRIx32 "10%016" PRIx64, TAG_THREADID, threadid);
  160. }
  161. if (mode & TAG_EXISTS) {
  162. sprintf(thread_str, "%08" PRIx32 "08%08" PRIx32, TAG_EXISTS, (detail->exists==true)?1:0);
  163. }
  164. if (mode & TAG_DISPLAY) {
  165. sprintf(thread_str, "%08" PRIx32 "%02x%s", TAG_DISPLAY, (unsigned char)strlen(detail->display_str), detail->display_str );
  166. }
  167. if (mode & TAG_MOREDISPLAY) {
  168. sprintf(thread_str, "%08" PRIx32 "%02x%s", TAG_MOREDISPLAY, (unsigned char)strlen(detail->extra_info_str), detail->extra_info_str );
  169. }
  170. if (mode & TAG_THREADNAME) {
  171. sprintf(thread_str, "%08" PRIx32 "%02x%s", TAG_THREADNAME, (unsigned char)strlen(detail->thread_name_str), detail->thread_name_str );
  172. }
  173. //gdb_put_packet(connection, tmpstr, sizeof(tmpstr)-1);
  174. gdb_put_packet(connection, thread_str, strlen(thread_str));
  175. // gdb_put_packet(connection, "", 0);
  176. // gdb_put_packet(connection, "OK", 2); // all threads alive
  177. return ERROR_OK;
  178. }
  179. else if (strstr(packet, "qThreadExtraInfo,"))
  180. {
  181. if ((target->rtos != NULL) && (target->rtos->thread_details != NULL) && (target->rtos->thread_count != 0))
  182. {
  183. threadid_t threadid = 0;
  184. int found = -1;
  185. sscanf(packet, "qThreadExtraInfo,%" SCNx64, &threadid );
  186. if ((target->rtos != NULL) && (target->rtos->thread_details
  187. != NULL)) {
  188. int thread_num;
  189. for (thread_num = 0; thread_num
  190. < target->rtos->thread_count; thread_num++) {
  191. if (target->rtos->thread_details[thread_num].threadid
  192. == threadid) {
  193. if (target->rtos->thread_details[thread_num].exists) {
  194. found = thread_num;
  195. }
  196. }
  197. }
  198. }
  199. if (found == -1) {
  200. gdb_put_packet(connection, "E01", 3); // thread not found
  201. return ERROR_OK;
  202. }
  203. struct thread_detail* detail = &target->rtos->thread_details[found];
  204. int str_size = 0;
  205. if ( detail->display_str != NULL )
  206. {
  207. str_size += strlen(detail->display_str);
  208. }
  209. if ( detail->thread_name_str != NULL )
  210. {
  211. str_size += strlen(detail->thread_name_str);
  212. }
  213. if ( detail->extra_info_str != NULL )
  214. {
  215. str_size += strlen(detail->extra_info_str);
  216. }
  217. char * tmp_str = (char*) malloc( str_size + 7 );
  218. char* tmp_str_ptr = tmp_str;
  219. if ( detail->display_str != NULL )
  220. {
  221. tmp_str_ptr += sprintf( tmp_str_ptr, "%s", detail->display_str );
  222. }
  223. if ( detail->thread_name_str != NULL )
  224. {
  225. if ( tmp_str_ptr != tmp_str )
  226. {
  227. tmp_str_ptr += sprintf( tmp_str_ptr, " : " );
  228. }
  229. tmp_str_ptr += sprintf( tmp_str_ptr, "%s", detail->thread_name_str );
  230. }
  231. if ( detail->extra_info_str != NULL )
  232. {
  233. if ( tmp_str_ptr != tmp_str )
  234. {
  235. tmp_str_ptr += sprintf( tmp_str_ptr, " : " );
  236. }
  237. tmp_str_ptr += sprintf( tmp_str_ptr, " : %s", detail->extra_info_str );
  238. }
  239. char * hex_str = (char*) malloc( strlen(tmp_str)*2 +1 );
  240. str_to_hex( hex_str, tmp_str );
  241. gdb_put_packet(connection, hex_str, strlen(hex_str));
  242. free(hex_str);
  243. free(tmp_str);
  244. return ERROR_OK;
  245. }
  246. gdb_put_packet(connection, "", 0);
  247. return ERROR_OK;
  248. }
  249. else if (strstr(packet, "qSymbol"))
  250. {
  251. if ( target->rtos != NULL )
  252. {
  253. int next_symbol_num = -1;
  254. if (target->rtos->symbols == NULL)
  255. {
  256. target->rtos->type->get_symbol_list_to_lookup( &target->rtos->symbols );
  257. }
  258. if (0 == strcmp( "qSymbol::", packet ) )
  259. {
  260. // first query -
  261. next_symbol_num = 0;
  262. }
  263. else
  264. {
  265. int64_t value = 0;
  266. char * hex_name_str = malloc( strlen(packet));
  267. char * name_str;
  268. int symbol_num;
  269. char* found = strstr( packet, "qSymbol::" );
  270. if (0 == found )
  271. {
  272. sscanf(packet, "qSymbol:%" SCNx64 ":%s", &value, hex_name_str);
  273. }
  274. else
  275. {
  276. // No value returned by GDB - symbol was not found
  277. sscanf(packet, "qSymbol::%s", hex_name_str);
  278. }
  279. name_str = (char*) malloc( 1+ strlen(hex_name_str) / 2 );
  280. hex_to_str( name_str, hex_name_str );
  281. symbol_num = 0;
  282. while ( ( target->rtos->symbols[ symbol_num ].symbol_name != NULL ) && ( 0 != strcmp( target->rtos->symbols[ symbol_num ].symbol_name, name_str ) ) )
  283. {
  284. symbol_num++;
  285. }
  286. if ( target->rtos->symbols[ symbol_num ].symbol_name == NULL )
  287. {
  288. LOG_OUTPUT("ERROR: unknown symbol\r\n");
  289. gdb_put_packet(connection, "OK", 2);
  290. return ERROR_OK;
  291. }
  292. target->rtos->symbols[ symbol_num ].address = value;
  293. next_symbol_num = symbol_num+1;
  294. free( hex_name_str );
  295. free( name_str );
  296. }
  297. int symbols_done = 0;
  298. if ( target->rtos->symbols[ next_symbol_num ].symbol_name == NULL )
  299. {
  300. if ( ( target->rtos_auto_detect == false ) ||
  301. ( 1 == target->rtos->type->detect_rtos( target ) ) )
  302. {
  303. // Found correct RTOS or not autodetecting
  304. if ( target->rtos_auto_detect == true )
  305. {
  306. LOG_OUTPUT( "Auto-detected RTOS: %s\r\n",target->rtos->type->name );
  307. }
  308. symbols_done = 1;
  309. }
  310. else
  311. {
  312. // Auto detecting RTOS and currently not found
  313. if( 1 != rtos_try_next( target ) )
  314. {
  315. // No more RTOS's to try
  316. symbols_done = 1;
  317. }
  318. else
  319. {
  320. next_symbol_num = 0;
  321. target->rtos->type->get_symbol_list_to_lookup( &target->rtos->symbols );
  322. }
  323. }
  324. }
  325. if ( symbols_done == 1 )
  326. {
  327. target->rtos_auto_detect = false;
  328. target->rtos->type->create( target );
  329. target->rtos->type->update_threads(target->rtos);
  330. // No more symbols needed
  331. gdb_put_packet(connection, "OK", 2);
  332. return ERROR_OK;
  333. }
  334. else
  335. {
  336. char* symname = target->rtos->symbols[ next_symbol_num ].symbol_name;
  337. char qsymstr[] = "qSymbol:";
  338. char * opstring = (char*)malloc(sizeof(qsymstr)+strlen(symname)*2+1);
  339. char * posptr = opstring;
  340. posptr += sprintf( posptr, "%s", qsymstr );
  341. str_to_hex( posptr, symname );
  342. gdb_put_packet(connection, opstring, strlen(opstring));
  343. free(opstring);
  344. return ERROR_OK;
  345. }
  346. }
  347. gdb_put_packet(connection, "OK", 2);
  348. return ERROR_OK;
  349. }
  350. else if (strstr(packet, "qfThreadInfo"))
  351. {
  352. int i;
  353. if ( ( target->rtos != NULL ) && ( target->rtos->thread_count != 0 ) )
  354. {
  355. char* out_str = (char*) malloc(17 * target->rtos->thread_count + 5);
  356. char* tmp_str = out_str;
  357. tmp_str += sprintf(tmp_str, "m");
  358. for (i = 0; i < target->rtos->thread_count; i++) {
  359. if (i != 0) {
  360. tmp_str += sprintf(tmp_str, ",");
  361. }
  362. tmp_str += sprintf(tmp_str, "%016" PRIx64,
  363. target->rtos->thread_details[i].threadid);
  364. }
  365. tmp_str[0] = 0;
  366. gdb_put_packet(connection, out_str, strlen(out_str));
  367. }
  368. else
  369. {
  370. gdb_put_packet(connection, "", 0);
  371. }
  372. return ERROR_OK;
  373. }
  374. else if (strstr(packet, "qsThreadInfo"))
  375. {
  376. gdb_put_packet(connection, "l", 1);
  377. return ERROR_OK;
  378. }
  379. else if (strstr(packet, "qAttached"))
  380. {
  381. gdb_put_packet(connection, "1", 1);
  382. return ERROR_OK;
  383. }
  384. else if (strstr(packet, "qOffsets"))
  385. {
  386. char offsets[] = "Text=0;Data=0;Bss=0";
  387. gdb_put_packet(connection, offsets, sizeof(offsets)-1);
  388. return ERROR_OK;
  389. }
  390. else if (strstr(packet, "qC"))
  391. {
  392. if( target->rtos!=NULL )
  393. {
  394. char buffer[15];
  395. int size;
  396. size = snprintf(buffer, 15, "QC%08X", (int)target->rtos->current_thread);
  397. gdb_put_packet(connection, buffer, size);
  398. }
  399. else
  400. {
  401. gdb_put_packet(connection, "QC0", 3);
  402. }
  403. return ERROR_OK;
  404. }
  405. else if ( packet[0] == 'T' ) // Is thread alive?
  406. {
  407. threadid_t threadid;
  408. int found = -1;
  409. sscanf(packet, "T%" SCNx64, &threadid);
  410. if ((target->rtos != NULL) && (target->rtos->thread_details
  411. != NULL)) {
  412. int thread_num;
  413. for (thread_num = 0; thread_num
  414. < target->rtos->thread_count; thread_num++) {
  415. if (target->rtos->thread_details[thread_num].threadid
  416. == threadid) {
  417. if (target->rtos->thread_details[thread_num].exists) {
  418. found = thread_num;
  419. }
  420. }
  421. }
  422. }
  423. if (found != -1) {
  424. gdb_put_packet(connection, "OK", 2); // thread alive
  425. } else {
  426. gdb_put_packet(connection, "E01", 3); // thread not found
  427. }
  428. }
  429. else if ( packet[0] == 'H') // Set current thread ( 'c' for step and continue, 'g' for all other operations )
  430. {
  431. if (packet[1] == 'g')
  432. {
  433. sscanf(packet, "Hg%16" SCNx64, &current_threadid);
  434. }
  435. gdb_put_packet(connection, "OK", 2);
  436. }
  437. return GDB_THREAD_PACKET_NOT_CONSUMED;
  438. }
  439. int rtos_get_gdb_reg_list(struct connection *connection, struct target *target, struct reg **reg_list[], int *reg_list_size)
  440. {
  441. if ( ( target->rtos != NULL ) &&
  442. ( current_threadid != -1 ) &&
  443. ( current_threadid != 0 ) &&
  444. ( current_threadid != target->rtos->current_thread ) )
  445. {
  446. char * hex_reg_list;
  447. target->rtos->type->get_thread_reg_list( target->rtos, current_threadid, &hex_reg_list );
  448. if ( hex_reg_list != NULL )
  449. {
  450. gdb_put_packet(connection, hex_reg_list, strlen(hex_reg_list));
  451. free(hex_reg_list);
  452. return ERROR_OK;
  453. }
  454. }
  455. return ERROR_FAIL;
  456. }
  457. int rtos_generic_stack_read( struct target * target, const struct rtos_register_stacking* stacking, int64_t stack_ptr, char ** hex_reg_list )
  458. {
  459. int list_size = 0;
  460. char * tmp_str_ptr;
  461. int64_t new_stack_ptr;
  462. int i;
  463. int retval;
  464. if ( stack_ptr == 0)
  465. {
  466. LOG_OUTPUT("Error: null stack pointer in thread\r\n");
  467. return -5;
  468. }
  469. // Read the stack
  470. uint8_t * stack_data = (uint8_t*) malloc( stacking->stack_registers_size );
  471. uint32_t address = stack_ptr;
  472. if ( stacking->stack_growth_direction == 1 )
  473. {
  474. address -= stacking->stack_registers_size;
  475. }
  476. retval = target_read_buffer( target, address, stacking->stack_registers_size, stack_data);
  477. if ( retval != ERROR_OK )
  478. {
  479. LOG_OUTPUT("Error reading stack frame from FreeRTOS thread\r\n");
  480. return retval;
  481. }
  482. /*
  483. LOG_OUTPUT("Stack Data :");
  484. for(i = 0; i < stacking->stack_registers_size; i++ )
  485. {
  486. LOG_OUTPUT("%02X",stack_data[i]);
  487. }
  488. LOG_OUTPUT("\r\n");
  489. */
  490. for( i = 0; i < stacking->num_output_registers; i++ )
  491. {
  492. list_size += stacking->register_offsets[i].width_bits/8;
  493. }
  494. *hex_reg_list = (char*)malloc( list_size*2 +1 );
  495. tmp_str_ptr = *hex_reg_list;
  496. new_stack_ptr = stack_ptr - stacking->stack_growth_direction * stacking->stack_registers_size;
  497. for( i = 0; i < stacking->num_output_registers; i++ )
  498. {
  499. int j;
  500. for ( j = 0; j < stacking->register_offsets[i].width_bits/8; j++ )
  501. {
  502. if ( stacking->register_offsets[i].offset == -1 )
  503. {
  504. tmp_str_ptr += sprintf( tmp_str_ptr, "%02x", 0 );
  505. }
  506. else if ( stacking->register_offsets[i].offset == -2 )
  507. {
  508. tmp_str_ptr += sprintf( tmp_str_ptr, "%02x", ((uint8_t*)&new_stack_ptr)[j] );
  509. }
  510. else
  511. {
  512. tmp_str_ptr += sprintf( tmp_str_ptr,"%02x", stack_data[ stacking->register_offsets[i].offset + j ] );
  513. }
  514. }
  515. }
  516. // LOG_OUTPUT("Output register string: %s\r\n", *hex_reg_list);
  517. return ERROR_OK;
  518. }
  519. int rtos_try_next( struct target * target )
  520. {
  521. int x;
  522. if ( target->rtos == NULL )
  523. {
  524. return -1;
  525. }
  526. for (x = 0 ; rtos_types[x] ; x++) {
  527. if (target->rtos->type == rtos_types[x] ) {
  528. /* found */
  529. if ( rtos_types[x+1] != NULL )
  530. {
  531. target->rtos->type = rtos_types[x+1];
  532. if ( target->rtos->symbols != NULL )
  533. {
  534. free( target->rtos->symbols );
  535. }
  536. return 1;
  537. }
  538. else
  539. {
  540. // No more rtos types
  541. return 0;
  542. }
  543. }
  544. }
  545. return 0;
  546. }
  547. static void hex_to_str( char* dst, char * hex_src )
  548. {
  549. int src_pos = 0;
  550. int dst_pos = 0;
  551. while ( hex_src[src_pos] != '\x00' )
  552. {
  553. char hex_char = hex_src[src_pos];
  554. char hex_digit_val = (hex_char>='a')?hex_char-'a'+10:(hex_char>='A')?hex_char-'A'+10:hex_char-'0';
  555. if ( 0 == (src_pos & 0x01) )
  556. {
  557. dst[dst_pos] = hex_digit_val;
  558. dst[dst_pos+1] = 0;
  559. }
  560. else
  561. {
  562. ((unsigned char*)dst)[dst_pos] <<= 4;
  563. ((unsigned char*)dst)[dst_pos] += hex_digit_val;
  564. dst_pos++;
  565. }
  566. src_pos++;
  567. }
  568. }
  569. static int str_to_hex( char* hex_dst, char* src )
  570. {
  571. char * posptr = hex_dst;
  572. unsigned i;
  573. for( i = 0; i < strlen(src); i++)
  574. {
  575. posptr += sprintf( posptr, "%02x", (unsigned char)src[i] );
  576. }
  577. return (posptr-hex_dst);
  578. }
  579. int rtos_update_threads( struct target* target )
  580. {
  581. if ((target->rtos != NULL) && (target->rtos->type != NULL))
  582. {
  583. target->rtos->type->update_threads(target->rtos);
  584. }
  585. return ERROR_OK;
  586. }