1278332Semaste//===-- InstrumentationRuntime.cpp ------------------------------*- C++ -*-===//
2278332Semaste//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6278332Semaste//
7309124Sdim//===---------------------------------------------------------------------===//
8278332Semaste
9314564Sdim#include "lldb/Target/InstrumentationRuntime.h"
10314564Sdim#include "lldb/Core/Module.h"
11314564Sdim#include "lldb/Core/ModuleList.h"
12314564Sdim#include "lldb/Core/PluginManager.h"
13314564Sdim#include "lldb/Target/Process.h"
14321369Sdim#include "lldb/Utility/RegularExpression.h"
15278332Semaste#include "lldb/lldb-private.h"
16278332Semaste
17278332Semasteusing namespace lldb;
18278332Semasteusing namespace lldb_private;
19278332Semaste
20314564Sdimvoid InstrumentationRuntime::ModulesDidLoad(
21314564Sdim    lldb_private::ModuleList &module_list, lldb_private::Process *process,
22314564Sdim    InstrumentationRuntimeCollection &runtimes) {
23314564Sdim  InstrumentationRuntimeCreateInstance create_callback = nullptr;
24314564Sdim  InstrumentationRuntimeGetType get_type_callback;
25314564Sdim  for (uint32_t idx = 0;; ++idx) {
26314564Sdim    create_callback =
27314564Sdim        PluginManager::GetInstrumentationRuntimeCreateCallbackAtIndex(idx);
28314564Sdim    if (create_callback == nullptr)
29314564Sdim      break;
30314564Sdim    get_type_callback =
31314564Sdim        PluginManager::GetInstrumentationRuntimeGetTypeCallbackAtIndex(idx);
32314564Sdim    InstrumentationRuntimeType type = get_type_callback();
33314564Sdim
34314564Sdim    InstrumentationRuntimeCollection::iterator pos;
35314564Sdim    pos = runtimes.find(type);
36314564Sdim    if (pos == runtimes.end()) {
37314564Sdim      runtimes[type] = create_callback(process->shared_from_this());
38278332Semaste    }
39314564Sdim  }
40278332Semaste}
41278332Semaste
42314564Sdimvoid InstrumentationRuntime::ModulesDidLoad(
43314564Sdim    lldb_private::ModuleList &module_list) {
44314564Sdim  if (IsActive())
45314564Sdim    return;
46278332Semaste
47314564Sdim  if (GetRuntimeModuleSP()) {
48314564Sdim    Activate();
49314564Sdim    return;
50314564Sdim  }
51314564Sdim
52314564Sdim  module_list.ForEach([this](const lldb::ModuleSP module_sp) -> bool {
53314564Sdim    const FileSpec &file_spec = module_sp->GetFileSpec();
54314564Sdim    if (!file_spec)
55314564Sdim      return true; // Keep iterating.
56314564Sdim
57314564Sdim    const RegularExpression &runtime_regex = GetPatternForRuntimeLibrary();
58314564Sdim    if (runtime_regex.Execute(file_spec.GetFilename().GetCString()) ||
59314564Sdim        module_sp->IsExecutable()) {
60314564Sdim      if (CheckIfRuntimeIsValid(module_sp)) {
61314564Sdim        SetRuntimeModuleSP(module_sp);
62314564Sdim        Activate();
63314564Sdim        return false; // Stop iterating, we're done.
64314564Sdim      }
65314564Sdim    }
66314564Sdim
67314564Sdim    return true;
68314564Sdim  });
69278332Semaste}
70309124Sdim
71309124Sdimlldb::ThreadCollectionSP
72314564SdimInstrumentationRuntime::GetBacktracesFromExtendedStopInfo(
73314564Sdim    StructuredData::ObjectSP info) {
74314564Sdim  return ThreadCollectionSP(new ThreadCollection());
75309124Sdim}
76