2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
29 /* FreeRTOS includes. */
\r
30 #include "FreeRTOS.h"
\r
33 /* FreeRTOS+CLI includes. */
\r
34 #include "FreeRTOS_CLI.h"
\r
37 * Writes trace data to a disk file when the trace recording is stopped.
\r
38 * This function will simply overwrite any trace files that already exist.
\r
40 static void prvSaveTraceFile( void );
\r
43 * Defines a command that returns a table showing the state of each task at the
\r
44 * time the command is called.
\r
46 static BaseType_t prvTaskStatsCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
49 * Defines a command that returns a table showing how much time each task has
\r
50 * spent in the Running state.
\r
52 static BaseType_t prvRunTimeStatsCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
55 * Defines a command that expects exactly three parameters. Each of the three
\r
56 * parameter are echoed back one at a time.
\r
58 static BaseType_t prvThreeParameterEchoCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
61 * Defines a command that can take a variable number of parameters. Each
\r
62 * parameter is echoes back one at a time.
\r
64 static BaseType_t prvParameterEchoCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
67 * Defines a command that starts/stops events being recorded for offline viewing
\r
68 * in FreeRTOS+Trace.
\r
70 static BaseType_t prvStartStopTraceCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
72 /* Structure that defines the "run-time-stats" command line command. */
\r
73 static const CLI_Command_Definition_t xRunTimeStats =
\r
75 "run-time-stats", /* The command string to type. */
\r
76 "\r\nrun-time-stats:\r\n Displays a table showing how much processing time each FreeRTOS task has used\r\n\r\n",
\r
77 prvRunTimeStatsCommand, /* The function to run. */
\r
78 0 /* No parameters are expected. */
\r
81 /* Structure that defines the "task-stats" command line command. */
\r
82 static const CLI_Command_Definition_t xTaskStats =
\r
84 "task-stats", /* The command string to type. */
\r
85 "\r\ntask-stats:\r\n Displays a table showing the state of each FreeRTOS task\r\n\r\n",
\r
86 prvTaskStatsCommand, /* The function to run. */
\r
87 0 /* No parameters are expected. */
\r
90 /* Structure that defines the "echo_3_parameters" command line command. This
\r
91 takes exactly three parameters that the command simply echos back one at a
\r
93 static const CLI_Command_Definition_t xThreeParameterEcho =
\r
95 "echo_3_parameters",
\r
96 "\r\necho_3_parameters <param1> <param2> <param3>:\r\n Expects three parameters, echos each in turn\r\n\r\n",
\r
97 prvThreeParameterEchoCommand, /* The function to run. */
\r
98 3 /* Three parameters are expected, which can take any value. */
\r
101 /* Structure that defines the "echo_parameters" command line command. This
\r
102 takes a variable number of parameters that the command simply echos back one at
\r
104 static const CLI_Command_Definition_t xParameterEcho =
\r
107 "\r\necho_parameters <...>:\r\n Take variable number of parameters, echos each in turn\r\n\r\n",
\r
108 prvParameterEchoCommand, /* The function to run. */
\r
109 -1 /* The user can enter any number of commands. */
\r
112 /* Structure that defines the "trace" command line command. This takes a single
\r
113 parameter, which can be either "start" or "stop". */
\r
114 static const CLI_Command_Definition_t xStartTrace =
\r
117 "\r\ntrace [start | stop]:\r\n Starts or stops a trace recording for viewing in FreeRTOS+Trace\r\n\r\n",
\r
118 prvStartStopTraceCommand, /* The function to run. */
\r
119 1 /* One parameter is expected. Valid values are "start" and "stop". */
\r
122 /*-----------------------------------------------------------*/
\r
124 void vRegisterCLICommands( void )
\r
126 /* Register all the command line commands defined immediately above. */
\r
127 FreeRTOS_CLIRegisterCommand( &xTaskStats );
\r
128 FreeRTOS_CLIRegisterCommand( &xRunTimeStats );
\r
129 FreeRTOS_CLIRegisterCommand( &xThreeParameterEcho );
\r
130 FreeRTOS_CLIRegisterCommand( &xParameterEcho );
\r
131 FreeRTOS_CLIRegisterCommand( &xStartTrace );
\r
133 /*-----------------------------------------------------------*/
\r
135 static BaseType_t prvTaskStatsCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
137 const char *const pcHeader = "Task State Priority Stack #\r\n************************************************\r\n";
\r
139 /* Remove compile time warnings about unused parameters, and check the
\r
140 write buffer is not NULL. NOTE - for simplicity, this example assumes the
\r
141 write buffer length is adequate, so does not check for buffer overflows. */
\r
142 ( void ) pcCommandString;
\r
143 ( void ) xWriteBufferLen;
\r
144 configASSERT( pcWriteBuffer );
\r
146 /* Generate a table of task stats. */
\r
147 strcpy( pcWriteBuffer, pcHeader );
\r
148 vTaskList( pcWriteBuffer + strlen( pcHeader ) );
\r
150 /* There is no more data to return after this single string, so return
\r
154 /*-----------------------------------------------------------*/
\r
156 static BaseType_t prvRunTimeStatsCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
158 const char * const pcHeader = "Task Abs Time % Time\r\n****************************************\r\n";
\r
160 /* Remove compile time warnings about unused parameters, and check the
\r
161 write buffer is not NULL. NOTE - for simplicity, this example assumes the
\r
162 write buffer length is adequate, so does not check for buffer overflows. */
\r
163 ( void ) pcCommandString;
\r
164 ( void ) xWriteBufferLen;
\r
165 configASSERT( pcWriteBuffer );
\r
167 /* Generate a table of task stats. */
\r
168 strcpy( pcWriteBuffer, pcHeader );
\r
169 vTaskGetRunTimeStats( pcWriteBuffer + strlen( pcHeader ) );
\r
171 /* There is no more data to return after this single string, so return
\r
175 /*-----------------------------------------------------------*/
\r
177 static BaseType_t prvThreeParameterEchoCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
179 const char *pcParameter;
\r
180 BaseType_t lParameterStringLength, xReturn;
\r
181 static BaseType_t lParameterNumber = 0;
\r
183 /* Remove compile time warnings about unused parameters, and check the
\r
184 write buffer is not NULL. NOTE - for simplicity, this example assumes the
\r
185 write buffer length is adequate, so does not check for buffer overflows. */
\r
186 ( void ) pcCommandString;
\r
187 ( void ) xWriteBufferLen;
\r
188 configASSERT( pcWriteBuffer );
\r
190 if( lParameterNumber == 0 )
\r
192 /* The first time the function is called after the command has been
\r
193 entered just a header string is returned. */
\r
194 sprintf( pcWriteBuffer, "The three parameters were:\r\n" );
\r
196 /* Next time the function is called the first parameter will be echoed
\r
198 lParameterNumber = 1L;
\r
200 /* There is more data to be returned as no parameters have been echoed
\r
206 /* Obtain the parameter string. */
\r
207 pcParameter = FreeRTOS_CLIGetParameter
\r
209 pcCommandString, /* The command string itself. */
\r
210 lParameterNumber, /* Return the next parameter. */
\r
211 &lParameterStringLength /* Store the parameter string length. */
\r
214 /* Sanity check something was returned. */
\r
215 configASSERT( pcParameter );
\r
217 /* Return the parameter string. */
\r
218 memset( pcWriteBuffer, 0x00, xWriteBufferLen );
\r
219 sprintf( pcWriteBuffer, "%d: ", lParameterNumber );
\r
220 strncat( pcWriteBuffer, pcParameter, lParameterStringLength );
\r
221 strncat( pcWriteBuffer, "\r\n", strlen( "\r\n" ) );
\r
223 /* If this is the last of the three parameters then there are no more
\r
224 strings to return after this one. */
\r
225 if( lParameterNumber == 3L )
\r
227 /* If this is the last of the three parameters then there are no more
\r
228 strings to return after this one. */
\r
230 lParameterNumber = 0L;
\r
234 /* There are more parameters to return after this one. */
\r
236 lParameterNumber++;
\r
242 /*-----------------------------------------------------------*/
\r
244 static BaseType_t prvParameterEchoCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
246 const char *pcParameter;
\r
247 BaseType_t lParameterStringLength, xReturn;
\r
248 static BaseType_t lParameterNumber = 0;
\r
250 /* Remove compile time warnings about unused parameters, and check the
\r
251 write buffer is not NULL. NOTE - for simplicity, this example assumes the
\r
252 write buffer length is adequate, so does not check for buffer overflows. */
\r
253 ( void ) pcCommandString;
\r
254 ( void ) xWriteBufferLen;
\r
255 configASSERT( pcWriteBuffer );
\r
257 if( lParameterNumber == 0 )
\r
259 /* The first time the function is called after the command has been
\r
260 entered just a header string is returned. */
\r
261 sprintf( pcWriteBuffer, "The parameters were:\r\n" );
\r
263 /* Next time the function is called the first parameter will be echoed
\r
265 lParameterNumber = 1L;
\r
267 /* There is more data to be returned as no parameters have been echoed
\r
273 /* Obtain the parameter string. */
\r
274 pcParameter = FreeRTOS_CLIGetParameter
\r
276 pcCommandString, /* The command string itself. */
\r
277 lParameterNumber, /* Return the next parameter. */
\r
278 &lParameterStringLength /* Store the parameter string length. */
\r
281 if( pcParameter != NULL )
\r
283 /* Return the parameter string. */
\r
284 memset( pcWriteBuffer, 0x00, xWriteBufferLen );
\r
285 sprintf( pcWriteBuffer, "%d: ", lParameterNumber );
\r
286 strncat( pcWriteBuffer, pcParameter, lParameterStringLength );
\r
287 strncat( pcWriteBuffer, "\r\n", strlen( "\r\n" ) );
\r
289 /* There might be more parameters to return after this one. */
\r
291 lParameterNumber++;
\r
295 /* No more parameters were found. Make sure the write buffer does
\r
296 not contain a valid string. */
\r
297 pcWriteBuffer[ 0 ] = 0x00;
\r
299 /* No more data to return. */
\r
302 /* Start over the next time this command is executed. */
\r
303 lParameterNumber = 0;
\r
309 /*-----------------------------------------------------------*/
\r
311 static BaseType_t prvStartStopTraceCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
313 const char *pcParameter;
\r
314 BaseType_t lParameterStringLength;
\r
316 /* Remove compile time warnings about unused parameters, and check the
\r
317 write buffer is not NULL. NOTE - for simplicity, this example assumes the
\r
318 write buffer length is adequate, so does not check for buffer overflows. */
\r
319 ( void ) pcCommandString;
\r
320 ( void ) xWriteBufferLen;
\r
321 configASSERT( pcWriteBuffer );
\r
323 /* Obtain the parameter string. */
\r
324 pcParameter = FreeRTOS_CLIGetParameter
\r
326 pcCommandString, /* The command string itself. */
\r
327 1, /* Return the first parameter. */
\r
328 &lParameterStringLength /* Store the parameter string length. */
\r
331 /* Sanity check something was returned. */
\r
332 configASSERT( pcParameter );
\r
334 /* There are only two valid parameter values. */
\r
335 if( strncmp( pcParameter, "start", strlen( "start" ) ) == 0 )
\r
337 /* Start or restart the trace. */
\r
342 sprintf( pcWriteBuffer, "Trace recording (re)started.\r\n" );
\r
344 else if( strncmp( pcParameter, "stop", strlen( "stop" ) ) == 0 )
\r
346 /* End the trace, if one is running. */
\r
348 sprintf( pcWriteBuffer, "Stopping trace recording and dumping log to disk.\r\n" );
\r
349 prvSaveTraceFile();
\r
353 sprintf( pcWriteBuffer, "Valid parameters are 'start' and 'stop'.\r\n" );
\r
356 /* There is no more data to return after this single string, so return
\r
360 /*-----------------------------------------------------------*/
\r
362 static void prvSaveTraceFile( void )
\r
364 FILE* pxOutputFile;
\r
366 fopen_s( &pxOutputFile, "Trace.dump", "wb");
\r
368 if( pxOutputFile != NULL )
\r
370 fwrite( RecorderDataPtr, sizeof( RecorderDataType ), 1, pxOutputFile );
\r
371 fclose( pxOutputFile );
\r
372 printf( "\r\nTrace output saved to Trace.dump\r\n" );
\r
376 printf( "\r\nFailed to create trace dump file\r\n" );
\r