author | rkennke |
Fri, 12 Oct 2018 16:25:24 +0200 | |
changeset 52107 | 0c1e44da019c |
parent 48235 | 8db54e2c453b |
child 53979 | 70c7c4db9c9a |
child 56230 | 489867818774 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
48235
8db54e2c453b
8192978: Missing checks and small fixes in jdwp library
clanger
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
#include "util.h" |
|
27 |
||
28 |
#include <time.h> |
|
29 |
#include <errno.h> |
|
30 |
#include <sys/types.h> |
|
31 |
||
32 |
#include "proc_md.h" |
|
33 |
||
34 |
#include "log_messages.h" |
|
35 |
||
36 |
#ifdef JDWP_LOGGING |
|
37 |
||
38 |
#define MAXLEN_INTEGER 20 |
|
39 |
#define MAXLEN_FILENAME 256 |
|
40 |
#define MAXLEN_TIMESTAMP 80 |
|
41 |
#define MAXLEN_LOCATION (MAXLEN_FILENAME+MAXLEN_INTEGER+16) |
|
42 |
#define MAXLEN_MESSAGE 256 |
|
43 |
#define MAXLEN_EXEC (MAXLEN_FILENAME*2+MAXLEN_INTEGER+16) |
|
44 |
||
45 |
static MUTEX_T my_mutex = MUTEX_INIT; |
|
46 |
||
47 |
/* Static variables (should be protected with mutex) */ |
|
48 |
static int logging; |
|
49 |
static FILE * log_file; |
|
50 |
static char logging_filename[MAXLEN_FILENAME+1+6]; |
|
51 |
static char location_stamp[MAXLEN_LOCATION+1]; |
|
52 |
static PID_T processPid; |
|
53 |
static int open_count; |
|
54 |
||
55 |
/* Ascii id of current native thread. */ |
|
56 |
static void |
|
57 |
get_time_stamp(char *tbuf, size_t ltbuf) |
|
58 |
{ |
|
23566
760a74c1b589
8037825: Fix warnings and enable "warnings as errors" in serviceability native libraries
sla
parents:
5506
diff
changeset
|
59 |
char timestamp_prefix[MAXLEN_TIMESTAMP+1]; |
760a74c1b589
8037825: Fix warnings and enable "warnings as errors" in serviceability native libraries
sla
parents:
5506
diff
changeset
|
60 |
char timestamp_postfix[MAXLEN_TIMESTAMP+1]; |
2 | 61 |
unsigned millisecs = 0; |
62 |
time_t t = 0; |
|
63 |
||
64 |
GETMILLSECS(millisecs); |
|
23566
760a74c1b589
8037825: Fix warnings and enable "warnings as errors" in serviceability native libraries
sla
parents:
5506
diff
changeset
|
65 |
if ( time(&t) == (time_t)(-1) ) { |
2 | 66 |
t = 0; |
23566
760a74c1b589
8037825: Fix warnings and enable "warnings as errors" in serviceability native libraries
sla
parents:
5506
diff
changeset
|
67 |
} |
760a74c1b589
8037825: Fix warnings and enable "warnings as errors" in serviceability native libraries
sla
parents:
5506
diff
changeset
|
68 |
/* Break this up so that the format strings are string literals |
760a74c1b589
8037825: Fix warnings and enable "warnings as errors" in serviceability native libraries
sla
parents:
5506
diff
changeset
|
69 |
and we avoid a compiler warning. */ |
760a74c1b589
8037825: Fix warnings and enable "warnings as errors" in serviceability native libraries
sla
parents:
5506
diff
changeset
|
70 |
(void)strftime(timestamp_prefix, sizeof(timestamp_prefix), |
760a74c1b589
8037825: Fix warnings and enable "warnings as errors" in serviceability native libraries
sla
parents:
5506
diff
changeset
|
71 |
"%d.%m.%Y %T", localtime(&t)); |
760a74c1b589
8037825: Fix warnings and enable "warnings as errors" in serviceability native libraries
sla
parents:
5506
diff
changeset
|
72 |
(void)strftime(timestamp_postfix, sizeof(timestamp_postfix), |
760a74c1b589
8037825: Fix warnings and enable "warnings as errors" in serviceability native libraries
sla
parents:
5506
diff
changeset
|
73 |
"%Z", localtime(&t)); |
760a74c1b589
8037825: Fix warnings and enable "warnings as errors" in serviceability native libraries
sla
parents:
5506
diff
changeset
|
74 |
(void)snprintf(tbuf, ltbuf, |
760a74c1b589
8037825: Fix warnings and enable "warnings as errors" in serviceability native libraries
sla
parents:
5506
diff
changeset
|
75 |
"%s.%.3d %s", timestamp_prefix, |
760a74c1b589
8037825: Fix warnings and enable "warnings as errors" in serviceability native libraries
sla
parents:
5506
diff
changeset
|
76 |
(int)(millisecs), timestamp_postfix); |
2 | 77 |
} |
78 |
||
79 |
/* Get basename of filename */ |
|
80 |
static const char * |
|
81 |
file_basename(const char *file) |
|
82 |
{ |
|
83 |
char *p1; |
|
84 |
char *p2; |
|
85 |
||
86 |
if ( file==NULL ) |
|
87 |
return "unknown"; |
|
88 |
p1 = strrchr(file, '\\'); |
|
89 |
p2 = strrchr(file, '/'); |
|
90 |
p1 = ((p1 > p2) ? p1 : p2); |
|
91 |
if (p1 != NULL) { |
|
92 |
file = p1 + 1; |
|
93 |
} |
|
94 |
return file; |
|
95 |
} |
|
96 |
||
97 |
/* Fill in the exact source location of the LOG entry. */ |
|
98 |
static void |
|
99 |
fill_location_stamp(const char *flavor, const char *file, int line) |
|
100 |
{ |
|
101 |
(void)snprintf(location_stamp, sizeof(location_stamp), |
|
102 |
"%s:\"%s\":%d;", |
|
103 |
flavor, file_basename(file), line); |
|
104 |
location_stamp[sizeof(location_stamp)-1] = 0; |
|
105 |
} |
|
106 |
||
107 |
/* Begin a log entry. */ |
|
108 |
void |
|
109 |
log_message_begin(const char *flavor, const char *file, int line) |
|
110 |
{ |
|
111 |
MUTEX_LOCK(my_mutex); /* Unlocked in log_message_end() */ |
|
112 |
if ( logging ) { |
|
113 |
location_stamp[0] = 0; |
|
114 |
fill_location_stamp(flavor, file, line); |
|
115 |
} |
|
116 |
} |
|
117 |
||
118 |
/* Standard Logging Format Entry */ |
|
119 |
static void |
|
120 |
standard_logging_format(FILE *fp, |
|
121 |
const char *datetime, |
|
122 |
const char *level, |
|
123 |
const char *product, |
|
124 |
const char *module, |
|
125 |
const char *optional, |
|
126 |
const char *messageID, |
|
127 |
const char *message) |
|
128 |
{ |
|
129 |
const char *format; |
|
130 |
||
131 |
/* "[#|Date&Time&Zone|LogLevel|ProductName|ModuleID| |
|
132 |
* OptionalKey1=Value1;OptionalKeyN=ValueN|MessageID:MessageText|#]\n" |
|
133 |
*/ |
|
134 |
||
135 |
format="[#|%s|%s|%s|%s|%s|%s:%s|#]\n"; |
|
136 |
||
137 |
print_message(fp, "", "", format, |
|
138 |
datetime, |
|
139 |
level, |
|
140 |
product, |
|
141 |
module, |
|
142 |
optional, |
|
143 |
messageID, |
|
144 |
message); |
|
145 |
} |
|
146 |
||
147 |
/* End a log entry */ |
|
148 |
void |
|
149 |
log_message_end(const char *format, ...) |
|
150 |
{ |
|
151 |
if ( logging ) { |
|
152 |
va_list ap; |
|
153 |
THREAD_T tid; |
|
154 |
char datetime[MAXLEN_TIMESTAMP+1]; |
|
155 |
const char *level; |
|
156 |
const char *product; |
|
157 |
const char *module; |
|
158 |
char optional[MAXLEN_INTEGER+6+MAXLEN_INTEGER+6+MAXLEN_LOCATION+1]; |
|
159 |
const char *messageID; |
|
160 |
char message[MAXLEN_MESSAGE+1]; |
|
161 |
||
162 |
/* Grab the location, start file if needed, and clear the lock */ |
|
163 |
if ( log_file == NULL && open_count == 0 && logging_filename[0] != 0 ) { |
|
164 |
open_count++; |
|
165 |
log_file = fopen(logging_filename, "w"); |
|
166 |
if ( log_file!=NULL ) { |
|
167 |
(void)setvbuf(log_file, NULL, _IOLBF, BUFSIZ); |
|
168 |
} else { |
|
169 |
logging = 0; |
|
170 |
} |
|
171 |
} |
|
172 |
||
173 |
if ( log_file != NULL ) { |
|
174 |
||
175 |
/* Get the rest of the needed information */ |
|
176 |
tid = GET_THREAD_ID(); |
|
177 |
level = "FINEST"; /* FIXUP? */ |
|
178 |
product = "J2SE1.5"; /* FIXUP? */ |
|
179 |
module = "jdwp"; /* FIXUP? */ |
|
180 |
messageID = ""; /* FIXUP: Unique message string ID? */ |
|
181 |
(void)snprintf(optional, sizeof(optional), |
|
182 |
"LOC=%s;PID=%d;THR=t@%d", |
|
183 |
location_stamp, |
|
184 |
(int)processPid, |
|
23566
760a74c1b589
8037825: Fix warnings and enable "warnings as errors" in serviceability native libraries
sla
parents:
5506
diff
changeset
|
185 |
(int)(intptr_t)tid); |
2 | 186 |
|
187 |
/* Construct message string. */ |
|
188 |
va_start(ap, format); |
|
189 |
(void)vsnprintf(message, sizeof(message), format, ap); |
|
48235
8db54e2c453b
8192978: Missing checks and small fixes in jdwp library
clanger
parents:
47216
diff
changeset
|
190 |
message[sizeof(message) - 1] = 0; |
2 | 191 |
va_end(ap); |
192 |
||
193 |
get_time_stamp(datetime, sizeof(datetime)); |
|
194 |
||
195 |
/* Send out standard logging format message */ |
|
196 |
standard_logging_format(log_file, |
|
197 |
datetime, |
|
198 |
level, |
|
199 |
product, |
|
200 |
module, |
|
201 |
optional, |
|
202 |
messageID, |
|
203 |
message); |
|
204 |
} |
|
205 |
location_stamp[0] = 0; |
|
206 |
} |
|
207 |
MUTEX_UNLOCK(my_mutex); /* Locked in log_message_begin() */ |
|
208 |
} |
|
209 |
||
210 |
#endif |
|
211 |
||
212 |
/* Set up the logging with the name of a logging file. */ |
|
213 |
void |
|
214 |
setup_logging(const char *filename, unsigned flags) |
|
215 |
{ |
|
216 |
#ifdef JDWP_LOGGING |
|
217 |
FILE *fp = NULL; |
|
218 |
||
219 |
/* Turn off logging */ |
|
220 |
logging = 0; |
|
221 |
gdata->log_flags = 0; |
|
222 |
||
223 |
/* Just return if not doing logging */ |
|
224 |
if ( filename==NULL || flags==0 ) |
|
225 |
return; |
|
226 |
||
227 |
/* Create potential filename for logging */ |
|
228 |
processPid = GETPID(); |
|
229 |
(void)snprintf(logging_filename, sizeof(logging_filename), |
|
230 |
"%s.%d", filename, (int)processPid); |
|
231 |
||
232 |
/* Turn on logging (do this last) */ |
|
233 |
logging = 1; |
|
234 |
gdata->log_flags = flags; |
|
235 |
||
236 |
#endif |
|
237 |
} |
|
238 |
||
239 |
/* Finish up logging, flush output to the logfile. */ |
|
240 |
void |
|
26223
3830bfd8440a
8049226: com/sun/jdi/OptionTest.java test times out again
dsamersoff
parents:
25859
diff
changeset
|
241 |
finish_logging() |
2 | 242 |
{ |
243 |
#ifdef JDWP_LOGGING |
|
244 |
MUTEX_LOCK(my_mutex); |
|
245 |
if ( logging ) { |
|
246 |
logging = 0; |
|
247 |
if ( log_file != NULL ) { |
|
248 |
(void)fflush(log_file); |
|
249 |
(void)fclose(log_file); |
|
250 |
log_file = NULL; |
|
251 |
} |
|
252 |
} |
|
253 |
MUTEX_UNLOCK(my_mutex); |
|
254 |
#endif |
|
255 |
} |