llvm-project/lldb/source/Host/posix/HostProcessPosix.cpp
Adrian Prantl 0642cd768b
[lldb] Turn lldb_private::Status into a value type. (#106163)
This patch removes all of the Set.* methods from Status.

This cleanup is part of a series of patches that make it harder use the
anti-pattern of keeping a long-lives Status object around and updating
it while dropping any errors it contains on the floor.

This patch is largely NFC, the more interesting next steps this enables
is to:
1. remove Status.Clear()
2. assert that Status::operator=() never overwrites an error
3. remove Status::operator=()

Note that step (2) will bring 90% of the benefits for users, and step
(3) will dramatically clean up the error handling code in various
places. In the end my goal is to convert all APIs that are of the form

`    ResultTy DoFoo(Status& error)
`
to

`    llvm::Expected<ResultTy> DoFoo()
`
How to read this patch?

The interesting changes are in Status.h and Status.cpp, all other
changes are mostly

` perl -pi -e 's/\.SetErrorString/ = Status::FromErrorString/g' $(git
grep -l SetErrorString lldb/source)
`
plus the occasional manual cleanup.
2024-08-27 10:59:31 -07:00

66 lines
1.8 KiB
C++

//===-- HostProcessPosix.cpp ----------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "lldb/Host/Host.h"
#include "lldb/Host/FileSystem.h"
#include "lldb/Host/posix/HostProcessPosix.h"
#include "llvm/ADT/STLExtras.h"
#include <climits>
#include <csignal>
#include <unistd.h>
using namespace lldb_private;
static const int kInvalidPosixProcess = 0;
HostProcessPosix::HostProcessPosix()
: HostNativeProcessBase(kInvalidPosixProcess) {}
HostProcessPosix::HostProcessPosix(lldb::process_t process)
: HostNativeProcessBase(process) {}
HostProcessPosix::~HostProcessPosix() = default;
Status HostProcessPosix::Signal(int signo) const {
if (m_process == kInvalidPosixProcess) {
return Status::FromErrorString(
"HostProcessPosix refers to an invalid process");
}
return HostProcessPosix::Signal(m_process, signo);
}
Status HostProcessPosix::Signal(lldb::process_t process, int signo) {
Status error;
if (-1 == ::kill(process, signo))
return Status::FromErrno();
return error;
}
Status HostProcessPosix::Terminate() { return Signal(SIGKILL); }
lldb::pid_t HostProcessPosix::GetProcessId() const { return m_process; }
bool HostProcessPosix::IsRunning() const {
if (m_process == kInvalidPosixProcess)
return false;
// Send this process the null signal. If it succeeds the process is running.
Status error = Signal(0);
return error.Success();
}
llvm::Expected<HostThread> HostProcessPosix::StartMonitoring(
const Host::MonitorChildProcessCallback &callback) {
return Host::StartMonitoringChildProcess(callback, m_process);
}