1*80814287SRaphael Isemann //===-- InstrumentationRuntime.cpp ----------------------------------------===//
2afdf842bSKuba Brecka //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6afdf842bSKuba Brecka //
79394d772SEugene Zelenko //===---------------------------------------------------------------------===//
8afdf842bSKuba Brecka 
9b9c1b51eSKate Stone #include "lldb/Target/InstrumentationRuntime.h"
101c23c148SVedant Kumar #include "lldb/Core/Module.h"
111c23c148SVedant Kumar #include "lldb/Core/ModuleList.h"
12afdf842bSKuba Brecka #include "lldb/Core/PluginManager.h"
13b9c1b51eSKate Stone #include "lldb/Target/Process.h"
14bf9a7730SZachary Turner #include "lldb/Utility/RegularExpression.h"
15b9c1b51eSKate Stone #include "lldb/lldb-private.h"
16afdf842bSKuba Brecka 
17afdf842bSKuba Brecka using namespace lldb;
18afdf842bSKuba Brecka using namespace lldb_private;
19afdf842bSKuba Brecka 
ModulesDidLoad(lldb_private::ModuleList & module_list,lldb_private::Process * process,InstrumentationRuntimeCollection & runtimes)20b9c1b51eSKate Stone void InstrumentationRuntime::ModulesDidLoad(
21b9c1b51eSKate Stone     lldb_private::ModuleList &module_list, lldb_private::Process *process,
22b9c1b51eSKate Stone     InstrumentationRuntimeCollection &runtimes) {
239394d772SEugene Zelenko   InstrumentationRuntimeCreateInstance create_callback = nullptr;
24afdf842bSKuba Brecka   InstrumentationRuntimeGetType get_type_callback;
25b9c1b51eSKate Stone   for (uint32_t idx = 0;; ++idx) {
26b9c1b51eSKate Stone     create_callback =
27b9c1b51eSKate Stone         PluginManager::GetInstrumentationRuntimeCreateCallbackAtIndex(idx);
289394d772SEugene Zelenko     if (create_callback == nullptr)
29afdf842bSKuba Brecka       break;
30b9c1b51eSKate Stone     get_type_callback =
31b9c1b51eSKate Stone         PluginManager::GetInstrumentationRuntimeGetTypeCallbackAtIndex(idx);
32afdf842bSKuba Brecka     InstrumentationRuntimeType type = get_type_callback();
33afdf842bSKuba Brecka 
34afdf842bSKuba Brecka     InstrumentationRuntimeCollection::iterator pos;
35afdf842bSKuba Brecka     pos = runtimes.find(type);
36afdf842bSKuba Brecka     if (pos == runtimes.end()) {
37afdf842bSKuba Brecka       runtimes[type] = create_callback(process->shared_from_this());
38afdf842bSKuba Brecka     }
39afdf842bSKuba Brecka   }
40afdf842bSKuba Brecka }
41afdf842bSKuba Brecka 
ModulesDidLoad(lldb_private::ModuleList & module_list)42b9c1b51eSKate Stone void InstrumentationRuntime::ModulesDidLoad(
43b9c1b51eSKate Stone     lldb_private::ModuleList &module_list) {
441c23c148SVedant Kumar   if (IsActive())
451c23c148SVedant Kumar     return;
461c23c148SVedant Kumar 
47b9c1b51eSKate Stone   if (GetRuntimeModuleSP()) {
481c23c148SVedant Kumar     Activate();
491c23c148SVedant Kumar     return;
501c23c148SVedant Kumar   }
511c23c148SVedant Kumar 
521c23c148SVedant Kumar   module_list.ForEach([this](const lldb::ModuleSP module_sp) -> bool {
531c23c148SVedant Kumar     const FileSpec &file_spec = module_sp->GetFileSpec();
541c23c148SVedant Kumar     if (!file_spec)
551c23c148SVedant Kumar       return true; // Keep iterating.
561c23c148SVedant Kumar 
571c23c148SVedant Kumar     const RegularExpression &runtime_regex = GetPatternForRuntimeLibrary();
58b9c1b51eSKate Stone     if (runtime_regex.Execute(file_spec.GetFilename().GetCString()) ||
59b9c1b51eSKate Stone         module_sp->IsExecutable()) {
60b9c1b51eSKate Stone       if (CheckIfRuntimeIsValid(module_sp)) {
611c23c148SVedant Kumar         SetRuntimeModuleSP(module_sp);
621c23c148SVedant Kumar         Activate();
631c23c148SVedant Kumar         return false; // Stop iterating, we're done.
641c23c148SVedant Kumar       }
651c23c148SVedant Kumar     }
661c23c148SVedant Kumar 
671c23c148SVedant Kumar     return true;
681c23c148SVedant Kumar   });
691c23c148SVedant Kumar }
701c23c148SVedant Kumar 
711aad8fb7SKuba Brecka lldb::ThreadCollectionSP
GetBacktracesFromExtendedStopInfo(StructuredData::ObjectSP info)72b9c1b51eSKate Stone InstrumentationRuntime::GetBacktracesFromExtendedStopInfo(
73b9c1b51eSKate Stone     StructuredData::ObjectSP info) {
741aad8fb7SKuba Brecka   return ThreadCollectionSP(new ThreadCollection());
751aad8fb7SKuba Brecka }
76