Reprint: http://blog.chinaunix.net/uid-29401328-id-4866849.html
Said before glibc and standard C library functions and system calls, but always feel a little shoe scratch itch, the following to trace the system calls the source code. (linux-3.3 kernel, glibc-2.17 source)
System calls take the open function as an example
How do you check it?
Using Man 2 open to view the use of open, see need to contain three header files Sys/types.h, sys/stat.h, Fcntl.h, looked at the first two, irrelevant,
It seems to be fcntl.h, but this header file also does not give the definition of open, but this header file contains a header file io/fcntl.h, followed, see 168 lines:
extern int open (const char *__file, int __oflag, ...) __nonnull ((1));
The Open function is introduced to find the definition of it (I use the source insight, just hold down CTRL and click the function to jump to the place where the function is defined),There are three macros two definitions, OLDFILEOPS.C and FILEOPS.C parameters do not match, another macro can not, fcntl2.h is not like. It should be loadmsgcat.c.In the
# define open (name, flags) open_not_cancel_2 (name, flags)
At this point open_not_cancel_2 corresponds to glibc-2.17\sysdeps\unix\sysv\linux\not-cancel.h 26 lines
#define OPEN_NOT_CANCEL_2 (name, flags) \
Inline_syscall (Open, 2, (const char *) (name), (flags))
The Inline_syscall package is different for each hardware platform, for ARM (in Glibc-2.17\ports\sysdeps\unix\sysv\linux\arm\sysdep.h)
#define Inline_syscall (name, nr, args ...) \
({unsigned int _sys_result = Internal_syscall (name, nr, args); \
if (__builtin_expect (Internal_syscall_error_p (_sys_result,), 0)) \
{\
__set_errno (Internal_syscall_errno (_sys_result,));
_sys_result = (unsigned int) -1;\
}\
(int) _sys_result; })
Keep track of Internal_syscall in the macro above
#else/* ARM */
# undef Internal_syscall_raw
# define INTERNAL_SYSCALL_RAW (name, err, nr, args ...) \
({\
Register int _a1 asm ("R0"), _nr asm ("R7"); \
load_args_# #nr (ARGS) \
_NR = name;\
ASM volatile ("swi0x0@ syscall" #name \
: "=r" (_A1) \
: "R" (_nr) asm_args_# #nr \
: "Memory"); \
_A1; })
#endif
See the above Embedded assembly instruction SWI exception, it will be implemented according to the Exception vector table system calls, NR represents the number of system calls to be implemented , each system call corresponding to a number. For SWI exceptions and system calls see other blog posts, just know that this assembly instruction can call the system call function defined in the kernel according to the number of the system call function that we passed in, starting from here, the system call goes into the kernel state. Why does it go into the kernel state and what does it do after it enters the kernel state?
this involves SWI exception handling, see post later.
System calls from GLIBC Library to kernel trace (open function)