-
Notifications
You must be signed in to change notification settings - Fork 0
/
mmkAndroidTest.cpp
74 lines (58 loc) · 3.04 KB
/
mmkAndroidTest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* Copyright 2017 MaulingMonkey
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <mmk/debug/stack.hpp>
#include <mmk/debug/modules.hpp>
#include <sstream>
#include <jni.h>
#define NO_INLINE ZMMK_DEBUG_STACK_NO_INLINE // Note: ZMMK_* isn't part of the public API
extern "C" JNIEXPORT void JNICALL
Java_com_mmkAndroidTest_mmkAndroidTest_mmkDebugStackSearchPaths( JNIEnv* env, jobject thiz, jstring jElfDir )
{
const char* elfDir = env->GetStringUTFChars(jElfDir, NULL);
mmkDebugStackSearchPaths(&elfDir, 1);
env->ReleaseStringUTFChars(jElfDir, elfDir);
}
extern "C" JNIEXPORT jstring JNICALL
Java_com_mmkAndroidTest_mmkAndroidTest_getModules( JNIEnv* env, jobject thiz )
{
std::stringstream ss;
mmk::debug::modulesLoaded( mmkDebugModulesResolveAll, [&](const mmk::debug::module& m){
ss << " " << m.path << "\n";
return true;
});
return env->NewStringUTF(ss.str().c_str());
}
const char* nameOnly(const char* filename) {
if (const char* bs = strrchr(filename, '\\')) return bs+1;
if (const char* fs = strrchr(filename, '/')) return fs+1;
return filename;
}
std::stringstream lastTrace;
#define TRACE() (void)(lastTrace.str(""), mmk::debug::stackCurrentThread(mmkDebugStackResolveAll, 0, 25, \
[](const mmk::debug::stackFunction& f) { \
lastTrace << " " << nameOnly(f.file) << "(" << f.line << "): " << f.name; \
if (f.functionBase) lastTrace << " + " << (void*)((size_t)f.address - (size_t)f.functionBase); \
else lastTrace << " @ " << f.address; \
lastTrace << " (" << f.module; \
if (f.moduleBase) lastTrace << " + " << (void*)((size_t)f.address - (size_t)f.moduleBase); \
lastTrace << ")\n"; \
return true; /* continue trace */ \
}, MMK_DEBUG_STACK_HINT()), 0)
NO_INLINE void test_trace() { TRACE(); }
template < size_t N > struct recursive { NO_INLINE static void test_trace() { recursive<N-1>::test_trace(); } };
template <> struct recursive<0> { NO_INLINE static void test_trace() { ::test_trace(); } };
extern "C" JNIEXPORT jstring JNICALL
Java_com_mmkAndroidTest_mmkAndroidTest_traceDemo( JNIEnv* env, jobject thiz )
{
recursive<10>::test_trace();
return env->NewStringUTF(lastTrace.str().c_str());
}