Fix FreeRTOS thread list parsing
[openocd/jflash.git] / src / rtos / FreeRTOS.c
blobe288dd4135bfcd44d9268f59aff1d3e6d7f6ddb0
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 ***************************************************************************/
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
26 #include <helper/time_support.h>
27 #include <jtag/jtag.h>
28 #include "target/target.h"
29 #include "target/target_type.h"
30 #include "rtos.h"
31 #include "helper/log.h"
32 #include "rtos_standard_stackings.h"
34 #define FreeRTOS_STRUCT( int_type, ptr_type, list_prev_offset )
37 struct FreeRTOS_params
39 const char * target_name;
40 const unsigned char thread_count_width;
41 const unsigned char pointer_width;
42 const unsigned char list_next_offset;
43 const unsigned char list_width;
44 const unsigned char list_elem_next_offset;
45 const unsigned char list_elem_content_offset;
46 const unsigned char thread_stack_offset;
47 const unsigned char thread_name_offset;
48 const struct rtos_register_stacking* stacking_info;
54 const struct FreeRTOS_params FreeRTOS_params_list[] =
56 { "cortex_m3", // target_name
57 4, // thread_count_width;
58 4, // pointer_width;
59 16, // list_next_offset;
60 20, // list_width;
61 8, // list_elem_next_offset;
62 12, // list_elem_content_offset
63 0, // thread_stack_offset;
64 52, // thread_name_offset;
65 &rtos_standard_Cortex_M3_stacking, // stacking_info
71 #define FREERTOS_NUM_PARAMS ((int)(sizeof(FreeRTOS_params_list)/sizeof(struct FreeRTOS_params)))
73 static int FreeRTOS_detect_rtos( struct target* target );
74 static int FreeRTOS_create( struct target* target );
75 static int FreeRTOS_update_threads( struct rtos *rtos );
76 static int FreeRTOS_get_thread_reg_list(struct rtos *rtos, int64_t thread_id, char ** hex_reg_list );
77 static int FreeRTOS_get_symbol_list_to_lookup(symbol_table_elem_t * symbol_list[]);
82 struct rtos_type FreeRTOS_rtos =
84 .name = "FreeRTOS",
86 .detect_rtos = FreeRTOS_detect_rtos,
87 .create = FreeRTOS_create,
88 .update_threads = FreeRTOS_update_threads,
89 .get_thread_reg_list = FreeRTOS_get_thread_reg_list,
90 .get_symbol_list_to_lookup = FreeRTOS_get_symbol_list_to_lookup,
93 enum FreeRTOS_symbol_values
95 FreeRTOS_VAL_pxCurrentTCB = 0,
96 FreeRTOS_VAL_pxReadyTasksLists = 1,
97 FreeRTOS_VAL_xDelayedTaskList1 = 2,
98 FreeRTOS_VAL_xDelayedTaskList2 = 3,
99 FreeRTOS_VAL_pxDelayedTaskList = 4,
100 FreeRTOS_VAL_pxOverflowDelayedTaskList = 5,
101 FreeRTOS_VAL_xPendingReadyList = 6,
102 FreeRTOS_VAL_xTasksWaitingTermination = 7,
103 FreeRTOS_VAL_xSuspendedTaskList = 8,
104 FreeRTOS_VAL_uxCurrentNumberOfTasks = 9,
105 FreeRTOS_VAL_uxTopUsedPriority = 10,
108 static char* FreeRTOS_symbol_list[] =
110 "pxCurrentTCB",
111 "pxReadyTasksLists",
112 "xDelayedTaskList1",
113 "xDelayedTaskList2",
114 "pxDelayedTaskList",
115 "pxOverflowDelayedTaskList",
116 "xPendingReadyList",
117 "xTasksWaitingTermination",
118 "xSuspendedTaskList",
119 "uxCurrentNumberOfTasks",
120 "uxTopUsedPriority",
121 NULL
124 #define FREERTOS_NUM_SYMBOLS (sizeof(FreeRTOS_symbol_list)/sizeof(char*))
126 // TODO:
127 // this is not safe for little endian yet
128 // may be problems reading if sizes are not 32 bit long integers.
129 // test mallocs for failure
131 static int FreeRTOS_update_threads( struct rtos *rtos )
133 int i = 0;
134 int retval;
135 int tasks_found = 0;
136 const struct FreeRTOS_params* param;
138 if (rtos->rtos_specific_params == NULL )
140 return -1;
143 param = (const struct FreeRTOS_params*) rtos->rtos_specific_params;
145 if ( rtos->symbols == NULL )
147 LOG_OUTPUT("No symbols for FreeRTOS\r\n");
148 return -3;
151 if ( rtos->symbols[FreeRTOS_VAL_uxCurrentNumberOfTasks].address == 0 )
153 LOG_OUTPUT("Don't have the number of threads in FreeRTOS \r\n");
154 return -2;
157 int thread_list_size = 0;
158 retval = target_read_buffer( rtos->target, rtos->symbols[FreeRTOS_VAL_uxCurrentNumberOfTasks].address, param->thread_count_width, (uint8_t *)&thread_list_size);
160 if ( retval != ERROR_OK )
162 LOG_OUTPUT("Could not read FreeRTOS thread count from target\r\n");
163 return retval;
167 // wipe out previous thread details if any
168 if ( rtos->thread_details != NULL )
170 int j;
171 for( j = 0; j < rtos->thread_count; j++ )
173 if ( rtos->thread_details[j].display_str != NULL )
175 free( rtos->thread_details[j].display_str );
176 rtos->thread_details[j].display_str = NULL;
178 if ( rtos->thread_details[j].thread_name_str != NULL )
180 free( rtos->thread_details[j].thread_name_str );
181 rtos->thread_details[j].thread_name_str = NULL;
183 if ( rtos->thread_details[j].extra_info_str != NULL )
185 free( rtos->thread_details[j].extra_info_str );
186 rtos->thread_details[j].extra_info_str = NULL;
189 free( rtos->thread_details );
190 rtos->thread_details = NULL;
194 // read the current thread
195 retval = target_read_buffer( rtos->target, rtos->symbols[FreeRTOS_VAL_pxCurrentTCB].address, param->pointer_width, (uint8_t *)&rtos->current_thread );
196 if ( retval != ERROR_OK )
198 LOG_OUTPUT("Error reading current thread in FreeRTOS thread list\r\n");
199 return retval;
202 if ( ( thread_list_size == 0 ) || ( rtos->current_thread == 0 ) )
204 // Either : No RTOS threads - there is always at least the current execution though
205 // OR : No current thread - all threads suspended - show the current execution of idling
206 char tmp_str[] = "Current Execution";
207 thread_list_size++;
208 tasks_found++;
209 rtos->thread_details = (struct thread_detail*) malloc( sizeof( struct thread_detail ) * thread_list_size );
210 rtos->thread_details->threadid = 1;
211 rtos->thread_details->exists = true;
212 rtos->thread_details->display_str = NULL;
213 rtos->thread_details->extra_info_str = NULL;
214 rtos->thread_details->thread_name_str = (char*) malloc( sizeof(tmp_str) );
215 strcpy( rtos->thread_details->thread_name_str, tmp_str );
218 if ( thread_list_size == 1 )
220 rtos->thread_count = 1;
221 return ERROR_OK;
224 else
226 // create space for new thread details
227 rtos->thread_details = (struct thread_detail*) malloc( sizeof( struct thread_detail ) * thread_list_size );
231 // Find out how many lists are needed to be read from pxReadyTasksLists,
232 int64_t max_used_priority = 0;
233 retval = target_read_buffer( rtos->target, rtos->symbols[FreeRTOS_VAL_uxTopUsedPriority].address, param->pointer_width, (uint8_t *)&max_used_priority );
236 symbol_address_t* list_of_lists = (symbol_address_t *)malloc( sizeof( symbol_address_t ) * ( max_used_priority + 5 ) );
238 int num_lists;
239 for( num_lists = 0; num_lists < max_used_priority; num_lists++ )
241 list_of_lists[num_lists] = rtos->symbols[FreeRTOS_VAL_pxReadyTasksLists].address + num_lists * param->list_width;
244 list_of_lists[num_lists++] = rtos->symbols[FreeRTOS_VAL_xDelayedTaskList1].address;
245 list_of_lists[num_lists++] = rtos->symbols[FreeRTOS_VAL_xDelayedTaskList2].address;
246 list_of_lists[num_lists++] = rtos->symbols[FreeRTOS_VAL_xPendingReadyList].address;
247 list_of_lists[num_lists++] = rtos->symbols[FreeRTOS_VAL_xTasksWaitingTermination].address;
250 for( i = 0; i < num_lists; i++ )
252 if ( list_of_lists[i] == 0 )
254 continue;
257 // Read the number of threads in this list
258 int64_t list_thread_count = 0;
259 retval = target_read_buffer( rtos->target, list_of_lists[i], param->thread_count_width, (uint8_t *)&list_thread_count);
260 if ( retval != ERROR_OK )
262 LOG_OUTPUT("Error reading number of threads in FreeRTOS thread list\r\n");
263 return retval;
266 if ( list_thread_count == 0 )
268 continue;
271 // Read the location of first list item
272 uint64_t prev_list_elem_ptr = -1;
273 uint64_t list_elem_ptr = 0;
274 retval = target_read_buffer( rtos->target, list_of_lists[i] + param->list_next_offset, param->pointer_width, (uint8_t *)&list_elem_ptr);
275 if ( retval != ERROR_OK )
277 LOG_OUTPUT("Error reading first thread item location in FreeRTOS thread list\r\n");
278 return retval;
282 while ( (list_thread_count > 0) && ( list_elem_ptr != 0) && ( list_elem_ptr != prev_list_elem_ptr ) && ( tasks_found < thread_list_size ) )
284 // Get the location of the thread structure.
285 rtos->thread_details[tasks_found].threadid = 0;
286 retval = target_read_buffer( rtos->target, list_elem_ptr + param->list_elem_content_offset, param->pointer_width, (uint8_t *)&(rtos->thread_details[tasks_found].threadid));
287 if ( retval != ERROR_OK )
289 LOG_OUTPUT("Error reading thread list item object in FreeRTOS thread list\r\n");
290 return retval;
294 // get thread name
296 #define FREERTOS_THREAD_NAME_STR_SIZE (200)
297 char tmp_str[FREERTOS_THREAD_NAME_STR_SIZE];
299 // Read the thread name
300 retval = target_read_buffer( rtos->target, rtos->thread_details[tasks_found].threadid + param->thread_name_offset, FREERTOS_THREAD_NAME_STR_SIZE, (uint8_t *)&tmp_str);
301 if ( retval != ERROR_OK )
303 LOG_OUTPUT("Error reading first thread item location in FreeRTOS thread list\r\n");
304 return retval;
306 tmp_str[FREERTOS_THREAD_NAME_STR_SIZE-1] = '\x00';
308 if ( tmp_str[0] == '\x00' )
310 strcpy(tmp_str,"No Name");
313 rtos->thread_details[tasks_found].thread_name_str = (char*)malloc( strlen(tmp_str)+1 );
314 strcpy( rtos->thread_details[tasks_found].thread_name_str, tmp_str );
315 rtos->thread_details[tasks_found].display_str = NULL;
316 rtos->thread_details[tasks_found].exists = true;
318 if ( rtos->thread_details[tasks_found].threadid == rtos->current_thread )
320 char running_str[] = "Running";
321 rtos->thread_details[tasks_found].extra_info_str = (char*) malloc( sizeof(running_str) );
322 strcpy( rtos->thread_details[tasks_found].extra_info_str, running_str );
324 else
326 rtos->thread_details[tasks_found].extra_info_str = NULL;
330 tasks_found++;
331 list_thread_count--;
333 prev_list_elem_ptr = list_elem_ptr;
334 list_elem_ptr = 0;
335 retval = target_read_buffer( rtos->target, prev_list_elem_ptr + param->list_elem_next_offset, param->pointer_width, (uint8_t *)&list_elem_ptr);
336 if ( retval != ERROR_OK )
338 LOG_OUTPUT("Error reading next thread item location in FreeRTOS thread list\r\n");
339 return retval;
345 free( list_of_lists );
346 rtos->thread_count = tasks_found;
347 return 0;
350 static int FreeRTOS_get_thread_reg_list(struct rtos *rtos, int64_t thread_id, char ** hex_reg_list )
352 int retval;
353 const struct FreeRTOS_params* param;
354 int64_t stack_ptr = 0;
357 *hex_reg_list = NULL;
358 if ( rtos == NULL )
360 return -1;
363 if ( thread_id == 0 )
365 return -2;
368 if (rtos->rtos_specific_params == NULL )
370 return -1;
373 param = (const struct FreeRTOS_params*) rtos->rtos_specific_params;
375 // Read the stack pointer
376 retval = target_read_buffer( rtos->target, thread_id + param->thread_stack_offset, param->pointer_width, (uint8_t*)&stack_ptr);
377 if ( retval != ERROR_OK )
379 LOG_OUTPUT("Error reading stack frame from FreeRTOS thread\r\n");
380 return retval;
383 return rtos_generic_stack_read( rtos->target, param->stacking_info, stack_ptr, hex_reg_list );
387 static int FreeRTOS_get_symbol_list_to_lookup(symbol_table_elem_t * symbol_list[])
389 unsigned int i;
390 *symbol_list = (symbol_table_elem_t *) malloc( sizeof( symbol_table_elem_t ) * FREERTOS_NUM_SYMBOLS );
392 for( i = 0; i < FREERTOS_NUM_SYMBOLS; i++ )
394 (*symbol_list)[i].symbol_name = FreeRTOS_symbol_list[i];
397 return 0;
400 #if 0
402 static int FreeRTOS_set_current_thread(struct rtos *rtos, threadid_t thread_id)
404 return 0;
409 static int FreeRTOS_get_thread_ascii_info( struct rtos* rtos, threadid_t thread_id, char ** info )
411 int retval;
412 const struct FreeRTOS_params* param;
414 if ( rtos == NULL )
416 return -1;
419 if ( thread_id == 0 )
421 return -2;
424 if (rtos->rtos_specific_params == NULL )
426 return -3;
429 param = (const struct FreeRTOS_params*) rtos->rtos_specific_params;
431 #define FREERTOS_THREAD_NAME_STR_SIZE (200)
432 char tmp_str[FREERTOS_THREAD_NAME_STR_SIZE];
434 // Read the thread name
435 retval = target_read_buffer( rtos->target, thread_id + param->thread_name_offset, FREERTOS_THREAD_NAME_STR_SIZE, (uint8_t *)&tmp_str);
436 if ( retval != ERROR_OK )
438 LOG_OUTPUT("Error reading first thread item location in FreeRTOS thread list\r\n");
439 return retval;
441 tmp_str[FREERTOS_THREAD_NAME_STR_SIZE-1] = '\x00';
443 if ( tmp_str[0] == '\x00' )
445 strcpy(tmp_str,"No Name");
448 *info = (char*)malloc( strlen(tmp_str)+1 );
449 strcpy( *info, tmp_str );
450 return 0;
453 #endif
455 static int FreeRTOS_detect_rtos( struct target* target )
457 if ( ( target->rtos->symbols != NULL ) &&
458 ( target->rtos->symbols[FreeRTOS_VAL_pxReadyTasksLists].address != 0 ) )
460 // looks like FreeRTOS
461 return 1;
463 return 0;
464 return 0;
468 static int FreeRTOS_create( struct target* target )
470 int i = 0;
471 while ( ( i < FREERTOS_NUM_PARAMS ) && ( 0 != strcmp( FreeRTOS_params_list[i].target_name, target->type->name ) ) )
473 i++;
475 if ( i >= FREERTOS_NUM_PARAMS )
477 LOG_OUTPUT("Could not find target in FreeRTOS compatibility list\r\n");
478 return -1;
481 target->rtos->rtos_specific_params = (void*) &FreeRTOS_params_list[i];
482 return 0;