Added a source code file date check when reading DWARF information
[clinton/Virtual-Jaguar-Rx.git] / src / debugger / DWARFManager.cpp
index 130342a..e0e07b9 100644 (file)
@@ -12,6 +12,7 @@
 // JPM   Oct./2018  Improve the DWARF parsing information, and the source file text reading; support the used source lines from DWARF structure, and the search paths for the files\r
 // JPM   Aug./2019  Added new functions to handle DWARF information, full filename fix\r
 // JPM   Mar./2020  Fix a random crash when reading the source lines information\r
+// JPM   Aug./2020  Added a source code file date check when reading DWARF information\r
 //\r
 \r
 // To Do\r
 #include <stdio.h>\r
 #include <stdint.h>\r
 #include <string.h>\r
-#include <libdwarf.h>\r
-#include <dwarf.h>\r
+#include <time.h>\r
+#include <sys/types.h>\r
+#include <sys/stat.h>\r
+#include "libdwarf.h"\r
+#include "dwarf.h"\r
 #include "LEB128.h"\r
+#include "DWARFManager.h"\r
 \r
 \r
 // Definitions for debugging\r
@@ -131,7 +136,7 @@ typedef struct CUStruct
        size_t Tag;\r
        size_t Language;                                                                // Language (C, etc.) used by the source code\r
        size_t LowPC, HighPC;                                                   // Memory range for the code\r
-       char *PtrProducer;                                                              // Pointer to the "Producer" text information (mostly compiler and compilation options used)\r
+       char *PtrProducer;                                                              // "Producer" text information (mostly compiler and compilation options used)\r
        char *PtrSourceFilename;                                                // Source file name\r
        char *PtrSourceFileDirectory;                                   // Directory of the source file\r
        char *PtrFullFilename;                                                  // Pointer to full namefile (directory & filename)\r
@@ -151,6 +156,8 @@ typedef struct CUStruct
        CUStruct_LineSrc *PtrUsedLinesSrc;                              // Pointer to the used source lines list structure\r
        char **PtrUsedLinesLoadSrc;                                             // Pointer lists to each used source line referenced by the CUStruct_LineSrc structure\r
        size_t *PtrUsedNumLines;                                                // List of the number lines used\r
+       struct _stat _statbuf;                                                  // File information\r
+       DWARFstatus Status;                                                             // File status\r
 }S_CUStruct;\r
 \r
 \r
@@ -163,6 +170,7 @@ Dwarf_Debug dbg;
 CUStruct *PtrCU;\r
 char **ListSearchPaths;\r
 size_t NbSearchPaths;\r
+struct _stat FileElfExeInfo;\r
 \r
 \r
 //\r
@@ -242,10 +250,11 @@ bool DWARFManager_Close(void)
 \r
 \r
 // Dwarf manager Elf init\r
-int    DWARFManager_ElfInit(Elf *ElfPtr)\r
+int    DWARFManager_ElfInit(Elf *ElfPtr, struct _stat FileElfInfo)\r
 {\r
        if ((LibDwarf = dwarf_elf_init(ElfPtr, DW_DLC_READ, (Dwarf_Handler)DWARFManager_ErrorHandler, errarg, &dbg, &error)) == DW_DLV_OK)\r
        {\r
+               FileElfExeInfo = FileElfInfo;\r
                DWARFManager_InitDMI();\r
        }\r
 \r
@@ -525,65 +534,88 @@ void DWARFManager_InitDMI(void)
                                                                strcpy((Ptr1 + 1), (Ptr + 4));\r
                                                        }\r
 \r
