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