blob: dc553dfe5d9c83a1bc1dc3e07a5af7f2a29f4381 [file] [log] [blame]
Phil Wang751beff2015-08-28 15:17:15 +08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <dlfcn.h>
18
19#include "code_simulator_container.h"
Andreas Gampe71da4872017-07-26 10:02:07 -070020
David Sehr1979c642018-04-26 14:41:18 -070021#include "base/globals.h"
Andreas Gampe57943812017-12-06 21:39:13 -080022#include "base/logging.h" // For VLOG.
Andreas Gampe71da4872017-07-26 10:02:07 -070023#include "code_simulator.h"
Phil Wang751beff2015-08-28 15:17:15 +080024
25namespace art {
26
27CodeSimulatorContainer::CodeSimulatorContainer(InstructionSet target_isa)
28 : libart_simulator_handle_(nullptr),
29 simulator_(nullptr) {
30 const char* libart_simulator_so_name =
31 kIsDebugBuild ? "libartd-simulator.so" : "libart-simulator.so";
32 libart_simulator_handle_ = dlopen(libart_simulator_so_name, RTLD_NOW);
33 // It is not a real error when libart-simulator does not exist, e.g., on target.
34 if (libart_simulator_handle_ == nullptr) {
35 VLOG(simulator) << "Could not load " << libart_simulator_so_name << ": " << dlerror();
36 } else {
Andreas Gampec55bb392018-09-21 00:02:02 +000037 using CreateCodeSimulatorPtr = CodeSimulator*(*)(InstructionSet);
38 CreateCodeSimulatorPtr create_code_simulator =
39 reinterpret_cast<CreateCodeSimulatorPtr>(
Phil Wang751beff2015-08-28 15:17:15 +080040 dlsym(libart_simulator_handle_, "CreateCodeSimulator"));
Andreas Gampec55bb392018-09-21 00:02:02 +000041 DCHECK(create_code_simulator != nullptr) << "Fail to find symbol of CreateCodeSimulator: "
Phil Wang751beff2015-08-28 15:17:15 +080042 << dlerror();
Andreas Gampec55bb392018-09-21 00:02:02 +000043 simulator_ = create_code_simulator(target_isa);
Phil Wang751beff2015-08-28 15:17:15 +080044 }
45}
46
47CodeSimulatorContainer::~CodeSimulatorContainer() {
48 // Free simulator object before closing libart-simulator because destructor of
49 // CodeSimulator lives in it.
50 if (simulator_ != nullptr) {
51 delete simulator_;
52 }
53 if (libart_simulator_handle_ != nullptr) {
54 dlclose(libart_simulator_handle_);
55 }
56}
57
58} // namespace art