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.
 
 
 
 
 
 

428 lines
12 KiB

  1. /***************************************************************************
  2. * *
  3. * This program is free software; you can redistribute it and/or modify *
  4. * it under the terms of the GNU General Public License as published by *
  5. * the Free Software Foundation; either version 2 of the License, or *
  6. * (at your option) any later version. *
  7. * *
  8. * This program is distributed in the hope that it will be useful, *
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  11. * GNU General Public License for more details. *
  12. * *
  13. * You should have received a copy of the GNU General Public License *
  14. * along with this program; if not, write to the *
  15. * Free Software Foundation, Inc., *
  16. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  17. ***************************************************************************/
  18. #ifdef HAVE_CONFIG_H
  19. #include "config.h"
  20. #endif
  21. #include <helper/time_support.h>
  22. #include <jtag/jtag.h>
  23. #include "target/target.h"
  24. #include "target/target_type.h"
  25. #include "rtos.h"
  26. #include "helper/log.h"
  27. #include "rtos_ecos_stackings.h"
  28. static int eCos_detect_rtos( struct target* target );
  29. static int eCos_create( struct target* target );
  30. static int eCos_update_threads( struct rtos* rtos);
  31. static int eCos_get_thread_reg_list(struct rtos *rtos, int64_t thread_id, char ** hex_reg_list );
  32. static int eCos_get_symbol_list_to_lookup(symbol_table_elem_t * symbol_list[]);
  33. struct eCos_thread_state
  34. {
  35. int value;
  36. char * desc;
  37. };
  38. struct eCos_thread_state eCos_thread_states[] =
  39. {
  40. { 0, "Ready" },
  41. { 1, "Sleeping" },
  42. { 2, "Countsleep" },
  43. { 4, "Suspended" },
  44. { 8, "Creating" },
  45. { 16, "Exited" }
  46. };
  47. #define ECOS_NUM_STATES (sizeof(eCos_thread_states)/sizeof(struct eCos_thread_state))
  48. struct eCos_params
  49. {
  50. char * target_name;
  51. unsigned char pointer_width;
  52. unsigned char thread_stack_offset;
  53. unsigned char thread_name_offset;
  54. unsigned char thread_state_offset;
  55. unsigned char thread_next_offset;
  56. unsigned char thread_uniqueid_offset;
  57. const struct rtos_register_stacking* stacking_info;
  58. };
  59. const struct eCos_params eCos_params_list[] =
  60. {
  61. { "cortex_m3", // target_name
  62. 4, // pointer_width;
  63. 0x0c, // thread_stack_offset;
  64. 0x9c, // thread_name_offset;
  65. 0x3c, // thread_state_offset;
  66. 0xa0, // thread_next_offset
  67. 0x4c, // thread_uniqueid_offset
  68. &rtos_eCos_Cortex_M3_stacking // stacking_info
  69. }
  70. };
  71. #define ECOS_NUM_PARAMS ((int)(sizeof(eCos_params_list)/sizeof(struct eCos_params)))
  72. enum eCos_symbol_values
  73. {
  74. eCos_VAL_thread_list = 0,
  75. eCos_VAL_current_thread_ptr = 1
  76. };
  77. static char* eCos_symbol_list[] =
  78. {
  79. "Cyg_Thread::thread_list",
  80. "Cyg_Scheduler_Base::current_thread",
  81. NULL
  82. };
  83. #define ECOS_NUM_SYMBOLS (sizeof(eCos_symbol_list)/sizeof(char*))
  84. const struct rtos_type eCos_rtos =
  85. {
  86. .name = "eCos",
  87. .detect_rtos = eCos_detect_rtos,
  88. .create = eCos_create,
  89. .update_threads = eCos_update_threads,
  90. .get_thread_reg_list = eCos_get_thread_reg_list,
  91. .get_symbol_list_to_lookup = eCos_get_symbol_list_to_lookup,
  92. };
  93. static int eCos_update_threads( struct rtos* rtos)
  94. {
  95. int retval;
  96. int tasks_found = 0;
  97. int thread_list_size = 0;
  98. const struct eCos_params* param;
  99. if ( rtos == NULL )
  100. {
  101. return -1;
  102. }
  103. if (rtos->rtos_specific_params == NULL )
  104. {
  105. return -3;
  106. }
  107. param = (const struct eCos_params*) rtos->rtos_specific_params;
  108. if ( rtos->symbols == NULL )
  109. {
  110. LOG_OUTPUT("No symbols for eCos\r\n");
  111. return -4;
  112. }
  113. if ( rtos->symbols[eCos_VAL_thread_list].address == 0 )
  114. {
  115. LOG_OUTPUT("Don't have the thread list head\r\n");
  116. return -2;
  117. }
  118. // wipe out previous thread details if any
  119. if ( rtos->thread_details != NULL )
  120. {
  121. int j;
  122. for( j = 0; j < rtos->thread_count; j++ )
  123. {
  124. if ( rtos->thread_details[j].display_str != NULL )
  125. {
  126. free( rtos->thread_details[j].display_str );
  127. rtos->thread_details[j].display_str = NULL;
  128. }
  129. if ( rtos->thread_details[j].thread_name_str != NULL )
  130. {
  131. free( rtos->thread_details[j].thread_name_str );
  132. rtos->thread_details[j].thread_name_str = NULL;
  133. }
  134. if ( rtos->thread_details[j].extra_info_str != NULL )
  135. {
  136. free( rtos->thread_details[j].extra_info_str );
  137. rtos->thread_details[j].extra_info_str = NULL;
  138. }
  139. }
  140. free( rtos->thread_details );
  141. rtos->thread_details = NULL;
  142. }
  143. // determine the number of current threads
  144. uint32_t thread_list_head = rtos->symbols[eCos_VAL_thread_list].address;
  145. uint32_t thread_index;
  146. target_read_buffer( rtos->target, thread_list_head, param->pointer_width, (uint8_t *) &thread_index );
  147. uint32_t first_thread = thread_index;
  148. do
  149. {
  150. thread_list_size++;
  151. retval = target_read_buffer( rtos->target, thread_index + param->thread_next_offset, param->pointer_width, (uint8_t *) &thread_index );
  152. } while( thread_index!=first_thread );
  153. // read the current thread id
  154. uint32_t current_thread_addr;
  155. retval = target_read_buffer( rtos->target, rtos->symbols[eCos_VAL_current_thread_ptr].address, 4, (uint8_t *)&current_thread_addr);
  156. rtos->current_thread = 0;
  157. retval = target_read_buffer( rtos->target, current_thread_addr + param->thread_uniqueid_offset, 2, (uint8_t *)&rtos->current_thread);
  158. if ( retval != ERROR_OK )
  159. {
  160. LOG_OUTPUT("Could not read eCos current thread from target\r\n");
  161. return retval;
  162. }
  163. if ( ( thread_list_size == 0 ) || ( rtos->current_thread == 0 ) )
  164. {
  165. // Either : No RTOS threads - there is always at least the current execution though
  166. // OR : No current thread - all threads suspended - show the current execution of idling
  167. char tmp_str[] = "Current Execution";
  168. thread_list_size++;
  169. tasks_found++;
  170. rtos->thread_details = (struct thread_detail*) malloc( sizeof( struct thread_detail ) * thread_list_size );
  171. rtos->thread_details->threadid = 1;
  172. rtos->thread_details->exists = true;
  173. rtos->thread_details->display_str = NULL;
  174. rtos->thread_details->extra_info_str = NULL;
  175. rtos->thread_details->thread_name_str = (char*) malloc( sizeof(tmp_str) );
  176. strcpy( rtos->thread_details->thread_name_str, tmp_str );
  177. if ( thread_list_size == 0 )
  178. {
  179. rtos->thread_count = 1;
  180. return ERROR_OK;
  181. }
  182. }
  183. else
  184. {
  185. // create space for new thread details
  186. rtos->thread_details = (struct thread_detail*) malloc( sizeof( struct thread_detail ) * thread_list_size );
  187. }
  188. // loop over all threads
  189. thread_index = first_thread;
  190. do
  191. {
  192. #define ECOS_THREAD_NAME_STR_SIZE (200)
  193. char tmp_str[ECOS_THREAD_NAME_STR_SIZE];
  194. unsigned int i = 0;
  195. uint32_t name_ptr = 0;
  196. uint32_t prev_thread_ptr;
  197. // Save the thread pointer
  198. uint16_t thread_id;
  199. retval = target_read_buffer( rtos->target, thread_index + param->thread_uniqueid_offset, 2, (uint8_t *)&thread_id);
  200. if ( retval != ERROR_OK )
  201. {
  202. LOG_OUTPUT("Could not read eCos thread id from target\r\n");
  203. return retval;
  204. }
  205. rtos->thread_details[tasks_found].threadid = thread_id;
  206. // read the name pointer
  207. retval = target_read_buffer( rtos->target, thread_index + param->thread_name_offset, param->pointer_width, (uint8_t *)&name_ptr);
  208. if ( retval != ERROR_OK )
  209. {
  210. LOG_OUTPUT("Could not read eCos thread name pointer from target\r\n");
  211. return retval;
  212. }
  213. // Read the thread name
  214. retval = target_read_buffer( rtos->target, name_ptr, ECOS_THREAD_NAME_STR_SIZE, (uint8_t *)&tmp_str);
  215. if ( retval != ERROR_OK )
  216. {
  217. LOG_OUTPUT("Error reading thread name from eCos target\r\n");
  218. return retval;
  219. }
  220. tmp_str[ECOS_THREAD_NAME_STR_SIZE-1] = '\x00';
  221. if ( tmp_str[0] == '\x00' )
  222. {
  223. strcpy(tmp_str,"No Name");
  224. }
  225. rtos->thread_details[tasks_found].thread_name_str = (char*)malloc( strlen(tmp_str)+1 );
  226. strcpy( rtos->thread_details[tasks_found].thread_name_str, tmp_str );
  227. // Read the thread status
  228. int64_t thread_status = 0;
  229. retval = target_read_buffer( rtos->target, thread_index + param->thread_state_offset, 4, (uint8_t *)&thread_status);
  230. if ( retval != ERROR_OK )
  231. {
  232. LOG_OUTPUT("Error reading thread state from eCos target\r\n");
  233. return retval;
  234. }
  235. for( i = 0; (i < ECOS_NUM_STATES) && (eCos_thread_states[i].value!=thread_status); i++ )
  236. {
  237. }
  238. char * state_desc;
  239. if (i < ECOS_NUM_STATES)
  240. {
  241. state_desc = eCos_thread_states[i].desc;
  242. }
  243. else
  244. {
  245. state_desc = "Unknown state";
  246. }
  247. rtos->thread_details[tasks_found].extra_info_str = (char*)malloc( strlen(state_desc)+1 );
  248. strcpy( rtos->thread_details[tasks_found].extra_info_str, state_desc );
  249. rtos->thread_details[tasks_found].exists = true;
  250. rtos->thread_details[tasks_found].display_str = NULL;
  251. tasks_found++;
  252. prev_thread_ptr = thread_index;
  253. // Get the location of the next thread structure.
  254. thread_index = rtos->symbols[eCos_VAL_thread_list].address;
  255. retval = target_read_buffer( rtos->target, prev_thread_ptr + param->thread_next_offset, param->pointer_width, (uint8_t *) &thread_index );
  256. if ( retval != ERROR_OK )
  257. {
  258. LOG_OUTPUT("Error reading next thread pointer in eCos thread list\r\n");
  259. return retval;
  260. }
  261. }
  262. while( thread_index!=first_thread );
  263. rtos->thread_count = tasks_found;
  264. return 0;
  265. }
  266. static int eCos_get_thread_reg_list(struct rtos *rtos, int64_t thread_id, char ** hex_reg_list )
  267. {
  268. int retval;
  269. const struct eCos_params* param;
  270. *hex_reg_list = NULL;
  271. if ( rtos == NULL )
  272. {
  273. return -1;
  274. }
  275. if ( thread_id == 0 )
  276. {
  277. return -2;
  278. }
  279. if (rtos->rtos_specific_params == NULL )
  280. {
  281. return -3;
  282. }
  283. param = (const struct eCos_params*) rtos->rtos_specific_params;
  284. // Find the thread with that thread id
  285. uint16_t id=0;
  286. uint32_t thread_list_head = rtos->symbols[eCos_VAL_thread_list].address;
  287. uint32_t thread_index;
  288. target_read_buffer( rtos->target, thread_list_head, param->pointer_width, (uint8_t *) &thread_index );
  289. bool done=false;
  290. while(!done)
  291. {
  292. retval = target_read_buffer( rtos->target, thread_index + param->thread_uniqueid_offset, 2, (uint8_t*)&id);
  293. if ( retval != ERROR_OK )
  294. {
  295. LOG_OUTPUT("Error reading unique id from eCos thread\r\n");
  296. return retval;
  297. }
  298. if( id==thread_id )
  299. {
  300. done=true;
  301. break;
  302. }
  303. target_read_buffer( rtos->target, thread_index + param->thread_next_offset, param->pointer_width, (uint8_t *) &thread_index );
  304. }
  305. if(done)
  306. {
  307. // Read the stack pointer
  308. int64_t stack_ptr = 0;
  309. retval = target_read_buffer( rtos->target, thread_index + param->thread_stack_offset, param->pointer_width, (uint8_t*)&stack_ptr);
  310. if ( retval != ERROR_OK )
  311. {
  312. LOG_OUTPUT("Error reading stack frame from eCos thread\r\n");
  313. return retval;
  314. }
  315. return rtos_generic_stack_read( rtos->target, param->stacking_info, stack_ptr, hex_reg_list );
  316. }
  317. return -1;
  318. }
  319. static int eCos_get_symbol_list_to_lookup(symbol_table_elem_t * symbol_list[])
  320. {
  321. unsigned int i;
  322. *symbol_list = (symbol_table_elem_t *) malloc( sizeof( symbol_table_elem_t ) * ECOS_NUM_SYMBOLS );
  323. for( i = 0; i < ECOS_NUM_SYMBOLS; i++ )
  324. {
  325. (*symbol_list)[i].symbol_name = eCos_symbol_list[i];
  326. }
  327. return 0;
  328. }
  329. static int eCos_detect_rtos( struct target* target )
  330. {
  331. if ( ( target->rtos->symbols != NULL ) &&
  332. ( target->rtos->symbols[eCos_VAL_thread_list].address != 0 ) )
  333. {
  334. // looks like eCos
  335. return 1;
  336. }
  337. return 0;
  338. }
  339. static int eCos_create( struct target* target )
  340. {
  341. int i = 0;
  342. while ( ( i < ECOS_NUM_PARAMS ) && ( 0 != strcmp( eCos_params_list[i].target_name, target->type->name ) ) )
  343. {
  344. i++;
  345. }
  346. if ( i >= ECOS_NUM_PARAMS )
  347. {
  348. LOG_OUTPUT("Could not find target in eCos compatibility list\r\n");
  349. return -1;
  350. }
  351. target->rtos->rtos_specific_params = (void*) &eCos_params_list[i];
  352. target->rtos->current_thread = 0;
  353. target->rtos->thread_details = NULL;
  354. return 0;
  355. }