tberghammer created this revision.
tberghammer added a reviewer: labath.
tberghammer added a subscriber: lldb-commits.
Herald added subscribers: srhines, danalbert, tberghammer, emaste.

Remove some duplicated code from PlatformPOSIX/Android

The code was duplicated to handle the custom symbol name for functions
in libdl.so for android. This change modify the way we handle the issue
to eliminate a lot of duplicated code.

http://reviews.llvm.org/D15183

Files:
  source/Plugins/Platform/Android/PlatformAndroid.cpp
  source/Plugins/Platform/Android/PlatformAndroid.h
  source/Plugins/Platform/POSIX/PlatformPOSIX.cpp
  source/Plugins/Platform/POSIX/PlatformPOSIX.h

Index: source/Plugins/Platform/POSIX/PlatformPOSIX.h
===================================================================
--- source/Plugins/Platform/POSIX/PlatformPOSIX.h
+++ source/Plugins/Platform/POSIX/PlatformPOSIX.h
@@ -191,6 +191,9 @@
                             const char *expr_prefix,
                             lldb::ValueObjectSP& result_valobj_sp);
 
+    virtual const char*
+    GetLibdlFunctionDeclarations() const;
+
 private:
     DISALLOW_COPY_AND_ASSIGN (PlatformPOSIX);
 };
Index: source/Plugins/Platform/POSIX/PlatformPOSIX.cpp
===================================================================
--- source/Plugins/Platform/POSIX/PlatformPOSIX.cpp
+++ source/Plugins/Platform/POSIX/PlatformPOSIX.cpp
@@ -918,10 +918,7 @@
                    the_result;
                   )",
                   path);
-    const char *prefix = R"(
-                            extern "C" void* dlopen (const char *path, int mode);
-                            extern "C" const char *dlerror (void);
-                            )";
+    const char *prefix = GetLibdlFunctionDeclarations();
     lldb::ValueObjectSP result_valobj_sp;
     error = EvaluateLibdlExpression(process, expr.GetData(), prefix, result_valobj_sp);
     if (error.Fail())
@@ -970,7 +967,7 @@
 
     StreamString expr;
     expr.Printf("dlclose((void *)0x%" PRIx64 ")", image_addr);
-    const char *prefix = "extern \"C\" int dlclose(void* handle);\n";
+    const char *prefix = GetLibdlFunctionDeclarations();
     lldb::ValueObjectSP result_valobj_sp;
     Error error = EvaluateLibdlExpression(process, expr.GetData(), prefix, result_valobj_sp);
     if (error.Fail())
@@ -988,3 +985,14 @@
     }
     return Error();
 }
+
+const char*
+PlatformPOSIX::GetLibdlFunctionDeclarations() const
+{
+    return R"(
+              extern "C" void* dlopen(const char*, int);
+              extern "C" void* dlsym(void*, const char*);
+              extern "C" int   dlclose(void*);
+              extern "C" char* dlerror(void);
+             )";
+}
Index: source/Plugins/Platform/Android/PlatformAndroid.h
===================================================================
--- source/Plugins/Platform/Android/PlatformAndroid.h
+++ source/Plugins/Platform/Android/PlatformAndroid.h
@@ -84,14 +84,6 @@
         uint32_t
         GetDefaultMemoryCacheLineSize() override;
 
-        uint32_t
-        LoadImage (lldb_private::Process* process,
-                   const lldb_private::FileSpec& image_spec,
-                   lldb_private::Error& error) override;
-
-        lldb_private::Error
-        UnloadImage (lldb_private::Process* process, uint32_t image_token) override;
-
      protected:
         const char *
         GetCacheHostname () override;
@@ -106,6 +98,9 @@
         DownloadSymbolFile (const lldb::ModuleSP& module_sp,
                             const FileSpec& dst_file_spec) override;
 
+        const char*
+        GetLibdlFunctionDeclarations() const override;
+
     private:
         std::string m_device_id;
         uint32_t m_sdk_version;
Index: source/Plugins/Platform/Android/PlatformAndroid.cpp
===================================================================
--- source/Plugins/Platform/Android/PlatformAndroid.cpp
+++ source/Plugins/Platform/Android/PlatformAndroid.cpp
@@ -377,90 +377,13 @@
     return m_major_os_version != 0;
 }
 
