[Lldb-commits] [lldb] r271551 - Fixed a problem where we couldn't call extern "C" functions.

Sean Callanan via lldb-commits lldb-commits at lists.llvm.org
Thu Jun 2 10:59:48 PDT 2016


Author: spyffe
Date: Thu Jun  2 12:59:47 2016
New Revision: 271551

URL: http://llvm.org/viewvc/llvm-project?rev=271551&view=rev
Log:
Fixed a problem where we couldn't call extern "C" functions.

Some compilers do not mark up C++ functions as extern "C" in the DWARF, so LLDB
has to fall back (if it is about to give up finding a symbol) to using the base
name of the function.

This fix also ensures that we search by full name rather than "auto," which
could cause unrelated C++ names to be found.  Finally, it adds a test case.

<rdar://problem/25094302>

Added:
    lldb/trunk/packages/Python/lldbsuite/test/lang/cpp/extern_c/
    lldb/trunk/packages/Python/lldbsuite/test/lang/cpp/extern_c/Makefile
    lldb/trunk/packages/Python/lldbsuite/test/lang/cpp/extern_c/TestExternCSymbols.py
    lldb/trunk/packages/Python/lldbsuite/test/lang/cpp/extern_c/main.cpp
Modified:
    lldb/trunk/include/lldb/Expression/IRExecutionUnit.h
    lldb/trunk/source/Expression/IRExecutionUnit.cpp

Modified: lldb/trunk/include/lldb/Expression/IRExecutionUnit.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Expression/IRExecutionUnit.h?rev=271551&r1=271550&r2=271551&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Expression/IRExecutionUnit.h (original)
+++ lldb/trunk/include/lldb/Expression/IRExecutionUnit.h Thu Jun  2 12:59:47 2016
@@ -294,6 +294,10 @@ private:
                                    const std::vector<SearchSpec> &C_specs,
                                    const SymbolContext &sc);
     
+    void
+    CollectFallbackNames(std::vector<SearchSpec> &fallback_specs,
+                         const std::vector<SearchSpec> &C_specs);
+    
     lldb::addr_t
     FindInSymbols(const std::vector<SearchSpec> &specs,
                   const lldb_private::SymbolContext &sc);

Added: lldb/trunk/packages/Python/lldbsuite/test/lang/cpp/extern_c/Makefile
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/lang/cpp/extern_c/Makefile?rev=271551&view=auto
==============================================================================
--- lldb/trunk/packages/Python/lldbsuite/test/lang/cpp/extern_c/Makefile (added)
+++ lldb/trunk/packages/Python/lldbsuite/test/lang/cpp/extern_c/Makefile Thu Jun  2 12:59:47 2016
@@ -0,0 +1,3 @@
+LEVEL = ../../../make
+C_SOURCES := main.c
+include $(LEVEL)/Makefile.rules

Added: lldb/trunk/packages/Python/lldbsuite/test/lang/cpp/extern_c/TestExternCSymbols.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/lang/cpp/extern_c/TestExternCSymbols.py?rev=271551&view=auto
==============================================================================
--- lldb/trunk/packages/Python/lldbsuite/test/lang/cpp/extern_c/TestExternCSymbols.py (added)
+++ lldb/trunk/packages/Python/lldbsuite/test/lang/cpp/extern_c/TestExternCSymbols.py Thu Jun  2 12:59:47 2016
@@ -0,0 +1,4 @@
+from lldbsuite.test import lldbinline
+from lldbsuite.test import decorators
+
+lldbinline.MakeInlineTest(__file__, globals(), [])

Added: lldb/trunk/packages/Python/lldbsuite/test/lang/cpp/extern_c/main.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/lang/cpp/extern_c/main.cpp?rev=271551&view=auto
==============================================================================
--- lldb/trunk/packages/Python/lldbsuite/test/lang/cpp/extern_c/main.cpp (added)
+++ lldb/trunk/packages/Python/lldbsuite/test/lang/cpp/extern_c/main.cpp Thu Jun  2 12:59:47 2016
@@ -0,0 +1,29 @@
+//===-- main.cpp ------------------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include <stdio.h>
+#include <stdint.h>
+
+extern "C"
+{
+   int foo();
+};
+
+int foo()
+{
+    puts("foo");
+    return 2;
+}
+
+int main (int argc, char const *argv[], char const *envp[])
+{          
+    foo();
+    return 0; //% self.expect("expression -- foo()", substrs = ['2'])
+}
+

Modified: lldb/trunk/source/Expression/IRExecutionUnit.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Expression/IRExecutionUnit.cpp?rev=271551&r1=271550&r2=271551&view=diff
==============================================================================
--- lldb/trunk/source/Expression/IRExecutionUnit.cpp (original)
+++ lldb/trunk/source/Expression/IRExecutionUnit.cpp Thu Jun  2 12:59:47 2016
@@ -758,7 +758,7 @@ struct IRExecutionUnit::SearchSpec
     ConstString name;
     uint32_t    mask;
 
-    SearchSpec(ConstString n, uint32_t m = lldb::eFunctionNameTypeAuto) :
+    SearchSpec(ConstString n, uint32_t m = lldb::eFunctionNameTypeFull) :
         name(n),
         mask(m)
     {
@@ -819,6 +819,36 @@ IRExecutionUnit::CollectCandidateCPlusPl
     }
 }
 
+void
+IRExecutionUnit::CollectFallbackNames(std::vector<SearchSpec> &fallback_specs,
+                                      const std::vector<SearchSpec> &C_specs)
+{
+    // As a last-ditch fallback, try the base name for C++ names.  It's terrible,
+    // but the DWARF doesn't always encode "extern C" correctly.
+    
+    for (const SearchSpec &C_spec : C_specs)
+    {
+        const ConstString &name = C_spec.name;
+        
+        if (CPlusPlusLanguage::IsCPPMangledName(name.GetCString()))
+        {
+            Mangled mangled_name(name);
+            ConstString demangled_name = mangled_name.GetDemangledName(lldb::eLanguageTypeC_plus_plus);
+            if (!demangled_name.IsEmpty())
+            {
+                const char *demangled_cstr = demangled_name.AsCString();
+                const char *lparen_loc = strchr(demangled_cstr, '(');
+                if (lparen_loc)
+                {
+                    llvm::StringRef base_name(demangled_cstr, lparen_loc-demangled_cstr);
+                    fallback_specs.push_back(ConstString(base_name));
+                }
+            }
+        }
+    }
+}
+
+
 lldb::addr_t
 IRExecutionUnit::FindInSymbols(const std::vector<IRExecutionUnit::SearchSpec> &specs, const lldb_private::SymbolContext &sc)
 {
@@ -1019,6 +1049,14 @@ IRExecutionUnit::FindSymbol(const lldb_p
         CollectCandidateCPlusPlusNames(candidate_CPlusPlus_names, candidate_C_names, m_sym_ctx);
         ret = FindInSymbols(candidate_CPlusPlus_names, m_sym_ctx);
     }
+    
+    if (ret == LLDB_INVALID_ADDRESS)
+    {
+        std::vector<SearchSpec> candidate_fallback_names;
+
+        CollectFallbackNames(candidate_fallback_names, candidate_C_names);
+        ret = FindInSymbols(candidate_fallback_names, m_sym_ctx);
+    }
 
     return ret;
 }




More information about the lldb-commits mailing list