Whamcloud - gitweb
3e01df111505d38d58123a72914e63501d56bbf3
[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 <linux/kp30.h>
22 #include <portals/ptlctl.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("nal");
100         ltrace_filter("portals");
101
102         ltrace_show("all_types");
103         ltrace_filter("trace");
104         ltrace_filter("malloc");
105         ltrace_filter("net");
106         ltrace_filter("page");
107         ltrace_filter("other");
108         ltrace_filter("info");
109         ltrace_applymasks();
110
111         return rc;
112 }
113
114
115 static inline void ltrace_stop()
116 {
117 #ifdef PORTALS_DEV_ID
118         unregister_ioc_dev(PORTALS_DEV_ID);
119 #endif
120 }
121
122 static inline int not_uml()
123 {
124   /* Return Values:
125    *   0 when run under UML
126    *   1 when run on host
127    *  <0 when lookup failed
128    */
129         struct stat buf;
130         int rc = stat("/dev/ubd", &buf);
131         rc = ((rc<0) && (errno == ENOENT)) ? 1 : rc;
132         if (rc<0) {
133           fprintf(stderr, "Cannot stat /dev/ubd: %s\n", strerror(errno));
134           rc = 1; /* Assume host */
135         }
136         return rc;
137 }
138
139 #define LTRACE_MAX_NOB   256
140 static inline void ltrace_add_processnames(char* fname)
141 {
142         char cmdbuf[LTRACE_MAX_NOB];
143         struct timeval tv;
144         struct timezone tz;
145         int nob;
146         int underuml = !not_uml();
147
148         gettimeofday(&tv, &tz);
149
150         nob = snprintf(cmdbuf, LTRACE_MAX_NOB, "ps --no-headers -eo \"");
151
152         /* Careful - these format strings need to match the CDEBUG
153          * formats in portals/linux/debug.c EXACTLY
154          */
155         nob += snprintf(cmdbuf+nob, LTRACE_MAX_NOB, "%02x:%06x:%d:%lu.%06lu ",
156                         S_RPC >> 24, D_VFSTRACE, 0, tv.tv_sec, tv.tv_usec);
157
158         if (underuml && (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))) {
159                 nob += snprintf (cmdbuf+nob, LTRACE_MAX_NOB,
160                                  "(%s:%d:%s() %d | %d+%lu): ",
161                                  "lltrace.h", __LINE__, __FUNCTION__, 0, 0, 0L);
162         }
163         else {
164                 nob += snprintf (cmdbuf+nob, LTRACE_MAX_NOB,
165                                  "(%s:%d:%s() %d+%lu): ",
166                                  "lltrace.h", __LINE__, __FUNCTION__, 0, 0L);
167         }
168
169         nob += snprintf(cmdbuf+nob, LTRACE_MAX_NOB, " %%p %%c\" >> %s", fname);
170         system(cmdbuf);
171 }
172
173 #endif