-
Notifications
You must be signed in to change notification settings - Fork 1
/
kernel_log.c
167 lines (137 loc) · 4.65 KB
/
kernel_log.c
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
/* This file is part of the MAYLIB libray.
Copyright 2007-2018 Patrick Pelissier
This Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at your
option) any later version.
This Library 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 Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with th Library; see the file COPYING.LESSER.txt.
If not, write to the Free Software Foundation, Inc.,
51 Franklin St, Fifth Floor, Boston,
MA 02110-1301, USA. */
#include "may-impl.h"
/* Logging MAY needs GCC >= 3.0 and GLIBC >= 2.0. */
#ifdef MAY_WANT_LOGGING
/* Can't include them before (in particular, printf.h) */
#include <printf.h>
#include <stdarg.h>
#include <time.h>
/* Define LOGGING variables */
FILE *may_log_file;
int may_log_type;
int may_log_level;
int may_log_current;
size_t may_log_size;
static int
may_vcb_printf (FILE *stream, const struct printf_info *info,
const void * const *arg)
{
size_t size;
int length;
int org_type_logging;
may_t w;
UNUSED (info);
org_type_logging = may_log_type;
may_log_type = 0; /* We disable the logging during this print! */
w = *((may_t *) (arg[0]));
/* Compute the size of w while being protective about invalid data */
size = MAY_UNLIKELY ((char*)w < may_g.Heap.base
|| (char*) w >= may_g.Heap.top
|| (((unsigned long)w) % sizeof(long)) != 0) ? 0
: may_length (w);
if (size >= may_log_size) {
const char * type = may_get_name (w);
length = fprintf (stream, "@@EXPR[hash=%X,size=%lu,type=%s,length=%lu,var={",
(unsigned int) MAY_HASH (w), (unsigned long) size, type, may_nops (w));
if (type == may_sum_name || type == may_product_name) {
w = may_indets (w, MAY_INDETS_NUM);
size_t n = may_nops (w);
for (size_t i = 0; i < n; i++) {
if (i != 0)
length += fprintf (stream, ",");
length += fprintf (stream, "%Y", may_op (w, i));
}
}
length += fprintf (stream, "}]@@");
} else
length = may_dump_rec (stream, w, 0);
may_log_type = org_type_logging;
return length;
}
static int
may_vcb_arginfo (const struct printf_info *info, size_t n,
int *argtypes)
{
UNUSED (info);
if (n > 0)
argtypes[0] = PA_POINTER;
return 1;
}
static void may_log_begin (void) __attribute__((constructor));
/* We let the system close the LOG itself
(Otherwise functions called by destructor can't use LOG File */
static void
may_log_begin (void)
{
const char *var;
time_t tt;
/* Grab some information */
var = getenv ("MAY_LOG_LEVEL");
may_log_level = var == NULL || *var == 0 ? 4 : atoi (var);
may_log_current = 0;
var = getenv ("MAY_LOG_SIZE");
may_log_size = var == NULL || *var == 0 ? 1000 : atol (var);
/* Get what we need to log */
may_log_type = 0;
if (getenv ("MAY_LOG_INPUT") != NULL)
may_log_type |= MAY_LOG_INPUT_F;
if (getenv ("MAY_LOG_TIME") != NULL)
may_log_type |= MAY_LOG_TIME_F;
if (getenv ("MAY_LOG_MSG") != NULL)
may_log_type |= MAY_LOG_MSG_F;
if (getenv ("MAY_LOG_STAT") != NULL)
may_log_type |= MAY_LOG_STAT_F;
if (getenv ("MAY_LOG_ALL") != NULL)
may_log_type = MAY_LOG_INPUT_F|MAY_LOG_TIME_F|MAY_LOG_MSG_F|MAY_LOG_STAT_F;
/* Register printf functions */
register_printf_function ('Y', may_vcb_printf, may_vcb_arginfo);
/* Open filename if needed */
var = getenv ("MAY_LOG_FILE");
if (var == NULL || *var == 0)
var = "may.log";
if (may_log_type != 0) {
may_log_file = fopen (var, "w");
if (may_log_file == NULL) {
fprintf (stderr, "[MAYLIB]: Can't open log file '%s' for writing.\n", var);
abort ();
} else {
fprintf (stderr, "[MAYLIB]: Open log file '%s' for writing.\n", var);
}
time (&tt);
fprintf (may_log_file, "MAY LOG FILE %s\n", ctime (&tt));
}
}
/* Return user CPU time measured in milliseconds. */
#if defined (ANSIONLY) || defined (USG) || defined (__SVR4) \
|| defined (_UNICOS) || defined(__hpux)
int
may_get_cputime (void)
{
return (int) ((unsigned long long) clock () * 1000 / CLOCKS_PER_SEC);
}
#else /* Use getrusage for cputime */
#include <sys/types.h>
#include <sys/resource.h>
int
may_get_cputime (void)
{
struct rusage rus;
getrusage (0, &rus);
return rus.ru_utime.tv_sec * 1000 + rus.ru_utime.tv_usec / 1000;
}
#endif /* cputime */
#endif /* MAY_WANT_LOGGING */