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 {
30 }
31 
32 const FileSpec &FileAction::GetFileSpec() const { return m_file_spec; }
33 
34 bool FileAction::Open(int fd, const FileSpec &file_spec, bool read,
35  bool write) {
36  if ((read || write) && fd >= 0 && file_spec) {
38  m_fd = fd;
39  if (read && write)
40  m_arg = O_NOCTTY | O_CREAT | O_RDWR;
41  else if (read)
42  m_arg = O_NOCTTY | O_RDONLY;
43  else
44  m_arg = O_NOCTTY | O_CREAT | O_WRONLY;
45  m_file_spec = file_spec;
46  return true;
47  } else {
48  Clear();
49  }
50  return false;
51 }
52 
53 bool FileAction::Close(int fd) {
54  Clear();
55  if (fd >= 0) {
57  m_fd = fd;
58  }
59  return m_fd >= 0;
60 }
61 
62 bool FileAction::Duplicate(int fd, int dup_fd) {
63  Clear();
64  if (fd >= 0 && dup_fd >= 0) {
66  m_fd = fd;
67  m_arg = dup_fd;
68  }
69  return m_fd >= 0;
70 }
71 
72 void FileAction::Dump(Stream &stream) const {
73  stream.PutCString("file action: ");
74  switch (m_action) {
75  case eFileActionClose:
76  stream.Printf("close fd %d", m_fd);
77  break;
79  stream.Printf("duplicate fd %d to %d", m_fd, m_arg);
80  break;
81  case eFileActionNone:
82  stream.PutCString("no action");
83  break;
84  case eFileActionOpen:
85  stream.Printf("open fd %d with '%s', OFLAGS = 0x%x", m_fd,
86  m_file_spec.GetPath().c_str(), m_arg);
87  break;
88  }
89 }
lldb_private::FileAction::Close
bool Close(int fd)
Definition: FileAction.cpp:53
lldb_private::FileAction::eFileActionDuplicate
@ eFileActionDuplicate
Definition: FileAction.h:22
lldb_private::ConstString::AsCString
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
Definition: ConstString.h:192
lldb_private::Stream
Definition: Stream.h:28
lldb_private::FileAction::Duplicate
bool Duplicate(int fd, int dup_fd)
Definition: FileAction.cpp:62
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:34
lldb_private::FileAction::m_fd
int m_fd
Definition: FileAction.h:50
lldb_private::FileSpec
Definition: FileSpec.h:55
O_NOCTTY
#define O_NOCTTY
Definition: windows/PosixApi.h:28
lldb_private::FileAction::Dump
void Dump(Stream &stream) const
Definition: FileAction.cpp:72
lldb_private::FileSpec::GetPathAsConstString
ConstString GetPathAsConstString(bool denormalize=true) const
Get the full path as a ConstString.
Definition: FileSpec.cpp:380
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:257
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:32
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
lldb_private::FileSpec::GetPath
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition: FileSpec.cpp:364