2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef ART_RUNTIME_TI_AGENT_H_
18 #define ART_RUNTIME_TI_AGENT_H_
21 #include <jni.h> // for jint, JavaVM* etc declarations
23 #include "base/stringprintf.h"
30 using AgentOnLoadFunction = jint (*)(JavaVM*, const char*, void*);
31 using AgentOnAttachFunction = jint (*)(JavaVM*, const char*, void*);
32 using AgentOnUnloadFunction = void (*)(JavaVM*);
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.
43 bool IsStarted() const {
44 return dlopen_handle_ != nullptr;
47 const std::string& GetName() const {
51 const std::string& GetArgs() const {
55 bool HasArgs() const {
56 return !GetArgs().empty();
59 // TODO We need to acquire some locks probably.
60 LoadError Load(/*out*/jint* call_res, /*out*/std::string* error_msg);
62 // TODO We need to acquire some locks probably.
65 // Tries to attach the agent using its OnAttach method. Returns true on success.
66 // TODO We need to acquire some locks probably.
67 LoadError Attach(std::string* error_msg) {
69 *error_msg = "Attach has not yet been implemented!";
73 static Agent Create(std::string arg);
75 static Agent Create(std::string name, std::string args) {
76 return Agent(name, args);
81 // We need move constructor and copy for vectors
82 Agent(const Agent& other);
87 dlopen_handle_(nullptr),
91 other.dlopen_handle_ = nullptr;
92 other.onload_ = nullptr;
93 other.onattach_ = nullptr;
94 other.onunload_ = nullptr;
97 // We don't need an operator=
98 void operator=(const Agent&) = delete;
101 Agent(std::string name, std::string args)
104 dlopen_handle_(nullptr),
107 onunload_(nullptr) { }
109 LoadError DoDlOpen(/*out*/std::string* error_msg);
111 const std::string name_;
112 const std::string args_;
113 void* dlopen_handle_;
116 AgentOnLoadFunction onload_;
117 AgentOnAttachFunction onattach_;
118 AgentOnUnloadFunction onunload_;
120 friend std::ostream& operator<<(std::ostream &os, Agent const& m);
123 std::ostream& operator<<(std::ostream &os, Agent const& m);
124 std::ostream& operator<<(std::ostream &os, const Agent* m);
129 #endif // ART_RUNTIME_TI_AGENT_H_