Whamcloud - gitweb
LU-1347 build: remove the vim/emacs modelines
[fs/lustre-release.git] / libcfs / include / libcfs / lltrace.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * libcfs/include/libcfs/lltrace.h
35  *
36  * Compile with:
37  * cc -I../../portals/include -o fio fio.c -L../../portals/linux/utils -lptlctl
38  */
39
40 #ifndef __LIBCFS_LLTRACE_H__
41 #define __LIBCFS_LLTRACE_H__
42
43 #if defined(__linux__)
44 #include <libcfs/linux/lltrace.h>
45 #elif defined(__APPLE__)
46 #include <libcfs/darwin/lltrace.h>
47 #elif defined(__WINNT__)
48 #include <libcfs/winnt/lltrace.h>
49 #else
50 #error Unsupported Operating System
51 #endif
52
53 static inline int ltrace_write_file(char* fname)
54 {
55         char* argv[3];
56
57         argv[0] = "debug_kernel";
58         argv[1] = fname;
59         argv[2] = "1";
60
61         fprintf(stderr, "[ptlctl] %s %s %s\n", argv[0], argv[1], argv[2]);
62
63         return jt_dbg_debug_kernel(3, argv);
64 }
65
66 static inline int ltrace_clear()
67 {
68         char* argv[1];
69
70         argv[0] = "clear";
71
72         fprintf(stderr, "[ptlctl] %s\n", argv[0]);
73
74         return jt_dbg_clear_debug_buf(1, argv);
75 }
76
77 static inline int ltrace_mark(int indent_level, char* text)
78 {
79         char* argv[2];
80         char mark_buf[PATH_MAX];
81
82         snprintf(mark_buf, PATH_MAX, "====%d=%s", indent_level, text);
83
84         argv[0] = "mark";
85         argv[1] = mark_buf;
86         return jt_dbg_mark_debug_buf(2, argv);
87 }
88
89 static inline int ltrace_applymasks()
90 {
91         char* argv[2];
92         argv[0] = "list";
93         argv[1] = "applymasks";
94
95         fprintf(stderr, "[ptlctl] %s %s\n", argv[0], argv[1]);
96
97         return jt_dbg_list(2, argv);
98 }
99
100
101 static inline int ltrace_filter(char* subsys_or_mask)
102 {
103         char* argv[2];
104         argv[0] = "filter";
105         argv[1] = subsys_or_mask;
106         return jt_dbg_filter(2, argv);
107 }
108
109 static inline int ltrace_show(char* subsys_or_mask)
110 {
111         char* argv[2];
112         argv[0] = "show";
113         argv[1] = subsys_or_mask;
114         return jt_dbg_show(2, argv);
115 }
116
117 static inline int ltrace_start()
118 {
119         int rc = 0;
120         dbg_initialize(0, NULL);
121 #ifdef LNET_DEV_ID
122         rc = register_ioc_dev(LNET_DEV_ID, LNET_DEV_PATH,
123                               LNET_DEV_MAJOR, LNET_DEV_MINOR);
124 #endif
125         ltrace_filter("class");
126         ltrace_filter("nal");
127         ltrace_filter("portals");
128
129         ltrace_show("all_types");
130         ltrace_filter("trace");
131         ltrace_filter("malloc");
132         ltrace_filter("net");
133         ltrace_filter("page");
134         ltrace_filter("other");
135         ltrace_filter("info");
136         ltrace_applymasks();
137
138         return rc;
139 }
140
141
142 static inline void ltrace_stop()
143 {
144 #ifdef LNET_DEV_ID
145         unregister_ioc_dev(LNET_DEV_ID);
146 #endif
147 }
148
149 static inline int not_uml()
150 {
151   /* Return Values:
152    *   0 when run under UML
153    *   1 when run on host
154    *  <0 when lookup failed
155    */
156         struct stat buf;
157         int rc = stat("/dev/ubd", &buf);
158         rc = ((rc<0) && (errno == ENOENT)) ? 1 : rc;
159         if (rc<0) {
160           fprintf(stderr, "Cannot stat /dev/ubd: %s\n", strerror(errno));
161           rc = 1; /* Assume host */
162         }
163         return rc;
164 }
165
166 #define LTRACE_MAX_NOB   256
167 static inline void ltrace_add_processnames(char* fname)
168 {
169         char cmdbuf[LTRACE_MAX_NOB];
170         struct timeval tv;
171         struct timezone tz;
172         int nob;
173         int underuml = !not_uml();
174
175         gettimeofday(&tv, &tz);
176
177         nob = snprintf(cmdbuf, LTRACE_MAX_NOB, "ps --no-headers -eo \"");
178
179         /* Careful - these format strings need to match the CDEBUG
180          * formats in portals/linux/debug.c EXACTLY
181          */
182         nob += snprintf(cmdbuf+nob, LTRACE_MAX_NOB, "%02x:%06x:%d:%lu.%06lu ",
183                         S_RPC >> 24, D_VFSTRACE, 0, tv.tv_sec, tv.tv_usec);
184         nob += snprintf (cmdbuf+nob, LTRACE_MAX_NOB, "(%s:%d:%s() %d+%lu): ",
185                          "lltrace.h", __LINE__, __FUNCTION__, 0, 0L);
186         nob += snprintf(cmdbuf+nob, LTRACE_MAX_NOB, " %%p %%c\" >> %s", fname);
187         system(cmdbuf);
188 }
189
190 #endif