]> git.proxmox.com Git - mirror_edk2.git/blobdiff - PerformancePkg/Dp_App/Dp.c
PerformancePkg/Dp_App: Add check to avoid NULL pointer deference
[mirror_edk2.git] / PerformancePkg / Dp_App / Dp.c
index 50598921922488070aca1969aeb99aa29ee5d3d4..4cdc39edb42e75ccf0221839550df5fa9fab0f2b 100644 (file)
@@ -13,7 +13,8 @@
   Dp uses this information to group records in different ways.  It also uses\r
   timer information to calculate elapsed time for each measurement.\r
  \r
-  Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.<BR>\r
+  (C) Copyright 2015-2016 Hewlett Packard Enterprise Development LP<BR>\r
   This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
   which accompanies this distribution.  The full text of the license may be found at\r
 **/\r
 \r
 #include <Library/UefiApplicationEntryPoint.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
 #include <Library/ShellLib.h>\r
 #include <Library/BaseLib.h>\r
 #include <Library/MemoryAllocationLib.h>\r
 #include <Library/DebugLib.h>\r
-#include <Library/TimerLib.h>\r
 #include <Library/UefiLib.h>\r
+#include <Library/UefiHiiServicesLib.h>\r
 #include <Library/HiiLib.h>\r
 #include <Library/PcdLib.h>\r
 \r
 #include "Literals.h"\r
 #include "DpInternal.h"\r
 \r
+//\r
+// String token ID of help message text.\r
+// Shell supports to find help message in the resource section of an application image if\r
+// .MAN file is not found. This global variable is added to make build tool recognizes\r
+// that the help string is consumed by user and then build tool will add the string into\r
+// the resource section. Thus the application can use '-?' option to show help message in\r
+// Shell.\r
+//\r
+GLOBAL_REMOVE_IF_UNREFERENCED EFI_STRING_ID mDpStrEngHelpTokenId = STRING_TOKEN (STR_DP_HELP_INFORMATION);\r
+\r
 //\r
 /// Module-Global Variables\r
 ///@{\r
@@ -49,6 +61,7 @@ CHAR16           *mPrintTokenBuffer = NULL;
 CHAR16           mGaugeString[DP_GAUGE_STRING_LENGTH + 1];\r
 CHAR16           mUnicodeToken[DXE_PERFORMANCE_STRING_SIZE];\r
 UINT64           mInterestThreshold;\r
+BOOLEAN          mShowId = FALSE;\r
 \r
 PERF_SUMMARY_DATA SummaryData = { 0 };    ///< Create the SummaryData structure and init. to ZERO.\r
 \r
@@ -79,6 +92,8 @@ PARAM_ITEM_LIST  ParamList[] = {
   {STRING_TOKEN (STR_DP_OPTION_UT), TypeFlag},   // -T   Dump Trace Data\r
 #endif\r
   {STRING_TOKEN (STR_DP_OPTION_LX), TypeFlag},   // -x   eXclude Cumulative Items\r
+  {STRING_TOKEN (STR_DP_OPTION_LI), TypeFlag},   // -i   Display Identifier\r
+  {STRING_TOKEN (STR_DP_OPTION_LC), TypeValue},  // -c   Display cumulative data.\r
   {STRING_TOKEN (STR_DP_OPTION_LN), TypeValue},  // -n # Number of records to display for A and R\r
   {STRING_TOKEN (STR_DP_OPTION_LT), TypeValue}   // -t # Threshold of interest\r
   };\r
@@ -98,7 +113,7 @@ InitialShellParamList( void )
   //\r
   // Allocate one more for the end tag.\r
   //\r
-  ListLength = sizeof (ParamList) / sizeof (ParamList[0]) + 1;  \r
+  ListLength = ARRAY_SIZE (ParamList) + 1;  \r
   DpParamList = AllocatePool (sizeof (SHELL_PARAM_ITEM) * ListLength);\r
   ASSERT (DpParamList != NULL);\r
   \r
@@ -135,6 +150,8 @@ ShowHelp( void )
 #endif // PROFILING_IMPLEMENTED\r
   PrintToken (STRING_TOKEN (STR_DP_HELP_THRESHOLD));\r
   PrintToken (STRING_TOKEN (STR_DP_HELP_COUNT));\r
