1 /***************************************************************************
2 * Copyright (C) 2011 by Broadcom Corporation *
3 * Evan Hunter - ehunter@broadcom.com *
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. *
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. *
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 ***************************************************************************/
26 #include <helper/time_support.h>
27 #include <jtag/jtag.h>
28 #include "target/target.h"
29 #include "target/target_type.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;
59 16, // list_next_offset;
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
=
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
[] =
114 "pxOverflowDelayedTaskList",
116 "xTasksWaitingTermination",
117 "xSuspendedTaskList",
118 "uxCurrentNumberOfTasks",
122 #define FREERTOS_NUM_SYMBOLS (sizeof(FreeRTOS_symbol_list)/sizeof(char*))
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
)
134 const struct FreeRTOS_params
* param
;
136 if (rtos
->rtos_specific_params
== NULL
)
141 param
= (const struct FreeRTOS_params
*) rtos
->rtos_specific_params
;
143 if ( rtos
->symbols
== NULL
)
145 LOG_OUTPUT("No symbols for FreeRTOS\r\n");
149 if ( rtos
->symbols
[FreeRTOS_VAL_uxCurrentNumberOfTasks
].address
== 0 )
151 LOG_OUTPUT("Don't have the number of threads in FreeRTOS \r\n");
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");
165 // wipe out previous thread details if any
166 if ( rtos
->thread_details
!= NULL
)
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");
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";
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;
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 ) );
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 )
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");
264 if ( list_thread_count
== 0 )
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");
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");
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");
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
);
324 rtos
->thread_details
[tasks_found
].extra_info_str
= NULL
;
331 prev_list_elem_ptr
= list_elem_ptr
;
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");
343 free( list_of_lists
);
344 rtos
->thread_count
= tasks_found
;
348 static int FreeRTOS_get_thread_reg_list(struct rtos
*rtos
, long long thread_id
, char ** hex_reg_list
)
351 const struct FreeRTOS_params
* param
;
352 long long stack_ptr
= 0;
355 *hex_reg_list
= NULL
;
361 if ( thread_id
== 0 )
366 if (rtos
->rtos_specific_params
== NULL
)
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");
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
[])
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
];
400 static int FreeRTOS_set_current_thread(struct rtos
*rtos
, threadid_t thread_id
)
407 static int FreeRTOS_get_thread_ascii_info( struct rtos
* rtos
, threadid_t thread_id
, char ** info
)
410 const struct FreeRTOS_params
* param
;
417 if ( thread_id
== 0 )
422 if (rtos
->rtos_specific_params
== NULL
)
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");
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
);
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
466 static int FreeRTOS_create( struct target
* target
)
469 while ( ( i
< FREERTOS_NUM_PARAMS
) && ( 0 != strcmp( FreeRTOS_params_list
[i
].target_name
, target
->type
->name
) ) )
473 if ( i
>= FREERTOS_NUM_PARAMS
)
475 LOG_OUTPUT("Could not find target in FreeRTOS compatability list\r\n");
479 target
->rtos
->rtos_specific_params
= (void*) &FreeRTOS_params_list
[i
];