Skip to content

Commit dc7aa94

Browse files
committed
[lldb][OpenBSD] Fixes for process handling
1 parent c8f29e3 commit dc7aa94

File tree

1 file changed

+67
-51
lines changed

1 file changed

+67
-51
lines changed

lldb/source/Host/openbsd/Host.cpp

Lines changed: 67 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -40,49 +40,63 @@ class ProcessLaunchInfo;
4040
static bool
4141
GetOpenBSDProcessArgs(const ProcessInstanceInfoMatch *match_info_ptr,
4242
ProcessInstanceInfo &process_info) {
43-
if (process_info.ProcessIDIsValid()) {
44-
int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ARGS,
45-
(int)process_info.GetProcessID()};
46-
47-
char arg_data[8192];
48-
size_t arg_data_size = sizeof(arg_data);
49-
if (::sysctl(mib, 4, arg_data, &arg_data_size, NULL, 0) == 0) {
50-
DataExtractor data(arg_data, arg_data_size, endian::InlHostByteOrder(),
51-
sizeof(void *));
52-
lldb::offset_t offset = 0;
53-
const char *cstr;
54-
55-
cstr = data.GetCStr(&offset);
56-
if (cstr) {
57-
process_info.GetExecutableFile().SetFile(cstr, FileSpec::Style::native);
58-
59-
if (!(match_info_ptr == NULL ||
60-
NameMatches(
61-
process_info.GetExecutableFile().GetFilename().GetCString(),
62-
match_info_ptr->GetNameMatchType(),
63-
match_info_ptr->GetProcessInfo().GetName())))
64-
return false;
65-
66-
Args &proc_args = process_info.GetArguments();
67-
while (1) {
68-
const uint8_t *p = data.PeekData(offset, 1);
69-
while ((p != NULL) && (*p == '\0') && offset < arg_data_size) {
70-
++offset;
71-
p = data.PeekData(offset, 1);
72-
}
73-
if (p == NULL || offset >= arg_data_size)
74-
return true;
75-
76-
cstr = data.GetCStr(&offset);
77-
if (cstr)
78-
proc_args.AppendArgument(llvm::StringRef(cstr));
79-
else
80-
return true;
81-
}
82-
}
83-
}
43+
if (!process_info.ProcessIDIsValid())
44+
return false;
45+
46+
int pid = process_info.GetProcessID();
47+
48+
int mib[4] = {CTL_KERN, KERN_PROC_ARGS, pid, KERN_PROC_ARGV};
49+
size_t kern_proc_args_size = 0;
50+
51+
// On OpenBSD, this will just fill ARG_MAX all the time
52+
if (::sysctl(mib, 4, NULL, &kern_proc_args_size, NULL, 0) == -1)
53+
return false;
54+
55+
std::string arg_data(kern_proc_args_size, 0);
56+
57+
if (::sysctl(mib, 4, (void *)arg_data.data(), &kern_proc_args_size, NULL,
58+
0) == -1)
59+
return false;
60+
61+
arg_data.resize(kern_proc_args_size);
62+
63+
// arg_data is a NULL terminated list of pointers, where the pointers
64+
// point within arg_data to the location of the arg string
65+
DataExtractor data(arg_data.data(), arg_data.length(),
66+
endian::InlHostByteOrder(), sizeof(void *));
67+
68+
lldb::offset_t offset = 0;
69+
lldb::offset_t arg_offset = 0;
70+
uint64_t arg_addr = 0;
71+
const char *cstr;
72+
73+
arg_addr = data.GetAddress(&offset);
74+
arg_offset = arg_addr - (uint64_t)arg_data.data();
75+
cstr = data.GetCStr(&arg_offset);
76+
77+
if (!cstr)
78+
return false;
79+
80+
process_info.GetExecutableFile().SetFile(cstr, FileSpec::Style::native);
81+
82+
if (match_info_ptr != NULL &&
83+
!NameMatches(process_info.GetExecutableFile().GetFilename().GetCString(),
84+
match_info_ptr->GetNameMatchType(),
85+
match_info_ptr->GetProcessInfo().GetName())) {
86+
return false;
8487
}
85-
return false;
88+
89+
Args &proc_args = process_info.GetArguments();
90+
91+
while (1) {
92+
arg_addr = data.GetAddress(&offset);
93+
if (arg_addr == 0)
94+
break;
95+
arg_offset = arg_addr - (uint64_t)arg_data.data();
96+
cstr = data.GetCStr(&arg_offset);
97+
proc_args.AppendArgument(cstr);
98+
}
99+
return true;
86100
}
87101

88102
static bool GetOpenBSDProcessCPUType(ProcessInstanceInfo &process_info) {
@@ -96,15 +110,15 @@ static bool GetOpenBSDProcessCPUType(ProcessInstanceInfo &process_info) {
96110
}
97111

98112
static bool GetOpenBSDProcessUserAndGroup(ProcessInstanceInfo &process_info) {
99-
struct kinfo_proc proc_kinfo;
100-
size_t proc_kinfo_size;
101113

102114
if (process_info.ProcessIDIsValid()) {
103-
int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID,
104-
(int)process_info.GetProcessID()};
105-
proc_kinfo_size = sizeof(struct kinfo_proc);
115+
struct kinfo_proc proc_kinfo = {};
116+
size_t proc_kinfo_size = sizeof(proc_kinfo);
117+
int mib[6] = {CTL_KERN, KERN_PROC,
118+
KERN_PROC_PID, (int)process_info.GetProcessID(),
119+
sizeof(proc_kinfo), 1};
106120

107-
if (::sysctl(mib, 4, &proc_kinfo, &proc_kinfo_size, NULL, 0) == 0) {
121+
if (::sysctl(mib, 6, &proc_kinfo, &proc_kinfo_size, NULL, 0) == 0) {
108122
if (proc_kinfo_size > 0) {
109123
process_info.SetParentProcessID(proc_kinfo.p_ppid);
110124
process_info.SetUserID(proc_kinfo.p_ruid);
@@ -127,20 +141,22 @@ uint32_t Host::FindProcessesImpl(const ProcessInstanceInfoMatch &match_info,
127141
ProcessInstanceInfoList &process_infos) {
128142
std::vector<struct kinfo_proc> kinfos;
129143

130-
int mib[3] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL};
144+
int mib[6] = {
145+
CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), 0};
131146

132147
size_t pid_data_size = 0;
133-
if (::sysctl(mib, 3, NULL, &pid_data_size, NULL, 0) != 0)
148+
if (::sysctl(mib, 6, NULL, &pid_data_size, NULL, 0) != 0)
134149
return 0;
135150

136151
// Add a few extra in case a few more show up
137152
const size_t estimated_pid_count =
138153
(pid_data_size / sizeof(struct kinfo_proc)) + 10;
139154

140155
kinfos.resize(estimated_pid_count);
156+
mib[5] = estimated_pid_count;
141157
pid_data_size = kinfos.size() * sizeof(struct kinfo_proc);
142158

143-
if (::sysctl(mib, 3, &kinfos[0], &pid_data_size, NULL, 0) != 0)
159+
if (::sysctl(mib, 6, &kinfos[0], &pid_data_size, NULL, 0) != 0)
144160
return 0;
145161

146162
const size_t actual_pid_count = (pid_data_size / sizeof(struct kinfo_proc));

0 commit comments

Comments
 (0)