+  PrintToken (STRING_TOKEN (STR_DP_HELP_ID));\r
+  PrintToken (STRING_TOKEN (STR_DP_HELP_CUM_DATA));\r
   PrintToken (STRING_TOKEN (STR_DP_HELP_HELP));\r
   Print(L"\n");\r
 }\r
@@ -165,16 +182,16 @@ DumpStatistics( void )
   FreePool (StringPtrUnknown);\r
 }\r
 \r
-/** \r
+/**\r
   Dump performance data.\r
   \r
   @param[in]  ImageHandle     The image handle.\r
   @param[in]  SystemTable     The system table.\r
-  \r
+\r
   @retval EFI_SUCCESS            Command completed successfully.\r
   @retval EFI_INVALID_PARAMETER  Command usage error.\r
+  @retval EFI_ABORTED            The user aborts the operation.\r
   @retval value                  Unknown error.\r
-  \r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
@@ -183,23 +200,27 @@ InitializeDp (
   IN EFI_SYSTEM_TABLE         *SystemTable\r
   )\r
 {\r
-  UINT64                    Freq;\r
-  UINT64                    Ticker;\r
-  UINT32                    ListIndex;\r
-  \r
-  LIST_ENTRY                *ParamPackage;\r
-  CONST CHAR16              *CmdLineArg;\r
-  EFI_STRING                StringPtr;\r
-  UINTN                     Number2Display;\r
-\r
-  EFI_STATUS                Status;\r
-  BOOLEAN                   SummaryMode;\r
-  BOOLEAN                   VerboseMode;\r
-  BOOLEAN                   AllMode;\r
-  BOOLEAN                   RawMode;\r
-  BOOLEAN                   TraceMode;\r
-  BOOLEAN                   ProfileMode;\r
-  BOOLEAN                   ExcludeMode;\r
+  PERFORMANCE_PROPERTY          *PerformanceProperty;\r
+  UINT32                        ListIndex;\r
+\r
+  LIST_ENTRY                    *ParamPackage;\r
+  CONST CHAR16                  *CmdLineArg;\r
+  EFI_STRING                    StringPtr;\r
+  UINTN                         Number2Display;\r
+\r
+  EFI_STATUS                    Status;\r
+  BOOLEAN                       SummaryMode;\r
+  BOOLEAN                       VerboseMode;\r
+  BOOLEAN                       AllMode;\r
+  BOOLEAN                       RawMode;\r
+  BOOLEAN                       TraceMode;\r
+  BOOLEAN                       ProfileMode;\r
+  BOOLEAN                       ExcludeMode;\r
+  BOOLEAN                       CumulativeMode;\r
+  CONST CHAR16                  *CustomCumulativeToken;\r
+  PERF_CUM_DATA                 *CustomCumulativeData;\r
+  UINTN                         NameSize;\r
+  EFI_HII_PACKAGE_LIST_HEADER   *PackageList;\r
 \r
   EFI_STRING                StringDpOptionQh;\r
   EFI_STRING                StringDpOptionLh;\r
@@ -214,6 +235,8 @@ InitializeDp (
   EFI_STRING                StringDpOptionLx;\r
   EFI_STRING                StringDpOptionLn;\r
   EFI_STRING                StringDpOptionLt;\r
+  EFI_STRING                StringDpOptionLi;\r
+  EFI_STRING                StringDpOptionLc;\r
   \r
   SummaryMode     = FALSE;\r
   VerboseMode     = FALSE;\r
@@ -222,6 +245,8 @@ InitializeDp (
   TraceMode       = FALSE;\r
   ProfileMode     = FALSE;\r
   ExcludeMode     = FALSE;\r
+  CumulativeMode = FALSE;\r
+  CustomCumulativeData = NULL;\r
 \r
   StringDpOptionQh = NULL;\r
   StringDpOptionLh = NULL;\r
@@ -236,17 +261,39 @@ InitializeDp (
   StringDpOptionLx = NULL;\r
   StringDpOptionLn = NULL;\r
   StringDpOptionLt = NULL;\r
+  StringDpOptionLi = NULL;\r
+  StringDpOptionLc = NULL;\r
   StringPtr        = NULL;\r
 \r
-  // Get DP's entry time as soon as possible.\r
-  // This is used as the Shell-Phase end time.\r
   //\r
-  Ticker  = GetPerformanceCounter ();\r
+  // Retrieve HII package list from ImageHandle\r
+  //\r
+  Status = gBS->OpenProtocol (\r
+                  ImageHandle,\r
+                  &gEfiHiiPackageListProtocolGuid,\r
+                  (VOID **) &PackageList,\r
+                  ImageHandle,\r
+                  NULL,\r
+                  EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
 \r
-  // Register our string package with HII and return the handle to it.\r
   //\r
-  gHiiHandle = HiiAddPackages (&gEfiCallerIdGuid, ImageHandle, DPStrings, NULL);\r
+  // Publish HII package list to HII Database.\r
+  //\r
+  Status = gHiiDatabase->NewPackageList (\r
+                          gHiiDatabase,\r
+                          PackageList,\r
+                          NULL,\r
+                          &gHiiHandle\r
+                          );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
   ASSERT (gHiiHandle != NULL);\r
+  \r
 \r
   // Initial the command list\r
   //\r
@@ -283,6 +330,8 @@ InitializeDp (
       StringDpOptionLx = HiiGetString (gHiiHandle, STRING_TOKEN (STR_DP_OPTION_LX), NULL);\r
       StringDpOptionLn = HiiGetString (gHiiHandle, STRING_TOKEN (STR_DP_OPTION_LN), NULL);\r
       StringDpOptionLt = HiiGetString (gHiiHandle, STRING_TOKEN (STR_DP_OPTION_LT), NULL);\r
+      StringDpOptionLi = HiiGetString (gHiiHandle, STRING_TOKEN (STR_DP_OPTION_LI), NULL);\r
+      StringDpOptionLc = HiiGetString (gHiiHandle, STRING_TOKEN (STR_DP_OPTION_LC), NULL);\r
       \r
       // Boolean Options\r
       // \r
@@ -296,6 +345,8 @@ InitializeDp (
       ProfileMode = ShellCommandLineGetFlag (ParamPackage, StringDpOptionUp);\r
 #endif  // PROFILING_IMPLEMENTED\r
       ExcludeMode = ShellCommandLineGetFlag (ParamPackage, StringDpOptionLx);\r
+      mShowId     =  ShellCommandLineGetFlag (ParamPackage, StringDpOptionLi);\r
+      CumulativeMode = ShellCommandLineGetFlag (ParamPackage, StringDpOptionLc);\r
 \r
       // Options with Values\r
       CmdLineArg  = ShellCommandLineGetValue (ParamPackage, StringDpOptionLn);\r
@@ -324,6 +375,22 @@ InitializeDp (
 #endif  // PROFILING_IMPLEMENTED\r
       }\r
 \r
+  //\r
+  // Init the custom cumulative data.\r
+  //\r
+  CustomCumulativeToken = ShellCommandLineGetValue (ParamPackage, StringDpOptionLc);\r
+  if (CustomCumulativeToken != NULL) {\r
+    CustomCumulativeData = AllocateZeroPool (sizeof (PERF_CUM_DATA));\r
+    ASSERT (CustomCumulativeData != NULL);\r
+    CustomCumulativeData->MinDur = 0;\r
+    CustomCumulativeData->MaxDur = 0;\r
+    CustomCumulativeData->Count  = 0;\r
+    CustomCumulativeData->Duration = 0;\r
+    NameSize = StrLen (CustomCumulativeToken) + 1;\r
+    CustomCumulativeData->Name   = AllocateZeroPool (NameSize);\r
+    UnicodeStrToAsciiStrS (CustomCumulativeToken, CustomCumulativeData->Name, NameSize);\r
+  }\r
+\r
 /****************************************************************************\r
 ****            Timer specific processing                                ****\r
 ****************************************************************************/\r
