// 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
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
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
CUStruct *PtrCU;\r
char **ListSearchPaths;\r
size_t NbSearchPaths;\r
+struct _stat FileElfExeInfo;\r
\r
\r
//\r
\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
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
}\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
// 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
\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