9
9
#include < fcntl.h>
10
10
#include < sstream>
11
11
#include < sys/procfs.h>
12
-
13
12
#include " lldb/Host/Host.h"
14
13
#include " lldb/Host/linux/Support.h"
15
14
#include " lldb/Utility/LLDBLog.h"
@@ -41,7 +40,6 @@ static bool GetStatusInfo(::pid_t Pid, ProcessInstanceInfo &ProcessInfo,
41
40
ProcessState &State, ::pid_t &TracerPid,
42
41
::pid_t &Tgid) {
43
42
Log *log = GetLog (LLDBLog::Host);
44
-
45
43
auto BufferOrError = getProcFile (Pid, " status" );
46
44
if (!BufferOrError)
47
45
return false ;
@@ -50,15 +48,13 @@ static bool GetStatusInfo(::pid_t Pid, ProcessInstanceInfo &ProcessInfo,
50
48
while (!Rest.empty ()) {
51
49
llvm::StringRef Line;
52
50
std::tie (Line, Rest) = Rest.split (' \n ' );
53
-
54
51
if (Line.consume_front (" Gid:" )) {
55
52
// Real, effective, saved set, and file system GIDs. Read the first two.
56
53
Line = Line.ltrim ();
57
54
uint32_t RGid, EGid;
58
55
Line.consumeInteger (10 , RGid);
59
56
Line = Line.ltrim ();
60
57
Line.consumeInteger (10 , EGid);
61
-
62
58
ProcessInfo.SetGroupID (RGid);
63
59
ProcessInfo.SetEffectiveGroupID (EGid);
64
60
} else if (Line.consume_front (" Uid:" )) {
@@ -68,7 +64,6 @@ static bool GetStatusInfo(::pid_t Pid, ProcessInstanceInfo &ProcessInfo,
68
64
Line.consumeInteger (10 , RUid);
69
65
Line = Line.ltrim ();
70
66
Line.consumeInteger (10 , EUid);
71
-
72
67
ProcessInfo.SetUserID (RUid);
73
68
ProcessInfo.SetEffectiveUserID (EUid);
74
69
} else if (Line.consume_front (" PPid:" )) {
@@ -101,48 +96,28 @@ static bool GetStatusInfo(::pid_t Pid, ProcessInstanceInfo &ProcessInfo,
101
96
return true ;
102
97
}
103
98
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) {
107
100
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));
137
111
llvm::StringRef PathRef (&(psinfoData.pr_psargs [0 ]));
138
-
139
112
if (!PathRef.empty ()) {
140
113
process_info.GetExecutableFile ().SetFile (PathRef, FileSpec::Style ::native);
141
114
ArchSpec arch_spec = ArchSpec ();
142
115
arch_spec.SetArchitecture (eArchTypeXCOFF, XCOFF::TCPU_PPC64,
143
116
LLDB_INVALID_CPUTYPE, llvm::Triple::AIX);
144
117
process_info.SetArchitecture (arch_spec);
118
+ return true ;
145
119
}
120
+ return false ;
146
121
}
147
122
148
123
static bool GetProcessAndStatInfo (::pid_t pid,
@@ -151,11 +126,10 @@ static bool GetProcessAndStatInfo(::pid_t pid,
151
126
::pid_t tgid;
152
127
tracerpid = 0 ;
153
128
process_info.Clear ();
154
-
155
129
process_info.SetProcessID (pid);
156
130
157
- GetExePathAndArch (pid, process_info);
158
-
131
+ if (! GetExePathAndArch (pid, process_info))
132
+ return false ;
159
133
// Get User and Group IDs and get tracer pid.
160
134
if (!GetStatusInfo (pid, process_info, State, tracerpid, tgid))
161
135
return false ;
0 commit comments