99#include < fcntl.h>
1010#include < sstream>
1111#include < sys/procfs.h>
12-
1312#include " lldb/Host/Host.h"
1413#include " lldb/Host/linux/Support.h"
1514#include " lldb/Utility/LLDBLog.h"
@@ -41,7 +40,6 @@ static bool GetStatusInfo(::pid_t Pid, ProcessInstanceInfo &ProcessInfo,
4140 ProcessState &State, ::pid_t &TracerPid,
4241 ::pid_t &Tgid) {
4342 Log *log = GetLog (LLDBLog::Host);
44-
4543 auto BufferOrError = getProcFile (Pid, " status" );
4644 if (!BufferOrError)
4745 return false ;
@@ -50,15 +48,13 @@ static bool GetStatusInfo(::pid_t Pid, ProcessInstanceInfo &ProcessInfo,
5048 while (!Rest.empty ()) {
5149 llvm::StringRef Line;
5250 std::tie (Line, Rest) = Rest.split (' \n ' );
53-
5451 if (Line.consume_front (" Gid:" )) {
5552 // Real, effective, saved set, and file system GIDs. Read the first two.
5653 Line = Line.ltrim ();
5754 uint32_t RGid, EGid;
5855 Line.consumeInteger (10 , RGid);
5956 Line = Line.ltrim ();
6057 Line.consumeInteger (10 , EGid);
61-
6258 ProcessInfo.SetGroupID (RGid);
6359 ProcessInfo.SetEffectiveGroupID (EGid);
6460 } else if (Line.consume_front (" Uid:" )) {
@@ -68,7 +64,6 @@ static bool GetStatusInfo(::pid_t Pid, ProcessInstanceInfo &ProcessInfo,
6864 Line.consumeInteger (10 , RUid);
6965 Line = Line.ltrim ();
7066 Line.consumeInteger (10 , EUid);
71-
7267 ProcessInfo.SetUserID (RUid);
7368 ProcessInfo.SetEffectiveUserID (EUid);
7469 } else if (Line.consume_front (" PPid:" )) {
@@ -101,48 +96,28 @@ static bool GetStatusInfo(::pid_t Pid, ProcessInstanceInfo &ProcessInfo,
10196 return true ;
10297}
10398
104- static void GetExePathAndArch (::pid_t pid, ProcessInstanceInfo &process_info) {
105- Log *log = GetLog (LLDBLog::Process);
106- std::string ExePath (PATH_MAX, ' \0 ' );
99+ static bool GetExePathAndArch (::pid_t pid, ProcessInstanceInfo &process_info) {
107100 struct psinfo psinfoData;
108-
109- // We can't use getProcFile here because proc/[pid]/exe is a symbolic link.
110- llvm::SmallString<64 > ProcExe;
111- (llvm::Twine (" /proc/" ) + llvm::Twine (pid) + " /cwd" ).toVector (ProcExe);
112-
113- ssize_t len = readlink (ProcExe.c_str (), &ExePath[0 ], PATH_MAX);
114- if (len > 0 ) {
115- ExePath.resize (len);
116-
117- struct stat statData;
118-
119- std::ostringstream oss;
120-
121- oss << " /proc/" << std::dec << pid << " /psinfo" ;
122- assert (stat (oss.str ().c_str (), &statData) == 0 );
123-
124- const int fd = open (oss.str ().c_str (), O_RDONLY);
125- assert (fd >= 0 );
126-
127- ssize_t readNum = read (fd, &psinfoData, sizeof (psinfoData));
128- assert (readNum >= 0 );
129-
130- close (fd);
131- } else {
132- LLDB_LOG (log, " failed to read link exe link for {0}: {1}" , pid,
133- Status (errno, eErrorTypePOSIX));
134- ExePath.resize (0 );
135- }
136-
101+ auto BufferOrError = getProcFile (pid, " psinfo" );
102+ if (!BufferOrError)
103+ return false ;
104+
105+ std::unique_ptr<llvm::MemoryBuffer> PsinfoBuffer = std::move (*BufferOrError);
106+ // Ensure there's enough data for psinfoData
107+ if (PsinfoBuffer->getBufferSize () < sizeof (psinfoData))
108+ return false ;
109+
110+ std::memcpy (&psinfoData, PsinfoBuffer->getBufferStart (), sizeof (psinfoData));
137111 llvm::StringRef PathRef (&(psinfoData.pr_psargs [0 ]));
138-
139112 if (!PathRef.empty ()) {
140113 process_info.GetExecutableFile ().SetFile (PathRef, FileSpec::Style::native);
141114 ArchSpec arch_spec = ArchSpec ();
142115 arch_spec.SetArchitecture (eArchTypeXCOFF, XCOFF::TCPU_PPC64,
143116 LLDB_INVALID_CPUTYPE, llvm::Triple::AIX);
144117 process_info.SetArchitecture (arch_spec);
118+ return true ;
145119 }
120+ return false ;
146121}
147122
148123static bool GetProcessAndStatInfo (::pid_t pid,
@@ -151,11 +126,10 @@ static bool GetProcessAndStatInfo(::pid_t pid,
151126 ::pid_t tgid;
152127 tracerpid = 0 ;
153128 process_info.Clear ();
154-
155129 process_info.SetProcessID (pid);
156130
157- GetExePathAndArch (pid, process_info);
158-
131+ if (! GetExePathAndArch (pid, process_info))
132+ return false ;
159133 // Get User and Group IDs and get tracer pid.
160134 if (!GetStatusInfo (pid, process_info, State, tracerpid, tgid))
161135 return false ;
0 commit comments