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
23 m_fd = -1;
24 m_arg = -1;
25 m_file_spec.Clear();
26}
27
29
30bool FileAction::Open(int fd, const FileSpec &file_spec, bool read,
31 bool write) {
32 if ((read || write) && fd >= 0 && file_spec) {
34 m_fd = fd;
35 if (read && write)
36 m_arg = O_NOCTTY | O_CREAT | O_RDWR;
37 else if (read)
38 m_arg = O_NOCTTY | O_RDONLY;
39 else
40 m_arg = O_NOCTTY | O_CREAT | O_WRONLY | O_TRUNC;
41 m_file_spec = file_spec;
42 return true;
43 } else {
44 Clear();
45 }
46 return false;
47}
48
49bool FileAction::Close(int fd) {
50 Clear();
51 if (fd >= 0) {
53 m_fd = fd;
54 }
55 return m_fd >= 0;
56}
57
58bool FileAction::Duplicate(int fd, int dup_fd) {
59 Clear();
60 if (fd >= 0 && dup_fd >= 0) {
62 m_fd = fd;
63 m_arg = dup_fd;
64 }
65 return m_fd >= 0;
66}
67
68void FileAction::Dump(Stream &stream) const {
69 stream.PutCString("file action: ");
70 switch (m_action) {
72 stream.Printf("close fd %d", m_fd);
73 break;
75 stream.Printf("duplicate fd %d to %d", m_fd, m_arg);
76 break;
77 case eFileActionNone:
78 stream.PutCString("no action");
79 break;
80 case eFileActionOpen:
81 stream.Printf("open fd %d with '%s', OFLAGS = 0x%x", m_fd,
82 m_file_spec.GetPath().c_str(), m_arg);
83 break;
84 }
85}
bool Duplicate(int fd, int dup_fd)
void Dump(Stream &stream) const
const FileSpec & GetFileSpec() const
bool Open(int fd, const FileSpec &file_spec, bool read, bool write)
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:65
A class that represents a running process on the host machine.
#define O_NOCTTY