एक विकल्प strace उपयोग करने के लिए है:
strace -o logfile -eopen yourapp
यह सभी फ़ाइल से खोलने की घटनाओं प्रवेश करेंगे, लेकिन यह होगा एक प्रदर्शन जुर्माना लगाओ जो महत्वपूर्ण हो सकता है। हालांकि इसका उपयोग करने में आसान होने का लाभ है।
अन्य विकल्प LD_PRELOAD का उपयोग करना है। यह आपके विकल्प # 2 से मेल खाता है। मूल विचार कुछ इस तरह करना है:
#define _GNU_SOURCE
#include <stdio.h>
#include <dlfcn.h>
int open(const char *fn, int flags) {
static int (*real_open)(const char *fn, int flags);
if (!real_open) {
real_open = dlsym(RTLD_NEXT, "open");
}
fprintf(stderr, "opened file '%s'\n", fn);
return real_open(fn, flags);
}
फिर से निर्माण:
gcc -fPIC -shared -ldl -o preload-example.so preload-example.c
और जैसे के साथ अपने कार्यक्रम चलाने:
$ LD_PRELOAD=$PWD/preload-example.so cat /dev/null
opened file '/dev/null'
यह बहुत कम भूमि के ऊपर है।
ध्यान दें, तथापि, वहाँ फ़ाइलों को खोलने के लिए अन्य प्रवेश द्वारों हैं कि - जैसे, fopen(), या कई विरासत संगतता प्रवेश द्वारों में से एक, openat():
00000000000747d0 g DF .text 000000000000071c GLIBC_2.2.5 _IO_file_fopen
0000000000068850 g DF .text 000000000000000a GLIBC_2.2.5 fopen
000000000006fe60 g DF .text 00000000000000e2 GLIBC_2.4 open_wmemstream
00000000001209c0 w DF .text 00000000000000ec GLIBC_2.2.5 posix_openpt
0000000000069e50 g DF .text 00000000000003fb GLIBC_2.2.5 _IO_proc_open
00000000000dcf70 g DF .text 0000000000000021 GLIBC_2.7 __open64_2
0000000000068a10 g DF .text 00000000000000f5 GLIBC_2.2.5 fopencookie
000000000006a250 g DF .text 000000000000009b GLIBC_2.2.5 popen
00000000000d7b10 w DF .text 0000000000000080 GLIBC_2.2.5 __open64
0000000000068850 g DF .text 000000000000000a GLIBC_2.2.5 _IO_fopen
00000000000d7e70 w DF .text 0000000000000020 GLIBC_2.7 __openat64_2
00000000000e1ef0 g DF .text 000000000000005b GLIBC_2.2.5 openlog
00000000000d7b10 w DF .text 0000000000000080 GLIBC_2.2.5 open64
0000000000370c10 g DO .bss 0000000000000008 GLIBC_PRIVATE _dl_open_hook
0000000000031680 g DF .text 0000000000000240 GLIBC_2.2.5 catopen
000000000006a250 g DF .text 000000000000009b GLIBC_2.2.5 _IO_popen
0000000000071af0 g DF .text 000000000000026a GLIBC_2.2.5 freopen64
00000000000723a0 g DF .text 0000000000000183 GLIBC_2.2.5 fmemopen
00000000000a44f0 w DF .text 0000000000000088 GLIBC_2.4 fdopendir
00000000000d7e70 g DF .text 0000000000000020 GLIBC_2.7 __openat_2
00000000000a3d00 w DF .text 0000000000000095 GLIBC_2.2.5 opendir
00000000000dcf40 g DF .text 0000000000000021 GLIBC_2.7 __open_2
00000000000d7b10 w DF .text 0000000000000080 GLIBC_2.2.5 __open
0000000000074370 g DF .text 00000000000000d7 GLIBC_2.2.5 _IO_file_open
0000000000070b40 g DF .text 00000000000000d2 GLIBC_2.2.5 open_memstream
0000000000070450 g DF .text 0000000000000272 GLIBC_2.2.5 freopen
00000000000318c0 g DF .text 00000000000008c4 GLIBC_PRIVATE __open_catalog
00000000000d7b10 w DF .text 0000000000000080 GLIBC_2.2.5 open
0000000000067e80 g DF .text 0000000000000332 GLIBC_2.2.5 fdopen
000000000001e9b0 g DF .text 00000000000003f5 GLIBC_2.2.5 iconv_open
00000000000daca0 g DF .text 000000000000067b GLIBC_2.2.5 fts_open
00000000000d7d60 w DF .text 0000000000000109 GLIBC_2.4 openat
0000000000068850 w DF .text 000000000000000a GLIBC_2.2.5 fopen64
00000000000d7d60 w DF .text 0000000000000109 GLIBC_2.4 openat64
00000000000d6490 g DF .text 00000000000000b6 GLIBC_2.2.5 posix_spawn_file_actions_addopen
0000000000121b80 g DF .text 000000000000008a GLIBC_PRIVATE __libc_dlopen_mode
0000000000067e80 g DF .text 0000000000000332 GLIBC_2.2.5 _IO_fdopen
आप हुक करने की आवश्यकता हो सकती इन सभी को पूर्णता के लिए - कम से कम, जिन्हें _ के साथ prefixed नहीं किया जाना चाहिए।विशेष रूप से, फॉपेन() से खोलने के लिए libc-internal कॉल() को LD_PRELOAD लाइब्रेरी द्वारा लगाया नहीं जाता है, क्योंकि fopen seperately को हुक करना सुनिश्चित करें।
एक समान चेतावनी स्ट्रेस पर लागू होती है - वहां 'ओपनैट' सिस्कल भी है, और आपके आर्किटेक्चर के आधार पर अन्य विरासत सिस्को भी हो सकते हैं। लेकिन एलडी_PRELOAD हुक के साथ जितना नहीं, इसलिए यदि आप प्रदर्शन हिट पर ध्यान नहीं देते हैं, तो यह एक आसान विकल्प हो सकता है।
strace -o file.txt ./command
फिर आप
cat file.txt | grep open
सभी फ़ाइलें कार्यक्रम खोला की एक सूची प्राप्त करने के लिए कर सकते हैं:
यह भी देखें http://stackoverflow.com/q/2972765/119790 –