[lldb][NFC] Fix all formatting errors in .cpp file headers
Summary:
A *.cpp file header in LLDB (and in LLDB) should like this:
```
//===-- TestUtilities.cpp -------------------------------------------------===//
```
However in LLDB most of our source files have arbitrary changes to this format and
these changes are spreading through LLDB as folks usually just use the existing
source files as templates for their new files (most notably the unnecessary
editor language indicator `-*- C++ -*-` is spreading and in every review
someone is pointing out that this is wrong, resulting in people pointing out that this
is done in the same way in other files).
This patch removes most of these inconsistencies including the editor language indicators,
all the different missing/additional '-' characters, files that center the file name, missing
trailing `===//` (mostly caused by clang-format breaking the line).
Reviewers: aprantl, espindola, jfb, shafik, JDevlieghere
Reviewed By: JDevlieghere
Subscribers: dexonsmith, wuzish, emaste, sdardis, nemanjai, kbarton, MaskRay, atanasyan, arphaman, jfb, abidh, jsji, JDevlieghere, usaxena95, lldb-commits
Tags: #lldb
Differential Revision: https://reviews.llvm.org/D73258
2020-01-24 15:23:27 +08:00
|
|
|
//===-- source/Host/freebsd/Host.cpp --------------------------------------===//
|
2011-08-03 04:52:42 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2011-08-03 04:52:42 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-09-07 05:25:31 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <sys/exec.h>
|
2013-08-27 07:57:52 +08:00
|
|
|
#include <sys/proc.h>
|
2016-09-07 05:25:31 +08:00
|
|
|
#include <sys/ptrace.h>
|
2011-08-03 04:52:42 +08:00
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/user.h>
|
|
|
|
|
2012-01-06 03:17:38 +08:00
|
|
|
#include <machine/elf.h>
|
2016-09-07 05:25:31 +08:00
|
|
|
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <execinfo.h>
|
|
|
|
#include <stdio.h>
|
2012-01-06 03:17:38 +08:00
|
|
|
|
2011-08-03 04:52:42 +08:00
|
|
|
#include "lldb/Host/Host.h"
|
2014-08-21 00:42:51 +08:00
|
|
|
#include "lldb/Host/HostInfo.h"
|
2017-06-30 19:31:13 +08:00
|
|
|
#include "lldb/Utility/DataBufferHeap.h"
|
2017-03-04 09:30:05 +08:00
|
|
|
#include "lldb/Utility/DataExtractor.h"
|
2017-02-15 03:06:07 +08:00
|
|
|
#include "lldb/Utility/Endian.h"
|
2017-03-04 04:56:28 +08:00
|
|
|
#include "lldb/Utility/Log.h"
|
2017-06-30 19:31:13 +08:00
|
|
|
#include "lldb/Utility/NameMatches.h"
|
Move ProcessInfo from Host to Utility.
There are set of classes in Target that describe the parameters of a
process - e.g. it's PID, name, user id, and similar. However, since it
is a bare description of a process and contains no actual functionality,
there's nothing specifically that makes this appropriate for being in
Target -- it could just as well be describing a process on the host, or
some hypothetical virtual process that doesn't even exist.
To cement this, I'm moving these classes to Utility. It's possible that
we can find a better place for it in the future, but as it is neither
Host specific nor Target specific, Utility seems like the most appropriate
place for the time being.
After this there is only 2 remaining references to Target from Host,
which I'll address in a followup.
Differential Revision: https://reviews.llvm.org/D58842
llvm-svn: 355342
2019-03-05 05:51:03 +08:00
|
|
|
#include "lldb/Utility/ProcessInfo.h"
|
2017-05-12 12:51:55 +08:00
|
|
|
#include "lldb/Utility/Status.h"
|
2017-02-03 05:39:50 +08:00
|
|
|
#include "lldb/Utility/StreamString.h"
|
2011-08-03 04:52:42 +08:00
|
|
|
|
2014-08-30 01:35:57 +08:00
|
|
|
#include "llvm/Support/Host.h"
|
2012-01-06 03:17:38 +08:00
|
|
|
|
2011-08-03 04:52:42 +08:00
|
|
|
extern "C" {
|
2012-01-06 03:17:38 +08:00
|
|
|
extern char **environ;
|
2011-08-03 04:52:42 +08:00
|
|
|
}
|
|
|
|
|
Move ProcessInfo from Host to Utility.
There are set of classes in Target that describe the parameters of a
process - e.g. it's PID, name, user id, and similar. However, since it
is a bare description of a process and contains no actual functionality,
there's nothing specifically that makes this appropriate for being in
Target -- it could just as well be describing a process on the host, or
some hypothetical virtual process that doesn't even exist.
To cement this, I'm moving these classes to Utility. It's possible that
we can find a better place for it in the future, but as it is neither
Host specific nor Target specific, Utility seems like the most appropriate
place for the time being.
After this there is only 2 remaining references to Target from Host,
which I'll address in a followup.
Differential Revision: https://reviews.llvm.org/D58842
llvm-svn: 355342
2019-03-05 05:51:03 +08:00
|
|
|
namespace lldb_private {
|
|
|
|
class ProcessLaunchInfo;
|
|
|
|
}
|
|
|
|
|
2011-08-03 04:52:42 +08:00
|
|
|
using namespace lldb;
|
|
|
|
using namespace lldb_private;
|
|
|
|
|
|
|
|
static bool
|
|
|
|
GetFreeBSDProcessArgs(const ProcessInstanceInfoMatch *match_info_ptr,
|
|
|
|
ProcessInstanceInfo &process_info) {
|
2017-04-23 10:28:28 +08:00
|
|
|
if (!process_info.ProcessIDIsValid())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
int pid = process_info.GetProcessID();
|
|
|
|
|
|
|
|
int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ARGS, pid};
|
|
|
|
|
|
|
|
char arg_data[8192];
|
|
|
|
size_t arg_data_size = sizeof(arg_data);
|
|
|
|
if (::sysctl(mib, 4, arg_data, &arg_data_size, NULL, 0) != 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
DataExtractor data(arg_data, arg_data_size, endian::InlHostByteOrder(),
|
|
|
|
sizeof(void *));
|
|
|
|
lldb::offset_t offset = 0;
|
|
|
|
const char *cstr;
|
|
|
|
|
|
|
|
cstr = data.GetCStr(&offset);
|
|
|
|
if (!cstr)
|
|
|
|
return false;
|
|
|
|
|
2017-09-03 09:41:31 +08:00
|
|
|
// Get pathname for pid. If that fails fall back to argv[0].
|
|
|
|
char pathname[MAXPATHLEN];
|
|
|
|
size_t pathname_len = sizeof(pathname);
|
|
|
|
mib[2] = KERN_PROC_PATHNAME;
|
|
|
|
if (::sysctl(mib, 4, pathname, &pathname_len, NULL, 0) == 0)
|
2018-11-02 05:26:58 +08:00
|
|
|
process_info.GetExecutableFile().SetFile(pathname, FileSpec::Style::native);
|
2017-09-03 09:41:31 +08:00
|
|
|
else
|
2018-11-02 05:26:58 +08:00
|
|
|
process_info.GetExecutableFile().SetFile(cstr, FileSpec::Style::native);
|
2017-04-23 10:28:28 +08:00
|
|
|
|
|
|
|
if (!(match_info_ptr == NULL ||
|
|
|
|
NameMatches(process_info.GetExecutableFile().GetFilename().GetCString(),
|
|
|
|
match_info_ptr->GetNameMatchType(),
|
|
|
|
match_info_ptr->GetProcessInfo().GetName())))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Args &proc_args = process_info.GetArguments();
|
|
|
|
while (1) {
|
|
|
|
const uint8_t *p = data.PeekData(offset, 1);
|
|
|
|
while ((p != NULL) && (*p == '\0') && offset < arg_data_size) {
|
|
|
|
++offset;
|
|
|
|
p = data.PeekData(offset, 1);
|
2011-08-03 04:52:42 +08:00
|
|
|
}
|
2017-04-23 10:28:28 +08:00
|
|
|
if (p == NULL || offset >= arg_data_size)
|
|
|
|
break;
|
|
|
|
|
|
|
|
cstr = data.GetCStr(&offset);
|
|
|
|
if (!cstr)
|
|
|
|
break;
|
|
|
|
|
|
|
|
proc_args.AppendArgument(llvm::StringRef(cstr));
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2017-04-23 10:28:28 +08:00
|
|
|
|
|
|
|
return true;
|
2011-08-03 04:52:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool GetFreeBSDProcessCPUType(ProcessInstanceInfo &process_info) {
|
2013-08-27 07:57:52 +08:00
|
|
|
if (process_info.ProcessIDIsValid()) {
|
2014-08-21 00:42:51 +08:00
|
|
|
process_info.GetArchitecture() =
|
|
|
|
HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
|
2012-01-06 03:17:38 +08:00
|
|
|
return true;
|
2011-08-03 04:52:42 +08:00
|
|
|
}
|
|
|
|
process_info.GetArchitecture().Clear();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool GetFreeBSDProcessUserAndGroup(ProcessInstanceInfo &process_info) {
|
|
|
|
struct kinfo_proc proc_kinfo;
|
|
|
|
size_t proc_kinfo_size;
|
2017-04-23 10:28:28 +08:00
|
|
|
const int pid = process_info.GetProcessID();
|
|
|
|
int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid};
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2017-04-23 10:28:28 +08:00
|
|
|
if (!process_info.ProcessIDIsValid())
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
proc_kinfo_size = sizeof(struct kinfo_proc);
|
|
|
|
|
|
|
|
if (::sysctl(mib, 4, &proc_kinfo, &proc_kinfo_size, NULL, 0) != 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (proc_kinfo_size == 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
process_info.SetParentProcessID(proc_kinfo.ki_ppid);
|
|
|
|
process_info.SetUserID(proc_kinfo.ki_ruid);
|
|
|
|
process_info.SetGroupID(proc_kinfo.ki_rgid);
|
|
|
|
process_info.SetEffectiveUserID(proc_kinfo.ki_uid);
|
|
|
|
if (proc_kinfo.ki_ngroups > 0)
|
|
|
|
process_info.SetEffectiveGroupID(proc_kinfo.ki_groups[0]);
|
|
|
|
else
|
|
|
|
process_info.SetEffectiveGroupID(UINT32_MAX);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
error:
|
2011-08-03 04:52:42 +08:00
|
|
|
process_info.SetParentProcessID(LLDB_INVALID_PROCESS_ID);
|
|
|
|
process_info.SetUserID(UINT32_MAX);
|
|
|
|
process_info.SetGroupID(UINT32_MAX);
|
|
|
|
process_info.SetEffectiveUserID(UINT32_MAX);
|
2012-01-06 03:17:38 +08:00
|
|
|
process_info.SetEffectiveGroupID(UINT32_MAX);
|
2011-08-03 04:52:42 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-03-16 11:36:40 +08:00
|
|
|
uint32_t Host::FindProcessesImpl(const ProcessInstanceInfoMatch &match_info,
|
|
|
|
ProcessInstanceInfoList &process_infos) {
|
2017-04-23 10:28:28 +08:00
|
|
|
const ::pid_t our_pid = ::getpid();
|
|
|
|
const ::uid_t our_uid = ::getuid();
|
2013-08-27 07:57:52 +08:00
|
|
|
std::vector<struct kinfo_proc> kinfos;
|
2017-04-23 10:28:28 +08:00
|
|
|
// Special case, if lldb is being run as root we can attach to anything.
|
|
|
|
bool all_users = match_info.GetMatchAllUsers() || (our_uid == 0);
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2013-08-27 07:57:52 +08:00
|
|
|
int mib[3] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL};
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2013-08-27 07:57:52 +08:00
|
|
|
size_t pid_data_size = 0;
|
|
|
|
if (::sysctl(mib, 3, NULL, &pid_data_size, NULL, 0) != 0)
|
|
|
|
return 0;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2013-08-27 07:57:52 +08:00
|
|
|
// Add a few extra in case a few more show up
|
|
|
|
const size_t estimated_pid_count =
|
|
|
|
(pid_data_size / sizeof(struct kinfo_proc)) + 10;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2013-08-27 07:57:52 +08:00
|
|
|
kinfos.resize(estimated_pid_count);
|
|
|
|
pid_data_size = kinfos.size() * sizeof(struct kinfo_proc);
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2013-08-27 07:57:52 +08:00
|
|
|
if (::sysctl(mib, 3, &kinfos[0], &pid_data_size, NULL, 0) != 0)
|
|
|
|
return 0;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2013-08-27 07:57:52 +08:00
|
|
|
const size_t actual_pid_count = (pid_data_size / sizeof(struct kinfo_proc));
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2014-02-28 21:46:51 +08:00
|
|
|
for (size_t i = 0; i < actual_pid_count; i++) {
|
2013-08-27 07:57:52 +08:00
|
|
|
const struct kinfo_proc &kinfo = kinfos[i];
|
2017-04-23 10:28:28 +08:00
|
|
|
|
|
|
|
/* Make sure the user is acceptable */
|
|
|
|
if (!all_users && kinfo.ki_ruid != our_uid)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (kinfo.ki_pid == our_pid || // Skip this process
|
|
|
|
kinfo.ki_pid == 0 || // Skip kernel (kernel pid is 0)
|
|
|
|
kinfo.ki_stat == SZOMB || // Zombies are bad
|
|
|
|
kinfo.ki_flag & P_TRACED || // Being debugged?
|
|
|
|
kinfo.ki_flag & P_WEXIT) // Working on exiting
|
2013-08-27 07:57:52 +08:00
|
|
|
continue;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2013-08-27 07:57:52 +08:00
|
|
|
// Every thread is a process in FreeBSD, but all the threads of a single
|
2018-05-01 00:49:04 +08:00
|
|
|
// process have the same pid. Do not store the process info in the result
|
|
|
|
// list if a process with given identifier is already registered there.
|
2013-08-27 07:57:52 +08:00
|
|
|
bool already_registered = false;
|
|
|
|
for (uint32_t pi = 0;
|
|
|
|
!already_registered && (const int)kinfo.ki_numthreads > 1 &&
|
2020-03-13 05:10:25 +08:00
|
|
|
pi < (const uint32_t)process_infos.size();
|
2013-08-27 07:57:52 +08:00
|
|
|
pi++)
|
|
|
|
already_registered =
|
2020-03-13 05:10:25 +08:00
|
|
|
(process_infos[pi].GetProcessID() == (uint32_t)kinfo.ki_pid);
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2013-08-27 07:57:52 +08:00
|
|
|
if (already_registered)
|
|
|
|
continue;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2013-08-27 07:57:52 +08:00
|
|
|
ProcessInstanceInfo process_info;
|
|
|
|
process_info.SetProcessID(kinfo.ki_pid);
|
|
|
|
process_info.SetParentProcessID(kinfo.ki_ppid);
|
|
|
|
process_info.SetUserID(kinfo.ki_ruid);
|
|
|
|
process_info.SetGroupID(kinfo.ki_rgid);
|
|
|
|
process_info.SetEffectiveUserID(kinfo.ki_svuid);
|
|
|
|
process_info.SetEffectiveGroupID(kinfo.ki_svgid);
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2013-08-27 07:57:52 +08:00
|
|
|
// Make sure our info matches before we go fetch the name and cpu type
|
|
|
|
if (match_info.Matches(process_info) &&
|
|
|
|
GetFreeBSDProcessArgs(&match_info, process_info)) {
|
|
|
|
GetFreeBSDProcessCPUType(process_info);
|
|
|
|
if (match_info.Matches(process_info))
|
2020-03-13 05:10:25 +08:00
|
|
|
process_infos.push_back(process_info);
|
2013-08-27 07:57:52 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2013-08-27 07:57:52 +08:00
|
|
|
|
2020-03-13 05:10:25 +08:00
|
|
|
return process_infos.size();
|
2013-08-27 07:57:52 +08:00
|
|
|
}
|
|
|
|
|
2011-08-03 04:52:42 +08:00
|
|
|
bool Host::GetProcessInfo(lldb::pid_t pid, ProcessInstanceInfo &process_info) {
|
|
|
|
process_info.SetProcessID(pid);
|
2013-08-27 07:57:52 +08:00
|
|
|
|
|
|
|
if (GetFreeBSDProcessArgs(NULL, process_info)) {
|
2011-08-03 04:52:42 +08:00
|
|
|
// should use libprocstat instead of going right into sysctl?
|
|
|
|
GetFreeBSDProcessCPUType(process_info);
|
|
|
|
GetFreeBSDProcessUserAndGroup(process_info);
|
|
|
|
return true;
|
|
|
|
}
|
2013-08-27 07:57:52 +08:00
|
|
|
|
2011-08-03 04:52:42 +08:00
|
|
|
process_info.Clear();
|
|
|
|
return false;
|
|
|
|
}
|
2012-01-06 03:17:38 +08:00
|
|
|
|
Add Utility/Environment class for handling... environments
Summary:
There was some confusion in the code about how to represent process
environment. Most of the code (ab)used the Args class for this purpose,
but some of it used a more basic StringList class instead. In either
case, the fact that the underlying abstraction did not provide primitive
operations for the typical environment operations meant that even a
simple operation like checking for an environment variable value was
several lines of code.
This patch adds a separate Environment class, which is essentialy a
llvm::StringMap<std::string> in disguise. To standard StringMap
functionality, it adds a couple of new functions, which are specific to
the environment use case:
- (most important) envp conversion for passing into execve() and likes.
Instead of trying to maintain a constantly up-to-date envp view, it
provides a function which creates a envp view on demand, with the
expectation that this will be called as the very last thing before
handing the value to the system function.
- insert(StringRef KeyEqValue) - splits KeyEqValue into (key, value)
pair and inserts it into the environment map.
- compose(value_type KeyValue) - takes a map entry and converts in back
into "KEY=VALUE" representation.
With this interface most of the environment-manipulating code becomes
one-liners. The only tricky part was maintaining compatibility in
SBLaunchInfo, which expects that the environment entries are accessible
by index and that the returned const char* is backed by the launch info
object (random access into maps is hard and the map stores the entry in
a deconstructed form, so we cannot just return a .c_str() value). To
solve this, I have the SBLaunchInfo convert the environment into the
"envp" form, and use it to answer the environment queries. Extra code is
added to make sure the envp version is always in sync.
(This also improves the layering situation as Args was in the Interpreter module
whereas Environment is in Utility.)
Reviewers: zturner, davide, jingham, clayborg
Subscribers: emaste, lldb-commits, mgorny
Differential Revision: https://reviews.llvm.org/D41359
llvm-svn: 322174
2018-01-10 19:57:31 +08:00
|
|
|
Environment Host::GetEnvironment() { return Environment(environ); }
|
2017-04-08 00:40:25 +08:00
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
Status Host::ShellExpandArguments(ProcessLaunchInfo &launch_info) {
|
|
|
|
return Status("unimplemented");
|
2015-02-21 05:48:38 +08:00
|
|
|
}
|