blob: c4683b84f10c0acbaaf89e5df945f9218631260c [file] [log] [blame]
Keun Soo Yimb2811ee2016-03-03 13:27:33 -08001/*
2 * Copyright (C) 2016 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 "component_loader/DllLoader.h"
18
19#include <stdlib.h>
20#include <stdio.h>
21#include <dlfcn.h>
22
23#include <iostream>
24
25#include "hardware/hardware.h"
26
27using namespace std;
28
29
30namespace android {
31namespace vts {
32
33
34DllLoader::DllLoader()
35 : handle_(NULL) {
36}
37
38
39DllLoader::~DllLoader() {
40 if (!handle_) {
41 dlclose(handle_);
42 handle_ = NULL;
43 }
44}
45
46
47void* DllLoader::Load(const char* file_path) {
48 if (!file_path) {
49 cerr << __FUNCTION__ << ": file_path is NULL" << endl;
50 return NULL;
51 }
52
53 // consider using the load mechanism in hardware/libhardware/hardware.c
54 handle_ = dlopen(file_path, RTLD_LAZY);
55 if (!handle_) {
56 fputs(dlerror(), stderr);
57 cerr << "Can't load a dll " << file_path << endl;
58 return NULL;
59 }
60 cout << "DLL loaded " << file_path << endl;
61 return handle_;
62}
63
64
Keun Soo Yim34067de2016-05-17 09:46:37 -070065struct hw_device_t* DllLoader::GetHWDevice(const char* module_name) {
Keun Soo Yimb2811ee2016-03-03 13:27:33 -080066 if (!handle_) {
67 cerr << __FUNCTION__ << ": handle_ is NULL" << endl;
68 return NULL;
69 }
Keun Soo Yim8e07a092016-05-04 16:30:35 -070070 if (module_name) {
71 cout << __FUNCTION__ << ":" << __LINE__
72 << " module_name" << module_name << endl;
73 }
74 struct hw_module_t* hmi = (struct hw_module_t*) dlsym(
Keun Soo Yimb2811ee2016-03-03 13:27:33 -080075 handle_, HAL_MODULE_INFO_SYM_AS_STR);
76 if (!hmi) {
77 cerr << __FUNCTION__ << ": " << HAL_MODULE_INFO_SYM_AS_STR
78 << " not found" << endl;
79 return NULL;
80 }
Keun Soo Yim34067de2016-05-17 09:46:37 -070081 cout << __FUNCTION__ << ":" << __LINE__ << endl;
Keun Soo Yimb2811ee2016-03-03 13:27:33 -080082 hmi->dso = handle_;
83 device_ = NULL;
Keun Soo Yim8e07a092016-05-04 16:30:35 -070084 cout << __FUNCTION__ << ": version " << hmi->module_api_version << endl;
85
Keun Soo Yim34067de2016-05-17 09:46:37 -070086 int ret;
87 if (!module_name || strlen(module_name) == 0) {
Keun Soo Yim8e07a092016-05-04 16:30:35 -070088 cout << __FUNCTION__ << ":" << __LINE__ << ": (default) " << hmi->name << endl;
Keun Soo Yim34067de2016-05-17 09:46:37 -070089 ret = hmi->methods->open(hmi, hmi->name,
90 (struct hw_device_t**) &device_);
91 } else {
Keun Soo Yim8e07a092016-05-04 16:30:35 -070092 cout << __FUNCTION__ << ":" << __LINE__ << ": module_name |"
93 << module_name << "|" << endl;
94 ret = hmi->methods->open(
95 hmi, module_name, (struct hw_device_t**) &device_);
Keun Soo Yim34067de2016-05-17 09:46:37 -070096 }
97 if (ret != 0) {
98 cout << "returns " << ret << " " << strerror(errno) << endl;
99 }
100 cout << __FUNCTION__ << ":" << __LINE__ << endl;
Keun Soo Yimb2811ee2016-03-03 13:27:33 -0800101 return device_;
102}
103
104
105loader_function DllLoader::GetLoaderFunction(const char* function_name) {
106 const char* error;
107 loader_function func;
108
109 func = (loader_function) dlsym(handle_, function_name);
110 if ((error = dlerror()) != NULL) {
111 fputs(error, stderr);
112 cerr << __FUNCTION__ << ": Can't find " << function_name << endl;
113 return NULL;
114 }
115 return func;
116}
117
Keun Soo Yim8e07a092016-05-04 16:30:35 -0700118
Keun Soo Yim04431a92016-05-24 16:26:42 -0700119bool DllLoader::SancovResetCoverage() {
120 const char* error;
121 void (*func)();
122
123 func = (void (*)()) dlsym(handle_, "__sanitizer_reset_coverage");
124 if ((error = dlerror()) != NULL) {
125 fputs(error, stderr);
126 cerr << __FUNCTION__ << ": Can't find __sanitizer_reset_coverage" << endl;
127 return false;
128 }
129 func();
130 return true;
131}
132
133
134bool DllLoader::GcovInit(writeout_fn wfn, flush_fn ffn) {
135 const char* error;
136 void (*func)(writeout_fn, flush_fn);
137
138 func = (void (*)(writeout_fn, flush_fn)) dlsym(handle_, "llvm_gcov_init");
139 if ((error = dlerror()) != NULL) {
140 fputs(error, stderr);
141 cerr << __FUNCTION__ << ": Can't find llvm_gcov_init" << endl;
142 return false;
143 }
144 func(wfn, ffn);
145 return true;
146}
147
148
149bool DllLoader::GcovFlush() {
150 const char* error;
151 void (*func)();
152
153 func = (void (*)()) dlsym(handle_, "__gcov_flush");
154 if ((error = dlerror()) != NULL) {
155 fputs(error, stderr);
156 cerr << __FUNCTION__ << ": Can't find __gcov_flush" << endl;
157 return false;
158 }
159 func();
160
161 return true;
162}
163
Keun Soo Yimb2811ee2016-03-03 13:27:33 -0800164} // namespace vts
165} // namespace android