@@ -332,10 +399,16 @@ InitializeDp (
       //    StartCount = Value loaded into the counter when it starts counting\r
       //      EndCount = Value counter counts to before it needs to be reset\r
       //\r
-      Freq = GetPerformanceCounterProperties (&TimerInfo.StartCount, &TimerInfo.EndCount);\r
+      Status = EfiGetSystemConfigurationTable (&gPerformanceProtocolGuid, (VOID **) &PerformanceProperty);\r
+      if (EFI_ERROR (Status) || (PerformanceProperty == NULL)) {\r
+        PrintToken (STRING_TOKEN (STR_PERF_PROPERTY_NOT_FOUND));\r
+        goto Done;\r
+      }\r
 \r
       // Convert the Frequency from Hz to KHz\r
-      TimerInfo.Frequency = (UINT32)DivU64x32 (Freq, 1000);\r
+      TimerInfo.Frequency  = (UINT32)DivU64x32 (PerformanceProperty->Frequency, 1000);\r
+      TimerInfo.StartCount = PerformanceProperty->TimerStartValue;\r
+      TimerInfo.EndCount   = PerformanceProperty->TimerEndValue;\r
 \r
       // Determine in which direction the performance counter counts.\r
       TimerInfo.CountUp = (BOOLEAN) (TimerInfo.EndCount >= TimerInfo.StartCount);\r
@@ -385,10 +458,15 @@ InitializeDp (
 ****    !T &&  P  := (2) Only Profile records are displayed\r
 ****     T &&  P  := (3) Same as Default, both are displayed\r
 ****************************************************************************/\r
-      GatherStatistics();\r
-      if (AllMode) {\r
+      GatherStatistics (CustomCumulativeData);\r
+      if (CumulativeMode) {                       \r
+        ProcessCumulative (CustomCumulativeData);\r
+      } else if (AllMode) {\r
         if (TraceMode) {\r
-          DumpAllTrace( Number2Display, ExcludeMode);\r
+          Status = DumpAllTrace( Number2Display, ExcludeMode);\r
+          if (Status == EFI_ABORTED) {\r
+            goto Done;\r
+          }\r
         }\r
         if (ProfileMode) {\r
           DumpAllProfile( Number2Display, ExcludeMode);\r
@@ -396,7 +474,10 @@ InitializeDp (
       }\r
       else if (RawMode) {\r
         if (TraceMode) {\r
-          DumpRawTrace( Number2Display, ExcludeMode);\r
+          Status = DumpRawTrace( Number2Display, ExcludeMode);\r
+          if (Status == EFI_ABORTED) {\r
+            goto Done;\r
+          }\r
         }\r
         if (ProfileMode) {\r
           DumpRawProfile( Number2Display, ExcludeMode);\r
@@ -405,14 +486,24 @@ InitializeDp (
       else {\r
         //------------- Begin Cooked Mode Processing\r
         if (TraceMode) {\r
-          ProcessPhases ( Ticker );\r
+          ProcessPhases ();\r
           if ( ! SummaryMode) {\r
             Status = ProcessHandles ( ExcludeMode);\r
-            if ( ! EFI_ERROR( Status)) {\r
-              ProcessPeims (     );\r
-              ProcessGlobal (    );\r
-              ProcessCumulative ();\r
+            if (Status == EFI_ABORTED) {\r
+              goto Done;\r
+            }\r
+\r
+            Status = ProcessPeims ();\r
+            if (Status == EFI_ABORTED) {\r
+              goto Done;\r
             }\r
+\r
+            Status = ProcessGlobal ();\r
+            if (Status == EFI_ABORTED) {\r
+              goto Done;\r
+            }\r
+\r
+            ProcessCumulative (NULL);\r
           }\r
         }\r
         if (ProfileMode) {\r
@@ -425,6 +516,9 @@ InitializeDp (
     }\r
   }\r
 \r
+Done:\r
+\r
+  //\r
   // Free the memory allocate from HiiGetString\r
   //\r
   ListIndex = 0;\r
@@ -447,9 +541,19 @@ InitializeDp (
   SafeFreePool (StringDpOptionLx);\r
   SafeFreePool (StringDpOptionLn);\r
   SafeFreePool (StringDpOptionLt);\r
+  SafeFreePool (StringDpOptionLi);\r
+  SafeFreePool (StringDpOptionLc);\r
   SafeFreePool (StringPtr);\r
   SafeFreePool (mPrintTokenBuffer);\r
 \r
+  if (ParamPackage != NULL) {\r
+    ShellCommandLineFreeVarList (ParamPackage);\r
+  }\r
+  if (CustomCumulativeData != NULL) {\r
+    SafeFreePool (CustomCumulativeData->Name);\r
+  }\r
+  SafeFreePool (CustomCumulativeData);\r
+\r
   HiiRemovePackages (gHiiHandle);\r
   return Status;\r
 }\r