blob: 598c8ffe576146aecec04643e3c54bcbb80a132e [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
Andreas Gampeaadcbc62017-12-28 14:05:42 -080023#include <memory>
24
Andreas Gampe85f1c572018-11-21 13:52:48 -080025#include <android-base/logging.h>
26#include <android-base/macros.h>
Alex Light7233c7e2016-07-28 10:07:45 -070027
28namespace art {
29namespace ti {
30
Andreas Gampeaadcbc62017-12-28 14:05:42 -080031class Agent;
32
33enum LoadError {
34 kNoError, // No error occurred..
35 kLoadingError, // dlopen or dlsym returned an error.
36 kInitializationError, // The entrypoint did not return 0. This might require an abort.
37};
38
39class AgentSpec {
40 public:
41 explicit AgentSpec(const std::string& arg);
42
43 const std::string& GetName() const {
44 return name_;
45 }
46
47 const std::string& GetArgs() const {
48 return args_;
49 }
50
51 bool HasArgs() const {
52 return !GetArgs().empty();
53 }
54
55 std::unique_ptr<Agent> Load(/*out*/jint* call_res,
56 /*out*/LoadError* error,
57 /*out*/std::string* error_msg);
58
59 // Tries to attach the agent using its OnAttach method. Returns true on success.
Andreas Gampe7b38e692017-12-28 19:18:28 -080060 std::unique_ptr<Agent> Attach(JNIEnv* env,
61 jobject class_loader,
62 /*out*/jint* call_res,
Andreas Gampeaadcbc62017-12-28 14:05:42 -080063 /*out*/LoadError* error,
64 /*out*/std::string* error_msg);
65
66 private:
Andreas Gampe7b38e692017-12-28 19:18:28 -080067 std::unique_ptr<Agent> DoDlOpen(JNIEnv* env,
68 jobject class_loader,
69 /*out*/LoadError* error,
70 /*out*/std::string* error_msg);
Andreas Gampeaadcbc62017-12-28 14:05:42 -080071
Andreas Gampe7b38e692017-12-28 19:18:28 -080072 std::unique_ptr<Agent> DoLoadHelper(JNIEnv* env,
73 bool attaching,
74 jobject class_loader,
Andreas Gampeaadcbc62017-12-28 14:05:42 -080075 /*out*/jint* call_res,
76 /*out*/LoadError* error,
77 /*out*/std::string* error_msg);
78
79 std::string name_;
80 std::string args_;
81
82 friend std::ostream& operator<<(std::ostream &os, AgentSpec const& m);
83};
84
85std::ostream& operator<<(std::ostream &os, AgentSpec const& m);
86
Alex Light7233c7e2016-07-28 10:07:45 -070087using AgentOnLoadFunction = jint (*)(JavaVM*, const char*, void*);
Alex Light7233c7e2016-07-28 10:07:45 -070088using AgentOnUnloadFunction = void (*)(JavaVM*);
89
Alex Light65af20b2017-04-20 09:15:08 -070090// Agents are native libraries that will be loaded by the runtime for the purpose of
91// instrumentation. They will be entered by Agent_OnLoad or Agent_OnAttach depending on whether the
92// agent is being attached during runtime startup or later.
93//
94// The agent's Agent_OnUnload function will be called during runtime shutdown.
95//
Leonard Mosescueb842212016-10-06 17:26:36 -070096// TODO: consider splitting ti::Agent into command line, agent and shared library handler classes
Alex Light65af20b2017-04-20 09:15:08 -070097// TODO Support native-bridge. Currently agents can only be the actual runtime ISA of the device.
Alex Light7233c7e2016-07-28 10:07:45 -070098class Agent {
99 public:
Alex Light7233c7e2016-07-28 10:07:45 -0700100 const std::string& GetName() const {
101 return name_;
102 }
103
Alex Light65af20b2017-04-20 09:15:08 -0700104 void* FindSymbol(const std::string& name) const;
105
Alex Light7233c7e2016-07-28 10:07:45 -0700106 // TODO We need to acquire some locks probably.
107 void Unload();
108
Andreas Gampe44b31742018-10-01 19:30:57 -0700109 Agent(Agent&& other) noexcept;
110 Agent& operator=(Agent&& other) noexcept;
Alex Light7233c7e2016-07-28 10:07:45 -0700111
112 ~Agent();
113
Alex Light7233c7e2016-07-28 10:07:45 -0700114 private:
Andreas Gampeaadcbc62017-12-28 14:05:42 -0800115 Agent(const std::string& name, void* dlopen_handle) : name_(name),
116 dlopen_handle_(dlopen_handle),
117 onload_(nullptr),
118 onattach_(nullptr),
119 onunload_(nullptr) {
120 DCHECK(dlopen_handle != nullptr);
121 }
Alex Light7233c7e2016-07-28 10:07:45 -0700122
Andreas Gampeaadcbc62017-12-28 14:05:42 -0800123 void PopulateFunctions();
Leonard Mosescueb842212016-10-06 17:26:36 -0700124
125 std::string name_;
Alex Light7233c7e2016-07-28 10:07:45 -0700126 void* dlopen_handle_;
127
128 // The entrypoints.
129 AgentOnLoadFunction onload_;
Leonard Mosescueb842212016-10-06 17:26:36 -0700130 AgentOnLoadFunction onattach_;
Alex Light7233c7e2016-07-28 10:07:45 -0700131 AgentOnUnloadFunction onunload_;
132
Andreas Gampeaadcbc62017-12-28 14:05:42 -0800133 friend class AgentSpec;
Alex Light7233c7e2016-07-28 10:07:45 -0700134 friend std::ostream& operator<<(std::ostream &os, Agent const& m);
Andreas Gampeaadcbc62017-12-28 14:05:42 -0800135
136 DISALLOW_COPY_AND_ASSIGN(Agent);
Alex Light7233c7e2016-07-28 10:07:45 -0700137};
138
139std::ostream& operator<<(std::ostream &os, Agent const& m);
140std::ostream& operator<<(std::ostream &os, const Agent* m);
141
142} // namespace ti
143} // namespace art
144
145#endif // ART_RUNTIME_TI_AGENT_H_
146