Skip to content

Commit b49d396

Browse files
committed
fakemod: improve debugging
1 parent 088aad3 commit b49d396

File tree

1 file changed

+57
-40
lines changed

1 file changed

+57
-40
lines changed

iop/fakemod/src/main.c

+57-40
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,54 @@ static int (*org_UnloadModule)(int id);
2222
static int (*org_SearchModuleByName)(const char *modname);
2323
static int (*org_ReferModuleStatus)(int mid, ModuleStatus *status);
2424

25+
#if 0 //def DEBUG // Too much text output, enable when needed
26+
//--------------------------------------------------------------
27+
static void print_libs()
28+
{
29+
ModuleInfo_t *m = GetLoadcoreInternalData()->image_info;
30+
M_DEBUG("Module list:\n");
31+
M_DEBUG(" name | start | text | data | bss\n");
32+
while (m != NULL) {
33+
M_DEBUG(" %-22s | 0x%6x | %6d | %5d | %5d\n", m->name, m->text_start, m->text_size, m->data_size, m->bss_size);
34+
m = m->next;
35+
}
36+
}
37+
#else
38+
static inline void print_libs() {}
39+
#endif
40+
41+
#ifdef DEBUG
42+
//--------------------------------------------------------------
43+
static void print_args(int arg_len, char *args)
44+
{
45+
// Multiple null terminated strings together
46+
int args_idx = 0;
47+
int was_null = 1;
48+
49+
if (arg_len == 0)
50+
return;
51+
52+
M_DEBUG("Module arguments (arg_len=%d):\n", arg_len);
53+
54+
// Search strings
55+
while(args_idx < arg_len) {
56+
if (args[args_idx] == 0) {
57+
if (was_null == 1) {
58+
M_DEBUG("- args[%d]=0\n", args_idx);
59+
}
60+
was_null = 1;
61+
}
62+
else if (was_null == 1) {
63+
M_DEBUG("- args[%d]='%s'\n", args_idx, &args[args_idx]);
64+
was_null = 0;
65+
}
66+
args_idx++;
67+
}
68+
}
69+
#else
70+
static inline void print_args(int arg_len, char *args) {}
71+
#endif
72+
2573
//--------------------------------------------------------------
2674
static struct FakeModule *checkFakemodByFile(const char *path, struct FakeModule *fakemod_list)
2775
{
@@ -64,45 +112,14 @@ static struct FakeModule *checkFakemodById(int id, struct FakeModule *fakemod_li
64112
return NULL;
65113
}
66114

67-
#ifdef DEBUG
68-
//--------------------------------------------------------------
69-
static void print_args(int arg_len, char *args)
70-
{
71-
// Multiple null terminated strings together
72-
int args_idx = 0;
73-
int was_null = 1;
74-
75-
if (arg_len == 0)
76-
return;
77-
78-
M_DEBUG("Module arguments (arg_len=%d):\n", arg_len);
79-
80-
// Search strings
81-
while(args_idx < arg_len) {
82-
if (args[args_idx] == 0) {
83-
if (was_null == 1) {
84-
M_DEBUG("- args[%d]=0\n", args_idx);
85-
}
86-
was_null = 1;
87-
}
88-
else if (was_null == 1) {
89-
M_DEBUG("- args[%d]='%s'\n", args_idx, &args[args_idx]);
90-
was_null = 0;
91-
}
92-
args_idx++;
93-
}
94-
}
95-
#endif
96-
97115
//--------------------------------------------------------------
98116
static int Hook_LoadStartModule(char *modpath, int arg_len, char *args, int *modres)
99117
{
100118
struct FakeModule *mod;
101119

120+
print_libs();
102121
M_DEBUG("%s(%s, %d, ...)\n", __FUNCTION__, modpath, arg_len);
103-
#ifdef DEBUG
104122
print_args(arg_len, args);
105-
#endif
106123

107124
mod = checkFakemodByFile(modpath, fmd.fake);
108125
if (mod != NULL) {
@@ -131,9 +148,7 @@ static int Hook_StartModule(int id, char *modname, int arg_len, char *args, int
131148
struct FakeModule *mod;
132149

133150
M_DEBUG("%s(0x%x, %s, %d, ...)\n", __FUNCTION__, id, modname, arg_len);
134-
#ifdef DEBUG
135151
print_args(arg_len, args);
136-
#endif
137152

138153
mod = checkFakemodById(id, fmd.fake);
139154
if (mod != NULL) {
@@ -164,7 +179,8 @@ static int Hook_LoadModuleBuffer(void *ptr)
164179
elf_pheader_t *eph = (elf_pheader_t *)(ptr + eh->phoff);
165180
const char *modname = (const char *)ptr + eph->offset + 0x1a;
166181

167-
M_DEBUG("%s() modname = '%s'\n", __FUNCTION__, modname);
182+
print_libs();
183+
M_DEBUG("%s(0x%x) modname = '%s'\n", __FUNCTION__, ptr, modname);
168184

169185
mod = checkFakemodByName(modname, fmd.fake);
170186
if (mod != NULL) {
@@ -192,9 +208,7 @@ static int Hook_StopModule(int id, int arg_len, char *args, int *modres)
192208
struct FakeModule *mod;
193209

194210
M_DEBUG("%s(0x%x, %d, ...)\n", __FUNCTION__, id, arg_len);
195-
#ifdef DEBUG
196211
print_args(arg_len, args);
197-
#endif
198212

199213
mod = checkFakemodById(id, fmd.fake);
200214
if (mod != NULL) {
@@ -255,11 +269,11 @@ static int Hook_ReferModuleStatus(int id, ModuleStatus *status)
255269
{
256270
struct FakeModule *mod;
257271

258-
M_DEBUG("%s(0x%x, ...)\n", __FUNCTION__, id);
272+
//M_DEBUG("%s(0x%x, ...)\n", __FUNCTION__, id);
259273

260274
mod = checkFakemodById(id, fmd.fake);
261275
if (mod != NULL && mod->returnLoad == 0) {
262-
M_DEBUG("- FAKING! id=0x%x\n", mod->id);
276+
//M_DEBUG("- FAKING! id=0x%x\n", mod->id);
263277
memset(status, 0, sizeof(ModuleStatus));
264278
strcpy(status->name, mod->name);
265279
status->version = mod->version;
@@ -275,8 +289,11 @@ int _start(int argc, char **argv)
275289
{
276290
int i;
277291

292+
print_libs();
293+
278294
// Change string index to string pointers
279295
M_DEBUG("Fake module list:\n");
296+
M_DEBUG(" fname | name | vers. | rl | rs | prop\n");
280297
for (i = 0; i < MODULE_SETTINGS_MAX_FAKE_COUNT; i++) {
281298
struct FakeModule *fm = &fmd.fake[i];
282299

@@ -293,7 +310,7 @@ int _start(int argc, char **argv)
293310
}
294311

295312
if (fm->fname != NULL) {
296-
M_DEBUG(" %d: %12s | %-14s | 0x%3x | %3d | %d | 0x%x\n", i, fm->fname, fm->name, fm->version, fm->returnLoad, fm->returnStart, fm->prop);
313+
M_DEBUG(" %12s | %-14s | 0x%3x | %3d | %2d | 0x%x\n", fm->fname, fm->name, fm->version, fm->returnLoad, fm->returnStart, fm->prop);
297314
}
298315
}
299316

0 commit comments

Comments
 (0)