1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>SystemTap: io.c Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.4.1 -->
<div class="qindex"><a class="qindex" href="index.html">Main Page</a> | <a class="qindex" href="annotated.html">Data Structures</a> | <a class="qindex" href="dirs.html">Directories</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="functions.html">Data Fields</a> | <a class="qindex" href="globals.html">Globals</a> | <a class="qindex" href="pages.html">Related Pages</a></div>
<h1>io.c</h1><a href="io_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">00001 <span class="comment">/* -*- linux-c -*- */</span><span class="comment"></span>
00002 <span class="comment">/** @file io.c</span>
00003 <span class="comment"> * @brief I/O functions</span>
00004 <span class="comment"> */</span>
00005 <span class="comment"></span>
00006 <span class="comment">/** Logs data.</span>
00007 <span class="comment"> * This function is compatible with printk. In fact it currently</span>
00008 <span class="comment"> * sends all output to vprintk, after sending "STP: ". This allows</span>
00009 <span class="comment"> * us to easily detect SystemTap output in the log file.</span>
00010 <span class="comment"> *</span>
00011 <span class="comment"> * @param fmt A variable number of args.</span>
00012 <span class="comment"> * @bug Lines are limited in length by printk buffer.</span>
00013 <span class="comment"> * @todo Needs replaced with something much faster that does not</span>
00014 <span class="comment"> * use the system log.</span>
00015 <span class="comment"> */</span>
<a name="l00016"></a><a class="code" href="io_8c.html#a4">00016</a> <span class="keywordtype">void</span> <a class="code" href="io_8c.html#a4">dlog</a> (<span class="keyword">const</span> <span class="keywordtype">char</span> *fmt, ...)
00017 {
00018 va_list args;
00019 printk(<span class="stringliteral">"STP: "</span>);
00020 va_start(args, fmt);
00021 vprintk(fmt, args);
00022 va_end(args);
00023 }
00024
00025 <span class="comment"></span>
00026 <span class="comment">/** Lookup symbol.</span>
00027 <span class="comment"> * This simply calls the kernel function kallsyms_lookup().</span>
00028 <span class="comment"> * That function is not exported, so this workaround is required.</span>
00029 <span class="comment"> * See the kernel source, kernel/kallsyms.c for more information.</span>
00030 <span class="comment"> */</span>
<a name="l00031"></a><a class="code" href="io_8c.html#a1">00031</a> <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">char</span> * (*_stp_kallsyms_lookup)(<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> addr,
00032 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> *symbolsize,
00033 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> *offset,
00034 <span class="keywordtype">char</span> **modname, <span class="keywordtype">char</span> *namebuf)=(<span class="keywordtype">void</span> *)KALLSYMS_LOOKUP;
00035
00036
00037 <span class="preprocessor">#define STP_BUF_LEN 8191</span>
00038 <span class="preprocessor"></span><span class="comment"></span>
00039 <span class="comment">/** Static buffer for printing */</span>
<a name="l00040"></a><a class="code" href="io_8c.html#a2">00040</a> <span class="keyword">static</span> <span class="keywordtype">char</span> <a class="code" href="io_8c.html#a2">_stp_pbuf</a>[STP_BUF_LEN+1];
00041 <span class="keyword">static</span> <span class="keywordtype">int</span> _stp_pbuf_len = STP_BUF_LEN;
00042 <span class="comment"></span>
00043 <span class="comment">/** Print into the print buffer.</span>
00044 <span class="comment"> * Like printf, except output goes into _stp_pbuf,</span>
00045 <span class="comment"> * which will contain the null-terminated output.</span>
00046 <span class="comment"> * Safe because overflowing _stp_pbuf is not allowed.</span>
00047 <span class="comment"> * Size is limited by length of print buffer.</span>
00048 <span class="comment"> *</span>
00049 <span class="comment"> * @param fmt A variable number of args.</span>
00050 <span class="comment"> * @note Formatting output should never be done within</span>
00051 <span class="comment"> * a probe. Use at module exit time.</span>
00052 <span class="comment"> * @sa _stp_print_buf_init</span>
00053 <span class="comment"> */</span>
00054
<a name="l00055"></a><a class="code" href="io_8c.html#a5">00055</a> <span class="keywordtype">void</span> <a class="code" href="io_8c.html#a5">_stp_print_buf</a> (<span class="keyword">const</span> <span class="keywordtype">char</span> *fmt, ...)
00056 {
00057 <span class="keywordtype">int</span> num;
00058 va_list args;
00059 <span class="keywordtype">char</span> *buf = <a class="code" href="io_8c.html#a2">_stp_pbuf</a> + STP_BUF_LEN - _stp_pbuf_len;
00060 va_start(args, fmt);
00061 num = vscnprintf(buf, _stp_pbuf_len, fmt, args);
00062 va_end(args);
00063 <span class="keywordflow">if</span> (num > 0)
00064 _stp_pbuf_len -= num;
00065 }
00066 <span class="comment"></span>
00067 <span class="comment">/** Clear the print buffer.</span>
00068 <span class="comment"> * Output from _stp_print_buf() will accumulate in the buffer</span>
00069 <span class="comment"> * until this is called.</span>
00070 <span class="comment"> */</span>
00071
<a name="l00072"></a><a class="code" href="io_8c.html#a6">00072</a> <span class="keywordtype">void</span> <a class="code" href="io_8c.html#a6">_stp_print_buf_init</a> (<span class="keywordtype">void</span>)
00073 {
00074 _stp_pbuf_len = STP_BUF_LEN;
00075 <a class="code" href="io_8c.html#a2">_stp_pbuf</a>[0] = 0;
00076 }
00077 <span class="comment"></span>
00078 <span class="comment">/** Print addresses symbolically into the print buffer.</span>
00079 <span class="comment"> * @param fmt A variable number of args.</span>
00080 <span class="comment"> * @param address The address to lookup.</span>
00081 <span class="comment"> * @note Formatting output should never be done within</span>
00082 <span class="comment"> * a probe. Use at module exit time.</span>
00083 <span class="comment"> */</span>
00084
<a name="l00085"></a><a class="code" href="io_8c.html#a7">00085</a> <span class="keywordtype">void</span> <a class="code" href="io_8c.html#a7">_stp_print_symbol</a>(<span class="keyword">const</span> <span class="keywordtype">char</span> *fmt, <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> address)
00086 {
00087 <span class="keywordtype">char</span> *modname;
00088 <span class="keyword">const</span> <span class="keywordtype">char</span> *name;
00089 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> offset, size;
00090 <span class="keywordtype">char</span> namebuf[KSYM_NAME_LEN+1];
00091
00092 name = <a class="code" href="io_8c.html#a1">_stp_kallsyms_lookup</a>(address, &size, &offset, &modname, namebuf);
00093
00094 <span class="keywordflow">if</span> (!name)
00095 <a class="code" href="io_8c.html#a5">_stp_print_buf</a>(<span class="stringliteral">"0x%lx"</span>, address);
00096 <span class="keywordflow">else</span> {
00097 <span class="keywordflow">if</span> (modname)
00098 <a class="code" href="io_8c.html#a5">_stp_print_buf</a>(<span class="stringliteral">"%s+%#lx/%#lx [%s]"</span>, name, offset,
00099 size, modname);
00100 <span class="keywordflow">else</span>
00101 <a class="code" href="io_8c.html#a5">_stp_print_buf</a>(<span class="stringliteral">"%s+%#lx/%#lx"</span>, name, offset, size);
00102 }
00103 }
00104 <span class="comment"></span>
00105 <span class="comment">/** Get the current return address.</span>
00106 <span class="comment"> * Call from kprobes (not jprobes).</span>
00107 <span class="comment"> * @param regs The pt_regs saved by the kprobe.</span>
00108 <span class="comment"> * @return The return address saved in esp or rsp.</span>
00109 <span class="comment"> * @note i386 and x86_64 only.</span>
00110 <span class="comment"> */</span>
00111
<a name="l00112"></a><a class="code" href="io_8c.html#a8">00112</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> <a class="code" href="io_8c.html#a8">cur_ret_addr</a> (<span class="keyword">struct</span> pt_regs *regs)
00113 {
00114 <span class="preprocessor">#ifdef __x86_64__</span>
00115 <span class="preprocessor"></span> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> *ra = (<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> *)regs->rsp;
00116 <span class="preprocessor">#else</span>
00117 <span class="preprocessor"></span> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> *ra = (<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> *)regs->esp;
00118 <span class="preprocessor">#endif</span>
00119 <span class="preprocessor"></span> <span class="keywordflow">if</span> (ra)
00120 <span class="keywordflow">return</span> *ra;
00121 <span class="keywordflow">else</span>
00122 <span class="keywordflow">return</span> 0;
00123 }
</pre></div><hr size="1"><address style="align: right;"><small>
Generated on Tue Mar 22 10:27:36 2005 for SystemTap.</small></body>
</html>
|