2014 05 16 01 43 [c] 寫個 infinite loop 用來 debug.

因為知道某個function有問題,
又不好直接用debugger去debug,
只好先讓它infinite loop,
之後再用debugger attach 那個 process 來 debug.
所以才寫了一個 INFINITE_LOOP 的 macro 來用.

PS: 以下是用MinGW gcc 編譯的.


#define strfy(line) #line
#define _INFINITE_LOOP(line) __asm__ __volatile__ ("line_" strfy(line) ":\njmp line_" strfy(line) );
#define INFINITE_LOOP() _INFINITE_LOOP(__LINE__)

void start()
{
    INFINITE_LOOP();
    INFINITE_LOOP();
}


$ gcc -nostartfiles test_loop.c -Wl,-entry,_start

$ objdump.exe -d a.exe


a.exe:     file format pei-i386
Disassembly of section .text:
00401000 <_start>:
  401000:       55                      push   %ebp
  401001:       89 e5                   mov    %esp,%ebp

00401003 <line_9>:
  401003:       eb fe                   jmp    401003 <line_9>

00401005 <line_10>:
  401005:       eb fe                   jmp    401005 <line_10>
  401007:       5d                      pop    %ebp
  401008:       c3                      ret
  401009:       90                      nop
  40100a:       90                      nop
  40100b:       90                      nop