Skip to content
This repository has been archived by the owner on May 2, 2018. It is now read-only.

Commit

Permalink
runtime: implement pprof support for windows
Browse files Browse the repository at this point in the history
Credit to jp for proof of concept.

R=alex.brainman, jp, rsc, dvyukov
CC=golang-dev
https://golang.org/cl/4960057
  • Loading branch information
hectorchu authored and alexbrainman committed Sep 17, 2011
1 parent 44f12eb commit 9fd2687
Show file tree
Hide file tree
Showing 10 changed files with 192 additions and 34 deletions.
3 changes: 3 additions & 0 deletions src/pkg/runtime/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ struct G
uintptr sigcode1;
uintptr sigpc;
uintptr gopc; // pc of go statement that created this goroutine
uintptr end[];
};
struct M
{
Expand Down Expand Up @@ -253,9 +254,11 @@ struct M
uint32 fflag; // floating point compare flags

#ifdef __WINDOWS__
void* thread; // thread handle
void* event; // event for signalling
M* nextwaitm; // next M waiting for lock
#endif
uintptr end[];
};

struct Stktop
Expand Down
4 changes: 4 additions & 0 deletions src/pkg/runtime/windows/386/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ enum {
PROT_EXEC = 0x4,
MAP_ANON = 0x1,
MAP_PRIVATE = 0x2,
DUPLICATE_SAME_ACCESS = 0x2,
THREAD_PRIORITY_HIGHEST = 0x2,
SIGINT = 0x2,
CTRL_C_EVENT = 0,
CTRL_BREAK_EVENT = 0x1,
CONTEXT_CONTROL = 0x10001,
CONTEXT_FULL = 0x10007,
EXCEPTION_ACCESS_VIOLATION = 0xc0000005,
EXCEPTION_BREAKPOINT = 0x80000003,
EXCEPTION_FLT_DENORMAL_OPERAND = 0xc000008d,
Expand Down
6 changes: 2 additions & 4 deletions src/pkg/runtime/windows/386/signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,7 @@ runtime·sighandler(ExceptionRecord *info, void *frame, Context *r)
}

void
runtime·resetcpuprofiler(int32 hz)
runtime·dosigprof(Context *r, G *gp)
{
// TODO: Enable profiling interrupts.

m->profilehz = hz;
runtime·sigprof((uint8*)r->Eip, (uint8*)r->Esp, nil, gp);
}
43 changes: 31 additions & 12 deletions src/pkg/runtime/windows/386/sys.s
Original file line number Diff line number Diff line change
Expand Up @@ -96,31 +96,52 @@ TEXT runtime·sigtramp1(SB),0,$16-40
sigdone:
RET

// Windows runs the ctrl handler in a new thread.
TEXT runtime·ctrlhandler(SB),7,$0
PUSHL $runtime·ctrlhandler1(SB)
CALL runtime·externalthreadhandler(SB)
MOVL 4(SP), CX
ADDL $12, SP
JMP CX

TEXT runtime·profileloop(SB),7,$0
PUSHL $runtime·profileloop1(SB)
CALL runtime·externalthreadhandler(SB)
MOVL 4(SP), CX
ADDL $12, SP
JMP CX

TEXT runtime·externalthreadhandler(SB),7,$0
PUSHL BP
MOVL SP, BP
PUSHL BX
PUSHL SI
PUSHL DI
PUSHL 0x2c(FS)
MOVL SP, BX
MOVL SP, DX

// setup dummy m, g
SUBL $(m_fflag+4), SP // at least space for m_fflag
SUBL $m_end, SP // space for M
MOVL SP, 0(SP)
MOVL $m_end, 4(SP)
CALL runtime·memclr(SB) // smashes AX,BX,CX

LEAL m_tls(SP), CX
MOVL CX, 0x2c(FS)
MOVL SP, m(CX)
MOVL SP, DX
SUBL $8, SP // space for g_stack{guard,base}
MOVL SP, BX
SUBL $g_end, SP // space for G
MOVL SP, g(CX)
MOVL SP, m_g0(DX)
MOVL SP, m_g0(BX)

MOVL SP, 0(SP)
MOVL $g_end, 4(SP)
CALL runtime·memclr(SB) // smashes AX,BX,CX
LEAL -4096(SP), CX
MOVL CX, g_stackguard(SP)
MOVL BX, g_stackbase(SP)
MOVL DX, g_stackbase(SP)

PUSHL 8(BP)
CALL runtime·ctrlhandler1(SB)
PUSHL 16(BP) // arg for handler
CALL 8(BP)
POPL CX

get_tls(CX)
Expand All @@ -131,9 +152,7 @@ TEXT runtime·ctrlhandler(SB),7,$0
POPL SI
POPL BX
POPL BP
MOVL 0(SP), CX
ADDL $8, SP
JMP CX
RET

