fix prevent violation
[framework/system/dynamic-analysis-probe.git] / probe_file / da_io_stdc.c
1 /*
2  *  DA probe
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: 
7  *
8  * Jaewon Lim <jaewon81.lim@samsung.com>
9  * Woojin Jung <woojin2.jung@samsung.com>
10  * Juyoung Kim <j0.kim@samsung.com>
11  * 
12  * This library is free software; you can redistribute it and/or modify it under
13  * the terms of the GNU Lesser General Public License as published by the
14  * Free Software Foundation; either version 2.1 of the License, or (at your option)
15  * any later version.
16  * 
17  * This library is distributed in the hope that it will be useful, but WITHOUT ANY
18  * WARRANTY; without even the implied warranty of MERCHANTABILITY or
19  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
20  * License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * along with this library; if not, write to the Free Software Foundation, Inc., 51
24  * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25  *
26  * Contributors:
27  * - S-Core Co., Ltd
28  * 
29  */
30
31 #include <stdio.h>
32 #include <stdarg.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include <dlfcn.h>
36 #include <errno.h>
37 #include "daprobe.h"
38 #include "probeinfo.h"
39 #include "dautil.h"
40 #include "dahelper.h"
41 #include "da_io.h"
42
43 static enum DaOptions _sopt = OPT_FILE;
44
45 FILE* fopen(const char* filename, const char* mode)
46 {
47         static FILE* (*fopenp)(const char* filename, const char* mode);
48         FILE* fret;
49         
50         BEFORE_ORIGINAL_FILE_NOFILTER(fopen, LIBC);
51         _filepath = (char*)filename;
52         
53         fret = fopenp(filename, mode);
54         
55         AFTER_ORIGINAL_FILEP_RET(VT_PTR, fret, 0, fret, FD_API_OPEN, "%s, %s", filename, mode);
56
57         return fret;
58 }
59
60 FILE* freopen(const char * filename, const char * mode, FILE * stream)
61 {
62         static FILE* (*freopenp)(const char * filename, const char * mode, FILE * stream);
63         FILE* fret;
64
65         BEFORE_ORIGINAL_FILE_NOFILTER(freopen, LIBC);
66         _filepath = (char*)filename;
67
68         fret = freopenp(filename, mode, stream);
69
70         AFTER_ORIGINAL_FILEP_RET(VT_PTR, fret, 0, fret, FD_API_OPEN,
71                         "%s, %s, %p", filename, mode, stream);
72
73         return fret;
74 }
75
76 FILE* fdopen(int fildes, const char *mode)
77 {
78         static FILE* (*fdopenp)(int fildes, const char *mode);
79         FILE* fret;
80
81         BEFORE_ORIGINAL_FILE_NOFILTER(fdopen, LIBC);
82
83         fret = fdopenp(fildes, mode);
84
85         AFTER_ORIGINAL_FILEP_RET(VT_PTR, fret, 0, fret, FD_API_OPEN, "%d, %s", fildes, mode);
86
87         return fret;
88 }
89
90 int fflush(FILE* stream)
91 {
92         static int (*fflushp)(FILE* stream);
93
94         BEFORE_ORIGINAL_FILE(fflush, LIBC);
95         ret = fflushp(stream);
96         AFTER_ORIGINAL_FILEP(0, stream, FD_API_OTHER, "%p", stream);
97         return ret;
98 }
99
100 int fclose(FILE* stream)
101 {
102         static int (*fclosep)(FILE* stream);
103         DECLARE_VARIABLE_FD;
104
105         GET_REAL_FUNC(fclose, LIBC);
106
107         bfiltering = false;
108         PRE_PROBEBLOCK_BEGIN();
109         GET_FD_FROM_FILEP(stream);
110         _fstatret = fstat(_fd, &_statbuf);
111         PRE_PROBEBLOCK_END();
112
113         ret = fclosep(stream);
114
115         POST_PROBEBLOCK_BEGIN(LC_RESOURCE, VT_INT, ret, "%p", stream);
116         POST_PROBEBLOCK_MIDDLE_FD(0, _fd, FD_API_CLOSE);
117         POST_PROBEBLOCK_CALLSTACK_RESOURCE(FD_API_CLOSE);
118         POST_PROBEBLOCK_END();
119
120         return ret;
121 }
122
123 int remove(const char* filename)
124 {
125         static int (*removep)(const char* filename);
126
127         BEFORE_ORIGINAL_FILE(remove, LIBC);
128         _filepath = (char*)filename;
129         ret = removep(filename);
130         AFTER_ORIGINAL_NOFD(0, FD_API_DIRECTORY, "%s", filename);
131         return ret;
132 }
133
134 int rename(const char* oldname, const char* newname)
135 {
136         static int (*renamep)(const char* oldname, const char* newname);
137
138         BEFORE_ORIGINAL_FILE(rename, LIBC);
139         _filepath = (char*)newname;
140         ret = renamep(oldname, newname);
141         AFTER_ORIGINAL_NOFD(0, FD_API_DIRECTORY, "%s, %s", oldname, newname);
142         return ret;
143 }
144
145 FILE * tmpfile ( void )
146 {
147         static FILE* (*tmpfilep) ( void );
148         FILE* fret;
149
150         BEFORE_ORIGINAL_FILE_NOFILTER(tmpfile, LIBC);
151         _filepath = "<temp file>";
152         fret = tmpfilep();
153
154         AFTER_ORIGINAL_FILEP_RET(VT_PTR, fret, 0, fret, FD_API_OPEN, "%s", "");
155
156         return fret;
157 }
158
159 int fgetpos(FILE* stream, fpos_t* position)
160 {
161         static int (*fgetposp)(FILE* stream, fpos_t* position);
162
163         BEFORE_ORIGINAL_FILE(fgetpos, LIBC);
164         ret = fgetposp(stream, position);
165         AFTER_ORIGINAL_FILEP(0, stream, FD_API_OTHER, "%p, %p", stream, position);
166         return ret;
167 }
168
169 int fseek(FILE* stream, long int offset, int origin)
170 {
171         static int (*fseekp)(FILE* stream, long int offset, int origin);
172
173         BEFORE_ORIGINAL_FILE(fseek, LIBC);
174         ret = fseekp(stream, offset, origin);
175         AFTER_ORIGINAL_FILEP((unsigned int)offset, stream, FD_API_OTHER,
176                         "%p, %ld, %d", stream, offset, origin);
177         return ret;
178 }
179
180 int fsetpos(FILE* stream, const fpos_t* pos)
181 {
182         static int (*fsetposp)(FILE* stream, const fpos_t* pos);
183
184         BEFORE_ORIGINAL_FILE(fsetpos, LIBC);
185         ret = fsetposp(stream, pos);
186         AFTER_ORIGINAL_FILEP(0, stream, FD_API_OTHER, "%p, %p", stream, pos);
187         return ret;
188 }
189
190 long int ftell(FILE* stream)
191 {
192         static long int (*ftellp)(FILE* stream);
193         long int lret;
194
195         BEFORE_ORIGINAL_FILE(ftell, LIBC);
196         
197         lret = ftellp(stream);
198
199         AFTER_ORIGINAL_FILEP_RET(VT_LONG, lret, 0, stream, FD_API_OTHER, "%p", stream);
200
201         return lret;
202 }
203
204 void rewind(FILE* stream)
205 {
206         static void (*rewindp)(FILE* stream);
207
208         BEFORE_ORIGINAL_FILE(rewind, LIBC);
209
210         rewindp(stream);
211         
212         AFTER_ORIGINAL_FILEP_RET(VT_NULL, NULL, 0, stream, FD_API_OTHER, "%p", stream);
213 }
214
215 void clearerr(FILE* stream)
216 {
217         static void (*clearerrp)(FILE* stream);
218
219         BEFORE_ORIGINAL_FILE(clearerr, LIBC);
220
221         clearerrp(stream);
222         
223         AFTER_ORIGINAL_FILEP_RET(VT_NULL, NULL, 0, stream, FD_API_OTHER, "%p", stream);
224 }
225
226 int feof(FILE* stream)
227 {
228         static int (*feofp)(FILE* stream);
229
230         BEFORE_ORIGINAL_FILE(feof, LIBC);
231         ret = feofp(stream);
232         AFTER_ORIGINAL_FILEP(0, stream, FD_API_OTHER, "%p", stream);
233         return ret;
234 }
235
236 int ferror(FILE* stream)
237 {
238         static int (*ferrorp)(FILE* stream);
239
240         BEFORE_ORIGINAL_FILE(ferror, LIBC);
241         ret = ferrorp(stream);
242         AFTER_ORIGINAL_FILEP(0, stream, FD_API_OTHER, "%p", stream);
243         return ret;
244 }
245
246 int fileno(FILE* stream)
247 {
248         static int (*filenop)(FILE* stream);
249
250         BEFORE_ORIGINAL_FILE(fileno, LIBC);
251         ret = filenop(stream);
252         AFTER_ORIGINAL_FILEP(0, stream, FD_API_OTHER, "%p", stream);
253         return ret;
254 }
255
256 void perror(const char* string)
257 {
258         static void (*perrorp)(const char* string);
259
260         BEFORE_ORIGINAL_FILE(perror, LIBC);
261
262         perrorp(string);
263
264         AFTER_ORIGINAL_NOFD_RET(VT_NULL, NULL, 0, FD_API_OTHER, "%s", string);
265 }
266
267 // *******************************************************************
268 // File read / write APIs
269 // *******************************************************************
270
271 int vfprintf(FILE* stream, const char* format, va_list arg)
272 {
273         static int (*vfprintfp)(FILE* stream, const char* format, va_list arg);
274
275         BEFORE_ORIGINAL_FILE(vfprintf, LIBC);
276         ret = vfprintfp(stream, format, arg);
277         AFTER_ORIGINAL_FILEP(ret, stream, FD_API_WRITE, "%p, %s", stream, format);
278         return ret;
279 }
280
281 int vfscanf(FILE* stream, const char* format, va_list arg)
282 {
283         static int (*vfscanfp)(FILE* stream, const char* format, va_list arg);
284
285         BEFORE_ORIGINAL_FILE(vfscanf, LIBC);
286         ret = vfscanfp(stream, format, arg);
287         AFTER_ORIGINAL_FILEP(ret, stream, FD_API_READ, "%p, %s", stream, format);
288         return ret;
289 }
290
291 int fgetc(FILE* stream)
292 {
293         static int (*fgetcp)(FILE* stream);
294
295         BEFORE_ORIGINAL_FILE(fgetc, LIBC);
296         ret = fgetcp(stream);
297         AFTER_ORIGINAL_FILEP((ret == EOF ? 0 : 1), stream, FD_API_READ, "%p", stream);
298         return ret;
299 }
300
301 #if 0   // why is this commented?
302 char* fgets(char* str, int size, FILE* stream)
303 {
304         static char* (*fgetsp)(char* str, int num, FILE* stream);
305         char* cret;
306
307         BEFORE_ORIGINAL_FILE(fgets, LIBC);
308         
309         cret = fgetsp(str, size, stream);
310
311         AFTER_ORIGINAL_FILEP_RET(VT_STR, cret, (ret == NULL ? 0 : strlen(cret)),
312                         stream, FD_API_READ, "%s, %d, %p", str, size, stream);
313
314         return cret;
315 }
316 #endif
317
318 int fputc(int character, FILE* stream)
319 {
320         static int (*fputcp)(int character, FILE* stream);
321
322         BEFORE_ORIGINAL_FILE(fputc, LIBC);
323         ret = fputcp(character, stream);
324         AFTER_ORIGINAL_FILEP((ret == EOF ? 0 : 1), stream, FD_API_WRITE,
325                         "%d, %p", character, stream);
326         return ret;
327 }
328
329 int fputs(const char* str, FILE* stream)
330 {
331         static int (*fputsp)(const char* str, FILE* stream);
332
333         BEFORE_ORIGINAL_FILE(fputs, LIBC);
334         ret = fputsp(str, stream);
335         AFTER_ORIGINAL_FILEP(ret, stream, FD_API_WRITE, "%s, %p", str, stream);
336         return ret;
337 }
338
339 int getc(FILE* stream)
340 {
341         static int (*getcp)(FILE* stream);
342
343         BEFORE_ORIGINAL_FILE(getc, LIBC);
344         ret = getcp(stream);
345         AFTER_ORIGINAL_FILEP((ret == EOF ? 0 : 1), stream, FD_API_READ, "%p", stream);
346         return ret;
347 }
348
349 int putc(int character, FILE* stream)
350 {
351         static int (*putcp)(int character, FILE* stream);
352
353         BEFORE_ORIGINAL_FILE(putc, LIBC);
354         ret = putcp(character, stream);
355         AFTER_ORIGINAL_FILEP((ret == EOF ? 0 : 1), stream, FD_API_WRITE,
356                         "%d, %p", character, stream);
357         return ret;
358 }
359
360 int ungetc(int character, FILE* stream)
361 {
362         static int (*ungetcp)(int character, FILE* stream);
363
364         BEFORE_ORIGINAL_FILE(ungetc, LIBC);
365         ret = ungetcp(character, stream);
366         AFTER_ORIGINAL_FILEP(0, stream, FD_API_OTHER, "%d, %p", character, stream);
367         return ret;
368 }
369
370 size_t fread(void* ptr, size_t size, size_t count, FILE* stream)
371 {
372         static size_t (*freadp)(void* ptr, size_t size, size_t count, FILE* stream);
373         size_t tret;
374
375         BEFORE_ORIGINAL_FILE(fread, LIBC);
376
377         tret = freadp(ptr, size, count, stream);
378         
379         AFTER_ORIGINAL_FILEP_RET(VT_SIZE_T, tret, 0, stream, FD_API_READ,
380                         "%p, %u, %u, %p", ptr, size, count, stream);
381         
382         return tret;
383 }
384
385 size_t fwrite(const void* ptr, size_t size, size_t count, FILE* stream)
386 {
387         static size_t (*fwritep)(const void* ptr, size_t size, size_t count, FILE* stream);
388         size_t tret;
389
390         BEFORE_ORIGINAL_FILE(fwrite, LIBC);
391
392         tret = fwritep(ptr, size, count, stream);
393
394         AFTER_ORIGINAL_FILEP_RET(VT_SIZE_T, tret, 0, stream, FD_API_WRITE,
395                         "%p, %u, %u, %p", ptr, size, count, stream);
396
397         return tret;
398 }
399
400 // *********************************************************
401 // variable parameter function
402 // *********************************************************
403 int fprintf(FILE* stream, const char* format, ...)
404 {
405         static int (*vfprintfp)(FILE* stream, const char* format, ...);
406
407         BEFORE_ORIGINAL_FILE(vfprintf, LIBC);
408
409         va_list arg;
410         va_start(arg, format);
411         ret = vfprintfp(stream, format, arg);
412
413         AFTER_ORIGINAL_FILEP(ret, stream, FD_API_WRITE, "%p, %s, ...", stream, format);
414         va_end(arg);
415
416         return ret;
417 }
418
419 int fscanf(FILE* stream, const char* format, ...)
420 {
421         static int (*vfscanfp)(FILE* stream, const char* format, ...);
422         
423         BEFORE_ORIGINAL_FILE(vfscanf, LIBC);
424
425         va_list arg;
426         va_start(arg, format);
427         ret = vfscanfp(stream, format, arg);
428
429         AFTER_ORIGINAL_FILEP(ret, stream, FD_API_READ, "%p, %s, ...", stream, format);
430         va_end(arg);
431
432         return ret;
433 }
434
435 #if !defined(DA_DEBUG_LOG) && !defined(PRINT_STDOUT)
436 int printf(const char* format, ...)
437 {
438         static int (*vprintfp)(const char* format, ...);
439         
440         BEFORE_ORIGINAL_FILE(vprintf, LIBC);
441
442         va_list arg;
443         va_start(arg, format);
444         ret = vprintfp(format, arg);
445
446         AFTER_ORIGINAL_NOFD(ret, FD_API_WRITE, "%s,...", format);
447         va_end(arg);
448
449         return ret;
450 }
451 #endif
452
453 int scanf(const char* format, ...)
454 {
455         static int (*vscanfp)(const char* format, ...);
456
457         BEFORE_ORIGINAL_FILE(vscanf, LIBC);
458
459         va_list arg;
460         va_start(arg, format);
461         ret = vscanfp(format, arg);
462
463         AFTER_ORIGINAL_NOFD(ret, FD_API_READ, "%s,...", format);
464         va_end(arg);
465
466         return ret;
467 }
468
469 int getchar()
470 {
471         static int (*getcharp)();
472
473         BEFORE_ORIGINAL_FILE(getchar, LIBC);
474         ret = getcharp();
475         AFTER_ORIGINAL_NOFD((ret == EOF ? 0 : 1), FD_API_READ, "%s", "");
476         return ret;
477 }
478
479 int putchar(int c)
480 {
481         static int (*putcharp)(int c);
482
483         BEFORE_ORIGINAL_FILE(putchar, LIBC);
484         ret = putcharp(c);
485         AFTER_ORIGINAL_NOFD((ret == EOF ? 0 : 1), FD_API_WRITE, "%d", c);
486         return ret;
487 }
488
489 char* gets(char* str)
490 {
491         static char* (*getsp)(char* str);
492         char* cret;
493
494         BEFORE_ORIGINAL_FILE(gets, LIBC);
495
496         cret = getsp(str);
497         
498         AFTER_ORIGINAL_NOFD_RET(VT_STR, cret, strlen(cret), FD_API_READ, "%s", str);
499
500         return cret;
501 }
502
503 #if !defined(DA_DEBUG_LOG) && !defined(PRINT_STDOUT)
504 int puts(const char* str)
505 {
506         static int (*putsp)(const char* str);
507
508         BEFORE_ORIGINAL_FILE(puts, LIBC);
509         ret = putsp(str);
510         AFTER_ORIGINAL_NOFD(ret, FD_API_WRITE, "%s", str);
511         return ret;
512 }
513 #endif
514
515 char* tmpnam(char* str)
516 {
517         static char* (*tmpnamp)(char* str);
518         char* cret;
519
520         BEFORE_ORIGINAL_FILE(tmpnam, LIBC);
521
522         cret = tmpnamp(str);
523
524         AFTER_ORIGINAL_NOFD_RET(VT_STR, cret, 0, FD_API_OTHER, "%s", str);
525
526         return cret;
527 }
528
529 void setbuf(FILE* stream, char* buf)
530 {
531         static void (*setbufp)(FILE* stream, char* buf);
532
533         BEFORE_ORIGINAL_FILE(setbuf, LIBC);
534
535         setbufp(stream, buf);
536
537         AFTER_ORIGINAL_FILEP_RET(VT_NULL, NULL, 0, stream, FD_API_OTHER,
538                         "%p, %p", stream, buf);
539 }
540
541 void setbuffer(FILE* stream, char* buf, size_t size)
542 {
543         static void (*setbufferp)(FILE* stream, char* buf, size_t size);
544
545         BEFORE_ORIGINAL_FILE(setbuffer, LIBC);
546
547         setbufferp(stream, buf, size);
548
549         AFTER_ORIGINAL_FILEP_RET(VT_NULL, NULL, size, stream, FD_API_OTHER,
550                         "%p, %p, %u", stream, buf, size);
551 }
552
553 void setlinebuf(FILE* stream)
554 {
555         static int (*setlinebufp)(FILE* stream);
556
557         BEFORE_ORIGINAL_FILE(setlinebuf, LIBC);
558
559         setlinebufp(stream);
560
561         AFTER_ORIGINAL_FILEP_RET(VT_NULL, NULL, 0, stream, FD_API_OTHER, "%p", stream);
562 }
563
564 int setvbuf(FILE* stream, char* buf, int mode, size_t size)
565 {
566         static int (*setvbufp)(FILE* stream, char* buf, int mode, size_t size);
567
568         BEFORE_ORIGINAL_FILE(setvbuf, LIBC);
569         ret = setvbufp(stream,buf,mode,size);
570         AFTER_ORIGINAL_FILEP(size, stream, FD_API_OTHER,
571                         "%p, %p, %d, %u", stream, buf, mode, size);
572         return ret;
573 }
574