warning fix: add self-consitency check to remove warning
[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, int64_t 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 FreeRTOS_VAL_uxTopUsedPriority = 10,
106 };
107
108 static char* FreeRTOS_symbol_list[] =
109 {
110 "pxCurrentTCB",
111 "pxReadyTasksLists",
112 "xDelayedTaskList1",
113 "xDelayedTaskList2",
114 "pxDelayedTaskList",
115 "pxOverflowDelayedTaskList",
116 "xPendingReadyList",
117 "xTasksWaitingTermination",
118 "xSuspendedTaskList",
119 "uxCurrentNumberOfTasks",
120 "uxTopUsedPriority",
121 NULL
122 };
123
124 #define FREERTOS_NUM_SYMBOLS (sizeof(FreeRTOS_symbol_list)/sizeof(char*))
125
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
130
131 static int FreeRTOS_update_threads( struct rtos *rtos )
132 {
133 int i = 0;
134 int retval;
135 int tasks_found = 0;
136 const struct FreeRTOS_params* param;
137
138 if (rtos->rtos_specific_params == NULL )
139 {
140 return -1;
141 }
142
143 param = (const struct FreeRTOS_params*) rtos->rtos_specific_params;
144
145 if ( rtos->symbols == NULL )
146 {
147 LOG_OUTPUT("No symbols for FreeRTOS\r\n");
148 return -3;
149 }
150
151 if ( rtos->symbols[FreeRTOS_VAL_uxCurrentNumberOfTasks].address == 0 )
152 {
153 LOG_OUTPUT("Don't have the number of threads in FreeRTOS \r\n");
154 return -2;
155 }
156
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);
159
160 if ( retval != ERROR_OK )
161 {
162 LOG_OUTPUT("Could not read FreeRTOS thread count from target\r\n");
163 return retval;
164 }
165
166
167 // wipe out previous thread details if any
168 if ( rtos->thread_details != NULL )
169 {
170 int j;
171 for( j = 0; j < rtos->thread_count; j++ )
172 {
173 if ( rtos->thread_details[j].display_str != NULL )
174 {
175 free( rtos->thread_details[j].display_str );
176 rtos->thread_details[j].display_str = NULL;
177 }
178 if ( rtos->thread_details[j].thread_name_str != NULL )
179 {
180 free( rtos->thread_details[j].thread_name_str );
181 rtos->thread_details[j].thread_name_str = NULL;
182 }
183 if ( rtos->thread_details[j].extra_info_str != NULL )
184 {
185 free( rtos->thread_details[j].extra_info_str );
186 rtos->thread_details[j].extra_info_str = NULL;
187 }
188 }
189 free( rtos->thread_details );
190 rtos->thread_details = NULL;
191 }
192
193
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 )
197 {
198 LOG_OUTPUT("Error reading current thread in FreeRTOS thread list\r\n");
199 return retval;
200 }
201
202 if ( ( thread_list_size == 0 ) || ( rtos->current_thread == 0 ) )
203 {
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 );
216
217
218 if ( thread_list_size == 1 )
219 {
220 rtos->thread_count = 1;
221 return ERROR_OK;
222 }
223 }
224 else
225 {
226 // create space for new thread details
227 rtos->thread_details = (struct thread_detail*) malloc( sizeof( struct thread_detail ) * thread_list_size );
228 }
229
230
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 );
234 if (retval != ERROR_OK)
235 return retval;
236
237 symbol_address_t* list_of_lists = (symbol_address_t *)malloc( sizeof( symbol_address_t ) * ( max_used_priority+1 + 5 ) );
238
239 int num_lists;
240 for( num_lists = 0; num_lists <= max_used_priority; num_lists++ )
241 {
242 list_of_lists[num_lists] = rtos->symbols[FreeRTOS_VAL_pxReadyTasksLists].address + num_lists * param->list_width;
243 }
244
245 list_of_lists[num_lists++] = rtos->symbols[FreeRTOS_VAL_xDelayedTaskList1].address;
246 list_of_lists[num_lists++] = rtos->symbols[FreeRTOS_VAL_xDelayedTaskList2].address;
247 list_of_lists[num_lists++] = rtos->symbols[FreeRTOS_VAL_xPendingReadyList].address;
248 list_of_lists[num_lists++] = rtos->symbols[FreeRTOS_VAL_xSuspendedTaskList].address;
249 list_of_lists[num_lists++] = rtos->symbols[FreeRTOS_VAL_xTasksWaitingTermination].address;
250
251
252 for( i = 0; i < num_lists; i++ )
253 {
254 if ( list_of_lists[i] == 0 )
255 {
256 continue;
257 }
258
259 // Read the number of threads in this list
260 int64_t list_thread_count = 0;
261 retval = target_read_buffer( rtos->target, list_of_lists[i], param->thread_count_width, (uint8_t *)&list_thread_count);
262 if ( retval != ERROR_OK )
263 {
264 LOG_OUTPUT("Error reading number of threads in FreeRTOS thread list\r\n");
265 return retval;
266 }
267
268 if ( list_thread_count == 0 )
269 {
270 continue;
271 }
272
273 // Read the location of first list item
274 uint64_t prev_list_elem_ptr = -1;
275 uint64_t list_elem_ptr = 0;
276 retval = target_read_buffer( rtos->target, list_of_lists[i] + param->list_next_offset, param->pointer_width, (uint8_t *)&list_elem_ptr);
277 if ( retval != ERROR_OK )
278 {
279 LOG_OUTPUT("Error reading first thread item location in FreeRTOS thread list\r\n");
280 return retval;
281 }
282
283
284 while ( (list_thread_count > 0) && ( list_elem_ptr != 0) && ( list_elem_ptr != prev_list_elem_ptr ) && ( tasks_found < thread_list_size ) )
285 {
286 // Get the location of the thread structure.
287 rtos->thread_details[tasks_found].threadid = 0;
288 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));
289 if ( retval != ERROR_OK )
290 {
291 LOG_OUTPUT("Error reading thread list item object in FreeRTOS thread list\r\n");
292 return retval;
293 }
294
295
296 // get thread name
297
298 #define FREERTOS_THREAD_NAME_STR_SIZE (200)
299 char tmp_str[FREERTOS_THREAD_NAME_STR_SIZE];
300
301 // Read the thread name
302 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);
303 if ( retval != ERROR_OK )
304 {
305 LOG_OUTPUT("Error reading first thread item location in FreeRTOS thread list\r\n");
306 return retval;
307 }
308 tmp_str[FREERTOS_THREAD_NAME_STR_SIZE-1] = '\x00';
309
310 if ( tmp_str[0] == '\x00' )
311 {
312 strcpy(tmp_str,"No Name");
313 }
314
315 rtos->thread_details[tasks_found].thread_name_str = (char*)malloc( strlen(tmp_str)+1 );
316 strcpy( rtos->thread_details[tasks_found].thread_name_str, tmp_str );
317 rtos->thread_details[tasks_found].display_str = NULL;
318 rtos->thread_details[tasks_found].exists = true;
319
320 if ( rtos->thread_details[tasks_found].threadid == rtos->current_thread )
321 {
322 char running_str[] = "Running";
323 rtos->thread_details[tasks_found].extra_info_str = (char*) malloc( sizeof(running_str) );
324 strcpy( rtos->thread_details[tasks_found].extra_info_str, running_str );
325 }
326 else
327 {
328 rtos->thread_details[tasks_found].extra_info_str = NULL;
329 }
330
331
332 tasks_found++;
333 list_thread_count--;
334
335 prev_list_elem_ptr = list_elem_ptr;
336 list_elem_ptr = 0;
337 retval = target_read_buffer( rtos->target, prev_list_elem_ptr + param->list_elem_next_offset, param->pointer_width, (uint8_t *)&list_elem_ptr);
338 if ( retval != ERROR_OK )
339 {
340 LOG_OUTPUT("Error reading next thread item location in FreeRTOS thread list\r\n");
341 return retval;
342 }
343 }
344
345
346 }
347 free( list_of_lists );
348 rtos->thread_count = tasks_found;
349 return 0;
350 }
351
352 static int FreeRTOS_get_thread_reg_list(struct rtos *rtos, int64_t thread_id, char ** hex_reg_list )
353 {
354 int retval;
355 const struct FreeRTOS_params* param;
356 int64_t stack_ptr = 0;
357
358
359 *hex_reg_list = NULL;
360 if ( rtos == NULL )
361 {
362 return -1;
363 }
364
365 if ( thread_id == 0 )
366 {
367 return -2;
368 }
369
370 if (rtos->rtos_specific_params == NULL )
371 {
372 return -1;
373 }
374
375 param = (const struct FreeRTOS_params*) rtos->rtos_specific_params;
376
377 // Read the stack pointer
378 retval = target_read_buffer( rtos->target, thread_id + param->thread_stack_offset, param->pointer_width, (uint8_t*)&stack_ptr);
379 if ( retval != ERROR_OK )
380 {
381 LOG_OUTPUT("Error reading stack frame from FreeRTOS thread\r\n");
382 return retval;
383 }
384
385 return rtos_generic_stack_read( rtos->target, param->stacking_info, stack_ptr, hex_reg_list );
386
387 }
388
389 static int FreeRTOS_get_symbol_list_to_lookup(symbol_table_elem_t * symbol_list[])
390 {
391 unsigned int i;
392 *symbol_list = (symbol_table_elem_t *) malloc( sizeof( symbol_table_elem_t ) * FREERTOS_NUM_SYMBOLS );
393
394 for( i = 0; i < FREERTOS_NUM_SYMBOLS; i++ )
395 {
396 (*symbol_list)[i].symbol_name = FreeRTOS_symbol_list[i];
397 }
398
399 return 0;
400 }
401
402 #if 0
403
404 static int FreeRTOS_set_current_thread(struct rtos *rtos, threadid_t thread_id)
405 {
406 return 0;
407 }
408
409
410
411 static int FreeRTOS_get_thread_ascii_info( struct rtos* rtos, threadid_t thread_id, char ** info )
412 {
413 int retval;
414 const struct FreeRTOS_params* param;
415
416 if ( rtos == NULL )
417 {
418 return -1;
419 }
420
421 if ( thread_id == 0 )
422 {
423 return -2;
424 }
425
426 if (rtos->rtos_specific_params == NULL )
427 {
428 return -3;
429 }
430
431 param = (const struct FreeRTOS_params*) rtos->rtos_specific_params;
432
433 #define FREERTOS_THREAD_NAME_STR_SIZE (200)
434 char tmp_str[FREERTOS_THREAD_NAME_STR_SIZE];
435
436 // Read the thread name
437 retval = target_read_buffer( rtos->target, thread_id + param->thread_name_offset, FREERTOS_THREAD_NAME_STR_SIZE, (uint8_t *)&tmp_str);
438 if ( retval != ERROR_OK )
439 {
440 LOG_OUTPUT("Error reading first thread item location in FreeRTOS thread list\r\n");
441 return retval;
442 }
443 tmp_str[FREERTOS_THREAD_NAME_STR_SIZE-1] = '\x00';
444
445 if ( tmp_str[0] == '\x00' )
446 {
447 strcpy(tmp_str,"No Name");
448 }
449
450 *info = (char*)malloc( strlen(tmp_str)+1 );
451 strcpy( *info, tmp_str );
452 return 0;
453 }
454
455 #endif
456
457 static int FreeRTOS_detect_rtos( struct target* target )
458 {
459 if ( ( target->rtos->symbols != NULL ) &&
460 ( target->rtos->symbols[FreeRTOS_VAL_pxReadyTasksLists].address != 0 ) )
461 {
462 // looks like FreeRTOS
463 return 1;
464 }
465 return 0;
466 return 0;
467 }
468
469
470 static int FreeRTOS_create( struct target* target )
471 {
472 int i = 0;
473 while ( ( i < FREERTOS_NUM_PARAMS ) && ( 0 != strcmp( FreeRTOS_params_list[i].target_name, target->type->name ) ) )
474 {
475 i++;
476 }
477 if ( i >= FREERTOS_NUM_PARAMS )
478 {
479 LOG_OUTPUT("Could not find target in FreeRTOS compatibility list\r\n");
480 return -1;
481 }
482
483 target->rtos->rtos_specific_params = (void*) &FreeRTOS_params_list[i];
484 return 0;
485 }
486

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)