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 
11 #include "lldb/Host/FileAction.h"
12 #include "lldb/Host/PosixApi.h"
13 #include "lldb/Utility/Stream.h"
14 
15 using namespace lldb_private;
16 
17 // FileAction member functions
18 
19 FileAction::FileAction() : m_file_spec() {}
20 
23  m_fd = -1;
24  m_arg = -1;
26 }
27 
28 llvm::StringRef FileAction::GetPath() const { return m_file_spec.GetCString(); }
29 
30 const FileSpec &FileAction::GetFileSpec() const { return m_file_spec; }
31 
32 bool FileAction::Open(int fd, const FileSpec &file_spec, bool read,
33  bool write) {
34  if ((read || write) && fd >= 0 && file_spec) {
36  m_fd = fd;
37  if (read && write)
38  m_arg = O_NOCTTY | O_CREAT | O_RDWR;
39  else if (read)
40  m_arg = O_NOCTTY | O_RDONLY;
41  else
42  m_arg = O_NOCTTY | O_CREAT | O_WRONLY;
43  m_file_spec = file_spec;
44  return true;
45  } else {
46  Clear();
47  }
48  return false;
49 }
50 
51 bool FileAction::Close(int fd) {
52  Clear();
53  if (fd >= 0) {
55  m_fd = fd;
56  }
57  return m_fd >= 0;
58 }
59 
60 bool FileAction::Duplicate(int fd, int dup_fd) {
61  Clear();
62  if (fd >= 0 && dup_fd >= 0) {
64  m_fd = fd;
65  m_arg = dup_fd;
66  }
67  return m_fd >= 0;
68 }
69 
70 void FileAction::Dump(Stream &stream) const {
71  stream.PutCString("file action: ");
72  switch (m_action) {
73  case eFileActionClose:
74  stream.Printf("close fd %d", m_fd);
75  break;
77  stream.Printf("duplicate fd %d to %d", m_fd, m_arg);
78  break;
79  case eFileActionNone:
80  stream.PutCString("no action");
81  break;
82  case eFileActionOpen:
83  stream.Printf("open fd %d with '%s', OFLAGS = 0x%x", m_fd,
85  break;
86  }
87 }
lldb_private::FileAction::Close
bool Close(int fd)
Definition: FileAction.cpp:51
lldb_private::FileAction::eFileActionDuplicate
@ eFileActionDuplicate
Definition: FileAction.h:22
lldb_private::Stream
Definition: Stream.h:28
lldb_private::FileAction::Duplicate
bool Duplicate(int fd, int dup_fd)
Definition: FileAction.cpp:60
lldb_private::FileSpec::GetCString
const char * GetCString(bool denormalize=true) const
Definition: FileSpec.cpp:364
lldb_private::FileAction::m_arg
int m_arg
Definition: FileAction.h:51
lldb_private::FileAction::Open
bool Open(int fd, const FileSpec &file_spec, bool read, bool write)
Definition: FileAction.cpp:32
lldb_private::FileAction::m_fd
int m_fd
Definition: FileAction.h:50
lldb_private::FileSpec
Definition: FileSpec.h:56
O_NOCTTY
#define O_NOCTTY
Definition: windows/PosixApi.h:28
lldb_private::FileAction::Dump
void Dump(Stream &stream) const
Definition: FileAction.cpp:70
lldb_private::FileAction::Clear
void Clear()
Definition: FileAction.cpp:21
lldb_private::FileAction::eFileActionClose
@ eFileActionClose
Definition: FileAction.h:21
lldb_private::FileAction::GetPath
llvm::StringRef GetPath() const
Definition: FileAction.cpp:28
lldb_private::FileAction::eFileActionOpen
@ eFileActionOpen
Definition: FileAction.h:23
lldb_private::FileSpec::Clear
void Clear()
Clears the object state.
Definition: FileSpec.cpp:261
lldb_private::FileAction::eFileActionNone
@ eFileActionNone
Definition: FileAction.h:20
lldb_private::FileAction::m_action
Action m_action
Definition: FileAction.h:49
lldb_private::FileAction::GetFileSpec
const FileSpec & GetFileSpec() const
Definition: FileAction.cpp:30
lldb_private::Stream::Printf
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::FileAction::m_file_spec
FileSpec m_file_spec
Definition: FileAction.h:53
PosixApi.h
lldb_private::FileAction::FileAction
FileAction()
Definition: FileAction.cpp:19
Stream.h
lldb_private::Stream::PutCString
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:63
FileAction.h