LLDB  mainline
StreamFile.cpp
Go to the documentation of this file.
1 //===-- StreamFile.cpp ----------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/Core/StreamFile.h"
10 #include "lldb/Host/FileSystem.h"
11 #include "lldb/Utility/Log.h"
12 
13 #include <cstdio>
14 
15 using namespace lldb;
16 using namespace lldb_private;
17 
18 StreamFile::StreamFile(uint32_t flags, uint32_t addr_size, ByteOrder byte_order)
19  : Stream(flags, addr_size, byte_order) {
20  m_file_sp = std::make_shared<File>();
21 }
22 
23 StreamFile::StreamFile(int fd, bool transfer_ownership) : Stream() {
24  m_file_sp = std::make_shared<NativeFile>(fd, File::eOpenOptionWriteOnly,
25  transfer_ownership);
26 }
27 
28 StreamFile::StreamFile(FILE *fh, bool transfer_ownership) : Stream() {
29  m_file_sp = std::make_shared<NativeFile>(fh, transfer_ownership);
30 }
31 
32 StreamFile::StreamFile(const char *path, File::OpenOptions options,
33  uint32_t permissions)
34  : Stream() {
35  auto file = FileSystem::Instance().Open(FileSpec(path), options, permissions);
36  if (file)
37  m_file_sp = std::move(file.get());
38  else {
39  // TODO refactor this so the error gets popagated up instead of logged here.
41  "Cannot open {1}: {0}", path);
42  m_file_sp = std::make_shared<File>();
43  }
44 }
45 
46 StreamFile::~StreamFile() = default;
47 
48 void StreamFile::Flush() { m_file_sp->Flush(); }
49 
50 size_t StreamFile::WriteImpl(const void *s, size_t length) {
51  m_file_sp->Write(s, length);
52  return length;
53 }
lldb_private::FileSystem::Open
int Open(const char *path, int flags, int mode)
Wraps ::open in a platform-independent way.
Definition: FileSystemPosix.cpp:79
lldb_private::StreamFile::WriteImpl
size_t WriteImpl(const void *s, size_t length) override
Output character bytes to the stream.
Definition: StreamFile.cpp:50
FileSystem.h
lldb_private::File::OpenOptions
OpenOptions
Definition: File.h:49
lldb_private::Stream
Definition: Stream.h:28
LIBLLDB_LOG_HOST
#define LIBLLDB_LOG_HOST
Definition: Logging.h:28
lldb_private::FileSpec
Definition: FileSpec.h:56
Log.h
lldb_private::GetLogIfAllCategoriesSet
Log * GetLogIfAllCategoriesSet(uint32_t mask)
Definition: Logging.cpp:58
lldb_private::StreamFile::~StreamFile
~StreamFile() override
lldb_private::StreamFile::StreamFile
StreamFile(uint32_t flags, uint32_t addr_size, lldb::ByteOrder byte_order)
Definition: StreamFile.cpp:18
StreamFile.h
lldb_private::File::eOpenOptionWriteOnly
@ eOpenOptionWriteOnly
Definition: File.h:51
uint32_t
lldb_private::StreamFile::m_file_sp
std::shared_ptr< File > m_file_sp
Definition: StreamFile.h:47
lldb_private::FileSystem::Instance
static FileSystem & Instance()
Definition: common/FileSystem.cpp:45
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::StreamFile::Flush
void Flush() override
Flush the stream.
Definition: StreamFile.cpp:48
lldb
Definition: SBAddress.h:15
LLDB_LOG_ERROR
#define LLDB_LOG_ERROR(log, error,...)
Definition: Log.h:265
lldb::ByteOrder
ByteOrder
Byte ordering definitions.
Definition: lldb-enumerations.h:138