[Lldb-commits] [lldb] r154328 - in /lldb/branches/lldb-platform-work: include/lldb/Host/Host.h include/lldb/lldb-forward.h source/Commands/CommandObjectPlatform.cpp source/Host/common/Host.cpp source/Host/macosx/Host.mm

Enrico Granata egranata at apple.com
Mon Apr 9 10:54:16 PDT 2012


Author: enrico
Date: Mon Apr  9 12:54:16 2012
New Revision: 154328

URL: http://llvm.org/viewvc/llvm-project?rev=154328&view=rev
Log:
Moving file I/O code in the Platform layer to use lldb_private::File instead of relying on POSIX API calls directly - This should make people's lives easier when porting LLDB to a new platform

Modified:
    lldb/branches/lldb-platform-work/include/lldb/Host/Host.h
    lldb/branches/lldb-platform-work/include/lldb/lldb-forward.h
    lldb/branches/lldb-platform-work/source/Commands/CommandObjectPlatform.cpp
    lldb/branches/lldb-platform-work/source/Host/common/Host.cpp
    lldb/branches/lldb-platform-work/source/Host/macosx/Host.mm

Modified: lldb/branches/lldb-platform-work/include/lldb/Host/Host.h
URL: http://llvm.org/viewvc/llvm-project/lldb/branches/lldb-platform-work/include/lldb/Host/Host.h?rev=154328&r1=154327&r2=154328&view=diff
==============================================================================
--- lldb/branches/lldb-platform-work/include/lldb/Host/Host.h (original)
+++ lldb/branches/lldb-platform-work/include/lldb/Host/Host.h Mon Apr  9 12:54:16 2012
@@ -13,8 +13,11 @@
 
 #include <stdarg.h>
 
+#include <map>
+
 #include "lldb/lldb-private.h"
 #include "lldb/Core/StringList.h"
+#include "lldb/Host/File.h"
 
 namespace lldb_private {
 

Modified: lldb/branches/lldb-platform-work/include/lldb/lldb-forward.h
URL: http://llvm.org/viewvc/llvm-project/lldb/branches/lldb-platform-work/include/lldb/lldb-forward.h?rev=154328&r1=154327&r2=154328&view=diff
==============================================================================
--- lldb/branches/lldb-platform-work/include/lldb/lldb-forward.h (original)
+++ lldb/branches/lldb-platform-work/include/lldb/lldb-forward.h Mon Apr  9 12:54:16 2012
@@ -102,6 +102,7 @@
 class   ExecutionContextRef;
 class   ExecutionContextRefLocker;
 class   ExecutionContextScope;
+class   File;
 class   FileSpec;
 class   FileSpecList;
 class   Flags;
@@ -269,6 +270,7 @@
     typedef STD_SHARED_PTR(lldb_private::Event) EventSP;
     typedef STD_SHARED_PTR(lldb_private::ExecutionContextRef) ExecutionContextRefSP;
     typedef STD_SHARED_PTR(lldb_private::TypeCategoryImpl) TypeCategoryImplSP;
+    typedef STD_SHARED_PTR(lldb_private::File) FileSP;
     typedef STD_SHARED_PTR(lldb_private::Function) FunctionSP;
     typedef STD_SHARED_PTR(lldb_private::InlineFunctionInfo) InlineFunctionInfoSP;
     typedef STD_SHARED_PTR(lldb_private::InputReader) InputReaderSP;

Modified: lldb/branches/lldb-platform-work/source/Commands/CommandObjectPlatform.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/branches/lldb-platform-work/source/Commands/CommandObjectPlatform.cpp?rev=154328&r1=154327&r2=154328&view=diff
==============================================================================
--- lldb/branches/lldb-platform-work/source/Commands/CommandObjectPlatform.cpp (original)
+++ lldb/branches/lldb-platform-work/source/Commands/CommandObjectPlatform.cpp Mon Apr  9 12:54:16 2012
@@ -328,7 +328,10 @@
             std::string cmd_line;
             args.GetCommandString(cmd_line);
             // TODO: make permissions safer
-            uint32_t retcode = platform_sp->OpenFile(FileSpec(cmd_line.c_str(),false),0x0200 | 0x0002, 0000700 | 0000070 | 0000007);
+            uint32_t retcode = platform_sp->OpenFile(FileSpec(cmd_line.c_str(),false),
+                                                     File::eOpenOptionRead | File::eOpenOptionWrite |
+                                                     File::eOpenOptionAppend | File::eOpenOptionCanCreate,
+                                                     0000700 | 0000070 | 0000007);
             result.AppendMessageWithFormat("Status = %d\n",retcode);
             result.SetStatus (eReturnStatusSuccessFinishResult);
         }

Modified: lldb/branches/lldb-platform-work/source/Host/common/Host.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/branches/lldb-platform-work/source/Host/common/Host.cpp?rev=154328&r1=154327&r2=154328&view=diff
==============================================================================
--- lldb/branches/lldb-platform-work/source/Host/common/Host.cpp (original)
+++ lldb/branches/lldb-platform-work/source/Host/common/Host.cpp Mon Apr  9 12:54:16 2012
@@ -1273,31 +1273,95 @@
 {
     return UINT32_MAX;
 }
