summaryrefslogtreecommitdiffstats
path: root/contrib/llvm/tools/lldb/source/Host/common/File.cpp
diff options
context:
space:
mode:
authoremaste <emaste@FreeBSD.org>2014-07-23 19:35:02 +0000
committeremaste <emaste@FreeBSD.org>2014-07-23 19:35:02 +0000
commitaa794b38fedea0f2e99519975acab0b289714b41 (patch)
treed542e0aa192601387eab969343acfada413521e6 /contrib/llvm/tools/lldb/source/Host/common/File.cpp
parent35d9abcb8d9ec0494bd89b56ca40aa22b6206e54 (diff)
downloadFreeBSD-src-aa794b38fedea0f2e99519975acab0b289714b41.zip
FreeBSD-src-aa794b38fedea0f2e99519975acab0b289714b41.tar.gz
MFC r262528: Update LLDB snapshot to upstream r202189
Highlights include (upstream revs in parens): - Improvements to the remote GDB protocol client (r196610, r197579, r197857, r200072, and others) - Bug fixes for big-endian targets (r196808) - Initial support for libdispatch (GCD) queues in the debuggee (r197190) - Add "step-avoid-libraries" setting (r199943) - IO subsystem improvements (including initial work on a curses gui) (r200263) - Support hardware watchpoints on FreeBSD (r201706) - Improved unwinding through hand-written assembly functions (r201839) - Handle DW_TAG_unspecified_parameters for variadic functions (r202061) - Fix Ctrl+C interrupting a running inferior process (r202086, r202154) - Various bug fixes for memory leaks, LLDB segfaults, the C++ demangler, ELF core files, DWARF debug info, and others. Sponsored by: DARPA, AFRL
Diffstat (limited to 'contrib/llvm/tools/lldb/source/Host/common/File.cpp')
-rw-r--r--contrib/llvm/tools/lldb/source/Host/common/File.cpp128
1 files changed, 101 insertions, 27 deletions
diff --git a/contrib/llvm/tools/lldb/source/Host/common/File.cpp b/contrib/llvm/tools/lldb/source/Host/common/File.cpp
index bbd1185..bb0ee39 100644
--- a/contrib/llvm/tools/lldb/source/Host/common/File.cpp
+++ b/contrib/llvm/tools/lldb/source/Host/common/File.cpp
@@ -13,10 +13,13 @@
#include <fcntl.h>
#include <limits.h>
#include <stdarg.h>
+#include <stdio.h>
#include <sys/stat.h>
#ifdef _WIN32
#include "lldb/Host/windows/windows.h"
+#else
+#include <sys/ioctl.h>
#endif
#include "lldb/Core/DataBufferHeap.h"
@@ -76,8 +79,11 @@ FILE * File::kInvalidStream = NULL;
File::File(const char *path, uint32_t options, uint32_t permissions) :
m_descriptor (kInvalidDescriptor),
m_stream (kInvalidStream),
- m_options (0),
- m_owned (false)
+ m_options (),
+ m_own_stream (false),
+ m_own_descriptor (false),
+ m_is_interactive (eLazyBoolCalculate),
+ m_is_real_terminal (eLazyBoolCalculate)
{
Open (path, options, permissions);
}
@@ -88,7 +94,11 @@ File::File (const FileSpec& filespec,
m_descriptor (kInvalidDescriptor),
m_stream (kInvalidStream),
m_options (0),
- m_owned (false)
+ m_own_stream (false),
+ m_own_descriptor (false),
+ m_is_interactive (eLazyBoolCalculate),
+ m_is_real_terminal (eLazyBoolCalculate)
+
{
if (filespec)
{
@@ -100,7 +110,10 @@ File::File (const File &rhs) :
m_descriptor (kInvalidDescriptor),
m_stream (kInvalidStream),
m_options (0),
- m_owned (false)
+ m_own_stream (false),
+ m_own_descriptor (false),
+ m_is_interactive (eLazyBoolCalculate),
+ m_is_real_terminal (eLazyBoolCalculate)
{
Duplicate (rhs);
}
@@ -141,7 +154,7 @@ File::SetDescriptor (int fd, bool transfer_ownership)
if (IsValid())
Close();
m_descriptor = fd;
- m_owned = transfer_ownership;
+ m_own_descriptor = transfer_ownership;
}
@@ -155,10 +168,31 @@ File::GetStream ()
const char *mode = GetStreamOpenModeFromOptions (m_options);
if (mode)
{
+ if (!m_own_descriptor)
+ {
+ // We must duplicate the file descriptor if we don't own it because
+ // when you call fdopen, the stream will own the fd
+#ifdef _WIN32
+ m_descriptor = ::_dup(GetDescriptor());
+#else
+ m_descriptor = ::fcntl(GetDescriptor(), F_DUPFD);
+#endif
+ m_own_descriptor = true;
+ }
+
do
{
m_stream = ::fdopen (m_descriptor, mode);
} while (m_stream == NULL && errno == EINTR);
+
+ // If we got a stream, then we own the stream and should no
+ // longer own the descriptor because fclose() will close it for us
+
+ if (m_stream)
+ {
+ m_own_stream = true;
+ m_own_descriptor = false;
+ }
}
}
}
@@ -172,7 +206,7 @@ File::SetStream (FILE *fh, bool transfer_ownership)
if (IsValid())
Close();
m_stream = fh;
- m_owned = transfer_ownership;
+ m_own_stream = transfer_ownership;
}
Error
@@ -194,7 +228,7 @@ File::Duplicate (const File &rhs)
else
{
m_options = rhs.m_options;
- m_owned = true;
+ m_own_descriptor = true;
}
}
else
@@ -272,7 +306,10 @@ File::Open (const char *path, uint32_t options, uint32_t permissions)
if (!DescriptorIsValid())
error.SetErrorToErrno();
else
- m_owned = true;
+ {
+ m_own_descriptor = true;
+ m_options = options;
+ }
return error;
}
@@ -328,27 +365,24 @@ Error
File::Close ()
{
Error error;
- if (IsValid ())
+ if (StreamIsValid() && m_own_stream)
{
- if (m_owned)
- {
- if (StreamIsValid())
- {
- if (::fclose (m_stream) == EOF)
- error.SetErrorToErrno();
- }
-
- if (DescriptorIsValid())
- {
- if (::close (m_descriptor) != 0)
- error.SetErrorToErrno();
- }
- }
- m_descriptor = kInvalidDescriptor;
- m_stream = kInvalidStream;
- m_options = 0;
- m_owned = false;
+ if (::fclose (m_stream) == EOF)
+ error.SetErrorToErrno();
}
+
+ if (DescriptorIsValid() && m_own_descriptor)
+ {
+ if (::close (m_descriptor) != 0)
+ error.SetErrorToErrno();
+ }
+ m_descriptor = kInvalidDescriptor;
+ m_stream = kInvalidStream;
+ m_options = 0;
+ m_own_stream = false;
+ m_own_descriptor = false;
+ m_is_interactive = eLazyBoolCalculate;
+ m_is_real_terminal = eLazyBoolCalculate;
return error;
}
@@ -844,3 +878,43 @@ File::ConvertOpenOptionsForPOSIXOpen (uint32_t open_options)
return mode;
}
+
+void
+File::CalculateInteractiveAndTerminal ()
+{
+ const int fd = GetDescriptor();
+ if (fd >= 0)
+ {
+ m_is_interactive = eLazyBoolNo;
+ m_is_real_terminal = eLazyBoolNo;
+#ifndef _MSC_VER
+ if (isatty(fd))
+ {
+ m_is_interactive = eLazyBoolYes;
+ struct winsize window_size;
+ if (::ioctl (fd, TIOCGWINSZ, &window_size) == 0)
+ {
+ if (window_size.ws_col > 0)
+ m_is_real_terminal = eLazyBoolYes;
+ }
+ }
+#endif
+ }
+}
+
+bool
+File::GetIsInteractive ()
+{
+ if (m_is_interactive == eLazyBoolCalculate)
+ CalculateInteractiveAndTerminal ();
+ return m_is_interactive == eLazyBoolYes;
+}
+
+bool
+File::GetIsRealTerminal ()
+{
+ if (m_is_real_terminal == eLazyBoolCalculate)
+ CalculateInteractiveAndTerminal();
+ return m_is_real_terminal == eLazyBoolYes;
+}
+
OpenPOWER on IntegriCloud