blob: 6561756edd7ef107a6f05f38f14eadf695395fa7 [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
23#include "base/stringprintf.h"
24#include "runtime.h"
25#include "utils.h"
26
27namespace art {
28namespace ti {
29
30using AgentOnLoadFunction = jint (*)(JavaVM*, const char*, void*);
Alex Light7233c7e2016-07-28 10:07:45 -070031using AgentOnUnloadFunction = void (*)(JavaVM*);
32
Leonard Mosescueb842212016-10-06 17:26:36 -070033// TODO: consider splitting ti::Agent into command line, agent and shared library handler classes
34
Alex Light7233c7e2016-07-28 10:07:45 -070035class Agent {
36 public:
37 enum LoadError {
38 kNoError, // No error occurred..
39 kAlreadyStarted, // The agent has already been loaded.
40 kLoadingError, // dlopen or dlsym returned an error.
41 kInitializationError, // The entrypoint did not return 0. This might require an abort.
42 };
43
44 bool IsStarted() const {
45 return dlopen_handle_ != nullptr;
46 }
47
48 const std::string& GetName() const {
49 return name_;
50 }
51
52 const std::string& GetArgs() const {
53 return args_;
54 }
55
56 bool HasArgs() const {
57 return !GetArgs().empty();
58 }
59
Leonard Mosescueb842212016-10-06 17:26:36 -070060 LoadError Load(/*out*/jint* call_res, /*out*/std::string* error_msg) {
61 VLOG(agents) << "Loading agent: " << name_ << " " << args_;
62 return DoLoadHelper(false, call_res, error_msg);
63 }
Alex Light7233c7e2016-07-28 10:07:45 -070064
65 // TODO We need to acquire some locks probably.
66 void Unload();
67
68 // Tries to attach the agent using its OnAttach method. Returns true on success.
Leonard Mosescueb842212016-10-06 17:26:36 -070069 LoadError Attach(/*out*/jint* call_res, /*out*/std::string* error_msg) {
70 VLOG(agents) << "Attaching agent: " << name_ << " " << args_;
71 return DoLoadHelper(true, call_res, error_msg);
Alex Light7233c7e2016-07-28 10:07:45 -070072 }
73
Leonard Mosescueb842212016-10-06 17:26:36 -070074 explicit Agent(std::string arg);
Alex Light7233c7e2016-07-28 10:07:45 -070075
Leonard Mosescueb842212016-10-06 17:26:36 -070076 Agent(const Agent& other);
77 Agent& operator=(const Agent& other);
78
79 Agent(Agent&& other);
80 Agent& operator=(Agent&& other);
Alex Light7233c7e2016-07-28 10:07:45 -070081
82 ~Agent();
83
Alex Light7233c7e2016-07-28 10:07:45 -070084 private:
Alex Light7233c7e2016-07-28 10:07:45 -070085 LoadError DoDlOpen(/*out*/std::string* error_msg);
86
Leonard Mosescueb842212016-10-06 17:26:36 -070087 LoadError DoLoadHelper(bool attaching,
88 /*out*/jint* call_res,
89 /*out*/std::string* error_msg);
90
91 std::string name_;
92 std::string args_;
Alex Light7233c7e2016-07-28 10:07:45 -070093 void* dlopen_handle_;
94
95 // The entrypoints.
96 AgentOnLoadFunction onload_;
Leonard Mosescueb842212016-10-06 17:26:36 -070097 AgentOnLoadFunction onattach_;
Alex Light7233c7e2016-07-28 10:07:45 -070098 AgentOnUnloadFunction onunload_;
99
100 friend std::ostream& operator<<(std::ostream &os, Agent const& m);
101};
102
103std::ostream& operator<<(std::ostream &os, Agent const& m);
104std::ostream& operator<<(std::ostream &os, const Agent* m);
105
106} // namespace ti
107} // namespace art
108
109#endif // ART_RUNTIME_TI_AGENT_H_
110