-
Notifications
You must be signed in to change notification settings - Fork 0
/
DisplayStackActivity.cpp
175 lines (134 loc) · 6.45 KB
/
DisplayStackActivity.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* 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.h>
#include <mmk/debug/modules.h>
// Private APIs for debugging!
#include "elf.reader.hpp"
#include "dwarf.debugLine.reader.hpp"
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#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_maulingmonkey_debug_stack_nvidiaCodeworksTest_DisplayStackActivity_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_maulingmonkey_debug_stack_nvidiaCodeworksTest_DisplayStackActivity_getModules( JNIEnv* env, jobject thiz )
{
std::stringstream ss;
mmk::debug::modulesLoaded( mmkDebugModulesResolveAll, [&](const mmk::debug::module& m){
//ss << " " << m.name << " ver=" << m.version << " path=" << m.path << "\n";
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_maulingmonkey_debug_stack_nvidiaCodeworksTest_DisplayStackActivity_traceDemo( JNIEnv* env, jobject thiz )
{
recursive<10>::test_trace();
return env->NewStringUTF(lastTrace.str().c_str());
}
void dumpElf(mmk::debug::elf::reader& p, std::ostream& o)
{
using namespace mmk::debug;
elf::fileHeader elf;
std::vector<char> names;
if (!p.read(elf)) return;
if (!p.readSectionNames(elf, names)) return;
for (size_t i=0; i<elf.sectionHeaderCount; ++i)
{
elf::sectionHeader sHeader;
if (!p.read(elf, i, sHeader)) return;
const char* sName = p.name(sHeader, names);
if (!sName) return;
o << "Section #" << i << ": " << sName << " = " << sHeader.fileOffset << ".." << (sHeader.fileOffset+sHeader.size) << "\n";
if (strcmp(sName, ".debug_line") == 0) {
std::vector<char> sBuffer;
p.read(sHeader, sBuffer);
dwarf::debugLine::header _prologueBuf;
if (auto spp = dwarf::debugLine::header::tryParse(sBuffer.data(), sBuffer.size(), _prologueBuf)) {
const char* end = sBuffer.data() + sBuffer.size();
if (spp->afterTotal < end) end = spp->afterTotal;
o << " totalLen: " << (unsigned)spp->totalLength << "\n";
o << " ver: " << (unsigned)spp->version << "\n";
o << " proLen: " << (unsigned)spp->prologueLength << "\n";
o << " minIns: " << (unsigned)spp->minimumInstructionLength << "\n";
o << " defIsSt: " << (bool) spp->defaultIsStatement << "\n";
o << " lineBase: " << ( signed)spp->lineBase << "\n";
o << " lineRange: " << (unsigned)spp->lineRange << "\n";
o << " specialOp: " << (unsigned)spp->firstSpecialOpcode << "\n";
o << " stdops: " << (unsigned)spp->standardOpcodeLengths.size() << "\n";
o << " dirs: " << (unsigned)spp->dirs.size() << "\n";
for (const char* dir : spp->dirs) o << " " << dir << "\n";
o << " files: " << spp->files.size() << "\n";
for (const auto& file : spp->files) if (file.name) o << " " << file.name << "\n";
if (spp->afterPrologue) {
dwarf::debugLine::reader r(*spp, spp->afterPrologue, end);
void* prev = NULL;
r.for_each([&](){
const char* file = "unknown";
if (r.file < spp->files.size()) file = spp->files[r.file].name;
o << "\n";
if (prev) o << prev << "..";
o << (void*)r.address << ": " << file << "(" << r.line << ")";
prev = r.endSequence ? NULL : (void*)r.address;
},[&](const char* debug){
o << " " << debug;
//o << "op" << opCode << " = " << (void*)r.address << ": " << file << "(" << r.line << ")\n";
});
o << "\n";
}
}
}
}
}
extern "C" JNIEXPORT jstring JNICALL
Java_com_maulingmonkey_debug_stack_nvidiaCodeworksTest_DisplayStackActivity_inspectElf( JNIEnv* env, jobject thiz, jstring jElfDir, jstring jElfName)
{
const char* elfDir = env->GetStringUTFChars(jElfDir, NULL);
const char* elfName = env->GetStringUTFChars(jElfName, NULL);
using namespace mmk::debug::elf;
std::stringstream o;
o << "ELF: " << elfDir << "/" << elfName << "\n";
reader p(&elfDir, 1, elfName);
dumpElf(p, o);
if (const char* fail = p.failureCause()) { o << "Failure cause: " << fail << "\n"; }
env->ReleaseStringUTFChars(jElfDir, elfDir);
env->ReleaseStringUTFChars(jElfName, elfName);
return env->NewStringUTF(o.str().c_str());
}