// Called from dynamic function created by ../thread.c compilecallback,
// running on Windows stack (not Go stack).
Expand Down
4 changes: 4 additions & 0 deletions src/pkg/runtime/windows/amd64/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ enum {
PROT_EXEC = 0x4,
MAP_ANON = 0x1,
MAP_PRIVATE = 0x2,
DUPLICATE_SAME_ACCESS = 0x2,
THREAD_PRIORITY_HIGHEST = 0x2,
SIGINT = 0x2,
CTRL_C_EVENT = 0,
CTRL_BREAK_EVENT = 0x1,
CONTEXT_CONTROL = 0x100001,
CONTEXT_FULL = 0x10000b,
EXCEPTION_ACCESS_VIOLATION = 0xc0000005,
EXCEPTION_BREAKPOINT = 0x80000003,
EXCEPTION_FLT_DENORMAL_OPERAND = 0xc000008d,
Expand Down
6 changes: 2 additions & 4 deletions src/pkg/runtime/windows/amd64/signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,7 @@ runtime·sighandler(ExceptionRecord *info, Context *r, G *gp)
}

void
runtime·resetcpuprofiler(int32 hz)
runtime·dosigprof(Context *r, G *gp)
{
// TODO: Enable profiling interrupts.

m->profilehz = hz;
runtime·sigprof((uint8*)r->Rip, (uint8*)r->Rsp, nil, gp);
}
40 changes: 30 additions & 10 deletions src/pkg/runtime/windows/amd64/sys.s
Original file line number Diff line number Diff line change
Expand Up @@ -100,31 +100,51 @@ TEXT runtime·sigtramp(SB),7,$56
sigdone:
RET

// Windows runs the ctrl handler in a new thread.
TEXT runtime·ctrlhandler(SB),7,$0
TEXT runtime·ctrlhandler(SB),7,$8
MOVQ CX, 16(SP) // spill
MOVQ $runtime·ctrlhandler1(SB), CX
MOVQ CX, 0(SP)
CALL runtime·externalthreadhandler(SB)
RET

TEXT runtime·profileloop(SB),7,$8
MOVQ $runtime·profileloop1(SB), CX
MOVQ CX, 0(SP)
CALL runtime·externalthreadhandler(SB)
RET

TEXT runtime·externalthreadhandler(SB),7,$0
PUSHQ BP
MOVQ SP, BP
PUSHQ BX
PUSHQ SI
PUSHQ DI
PUSHQ 0x58(GS)
MOVQ SP, BX
MOVQ SP, DX

// setup dummy m, g
SUBQ $(m_fflag+4), SP // at least space for m_fflag
SUBQ $m_end, SP // space for M
MOVQ SP, 0(SP)
MOVQ $m_end, 8(SP)
CALL runtime·memclr(SB) // smashes AX,BX,CX

LEAQ m_tls(SP), CX
MOVQ CX, 0x58(GS)
MOVQ SP, m(CX)
MOVQ SP, DX
SUBQ $16, SP // space for g_stack{guard,base}
MOVQ SP, BX
SUBQ $g_end, SP // space for G
MOVQ SP, g(CX)
MOVQ SP, m_g0(DX)
MOVQ SP, m_g0(BX)

MOVQ SP, 0(SP)
MOVQ $g_end, 8(SP)
CALL runtime·memclr(SB) // smashes AX,BX,CX
LEAQ -8192(SP), CX
MOVQ CX, g_stackguard(SP)
MOVQ BX, g_stackbase(SP)
MOVQ DX, g_stackbase(SP)

PUSHQ 16(BP)
CALL runtime·ctrlhandler1(SB)
PUSHQ 32(BP) // arg for handler
CALL 16(BP)
POPQ CX

get_tls(CX)
Expand Down
6 changes: 6 additions & 0 deletions src/pkg/runtime/windows/defs.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@ enum {
$MAP_ANON = 1,
$MAP_PRIVATE = 2,

$DUPLICATE_SAME_ACCESS = DUPLICATE_SAME_ACCESS,
$THREAD_PRIORITY_HIGHEST = THREAD_PRIORITY_HIGHEST,

$SIGINT = SIGINT,
$CTRL_C_EVENT = CTRL_C_EVENT,
$CTRL_BREAK_EVENT = CTRL_BREAK_EVENT,

$CONTEXT_CONTROL = CONTEXT_CONTROL,
$CONTEXT_FULL = CONTEXT_FULL,

$EXCEPTION_ACCESS_VIOLATION = STATUS_ACCESS_VIOLATION,
$EXCEPTION_BREAKPOINT = STATUS_BREAKPOINT,
$EXCEPTION_FLT_DENORMAL_OPERAND = STATUS_FLOAT_DENORMAL_OPERAND,
Expand Down
4 changes: 2 additions & 2 deletions src/pkg/runtime/windows/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ extern void *runtime·GetProcAddress;
void runtime·asmstdcall(void *c);
void *runtime·stdcall(void *fn, int32 count, ...);

uintptr runtime·getlasterror(void);
void runtime·setlasterror(uintptr err);
uint32 runtime·getlasterror(void);
void runtime·setlasterror(uint32 err);

// Function to be called by windows CreateThread
// to start new os thread.
Expand Down
Loading

0 comments on commit 9fd2687

Please sign in to comment.