RTOS Thread awareness support wip
[openocd/openocdswd.git] / src / rtos / FreeRTOS.c
blob000741607282f80915f01df3d3d4b648403b6f69
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, long long 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,
107 static char* FreeRTOS_symbol_list[] =
109 "pxCurrentTCB",
110 "pxReadyTasksLists",
111 "xDelayedTaskList1",
112 "xDelayedTaskList2",
113 "pxDelayedTaskList",
114 "pxOverflowDelayedTaskList",
115 "xPendingReadyList",
116 "xTasksWaitingTermination",
117 "xSuspendedTaskList",
118 "uxCurrentNumberOfTasks",
119 NULL
122 #define FREERTOS_NUM_SYMBOLS (sizeof(FreeRTOS_symbol_list)/sizeof(char*))
124 // TODO:
125 // this is not safe for little endian yet
126 // may be problems reading if sizes are not 32 bit long integers.
127 // test mallocs for failure
129 static int FreeRTOS_update_threads( struct rtos *rtos )
131 int i = 0;
132 int retval;
133 int tasks_found = 0;
134 const struct FreeRTOS_params* param;
136 if (rtos->rtos_specific_params == NULL )
138 return -1;
141 param = (const struct FreeRTOS_params*) rtos->rtos_specific_params;
143 if ( rtos->symbols == NULL )
145 LOG_OUTPUT("No symbols for FreeRTOS\r\n");
146 return -3;
149 if ( rtos->symbols[FreeRTOS_VAL_uxCurrentNumberOfTasks].address == 0 )
151 LOG_OUTPUT("Don't have the number of threads in FreeRTOS \r\n");
152 return -2;
155 int thread_list_size = 0;
156 retval = target_read_buffer( rtos->target, rtos->symbols[FreeRTOS_VAL_uxCurrentNumberOfTasks].address, param->thread_count_width, (uint8_t *)&thread_list_size);
158 if ( retval != ERROR_OK )
160 LOG_OUTPUT("Could not read FreeRTOS thread count from target\r\n");
161 return retval;
165 // wipe out previous thread details if any
166 if ( rtos->thread_details != NULL )
168 int j;
169 for( j = 0; j < rtos->thread_count; j++ )
171 if ( rtos->thread_details[j].display_str != NULL )
173 free( rtos->thread_details[j].display_str );
174 rtos->thread_details[j].display_str = NULL;
176 if ( rtos->thread_details[j].thread_name_str != NULL )
178 free( rtos->thread_details[j].thread_name_str );
179 rtos->thread_details[j].thread_name_str = NULL;
181 if ( rtos->thread_details[j].extra_info_str != NULL )
183 free( rtos->thread_details[j].extra_info_str );
184 rtos->thread_details[j].extra_info_str = NULL;
187 free( rtos->thread_details );
188 rtos->thread_details = NULL;
192 // read the current thread
193 retval = target_read_buffer( rtos->target, rtos->symbols[FreeRTOS_VAL_pxCurrentTCB].address, param->pointer_width, (uint8_t *)&rtos->current_thread );
194 if ( retval != ERROR_OK )
196 LOG_OUTPUT("Error reading current thread in FreeRTOS thread list\r\n");
197 return retval;
200 if ( ( thread_list_size == 0 ) || ( rtos->current_thread == 0 ) )
202 // Either : No RTOS threads - there is always at least the current execution though
203 // OR : No current thread - all threads suspended - show the current execution of idling
204 char tmp_str[] = "Current Execution";
205 thread_list_size++;
206 tasks_found++;
207 rtos->thread_details = (struct thread_detail*) malloc( sizeof( struct thread_detail ) * thread_list_size );
208 rtos->thread_details->threadid = 1;
209 rtos->thread_details->exists = true;
210 rtos->thread_details->display_str = NULL;
211 rtos->thread_details->extra_info_str = NULL;
212 rtos->thread_details->thread_name_str = (char*) malloc( sizeof(tmp_str) );
213 strcpy( rtos->thread_details->thread_name_str, tmp_str );
216 if ( thread_list_size == 1 )
218 rtos->thread_count = 1;
219 return ERROR_OK;
222 else
224 // create space for new thread details
225 rtos->thread_details = (struct thread_detail*) malloc( sizeof( struct thread_detail ) * thread_list_size );
229 // Unfortunately, we can't know how many lists there are for pxReadyTasksLists,
230 // So figure it out via other variables
231 int num_ready_task_lists = (rtos->symbols[FreeRTOS_VAL_xDelayedTaskList1].address - rtos->symbols[FreeRTOS_VAL_pxReadyTasksLists].address) / param->list_width;
234 symbol_address_t* list_of_lists = (symbol_address_t *)malloc( sizeof( symbol_address_t ) * ( num_ready_task_lists + 5 ) );
236 int num_lists;
237 for( num_lists = 0; num_lists < num_ready_task_lists; num_lists++ )
239 list_of_lists[num_lists] = rtos->symbols[FreeRTOS_VAL_pxReadyTasksLists].address + num_lists * param->list_width;
242 list_of_lists[num_lists++] = rtos->symbols[FreeRTOS_VAL_xDelayedTaskList1].address;
243 list_of_lists[num_lists++] = rtos->symbols[FreeRTOS_VAL_xDelayedTaskList2].address;
244 list_of_lists[num_lists++] = rtos->symbols[FreeRTOS_VAL_xPendingReadyList].address;
245 list_of_lists[num_lists++] = rtos->symbols[FreeRTOS_VAL_xTasksWaitingTermination].address;
248 for( i = 0; i < num_lists; i++ )
250 if ( list_of_lists[i] == 0 )
252 continue;
255 // Read the number of threads in this list
256 long long list_thread_count = 0;
257 retval = target_read_buffer( rtos->target, list_of_lists[i], param->thread_count_width, (uint8_t *)&list_thread_count);
258 if ( retval != ERROR_OK )
260 LOG_OUTPUT("Error reading number of threads in FreeRTOS thread list\r\n");
261 return retval;
264 if ( list_thread_count == 0 )
266 continue;
269 // Read the location of first list item
270 unsigned long long prev_list_elem_ptr = -1;
271 unsigned long long list_elem_ptr = 0;
272 retval = target_read_buffer( rtos->target, list_of_lists[i] + param->list_next_offset, param->pointer_width, (uint8_t *)&list_elem_ptr);
273 if ( retval != ERROR_OK )
275 LOG_OUTPUT("Error reading first thread item location in FreeRTOS thread list\r\n");
276 return retval;
280 while ( (list_thread_count > 0) && ( list_elem_ptr != 0) && ( list_elem_ptr != prev_list_elem_ptr ) && ( tasks_found < thread_list_size ) )
282 // Get the location of the thread structure.
283 rtos->thread_details[tasks_found].threadid = 0;
284 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));
285 if ( retval != ERROR_OK )
287 LOG_OUTPUT("Error reading thread list item object in FreeRTOS thread list\r\n");
288 return retval;
292 // get thread name
294 #define FREERTOS_THREAD_NAME_STR_SIZE (200)
295 char tmp_str[FREERTOS_THREAD_NAME_STR_SIZE];
297 // Read the thread name
298 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);
299 if ( retval != ERROR_OK )
301 LOG_OUTPUT("Error reading first thread item location in FreeRTOS thread list\r\n");
302 return retval;
304 tmp_str[FREERTOS_THREAD_NAME_STR_SIZE-1] = '\x00';
306 if ( tmp_str[0] == '\x00' )
308 strcpy(tmp_str,"No Name");
311 rtos->thread_details[tasks_found].thread_name_str = (char*)malloc( strlen(tmp_str)+1 );
312 strcpy( rtos->thread_details[tasks_found].thread_name_str, tmp_str );
313 rtos->thread_details[tasks_found].display_str = NULL;
314 rtos->thread_details[tasks_found].exists = true;
316 if ( rtos->thread_details[tasks_found].threadid == rtos->current_thread )
318 char running_str[] = "Running";
319 rtos->thread_details[tasks_found].extra_info_str = (char*) malloc( sizeof(running_str) );
320 strcpy( rtos->thread_details[tasks_found].extra_info_str, running_str );
322 else
324 rtos->thread_details[tasks_found].extra_info_str = NULL;
328 tasks_found++;
329 list_thread_count--;
331 prev_list_elem_ptr = list_elem_ptr;
332 list_elem_ptr = 0;
333 retval = target_read_buffer( rtos->target, prev_list_elem_ptr + param->list_elem_next_offset, param->pointer_width, (uint8_t *)&list_elem_ptr);
334 if ( retval != ERROR_OK )
336 LOG_OUTPUT("Error reading next thread item location in FreeRTOS thread list\r\n");
337 return retval;
343 free( list_of_lists );
344 rtos->thread_count = tasks_found;
345 return 0;
348 static int FreeRTOS_get_thread_reg_list(struct rtos *rtos, long long thread_id, char ** hex_reg_list )
350 int retval;
351 const struct FreeRTOS_params* param;
352 long long stack_ptr = 0;
355 *hex_reg_list = NULL;
356 if ( rtos == NULL )
358 return -1;
361 if ( thread_id == 0 )
363 return -2;
366 if (rtos->rtos_specific_params == NULL )
368 return -1;
371 param = (const struct FreeRTOS_params*) rtos->rtos_specific_params;
373 // Read the stack pointer
374 retval = target_read_buffer( rtos->target, thread_id + param->thread_stack_offset, param->pointer_width, (uint8_t*)&stack_ptr);
375 if ( retval != ERROR_OK )
377 LOG_OUTPUT("Error reading stack frame from FreeRTOS thread\r\n");
378 return retval;
381 return rtos_generic_stack_read( rtos->target, param->stacking_info, stack_ptr, hex_reg_list );
385 static int FreeRTOS_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 ) * FREERTOS_NUM_SYMBOLS );
390 for( i = 0; i < FREERTOS_NUM_SYMBOLS; i++ )
392 (*symbol_list)[i].symbol_name = FreeRTOS_symbol_list[i];
395 return 0;
398 #if 0
400 static int FreeRTOS_set_current_thread(struct rtos *rtos, threadid_t thread_id)
402 return 0;
407 static int FreeRTOS_get_thread_ascii_info( struct rtos* rtos, threadid_t thread_id, char ** info )
409 int retval;
410 const struct FreeRTOS_params* param;
412 if ( rtos == NULL )
414 return -1;
417 if ( thread_id == 0 )
419 return -2;
422 if (rtos->rtos_specific_params == NULL )
424 return -3;
427 param = (const struct FreeRTOS_params*) rtos->rtos_specific_params;
429 #define FREERTOS_THREAD_NAME_STR_SIZE (200)
430 char tmp_str[FREERTOS_THREAD_NAME_STR_SIZE];
432 // Read the thread name
433 retval = target_read_buffer( rtos->target, thread_id + param->thread_name_offset, FREERTOS_THREAD_NAME_STR_SIZE, (uint8_t *)&tmp_str);
434 if ( retval != ERROR_OK )
436 LOG_OUTPUT("Error reading first thread item location in FreeRTOS thread list\r\n");
437 return retval;
439 tmp_str[FREERTOS_THREAD_NAME_STR_SIZE-1] = '\x00';
441 if ( tmp_str[0] == '\x00' )
443 strcpy(tmp_str,"No Name");
446 *info = (char*)malloc( strlen(tmp_str)+1 );
447 strcpy( *info, tmp_str );
448 return 0;
451 #endif
453 static int FreeRTOS_detect_rtos( struct target* target )
455 if ( ( target->rtos->symbols != NULL ) &&
456 ( target->rtos->symbols[FreeRTOS_VAL_pxReadyTasksLists].address != 0 ) )
458 // looks like FreeRTOS
459 return 1;
461 return 0;
462 return 0;
466 static int FreeRTOS_create( struct target* target )
468 int i = 0;
469 while ( ( i < FREERTOS_NUM_PARAMS ) && ( 0 != strcmp( FreeRTOS_params_list[i].target_name, target->type->name ) ) )
471 i++;
473 if ( i >= FREERTOS_NUM_PARAMS )
475 LOG_OUTPUT("Could not find target in FreeRTOS compatability list\r\n");
476 return -1;
479 target->rtos->rtos_specific_params = (void*) &FreeRTOS_params_list[i];
480 return 0;