内核中的源码
//"fs/read_write.c"
372 SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
373 {
374 struct file *file;
375 ssize_t ret = -EBADF;
376 int fput_needed;
377
378 file = fget_light(fd, &fput_needed);
379 if (file) {
380 loff_t pos = file_pos_read(file);
381 ret = vfs_read(file, buf, count, &pos);
382 file_pos_write(file, pos);
383 fput_light(file, fput_needed);
384 }
385
386 return ret;
387 }
//:cs find t define SYSCALL_DEFINE3或者Ctrl+]
//"include/linux/syscalls.h"
259 #define SYSCALL_DEFINE1(name, ...) SYSCALL_DEFINEx(1, _##name, __VA_ARGS__)
260 #define SYSCALL_DEFINE2(name, ...) SYSCALL_DEFINEx(2, _##name, __VA_ARGS__)
261 #define SYSCALL_DEFINE3(name, ...) SYSCALL_DEFINEx(3, _##name, __VA_ARGS__)
262 #define SYSCALL_DEFINE4(name, ...) SYSCALL_DEFINEx(4, _##name, __VA_ARGS__)
263 #define SYSCALL_DEFINE5(name, ...) SYSCALL_DEFINEx(5, _##name, __VA_ARGS__)
264 #define SYSCALL_DEFINE6(name, ...) SYSCALL_DEFINEx(6, _##name, __VA_ARGS__)
==>
//"include/linux/syscalls.h"
280 #ifdef CONFIG_FTRACE_SYSCALLS
281 #define SYSCALL_DEFINEx(x, sname, ...) \
282 static const char *types_##sname[] = { \
283 __SC_STR_TDECL##x(__VA_ARGS__) \
284 }; \
285 static const char *args_##sname[] = { \
286 __SC_STR_ADECL##x(__VA_ARGS__) \
287 }; \
288 SYSCALL_METADATA(sname, x); \
289 __SYSCALL_DEFINEx(x, sname, __VA_ARGS__)
290 #else
291 #define SYSCALL_DEFINEx(x, sname, ...) \
292 __SYSCALL_DEFINEx(x, sname, __VA_ARGS__)
293 #endif
==>
//"include/linux/syscalls.h"
295 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
296
297 #define SYSCALL_DEFINE(name) static inline long SYSC_##name
298
299 #define __SYSCALL_DEFINEx(x, name, ...) \
300 asmlinkage long sys##name(__SC_DECL##x(__VA_ARGS__)); \
301 static inline long SYSC##name(__SC_DECL##x(__VA_ARGS__)); \
302 asmlinkage long SyS##name(__SC_LONG##x(__VA_ARGS__)) \
303 { \
304 __SC_TEST##x(__VA_ARGS__); \
305 return (long) SYSC##name(__SC_CAST##x(__VA_ARGS__)); \
306 } \
307 SYSCALL_ALIAS(sys##name, SyS##name); \
308 static inline long SYSC##name(__SC_DECL##x(__VA_ARGS__))
309
310 #else /* CONFIG_HAVE_SYSCALL_WRAPPERS */
311
312 #define SYSCALL_DEFINE(name) asmlinkage long sys_##name
313 #define __SYSCALL_DEFINEx(x, name, ...) \
314 asmlinkage long sys##name(__SC_DECL##x(__VA_ARGS__))
315
316 #endif /* CONFIG_HAVE_SYSCALL_WRAPPERS */
#define SYSCALL_DEFINEx(x, name, ...)
asmlinkage long sys##name(__SC_DECL##x(__VA_ARGS__))
下面进行以下三个宏的跟踪:
__SC_DECL
__SC_CAST
__SC_LONG
//:cs find t __SC_DECL
//include/linux/syscalls.h"
71
72 #define __SC_DECL1(t1, a1) t1 a1
73 #define __SC_DECL2(t2, a2, ...) t2 a2, __SC_DECL1(__VA_ARGS__)
74 #define __SC_DECL3(t3, a3, ...) t3 a3, __SC_DECL2(__VA_ARGS__)
75 #define __SC_DECL4(t4, a4, ...) t4 a4, __SC_DECL3(__VA_ARGS__)
76 #define __SC_DECL5(t5, a5, ...) t5 a5, __SC_DECL4(__VA_ARGS__)
77 #define __SC_DECL6(t6, a6, ...) t6 a6, __SC_DECL5(__VA_ARGS__)
78
79 #define __SC_LONG1(t1, a1) long a1
80 #define __SC_LONG2(t2, a2, ...) long a2, __SC_LONG1(__VA_ARGS__)
81 #define __SC_LONG3(t3, a3, ...) long a3, __SC_LONG2(__VA_ARGS__)
82 #define __SC_LONG4(t4, a4, ...) long a4, __SC_LONG3(__VA_ARGS__)
83 #define __SC_LONG5(t5, a5, ...) long a5, __SC_LONG4(__VA_ARGS__)
84 #define __SC_LONG6(t6, a6, ...) long a6, __SC_LONG5(__VA_ARGS__)
85
86 #define __SC_CAST1(t1, a1) (t1) a1
87 #define __SC_CAST2(t2, a2, ...) (t2) a2, __SC_CAST1(__VA_ARGS__)
88 #define __SC_CAST3(t3, a3, ...) (t3) a3, __SC_CAST2(__VA_ARGS__)
89 #define __SC_CAST4(t4, a4, ...) (t4) a4, __SC_CAST3(__VA_ARGS__)
90 #define __SC_CAST5(t5, a5, ...) (t5) a5, __SC_CAST4(__VA_ARGS__)
91 #define __SC_CAST6(t6, a6, ...) (t6) a6, __SC_CAST5(__VA_ARGS__)
可以看到这些都是递归宏
以read系统调用为例进行分析:
它的定义为:
SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
==>
经过如下宏的转换
313 #define __SYSCALL_DEFINEx(x, name, ...) \
314 asmlinkage long sys##name(__SC_DECL##x(__VA_ARGS__))
==>
asmlinkage long sys_read(__SC_DECL3(__VA_ARGS__))
==>
其中:__SC_DECL3(__VA_ARGS__)
==>
__SC_DECL3(unsigned int, fd, char __user *, buf, size_t, count)
==>
完全函数参数原型
asmlinkage long sys_read(unsigned int fd,char __user * buf, size_t count)