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