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