-
Notifications
You must be signed in to change notification settings - Fork 721
/
trclog.c
2716 lines (2373 loc) · 86.3 KB
/
trclog.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
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright IBM Corp. and others 1998
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution and
* is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following
* Secondary Licenses when the conditions for such availability set
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
* General Public License, version 2 with the GNU Classpath
* Exception [1] and GNU General Public License, version 2 with the
* OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] https://openjdk.org/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
*******************************************************************************/
#include "j9cfg.h"
#include "rastrace_internal.h"
#include "omrutilbase.h"
#include "omrstdarg.h"
#include "j9trcnls.h"
#include "trctrigger.h"
#include "j9rastrace.h"
#define MAX_QUALIFIED_NAME_LENGTH 16
static UtProcessorInfo *getProcessorInfo(void);
static void traceExternal(UtThreadData **thr, UtListenerWrapper func, void *userData, const char *modName, uint32_t traceId, const char *spec, va_list varArgs);
static void raiseAssertion(UtThreadData **thread, UtModuleInfo *modInfo, uint32_t traceId);
static void fireTriggerHit(UtThreadData **thread, char *compName, uint32_t traceId, TriggerPhase phase);
static void callSubscriber(UtThreadData **thr, UtSubscription *subscription, UtModuleInfo *modInfo, uint32_t traceId, va_list args);
char pointerSpec[2] = {(char)sizeof(char *), '\0'};
extern omrthread_tls_key_t j9rasTLSKey;
#define UNKNOWN_SERVICE_LEVEL "Unknown version"
/*******************************************************************************
* name - initEvent
* description - Initializes a monitor. Takes a monitor name that will be copied.
* parameters - UtEventSem
* returns - int32_t
******************************************************************************/
omr_error_t
initEvent(UtEventSem **sem, char* name)
{
PORT_ACCESS_FROM_PORT(UT_GLOBAL(portLibrary));
omr_error_t ret = OMR_ERROR_NONE;
UtEventSem *newSem;
UT_DBGOUT(2, ("<UT> initEvent called\n"));
newSem = j9mem_allocate_memory(sizeof(UtEventSem), OMRMEM_CATEGORY_TRACE);
if (newSem !=NULL) {
omrthread_monitor_t monitor;
memset(newSem, '\0', sizeof(UtEventSem));
initHeader(&newSem->header, "UTES", sizeof(UtEventSem));
ret = (int32_t)omrthread_monitor_init_with_name(&monitor, 0, name);
if (ret == 0) {
newSem->pfmInfo.sem = monitor;
*sem = newSem;
}
} else {
ret = OMR_ERROR_OUT_OF_NATIVE_MEMORY;
}
UT_DBGOUT(2, ("<UT> initEvent returned %d for semaphore %p\n", ret, newSem));
return ret;
}
/*******************************************************************************
* name - initEvent
* description - Frees a monitor.
* parameters - UtEventSem
* returns - void
******************************************************************************/
void
destroyEvent(UtEventSem *sem)
{
PORT_ACCESS_FROM_PORT(UT_GLOBAL(portLibrary));
intptr_t ret = 0;
UT_DBGOUT(2, ("<UT> destroyEvent called for %p\n", sem));
ret = omrthread_monitor_destroy(sem->pfmInfo.sem);
if (0 == ret) {
/* Prevent users that keep a stale pointer to the sem from
* getting into the omrthread library with it.
*/
sem->pfmInfo.sem = NULL;
j9mem_free_memory(sem);
}
}
/*******************************************************************************
* name - waitEvent
* description - Waits for an event to occur
* parameters - UtEventSem
* returns - void
******************************************************************************/
void
waitEvent(UtEventSem * sem)
{
omrthread_monitor_enter(sem->pfmInfo.sem);
if (sem->pfmInfo.flags != UT_SEM_POSTED) {
sem->pfmInfo.flags = UT_SEM_WAITING;
omrthread_monitor_wait(sem->pfmInfo.sem);
if (omrthread_monitor_num_waiting(sem->pfmInfo.sem) == 0) {
sem->pfmInfo.flags = 0;
}
} else {
sem->pfmInfo.flags = 0;
}
omrthread_monitor_exit(sem->pfmInfo.sem);
}
/*******************************************************************************
* name - postEvent
* description - Wakes up the trace write thread
* parameters - UtEventSem
* returns - void
******************************************************************************/
void
postEvent(UtEventSem * sem)
{
omrthread_monitor_enter(sem->pfmInfo.sem);
if (sem->pfmInfo.flags == UT_SEM_WAITING) {
omrthread_monitor_notify(sem->pfmInfo.sem);
} else {
sem->pfmInfo.flags = UT_SEM_POSTED;
}
omrthread_monitor_exit(sem->pfmInfo.sem);
}
/*******************************************************************************
* name - postEventAll
* description - Wakes all threads waiting on this event
* parameters - UtEventSem
* returns - void
******************************************************************************/
void
postEventAll(UtEventSem * sem)
{
UT_DBGOUT(2, ("<UT> postEventAll called for semaphore %p\n", sem));
omrthread_monitor_enter(sem->pfmInfo.sem);
if (omrthread_monitor_num_waiting(sem->pfmInfo.sem) == 0) {
sem->pfmInfo.flags = UT_SEM_POSTED;
} else {
sem->pfmInfo.flags = 0;
omrthread_monitor_notify_all(sem->pfmInfo.sem);
}
omrthread_monitor_exit(sem->pfmInfo.sem);
UT_DBGOUT(2, ("<UT> postEventAll for semaphore %p done\n", sem));
}
/*******************************************************************************
* name - initTraceHeader
* description - Initializes the trace header.
* parameters - void
* returns - OMR error code
******************************************************************************/
omr_error_t
initTraceHeader(void)
{
int size;
UtTraceFileHdr *trcHdr;
char *ptr;
UtTraceCfg *cfg;
int actSize, srvSize, startSize;
UtProcessorInfo *procinfo;
PORT_ACCESS_FROM_PORT(UT_GLOBAL(portLibrary));
/*
* Return if it already exists
*/
if (UT_GLOBAL(traceHeader) != NULL) {
return OMR_ERROR_NONE;
}
size = offsetof(UtTraceFileHdr, traceSection);
size += sizeof(UtTraceSection);
/*
* Calculate length of service section
*/
srvSize = offsetof(UtServiceSection, level);
if (UT_GLOBAL(serviceInfo) == NULL) {
/* Make sure we allocate this so we can free it on shutdown. */
UT_GLOBAL(serviceInfo) = j9mem_allocate_memory(sizeof(UNKNOWN_SERVICE_LEVEL) + 1, OMRMEM_CATEGORY_TRACE);
if( NULL == UT_GLOBAL(serviceInfo) ) {
return OMR_ERROR_OUT_OF_NATIVE_MEMORY;
}
strcpy(UT_GLOBAL(serviceInfo), UNKNOWN_SERVICE_LEVEL);
}
srvSize += (int)strlen(UT_GLOBAL(serviceInfo)) + 1;
srvSize = ((srvSize +
UT_STRUCT_ALIGN - 1) / UT_STRUCT_ALIGN) * UT_STRUCT_ALIGN;
size += srvSize;
/*
* Calculate length of startup options
*/
startSize = offsetof(UtStartupSection, options);
if (UT_GLOBAL(properties) != NULL){
startSize += (int)strlen(UT_GLOBAL(properties)) + 1;
}
startSize = ((startSize +
UT_STRUCT_ALIGN - 1) / UT_STRUCT_ALIGN) * UT_STRUCT_ALIGN;
startSize = ((startSize + 3) / 4) * 4;
size += startSize;
/*
* Calculate length of trace activation commands
*/
actSize = offsetof(UtActiveSection, active);
for (cfg = UT_GLOBAL(config);
cfg != NULL;
cfg = cfg->next) {
actSize += (int)strlen(cfg->command) + 1;
}
actSize = ((actSize +
UT_STRUCT_ALIGN - 1) / UT_STRUCT_ALIGN) * UT_STRUCT_ALIGN;
actSize = ((actSize + 3) / 4) * 4;
size += actSize;
/*
* Add length of UtProcSection
*/
size += sizeof(UtProcSection);
if ((trcHdr = j9mem_allocate_memory(size, OMRMEM_CATEGORY_TRACE )) == NULL) {
UT_DBGOUT(1, ("<UT> Out of memory in initTraceHeader\n"));
return OMR_ERROR_OUT_OF_NATIVE_MEMORY;
}
memset(trcHdr, '\0', size);
initHeader(&trcHdr->header, UT_TRACE_HEADER_NAME, size);
trcHdr->bufferSize = UT_GLOBAL(bufferSize);
trcHdr->endianSignature = UT_ENDIAN_SIGNATURE;
trcHdr->traceStart = offsetof(UtTraceFileHdr, traceSection);
trcHdr->serviceStart = trcHdr->traceStart + sizeof(UtTraceSection);
trcHdr->startupStart = trcHdr->serviceStart + srvSize;
trcHdr->activeStart = trcHdr->startupStart + startSize;
trcHdr->processorStart = trcHdr->activeStart + actSize;
/*
* Initialize trace section
*/
ptr = (char *)trcHdr + trcHdr->traceStart;
initHeader((UtDataHeader *)ptr, UT_TRACE_SECTION_NAME,
sizeof(UtTraceSection));
((UtTraceSection *)ptr)->startPlatform = UT_GLOBAL(startPlatform);
((UtTraceSection *)ptr)->startSystem = UT_GLOBAL(startSystem);
((UtTraceSection *)ptr)->type = UT_GLOBAL(traceInCore) ? UT_TRACE_INTERNAL : UT_TRACE_EXTERNAL;
((UtTraceSection *)ptr)->generations = UT_GLOBAL(traceGenerations);
((UtTraceSection *)ptr)->pointerSize = sizeof(void *);
/*
* Initialize service level section
*/
ptr = (char *)trcHdr + trcHdr->serviceStart;
initHeader((UtDataHeader *)ptr, UT_SERVICE_SECTION_NAME, srvSize);
ptr += offsetof(UtServiceSection, level);
strcpy(ptr, UT_GLOBAL(serviceInfo));
/*
* Initialize startup option section
*/
ptr = (char *)trcHdr + trcHdr->startupStart;
initHeader((UtDataHeader *)ptr, UT_STARTUP_SECTION_NAME, startSize);
ptr += offsetof(UtStartupSection, options);
if (UT_GLOBAL(properties) != NULL) {
strcpy(ptr, UT_GLOBAL(properties));
/*ptr += strlen(UT_GLOBAL(properties)) + 1;*/
}
/*
* Fill in UtActiveSection with trace activation commands
*/
ptr = (char *)trcHdr + trcHdr->activeStart;
initHeader((UtDataHeader *)ptr, UT_ACTIVE_SECTION_NAME, actSize);
ptr += offsetof(UtActiveSection, active);
for (cfg = UT_GLOBAL(config);
cfg != NULL;
cfg = cfg->next) {
strcpy(ptr, cfg->command);
ptr += strlen(cfg->command) + 1;
}
/*
* Initialize UtProcSection
*/
ptr = (char *)trcHdr + trcHdr->processorStart;
initHeader((UtDataHeader *)ptr, UT_PROC_SECTION_NAME,
sizeof(UtProcSection));
ptr += offsetof(UtProcSection, processorInfo);
procinfo = getProcessorInfo();
if (procinfo == NULL){
return OMR_ERROR_OUT_OF_NATIVE_MEMORY;
} else {
memcpy(ptr, procinfo, sizeof(UtProcessorInfo));
j9mem_free_memory(procinfo);
}
UT_GLOBAL(traceHeader) = trcHdr;
return OMR_ERROR_NONE;
}
/*******************************************************************************
* name - setTraceType
* description - Sets the trace file header as internal or external
* parameters - UtThreadData
* returns - void
******************************************************************************/
static void
setTraceType(int bufferType)
{
UtTraceFileHdr *trcHdr = UT_GLOBAL(traceHeader);
char *ptr;
/*
* Initialize trace section
*/
ptr = (char *)trcHdr + trcHdr->traceStart;
((UtTraceSection *)ptr)->type = UT_GLOBAL(traceInCore) ? UT_TRACE_INTERNAL : UT_TRACE_EXTERNAL;
if (bufferType == UT_NORMAL_BUFFER) {
((UtTraceSection *)ptr)->generations = UT_GLOBAL(traceGenerations);
} else if (bufferType == UT_EXCEPTION_BUFFER ) {
((UtTraceSection *)ptr)->generations = 1;
}
}
/*******************************************************************************
* name - freeBuffers
* description - Place trace buffer(s) on global free queue
* parameters - thr, UtTraceBuffer *
* returns - Nothing
******************************************************************************/
void
freeBuffers(qMessage *msg)
{
UtTraceBuffer *nextBuf, *trcBuf;
uint32_t newFlags = 0;
uint32_t oldFlags = 0;
if (msg == NULL || msg->data == NULL){
return;
}
trcBuf = (UtTraceBuffer*)msg->data;
do {
oldFlags = trcBuf->flags;
newFlags = ~UT_TRC_BUFFER_WRITE & ~UT_TRC_BUFFER_PURGE & ~UT_TRC_BUFFER_ACTIVE & oldFlags;
} while (!twCompareAndSwap32((unsigned int *)(&trcBuf->flags),
oldFlags,
newFlags));
if (oldFlags & UT_TRC_BUFFER_PURGE) {
if (UT_GLOBAL(traceInCore)) {
UtTraceBuffer *lastQueued = NULL;
UtTraceBuffer *nextQueued = NULL;
nextBuf = trcBuf->next;
/* Most of the time we'll only have one buffer to deal with if we're using
* in core trace. The exception is when we've moved from external trace
* to in core trace where our last subscriber has deregistered.
* In this case it's possible that not all buffers in the queue have been
* written out by the time we reach here (buffers will be processed in order
* from the queue, but we call freeBuffers directly under some circumstances).
*/
for (nextQueued = nextBuf; nextQueued != NULL && nextQueued != trcBuf; nextQueued = nextQueued->next) {
if ((nextQueued->flags & UT_TRC_BUFFER_WRITE)) {
lastQueued = nextQueued;
}
}
if (lastQueued != NULL) {
UT_DBGOUT(5, ("<UT> found a queued buffer in in-core trace mode: " UT_POINTER_SPEC "\n", lastQueued));
/* if there is a buffer queued for writing then make that purge instead of this one */
do {
oldFlags = lastQueued->flags;
newFlags = UT_TRC_BUFFER_PURGE | oldFlags;
} while ((oldFlags & UT_TRC_BUFFER_WRITE) && !twCompareAndSwap32((unsigned int *)(&lastQueued->flags),
oldFlags,
newFlags));
/* check to make sure that state didn't change under us */
if (oldFlags & UT_TRC_BUFFER_WRITE) {
/* this chain will be purged when lastQueued is written so bail */
return;
}
}
}
nextBuf = trcBuf->next;
/*
* If this is a circular chain of buffers then break the circle
*/
if (nextBuf != NULL) {
trcBuf->next = NULL;
} else {
nextBuf = trcBuf;
}
UT_DBGOUT(5, ("<UT> adding buffer " UT_POINTER_SPEC " to free list\n", nextBuf));
/* sanity check the chain */
if (UT_GLOBAL(traceDebug) > 0) {
UtTraceBuffer *buf;
for (buf = nextBuf; buf != NULL; buf = buf->next) {
DBG_ASSERT((UT_GLOBAL(traceInCore) || buf->queueData.next == CLEANING_MSG_FLAG || buf->flags & UT_TRC_BUFFER_NEW) && buf->queueData.referenceCount == 0 && buf->queueData.subscriptions == 0 && buf->queueData.pauseCount == 0);
}
}
omrthread_monitor_enter(UT_GLOBAL(freeQueueLock));
trcBuf->next = UT_GLOBAL(freeQueue);
UT_GLOBAL(freeQueue) = nextBuf;
omrthread_monitor_exit(UT_GLOBAL(freeQueueLock));
}
}
/*******************************************************************************
* name - openTraceFile
* description - Open a trace file
* parameters - UtThreadData, filename or NULL for external trace
* returns - The file handle or -1 if any errors encountered
******************************************************************************/
static intptr_t
openTraceFile(char *filename)
{
intptr_t trcFile;
char replaceChar[36] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
PORT_ACCESS_FROM_PORT(UT_GLOBAL(portLibrary));
/*
* Check for external trace file
*/
if (filename == NULL) {
filename = UT_GLOBAL(traceFilename);
/*
* Check for multi-generation mode
*/
if (UT_GLOBAL(traceGenerations) > 1) {
*(UT_GLOBAL(generationChar)) =
replaceChar[UT_GLOBAL(nextGeneration)];
(UT_GLOBAL(nextGeneration))++;
if (UT_GLOBAL(nextGeneration) >=
UT_GLOBAL(traceGenerations)) {
UT_GLOBAL(nextGeneration) = 0;
}
}
}
UT_DBGOUT(1, ("<UT> Opening trace file \"%s\"\n", filename));
/*
* Try opening an existing file, and if that fails, create one
*/
if ((trcFile = j9file_open(filename, EsOpenWrite | EsOpenTruncate | EsOpenCreateNoTag, 0)) == -1) {
if ((trcFile = j9file_open(filename, EsOpenWrite | EsOpenCreate | EsOpenCreateNoTag, 0666)) == -1) {
/* Error opening tracefile: %s */
j9nls_printf(PORTLIB, J9NLS_WARNING | J9NLS_STDERR, J9NLS_TRC_FILE_OPEN_FAIL_STR, filename);
trcFile = -1;
}
}
/*
* If the open worked, write out the header
*/
if (trcFile != -1) {
if (j9file_write(trcFile, UT_GLOBAL(traceHeader),
UT_GLOBAL(traceHeader->header.length)) !=
(int)UT_GLOBAL(traceHeader->header.length)) {
/* Error writing header to tracefile: %s */
j9nls_printf(PORTLIB, J9NLS_WARNING | J9NLS_STDERR, J9NLS_TRC_HEADER_WRITE_FAIL_STR, filename);
j9file_close(trcFile);
trcFile = -1;
}
}
return trcFile;
}
/*******************************************************************************
* name - closeTraceFile
* description - Close a trace file
* parameters - UtThreadData, file handle, filename, filesize
* returns - Nothing
******************************************************************************/
static void
closeTraceFile(intptr_t trcFile, char *filename,
int64_t maxFileSize)
{
int rc;
PORT_ACCESS_FROM_PORT(UT_GLOBAL(portLibrary));
rc = j9file_set_length(trcFile, maxFileSize);
if (0 != rc) {
UT_DBGOUT(1, ("<UT> Error from j9file_set_length for tracefile: %s\n", filename));
}
j9file_close(trcFile);
}
/*******************************************************************************
* name - queueWrite
* description - Place a buffer on the write queue if safe to do so
* parameters - thr, buffer address, flag
* returns - a pointer to the buffer queued or NULL if not queued
******************************************************************************/
UtTraceBuffer *
queueWrite(UtTraceBuffer *trcBuf, int flags)
{
uint32_t newFlags;
uint32_t oldFlags;
PORT_ACCESS_FROM_PORT(UT_GLOBAL(portLibrary));
UT_DBGOUT(5, ("<UT> QueueWrite entered for buffer "UT_POINTER_SPEC", flags 0x%x, existing flags 0x%x\n", trcBuf, flags, trcBuf->flags));
/*
* First, set the write flag. Reset buffer active flag for external trace
*/
do {
oldFlags = trcBuf->flags;
newFlags = ~UT_TRC_BUFFER_ACTIVE & ((uint32_t)flags | oldFlags);
} while (!twCompareAndSwap32((unsigned int *)(&trcBuf->flags),
oldFlags,
newFlags));
/* Only put on queue if the buffer was active */
if ((oldFlags & UT_TRC_BUFFER_ACTIVE) && !(oldFlags & UT_TRC_BUFFER_NEW)) {
trcBuf->record.writePlatform = j9time_hires_clock();
trcBuf->record.writeSystem = ((uint64_t) j9time_current_time_millis());
trcBuf->record.writePlatform = (trcBuf->record.writePlatform >> 1) + (j9time_hires_clock() >> 1);
if (publishMessage(&UT_GLOBAL(outputQueue), &trcBuf->queueData) == TRUE) {
return trcBuf;
}
} else if (oldFlags & UT_TRC_BUFFER_PURGE) {
UT_DBGOUT(1, ("<UT> skipping queue write for buffer "UT_POINTER_SPEC" with purge set, flags 0x%x, belonging to UT thread "UT_POINTER_SPEC"\n", trcBuf, oldFlags, trcBuf->thr));
}
return NULL;
}
/*******************************************************************************
* name - openSnap
* description - Open a file in order to snap internal trace buffers
* parameters - UtThreadData, filename
* returns - handle to snap file
******************************************************************************/
intptr_t
openSnap(char *label)
{
#define FILENAMELEN 64
PORT_ACCESS_FROM_PORT(UT_GLOBAL(portLibrary));
static char fileName[FILENAMELEN];
UT_DBGOUT(1, ("<UT> Trace snap requested\n"));
if (initTraceHeader() != OMR_ERROR_NONE){
return OMR_ERROR_INTERNAL;
}
UT_GLOBAL(snapSequence)++;
if ( label == NULL ) {
uintptr_t pid = j9sysinfo_get_pid();
int64_t curTime = j9time_current_time_millis();
struct J9StringTokens* stringTokens = j9str_create_tokens(curTime);
j9str_set_token(PORTLIB, stringTokens, "pid", "%lld", pid);
j9str_set_token(PORTLIB, stringTokens, "sid", "%04.4d", UT_GLOBAL(snapSequence));
j9str_subst_tokens(fileName, FILENAMELEN, "Snap%sid.%Y%m%d%H%M%S.%pid.trc", stringTokens);
j9str_free_tokens(stringTokens);
#undef FILENAMELEN
label = fileName;
}
/*
* Open external trace file
*/
setTraceType(UT_NORMAL_BUFFER);
return openTraceFile(label);
}
/*******************************************************************************
* name - subscriptionHandler
* description - Wrapper thread for a buffer subscriber
* parameters - arg: a UtSubscription pointer with userdata->description set
* returns - non zero for unclean exit
******************************************************************************/
int
subscriptionHandler(void *arg)
{
UtSubscription *subscription = (UtSubscription*)arg;
UtThreadData thrData;
UtThreadData *thrSlot = &thrData;
UtThreadData **thr = &thrSlot;
char *description = subscription->description;
qMessage *trcBuf = NULL;
int32_t detachThread = FALSE;
omr_error_t rc = OMR_ERROR_NONE;
#if defined(J9VM_OPT_JAVA_OFFLOAD_SUPPORT)
J9JavaVM *vm = (J9JavaVM *)(UT_GLOBAL(vm)->_language_vm);
#endif
subscription->thr = thr;
subscription->dataLength = UT_GLOBAL(bufferSize);
if (subscription->threadAttach) {
if (OMR_ERROR_NONE != twThreadAttach(thr, description)) {
goto cleanup;
}
#if defined(J9VM_OPT_JAVA_OFFLOAD_SUPPORT)
if (NULL != vm->javaOffloadSwitchOnWithReasonFunc) {
J9VMThread *vmThread = (J9VMThread *)OMR_VM_THREAD_FROM_UT_THREAD(thr)->_language_vmthread;
(*vm->javaOffloadSwitchOnWithReasonFunc)(vmThread, J9_JNI_OFFLOAD_SWITCH_TRACE_SUBSCRIBER_THREAD);
}
#endif
}
/* Make sure this thread isn't traced */
incrementRecursionCounter(*thr);
UT_DBGOUT(1, ("<UT> Trace subscriber thread \"%s\" started\n", description));
if (OMR_ERROR_NONE != initTraceHeader()) {
goto cleanup;
}
/* handler main loop */
do {
utsSubscriberCallback subscriber;
if (subscription->threadAttach) {
if (((int32_t)omrthread_get_priority(OS_THREAD_FROM_UT_THREAD(thr))) != subscription->threadPriority) {
omrthread_set_priority(OS_THREAD_FROM_UT_THREAD(thr), subscription->threadPriority);
}
}
trcBuf = acquireNextMessage(subscription->queueSubscription);
subscriber = subscription->subscriber;
if (trcBuf == NULL) {
UT_DBGOUT(5, ("<UT> Subscription handler exiting from NULL message for subscription " UT_POINTER_SPEC "\n", subscription));
break;
}
if (UT_SUBSCRIPTION_KILLED == subscription->state) {
UT_DBGOUT(5, ("<UT> Subscription handler exiting due to deregistration of subscription " UT_POINTER_SPEC "\n", subscription));
releaseCurrentMessage(subscription->queueSubscription);
break;
}
if (subscription->description == NULL) {
UT_DBGOUT(5, ("<UT> Passing buffer " UT_POINTER_SPEC " to " UT_POINTER_SPEC "\n", trcBuf, subscription->subscriber));
} else {
UT_DBGOUT(5, ("<UT> Passing buffer " UT_POINTER_SPEC " to \"%s\"\n", trcBuf, subscription->description));
}
/* TODO: j9sig_protect and mprotect */
subscription->data = &((UtTraceBuffer*)trcBuf->data)->record;
rc = subscriber(subscription);
releaseCurrentMessage(subscription->queueSubscription);
/* checking the return code from the subscriber callback */
if (OMR_ERROR_NONE != rc) {
UT_DBGOUT(1, ("<UT> Removing trace subscription for \"%s\" due to subscriber error %i\n", description, rc));
break;
}
} while (UT_SUBSCRIPTION_KILLED != subscription->state);
cleanup:
UT_DBGOUT(1, ("<UT> Trace subscriber thread \"%s\" stopping\n", description));
UT_DBGOUT(5, ("<UT thr="UT_POINTER_SPEC"> Acquiring lock for handler cleanup\n", thr));
omrthread_monitor_enter(UT_GLOBAL(subscribersLock));
getTraceLock(thr);
UT_DBGOUT(5, ("<UT thr="UT_POINTER_SPEC"> Lock acquired for handler cleanup\n", thr));
if (subscription->alarm != NULL) {
UT_DBGOUT(3, ("<UT> Calling alarm function " UT_POINTER_SPEC " for \"%s\"\n", subscription->alarm, description));
subscription->alarm(subscription);
UT_DBGOUT(3, ("<UT> Returned from alarm function " UT_POINTER_SPEC "\n", subscription->alarm, description));
}
if (thrSlot != &thrData) {
detachThread = TRUE;
}
if (UT_SUBSCRIPTION_KILLED == subscription->state) {
subscription->state = UT_SUBSCRIPTION_DEAD;
/* The killer destroys the subscription */
} else {
destroyRecordSubscriber(thr, subscription);
}
UT_DBGOUT(5, ("<UT thr="UT_POINTER_SPEC"> Releasing lock for cleanup on handler exit\n", thr));
/* Need to exit the trace lock first as detaching a thread needs the vm thread list lock.
* This can be held by another thread (especially one doing GC) that is trying to write a tracepoint
* and waiting for the trace lock itself (for example to write to the global gc trace buffer).
* See CMVC 194605 for details.
*/
/* @alin-todo Why don't we decrement the recursion counter, which
* was incremented by getTraceLock() above?
* Not very important since the thread is about to exit anyway.
*/
omrthread_monitor_exit(UT_GLOBAL(traceLock));
omrthread_monitor_notify_all(UT_GLOBAL(subscribersLock));
omrthread_monitor_exit(UT_GLOBAL(subscribersLock));
if (detachThread) {
#if defined(J9VM_OPT_JAVA_OFFLOAD_SUPPORT)
if (NULL != vm->javaOffloadSwitchOffWithReasonFunc) {
J9VMThread *vmThread = (J9VMThread *)OMR_VM_THREAD_FROM_UT_THREAD(thr)->_language_vmthread;
(vm->javaOffloadSwitchOffWithReasonFunc)(vmThread, J9_JNI_OFFLOAD_SWITCH_TRACE_SUBSCRIBER_THREAD);
}
#endif
twThreadDetach(thr);
}
return 0;
}
/*******************************************************************************
* name - writeBuffer
* description - Trace Writer main function to write buffers to disk
* parameters - UtSubscription *
* returns - OMR_ERROR_NONE on success, otherwise error
******************************************************************************/
omr_error_t
writeBuffer(UtSubscription *subscription)
{
TraceWorkerData *state = subscription->userData;
UtThreadData **thr = NULL;
UtTraceBuffer *trcBuf;
intptr_t outputFile = -1;
int64_t *fileSize;
int64_t *maxFileSize;
int32_t *wrap;
int32_t bufferType;
char *filename;
int32_t rc;
PORT_ACCESS_FROM_PORT(UT_GLOBAL(portLibrary));
thr = subscription->thr;
/* using subscription->queueSubscription->current like this is ugly, but we're constrained to only pass the
* subscription, so the buffer type isn't accessible via in-band means.
*/
trcBuf = (UtTraceBuffer*)subscription->queueSubscription->current->data;
bufferType = trcBuf->bufferType;
switch (bufferType) {
case UT_NORMAL_BUFFER:
UT_DBGOUT(5, ("<UT thr=" UT_POINTER_SPEC "> processing TraceRecord " UT_POINTER_SPEC " of type UT_NORMAL_BUFFER\n", thr, trcBuf));
outputFile = state->trcFile;
fileSize = &state->trcSize;
maxFileSize = &state->maxTrc;
filename = UT_GLOBAL(traceFilename);
wrap = &UT_GLOBAL(traceWrap);
break;
case UT_EXCEPTION_BUFFER:
UT_DBGOUT(5, ("<UT thr=" UT_POINTER_SPEC "> processing TraceRecord " UT_POINTER_SPEC " of type UT_EXCEPTION_BUFFER\n", thr, trcBuf));
outputFile = state->exceptFile;
fileSize = &state->exceptSize;
maxFileSize = &state->maxExcept;
filename = UT_GLOBAL(exceptFilename);
wrap = &UT_GLOBAL(exceptTraceWrap);
break;
default:
/* not a buffer type we know about so skip it */
return OMR_ERROR_NONE;
break;
}
if (outputFile != -1) {
UT_DBGOUT(5, ("<UT thr=" UT_POINTER_SPEC "> writeBuffer writing buffer " UT_POINTER_SPEC " to %s\n", thr, trcBuf, filename));
/*
* Write the record
*/
*fileSize += subscription->dataLength;
rc = (int32_t)j9file_write(outputFile, subscription->data, (int32_t)subscription->dataLength);
if (rc != subscription->dataLength) {
/* Error writing %d bytes to tracefile: %s rc: %d */
j9nls_printf(PORTLIB, J9NLS_WARNING | J9NLS_STDERR, J9NLS_TRC_TRACE_WRITE_FAIL_STR, subscription->dataLength, filename, rc);
*fileSize = -1;
return OMR_ERROR_INTERNAL;
}
/*
* Check for file wrap
*/
if (*wrap != 0 && *fileSize >= *wrap) {
/* Trace options may have changed, re-initialize the trace file header data if necessary */
initTraceHeader();
if ((bufferType == UT_NORMAL_BUFFER) && (UT_GLOBAL(traceGenerations) > 1)) {
/* For multiple-generation file mode, open the next file */
j9file_close(outputFile);
setTraceType(UT_NORMAL_BUFFER);
state->trcFile = openTraceFile(NULL);
if (state->trcFile > 0) {
*fileSize = UT_GLOBAL(traceHeader->header.length);
*maxFileSize = *fileSize;
outputFile = state->trcFile;
} else {
/* Error opening next generation: %s */
j9nls_printf(PORTLIB, J9NLS_WARNING | J9NLS_STDERR, J9NLS_TRC_NEXT_GEN_FILE_OPEN_FAIL_STR, filename);
*fileSize = -1;
return OMR_ERROR_INTERNAL;
}
} else {
/* For single file wrap mode, seek back to start of file */
*maxFileSize = *fileSize;
*fileSize = j9file_seek(outputFile, 0, SEEK_SET);
if (*fileSize != 0 ) {
/* Error performing seek in trace file: %s */
j9nls_printf(PORTLIB, J9NLS_WARNING | J9NLS_STDERR, J9NLS_TRC_FILE_SEEK_FAIL_STR, filename);
*fileSize = -1;
return OMR_ERROR_INTERNAL;
}
*fileSize = j9file_write(outputFile, UT_GLOBAL(traceHeader), UT_GLOBAL(traceHeader->header.length));
if (*fileSize != UT_GLOBAL(traceHeader->header.length)) {
/* Error writing %d bytes to trace file: %s rc: %d */
j9nls_printf(PORTLIB, J9NLS_WARNING | J9NLS_STDERR, J9NLS_TRC_TRACE_WRITE_FAIL_STR, UT_GLOBAL(traceHeader->header.length), filename, rc);
*fileSize = -1;
return OMR_ERROR_INTERNAL;
}
}
}
if (*fileSize > *maxFileSize) {
*maxFileSize = *fileSize;
}
}
return OMR_ERROR_NONE;
}
/*******************************************************************************
* name - getTrcBuf
* description - Get and initialize a TraceBuffer
* parameters - thr, current buffer pointer or null, buffer type
* returns - TraceBuffer pointer or NULL
******************************************************************************/
static UtTraceBuffer *
getTrcBuf(UtThreadData **thr, UtTraceBuffer * oldBuf, int bufferType)
{
UtTraceBuffer *nextBuf = NULL;
UtTraceBuffer *trcBuf;
int32_t newBuffer = FALSE;
uint32_t typeFlags = UT_TRC_BUFFER_ACTIVE | UT_TRC_BUFFER_NEW;
uint64_t writePlatform, writeSystem;
PORT_ACCESS_FROM_PORT(UT_GLOBAL(portLibrary));
writePlatform = j9time_hires_clock();
writeSystem = ((uint64_t) j9time_current_time_millis());
writePlatform = (writePlatform >> 1) + (j9time_hires_clock() >> 1);
if (oldBuf != NULL) {
/*
* Update write timestamp in case of a dump being taken before
* the buffer is ever written to disk for any reason
*/
oldBuf->record.writeSystem = writeSystem;
oldBuf->record.writePlatform = writePlatform;
/* Null the thread reference as there's no guaranty it's valid after this point */
oldBuf->thr = NULL;
if (UT_GLOBAL(traceInCore)) {
/*
* In core trace mode so reuse existing buffer, wrapping to the top
*/
trcBuf = oldBuf;
goto out;
} else {
/*
* External trace mode
*/
nextBuf = oldBuf->next;
/* Is there a new buffer we can switch to? */
if (nextBuf && ((nextBuf->flags & UT_TRC_BUFFER_WRITE) == 0)) {
DBG_ASSERT(nextBuf->queueData.next == CLEANING_MSG_FLAG && nextBuf->queueData.referenceCount == 0 && nextBuf->queueData.subscriptions == 0 && nextBuf->queueData.pauseCount == 0);
/* YES - put the full buffer on the write thread */
if (queueWrite(oldBuf, UT_TRC_BUFFER_FULL) != NULL) {
notifySubscribers(&UT_GLOBAL(outputQueue));
}
/* Set up nextBuf */
/* it's okay to set the global buffers here and initialize later because the entire
* function call's under the trace lock so nothing can be written to it until we release.
*/
if (bufferType == UT_NORMAL_BUFFER) {
(*thr)->trcBuf = nextBuf;
} else if (bufferType == UT_EXCEPTION_BUFFER) {
UT_GLOBAL(exceptionTrcBuf) = nextBuf;
}
trcBuf = nextBuf;
/* make sure that if we've dropped buffers that we don't double account */
trcBuf->lostCount = 0;
goto out;
}
/* In nodynamic mode we allow up to 3 buffers per thread then we spill tracepoints */
if (nextBuf && !UT_GLOBAL(dynamicBuffers) && nextBuf->next != oldBuf) {
/*
* We're using "nodynamic" buffering, so we won't be simply
* mallocing another buffer. Reuse the current buffer and flag
* the fact that we have lost tracepoints.
*/
if (UT_GLOBAL(lostRecords) == 0) {
UT_DBGOUT(1, ("<UT> Trace buffer discarded. Count of discarded buffers will be printed at VM shutdown\n"));
}
UT_DBGOUT(4, ("<UT> discarding buffer because "UT_POINTER_SPEC" queued\n", nextBuf));
UT_ATOMIC_INC((volatile uint32_t*)&UT_GLOBAL(lostRecords));
oldBuf->lostCount += 1;
/* it's okay to set the global buffers here and initialize later because the entire
* function calls under the trace lock so nothing can be written to it until we release.
*/
if (bufferType == UT_NORMAL_BUFFER) {
(*thr)->trcBuf = oldBuf;
} else if (bufferType == UT_EXCEPTION_BUFFER) {
UT_GLOBAL(exceptionTrcBuf) = oldBuf;
}
trcBuf = oldBuf;
goto out;
}
if (queueWrite(oldBuf, UT_TRC_BUFFER_FULL) != NULL) {
notifySubscribers(&UT_GLOBAL(outputQueue));
}
}
}
/*
* Reuse buffer if there is one
*/
omrthread_monitor_enter(UT_GLOBAL(freeQueueLock));
trcBuf = UT_GLOBAL(freeQueue);
if (NULL != trcBuf) {
UT_GLOBAL(freeQueue) = trcBuf->next;
}
omrthread_monitor_exit(UT_GLOBAL(freeQueueLock));
if (trcBuf != NULL) {
DBG_ASSERT(trcBuf->queueData.next == NULL || trcBuf->queueData.next == CLEANING_MSG_FLAG);