add osp thread probes in bada_thread.cpp
[platform/core/system/swap-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         if(_fd != -1) {                                                                                                                                         
111                 _fstatret = fstat(_fd, &_statbuf);
112         }                                                                                                                                                               
113         PRE_PROBEBLOCK_END();
114
115         ret = fclosep(stream);
116
117         POST_PROBEBLOCK_BEGIN(LC_RESOURCE, VT_INT, ret, "%p", stream);
118         POST_PROBEBLOCK_MIDDLE_FD(0, _fd, FD_API_CLOSE);
119         POST_PROBEBLOCK_CALLSTACK_RESOURCE(FD_API_CLOSE);
120         POST_PROBEBLOCK_END();
121
122         return ret;
123 }
124
125 int remove(const char* filename)
126 {
127         static int (*removep)(const char* filename);
128
129         BEFORE_ORIGINAL_FILE(remove, LIBC);
130         _filepath = (char*)filename;
131         ret = removep(filename);
132         AFTER_ORIGINAL_NOFD(0, FD_API_DIRECTORY, "%s", filename);
133         return ret;
134 }
135
136 int rename(const char* oldname, const char* newname)
137 {
138         static int (*renamep)(const char* oldname, const char* newname);
139
140         BEFORE_ORIGINAL_FILE(rename, LIBC);
141         _filepath = (char*)newname;
142         ret = renamep(oldname, newname);
143         AFTER_ORIGINAL_NOFD(0, FD_API_DIRECTORY, "%s, %s", oldname, newname);
144         return ret;
145 }
146
147 FILE * tmpfile ( void )
148 {
149         static FILE* (*tmpfilep) ( void );
150         FILE* fret;
151
152         BEFORE_ORIGINAL_FILE_NOFILTER(tmpfile, LIBC);
153         _filepath = "<temp file>";
154         fret = tmpfilep();
155
156         AFTER_ORIGINAL_FILEP_RET(VT_PTR, fret, 0, fret, FD_API_OPEN, "%s", "");
157
158         return fret;
159 }
160
161 int fgetpos(FILE* stream, fpos_t* position)
162 {
163         static int (*fgetposp)(FILE* stream, fpos_t* position);
164
165         BEFORE_ORIGINAL_FILE(fgetpos, LIBC);
166         ret = fgetposp(stream, position);
167         AFTER_ORIGINAL_FILEP(0, stream, FD_API_OTHER, "%p, %p", stream, position);
168         return ret;
169 }
170
171 int fseek(FILE* stream, long int offset, int origin)
172 {
173         static int (*fseekp)(FILE* stream, long int offset, int origin);
174
175         BEFORE_ORIGINAL_FILE(fseek, LIBC);
176         ret = fseekp(stream, offset, origin);
177         AFTER_ORIGINAL_FILEP((unsigned int)offset, stream, FD_API_OTHER,
178                         "%p, %ld, %d", stream, offset, origin);
179         return ret;
180 }
181
182 int fsetpos(FILE* stream, const fpos_t* pos)
183 {
184         static int (*fsetposp)(FILE* stream, const fpos_t* pos);
185
186         BEFORE_ORIGINAL_FILE(fsetpos, LIBC);
187         ret = fsetposp(stream, pos);
188         AFTER_ORIGINAL_FILEP(0, stream, FD_API_OTHER, "%p, %p", stream, pos);
189         return ret;
190 }
191
192 long int ftell(FILE* stream)
193 {
194         static long int (*ftellp)(FILE* stream);
195         long int lret;
196
197         BEFORE_ORIGINAL_FILE(ftell, LIBC);
198         
199         lret = ftellp(stream);
200
201         AFTER_ORIGINAL_FILEP_RET(VT_LONG, lret, 0, stream, FD_API_OTHER, "%p", stream);
202
203         return lret;
204 }
205
206 void rewind(FILE* stream)
207 {
208         static void (*rewindp)(FILE* stream);
209
210         BEFORE_ORIGINAL_FILE(rewind, LIBC);
211
212         rewindp(stream);
213         
214         AFTER_ORIGINAL_FILEP_RET(VT_NULL, NULL, 0, stream, FD_API_OTHER, "%p", stream);
215 }
216
217 void clearerr(FILE* stream)
218 {
219         static void (*clearerrp)(FILE* stream);
220
221         BEFORE_ORIGINAL_FILE(clearerr, LIBC);
222
223         clearerrp(stream);
224         
225         AFTER_ORIGINAL_FILEP_RET(VT_NULL, NULL, 0, stream, FD_API_OTHER, "%p", stream);
226 }
227
228 int feof(FILE* stream)
229 {
230         static int (*feofp)(FILE* stream);
231
232         BEFORE_ORIGINAL_FILE(feof, LIBC);
233         ret = feofp(stream);
234         AFTER_ORIGINAL_FILEP(0, stream, FD_API_OTHER, "%p", stream);
235         return ret;
236 }
237
238 int ferror(FILE* stream)
239 {
240         static int (*ferrorp)(FILE* stream);
241
242         BEFORE_ORIGINAL_FILE(ferror, LIBC);
243         ret = ferrorp(stream);
244         AFTER_ORIGINAL_FILEP(0, stream, FD_API_OTHER, "%p", stream);
245         return ret;
246 }
247
248 int fileno(FILE* stream)
249 {
250         static int (*filenop)(FILE* stream);
251
252         BEFORE_ORIGINAL_FILE(fileno, LIBC);
253         ret = filenop(stream);
254         AFTER_ORIGINAL_FILEP(0, stream, FD_API_OTHER, "%p", stream);
255         return ret;
256 }
257
258 void perror(const char* string)
259 {
260         static void (*perrorp)(const char* string);
261
262         BEFORE_ORIGINAL_FILE(perror, LIBC);
263
264         perrorp(string);
265
266         AFTER_ORIGINAL_NOFD_RET(VT_NULL, NULL, 0, FD_API_OTHER, "%s", string);
267 }
268
269 // *******************************************************************
270 // File read / write APIs
271 // *******************************************************************
272
273 int vfprintf(FILE* stream, const char* format, va_list arg)
274 {
275         static int (*vfprintfp)(FILE* stream, const char* format, va_list arg);
276
277         BEFORE_ORIGINAL_FILE(vfprintf, LIBC);
278         ret = vfprintfp(stream, format, arg);
279         AFTER_ORIGINAL_FILEP(ret, stream, FD_API_WRITE, "%p, %s", stream, format);
280         return ret;
281 }
282
283 int vfscanf(FILE* stream, const char* format, va_list arg)
284 {
285         static int (*vfscanfp)(FILE* stream, const char* format, va_list arg);
286
287         BEFORE_ORIGINAL_FILE(vfscanf, LIBC);
288         ret = vfscanfp(stream, format, arg);
289         AFTER_ORIGINAL_FILEP(ret, stream, FD_API_READ, "%p, %s", stream, format);
290         return ret;
291 }
292
293 int fgetc(FILE* stream)
294 {
295         static int (*fgetcp)(FILE* stream);
296
297         BEFORE_ORIGINAL_FILE(fgetc, LIBC);
298         ret = fgetcp(stream);
299         AFTER_ORIGINAL_FILEP((ret == EOF ? 0 : 1), stream, FD_API_READ, "%p", stream);
300         return ret;
301 }
302
303 #if 0   // why is this commented?
304 char* fgets(char* str, int size, FILE* stream)
305 {
306         static char* (*fgetsp)(char* str, int num, FILE* stream);
307         char* cret;
308
309         BEFORE_ORIGINAL_FILE(fgets, LIBC);
310         
311         cret = fgetsp(str, size, stream);
312
313         AFTER_ORIGINAL_FILEP_RET(VT_STR, cret, (ret == NULL ? 0 : strlen(cret)),
314                         stream, FD_API_READ, "%s, %d, %p", str, size, stream);
315
316         return cret;
317 }
318 #endif
319
320 int fputc(int character, FILE* stream)
321 {
322         static int (*fputcp)(int character, FILE* stream);
323
324         BEFORE_ORIGINAL_FILE(fputc, LIBC);
325         ret = fputcp(character, stream);
326         AFTER_ORIGINAL_FILEP((ret == EOF ? 0 : 1), stream, FD_API_WRITE,
327                         "%d, %p", character, stream);
328         return ret;
329 }
330
331 int fputs(const char* str, FILE* stream)
332 {
333         static int (*fputsp)(const char* str, FILE* stream);
334
335         BEFORE_ORIGINAL_FILE(fputs, LIBC);
336         ret = fputsp(str, stream);
337         AFTER_ORIGINAL_FILEP(ret, stream, FD_API_WRITE, "%s, %p", str, stream);
338         return ret;
339 }
340
341 int getc(FILE* stream)
342 {
343         static int (*getcp)(FILE* stream);
344
345         BEFORE_ORIGINAL_FILE(getc, LIBC);
346         ret = getcp(stream);
347         AFTER_ORIGINAL_FILEP((ret == EOF ? 0 : 1), stream, FD_API_READ, "%p", stream);
348         return ret;
349 }
350
351 int putc(int character, FILE* stream)
352 {
353         static int (*putcp)(int character, FILE* stream);
354
355         BEFORE_ORIGINAL_FILE(putc, LIBC);
356         ret = putcp(character, stream);
357         AFTER_ORIGINAL_FILEP((ret == EOF ? 0 : 1), stream, FD_API_WRITE,
358                         "%d, %p", character, stream);
359         return ret;
360 }
361
362 int ungetc(int character, FILE* stream)
363 {
364         static int (*ungetcp)(int character, FILE* stream);
365
366         BEFORE_ORIGINAL_FILE(ungetc, LIBC);
367         ret = ungetcp(character, stream);
368         AFTER_ORIGINAL_FILEP(0, stream, FD_API_OTHER, "%d, %p", character, stream);
369         return ret;
370 }
371
372 size_t fread(void* ptr, size_t size, size_t count, FILE* stream)
373 {
374         static size_t (*freadp)(void* ptr, size_t size, size_t count, FILE* stream);
375         size_t tret;
376
377         BEFORE_ORIGINAL_FILE(fread, LIBC);
378
379         tret = freadp(ptr, size, count, stream);
380         
381         AFTER_ORIGINAL_FILEP_RET(VT_SIZE_T, tret, 0, stream, FD_API_READ,
382                         "%p, %u, %u, %p", ptr, size, count, stream);
383         
384         return tret;
385 }
386
387 size_t fwrite(const void* ptr, size_t size, size_t count, FILE* stream)
388 {
389         static size_t (*fwritep)(const void* ptr, size_t size, size_t count, FILE* stream);
390         size_t tret;
391
392         BEFORE_ORIGINAL_FILE(fwrite, LIBC);
393
394         tret = fwritep(ptr, size, count, stream);
395
396         AFTER_ORIGINAL_FILEP_RET(VT_SIZE_T, tret, 0, stream, FD_API_WRITE,
397                         "%p, %u, %u, %p", ptr, size, count, stream);
398
399         return tret;
400 }
401
402 // *********************************************************
403 // variable parameter function
404 // *********************************************************
405 int fprintf(FILE* stream, const char* format, ...)
406 {
407         static int (*vfprintfp)(FILE* stream, const char* format, ...);
408
409         BEFORE_ORIGINAL_FILE(vfprintf, LIBC);
410
411         va_list arg;
412         va_start(arg, format);
413         ret = vfprintfp(stream, format, arg);
414
415         AFTER_ORIGINAL_FILEP(ret, stream, FD_API_WRITE, "%p, %s, ...", stream, format);
416         va_end(arg);
417
418         return ret;
419 }
420
421 int fscanf(FILE* stream, const char* format, ...)
422 {
423         static int (*vfscanfp)(FILE* stream, const char* format, ...);
424         
425         BEFORE_ORIGINAL_FILE(vfscanf, LIBC);
426
427         va_list arg;
428         va_start(arg, format);
429         ret = vfscanfp(stream, format, arg);
430
431         AFTER_ORIGINAL_FILEP(ret, stream, FD_API_READ, "%p, %s, ...", stream, format);
432         va_end(arg);
433
434         return ret;
435 }
436
437 #if !defined(DA_DEBUG_LOG) && !defined(PRINT_STDOUT)
438 int printf(const char* format, ...)
439 {
440         static int (*vprintfp)(const char* format, ...);
441         
442         BEFORE_ORIGINAL_FILE(vprintf, LIBC);
443
444         va_list arg;
445         va_start(arg, format);
446         ret = vprintfp(format, arg);
447
448         AFTER_ORIGINAL_NOFD(ret, FD_API_WRITE, "%s,...", format);
449         va_end(arg);
450
451         return ret;
452 }
453 #endif
454
455 int scanf(const char* format, ...)
456 {
457         static int (*vscanfp)(const char* format, ...);
458
459         BEFORE_ORIGINAL_FILE(vscanf, LIBC);
460
461         va_list arg;
462         va_start(arg, format);
463         ret = vscanfp(format, arg);
464
465         AFTER_ORIGINAL_NOFD(ret, FD_API_READ, "%s,...", format);
466         va_end(arg);
467
468         return ret;
469 }
470
471 int getchar()
472 {
473         static int (*getcharp)();
474
475         BEFORE_ORIGINAL_FILE(getchar, LIBC);
476         ret = getcharp();
477         AFTER_ORIGINAL_NOFD((ret == EOF ? 0 : 1), FD_API_READ, "%s", "");
478         return ret;
479 }
480
481 int putchar(int c)
482 {
483         static int (*putcharp)(int c);
484
485         BEFORE_ORIGINAL_FILE(putchar, LIBC);
486         ret = putcharp(c);
487         AFTER_ORIGINAL_NOFD((ret == EOF ? 0 : 1), FD_API_WRITE, "%d", c);
488         return ret;
489 }
490
491 char* gets(char* str)
492 {
493         static char* (*getsp)(char* str);
494         char* cret;
495
496         BEFORE_ORIGINAL_FILE(gets, LIBC);
497
498         cret = getsp(str);
499         
500         AFTER_ORIGINAL_NOFD_RET(VT_STR, cret, strlen(cret), FD_API_READ, "%s", str);
501
502         return cret;
503 }
504
505 #if !defined(DA_DEBUG_LOG) && !defined(PRINT_STDOUT)
506 int puts(const char* str)
507 {
508         static int (*putsp)(const char* str);
509
510         BEFORE_ORIGINAL_FILE(puts, LIBC);
511         ret = putsp(str);
512         AFTER_ORIGINAL_NOFD(ret, FD_API_WRITE, "%s", str);
513         return ret;
514 }
515 #endif
516
517 char* tmpnam(char* str)
518 {
519         static char* (*tmpnamp)(char* str);
520         char* cret;
521
522         BEFORE_ORIGINAL_FILE(tmpnam, LIBC);
523
524         cret = tmpnamp(str);
525
526         AFTER_ORIGINAL_NOFD_RET(VT_STR, cret, 0, FD_API_OTHER, "%s", str);
527
528         return cret;
529 }
530
531 void setbuf(FILE* stream, char* buf)
532 {
533         static void (*setbufp)(FILE* stream, char* buf);
534
535         BEFORE_ORIGINAL_FILE(setbuf, LIBC);
536
537         setbufp(stream, buf);
538
539         AFTER_ORIGINAL_FILEP_RET(VT_NULL, NULL, 0, stream, FD_API_OTHER,
540                         "%p, %p", stream, buf);
541 }
542
543 void setbuffer(FILE* stream, char* buf, size_t size)
544 {
545         static void (*setbufferp)(FILE* stream, char* buf, size_t size);
546
547         BEFORE_ORIGINAL_FILE(setbuffer, LIBC);
548
549         setbufferp(stream, buf, size);
550
551         AFTER_ORIGINAL_FILEP_RET(VT_NULL, NULL, size, stream, FD_API_OTHER,
552                         "%p, %p, %u", stream, buf, size);
553 }
554
555 void setlinebuf(FILE* stream)
556 {
557         static int (*setlinebufp)(FILE* stream);
558
559         BEFORE_ORIGINAL_FILE(setlinebuf, LIBC);
560
561         setlinebufp(stream);
562
563         AFTER_ORIGINAL_FILEP_RET(VT_NULL, NULL, 0, stream, FD_API_OTHER, "%p", stream);
564 }
565
566 int setvbuf(FILE* stream, char* buf, int mode, size_t size)
567 {
568         static int (*setvbufp)(FILE* stream, char* buf, int mode, size_t size);
569
570         BEFORE_ORIGINAL_FILE(setvbuf, LIBC);
571         ret = setvbufp(stream,buf,mode,size);
572         AFTER_ORIGINAL_FILEP(size, stream, FD_API_OTHER,
573                         "%p, %p, %d, %u", stream, buf, mode, size);
574         return ret;
575 }
576