-                                                       // Open the source file as a binary file\r
-                                                       if (!fopen_s(&SrcFile, PtrCU[NbCU].PtrFullFilename, "rb"))\r
+                                                       // Get the source file information\r
+                                                       if (!_stat(PtrCU[NbCU].PtrFullFilename, &PtrCU[NbCU]._statbuf))\r
                                                        {\r
-                                                               if (!fseek(SrcFile, 0, SEEK_END))\r
+                                                               // check the time stamp with the executable\r
+                                                               if (PtrCU[NbCU]._statbuf.st_mtime <= FileElfExeInfo.st_mtime)\r
                                                                {\r
-                                                                       if ((PtrCU[NbCU].SizeLoadSrc = ftell(SrcFile)) > 0)\r
+                                                                       // Open the source file as a binary file\r
+                                                                       if (!fopen_s(&SrcFile, PtrCU[NbCU].PtrFullFilename, "rb"))\r
                                                                        {\r
-                                                                               if (!fseek(SrcFile, 0, SEEK_SET))\r
+                                                                               if (!fseek(SrcFile, 0, SEEK_END))\r
                                                                                {\r
-                                                                                       if (PtrCU[NbCU].PtrLoadSrc = Ptr = Ptr1 = (char *)calloc(1, (PtrCU[NbCU].SizeLoadSrc + 2)))\r
+                                                                                       if ((PtrCU[NbCU].SizeLoadSrc = ftell(SrcFile)) > 0)\r
                                                                                        {\r
-                                                                                               // Read whole file\r
-                                                                                               if (fread_s(PtrCU[NbCU].PtrLoadSrc, PtrCU[NbCU].SizeLoadSrc, PtrCU[NbCU].SizeLoadSrc, 1, SrcFile) != 1)\r
-                                                                                               {\r
-                                                                                                       free(PtrCU[NbCU].PtrLoadSrc);\r
-                                                                                                       PtrCU[NbCU].PtrLoadSrc = NULL;\r
-                                                                                                       PtrCU[NbCU].SizeLoadSrc = 0;\r
-                                                                                               }\r
-                                                                                               else\r
+                                                                                               if (!fseek(SrcFile, 0, SEEK_SET))\r
                                                                                                {\r
-                                                                                                       // Eliminate all carriage return code '\r' (oxd)\r
-                                                                                                       do\r
+                                                                                                       if (PtrCU[NbCU].PtrLoadSrc = Ptr = Ptr1 = (char *)calloc(1, (PtrCU[NbCU].SizeLoadSrc + 2)))\r
                                                                                                        {\r
-                                                                                                               if ((*Ptr = *Ptr1) != '\r')\r
+                                                                                                               // Read whole file\r
+                                                                                                               if (fread_s(PtrCU[NbCU].PtrLoadSrc, PtrCU[NbCU].SizeLoadSrc, PtrCU[NbCU].SizeLoadSrc, 1, SrcFile) != 1)\r
                                                                                                                {\r
-                                                                                                                       Ptr++;\r
+                                                                                                                       free(PtrCU[NbCU].PtrLoadSrc);\r
+                                                                                                                       PtrCU[NbCU].PtrLoadSrc = NULL;\r
+                                                                                                                       PtrCU[NbCU].SizeLoadSrc = 0;\r
                                                                                                                }\r
-                                                                                                       }\r
-                                                                                                       while (*Ptr1++);\r
+                                                                                                               else\r
+                                                                                                               {\r
+                                                                                                                       // Eliminate all carriage return code '\r' (oxd)\r
+                                                                                                                       do\r
+                                                                                                                       {\r
+                                                                                                                               if ((*Ptr = *Ptr1) != '\r')\r
+                                                                                                                               {\r
+                                                                                                                                       Ptr++;\r
+                                                                                                                               }\r
+                                                                                                                       } while (*Ptr1++);\r
 \r
-                                                                                                       // Get back the new text file size\r
-                                                                                                       PtrCU[NbCU].SizeLoadSrc = strlen(Ptr = PtrCU[NbCU].PtrLoadSrc);\r
+                                                                                                                       // Get back the new text file size\r
+                                                                                                                       PtrCU[NbCU].SizeLoadSrc = strlen(Ptr = PtrCU[NbCU].PtrLoadSrc);\r
 \r
-                                                                                                       // Make sure the text file finish with a new line code '\n' (0xa)\r
-                                                                                                       if (PtrCU[NbCU].PtrLoadSrc[PtrCU[NbCU].SizeLoadSrc - 1] != '\n')\r
-                                                                                                       {\r
-                                                                                                               PtrCU[NbCU].PtrLoadSrc[PtrCU[NbCU].SizeLoadSrc++] = '\n';\r
-                                                                                                               PtrCU[NbCU].PtrLoadSrc[PtrCU[NbCU].SizeLoadSrc] = 0;\r
-                                                                                                       }\r
+                                                                                                                       // Make sure the text file finish with a new line code '\n' (0xa)\r
+                                                                                                                       if (PtrCU[NbCU].PtrLoadSrc[PtrCU[NbCU].SizeLoadSrc - 1] != '\n')\r
+                                                                                                                       {\r
+                                                                                                                               PtrCU[NbCU].PtrLoadSrc[PtrCU[NbCU].SizeLoadSrc++] = '\n';\r
+                                                                                                                               PtrCU[NbCU].PtrLoadSrc[PtrCU[NbCU].SizeLoadSrc] = 0;\r
+                                                                                                                       }\r
 \r
-                                                                                                       // Reallocate text file\r
-                                                                                                       if (PtrCU[NbCU].PtrLoadSrc = Ptr = (char *)realloc(PtrCU[NbCU].PtrLoadSrc, (PtrCU[NbCU].SizeLoadSrc + 1)))\r
-                                                                                                       {\r
-                                                                                                               // Count line numbers, based on the new line code '\n' (0xa), and finish each line with 0\r
-                                                                                                               do\r
-                                                                                                               {\r
-                                                                                                                       if (*Ptr == '\n')\r
+                                                                                                                       // Reallocate text file\r
+                                                                                                                       if (PtrCU[NbCU].PtrLoadSrc = Ptr = (char *)realloc(PtrCU[NbCU].PtrLoadSrc, (PtrCU[NbCU].SizeLoadSrc + 1)))\r
                                                                                                                        {\r
-                                                                                                                               PtrCU[NbCU].NbLinesLoadSrc++;\r
-                                                                                                                               *Ptr = 0;\r
+                                                                                                                               // Count line numbers, based on the new line code '\n' (0xa), and finish each line with 0\r
+                                                                                                                               do\r
+                                                                                                                               {\r
+                                                                                                                                       if (*Ptr == '\n')\r
+                                                                                                                                       {\r
+                                                                                                                                               PtrCU[NbCU].NbLinesLoadSrc++;\r
+                                                                                                                                               *Ptr = 0;\r
+                                                                                                                                       }\r
+                                                                                                                               } while (*++Ptr);\r
                                                                                                                        }\r
-                                                                                                               } while (*++Ptr);\r
+                                                                                                               }\r
                                                                                                        }\r
                                                                                                }\r
                                                                                        }\r
                                                                                }\r
+\r
+                                                                               fclose(SrcFile);\r
                                                                        }\r
+                                                                       else\r
+                                                                       {\r
+                                                                               // Source file doesn't exist\r
+                                                                               PtrCU[NbCU].Status = DWARFSTATUS_NOFILE;\r
+                                                                       }\r
+                                                               }\r
+                                                               else\r
+                                                               {\r
+                                                                       // Source file is outdated\r
+                                                                       PtrCU[NbCU].Status = DWARFSTATUS_OUTDATEDFILE;\r
                                                                }\r
-                                                               fclose(SrcFile);\r
+                                                       }\r
+                                                       else\r
+                                                       {\r
+                                                               // Source file doesn't have information\r
+                                                               PtrCU[NbCU].Status = DWARFSTATUS_NOFILEINFO;\r
                                                        }\r
                                                        break;\r
 \r
