Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 1 | //===-- 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 Fiala | 348fb38 | 2014-10-10 00:09:16 +0000 | [diff] [blame^] | 16 | #include "lldb/Core/Stream.h" |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 17 | #include "lldb/Target/FileAction.h" |
| 18 | |
| 19 | using namespace lldb_private; |
| 20 | |
| 21 | //---------------------------------------------------------------------------- |
| 22 | // FileAction member functions |
| 23 | //---------------------------------------------------------------------------- |
| 24 | |
Jason Molenda | e6481c7 | 2014-09-12 01:50:46 +0000 | [diff] [blame] | 25 | FileAction::FileAction() : |
| 26 | m_action(eFileActionNone), |
| 27 | m_fd(-1), |
| 28 | m_arg(-1), |
| 29 | m_path() |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 30 | { |
| 31 | } |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 32 | |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 33 | void |
| 34 | FileAction::Clear() |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 35 | { |
| 36 | m_action = eFileActionNone; |
| 37 | m_fd = -1; |
| 38 | m_arg = -1; |
| 39 | m_path.clear(); |
| 40 | } |
| 41 | |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 42 | const char * |
| 43 | FileAction::GetPath() const |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 44 | { |
| 45 | if (m_path.empty()) |
| 46 | return NULL; |
| 47 | return m_path.c_str(); |
| 48 | } |
| 49 | |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 50 | bool |
| 51 | FileAction::Open(int fd, const char *path, bool read, bool write) |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 52 | { |
| 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 Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 73 | bool |
| 74 | FileAction::Close(int fd) |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 75 | { |
| 76 | Clear(); |
| 77 | if (fd >= 0) |
| 78 | { |
| 79 | m_action = eFileActionClose; |
| 80 | m_fd = fd; |
| 81 | } |
| 82 | return m_fd >= 0; |
| 83 | } |
| 84 | |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 85 | bool |
| 86 | FileAction::Duplicate(int fd, int dup_fd) |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 87 | { |
| 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 Fiala | 348fb38 | 2014-10-10 00:09:16 +0000 | [diff] [blame^] | 97 | |
| 98 | void |
| 99 | FileAction::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 | } |