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 <mutex>
18 #include <unordered_set>
19
20 namespace {
21
22 ////////////////////////////////////////////////////////////////////////////////
23 // FileBase
24 ////////////////////////////////////////////////////////////////////////////////
25 class FileBase : public vk::dbg::File
26 {
27 public:
28 void clearBreakpoints() override;
29 void addBreakpoint(int line) override;
30 bool hasBreakpoint(int line) const override;
31
32 protected:
33 FileBase(ID id, std::string dir, std::string name, std::string source);
34
35 private:
36 mutable std::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 std::unique_lock<std::mutex> lock(breakpointMutex);
47 breakpoints.clear();
48 }
49
addBreakpoint(int line)50 void FileBase::addBreakpoint(int line)
51 {
52 std::unique_lock<std::mutex> lock(breakpointMutex);
53 breakpoints.emplace(line);
54 }
55
hasBreakpoint(int line) const56 bool FileBase::hasBreakpoint(int line) const
57 {
58 std::unique_lock<std::mutex> lock(breakpointMutex);
59 return breakpoints.count(line) > 0;
60 }
61
62 ////////////////////////////////////////////////////////////////////////////////
63 // VirtualFile
64 ////////////////////////////////////////////////////////////////////////////////
65 class VirtualFile : public FileBase
66 {
67 public:
68 VirtualFile(ID id, std::string name, std::string source);
69
70 bool isVirtual() const override;
71
72 private:
73 };
74
VirtualFile(ID id,std::string name,std::string source)75 VirtualFile::VirtualFile(ID id, std::string name, std::string source)
76 : FileBase(id, "", std::move(name), std::move(source))
77 {}
78
isVirtual() const79 bool VirtualFile::isVirtual() const
80 {
81 return true;
82 }
83
84 ////////////////////////////////////////////////////////////////////////////////
85 // PhysicalFile
86 ////////////////////////////////////////////////////////////////////////////////
87 struct PhysicalFile : public FileBase
88 {
89 PhysicalFile(ID id,
90 std::string dir,
91 std::string name);
92
93 bool isVirtual() const override;
94 };
95
PhysicalFile(ID id,std::string dir,std::string name)96 PhysicalFile::PhysicalFile(ID id,
97 std::string dir,
98 std::string name)
99 : FileBase(id, std::move(dir), std::move(name), "")
100 {}
101
isVirtual() const102 bool PhysicalFile::isVirtual() const
103 {
104 return false;
105 }
106
107 } // anonymous namespace
108
109 namespace vk {
110 namespace dbg {
111
createVirtual(ID id,std::string name,std::string source)112 std::shared_ptr<File> File::createVirtual(ID id, std::string name, std::string source)
113 {
114 return std::make_shared<VirtualFile>(id, std::move(name), std::move(source));
115 }
116
createPhysical(ID id,std::string path)117 std::shared_ptr<File> File::createPhysical(ID id, std::string path)
118 {
119 auto pathstr = path;
120 auto pos = pathstr.rfind("/");
121 if(pos != std::string::npos)
122 {
123 auto dir = pathstr.substr(0, pos);
124 auto name = pathstr.substr(pos + 1);
125 return std::make_shared<PhysicalFile>(id, dir.c_str(), name.c_str());
126 }
127 return std::make_shared<PhysicalFile>(id, "", std::move(path));
128 }
129
130 } // namespace dbg
131 } // namespace vk