1 /**
2 * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
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
16 #include "source_manager.h"
17 #include "types/numeric_id.h"
18
19 #include <string>
20 #include <string_view>
21
22 namespace ark::tooling::inspector {
GetScriptId(PtThread thread,std::string_view fileName)23 std::pair<ScriptId, bool> SourceManager::GetScriptId(PtThread thread, std::string_view fileName)
24 {
25 os::memory::LockHolder lock(mutex_);
26
27 auto p = fileNameToId_.emplace(std::string(fileName), fileNameToId_.size());
28 ScriptId id(p.first->second);
29
30 bool isNewForThread = knownSources_[thread].insert(id).second;
31
32 if (p.second) {
33 std::string_view name {p.first->first};
34 idToFileName_.emplace(id, name);
35 }
36
37 return {id, isNewForThread};
38 }
39
GetSourceFileName(ScriptId id) const40 std::string_view SourceManager::GetSourceFileName(ScriptId id) const
41 {
42 os::memory::LockHolder lock(mutex_);
43
44 auto it = idToFileName_.find(id);
45 if (it != idToFileName_.end()) {
46 return it->second;
47 }
48
49 LOG(ERROR, DEBUGGER) << "No file with script id " << id;
50
51 return {};
52 }
53
RemoveThread(PtThread thread)54 void SourceManager::RemoveThread(PtThread thread)
55 {
56 os::memory::LockHolder lock(mutex_);
57 knownSources_.erase(thread);
58 }
59 } // namespace ark::tooling::inspector
60