forked from eclipse-omr/omr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathomrtracemisc.cpp
436 lines (389 loc) · 13.1 KB
/
omrtracemisc.cpp
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/*******************************************************************************
*
* (c) Copyright IBM Corp. 1998, 2016
*
* This program and the accompanying materials are made available
* under the terms of the Eclipse Public License v1.0 and
* Apache License v2.0 which accompanies this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* The Apache License v2.0 is available at
* http://www.opensource.org/licenses/apache2.0.php
*
* Contributors:
* Multiple authors (IBM Corp.) - initial implementation and documentation
*******************************************************************************/
#include <string.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdlib.h>
#include <sys/types.h>
#include <time.h>
#include "omrcomp.h"
#include "omrtrace_internal.h"
#include "pool_api.h"
/*******************************************************************************
* name - getTraceLock
* description - Obtains the trace lock
* parameters - OMR_TraceThread
* returns - True or false
******************************************************************************/
int32_t
getTraceLock(OMR_TraceThread *thr)
{
incrementRecursionCounter(thr);
omrthread_monitor_enter(OMR_TRACEGLOBAL(traceLock));
return TRUE;
}
/*******************************************************************************
* name - freeTraceLock
* description - Frees the trace lock
* parameters - OMR_TraceThread
* returns - True or false
******************************************************************************/
int32_t
freeTraceLock(OMR_TraceThread *thr)
{
omrthread_monitor_exit(OMR_TRACEGLOBAL(traceLock));
decrementRecursionCounter(thr);
return TRUE;
}
void
incrementRecursionCounter(OMR_TraceThread *thr)
{
thr->recursion++;
}
void
decrementRecursionCounter(OMR_TraceThread *thr)
{
thr->recursion--;
}
/**
* Acquire the global trace lock if the trace engine's initialization phase
* has completed.
*/
void
checkGetTraceLock(OMR_TraceThread *thr)
{
if (NULL != thr) {
getTraceLock(thr);
} else {
UT_ASSERT(OMR_TRACE_ENGINE_MT_ENABLED != OMR_TRACEGLOBAL(initState));
}
}
/**
* Release the global trace lock if the trace engine's initialization phase
* has completed.
*/
void
checkFreeTraceLock(OMR_TraceThread *thr)
{
if (NULL != thr) {
freeTraceLock(thr);
} else {
UT_ASSERT(OMR_TRACE_ENGINE_MT_ENABLED != OMR_TRACEGLOBAL(initState));
}
}
/*******************************************************************************
* name - expandString
* description - take a string and substutue pid number for %p, date for
* %d and time for %t
* parameters - returnBuffer - the expanded string as return
* value - the original string
* returns - OMR_ERROR_NONE or OMR_ERROR_ILLEGAL_ARGUMENT
******************************************************************************/
omr_error_t
expandString(char *returnBuffer, const char *original, BOOLEAN atRuntime)
{
OMRPORT_ACCESS_FROM_OMRPORT(OMR_TRACEGLOBAL(portLibrary));
const char *pidTokenString = "%pid";
const char *dateTokenString = "%Y" "%m" "%d";
const char *timeTokenString = "%H" "%M" "%S";
char formatString[MAX_IMAGE_PATH_LENGTH];
char resultString[MAX_IMAGE_PATH_LENGTH];
size_t originalLen, originalCursor = 0, formatCursor = 0;
struct J9StringTokens *stringTokens;
int64_t curTime;
if ((returnBuffer == NULL) || (original == NULL)) {
return OMR_ERROR_ILLEGAL_ARGUMENT;
}
originalLen = strlen(original);
for (; originalCursor < originalLen ; originalCursor++) {
if (original[originalCursor] == '%') {
originalCursor++;
if (original[originalCursor] == 'p') {
strncpy(&formatString[formatCursor], pidTokenString, strlen(pidTokenString));
formatCursor += strlen(pidTokenString);
} else if (original[originalCursor] == 'd') {
strncpy(&formatString[formatCursor], dateTokenString, strlen(dateTokenString));
formatCursor += strlen(dateTokenString);
} else if (original[originalCursor] == 't') {
strncpy(&formatString[formatCursor], timeTokenString, strlen(timeTokenString));
formatCursor += strlen(timeTokenString);
} else {
/* character following % was not 'p', 'd' or 't' */
reportCommandLineError(atRuntime, "Invalid special character '%%%c' in a trace filename. Only %%p, %%d and %%t are allowed.", original[originalCursor]);
returnBuffer[0] = '\0';
return OMR_ERROR_ILLEGAL_ARGUMENT;
}
} else {
formatString[formatCursor] = original[originalCursor];
formatCursor++;
}
if (formatCursor >= MAX_IMAGE_PATH_LENGTH - 1) {
formatCursor = MAX_IMAGE_PATH_LENGTH - 1;
break;
}
}
formatString[formatCursor] = '\0';
curTime = omrtime_current_time_millis();
stringTokens = omrstr_create_tokens(curTime);
if (NULL == stringTokens) {
returnBuffer[0] = '\0';
return OMR_ERROR_ILLEGAL_ARGUMENT;
}
if (omrstr_subst_tokens(resultString, MAX_IMAGE_PATH_LENGTH, formatString, stringTokens) > MAX_IMAGE_PATH_LENGTH) {
returnBuffer[0] = '\0';
omrstr_free_tokens(stringTokens);
return OMR_ERROR_ILLEGAL_ARGUMENT;
}
omrstr_free_tokens(stringTokens);
strncpy(returnBuffer, resultString, 255);
returnBuffer[255] = '\0';
return OMR_ERROR_NONE;
}
/*******************************************************************************
* name - listCounters
* description - Print out the entire trace counter table.
* parameters - void
* returns - void
******************************************************************************/
void
listCounters(void)
{
int i;
#define TEMPBUFLEN 150
char tempBuf[TEMPBUFLEN];
intptr_t f;
UtComponentData *compData = OMR_TRACEGLOBAL(componentList)->head;
OMRPORT_ACCESS_FROM_OMRPORT(OMR_TRACEGLOBAL(portLibrary));
UT_DBGOUT(1, ("<UT> Listing trace counters\n"));
if ((f = omrfile_open("utTrcCounters", EsOpenText | EsOpenWrite | EsOpenTruncate | EsOpenCreateNoTag, 0)) < 0) {
if ((f = omrfile_open("utTrcCounters", EsOpenText | EsOpenWrite | EsOpenCreate | EsOpenCreateNoTag, 0666)) < 0) {
/* Unable to open tracepoint counter file %s, counters redirected to stderr. */
}
}
/* Writing trace count info to %s */
/* list currently loaded components */
while (compData != NULL) {
if (compData->tracepointcounters != NULL) {
for (i = 0; i < compData->tracepointCount; i++) {
if (compData->tracepointcounters[i] > 0) {
if (f < 0) {
omrtty_err_printf("%s.%d %ld \n", compData->qualifiedComponentName, i, compData->tracepointcounters[i]);
} else {
omrstr_printf(tempBuf, TEMPBUFLEN, "%s.%d %lld \n", compData->qualifiedComponentName, i, compData->tracepointcounters[i]);
/* convert to ebcdic if on zos */
omrfile_write_text(f, tempBuf, strlen(tempBuf));
}
}
}
}
compData = compData->next;
}
/* list the unloaded components */
compData = OMR_TRACEGLOBAL(unloadedComponentList)->head;
while (compData != NULL) {
if (compData->tracepointcounters != NULL) {
for (i = 0; i < compData->tracepointCount; i++) {
if (compData->tracepointcounters[i] > 0) {
if (f < 0) {
omrtty_err_printf("%s.%d %ld \n", compData->qualifiedComponentName, i, compData->tracepointcounters[i]);
} else {
omrstr_printf(tempBuf, TEMPBUFLEN, "%s.%d %lld \n", compData->qualifiedComponentName, i, compData->tracepointcounters[i]);
/* convert to ebcdic if on zos */
omrfile_write_text(f, tempBuf, strlen(tempBuf));
}
}
}
}
compData = compData->next;
}
if (f > 0) {
omrfile_close(f);
}
#undef TEMPBUFLEN
}
/*******************************************************************************
* name - getTimestamp
* description - Get the time in various component units
* parameters - pointers to result for hours, minutes, seconds and milliseconds
* returns - void
*
******************************************************************************/
void
getTimestamp(int64_t time, uint32_t *pHours, uint32_t *pMinutes, uint32_t *pSeconds, uint32_t *pMillis)
{
uint32_t hours, minutes, seconds, millis;
#define MILLIS2SECONDS 1000
#define SECONDS2MINUTES 60
#define MINUTES2HOURS 60
#define HOURS2DAYS 24
seconds = (uint32_t)(time / MILLIS2SECONDS);
millis = (uint32_t)(time % MILLIS2SECONDS);
minutes = seconds / SECONDS2MINUTES;
seconds = seconds % SECONDS2MINUTES;
hours = minutes / MINUTES2HOURS;
minutes = minutes % MINUTES2HOURS;
hours = hours % HOURS2DAYS;
*pHours = hours;
*pMinutes = minutes;
*pSeconds = seconds;
*pMillis = millis;
#undef MILLIS2SECONDS
#undef SECONDS2MINUTES
#undef MINUTES2HOURS
#undef HOURS2DAYS
}
/*******************************************************************************
* name - addTraceConfig
* description - add trace configuration command to list stored in utglobal data
* parameters - OMR_TraceThread, trace configuration command
* returns - return code
******************************************************************************/
omr_error_t
addTraceConfig(OMR_TraceThread *thr, const char *cmd)
{
UtTraceCfg *tmp;
omr_error_t rc = OMR_ERROR_NONE;
OMRPORT_ACCESS_FROM_OMRPORT(OMR_TRACEGLOBAL(portLibrary));
/*
* Obtain and initialize a config buffer
*/
UtTraceCfg *cfg = (UtTraceCfg *)omrmem_allocate_memory(sizeof(UtTraceCfg) + strlen(cmd) + 1, OMRMEM_CATEGORY_TRACE);
if (NULL != cfg) {
initHeader(&cfg->header, UT_TRACE_CONFIG_NAME, sizeof(UtTraceCfg) +
strlen(cmd) + 1);
cfg->next = NULL;
strcpy(cfg->command, cmd);
/*
* Add it to the end of config chain while holding traceLock
*/
checkGetTraceLock(thr);
if ((tmp = OMR_TRACEGLOBAL(config)) == NULL) {
OMR_TRACEGLOBAL(config) = cfg;
} else {
while (tmp->next != NULL) {
tmp = tmp->next;
}
tmp->next = cfg;
}
/* We should update the trace header here (if it has already been initialised) to
* reflect the change in trace settings.
* Unfortunately it may be in use by a snap dump or we may have returned a pointer to
* it via trcGetTraceMetadata so freeing it or nulling it is unsafe.
* For now we are leaving it unchanged.
* If nothing has requested the trace header yet and it is NULL then it will be created
* correctly when it is first used.
*/
checkFreeTraceLock(thr);
} else {
UT_DBGOUT(1, ("<UT> Out of memory in addTraceConfig\n"));
rc = OMR_ERROR_OUT_OF_NATIVE_MEMORY;
}
return rc;
}
/*******************************************************************************
* name - addTraceConfigKeyValuePair
* description - add trace configuration command key/value pair to list
* parameters - OMR_TraceThread, trace configuration command key, trace configuration
* value
* value can safely be null, but null key is an error.
* returns - OMR_ERROR_NONE if it worked,
* OMR_ERROR_OUT_OF_NATIVE_MEMORY or OMR_ERROR_ILLEGAL_ARGUMENT if problem
******************************************************************************/
omr_error_t
addTraceConfigKeyValuePair(OMR_TraceThread *thr, const char *cmdKey, const char *cmdValue)
{
uintptr_t cmdLen = 1; /* terminating null */
char *cmd = NULL;
omr_error_t rc = OMR_ERROR_NONE;
int32_t addBraces = FALSE;
OMRPORT_ACCESS_FROM_OMRPORT(OMR_TRACEGLOBAL(portLibrary));
/* could use a sprintf equivalent, but would have to mock up empty strings
* for cases where cmdValue was null
*/
if (cmdKey != NULL) {
cmdLen += strlen(cmdKey);
} else {
UT_DBGOUT(1, ("<UT> Out of memory recording option : \"%s\"\n", cmdKey));
return OMR_ERROR_ILLEGAL_ARGUMENT;
}
if (cmdValue != NULL) {
cmdLen += strlen("=");
cmdLen += strlen(cmdValue);
if (strchr(cmdValue, ',') != NULL) {
/* we have a comma separated item whose braces will have been stripped */
addBraces = TRUE;
cmdLen += strlen("{}");
}
}
cmd = (char *)omrmem_allocate_memory(cmdLen, OMRMEM_CATEGORY_TRACE);
if (NULL == cmd) {
UT_DBGOUT(1, ("<UT> Out of memory recording option : \"%s\"\n", cmdKey));
return OMR_ERROR_OUT_OF_NATIVE_MEMORY;
}
if (cmdKey != NULL) {
strcpy(cmd, cmdKey);
}
if (cmdValue != NULL) {
strcat(cmd, "=");
if (addBraces == TRUE) {
strcat(cmd, "{");
}
strcat(cmd, cmdValue);
if (addBraces == TRUE) {
strcat(cmd, "}");
}
}
rc = addTraceConfig(thr, cmd);
omrmem_free_memory(cmd);
return rc;
}
/*******************************************************************************
* name - initHeader
* description - Header initialization routine
* parameters - UtDataHeader, name and size
* returns - None
******************************************************************************/
void
initHeader(UtDataHeader *header, const char *name, uintptr_t size)
{
memcpy(header->eyecatcher, name, 4);
header->length = (int32_t)size;
header->version = UT_VERSION;
header->modification = UT_MODIFICATION;
}
/*******************************************************************************
* name - hexStringLength
* description - Return the number of consecutive hex characters
* parameters - string pointer
* returns - Length of hex string
******************************************************************************/
int
hexStringLength(const char *str)
{
int i;
for (i = 0;
((str[i] >= '0' &&
str[i] <= '9') ||
(str[i] >= 'A' &&
str[i] <= 'F') ||
(str[i] >= 'a' &&
str[i] <= 'f'));
i++) {
}
return i;
}