File: cr_ktrace.c

package info (click to toggle)
blcr 0.8.5-2.3
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 10,740 kB
  • sloc: ansic: 31,999; sh: 12,633; makefile: 929; perl: 401; cpp: 79; java: 9
file content (173 lines) | stat: -rw-r--r-- 4,094 bytes parent folder | download | duplicates (4)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * Berkeley Lab Checkpoint/Restart (BLCR) for Linux is Copyright (c)
 * 2008, The Regents of the University of California, through Lawrence
 * Berkeley National Laboratory (subject to receipt of any required
 * approvals from the U.S. Dept. of Energy).  All rights reserved.
 *
 * Portions may be copyrighted by others, as may be noted in specific
 * copyright notices within specific files.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * $Id: cr_ktrace.c,v 1.15 2008/12/11 06:33:39 phargrov Exp $
 *
 *
 * Tracing and error/warning output for checkpoint/restart kernel code.
 */

#include "cr_module.h"

#include <asm/uaccess.h>
#include "cr_ktrace.h"

static char  cr_trace_buf[4096];
static CR_DEFINE_SPINLOCK(cr_trace_lock);

#if CR_KERNEL_TRACING

unsigned int cr_ktrace_mask = CR_KTRACE_DEFAULT;

void
cr_print_trace(const char *file, int line, const char *func, const char * fmt, ...)
{
	va_list args;
	const char *p;

	spin_lock(&cr_trace_lock);

	va_start(args, fmt);
	vsprintf(cr_trace_buf, fmt, args);
	va_end(args);

	/* simple basename implementation: */
        for (p = file; *p != '\0'; ++p) {
	    if (*p == '/') {
		file = p + 1;
	    }
	}

	printk(KERN_INFO "%s <%s:%d>, pid %d: %s\n", func, file, line,
	       current->pid, cr_trace_buf);

	spin_unlock(&cr_trace_lock);
}

#endif 

/*
 * Error/warning generation
 */

static CR_DEFINE_SPINLOCK(cr_errbuf_lock);

struct cr_errbuf_s {
    unsigned short	used, remain;
    char		buffer[0];  // GNU (not ISO C99) variable length array
};

cr_errbuf_t *cr_errbuf_alloc(void) {
    cr_errbuf_t *p = (cr_errbuf_t *)__get_free_page(GFP_KERNEL);
    p->used = 0;
    p->remain = PAGE_SIZE - sizeof(cr_errbuf_t);
    return p;
}

void cr_errbuf_free(cr_errbuf_t *eb) {
    free_page((unsigned long)eb);
}

static int
cr_errbuf_vprintf(const char *lvl, cr_errbuf_t *eb, const char * fmt, va_list args)
{
    char *p;
    int len;

    if (!eb || !eb->remain) {
#ifndef CR_QUIET
	spin_lock(&cr_trace_lock);
	vsprintf(cr_trace_buf, fmt, args);
	printk("%sblcr: %s", lvl, cr_trace_buf);
	spin_unlock(&cr_trace_lock);
#endif
	return 0;
    }

    spin_lock(&cr_errbuf_lock);

    p = eb->buffer + eb->used;
    len = vsnprintf(p, eb->remain, fmt, args);

    if (len > 0) {
#ifndef CR_QUIET
	printk("%sblcr: %s", lvl, p);
#endif
	if (len > eb->remain) {
	    eb->used += eb->remain;
	    eb->remain = 0;
	} else {
	    eb->remain -= len;
	    eb->used += len;
	}
    }

    spin_unlock(&cr_errbuf_lock);

    return len;
}
    
int cr_errbuf_printf(const char *lvl, cr_errbuf_t *eb, const char * fmt, ...)
{
    va_list args;
    int len;

    va_start(args, fmt);
    len = cr_errbuf_vprintf(lvl, eb, fmt, args);
    va_end(args);

    return len;
}

/*
 * Copy eb contents up to len bytes (including final nul)
 * Returns space that would be required to accept full contents.
 * If len is zero, buf is ignored.
 */
int cr_errbuf_read(char /*__user*/ *buf, unsigned int len, cr_errbuf_t *eb)
{
    if (!eb) {
	return -EINVAL;
    }

    if (len) {
	/* Don't read past used space (rest of buffer uninitialized == security risk)
	 * or write beyond user's buffer.
	 */
	len = min((len-1), (unsigned int)(eb->used));

	/* Copy to user if non-empty */
	if (len && copy_to_user(buf, eb->buffer, len)) {
	    return -EFAULT;
	}

	/* Add nul termination */
	if (put_user('\0', buf+len)) {
	    return -EFAULT;
	}
    }

    return eb->used ? (eb->used + 1) : 0;
}