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), |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 29 | m_file_spec() |
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; |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 39 | m_file_spec.Clear(); |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 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 | { |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 45 | return m_file_spec.GetCString(); |
| 46 | } |
| 47 | |
| 48 | const FileSpec & |
| 49 | FileAction::GetFileSpec() const |
| 50 | { |
| 51 | return m_file_spec; |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 54 | bool |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 55 | FileAction::Open(int fd, const FileSpec &file_spec, bool read, bool write) |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 56 | { |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 57 | if ((read || write) && fd >= 0 && file_spec) |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 58 | { |
| 59 | m_action = eFileActionOpen; |
| 60 | m_fd = fd; |
| 61 | if (read && write) |
| 62 | m_arg = O_NOCTTY | O_CREAT | O_RDWR; |
| 63 | else if (read) |
| 64 | m_arg = O_NOCTTY | O_RDONLY; |
| 65 | else |
| 66 | m_arg = O_NOCTTY | O_CREAT | O_WRONLY; |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 67 | m_file_spec = file_spec; |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 68 | return true; |
| 69 | } |
| 70 | else |
| 71 | { |
| 72 | Clear(); |
| 73 | } |
| 74 | return false; |
| 75 | } |
| 76 | |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 77 | bool |
| 78 | FileAction::Close(int fd) |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 79 | { |
| 80 | Clear(); |
| 81 | if (fd >= 0) |
| 82 | { |
| 83 | m_action = eFileActionClose; |
| 84 | m_fd = fd; |
| 85 | } |
| 86 | return m_fd >= 0; |
| 87 | } |
| 88 | |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 89 | bool |
| 90 | FileAction::Duplicate(int fd, int dup_fd) |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 91 | { |
| 92 | Clear(); |
| 93 | if (fd >= 0 && dup_fd >= 0) |
| 94 | { |
| 95 | m_action = eFileActionDuplicate; |
| 96 | m_fd = fd; |
| 97 | m_arg = dup_fd; |
| 98 | } |
| 99 | return m_fd >= 0; |
| 100 | } |
Todd Fiala | 348fb38 | 2014-10-10 00:09:16 +0000 | [diff] [blame] | 101 | |
| 102 | void |
| 103 | FileAction::Dump(Stream &stream) const |
| 104 | { |
| 105 | stream.PutCString("file action: "); |
| 106 | switch (m_action) |
| 107 | { |
| 108 | case eFileActionClose: |
| 109 | stream.Printf("close fd %d", m_fd); |
| 110 | break; |
| 111 | case eFileActionDuplicate: |
| 112 | stream.Printf("duplicate fd %d to %d", m_fd, m_arg); |
| 113 | break; |
| 114 | case eFileActionNone: |
| 115 | stream.PutCString("no action"); |
| 116 | break; |
| 117 | case eFileActionOpen: |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 118 | stream.Printf("open fd %d with '%s', OFLAGS = 0x%x", |
| 119 | m_fd, m_file_spec.GetCString(), m_arg); |
Todd Fiala | 348fb38 | 2014-10-10 00:09:16 +0000 | [diff] [blame] | 120 | break; |
| 121 | } |
| 122 | } |