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 16 #include "lldb/Core/Stream.h" 17 #include "lldb/Target/FileAction.h" 18 19 using namespace lldb_private; 20 21 //---------------------------------------------------------------------------- 22 // FileAction member functions 23 //---------------------------------------------------------------------------- 24 25 FileAction::FileAction() : 26 m_action(eFileActionNone), 27 m_fd(-1), 28 m_arg(-1), 29 m_path() 30 { 31 } 32 33 void 34 FileAction::Clear() 35 { 36 m_action = eFileActionNone; 37 m_fd = -1; 38 m_arg = -1; 39 m_path.clear(); 40 } 41 42 const char * 43 FileAction::GetPath() const 44 { 45 if (m_path.empty()) 46 return NULL; 47 return m_path.c_str(); 48 } 49 50 bool 51 FileAction::Open(int fd, const char *path, bool read, bool write) 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 73 bool 74 FileAction::Close(int fd) 75 { 76 Clear(); 77 if (fd >= 0) 78 { 79 m_action = eFileActionClose; 80 m_fd = fd; 81 } 82 return m_fd >= 0; 83 } 84 85 bool 86 FileAction::Duplicate(int fd, int dup_fd) 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 } 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 } 118