linux pause函数,linux – 为什么我在调用sys_pause系统调用时...

我正在尝试创建一个x86_64汇编程序,只要发送SIGTERM信号,就会显示“SIGTERM received”.我的应用程序直接使用Linux系统调用:

%define sys_write 0x01

%define sys_rt_sigaction 0x0d

%define sys_pause 0x22

%define sys_exit 0x3c

%define SIGTERM 0x0f

%define STDOUT 0x01

; Definition of sigaction struct for sys_rt_sigaction

struc sigaction

.sa_handler resq 1

.sa_flags resq 1

.sa_restorer resq 1

.sa_mask resq 1

endstruc

section .data

; Message shown when a syscall fails

error_msg db 'syscall error', 0x0a

error_msg_len equ $- error_msg

; Message shown when SIGTERM is received

sigterm_msg db 'SIGTERM received', 0x0a

sigterm_msg_len equ $- sigterm_msg

section .bss

act resb sigaction_size

val resd 1

section .text

global _start

_start:

; Initialize act

lea rax, [handler]

mov [act + sigaction.sa_handler], rax

; Set the handler

mov rax, sys_rt_sigaction

mov rdi, SIGTERM

lea rsi, [act]

mov rdx, 0x00

mov r10, 0x08

syscall

; Ensure the syscall succeeded

cmp rax, 0

jne error

; Pause until a signal is received

mov rax, sys_pause

syscall

; Upon success, jump to exit

jmp exit

error:

; Display an error message

mov rax, sys_write

mov rdi, STDOUT

mov rsi, error_msg

mov rdx, error_msg_len

syscall

; Set the return value to one

mov dword [val], 0x01

exit:

; Terminate the application gracefully

mov rax, sys_exit

mov rdi, [val]

syscall

handler:

; Display a message

mov rax, sys_write

mov rdi, STDOUT

mov rsi, sigterm_msg

mov rdx, sigterm_msg_len

syscall

ret

当我运行应用程序时,它会在sys_pause系统调用中挂起(如预期的那样),但是当我发送SIGTERM信号时,它会因分段错误而崩溃.

所以我将应用程序加载到GDB中以弄清楚发生了什么:

(gdb) break _start

Breakpoint 1 at 0x4000b0

(gdb) run

Starting program: [...]

Breakpoint 1, 0x00000000004000b0 in _start ()

(gdb) info proc

process 9639

(gdb) continue

Continuing.

GDB会话挂起,然后我打开另一个终端并运行kill SIGTERM 9639.这导致以下输出:

Program received signal SIGTERM, Terminated.

0x00000000004000ec in _start ()

然后我跑了:

(gdb) disas _start

Dump of assembler code for function _start:

0x00000000004000b0 : lea 0x400123,%rax

0x00000000004000b8 : mov %rax,0x600160

0x00000000004000c0 : mov $0xd,%eax

0x00000000004000c5 : mov $0xf,%edi

0x00000000004000ca : lea 0x600160,%rsi

0x00000000004000d2 : mov $0x0,%edx

0x00000000004000d7 : mov $0x8,%r10d

0x00000000004000dd : syscall

0x00000000004000df : cmp $0x0,%rax

0x00000000004000e3 : jne 0x4000ee

0x00000000004000e5 : mov $0x22,%eax

0x00000000004000ea : syscall

=> 0x00000000004000ec : jmp 0x400114

End of assembler dump.

然后我继续申请:

(gdb) continue

Continuing.

Program received signal SIGSEGV, Segmentation fault.

0x00000000004000ec in _start ()

永远不会调用信号处理程序,并且应用程序已崩溃.

我究竟做错了什么?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值