+#endif
+
+typedef std::map<uint32_t, lldb::FileSP> FDToFileMap;
+FDToFileMap& GetFDToFileMap()
+{
+    static FDToFileMap g_fd2filemap;
+    return g_fd2filemap;
+}
 
 uint32_t
 Host::OpenFile (const FileSpec& file_spec,
                 uint32_t flags,
                 mode_t mode)
 {
-    return UINT32_MAX;
+    std::string path(512, ' ');
+    uint32_t len = file_spec.GetPath(&path[0], 512);
+    if (len >= 512)
+    {
+        path = std::string(len+1,' ');
+        len = file_spec.GetPath(&path[0], len);
+    }
+    FileSP file_sp(new File(path.c_str(),flags,mode));
+    if (file_sp->IsValid() == false)
+        return UINT32_MAX;
+    uint32_t fd = file_sp->GetDescriptor();
+    GetFDToFileMap()[fd] = file_sp;
+    return fd;
 }
 
 bool
 Host::CloseFile (uint32_t fd)
 {
-    return false;
+    if (fd == UINT32_MAX)
+        return false;
+    FDToFileMap::iterator i = GetFDToFileMap().find(fd),
+    end = GetFDToFileMap().end();
+    if (i == end)
+        return false;
+    FileSP file_sp = i->second;
+    Error err;
+    if (file_sp)
+        err = file_sp->Close();
+    GetFDToFileMap().erase(i);
+    return err.Success();
 }
 
 uint32_t
 Host::WriteFile (uint32_t fd, uint64_t offset, void* data, size_t data_len)
 {
-    return UINT32_MAX;
+    if (fd == UINT32_MAX)
+        return false;
+    FDToFileMap::iterator i = GetFDToFileMap().find(fd),
+    end = GetFDToFileMap().end();
+    if (i == end)
+        return false;
+    FileSP file_sp = i->second;
+    off_t offset_ = offset;
+    Error err = file_sp->SeekFromStart(offset_);
+    if (err.Fail())
+        return 0;
+    if (!file_sp)
+        return 0;
+    size_t data_len_ = data_len;
+    err = file_sp->Write(data,data_len_);
+    if (err.Fail())
+        return 0;
+    return data_len_;
 }
 
 uint32_t
-Host::ReadFile (uint32_t fd, uint64_t offset, uint8_t* data_ptr, size_t len_wanted)
+Host::ReadFile (uint32_t fd, uint64_t offset, void* data_ptr, size_t len_wanted)
 {
-    return UINT32_MAX;
+    if (fd == UINT32_MAX)
+        return false;
+    FDToFileMap::iterator i = GetFDToFileMap().find(fd),
+    end = GetFDToFileMap().end();
+    if (i == end)
+        return false;
+    FileSP file_sp = i->second;
+    off_t offset_ = offset;
+    Error err = file_sp->SeekFromStart(offset_);
+    if (err.Fail())
+        return 0;
+    if (!file_sp)
+        return 0;
+    size_t len_wanted_ = len_wanted;
+    err = file_sp->Read(data_ptr,len_wanted_);
+    if (err.Fail())
+        return 0;
+    return len_wanted_;
 }
 
-#endif

Modified: lldb/branches/lldb-platform-work/source/Host/macosx/Host.mm
URL: http://llvm.org/viewvc/llvm-project/lldb/branches/lldb-platform-work/source/Host/macosx/Host.mm?rev=154328&r1=154327&r2=154328&view=diff
==============================================================================
--- lldb/branches/lldb-platform-work/source/Host/macosx/Host.mm (original)
+++ lldb/branches/lldb-platform-work/source/Host/macosx/Host.mm Mon Apr  9 12:54:16 2012
@@ -1863,35 +1863,3 @@
     return ::mkdir(path,mode);
 }
 
-uint32_t
-Host::OpenFile (const FileSpec& file_spec,
-                uint32_t flags,
-                mode_t mode)
-{
-    std::string path(512, ' ');
-    uint32_t len = file_spec.GetPath(&path[0], 512);
-    if (len >= 512)
-    {
-        path = std::string(len+1,' ');
-        len = file_spec.GetPath(&path[0], len);
-    }
-    return ::open(path.c_str(),flags,mode);
-}
-
-bool
-Host::CloseFile (uint32_t fd)
-{
-    return (::close(fd) == 0);
-}
-
-uint32_t
-Host::WriteFile (uint32_t fd, uint64_t offset, void* data, size_t data_len)
-{
-    return ::pwrite(fd, data, data_len, offset);
-}
-
-uint32_t
-Host::ReadFile (uint32_t fd, uint64_t offset, void* data_ptr, size_t len_wanted)
-{
-    return ::pread(fd, data_ptr, len_wanted, offset);
-}





More information about the lldb-commits mailing list