Whamcloud - gitweb
dbeae911d2e24b26326a422797eaa3f7d48882ea
[fs/lustre-release.git] / libcfs / include / libcfs / lltrace.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Compile with:
5  * cc -I../../portals/include -o fio fio.c -L../../portals/linux/utils -lptlctl
6  */
7 #ifndef __LIBCFS_LLTRACE_H__
8 #define __LIBCFS_LLTRACE_H__
9
10 #if defined(__linux__)
11 #include <libcfs/linux/lltrace.h>
12 #elif defined(__APPLE__)
13 #include <libcfs/darwin/lltrace.h>
14 #elif defined(__WINNT__)
15 #include <libcfs/winnt/lltrace.h>
16 #else
17 #error Unsupported Operating System
18 #endif
19
20 static inline int ltrace_write_file(char* fname)
21 {
22         char* argv[3];
23
24         argv[0] = "debug_kernel";
25         argv[1] = fname;
26         argv[2] = "1";
27
28         fprintf(stderr, "[ptlctl] %s %s %s\n", argv[0], argv[1], argv[2]);
29
30         return jt_dbg_debug_kernel(3, argv);
31 }
32
33 static inline int ltrace_clear()
34 {
35         char* argv[1];
36
37         argv[0] = "clear";
38
39         fprintf(stderr, "[ptlctl] %s\n", argv[0]);
40
41         return jt_dbg_clear_debug_buf(1, argv);
42 }
43
44 static inline int ltrace_mark(int indent_level, char* text)
45 {
46         char* argv[2];
47         char mark_buf[PATH_MAX];
48
49         snprintf(mark_buf, PATH_MAX, "====%d=%s", indent_level, text);
50
51         argv[0] = "mark";
52         argv[1] = mark_buf;
53         return jt_dbg_mark_debug_buf(2, argv);
54 }
55
56 static inline int ltrace_applymasks()
57 {
58         char* argv[2];
59         argv[0] = "list";
60         argv[1] = "applymasks";
61
62         fprintf(stderr, "[ptlctl] %s %s\n", argv[0], argv[1]);
63
64         return jt_dbg_list(2, argv);
65 }
66
67
68 static inline int ltrace_filter(char* subsys_or_mask)
69 {
70         char* argv[2];
71         argv[0] = "filter";
72         argv[1] = subsys_or_mask;
73         return jt_dbg_filter(2, argv);
74 }
75
76 static inline int ltrace_show(char* subsys_or_mask)
77 {
78         char* argv[2];
79         argv[0] = "show";
80         argv[1] = subsys_or_mask;
81         return jt_dbg_show(2, argv);
82 }
83
84 static inline int ltrace_start()
85 {
86         int rc = 0;
87         dbg_initialize(0, NULL);
88 #ifdef LNET_DEV_ID
89         rc = register_ioc_dev(LNET_DEV_ID, LNET_DEV_PATH,
90                               LNET_DEV_MAJOR, LNET_DEV_MINOR);
91 #endif
92         ltrace_filter("class");
93         ltrace_filter("nal");
94         ltrace_filter("portals");
95
96         ltrace_show("all_types");
97         ltrace_filter("trace");
98         ltrace_filter("malloc");
99         ltrace_filter("net");
100         ltrace_filter("page");
101         ltrace_filter("other");
102         ltrace_filter("info");
103         ltrace_applymasks();
104
105         return rc;
106 }
107
108
109 static inline void ltrace_stop()
110 {
111 #ifdef LNET_DEV_ID
112         unregister_ioc_dev(LNET_DEV_ID);
113 #endif
114 }
115
116 static inline int not_uml()
117 {
118   /* Return Values:
119    *   0 when run under UML
120    *   1 when run on host
121    *  <0 when lookup failed
122    */
123         struct stat buf;
124         int rc = stat("/dev/ubd", &buf);
125         rc = ((rc<0) && (errno == ENOENT)) ? 1 : rc;
126         if (rc<0) {
127           fprintf(stderr, "Cannot stat /dev/ubd: %s\n", strerror(errno));
128           rc = 1; /* Assume host */
129         }
130         return rc;
131 }
132
133 #define LTRACE_MAX_NOB   256
134 static inline void ltrace_add_processnames(char* fname)
135 {
136         char cmdbuf[LTRACE_MAX_NOB];
137         struct timeval tv;
138         struct timezone tz;
139         int nob;
140         int underuml = !not_uml();
141
142         gettimeofday(&tv, &tz);
143
144         nob = snprintf(cmdbuf, LTRACE_MAX_NOB, "ps --no-headers -eo \"");
145
146         /* Careful - these format strings need to match the CDEBUG
147          * formats in portals/linux/debug.c EXACTLY
148          */
149         nob += snprintf(cmdbuf+nob, LTRACE_MAX_NOB, "%02x:%06x:%d:%lu.%06lu ",
150                         S_RPC >> 24, D_VFSTRACE, 0, tv.tv_sec, tv.tv_usec);
151
152         if (underuml && (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))) {
153                 nob += snprintf (cmdbuf+nob, LTRACE_MAX_NOB,
154                                  "(%s:%d:%s() %d | %d+%lu): ",
155                                  "lltrace.h", __LINE__, __FUNCTION__, 0, 0, 0L);
156         }
157         else {
158                 nob += snprintf (cmdbuf+nob, LTRACE_MAX_NOB,
159                                  "(%s:%d:%s() %d+%lu): ",
160                                  "lltrace.h", __LINE__, __FUNCTION__, 0, 0L);
161         }
162
163         nob += snprintf(cmdbuf+nob, LTRACE_MAX_NOB, " %%p %%c\" >> %s", fname);
164         system(cmdbuf);
165 }
166
167 #endif