LLDB mainline
FileAction.cpp
Go to the documentation of this file.
1//===-- FileAction.cpp ----------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include <fcntl.h>
10
12#include "lldb/Host/PosixApi.h"
13#include "lldb/Utility/Stream.h"
14
15using namespace lldb_private;
16
17// FileAction member functions
18
20
22
23bool FileAction::Open(int fd, const FileSpec &file_spec, bool read,
24 bool write) {
25 if ((read || write) && fd >= 0 && file_spec) {
27 m_fd = fd;
28 if (read && write)
29 m_arg = O_NOCTTY | O_CREAT | O_RDWR;
30 else if (read)
31 m_arg = O_NOCTTY | O_RDONLY;
32 else
33 m_arg = O_NOCTTY | O_CREAT | O_WRONLY | O_TRUNC;
34 m_file_spec = file_spec;
35 return true;
36 }
37 *this = FileAction();
38 return false;
39}
40
41bool FileAction::Close(int fd) {
42 *this = FileAction();
43 if (fd >= 0) {
45 m_fd = fd;
46 }
47 return m_fd >= 0;
48}
49
50bool FileAction::Duplicate(int fd, int dup_fd) {
51 *this = FileAction();
52 if (fd >= 0 && dup_fd >= 0) {
54 m_fd = fd;
55 m_arg = dup_fd;
56 }
57 return m_fd >= 0;
58}
59
60void FileAction::Dump(Stream &stream) const {
61 stream.PutCString("file action: ");
62 switch (m_action) {
64 stream.Printf("close fd %d", m_fd);
65 break;
67 stream.Printf("duplicate fd %d to %d", m_fd, m_arg);
68 break;
69 case eFileActionNone:
70 stream.PutCString("no action");
71 break;
72 case eFileActionOpen:
73 stream.Printf("open fd %d with '%s', OFLAGS = 0x%x", m_fd,
74 m_file_spec.GetPath().c_str(), m_arg);
75 break;
76 }
77}
bool Duplicate(int fd, int dup_fd)
Configure this action to duplicate a file descriptor.
void Dump(Stream &stream) const
Action m_action
The action for this file.
Definition FileAction.h:74
int m_fd
The file descriptor this action applies to.
Definition FileAction.h:76
int m_arg
oflag for eFileActionOpen, dup_fd for eFileActionDuplicate.
Definition FileAction.h:78
const FileSpec & GetFileSpec() const
Get the file specification for open actions.
bool Open(int fd, const FileSpec &file_spec, bool read, bool write)
Configure this action to open a file.
bool Close(int fd)
Configure this action to close a file descriptor.
FileSpec m_file_spec
File spec to use for opening after fork or posix_spawn.
Definition FileAction.h:80
A file utility class.
Definition FileSpec.h:57
A stream class that can stream formatted output to a file.
Definition Stream.h:28
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:134
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition Stream.cpp:63
A class that represents a running process on the host machine.
#define O_NOCTTY