Improve source code file reading to avoid additional text/bytes reading
[clinton/Virtual-Jaguar-Rx.git] / src / debugger / DWARFManager.cpp
index df4f671..e522e4f 100644 (file)
@@ -10,7 +10,7 @@
 // JPM  12/03/2016  Created this file\r
 // JPM  12/03/2016  DWARF format support\r
 // JPM  Sept./2018  Added LEB128 decoding features, and improve the DWARF parsing information\r
-// JPM   Oct./2018  Improve the DWARF parsing information\r
+// JPM  10/06/2018  Improve the DWARF parsing information, and the source file text reading\r
 //\r
 \r
 // To Do\r
@@ -32,7 +32,7 @@
 //#define DEBUG_VariableName   "sound_death"                           // Variable name to look for or undefine it\r
 //#define DEBUG_TypeName               "Cbuf_Execute"                  // Type name to look for or undefine it\r
 //#define DEBUG_TypeDef                        DW_TAG_typedef          // Type def to look for or undefine it (not supported)\r
-//#define DEBUG_Filename               "cmd.c"                 // Filename to look for or undefine it\r
+//#define DEBUG_Filename               "net_jag.c"                     // Filename to look for or undefine it\r
 \r
 \r
 // Source line internal structure\r
@@ -433,36 +433,60 @@ void DWARFManager_InitDMI(void)
                                                                strcpy((Ptr1 + 1), (Ptr + 4));\r
                                                        }\r
 \r
-                                                       // Read the file as text\r
-#ifndef __CYGWIN__\r
-                                                       if (!fopen_s(&SrcFile, PtrCU[NbCU].PtrFullFilename, "rt"))\r
-#else\r
-                                                       if (!(SrcFile = fopen(SourceFullFilename, "rt")))\r
-#endif\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_END))\r
                                                                {\r
                                                                        if ((PtrCU[NbCU].SizeLoadSrc = ftell(SrcFile)) > 0)\r
                                                                        {\r
-                                                                               if (PtrCU[NbCU].PtrLoadSrc = Ptr = (char *)calloc((PtrCU[NbCU].SizeLoadSrc + 1), 1))\r
+                                                                               if (!fseek(SrcFile, 0, SEEK_SET))\r
                                                                                {\r
-                                                                                       rewind(SrcFile);\r
-                                                                                       if (PtrCU[NbCU].SizeLoadSrc < fread(Ptr, 1, PtrCU[NbCU].SizeLoadSrc, SrcFile))\r
-                                                                                       {\r
-                                                                                               free(PtrCU[NbCU].PtrLoadSrc);\r
-                                                                                               PtrCU[NbCU].PtrLoadSrc = NULL;\r
-                                                                                               PtrCU[NbCU].SizeLoadSrc = 0;\r
-                                                                                       }\r
-                                                                                       else\r
+                                                                                       if (PtrCU[NbCU].PtrLoadSrc = Ptr = Ptr1 = (char *)calloc(1, (PtrCU[NbCU].SizeLoadSrc + 2)))\r
                                                                                        {\r
-                                                                                               do\r
+                                                                                               // Read whole file\r
+                                                                                               if (fread_s(PtrCU[NbCU].PtrLoadSrc, PtrCU[NbCU].SizeLoadSrc, PtrCU[NbCU].SizeLoadSrc, 1, SrcFile) != 1)\r
                                                                                                {\r
-                                                                                                       if (*Ptr == 0xa)\r
+                                                                                                       free(PtrCU[NbCU].PtrLoadSrc);\r
+                                                                                                       PtrCU[NbCU].PtrLoadSrc = NULL;\r
+                                                                                                       PtrCU[NbCU].SizeLoadSrc = 0;\r
+                                                                                               }\r
+                                                                                               else\r
+                                                                                               {\r
+                                                                                                       // Eliminate all carriage return code '\r' (oxd)\r
+                                                                                                       do\r
+                                                                                                       {\r
+                                                                                                               if ((*Ptr = *Ptr1) != '\r')\r
+                                                                                                               {\r
+                                                                                                                       Ptr++;\r
+                                                                                                               }\r
+                                                                                                       }\r
+                                                                                                       while (*Ptr1++);\r
+\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].NbLinesLoadSrc++;\r
-                                                                                                               *Ptr = 0;\r
+                                                                                                               PtrCU[NbCU].PtrLoadSrc[PtrCU[NbCU].SizeLoadSrc++] = '\n';\r
+                                                                                                               PtrCU[NbCU].PtrLoadSrc[PtrCU[NbCU].SizeLoadSrc] = 0;\r
                                                                                                        }\r
-                                                                                               } while (*++Ptr);\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
+                                                                                                                       {\r
+                                                                                                                               PtrCU[NbCU].NbLinesLoadSrc++;\r
+                                                                                                                               *Ptr = 0;\r
+                                                                                                                       }\r
+                                                                                                               } while (*++Ptr);\r
+                                                                                                       }\r
+                                                                                               }\r
                                                                                        }\r
                                                                                }\r
                                                                        }\r
@@ -476,7 +500,7 @@ void DWARFManager_InitDMI(void)
                                                }\r
                                        }\r
 \r
-                                       // Get the source lines table located in the Compilation Unit\r
+                                       // Get the source lines table located in the CU\r
                                        if (dwarf_srclines(return_sib, &linebuf, &cnt, &error) == DW_DLV_OK)\r
                                        {\r
                                        }\r
@@ -1768,8 +1792,8 @@ char *DWARFManager_GetLineSrcFromAdrNumLine(size_t Adr, size_t NumLine)
 }\r
 \r
 \r
-// Get text line from source based on address and num line (starting by 1)\r
-// Return NULL if no text line has been found\r
+// Get text line pointer from source, based on address and line number (starting by 1)\r
+// Return NULL if no text line has been found, or if requested number line is above the source total number of lines\r
 char *DWARFManager_GetLineSrcFromNumLineBaseAdr(size_t Adr, size_t NumLine)\r
 {\r
        size_t i;\r