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
|
<!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: scbuf.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="modules.html">Modules</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>scbuf.c</h1><div class="fragment"><pre class="fragment">00001 <span class="preprocessor">#ifndef _SCBUF_C_ </span><span class="comment">/* -*- linux-c -*- */</span>
00002 <span class="preprocessor">#define _SCBUF_C_</span>
00003 <span class="preprocessor"></span>
00004 <span class="preprocessor">#include <linux/config.h></span>
00005 <span class="comment"></span>
00006 <span class="comment">/** @file scbuf.c</span>
00007 <span class="comment"> * @addtogroup scbuf Scratch Buffer</span>
00008 <span class="comment"> * Scratch Buffer Functions.</span>
00009 <span class="comment"> * The scratch buffer is for collecting output before storing in a map,</span>
00010 <span class="comment"> * printing, etc. This is a per-cpu static buffer. It is necessary because </span>
00011 <span class="comment"> * of the limited stack space available in the kernel.</span>
00012 <span class="comment"> * @todo Need careful review of these to insure safety.</span>
00013 <span class="comment"> * @{</span>
00014 <span class="comment"> */</span>
00015 <span class="comment"></span>
00016 <span class="comment">/** Maximum size of buffer, not including terminating NULL */</span>
<a name="l00017"></a><a class="code" href="group__scbuf.html#ga6">00017</a> <span class="preprocessor">#define STP_BUF_LEN 8191</span>
00018 <span class="preprocessor"></span><span class="comment"></span>
00019 <span class="comment">/** Scratch buffer for printing, building strings, etc */</span>
00020 <span class="keyword">static</span> <span class="keywordtype">char</span> _stp_scbuf[NR_CPUS][<a class="code" href="group__scbuf.html#ga6">STP_BUF_LEN</a>+1];
00021 <span class="keyword">static</span> <span class="keywordtype">int</span> _stp_scbuf_len[NR_CPUS];
00022 <span class="comment"></span>
00023 <span class="comment">/** Sprint into the scratch buffer.</span>
00024 <span class="comment"> * Like printf, except output goes into a global scratch buffer</span>
00025 <span class="comment"> * which will contain the null-terminated output.</span>
00026 <span class="comment"> * Safe because overflowing the buffer is not allowed.</span>
00027 <span class="comment"> * Size is limited by length of scratch buffer, STP_BUF_LEN.</span>
00028 <span class="comment"> *</span>
00029 <span class="comment"> * @param fmt A printf-style format string followed by a </span>
00030 <span class="comment"> * variable number of args.</span>
00031 <span class="comment"> * @sa _stp_scbuf_clear</span>
00032 <span class="comment"> */</span>
00033
<a name="l00034"></a><a class="code" href="group__scbuf.html#ga2">00034</a> <span class="keywordtype">void</span> <a class="code" href="group__scbuf.html#ga2">_stp_sprint</a> (<span class="keyword">const</span> <span class="keywordtype">char</span> *fmt, ...)
00035 {
00036 <span class="keywordtype">int</span> num;
00037 va_list args;
00038 <span class="keywordtype">int</span> cpu = smp_processor_id();
00039 <span class="keywordtype">char</span> *buf = _stp_scbuf[cpu] + <a class="code" href="group__scbuf.html#ga6">STP_BUF_LEN</a> - _stp_scbuf_len[cpu];
00040 va_start(args, fmt);
00041 num = vscnprintf(buf, _stp_scbuf_len[cpu], fmt, args);
00042 va_end(args);
00043 <span class="keywordflow">if</span> (num > 0)
00044 _stp_scbuf_len[cpu] -= num;
00045 }
00046 <span class="comment"></span>
00047 <span class="comment">/** Write a string into the scratch buffer.</span>
00048 <span class="comment"> * Copies a string into a global scratch buffer.</span>
00049 <span class="comment"> * Safe because overflowing the buffer is not allowed.</span>
00050 <span class="comment"> * Size is limited by length of scratch buffer, STP_BUF_LEN.</span>
00051 <span class="comment"> * This is more efficient than using _stp_sprint().</span>
00052 <span class="comment"> *</span>
00053 <span class="comment"> * @param str A string.</span>
00054 <span class="comment"> */</span>
00055
<a name="l00056"></a><a class="code" href="group__scbuf.html#ga3">00056</a> <span class="keywordtype">void</span> <a class="code" href="group__scbuf.html#ga3">_stp_sprint_str</a> (<span class="keyword">const</span> <span class="keywordtype">char</span> *str)
00057 {
00058 <span class="keywordtype">int</span> cpu = smp_processor_id();
00059 <span class="keywordtype">char</span> *buf = _stp_scbuf[cpu] + <a class="code" href="group__scbuf.html#ga6">STP_BUF_LEN</a> - _stp_scbuf_len[cpu];
00060 <span class="keywordtype">int</span> num = strlen (str);
00061 <span class="keywordflow">if</span> (num > _stp_scbuf_len[cpu])
00062 num = _stp_scbuf_len[cpu];
00063 strncpy (buf, str, num);
00064 _stp_scbuf_len[cpu] -= num;
00065 }
00066 <span class="comment"></span>
00067 <span class="comment">/** Clear the scratch buffer.</span>
00068 <span class="comment"> * This function should be called before anything is written to </span>
00069 <span class="comment"> * the scratch buffer. Output will accumulate in the buffer</span>
00070 <span class="comment"> * until this function is called again. </span>
00071 <span class="comment"> * @returns A pointer to the buffer.</span>
00072 <span class="comment"> */</span>
00073
<a name="l00074"></a><a class="code" href="group__scbuf.html#ga4">00074</a> <span class="keywordtype">char</span> *<a class="code" href="group__scbuf.html#ga4">_stp_scbuf_clear</a> (<span class="keywordtype">void</span>)
00075 {
00076 <span class="keywordtype">int</span> cpu = smp_processor_id();
00077 _stp_scbuf_len[cpu] = <a class="code" href="group__scbuf.html#ga6">STP_BUF_LEN</a>;
00078 *_stp_scbuf[cpu] = 0;
00079 <span class="keywordflow">return</span> _stp_scbuf[cpu];
00080 }
00081 <span class="comment"></span>
00082 <span class="comment">/** Get the current top of the scratch buffer.</span>
00083 <span class="comment"> * This returns the address of the location where</span>
00084 <span class="comment"> * data will be written next in the scratch buffer.</span>
00085 <span class="comment"> * @returns A pointer</span>
00086 <span class="comment"> */</span>
00087
00088 <span class="keyword">static</span> <span class="keywordtype">char</span> *_stp_scbuf_cur (<span class="keywordtype">void</span>)
00089 {
00090 <span class="keywordtype">int</span> cpu = smp_processor_id();
00091 <span class="keywordflow">return</span> _stp_scbuf[cpu] + <a class="code" href="group__scbuf.html#ga6">STP_BUF_LEN</a> - _stp_scbuf_len[cpu];
00092 }
00093 <span class="comment"></span>
00094 <span class="comment">/** @} */</span>
00095 <span class="preprocessor">#endif </span><span class="comment">/* _SCBUF_C_ */</span>
</pre></div></body></html>
|