DebugServer2
Loading...
Searching...
No Matches
FileOperationsMixin.hpp
1//
2// Copyright (c) 2014-present, Facebook, Inc.
3// All rights reserved.
4//
5// This source code is licensed under the University of Illinois/NCSA Open
6// Source License found in the LICENSE file in the root directory of this
7// source tree. An additional grant of patent rights can be found in the
8// PATENTS file in the same directory.
9//
10
11#pragma once
12
13#include "DebugServer2/GDBRemote/Mixins/FileOperationsMixin.h"
14#include "DebugServer2/Host/Platform.h"
15
16#include <iomanip>
17#include <sstream>
18
19using ds2::Host::File;
21
22namespace ds2 {
23namespace GDBRemote {
24
25template <typename T>
26ErrorCode FileOperationsMixin<T>::onFileOpen(Session &, std::string const &path,
27 OpenFlags flags, uint32_t mode,
28 int &fd) {
29 static int fileIdx = 0;
30
31 Host::File file(path, flags, mode);
32 if (!file.valid()) {
33 return file.lastError();
34 }
35
36 fd = fileIdx++;
37 _openFiles.emplace(fd, std::move(file));
38
39 return kSuccess;
40}
41
42template <typename T>
43ErrorCode FileOperationsMixin<T>::onFileClose(Session &session, int fd) {
44 auto const it = _openFiles.find(fd);
45 if (it == _openFiles.end()) {
46 return kErrorInvalidHandle;
47 }
48
49 _openFiles.erase(it);
50 return kSuccess;
51}
52
53template <typename T>
54ErrorCode FileOperationsMixin<T>::onFileRead(Session &session, int fd,
55 uint64_t &count, uint64_t offset,
56 ByteVector &buffer) {
57 auto it = _openFiles.find(fd);
58 if (it == _openFiles.end()) {
59 return kErrorInvalidHandle;
60 }
61
62 return it->second.pread(buffer, count, offset);
63}
64
65template <typename T>
66ErrorCode FileOperationsMixin<T>::onFileWrite(Session &session, int fd,
67 uint64_t offset,
68 ByteVector const &buffer,
69 uint64_t &nwritten) {
70 auto it = _openFiles.find(fd);
71 if (it == _openFiles.end()) {
72 return kErrorInvalidHandle;
73 }
74
75 return it->second.pwrite(buffer, nwritten, offset);
76}
77
78template <typename T>
79ErrorCode FileOperationsMixin<T>::onFileCreateDirectory(Session &,
80 std::string const &path,
81 uint32_t flags) {
82 return Host::File::createDirectory(path, flags);
83}
84
85template <typename T>
86ErrorCode FileOperationsMixin<T>::onFileExists(Session &,
87 std::string const &path) {
88 return Host::Platform::IsFilePresent(path) ? kSuccess : kErrorNotFound;
89}
90
91template <typename T>
92ErrorCode FileOperationsMixin<T>::onFileGetSize(Session &session, std::string const &path,
93 uint64_t &size){
94 return Host::File::fileSize(path, size);
95}
96
97template <typename T>
98ErrorCode FileOperationsMixin<T>::onFileGetMode(Session &session,
99 std::string const &path,
100 uint32_t &mode) const {
101 return Host::File::fileMode(path, mode);
102}
103
104template <typename T>
105ErrorCode FileOperationsMixin<T>::onFileFstat(Session &session, int fd,
106 ByteVector &buffer) const {
107 auto it = _openFiles.find(fd);
108 if (it == _openFiles.end())
109 return kErrorInvalidHandle;
110
111 return it->second.fstat(buffer);
112}
113
114template <typename T>
115ErrorCode FileOperationsMixin<T>::onFileRemove(Session &session,
116 std::string const &path) {
117 return Host::File::unlink(path);
118}
119
120template <typename T>
121ErrorCode FileOperationsMixin<T>::onFileSetPermissions(Session &session,
122 std::string const &path,
123 uint32_t mode) {
124 return Host::File::chmod(path, mode);
125}
126
127template <typename T>
128ErrorCode FileOperationsMixin<T>::onQueryModuleInfo(Session &session,
129 std::string &path,
130 std::string &triple,
131 ModuleInfo &info) const {
132 ByteVector buildId;
133 if (Platform::GetExecutableFileBuildID(path, buildId)) {
134 // format the uuid as an upper-case string with two hex chars per byte
135 std::ostringstream ss;
136 for(const auto b : buildId)
137 ss << std::uppercase << std::hex << std::setfill('0') << std::setw(2) << int(b);
138
139 info.uuid = ss.str();
140 }
141
142 // TODO(andrurogerz): Not all executable files contain an embedded build ID.
143 // If GetExecutableFileBuildID fails, calculate an md5 hash of the file
144 // contents and return that as an "md5" field instead of the "uuid" field.
145
146 auto error = File::fileSize(path, info.file_size);
147 if (error != kSuccess)
148 return error;
149
150 info.triple = triple;
151 info.file_path = path;
152 info.file_offset = 0;
153
154 return kSuccess;
155}
156} // namespace GDBRemote
157} // namespace ds2
Definition File.h:24
Definition Platform.h:24