@@ -593,7 +625,7 @@ void DWARFManager_InitDMI(void)
                                        }\r
 \r
                                        // Get the source lines table located in the CU\r
-                                       if (dwarf_srclines(return_sib, &linebuf, &cnt, &error) == DW_DLV_OK)\r
+                                       if ((dwarf_srclines(return_sib, &linebuf, &cnt, &error) == DW_DLV_OK) && (PtrCU[NbCU].Status == DWARFSTATUS_OK))\r
                                        {\r
                                                if (cnt)\r
                                                {\r
@@ -903,7 +935,8 @@ void DWARFManager_InitDMI(void)
                                                                                // Get source line number and associated block of address\r
                                                                                for (Dwarf_Signed i = 0; i < cnt; ++i)\r
                                                                                {\r
-                                                                                       if ((PtrCU[NbCU].PtrUsedLinesSrc[i].StartPC >= return_lowpc) && (PtrCU[NbCU].PtrUsedLinesSrc[i].StartPC <= return_highpc))\r
+                                                                                       // Check the presence of the line in the memory frame\r
+                                                                                       if (PtrCU[NbCU].PtrUsedLinesSrc && (PtrCU[NbCU].PtrUsedLinesSrc[i].StartPC >= return_lowpc) && (PtrCU[NbCU].PtrUsedLinesSrc[i].StartPC <= return_highpc))\r
                                                                                        {\r
                                                                                                PtrCU[NbCU].PtrSubProgs[PtrCU[NbCU].NbSubProgs].PtrLinesSrc = (DMIStruct_LineSrc *)realloc(PtrCU[NbCU].PtrSubProgs[PtrCU[NbCU].NbSubProgs].PtrLinesSrc, (PtrCU[NbCU].PtrSubProgs[PtrCU[NbCU].NbSubProgs].NbLinesSrc + 1) * sizeof(DMIStruct_LineSrc));\r
                                                                                                memset((void *)(PtrCU[NbCU].PtrSubProgs[PtrCU[NbCU].NbSubProgs].PtrLinesSrc + PtrCU[NbCU].PtrSubProgs[PtrCU[NbCU].NbSubProgs].NbLinesSrc), 0, sizeof(DMIStruct_LineSrc));\r
@@ -1371,16 +1404,16 @@ char *DWARFManager_GetSymbolnameFromAdr(size_t Adr)
 \r
 // Get complete source filename based from address\r
 // Return NULL if no source filename exists\r
-// Return the existence status (true or false) in Error if pointer not NULL\r
-char *DWARFManager_GetFullSourceFilenameFromAdr(size_t Adr, bool *Error)\r
+// Return the existence status in Status if pointer not NULL\r
+char *DWARFManager_GetFullSourceFilenameFromAdr(size_t Adr, DWARFstatus *Status)\r
 {\r
        for (size_t i = 0; i < NbCU; i++)\r
        {\r
                if ((Adr >= PtrCU[i].LowPC) && (Adr < PtrCU[i].HighPC))\r
                {\r
-                       if (Error)\r
+                       if (Status)\r
                        {\r
-                               *Error = PtrCU[i].PtrLoadSrc ? true : false;\r
+                               *Status = PtrCU[i].Status;\r
                        }\r
 \r
                        return PtrCU[i].PtrFullFilename;\r