Prevent SIGSEGV in managed code.
authorMikhail Kurinnoi <m.kurinnoi@samsung.com>
Fri, 22 Sep 2023 15:03:23 +0000 (18:03 +0300)
committerGleb Balykov/Advanced System SW Lab /SRR/Staff Engineer/Samsung Electronics <g.balykov@samsung.com>
Tue, 26 Sep 2023 09:06:30 +0000 (18:06 +0900)
src/managed/SymbolReader.cs
src/protocols/cliprotocol.cpp
src/protocols/sourcestorage.cpp
src/protocols/sourcestorage.h

index 86b0a79339e2ca76d3b7eb64f4da4e44c891644a..4440c54b29c6702db6c5937fb1e25bb324111c59 100644 (file)
@@ -1517,6 +1517,12 @@ namespace NetCoreDbg
                     if (docPath == fileName)
                     {
                         MemoryStream ms = GetEmbeddedSource(mdReader, handle, out docSize);
+
+                        // PDB loaded, but don't contain source file lines.
+                        // This is not an error, but also no data to return.
+                        if (docSize == 0)
+                            return RetCode.OK;
+
                         data = Marshal.AllocCoTaskMem(docSize);
                         Marshal.Copy(ms.ToArray(), 0, data, docSize);
                         length = docSize;
index 5156d0163d3d87a1e4397f73b9949754991bd444..39722731a40bbc5758a07b14c49cbe48eb444c7e 100644 (file)
@@ -1628,7 +1628,12 @@ HRESULT CLIProtocol::doCommand<CommandTag::List>(const std::vector<std::string>
     {
         for (int i = 0; i < lines; i++, line++)
         {
-            char* toPrint = m_sources->getLine(m_sourcePath, line);
+            const char* errMessage = nullptr;
+            char* toPrint = m_sources->getLine(m_sourcePath, line, &errMessage);
+            if (errMessage)
+            {
+                printf("Source code file: %s\n%s\n", m_sourcePath.c_str(), errMessage);
+            }
             if (toPrint)
             {
                 if(line == m_stoppedAt)
index 631e43a2cdd368e9c4892412e7c2765ed889c6b5..244f51d22444c9a2ccdc534ff8efe84df83aa5cf 100644 (file)
@@ -16,7 +16,7 @@ namespace netcoredbg
         }
     }
 
-    char* SourceStorage::getLine(std::string& file, int linenum)
+    char* SourceStorage::getLine(std::string& file, int linenum, const char **errMessage)
     {
         if(files.empty() || files.front()->filePath != file)
         {
@@ -36,7 +36,7 @@ namespace netcoredbg
             if (notFound)
             {
                 // file is not in the list -- try to load it from pdb
-                if (loadFile(file) != S_OK)
+                if (loadFile(file, errMessage) != S_OK)
                     return NULL;
             }
         }
@@ -47,13 +47,23 @@ namespace netcoredbg
             return NULL;
     }
 
-    HRESULT SourceStorage::loadFile(std::string& file)
+    HRESULT SourceStorage::loadFile(std::string& file, const char **errMessage)
     {
         char* fileBuff = NULL;
         int fileLen = 0;
         HRESULT Status = S_OK;
 
-        IfFailRet(m_dbg->GetSourceFile(file, &fileBuff, &fileLen));
+        if (FAILED(Status = m_dbg->GetSourceFile(file, &fileBuff, &fileLen)))
+        {
+            *errMessage = "Debug information (PDB file) cannot be opened. Check that PDB file exists and is correct.";
+            return Status;
+        }
+        if (fileLen == 0)
+        {
+            *errMessage = "Debug information (PDB file) doesn't contain source code. Make sure csproj file has <EmbedAllSources>true</EmbedAllSources> line.";
+            return E_FAIL;
+        }
+
         SourceFile *sf = new SourceFile();
         sf->filePath = file;
         sf->size = fileLen;
index e28ca11ad6f74796bce6516227ee87faae8584b8..ca915870c85afbc90c64af0720331d08f91d05b7 100644 (file)
@@ -25,7 +25,7 @@ class SourceStorage
     int totalLen;
 
 private:
-    HRESULT loadFile(std::string& file);
+    HRESULT loadFile(std::string& file, const char **errMessage);
 
 public:
     SourceStorage(IDebugger* d) 
@@ -35,7 +35,7 @@ public:
     }
     ~SourceStorage();
 
-    char* getLine(std::string& file, int linenum);
+    char* getLine(std::string& file, int linenum, const char **errMessage);
 
 }; // class sourcestorage
 } // namespace
\ No newline at end of file