blob: 8611ff5f2d2cc7f55c2c479a4895a4f8708087fb [file] [log] [blame]
Zachary Turner696b5282014-08-14 16:01:25 +00001//===-- FileAction.cpp ------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include <fcntl.h>
11
12#if defined(_WIN32)
13#include "lldb/Host/Windows/win32.h" // For O_NOCTTY
14#endif
15
Todd Fiala348fb382014-10-10 00:09:16 +000016#include "lldb/Core/Stream.h"
Zachary Turner696b5282014-08-14 16:01:25 +000017#include "lldb/Target/FileAction.h"
18
19using namespace lldb_private;
20
21//----------------------------------------------------------------------------
22// FileAction member functions
23//----------------------------------------------------------------------------
24
Jason Molendae6481c72014-09-12 01:50:46 +000025FileAction::FileAction() :
26 m_action(eFileActionNone),
27 m_fd(-1),
28 m_arg(-1),
29 m_path()
Zachary Turnerc00cf4a2014-08-15 22:04:21 +000030{
31}
Zachary Turner696b5282014-08-14 16:01:25 +000032
Zachary Turnerc00cf4a2014-08-15 22:04:21 +000033void
34FileAction::Clear()
Zachary Turner696b5282014-08-14 16:01:25 +000035{
36 m_action = eFileActionNone;
37 m_fd = -1;
38 m_arg = -1;
39 m_path.clear();
40}
41
Zachary Turnerc00cf4a2014-08-15 22:04:21 +000042const char *
43FileAction::GetPath() const
Zachary Turner696b5282014-08-14 16:01:25 +000044{
45 if (m_path.empty())
46 return NULL;
47 return m_path.c_str();
48}
49
Zachary Turnerc00cf4a2014-08-15 22:04:21 +000050bool
51FileAction::Open(int fd, const char *path, bool read, bool write)
Zachary Turner696b5282014-08-14 16:01:25 +000052{
53 if ((read || write) && fd >= 0 && path && path[0])
54 {
55 m_action = eFileActionOpen;
56 m_fd = fd;
57 if (read && write)
58 m_arg = O_NOCTTY | O_CREAT | O_RDWR;
59 else if (read)
60 m_arg = O_NOCTTY | O_RDONLY;
61 else
62 m_arg = O_NOCTTY | O_CREAT | O_WRONLY;
63 m_path.assign(path);
64 return true;
65 }
66 else
67 {
68 Clear();
69 }
70 return false;
71}
72
Zachary Turnerc00cf4a2014-08-15 22:04:21 +000073bool
74FileAction::Close(int fd)
Zachary Turner696b5282014-08-14 16:01:25 +000075{
76 Clear();
77 if (fd >= 0)
78 {
79 m_action = eFileActionClose;
80 m_fd = fd;
81 }
82 return m_fd >= 0;
83}
84
Zachary Turnerc00cf4a2014-08-15 22:04:21 +000085bool
86FileAction::Duplicate(int fd, int dup_fd)
Zachary Turner696b5282014-08-14 16:01:25 +000087{
88 Clear();
89 if (fd >= 0 && dup_fd >= 0)
90 {
91 m_action = eFileActionDuplicate;
92 m_fd = fd;
93 m_arg = dup_fd;
94 }
95 return m_fd >= 0;
96}
Todd Fiala348fb382014-10-10 00:09:16 +000097
98void
99FileAction::Dump(Stream &stream) const
100{
101 stream.PutCString("file action: ");
102 switch (m_action)
103 {
104 case eFileActionClose:
105 stream.Printf("close fd %d", m_fd);
106 break;
107 case eFileActionDuplicate:
108 stream.Printf("duplicate fd %d to %d", m_fd, m_arg);
109 break;
110 case eFileActionNone:
111 stream.PutCString("no action");
112 break;
113 case eFileActionOpen:
114 stream.Printf("open fd %d with '%s', OFLAGS = 0x%x", m_fd, m_path.c_str(), m_arg);
115 break;
116 }
117}