RTOS Thread awareness support wip
[openocd/cederom.git] / src / rtos / ThreadX.c
blobda841118aacc07b3abee5c3fbb3e3fe78ce6e8af
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 ***************************************************************************/
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
25 #include <helper/time_support.h>
26 #include <jtag/jtag.h>
27 #include "target/target.h"
28 #include "target/target_type.h"
29 #include "rtos.h"
30 #include "helper/log.h"
31 #include "rtos_standard_stackings.h"
34 static int ThreadX_detect_rtos( struct target* target );
35 static int ThreadX_create( struct target* target );
36 static int ThreadX_update_threads( struct rtos* rtos);
37 static int ThreadX_get_thread_reg_list(struct rtos *rtos, long long thread_id, char ** hex_reg_list );
38 static int ThreadX_get_symbol_list_to_lookup(symbol_table_elem_t * symbol_list[]);
42 struct ThreadX_thread_state
44 int value;
45 char * desc;
49 struct ThreadX_thread_state ThreadX_thread_states[] =
51 { 0, "Ready" },
52 { 1, "Completed" },
53 { 2, "Terminated" },
54 { 3, "Suspended" },
55 { 4, "Sleeping" },
56 { 5, "Waiting - Queue" },
57 { 6, "Waiting - Semaphore" },
58 { 7, "Waiting - Event flag" },
59 { 8, "Waiting - Memory" },
60 { 9, "Waiting - Memory" },
61 { 10, "Waiting - I/O" },
62 { 11, "Waiting - Filesystem" },
63 { 12, "Waiting - Network" },
64 { 13, "Waiting - Mutex" },
68 #define THREADX_NUM_STATES (sizeof(ThreadX_thread_states)/sizeof(struct ThreadX_thread_state))
71 struct ThreadX_params
73 char * target_name;
74 unsigned char pointer_width;
75 unsigned char thread_stack_offset;
76 unsigned char thread_name_offset;
77 unsigned char thread_state_offset;
78 unsigned char thread_next_offset;
79 const struct rtos_register_stacking* stacking_info;
82 const struct ThreadX_params ThreadX_params_list[] =
84 { "cortex_m3", // target_name
85 4, // pointer_width;
86 8, // thread_stack_offset;
87 40, // thread_name_offset;
88 48, // thread_state_offset;
89 136, // thread_next_offset
90 &rtos_standard_Cortex_M3_stacking, // stacking_info
95 #define THREADX_NUM_PARAMS ((int)(sizeof(ThreadX_params_list)/sizeof(struct ThreadX_params)))
97 enum ThreadX_symbol_values
99 ThreadX_VAL_tx_thread_current_ptr = 0,
100 ThreadX_VAL_tx_thread_created_ptr = 1,
101 ThreadX_VAL_tx_thread_created_count = 2,
104 static char* ThreadX_symbol_list[] =
106 "_tx_thread_current_ptr",
107 "_tx_thread_created_ptr",
108 "_tx_thread_created_count",
109 NULL
114 #define THREADX_NUM_SYMBOLS (sizeof(ThreadX_symbol_list)/sizeof(char*))
117 const struct rtos_type ThreadX_rtos =
119 .name = "ThreadX",
121 .detect_rtos = ThreadX_detect_rtos,
122 .create = ThreadX_create,
123 .update_threads = ThreadX_update_threads,
124 .get_thread_reg_list = ThreadX_get_thread_reg_list,
125 .get_symbol_list_to_lookup = ThreadX_get_symbol_list_to_lookup,
129 static int ThreadX_update_threads( struct rtos* rtos)
131 int retval;
132 int tasks_found = 0;
133 int thread_list_size = 0;
134 const struct ThreadX_params* param;
136 if ( rtos == NULL )
138 return -1;
141 if (rtos->rtos_specific_params == NULL )
143 return -3;
146 param = (const struct ThreadX_params*) rtos->rtos_specific_params;
148 if ( rtos->symbols == NULL )
150 LOG_OUTPUT("No symbols for ThreadX\r\n");
151 return -4;
154 if ( rtos->symbols[ThreadX_VAL_tx_thread_created_count].address == 0 )
156 LOG_OUTPUT("Don't have the number of threads in ThreadX \r\n");
157 return -2;
164 // read the number of threads
165 retval = target_read_buffer( rtos->target, rtos->symbols[ThreadX_VAL_tx_thread_created_count].address, 4, (uint8_t *)&thread_list_size);
167 if ( retval != ERROR_OK )
169 LOG_OUTPUT("Could not read ThreadX thread count from target\r\n");
170 return retval;
174 // wipe out previous thread details if any
175 if ( rtos->thread_details != NULL )
177 int j;
178 for( j = 0; j < rtos->thread_count; j++ )
180 if ( rtos->thread_details[j].display_str != NULL )
182 free( rtos->thread_details[j].display_str );
183 rtos->thread_details[j].display_str = NULL;
185 if ( rtos->thread_details[j].thread_name_str != NULL )
187 free( rtos->thread_details[j].thread_name_str );
188 rtos->thread_details[j].thread_name_str = NULL;
190 if ( rtos->thread_details[j].extra_info_str != NULL )
192 free( rtos->thread_details[j].extra_info_str );
193 rtos->thread_details[j].extra_info_str = NULL;
196 free( rtos->thread_details );
197 rtos->thread_details = NULL;
201 // read the current thread id
202 retval = target_read_buffer( rtos->target, rtos->symbols[ThreadX_VAL_tx_thread_current_ptr].address, 4, (uint8_t *)&rtos->current_thread);
204 if ( retval != ERROR_OK )
206 LOG_OUTPUT("Could not read ThreadX current thread from target\r\n");
207 return retval;
210 if ( ( thread_list_size == 0 ) || ( rtos->current_thread == 0 ) )
212 // Either : No RTOS threads - there is always at least the current execution though
213 // OR : No current thread - all threads suspended - show the current execution of idling
214 char tmp_str[] = "Current Execution";
215 thread_list_size++;
216 tasks_found++;
217 rtos->thread_details = (struct thread_detail*) malloc( sizeof( struct thread_detail ) * thread_list_size );
218 rtos->thread_details->threadid = 1;
219 rtos->thread_details->exists = true;
220 rtos->thread_details->display_str = NULL;
221 rtos->thread_details->extra_info_str = NULL;
222 rtos->thread_details->thread_name_str = (char*) malloc( sizeof(tmp_str) );
223 strcpy( rtos->thread_details->thread_name_str, tmp_str );
226 if ( thread_list_size == 0 )
228 rtos->thread_count = 1;
229 return ERROR_OK;
232 else
234 // create space for new thread details
235 rtos->thread_details = (struct thread_detail*) malloc( sizeof( struct thread_detail ) * thread_list_size );
240 // Read the pointer to the first thread
241 long long thread_ptr = 0;
242 retval = target_read_buffer( rtos->target, rtos->symbols[ThreadX_VAL_tx_thread_created_ptr].address, param->pointer_width, (uint8_t *)&thread_ptr);
243 if ( retval != ERROR_OK )
245 LOG_OUTPUT("Could not read ThreadX thread location from target\r\n");
246 return retval;
250 // loop over all threads
251 long long prev_thread_ptr = 0;
252 while ( ( thread_ptr != prev_thread_ptr ) && ( tasks_found < thread_list_size ) )
255 #define THREADX_THREAD_NAME_STR_SIZE (200)
256 char tmp_str[THREADX_THREAD_NAME_STR_SIZE];
257 unsigned int i = 0;
258 long long name_ptr = 0;
260 // Save the thread pointer
261 rtos->thread_details[tasks_found].threadid = thread_ptr;
264 // read the name pointer
265 retval = target_read_buffer( rtos->target, thread_ptr + param->thread_name_offset, param->pointer_width, (uint8_t *)&name_ptr);
266 if ( retval != ERROR_OK )
268 LOG_OUTPUT("Could not read ThreadX thread name pointer from target\r\n");
269 return retval;
272 // Read the thread name
273 retval = target_read_buffer( rtos->target, name_ptr, THREADX_THREAD_NAME_STR_SIZE, (uint8_t *)&tmp_str);
274 if ( retval != ERROR_OK )
276 LOG_OUTPUT("Error reading thread name from ThreadX target\r\n");
277 return retval;
279 tmp_str[THREADX_THREAD_NAME_STR_SIZE-1] = '\x00';
281 if ( tmp_str[0] == '\x00' )
283 strcpy(tmp_str,"No Name");
287 rtos->thread_details[tasks_found].thread_name_str = (char*)malloc( strlen(tmp_str)+1 );
288 strcpy( rtos->thread_details[tasks_found].thread_name_str, tmp_str );
292 // Read the thread status
293 long long thread_status = 0;
294 retval = target_read_buffer( rtos->target, thread_ptr + param->thread_state_offset, 4, (uint8_t *)&thread_status);
295 if ( retval != ERROR_OK )
297 LOG_OUTPUT("Error reading thread state from ThreadX target\r\n");
298 return retval;
301 for( i = 0; (i < THREADX_NUM_STATES) && (ThreadX_thread_states[i].value!=thread_status); i++ )
306 char * state_desc;
307 if (i < THREADX_NUM_STATES)
309 state_desc = ThreadX_thread_states[i].desc;
311 else
313 state_desc = "Unknown state";
316 rtos->thread_details[tasks_found].extra_info_str = (char*)malloc( strlen(state_desc)+1 );
317 strcpy( rtos->thread_details[tasks_found].extra_info_str, state_desc );
319 rtos->thread_details[tasks_found].exists = true;
321 rtos->thread_details[tasks_found].display_str = NULL;
327 tasks_found++;
328 prev_thread_ptr = thread_ptr;
330 // Get the location of the next thread structure.
331 thread_ptr = 0;
332 retval = target_read_buffer( rtos->target, prev_thread_ptr + param->thread_next_offset, param->pointer_width, (uint8_t *) &thread_ptr );
333 if ( retval != ERROR_OK )
335 LOG_OUTPUT("Error reading next thread pointer in ThreadX thread list\r\n");
336 return retval;
341 rtos->thread_count = tasks_found;
343 return 0;
346 static int ThreadX_get_thread_reg_list(struct rtos *rtos, long long thread_id, char ** hex_reg_list )
349 int retval;
350 const struct ThreadX_params* param;
352 *hex_reg_list = NULL;
354 if ( rtos == NULL )
356 return -1;
359 if ( thread_id == 0 )
361 return -2;
364 if (rtos->rtos_specific_params == NULL )
366 return -3;
369 param = (const struct ThreadX_params*) rtos->rtos_specific_params;
371 // Read the stack pointer
372 long long stack_ptr = 0;
373 retval = target_read_buffer( rtos->target, thread_id + param->thread_stack_offset, param->pointer_width, (uint8_t*)&stack_ptr);
374 if ( retval != ERROR_OK )
376 LOG_OUTPUT("Error reading stack frame from ThreadX thread\r\n");
377 return retval;
380 return rtos_generic_stack_read( rtos->target, param->stacking_info, stack_ptr, hex_reg_list );
385 static int ThreadX_get_symbol_list_to_lookup(symbol_table_elem_t * symbol_list[])
387 unsigned int i;
388 *symbol_list = (symbol_table_elem_t *) malloc( sizeof( symbol_table_elem_t ) * THREADX_NUM_SYMBOLS );
390 for( i = 0; i < THREADX_NUM_SYMBOLS; i++ )
392 (*symbol_list)[i].symbol_name = ThreadX_symbol_list[i];
395 return 0;
398 static int ThreadX_detect_rtos( struct target* target )
400 if ( ( target->rtos->symbols != NULL ) &&
401 ( target->rtos->symbols[ThreadX_VAL_tx_thread_created_ptr].address != 0 ) )
403 // looks like ThreadX
404 return 1;
406 return 0;
411 #if 0
413 static int ThreadX_set_current_thread(struct rtos *rtos, threadid_t thread_id)
415 return 0;
420 static int ThreadX_get_thread_detail( struct rtos* rtos, threadid_t thread_id, struct thread_detail* detail )
422 unsigned int i = 0;
423 int retval;
425 #define THREADX_THREAD_NAME_STR_SIZE (200)
426 char tmp_str[THREADX_THREAD_NAME_STR_SIZE];
428 const struct ThreadX_params* param;
430 if ( rtos == NULL )
432 return -1;
435 if ( thread_id == 0 )
437 return -2;
440 if (rtos->rtos_specific_params == NULL )
442 return -3;
445 param = (const struct ThreadX_params*) rtos->rtos_specific_params;
447 if ( rtos->symbols == NULL )
449 LOG_OUTPUT("No symbols for ThreadX\r\n");
450 return -3;
453 detail->threadid = thread_id;
455 long long name_ptr = 0;
456 // read the name pointer
457 retval = target_read_buffer( rtos->target, thread_id + param->thread_name_offset, param->pointer_width, (uint8_t *)&name_ptr);
458 if ( retval != ERROR_OK )
460 LOG_OUTPUT("Could not read ThreadX thread name pointer from target\r\n");
461 return retval;
464 // Read the thread name
465 retval = target_read_buffer( rtos->target, name_ptr, THREADX_THREAD_NAME_STR_SIZE, (uint8_t *)&tmp_str);
466 if ( retval != ERROR_OK )
468 LOG_OUTPUT("Error reading thread name from ThreadX target\r\n");
469 return retval;
471 tmp_str[THREADX_THREAD_NAME_STR_SIZE-1] = '\x00';
473 if ( tmp_str[0] == '\x00' )
475 strcpy(tmp_str,"No Name");
478 detail->thread_name_str = (char*)malloc( strlen(tmp_str)+1 );
481 // Read the thread status
482 long long thread_status = 0;
483 retval = target_read_buffer( rtos->target, thread_id + param->thread_state_offset, 4, (uint8_t *)&thread_status);
484 if ( retval != ERROR_OK )
486 LOG_OUTPUT("Error reading thread state from ThreadX target\r\n");
487 return retval;
490 for( i = 0; (i < THREADX_NUM_STATES) && (ThreadX_thread_states[i].value!=thread_status); i++ )
495 char * state_desc;
496 if (i < THREADX_NUM_STATES)
498 state_desc = ThreadX_thread_states[i].desc;
500 else
502 state_desc = "Unknown state";
505 detail->extra_info_str = (char*)malloc( strlen(state_desc)+1 );
507 detail->exists = true;
509 detail->display_str = NULL;
514 return 0;
517 #endif
519 static int ThreadX_create( struct target* target )
521 int i = 0;
522 while ( ( i < THREADX_NUM_PARAMS ) && ( 0 != strcmp( ThreadX_params_list[i].target_name, target->type->name ) ) )
524 i++;
526 if ( i >= THREADX_NUM_PARAMS )
528 LOG_OUTPUT("Could not find target in ThreadX compatability list\r\n");
529 return -1;
532 target->rtos->rtos_specific_params = (void*) &ThreadX_params_list[i];
533 target->rtos->current_thread = 0;
534 target->rtos->thread_details = NULL;
535 return 0;