2 * Macros to hook functions.
4 * Copyright (C) 2013 Simon Ruderich
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 /* Hook the function by creating a function with the same name. With
24 * LD_PRELOAD our function will be preferred. The original function is stored
25 * in a static variable (real_*). */
27 #define _HOOK_PRE(type, name) \
29 DLSYM_FUNCTION(real_ ## name, #name);
30 #define _HOOK_PRE_FD(type, name, fd) \
32 _HOOK_PRE_FD_(type, name, fd)
33 #define _HOOK_PRE_FD_(type, name, fd) \
34 _HOOK_PRE(type, name) \
35 handle = check_handle_fd(fd); \
39 #define _HOOK_PRE_FILE(type, name, file) \
41 _HOOK_PRE(type, name) \
42 handle = check_handle_fd(fileno(file)); \
44 handle_file_pre(file); \
46 #define _HOOK_POST_FD_(fd) \
50 #define _HOOK_POST_FD(fd) \
53 #define _HOOK_POST_FILE(file) \
55 handle_file_post(file); \
60 #define HOOK_VOID1(type, name, fd, type1, arg1) \
61 static type (*real_ ## name)(type1); \
62 type name(type1 arg1) { \
63 _HOOK_PRE_FD_(type, name, fd) \
64 real_ ## name(arg1); \
68 #define HOOK_FD3(type, name, fd, type1, arg1, type2, arg2, type3, arg3) \
69 static type (*real_ ## name)(type1, type2, type3); \
70 type name(type1 arg1, type2 arg2, type3 arg3) { \
71 _HOOK_PRE_FD(type, name, fd) \
72 result = real_ ## name(arg1, arg2, arg3); \
76 #define HOOK_FILE1(type, name, file, type1, arg1) \
77 static type (*real_ ## name)(type1); \
78 type name(type1 arg1) { \
79 _HOOK_PRE_FILE(type, name, file) \
80 result = real_ ## name(arg1); \
81 _HOOK_POST_FILE(file) \
83 #define HOOK_FILE2(type, name, file, type1, arg1, type2, arg2) \
84 static type (*real_ ## name)(type1, type2); \
85 type name(type1 arg1, type2 arg2) { \
86 _HOOK_PRE_FILE(type, name, file) \
87 result = real_ ## name(arg1, arg2); \
88 _HOOK_POST_FILE(file) \
90 #define HOOK_FILE3(type, name, file, type1, arg1, type2, arg2, type3, arg3) \
91 static type (*real_ ## name)(type1, type2, type3); \
92 type name(type1 arg1, type2 arg2, type3 arg3) { \
93 _HOOK_PRE_FILE(type, name, file) \
94 result = real_ ## name(arg1, arg2, arg3); \
95 _HOOK_POST_FILE(file) \
97 #define HOOK_FILE4(type, name, file, type1, arg1, type2, arg2, type3, arg3, type4, arg4) \
98 static type (*real_ ## name)(type1, type2, type3, type4); \
99 type name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) { \
100 _HOOK_PRE_FILE(type, name, file) \
101 result = real_ ## name(arg1, arg2, arg3, arg4); \
102 _HOOK_POST_FILE(file) \
105 #define HOOK_VAR_FILE1(type, name, file, func, type1, arg1) \
106 static type (*real_ ## func)(type1, va_list); \
107 type name(type1 arg1, ...) { \
109 _HOOK_PRE_FILE(type, func, file) \
110 va_start(ap, arg1); \
111 result = real_ ## func(arg1, ap); \
113 _HOOK_POST_FILE(file) \
115 #define HOOK_VAR_FILE2(type, name, file, func, type1, arg1, type2, arg2) \
116 static type (*real_ ## func)(type1, type2, va_list); \
117 type name(type1 arg1, type2 arg2, ...) { \
119 _HOOK_PRE_FILE(type, func, file) \
120 va_start(ap, arg2); \
121 result = real_ ## func(arg1, arg2, ap); \
123 _HOOK_POST_FILE(file) \
125 #define HOOK_VAR_FILE3(type, name, file, func, type1, arg1, type2, arg2, type3, arg3) \
126 static type (*real_ ## func)(type1, type2, type3, va_list); \
127 type name(type1 arg1, type2 arg2, type3 arg3, ...) \
130 _HOOK_PRE_FILE(type, func, file) \
131 va_start(ap, arg3); \
132 result = real_ ## func(arg1, arg2, arg3, ap); \
134 _HOOK_POST_FILE(file) \