1 // Copyright 2019 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "File.hpp"
16
17 #include "marl/mutex.h"
18
19 namespace {
20
21 ////////////////////////////////////////////////////////////////////////////////
22 // FileBase
23 ////////////////////////////////////////////////////////////////////////////////
24 class FileBase : public vk::dbg::File
25 {
26 public:
27 void clearBreakpoints() override;
28 void addBreakpoint(int line) override;
29 bool hasBreakpoint(int line) const override;
30 std::unordered_set<int> getBreakpoints() const override;
31
32 protected:
33 FileBase(ID id, std::string dir, std::string name, std::string source);
34
35 private:
36 mutable marl::mutex breakpointMutex;
37 std::unordered_set<int> breakpoints GUARDED_BY(breakpointMutex);
38 };
39
FileBase(ID id,std::string dir,std::string name,std::string source)40 FileBase::FileBase(ID id, std::string dir, std::string name, std::string source)
41 : File(id, std::move(dir), std::move(name), std::move(source))
42 {}
43
clearBreakpoints()44 void FileBase::clearBreakpoints()
45 {
46 marl::lock lock(breakpointMutex);
47 breakpoints.clear();
48 }
49
addBreakpoint(int line)50 void FileBase::addBreakpoint(int line)
51 {
52 marl::lock lock(breakpointMutex);
53 breakpoints.emplace(line);
54 }
55
hasBreakpoint(int line) const56 bool FileBase::hasBreakpoint(int line) const
57 {
58 marl::lock lock(breakpointMutex);
59 return breakpoints.count(line) > 0;
60 }
61
getBreakpoints() const62 std::unordered_set<int> FileBase::getBreakpoints() const
63 {
64 marl::lock lock(breakpointMutex);
65 return breakpoints;
66 }
67
68 ////////////////////////////////////////////////////////////////////////////////
69 // VirtualFile
70 ////////////////////////////////////////////////////////////////////////////////
71 class VirtualFile : public FileBase
72 {
73 public:
74 VirtualFile(ID id, std::string name, std::string source);
75
76 bool isVirtual() const override;
77
78 private:
79 };
80
VirtualFile(ID id,std::string name,std::string source)81 VirtualFile::VirtualFile(ID id, std::string name, std::string source)
82 : FileBase(id, "", std::move(name), std::move(source))
83 {}
84
isVirtual() const85 bool VirtualFile::isVirtual() const
86 {
87 return true;
88 }
89
90 ////////////////////////////////////////////////////////////////////////////////
91 // PhysicalFile
92 ////////////////////////////////////////////////////////////////////////////////
93 struct PhysicalFile : public FileBase
94 {
95 PhysicalFile(ID id,
96 std::string dir,
97 std::string name);
98
99 bool isVirtual() const override;
100 };
101
PhysicalFile(ID id,std::string dir,std::string name)102 PhysicalFile::PhysicalFile(ID id,
103 std::string dir,
104 std::string name)
105 : FileBase(id, std::move(dir), std::move(name), "")
106 {}
107
isVirtual() const108 bool PhysicalFile::isVirtual() const
109 {
110 return false;
111 }
112
113 } // anonymous namespace
114
115 namespace vk {
116 namespace dbg {
117
createVirtual(ID id,std::string name,std::string source)118 std::shared_ptr<File> File::createVirtual(ID id, std::string name, std::string source)
119 {
120 return std::make_shared<VirtualFile>(id, std::move(name), std::move(source));
121 }
122
createPhysical(ID id,std::string path)123 std::shared_ptr<File> File::createPhysical(ID id, std::string path)
124 {
125 auto pathstr = path;
126 auto pos = pathstr.rfind("/");
127 if(pos != std::string::npos)
128 {
129 auto dir = pathstr.substr(0, pos);
130 auto name = pathstr.substr(pos + 1);
131 return std::make_shared<PhysicalFile>(id, dir.c_str(), name.c_str());
132 }
133 return std::make_shared<PhysicalFile>(id, "", std::move(path));
134 }
135
136 } // namespace dbg
137 } // namespace vk
138