diff options
author | Todd Fiala <todd.fiala@gmail.com> | 2014-06-30 04:14:13 +0000 |
---|---|---|
committer | Todd Fiala <todd.fiala@gmail.com> | 2014-06-30 04:14:13 +0000 |
commit | 3dc2fb2fc03b315951b8739435bbfc7f6854576b (patch) | |
tree | 361639a73ce7cf3fe8845fc745a3345d5408a2dc /lldb/source/Host/linux/Host.cpp | |
parent | 8dfca43296dc311a342bf9ebf8b37cf19724c3e9 (diff) | |
download | bcm5719-llvm-3dc2fb2fc03b315951b8739435bbfc7f6854576b.tar.gz bcm5719-llvm-3dc2fb2fc03b315951b8739435bbfc7f6854576b.zip |
Factored out Linux proc file reading into separate class.
Both NativeProcessLinux (in llgs branch) and Linux Host.cpp had similar code to handle /proc
file reading. I factored that out into a new Linux-specific ProcFileReader class and added a method
that the llgs branch will use for line-by-line parsing.
This change also adds numerous Linux-specific files to Xcode that were missing from the Xcode
project files.
Related to https://github.com/tfiala/lldb/issues/27
llvm-svn: 212015
Diffstat (limited to 'lldb/source/Host/linux/Host.cpp')
-rw-r--r-- | lldb/source/Host/linux/Host.cpp | 69 |
1 files changed, 7 insertions, 62 deletions
diff --git a/lldb/source/Host/linux/Host.cpp b/lldb/source/Host/linux/Host.cpp index 3c05243f92a..b30e6d5613f 100644 --- a/lldb/source/Host/linux/Host.cpp +++ b/lldb/source/Host/linux/Host.cpp @@ -29,6 +29,7 @@ #include "lldb/Core/ModuleSpec.h" #include "lldb/Symbol/ObjectFile.h" +#include "Plugins/Process/Linux/ProcFileReader.h" using namespace lldb; using namespace lldb_private; @@ -52,67 +53,11 @@ typedef struct ProcessStatInfo // Get the process info with additional information from /proc/$PID/stat (like process state, and tracer pid). static bool GetProcessAndStatInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info, ProcessStatInfo &stat_info, lldb::pid_t &tracerpid); - -namespace -{ - -lldb::DataBufferSP -ReadProcPseudoFile (lldb::pid_t pid, const char *name) -{ - int fd; - char path[PATH_MAX]; - - // Make sure we've got a nil terminated buffer for all the folks calling - // GetBytes() directly off our returned DataBufferSP if we hit an error. - lldb::DataBufferSP buf_sp (new DataBufferHeap(1, 0)); - - // Ideally, we would simply create a FileSpec and call ReadFileContents. - // However, files in procfs have zero size (since they are, in general, - // dynamically generated by the kernel) which is incompatible with the - // current ReadFileContents implementation. Therefore we simply stream the - // data into a DataBuffer ourselves. - if (snprintf (path, PATH_MAX, "/proc/%" PRIu64 "/%s", pid, name) > 0) - { - if ((fd = open (path, O_RDONLY, 0)) >= 0) - { - size_t bytes_read = 0; - std::unique_ptr<DataBufferHeap> buf_ap(new DataBufferHeap(1024, 0)); - - for (;;) - { - size_t avail = buf_ap->GetByteSize() - bytes_read; - ssize_t status = read (fd, buf_ap->GetBytes() + bytes_read, avail); - - if (status < 0) - break; - - if (status == 0) - { - buf_ap->SetByteSize (bytes_read); - buf_sp.reset (buf_ap.release()); - break; - } - - bytes_read += status; - - if (avail - status == 0) - buf_ap->SetByteSize (2 * buf_ap->GetByteSize()); - } - - close (fd); - } - } - - return buf_sp; -} - -} // anonymous namespace - static bool ReadProcPseudoFileStat (lldb::pid_t pid, ProcessStatInfo& stat_info) { // Read the /proc/$PID/stat file. - lldb::DataBufferSP buf_sp = ReadProcPseudoFile (pid, "stat"); + lldb::DataBufferSP buf_sp = ProcFileReader::ReadIntoDataBuffer (pid, "stat"); // The filename of the executable is stored in parenthesis right after the pid. We look for the closing // parenthesis for the filename and work from there in case the name has something funky like ')' in it. @@ -165,7 +110,7 @@ GetLinuxProcessUserAndGroup (lldb::pid_t pid, ProcessInstanceInfo &process_info, uint32_t eGid = UINT32_MAX; // Effective Group ID // Read the /proc/$PID/status file and parse the Uid:, Gid:, and TracerPid: fields. - lldb::DataBufferSP buf_sp = ReadProcPseudoFile (pid, "status"); + lldb::DataBufferSP buf_sp = ProcFileReader::ReadIntoDataBuffer (pid, "status"); static const char uid_token[] = "Uid:"; char *buf_uid = strstr ((char *)buf_sp->GetBytes(), uid_token); @@ -227,7 +172,7 @@ Host::GetOSVersion(uint32_t &major, lldb::DataBufferSP Host::GetAuxvData(lldb_private::Process *process) { - return ReadProcPseudoFile(process->GetID(), "auxv"); + return ProcFileReader::ReadIntoDataBuffer (process->GetID(), "auxv"); } static bool @@ -391,7 +336,7 @@ GetProcessAndStatInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info, Proce lldb::DataBufferSP buf_sp; // Get the process environment. - buf_sp = ReadProcPseudoFile(pid, "environ"); + buf_sp = ProcFileReader::ReadIntoDataBuffer(pid, "environ"); Args &info_env = process_info.GetEnvironmentEntries(); char *next_var = (char *)buf_sp->GetBytes(); char *end_buf = next_var + buf_sp->GetByteSize(); @@ -402,7 +347,7 @@ GetProcessAndStatInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info, Proce } // Get the commond line used to start the process. - buf_sp = ReadProcPseudoFile(pid, "cmdline"); + buf_sp = ProcFileReader::ReadIntoDataBuffer(pid, "cmdline"); // Grab Arg0 first, if there is one. char *cmd = (char *)buf_sp->GetBytes(); @@ -458,7 +403,7 @@ Host::GetThreadName (lldb::pid_t pid, lldb::tid_t tid) assert(tid != LLDB_INVALID_THREAD_ID); // Read /proc/$TID/comm file. - lldb::DataBufferSP buf_sp = ReadProcPseudoFile (tid, "comm"); + lldb::DataBufferSP buf_sp = ProcFileReader::ReadIntoDataBuffer (tid, "comm"); const char *comm_str = (const char *)buf_sp->GetBytes(); const char *cr_str = ::strchr(comm_str, '\n'); size_t length = cr_str ? (cr_str - comm_str) : strlen(comm_str); |