Add dex lookup overloads which take C++ lambda callback.
Provide more direct access to the underlying C api.
Bug: 123517037
Test: m test-art-host-gtest
Change-Id: I364814e284e7569cf96eb7a7596a95bef38abbd7
diff --git a/libdexfile/external/dex_file_supp.cc b/libdexfile/external/dex_file_supp.cc
index 7200451..63dec1c 100644
--- a/libdexfile/external/dex_file_supp.cc
+++ b/libdexfile/external/dex_file_supp.cc
@@ -179,21 +179,17 @@
MethodInfo DexFile::GetMethodInfoForOffset(int64_t dex_offset, bool with_signature) {
MethodInfo res{};
- auto set_method = [](void* ctx, ExtDexFileMethodInfo* info) {
- *reinterpret_cast<MethodInfo*>(ctx) = AbsorbMethodInfo(info);
- };
+ auto set_method = [&res](ExtDexFileMethodInfo* info) { res = AbsorbMethodInfo(info); };
uint32_t flags = with_signature ? kExtDexFileWithSignature : 0;
- g_ExtDexFileGetMethodInfoForOffset(ext_dex_file_, dex_offset, flags, set_method, &res);
+ GetMethodInfoForOffset(dex_offset, set_method, flags);
return res;
}
std::vector<MethodInfo> DexFile::GetAllMethodInfos(bool with_signature) {
std::vector<MethodInfo> res;
- auto add_method = [](void* ctx, ExtDexFileMethodInfo* info) {
- reinterpret_cast<std::vector<MethodInfo>*>(ctx)->push_back(AbsorbMethodInfo(info));
- };
+ auto add_method = [&res](ExtDexFileMethodInfo* info) { res.push_back(AbsorbMethodInfo(info)); };
uint32_t flags = with_signature ? kExtDexFileWithSignature : 0;
- g_ExtDexFileGetAllMethodInfos(ext_dex_file_, flags, add_method, &res);
+ GetAllMethodInfos(add_method, flags);
return res;
}
diff --git a/libdexfile/external/include/art_api/dex_file_support.h b/libdexfile/external/include/art_api/dex_file_support.h
index 3fbf169..7c4ff5c 100644
--- a/libdexfile/external/include/art_api/dex_file_support.h
+++ b/libdexfile/external/include/art_api/dex_file_support.h
@@ -100,12 +100,28 @@
/*out*/ std::string* error_msg);
// Given an offset relative to the start of the dex file header, if there is a
+ // method whose instruction range includes that offset then calls the provided
+ // callback with ExtDexFileMethodInfo* (which is live only during the callback).
+ template<typename T /* lambda taking (ExtDexFileMethodInfo*) */>
+ void GetMethodInfoForOffset(int64_t dex_offset, T& callback, uint32_t flags = 0) {
+ auto cb = [](void* ctx, ExtDexFileMethodInfo* info) { (*reinterpret_cast<T*>(ctx))(info); };
+ g_ExtDexFileGetMethodInfoForOffset(ext_dex_file_, dex_offset, flags, cb, &callback);
+ }
+
+ // Given an offset relative to the start of the dex file header, if there is a
// method whose instruction range includes that offset then returns info about
// it, otherwise returns a struct with offset == 0. MethodInfo.name receives
// the full function signature if with_signature is set, otherwise it gets the
// class and method name only.
MethodInfo GetMethodInfoForOffset(int64_t dex_offset, bool with_signature);
+ // Call the provided callback for all dex methods.
+ template<typename T /* lambda taking (ExtDexFileMethodInfo*) */>
+ void GetAllMethodInfos(T& callback, uint32_t flags = 0) {
+ auto cb = [](void* ctx, ExtDexFileMethodInfo* info) { (*reinterpret_cast<T*>(ctx))(info); };
+ g_ExtDexFileGetAllMethodInfos(ext_dex_file_, flags, cb, &callback);
+ }
+
// Returns info structs about all methods in the dex file. MethodInfo.name
// receives the full function signature if with_signature is set, otherwise it
// gets the class and method name only.