The code below returns correct pid value and then crashes. I don't understand the reason. The platform is x86 CPU android emulator (Nexus 5X).
long pid;
__asm__ __volatile__ ("int $0x80" : "=a"(pid) : "0"(__NR_getpid));
printf("Pid : %d", pid);
Here is the output:
D/APP: Pid:8261
A/libc: Fatal signal 31 (SIGSYS), code 1 in tid 8261 (.APP), pid 8261 (.APP)
Application terminated.
Can anybody give an idea?
Edit:
I have tried to replace "0"(__NR_getpid)
with "a"(__NR_getpid)
and got the same result.
I was calling the same syscall with __NR_getppid
too, once I removed second call, I saw that it was not crashing anymore.
Platform: Android Studio - NDK Clang x86 (i386)
Test code:
#include <syscall.h>
#include <stdbool.h>
#include <stdint.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
static inline long _syscall0(long n)
{
long __ret;
__asm__ __volatile__ ("int $0x80" : "=a"(__ret) : "0"(n));
return __ret;
}
int main(void)
{
int pid = (int)_syscall0(__NR_getpid);
printf("Pid:%d", pid);
int ppid = (int)_syscall0(__NR_getppid); // <--- crashing here
printf("PPid:%d", ppid);
return 0;
}
Compiler assembly output : https://godbolt.org/g/ibmxTb
Edit2 : Tried replacing ppid with pid, so it became 2 pids but still crashing.