blob: 7408aeec3522d5c30c5ec809f965d88c4da345e6 [file] [log] [blame]
Alex Light7233c7e2016-07-28 10:07:45 -07001/*
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#ifndef ART_RUNTIME_TI_AGENT_H_
18#define ART_RUNTIME_TI_AGENT_H_
19
20#include <dlfcn.h>
21#include <jni.h> // for jint, JavaVM* etc declarations
22
Alex Light7233c7e2016-07-28 10:07:45 -070023#include "runtime.h"
24#include "utils.h"
25
26namespace art {
27namespace ti {
28
29using AgentOnLoadFunction = jint (*)(JavaVM*, const char*, void*);
Alex Light7233c7e2016-07-28 10:07:45 -070030using AgentOnUnloadFunction = void (*)(JavaVM*);
31
Leonard Mosescueb842212016-10-06 17:26:36 -070032// TODO: consider splitting ti::Agent into command line, agent and shared library handler classes
33
Alex Light7233c7e2016-07-28 10:07:45 -070034class Agent {
35 public:
36 enum LoadError {
37 kNoError, // No error occurred..
38 kAlreadyStarted, // The agent has already been loaded.
39 kLoadingError, // dlopen or dlsym returned an error.
40 kInitializationError, // The entrypoint did not return 0. This might require an abort.
41 };
42
43 bool IsStarted() const {
44 return dlopen_handle_ != nullptr;
45 }
46
47 const std::string& GetName() const {
48 return name_;
49 }
50
51 const std::string& GetArgs() const {
52 return args_;
53 }
54
55 bool HasArgs() const {
56 return !GetArgs().empty();
57 }
58
Leonard Mosescueb842212016-10-06 17:26:36 -070059 LoadError Load(/*out*/jint* call_res, /*out*/std::string* error_msg) {
60 VLOG(agents) << "Loading agent: " << name_ << " " << args_;
61 return DoLoadHelper(false, call_res, error_msg);
62 }
Alex Light7233c7e2016-07-28 10:07:45 -070063
64 // TODO We need to acquire some locks probably.
65 void Unload();
66
67 // Tries to attach the agent using its OnAttach method. Returns true on success.
Leonard Mosescueb842212016-10-06 17:26:36 -070068 LoadError Attach(/*out*/jint* call_res, /*out*/std::string* error_msg) {
69 VLOG(agents) << "Attaching agent: " << name_ << " " << args_;
70 return DoLoadHelper(true, call_res, error_msg);
Alex Light7233c7e2016-07-28 10:07:45 -070071 }
72
Leonard Mosescueb842212016-10-06 17:26:36 -070073 explicit Agent(std::string arg);
Alex Light7233c7e2016-07-28 10:07:45 -070074
Leonard Mosescueb842212016-10-06 17:26:36 -070075 Agent(const Agent& other);
76 Agent& operator=(const Agent& other);
77
78 Agent(Agent&& other);
79 Agent& operator=(Agent&& other);
Alex Light7233c7e2016-07-28 10:07:45 -070080
81 ~Agent();
82
Alex Light7233c7e2016-07-28 10:07:45 -070083 private:
Alex Light7233c7e2016-07-28 10:07:45 -070084 LoadError DoDlOpen(/*out*/std::string* error_msg);
85
Leonard Mosescueb842212016-10-06 17:26:36 -070086 LoadError DoLoadHelper(bool attaching,
87 /*out*/jint* call_res,
88 /*out*/std::string* error_msg);
89
90 std::string name_;
91 std::string args_;
Alex Light7233c7e2016-07-28 10:07:45 -070092 void* dlopen_handle_;
93
94 // The entrypoints.
95 AgentOnLoadFunction onload_;
Leonard Mosescueb842212016-10-06 17:26:36 -070096 AgentOnLoadFunction onattach_;
Alex Light7233c7e2016-07-28 10:07:45 -070097 AgentOnUnloadFunction onunload_;
98
99 friend std::ostream& operator<<(std::ostream &os, Agent const& m);
100};
101
102std::ostream& operator<<(std::ostream &os, Agent const& m);
103std::ostream& operator<<(std::ostream &os, const Agent* m);
104
105} // namespace ti
106} // namespace art
107
108#endif // ART_RUNTIME_TI_AGENT_H_
109