Skip to content

Commit 098e2fb

Browse files
committed
Debug output intercept demo
1 parent a004066 commit 098e2fb

File tree

3 files changed

+89
-2
lines changed

3 files changed

+89
-2
lines changed

samples/hello/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
XBE_TITLE = hello
22
GEN_XISO = $(XBE_TITLE).iso
3-
SRCS = $(CURDIR)/main.c
3+
SRCS = $(CURDIR)/main.c $(CURDIR)/int.s
44
NXDK_DIR = $(CURDIR)/../..
55
include $(NXDK_DIR)/Makefile

samples/hello/int.s

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
.globl _dbg_output_isr_stub
2+
_dbg_output_isr_stub:
3+
pushl %ebp
4+
pushl %edi
5+
pushl %esi
6+
pushl %edx
7+
pushl %ecx
8+
pushl %ebx
9+
pushl %eax
10+
call _dbg_output
11+
popl %eax
12+
popl %ebx
13+
popl %ecx
14+
popl %edx
15+
popl %esi
16+
popl %edi
17+
popl %ebp
18+
.extern dbg_output_original_isr_addr
19+
jmp *_dbg_output_original_isr_addr

samples/hello/main.c

+69-1
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,80 @@
22
#include <hal/video.h>
33
#include <windows.h>
44

5+
struct idtr_t
6+
{
7+
uint16_t limit;
8+
uint32_t base;
9+
} __attribute__((packed));
10+
11+
struct idt_descriptor_t
12+
{
13+
uint16_t offset_low;
14+
uint16_t selector;
15+
uint8_t zero;
16+
uint8_t type_attr;
17+
uint16_t offset_high;
18+
} __attribute__((packed));
19+
20+
void get_idtr (struct idtr_t *idtr)
21+
{
22+
asm ("sidt %0" : "=m" (*idtr));
23+
}
24+
25+
uint32_t dbg_output_original_isr_addr;
26+
extern char dbg_output_isr_stub;
27+
28+
void hook_int_2d ()
29+
{
30+
struct idtr_t idtr;
31+
struct idt_descriptor_t *entries;
32+
uint32_t new_isr = (uint32_t)&dbg_output_isr_stub;
33+
34+
get_idtr(&idtr);
35+
entries = (struct idt_descriptor_t *)idtr.base;
36+
37+
uint32_t old_isr = (((uint32_t)entries[0x2d].offset_high) << 16) | entries[0x2d].offset_low;
38+
dbg_output_original_isr_addr = old_isr;
39+
entries[0x2d].offset_low = ((uint32_t)new_isr) & 0xffff;
40+
entries[0x2d].offset_high = (((uint32_t)new_isr) >> 16) & 0xffff;
41+
}
42+
43+
void unhook_int_2d ()
44+
{
45+
struct idtr_t idtr;
46+
struct idt_descriptor_t *entries;
47+
48+
get_idtr(&idtr);
49+
entries = (struct idt_descriptor_t *)idtr.base;
50+
entries[0x2d].offset_low = dbg_output_original_isr_addr & 0xffff;
51+
entries[0x2d].offset_high = (dbg_output_original_isr_addr >> 16) & 0xffff;
52+
}
53+
54+
__cdecl void dbg_output (uint32_t eax, uint32_t ebx, uint32_t ecx, uint32_t edx, uint32_t esi, uint32_t edi, uint32_t ebp)
55+
{
56+
if (eax == 1) {
57+
PANSI_STRING s = (PANSI_STRING)ecx;
58+
debugPrint("%s\n", s->Buffer);
59+
}
60+
}
61+
562
int main(void)
663
{
764
XVideoSetMode(640, 480, 32, REFRESH_DEFAULT);
865

66+
hook_int_2d();
67+
68+
// This will be visible on screen
69+
OutputDebugString("test\n");
70+
71+
unhook_int_2d();
72+
73+
// This will not be visible on screen
74+
OutputDebugString("test2\n");
75+
76+
debugPrint("tests done\n");
77+
978
while(1) {
10-
debugPrint("Hello nxdk!\n");
1179
Sleep(2000);
1280
}
1381

0 commit comments

Comments
 (0)