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