2 * FreeRTOS Kernel V10.3.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.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
28 /* FreeRTOS includes. */
\r
29 #include "FreeRTOS.h"
\r
32 /* FreeRTOS+CLI includes. */
\r
33 #include "FreeRTOS_CLI.h"
\r
36 * Writes trace data to a disk file when the trace recording is stopped.
\r
37 * This function will simply overwrite any trace files that already exist.
\r
39 static void prvSaveTraceFile( void );
\r
42 * Defines a command that returns a table showing the state of each task at the
\r
43 * time the command is called.
\r
45 static BaseType_t prvTaskStatsCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
48 * Defines a command that returns a table showing how much time each task has
\r
49 * spent in the Running state.
\r
51 static BaseType_t prvRunTimeStatsCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
54 * Defines a command that expects exactly three parameters. Each of the three
\r
55 * parameter are echoed back one at a time.
\r
57 static BaseType_t prvThreeParameterEchoCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
60 * Defines a command that can take a variable number of parameters. Each
\r
61 * parameter is echoes back one at a time.
\r
63 static BaseType_t prvParameterEchoCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
66 * Defines a command that starts/stops events being recorded for offline viewing
\r
67 * in FreeRTOS+Trace.
\r
69 static BaseType_t prvStartStopTraceCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
71 /* Structure that defines the "run-time-stats" command line command. */
\r
72 static const CLI_Command_Definition_t xRunTimeStats =
\r
74 "run-time-stats", /* The command string to type. */
\r
75 "\r\nrun-time-stats:\r\n Displays a table showing how much processing time each FreeRTOS task has used\r\n\r\n",
\r
76 prvRunTimeStatsCommand, /* The function to run. */
\r
77 0 /* No parameters are expected. */
\r
80 /* Structure that defines the "task-stats" command line command. */
\r
81 static const CLI_Command_Definition_t xTaskStats =
\r
83 "task-stats", /* The command string to type. */
\r
84 "\r\ntask-stats:\r\n Displays a table showing the state of each FreeRTOS task\r\n\r\n",
\r
85 prvTaskStatsCommand, /* The function to run. */
\r
86 0 /* No parameters are expected. */
\r
89 /* Structure that defines the "echo_3_parameters" command line command. This
\r
90 takes exactly three parameters that the command simply echos back one at a
\r
92 static const CLI_Command_Definition_t xThreeParameterEcho =
\r
94 "echo_3_parameters",
\r
95 "\r\necho_3_parameters <param1> <param2> <param3>:\r\n Expects three parameters, echos each in turn\r\n\r\n",
\r
96 prvThreeParameterEchoCommand, /* The function to run. */
\r
97 3 /* Three parameters are expected, which can take any value. */
\r
100 /* Structure that defines the "echo_parameters" command line command. This
\r
101 takes a variable number of parameters that the command simply echos back one at
\r
103 static const CLI_Command_Definition_t xParameterEcho =
\r
106 "\r\necho_parameters <...>:\r\n Take variable number of parameters, echos each in turn\r\n\r\n",
\r
107 prvParameterEchoCommand, /* The function to run. */
\r
108 -1 /* The user can enter any number of commands. */
\r
111 /* Structure that defines the "trace" command line command. This takes a single
\r
112 parameter, which can be either "start" or "stop". */
\r
113 static const CLI_Command_Definition_t xStartTrace =
\r
116 "\r\ntrace [start | stop]:\r\n Starts or stops a trace recording for viewing in FreeRTOS+Trace\r\n\r\n",
\r
117 prvStartStopTraceCommand, /* The function to run. */
\r
118 1 /* One parameter is expected. Valid values are "start" and "stop". */
\r
121 /*-----------------------------------------------------------*/
\r
123 void vRegisterCLICommands( void )
\r
125 /* Register all the command line commands defined immediately above. */
\r
126 FreeRTOS_CLIRegisterCommand( &xTaskStats );
\r
127 FreeRTOS_CLIRegisterCommand( &xRunTimeStats );
\r
128 FreeRTOS_CLIRegisterCommand( &xThreeParameterEcho );
\r
129 FreeRTOS_CLIRegisterCommand( &xParameterEcho );
\r
130 FreeRTOS_CLIRegisterCommand( &xStartTrace );
\r
132 /*-----------------------------------------------------------*/
\r
134 static BaseType_t prvTaskStatsCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
136 const char *const pcHeader = "Task State Priority Stack #\r\n************************************************\r\n";
\r
138 /* Remove compile time warnings about unused parameters, and check the
\r
139 write buffer is not NULL. NOTE - for simplicity, this example assumes the
\r
140 write buffer length is adequate, so does not check for buffer overflows. */
\r
141 ( void ) pcCommandString;
\r
142 ( void ) xWriteBufferLen;
\r
143 configASSERT( pcWriteBuffer );
\r
145 /* Generate a table of task stats. */
\r
146 strcpy( pcWriteBuffer, pcHeader );
\r
147 vTaskList( pcWriteBuffer + strlen( pcHeader ) );
\r
149 /* There is no more data to return after this single string, so return
\r
153 /*-----------------------------------------------------------*/
\r
155 static BaseType_t prvRunTimeStatsCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
157 const char * const pcHeader = "Task Abs Time % Time\r\n****************************************\r\n";
\r
159 /* Remove compile time warnings about unused parameters, and check the
\r
160 write buffer is not NULL. NOTE - for simplicity, this example assumes the
\r
161 write buffer length is adequate, so does not check for buffer overflows. */
\r
162 ( void ) pcCommandString;
\r
163 ( void ) xWriteBufferLen;
\r
164 configASSERT( pcWriteBuffer );
\r
166 /* Generate a table of task stats. */
\r
167 strcpy( pcWriteBuffer, pcHeader );
\r
168 vTaskGetRunTimeStats( pcWriteBuffer + strlen( pcHeader ) );
\r
170 /* There is no more data to return after this single string, so return
\r
174 /*-----------------------------------------------------------*/
\r
176 static BaseType_t prvThreeParameterEchoCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
178 const char *pcParameter;
\r
179 BaseType_t lParameterStringLength, xReturn;
\r
180 static BaseType_t lParameterNumber = 0;
\r
182 /* Remove compile time warnings about unused parameters, and check the
\r
183 write buffer is not NULL. NOTE - for simplicity, this example assumes the
\r
184 write buffer length is adequate, so does not check for buffer overflows. */
\r
185 ( void ) pcCommandString;
\r
186 ( void ) xWriteBufferLen;
\r
187 configASSERT( pcWriteBuffer );
\r
189 if( lParameterNumber == 0 )
\r
191 /* The first time the function is called after the command has been
\r
192 entered just a header string is returned. */
\r
193 sprintf( pcWriteBuffer, "The three parameters were:\r\n" );
\r
195 /* Next time the function is called the first parameter will be echoed
\r
197 lParameterNumber = 1L;
\r
199 /* There is more data to be returned as no parameters have been echoed
\r
205 /* Obtain the parameter string. */
\r
206 pcParameter = FreeRTOS_CLIGetParameter
\r
208 pcCommandString, /* The command string itself. */
\r
209 lParameterNumber, /* Return the next parameter. */
\r
210 &lParameterStringLength /* Store the parameter string length. */
\r
213 /* Sanity check something was returned. */
\r
214 configASSERT( pcParameter );
\r
216 /* Return the parameter string. */
\r
217 memset( pcWriteBuffer, 0x00, xWriteBufferLen );
\r
218 sprintf( pcWriteBuffer, "%d: ", lParameterNumber );
\r
219 strncat( pcWriteBuffer, pcParameter, lParameterStringLength );
\r
220 strncat( pcWriteBuffer, "\r\n", strlen( "\r\n" ) );
\r
222 /* If this is the last of the three parameters then there are no more
\r
223 strings to return after this one. */
\r
224 if( lParameterNumber == 3L )
\r
226 /* If this is the last of the three parameters then there are no more
\r
227 strings to return after this one. */
\r
229 lParameterNumber = 0L;
\r
233 /* There are more parameters to return after this one. */
\r
235 lParameterNumber++;
\r
241 /*-----------------------------------------------------------*/
\r
243 static BaseType_t prvParameterEchoCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
245 const char *pcParameter;
\r
246 BaseType_t lParameterStringLength, xReturn;
\r
247 static BaseType_t lParameterNumber = 0;
\r
249 /* Remove compile time warnings about unused parameters, and check the
\r
250 write buffer is not NULL. NOTE - for simplicity, this example assumes the
\r
251 write buffer length is adequate, so does not check for buffer overflows. */
\r
252 ( void ) pcCommandString;
\r
253 ( void ) xWriteBufferLen;
\r
254 configASSERT( pcWriteBuffer );
\r
256 if( lParameterNumber == 0 )
\r
258 /* The first time the function is called after the command has been
\r
259 entered just a header string is returned. */
\r
260 sprintf( pcWriteBuffer, "The parameters were:\r\n" );
\r
262 /* Next time the function is called the first parameter will be echoed
\r
264 lParameterNumber = 1L;
\r
266 /* There is more data to be returned as no parameters have been echoed
\r
272 /* Obtain the parameter string. */
\r
273 pcParameter = FreeRTOS_CLIGetParameter
\r
275 pcCommandString, /* The command string itself. */
\r
276 lParameterNumber, /* Return the next parameter. */
\r
277 &lParameterStringLength /* Store the parameter string length. */
\r
280 if( pcParameter != NULL )
\r
282 /* Return the parameter string. */
\r
283 memset( pcWriteBuffer, 0x00, xWriteBufferLen );
\r
284 sprintf( pcWriteBuffer, "%d: ", lParameterNumber );
\r
285 strncat( pcWriteBuffer, pcParameter, lParameterStringLength );
\r
286 strncat( pcWriteBuffer, "\r\n", strlen( "\r\n" ) );
\r
288 /* There might be more parameters to return after this one. */
\r
290 lParameterNumber++;
\r
294 /* No more parameters were found. Make sure the write buffer does
\r
295 not contain a valid string. */
\r
296 pcWriteBuffer[ 0 ] = 0x00;
\r
298 /* No more data to return. */
\r
301 /* Start over the next time this command is executed. */
\r
302 lParameterNumber = 0;
\r
308 /*-----------------------------------------------------------*/
\r
310 static BaseType_t prvStartStopTraceCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
312 const char *pcParameter;
\r
313 BaseType_t lParameterStringLength;
\r
315 /* Remove compile time warnings about unused parameters, and check the
\r
316 write buffer is not NULL. NOTE - for simplicity, this example assumes the
\r
317 write buffer length is adequate, so does not check for buffer overflows. */
\r
318 ( void ) pcCommandString;
\r
319 ( void ) xWriteBufferLen;
\r
320 configASSERT( pcWriteBuffer );
\r
322 /* Obtain the parameter string. */
\r
323 pcParameter = FreeRTOS_CLIGetParameter
\r
325 pcCommandString, /* The command string itself. */
\r
326 1, /* Return the first parameter. */
\r
327 &lParameterStringLength /* Store the parameter string length. */
\r
330 /* Sanity check something was returned. */
\r
331 configASSERT( pcParameter );
\r
333 /* There are only two valid parameter values. */
\r
334 if( strncmp( pcParameter, "start", strlen( "start" ) ) == 0 )
\r
336 /* Start or restart the trace. */
\r
341 sprintf( pcWriteBuffer, "Trace recording (re)started.\r\n" );
\r
343 else if( strncmp( pcParameter, "stop", strlen( "stop" ) ) == 0 )
\r
345 /* End the trace, if one is running. */
\r
347 sprintf( pcWriteBuffer, "Stopping trace recording and dumping log to disk.\r\n" );
\r
348 prvSaveTraceFile();
\r
352 sprintf( pcWriteBuffer, "Valid parameters are 'start' and 'stop'.\r\n" );
\r
355 /* There is no more data to return after this single string, so return
\r
359 /*-----------------------------------------------------------*/
\r
361 static void prvSaveTraceFile( void )
\r
363 FILE* pxOutputFile;
\r
365 fopen_s( &pxOutputFile, "Trace.dump", "wb");
\r
367 if( pxOutputFile != NULL )
\r
369 fwrite( RecorderDataPtr, sizeof( RecorderDataType ), 1, pxOutputFile );
\r
370 fclose( pxOutputFile );
\r
371 printf( "\r\nTrace output saved to Trace.dump\r\n" );
\r
375 printf( "\r\nFailed to create trace dump file\r\n" );
\r