-uint32_t
-PlatformAndroid::LoadImage(lldb_private::Process* process, const FileSpec& image_spec, Error& error)
-{
-    char path[PATH_MAX];
-    image_spec.GetPath(path, sizeof(path));
-
-    StreamString expr;
-    expr.Printf(R"(
-                   struct __lldb_dlopen_result { void *image_ptr; const char *error_str; } the_result;
-                   the_result.image_ptr = __dl_dlopen ("%s", 2);
-                   if (the_result.image_ptr == (void*)0x0)
-                       the_result.error_str = __dl_dlerror();
-                   else
-                       the_result.error_str = (const char*)0x0;
-                   the_result;
-                  )",
-                  path);
-    const char *prefix = R"(
-                            extern "C" void* __dl_dlopen(const char* path, int mode);
-                            extern "C" const char *__dl_dlerror(void);
-                            )";
-    lldb::ValueObjectSP result_valobj_sp;
-    error = EvaluateLibdlExpression(process, expr.GetData(), prefix, result_valobj_sp);
-    if (error.Fail())
-        return LLDB_INVALID_IMAGE_TOKEN;
-
-    error = result_valobj_sp->GetError();
-    if (error.Fail())
-        return LLDB_INVALID_IMAGE_TOKEN;
-
-    Scalar scalar;
-    ValueObjectSP image_ptr_sp = result_valobj_sp->GetChildAtIndex(0, true);
-    if (!image_ptr_sp || !image_ptr_sp->ResolveValue(scalar))
-    {
-        error.SetErrorStringWithFormat("unable to load '%s'", path);
-        return LLDB_INVALID_IMAGE_TOKEN;
-    }
-
-    addr_t image_ptr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
-    if (image_ptr != 0 && image_ptr != LLDB_INVALID_ADDRESS)
-        return process->AddImageToken(image_ptr);
-
-    if (image_ptr == 0)
-    {
-        ValueObjectSP error_str_sp = result_valobj_sp->GetChildAtIndex(1, true);
-        if (error_str_sp && error_str_sp->IsCStringContainer(true))
-        {
-            DataBufferSP buffer_sp(new DataBufferHeap(10240,0));
-            size_t num_chars = error_str_sp->ReadPointedString (buffer_sp, error, 10240).first;
-            if (error.Success() && num_chars > 0)
-                error.SetErrorStringWithFormat("dlopen error: %s", buffer_sp->GetBytes());
-            else
-                error.SetErrorStringWithFormat("dlopen failed for unknown reasons.");
-            return LLDB_INVALID_IMAGE_TOKEN;
-        }
-    }
-    error.SetErrorStringWithFormat("unable to load '%s'", path);
-    return LLDB_INVALID_IMAGE_TOKEN;
-}
-
-Error
-PlatformAndroid::UnloadImage (lldb_private::Process* process, uint32_t image_token)
+const char*
+PlatformAndroid::GetLibdlFunctionDeclarations() const
 {
-    const addr_t image_addr = process->GetImagePtrFromToken(image_token);
-    if (image_addr == LLDB_INVALID_ADDRESS)
-        return Error("Invalid image token");
-
-    StreamString expr;
-    expr.Printf("__dl_dlclose((void*)0x%" PRIx64 ")", image_addr);
-    const char *prefix = "extern \"C\" int __dl_dlclose(void* handle);\n";
-    lldb::ValueObjectSP result_valobj_sp;
-    Error error = EvaluateLibdlExpression(process, expr.GetData(), prefix, result_valobj_sp);
-    if (error.Fail())
-        return error;
-
-    if (result_valobj_sp->GetError().Fail())
-        return result_valobj_sp->GetError();
-
-    Scalar scalar;
-    if (result_valobj_sp->ResolveValue(scalar))
-    {
-        if (scalar.UInt(1))
-            return Error("expression failed: \"%s\"", expr.GetData());
-        process->ResetImageToken(image_token);
-    }
-    return Error();
+    return R"(
+              extern "C" void* dlopen(const char*, int) asm("__dl_dlopen");
+              extern "C" void* dlsym(void*, const char*) asm("__dl_dlsym");
+              extern "C" int   dlclose(void*) asm("__dl_dlclose");
+              extern "C" char* dlerror(void) asm("__dl_dlerror");
+             )";
 }
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to