RTOS Thread awareness support wip
[openocd.git] / src / rtos / FreeRTOS.c
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 ***************************************************************************/
20
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
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"
33
34 #define FreeRTOS_STRUCT( int_type, ptr_type, list_prev_offset )
35
36
37 struct FreeRTOS_params
38 {
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;
49 };
50
51
52
53
54 const struct FreeRTOS_params FreeRTOS_params_list[] =
55 {
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
66 }
67
68 };
69
70
71 #define FREERTOS_NUM_PARAMS ((int)(sizeof(FreeRTOS_params_list)/sizeof(struct FreeRTOS_params)))
72
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[]);
78
79
80
81
82 struct rtos_type FreeRTOS_rtos =
83 {
84 .name = "FreeRTOS",
85
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,
91 };
92
93 enum FreeRTOS_symbol_values
94 {
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 };
106
107 static char* FreeRTOS_symbol_list[] =
108 {
109 "pxCurrentTCB",
110 "pxReadyTasksLists",
111 "xDelayedTaskList1",
112 "xDelayedTaskList2",
113 "pxDelayedTaskList",
114 "pxOverflowDelayedTaskList",
115 "xPendingReadyList",
116 "xTasksWaitingTermination",
117 "xSuspendedTaskList",
118 "uxCurrentNumberOfTasks",
119 NULL
120 };
121
122 #define FREERTOS_NUM_SYMBOLS (sizeof(FreeRTOS_symbol_list)/sizeof(char*))
123
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
128
129 static int FreeRTOS_update_threads( struct rtos *rtos )
130 {
131 int i = 0;
132 int retval;
133 int tasks_found = 0;
134 const struct FreeRTOS_params* param;
135
136 if (rtos->rtos_specific_params == NULL )
137 {
138 return -1;
139 }
140
141 param = (const struct FreeRTOS_params*) rtos->rtos_specific_params;
142
143 if ( rtos->symbols == NULL )
144 {
145 LOG_OUTPUT("No symbols for FreeRTOS\r\n");
146 return -3;
147 }
148
149 if ( rtos->symbols[FreeRTOS_VAL_uxCurrentNumberOfTasks].address == 0 )
150 {
151 LOG_OUTPUT("Don't have the number of threads in FreeRTOS \r\n");
152 return -2;
153 }
154
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);
157
158 if ( retval != ERROR_OK )
159 {
160 LOG_OUTPUT("Could not read FreeRTOS thread count from target\r\n");
161 return retval;
162 }
163
164
165 // wipe out previous thread details if any
166 if ( rtos->thread_details != NULL )
167 {
168 int j;
169 for( j = 0; j < rtos->thread_count; j++ )
170 {
171 if ( rtos->thread_details[j].display_str != NULL )
172 {
173 free( rtos->thread_details[j].display_str );
174 rtos->thread_details[j].display_str = NULL;
175 }
176 if ( rtos->thread_details[j].thread_name_str != NULL )
177 {
178 free( rtos->thread_details[j].thread_name_str );
179 rtos->thread_details[j].thread_name_str = NULL;
180 }
181 if ( rtos->thread_details[j].extra_info_str != NULL )
182 {
183 free( rtos->thread_details[j].extra_info_str );
184 rtos->thread_details[j].extra_info_str = NULL;
185 }
186 }
187 free( rtos->thread_details );
188 rtos->thread_details = NULL;
189 }
190
191
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 )
195 {
196 LOG_OUTPUT("Error reading current thread in FreeRTOS thread list\r\n");
197 return retval;
198 }
199
200 if ( ( thread_list_size == 0 ) || ( rtos->current_thread == 0 ) )
201 {
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 );
214
215
216 if ( thread_list_size == 1 )
217 {
218 rtos->thread_count = 1;
219 return ERROR_OK;
220 }
221 }
222 else
223 {
224 // create space for new thread details
225 rtos->thread_details = (struct thread_detail*) malloc( sizeof( struct thread_detail ) * thread_list_size );
226 }
227
228
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;
232
233
234 symbol_address_t* list_of_lists = (symbol_address_t *)malloc( sizeof( symbol_address_t ) * ( num_ready_task_lists + 5 ) );
235
236 int num_lists;
237 for( num_lists = 0; num_lists < num_ready_task_lists; num_lists++ )
238 {
239 list_of_lists[num_lists] = rtos->symbols[FreeRTOS_VAL_pxReadyTasksLists].address + num_lists * param->list_width;
240 }
241
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;
246
247
248 for( i = 0; i < num_lists; i++ )
249 {
250 if ( list_of_lists[i] == 0 )
251 {
252 continue;
253 }
254
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 )
259 {
260 LOG_OUTPUT("Error reading number of threads in FreeRTOS thread list\r\n");
261 return retval;
262 }
263
264 if ( list_thread_count == 0 )
265 {
266 continue;
267 }
268
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 )
274 {
275 LOG_OUTPUT("Error reading first thread item location in FreeRTOS thread list\r\n");
276 return retval;
277 }
278
279
280 while ( (list_thread_count > 0) && ( list_elem_ptr != 0) && ( list_elem_ptr != prev_list_elem_ptr ) && ( tasks_found < thread_list_size ) )
281 {
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 )
286 {
287 LOG_OUTPUT("Error reading thread list item object in FreeRTOS thread list\r\n");
288 return retval;
289 }
290
291
292 // get thread name
293
294 #define FREERTOS_THREAD_NAME_STR_SIZE (200)
295 char tmp_str[FREERTOS_THREAD_NAME_STR_SIZE];
296
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 )
300 {
301 LOG_OUTPUT("Error reading first thread item location in FreeRTOS thread list\r\n");
302 return retval;
303 }
304 tmp_str[FREERTOS_THREAD_NAME_STR_SIZE-1] = '\x00';
305
306 if ( tmp_str[0] == '\x00' )
307 {
308 strcpy(tmp_str,"No Name");
309 }
310
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;
315
316 if ( rtos->thread_details[tasks_found].threadid == rtos->current_thread )
317 {
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 );
321 }
322 else
323 {
324 rtos->thread_details[tasks_found].extra_info_str = NULL;
325 }
326
327
328 tasks_found++;
329 list_thread_count--;
330
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 )
335 {
336 LOG_OUTPUT("Error reading next thread item location in FreeRTOS thread list\r\n");
337 return retval;
338 }
339 }
340
341
342 }
343 free( list_of_lists );
344 rtos->thread_count = tasks_found;
345 return 0;
346 }
347
348 static int FreeRTOS_get_thread_reg_list(struct rtos *rtos, long long thread_id, char ** hex_reg_list )
349 {
350 int retval;
351 const struct FreeRTOS_params* param;
352 long long stack_ptr = 0;
353
354
355 *hex_reg_list = NULL;
356 if ( rtos == NULL )
357 {
358 return -1;
359 }
360
361 if ( thread_id == 0 )
362 {
363 return -2;
364 }
365
366 if (rtos->rtos_specific_params == NULL )
367 {
368 return -1;
369 }
370
371 param = (const struct FreeRTOS_params*) rtos->rtos_specific_params;
372
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 )
376 {
377 LOG_OUTPUT("Error reading stack frame from FreeRTOS thread\r\n");
378 return retval;
379 }
380
381 return rtos_generic_stack_read( rtos->target, param->stacking_info, stack_ptr, hex_reg_list );
382
383 }
384
385 static int FreeRTOS_get_symbol_list_to_lookup(symbol_table_elem_t * symbol_list[])
386 {
387 unsigned int i;
388 *symbol_list = (symbol_table_elem_t *) malloc( sizeof( symbol_table_elem_t ) * FREERTOS_NUM_SYMBOLS );
389
390 for( i = 0; i < FREERTOS_NUM_SYMBOLS; i++ )
391 {
392 (*symbol_list)[i].symbol_name = FreeRTOS_symbol_list[i];
393 }
394
395 return 0;
396 }
397
398 #if 0
399
400 static int FreeRTOS_set_current_thread(struct rtos *rtos, threadid_t thread_id)
401 {
402 return 0;
403 }
404
405
406
407 static int FreeRTOS_get_thread_ascii_info( struct rtos* rtos, threadid_t thread_id, char ** info )
408 {
409 int retval;
410 const struct FreeRTOS_params* param;
411
412 if ( rtos == NULL )
413 {
414 return -1;
415 }
416
417 if ( thread_id == 0 )
418 {
419 return -2;
420 }
421
422 if (rtos->rtos_specific_params == NULL )
423 {
424 return -3;
425 }
426
427 param = (const struct FreeRTOS_params*) rtos->rtos_specific_params;
428
429 #define FREERTOS_THREAD_NAME_STR_SIZE (200)
430 char tmp_str[FREERTOS_THREAD_NAME_STR_SIZE];
431
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 )
435 {
436 LOG_OUTPUT("Error reading first thread item location in FreeRTOS thread list\r\n");
437 return retval;
438 }
439 tmp_str[FREERTOS_THREAD_NAME_STR_SIZE-1] = '\x00';
440
441 if ( tmp_str[0] == '\x00' )
442 {
443 strcpy(tmp_str,"No Name");
444 }
445
446 *info = (char*)malloc( strlen(tmp_str)+1 );
447 strcpy( *info, tmp_str );
448 return 0;
449 }
450
451 #endif
452
453 static int FreeRTOS_detect_rtos( struct target* target )
454 {
455 if ( ( target->rtos->symbols != NULL ) &&
456 ( target->rtos->symbols[FreeRTOS_VAL_pxReadyTasksLists].address != 0 ) )
457 {
458 // looks like FreeRTOS
459 return 1;
460 }
461 return 0;
462 return 0;
463 }
464
465
466 static int FreeRTOS_create( struct target* target )
467 {
468 int i = 0;
469 while ( ( i < FREERTOS_NUM_PARAMS ) && ( 0 != strcmp( FreeRTOS_params_list[i].target_name, target->type->name ) ) )
470 {
471 i++;
472 }
473 if ( i >= FREERTOS_NUM_PARAMS )
474 {
475 LOG_OUTPUT("Could not find target in FreeRTOS compatability list\r\n");
476 return -1;
477 }
478
479 target->rtos->rtos_specific_params = (void*) &FreeRTOS_params_list[i];
480 return 0;
481 }

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)