-
Notifications
You must be signed in to change notification settings - Fork 748
/
Copy pathtrcmain.c
2271 lines (1956 loc) · 72.4 KB
/
trcmain.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 <limits.h>
#include <string.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdlib.h>
#include <sys/types.h>
#include <time.h>
#include <assert.h>
#include "ute_core.h"
#include "rastrace_internal.h"
#include "omrutil.h"
#include "j9trcnls.h"
#include "j9.h"
#define UT_TRACE_SLEEPTIME_DEFAULT_MILLIS 30000
/* Timeout value for trace shutdown. This is the maximum length of time we wait for trace subscribers
* to finish. Set as a long safety of 2 minutes, on the basis that a hang is likely by then, and we
* can issue a diagnostic message and exit before other hang detection in the stack kills the process.
*/
#define UT_TRACE_SHUTDOWN_TIMEOUT_MILLIS 120000
static const char* UT_NO_THREAD_NAME = "MISSING";
/* Structures for trace interfaces.
* The public interface is returned to the language layer, and may be
* modified. The private interface is not and can be considered safe.
*/
static UtInterface externalUtIntfS;
UtInterface internalUtIntfS;
/* Structures for trace interfaces. */
static UtServerInterface utServerIntfS;
static UtModuleInterface utModuleIntfS;
UtGlobalData *utGlobal = NULL;
extern J9JavaVM *globalVM;
omrthread_tls_key_t j9rasTLSKey;
omrthread_tls_key_t j9uteTLSKey;
static omr_error_t trcFlushTraceData(UtThreadData **thr, UtTraceBuffer **first, UtTraceBuffer **last, int32_t pause);
static omr_error_t trcGetTraceMetadata(void **data, int32_t *length);
static omr_error_t trcRegisterTracePointSubscriber(UtThreadData **thr, char *description, utsSubscriberCallback subscriber, utsSubscriberAlarmCallback alarm, void *userData, UtSubscription **subscriptionReference);
static omr_error_t trcDeregisterTracePointSubscriber(UtThreadData **thr, UtSubscription *subscriptionID);
static omr_error_t trcTraceSnap(UtThreadData **thr, char *label, char **response);
static omr_error_t trcTraceSnapWithPriority(UtThreadData **thr, char *label, int32_t snapPriority, char **response, int32_t sync);
static omr_error_t internalTraceSnapWithPriority(UtThreadData **thr, char *label, int32_t snapPriority, char **response, int32_t sync);
static omr_error_t trcAddComponent(UtModuleInfo *modInfo, const char **format) ;
static omr_error_t trcGetComponents(UtThreadData **thr, char ***list, int32_t *number);
static omr_error_t trcGetComponent(char *name, unsigned char **bitMap, int32_t *first, int32_t *last);
static omr_error_t trcTraceRegister(UtThreadData **thr, UtListenerWrapper func, void *userData);
static omr_error_t trcTraceDeregister(UtThreadData **thr, UtListenerWrapper func, void *userData);
static void trcDisableTrace(int32_t type);
static void trcEnableTrace(int32_t type);
static omr_error_t trcSetOptions(UtThreadData **thr, const char *opts[]);
static void omrTraceInit (void* env, UtModuleInfo *modInfo);
static void omrTraceTerm (void* env, UtModuleInfo *modInfo);
static void setStartTime(void);
/*******************************************************************************
* name - startTraceWorkerThread
* description - Start a worker thread to write data to disk
* parameters - UtThreadData
* returns - OMR_ERROR_INTERNAL if failure, OMR_ERROR_NONE if success
******************************************************************************/
omr_error_t
startTraceWorkerThread(UtThreadData **thr)
{
/* This function's present to keep the separation of function and interface
* between ut_main.c and ut_trace.c
*/
omr_error_t rc = OMR_ERROR_NONE;
if (!UT_GLOBAL(traceInCore)) {
rc = setupTraceWorkerThread(thr);
}
if (OMR_ERROR_NONE == rc) {
UT_GLOBAL(traceInitialized) = TRUE;
}
return rc;
}
/**
* Register an external trace listener
* @param[in] thr UtThreadData
* @param[in] func Listener function pointer
* @param[in] userData Data passed to func
* @return OMR error code
*/
static omr_error_t
trcTraceRegister(UtThreadData **thr, UtListenerWrapper func, void *userData)
{
UtTraceListener *this;
UtTraceListener *next;
PORT_ACCESS_FROM_PORT(UT_GLOBAL(portLibrary));
UT_DBGOUT(1, ("<UT> TraceRegister entered. Func: " UT_POINTER_SPEC "\n", func));
this = (UtTraceListener *)j9mem_allocate_memory(sizeof(UtTraceListener), OMRMEM_CATEGORY_TRACE);
if (this == NULL) {
UT_DBGOUT(1, ("<UT> Out of memory in trcTraceRegister\n"));
return OMR_ERROR_OUT_OF_NATIVE_MEMORY;
}
initHeader(&this->header, UT_TRACE_LISTENER_NAME, sizeof(UtTraceListener));
this->listener = func;
this->userData = userData;
this->next = NULL;
getTraceLock(thr);
if (UT_GLOBAL(traceListeners) == NULL) {
UT_GLOBAL(traceListeners) = this;
} else {
for (next = UT_GLOBAL(traceListeners); next != NULL;
next = next->next) {
if (next->next == NULL) {
next->next = this;
break;
}
}
}
freeTraceLock(thr);
return OMR_ERROR_NONE;
}
/**
* Deregister an external trace listener
* @param[in] thr UtThreadData
* @param[in] func Listener function pointer
* @param[in] userData Data passed to func
* @return OMR error code
*/
static omr_error_t
trcTraceDeregister(UtThreadData **thr, UtListenerWrapper func, void *userData)
{
UtTraceListener **prev;
UtTraceListener *next;
omr_error_t rc = OMR_ERROR_NONE;
PORT_ACCESS_FROM_PORT(UT_GLOBAL(portLibrary));
UT_DBGOUT(1, ("<UT> TraceDeregister entered. Func: " UT_POINTER_SPEC "\n", func));
getTraceLock(thr);
prev = &UT_GLOBAL(traceListeners);
for (next = UT_GLOBAL(traceListeners); next != NULL;
next = next->next) {
if ((next->listener == func) && (next->userData == userData)) {
*prev = next->next;
j9mem_free_memory(next);
break;
}
prev = &next->next;
}
freeTraceLock(thr);
rc = (next == NULL) ? OMR_ERROR_ILLEGAL_ARGUMENT : OMR_ERROR_NONE;
return rc;
}
static void
omrTraceInit(void *env, UtModuleInfo *modInfo)
{
moduleLoaded(UT_THREAD_FROM_ENV(env), modInfo);
}
/*******************************************************************************
* name - moduleLoaded
* description - Initialize tracing for a loaded module
* parameters - UtThreadData, UtModuleInfo pointer
* returns - int32_t
******************************************************************************/
omr_error_t
moduleLoaded(UtThreadData **thr, UtModuleInfo *modInfo)
{
UtComponentData *compData = NULL;
omr_error_t rc = OMR_ERROR_NONE;
PORT_ACCESS_FROM_PORT(UT_GLOBAL(portLibrary));
/*
* Ensure we have a thread pointer
*/
if (thr == NULL) {
thr = twThreadSelf();
}
if (*thr == NULL || modInfo == NULL) {
return OMR_ERROR_ILLEGAL_ARGUMENT;
}
UT_DBGOUT(1, ("<UT> ModuleLoaded: %s\n", modInfo->name));
if (modInfo->traceVersionInfo == NULL) {
/* this is a pre 142 module - not compatible with this trace engine fail silently to register this module */
UT_DBGOUT(1, ("<UT> ModuleLoaded refusing registration to %s because it's version is less than the supported UT version %d\n", modInfo->name, UT_VERSION));
return OMR_ERROR_NONE;
} /* else {
this field contains the version number and can be used to modify behaviour based on level of module loaded
} */
getTraceLock(thr);
if (modInfo->intf == NULL) {
modInfo->intf = internalUtIntfS.module;
rc = initializeComponentData(&compData, modInfo, modInfo->name);
if (OMR_ERROR_NONE == rc) {
rc = addComponentToList(compData, UT_GLOBAL(componentList));
}
if (OMR_ERROR_NONE == rc) {
rc = processComponentDefferedConfig(compData, UT_GLOBAL(componentList));
}
if (OMR_ERROR_NONE != rc) {
/* Module not configured for trace: %s */
j9nls_printf(PORTLIB, J9NLS_WARNING | J9NLS_STDERR, J9NLS_TRC_MODULE_NOT_LOADED, modInfo->name);
freeTraceLock(thr);
return OMR_ERROR_INTERNAL;
}
} else {
/* guard against stale pointers */
modInfo->intf = internalUtIntfS.module;
/* this module has already been registered. Just increment the reference count */
modInfo->referenceCount += 1;
}
freeTraceLock(thr);
UT_DBGOUT(1, ("<UT> ModuleLoaded: %s, interface: "
UT_POINTER_SPEC "\n", modInfo->name, modInfo->intf));
return OMR_ERROR_NONE;
}
static void
omrTraceTerm(void* env, UtModuleInfo *modInfo)
{
UtThreadData **thr = UT_THREAD_FROM_ENV(env);
if (thr == NULL) {
/* unloading a dead module - safe to ignore this */
return;
}
/* moduleUnLoading() handles the case when *thr is NULL */
moduleUnLoading(thr, modInfo);
}
/*******************************************************************************
* name - moduleUnLoading
* description - Terminate tracing for an module
* parameters - UtThreadData, UtModuleInfo pointer
* returns - void
******************************************************************************/
omr_error_t
moduleUnLoading(UtThreadData **thr, UtModuleInfo *modInfo)
{
int32_t i;
omr_error_t rc = OMR_ERROR_NONE;
if (utGlobal == NULL || UT_GLOBAL(traceFinalized)) {
return OMR_ERROR_INTERNAL;
}
/*
*/
if (thr == NULL) {
thr = twThreadSelf();
}
if (*thr == NULL) {
if( modInfo != NULL ) {
/* Unloading a module that was not initialized correctly */
/* This is a little dangerous because we reach for ->count and ->active
* here and all other uses of those fields is in the ut_runtimedata.c file.
*
* The trace code is generally carefully wrapped to check for *thr == NULL,
* so having trace enabled, but *thr == NULL is not a problem.
*
* During runtime shutdown - when we unload DLLs, the *thr check is not
* sufficient, we need to avoid trying to call any functions in the trace DLL.
*
* Forcefully clearing the active array to zero ensures that any module calling
* this function will not have trace enabled - even if the *thr == NULL, usually
* indicating some sort of problem with initialization of that field.
*/
for(i=0;i<modInfo->count;i++) {
modInfo->active[i] = 0; /* force disable of trace */
}
}
return OMR_ERROR_ILLEGAL_ARGUMENT;
}
if (modInfo == NULL) {
return OMR_ERROR_ILLEGAL_ARGUMENT;
}
UT_DBGOUT(1, ("<UT> ModuleUnloading: %s\n", modInfo->name));
if (modInfo->traceVersionInfo == NULL) {
/* this is a pre 142 module - not compatible with this trace engine fail silently to register this module */
UT_DBGOUT(1, ("<UT> ModuleLoaded refusing deregistration to %s because it's version is less than the supported UT version %d\n", modInfo->name, UT_VERSION));
return OMR_ERROR_NONE;
} /* else {
this field contains the version number and can be used to modify behaviour based on level of module loaded
} */
getTraceLock(thr);
if (modInfo->referenceCount > 0) {
modInfo->referenceCount -= 1;
} else {
rc = setTracePointsTo(modInfo->name, UT_GLOBAL(componentList), TRUE, 0, 0, 0, -1, NULL, FALSE, TRUE);
if (OMR_ERROR_NONE != rc) {
UT_DBGOUT(1, ("<UT> problem turning off trace in %s as it unloads\n", modInfo->name));
/* proceed to remove it from list in case it has gone and we try and manipulate it's control array */
}
rc = removeModuleFromList(modInfo, UT_GLOBAL(componentList));
}
freeTraceLock(thr);
return rc;
}
/*******************************************************************************
* name - internalTraceSnapWithPriority
* description - Take a snapshot of the current trace buffers
* parameters - UtThreadData, label, priority, response, sync
* returns - OMR_ERROR_NONE or OMR_ERROR_INTERNAL
******************************************************************************/
static omr_error_t
internalTraceSnapWithPriority(UtThreadData **thr, char *label, int32_t snapPriority, char **response, int32_t sync)
{
uint32_t oldFlags;
uint32_t newFlags;
omr_error_t result = OMR_ERROR_NONE;
char *sink = "";
/* These UtThreadData locals are only used if we're passed a null thr or *thr */
UtThreadData thrData;
UtThreadData *thrSlot = &thrData;
if (response == NULL) {
response = &sink;
}
if (thr == NULL || *thr == NULL) {
/* fake up a UtThreadData just for use in snapping */
thr = &thrSlot;
thrData.recursion = 1;
}
UT_DBGOUT(1, ("<UT thr="UT_POINTER_SPEC"> entered snap\n", thr));
/* if trace has finalized the trace write thread will have gone away and any
* attempt at a snap is futile. (there won't be anything to snap either).
*/
if (UT_GLOBAL(traceFinalized) == TRUE) {
UT_DBGOUT(1, ("<UT thr="UT_POINTER_SPEC"> not snapping because trace is terminated\n", thr));
*response = "{trace terminated - snap not available}";
return OMR_ERROR_INTERNAL;
}
/*
* If there are any buffers at all
*/
if (UT_GLOBAL(traceGlobal) != NULL) {
/*
* Set the snap flag to true,
*/
do {
oldFlags = UT_GLOBAL(traceSnap);
newFlags = oldFlags | TRUE;
} while (!twCompareAndSwap32(&UT_GLOBAL(traceSnap),
oldFlags,
newFlags));
/*
* If the snap flag wasn't already on....
*/
if (!oldFlags) {
UtTraceBuffer *start = NULL;
UtTraceBuffer *stop = NULL;
UT_DBGOUT(1, ("<UT thr="UT_POINTER_SPEC"> flushing trace data for snap\n", thr));
trcFlushTraceData(thr, &start, &stop, TRUE);
/*
* Only schedule snap if an active buffer was found
*/
if (start != NULL) {
/* inform subscribers that buffers were queued */
notifySubscribers(&UT_GLOBAL(outputQueue));
if (!UT_GLOBAL(externalTrace)) {
omr_error_t result = OMR_ERROR_NONE;
UtSubscription *subscription;
UT_GLOBAL(snapFile) = openSnap(label);
UT_DBGOUT(1, ("<UT thr="UT_POINTER_SPEC"> Starting Snap write thread, start: " UT_POINTER_SPEC ", stop: " UT_POINTER_SPEC "\n", thr, start, stop));
/* Spawn a snap writer with it's subscription as its userData*/
/* TODO: if a crash occurs during registration/deregistration the trace lock is already held and this
* can hang. We need to check to see if this thread is already the owner of the trace lock before trying
* to snap dump in this fashion.
*/
result = trcRegisterRecordSubscriber(thr, "Snap Dump Thread", writeSnapBuffer, cleanupSnapDumpThread, NULL, start, stop, &subscription, FALSE);
if (OMR_ERROR_NONE == result) {
subscription->threadPriority = snapPriority;
subscription->userData = sync ? (void *)1 : NULL;
} else {
/* Snap thread could not be started, need to clean up. */
PORT_ACCESS_FROM_PORT(UT_GLOBAL(portLibrary));
/* Mark snap as not in progress otherwise we may wait for it to terminate below */
do {
oldFlags = UT_GLOBAL(traceSnap);
newFlags = oldFlags & ~TRUE;
} while (!twCompareAndSwap32(&UT_GLOBAL(traceSnap), oldFlags, newFlags));
/* Close the file we opened */
j9file_close(UT_GLOBAL(snapFile));
}
/* Wrote snap to the provided filename */
*response = label;
} else {
do {
oldFlags = UT_GLOBAL(traceSnap);
newFlags = oldFlags & ~TRUE;
} while (!twCompareAndSwap32(&UT_GLOBAL(traceSnap), oldFlags, newFlags));
/* tell them it was flushed to the external trace file */
*response = UT_GLOBAL(traceFilename);
}
/* unblock the start buffer */
UT_DBGOUT(1, ("<UT thr="UT_POINTER_SPEC"> snap unpausing write queue at "UT_POINTER_SPEC"\n", thr, &start->queueData));
resumeDequeueAtMessage(&start->queueData);
if (sync) {
/* Wait for the snap dump to finish (e.g. for fatal events, when the JVM is terminating) */
while (!twCompareAndSwap32(&UT_GLOBAL(traceSnap), FALSE, FALSE)) {
UT_DBGOUT(1, ("<UT thr="UT_POINTER_SPEC"> waiting for snap dump thread to complete\n", thr));
omrthread_sleep(100);
}
}
} else {
do {
oldFlags = UT_GLOBAL(traceSnap);
newFlags = oldFlags & ~TRUE;
} while (!twCompareAndSwap32(&UT_GLOBAL(traceSnap), oldFlags, newFlags));
*response = "{nothing to snap}";
result = OMR_ERROR_NONE;
}
} else {
UT_DBGOUT(1, ("<UT> Snap requested when one is already in progress, therefore ignoring it (no data will be lost)\n"));
/*
* Already snapping to another filename
*/
*response = "{snap already in progress}";
result = OMR_ERROR_INTERNAL;
}
} else {
/*
* Nothing to snap => no filename
*/
*response = "{nothing to snap}";
result = OMR_ERROR_NONE;
}
return result;
}
/*******************************************************************************
* name - trcTraceSnap
* description - Take a snapshot of the current trace buffers
* parameters - UtThreadData, label
* returns - label used to snap buffers
******************************************************************************/
static omr_error_t
trcTraceSnap(UtThreadData **thr, char *label, char **response)
{
return internalTraceSnapWithPriority(thr, label, UT_TRACE_WRITE_PRIORITY, response, FALSE);
}
/*******************************************************************************
* name - trcTraceSnapWithPriority
* description - Take a snapshot of the current trace buffers
* parameters - UtThreadData, label, priority, response, sync
* returns - OMR_ERROR_NONE or OMR_ERROR_INTERNAL
******************************************************************************/
static omr_error_t
trcTraceSnapWithPriority(UtThreadData **thr, char *label, int32_t snapPriority, char **response, int32_t sync)
{
int32_t detachRequired = FALSE;
omr_error_t ret = OMR_ERROR_NONE;
UtThreadData *thrSlot = NULL;
/* Temporarily attach non-VM threads to UTE (ie. SigQuit thread) */
if (NULL == thr) {
thr = &thrSlot;
twThreadAttach(thr, "UTE snap thread");
detachRequired = TRUE;
}
ret = internalTraceSnapWithPriority(thr, label, snapPriority, response, sync);
if (detachRequired) {
twThreadDetach(thr);
}
return ret;
}
/*******************************************************************************
* name - threadStop
* description - Handle Thread termination
* parameters - UtThreadData
* returns - void
******************************************************************************/
omr_error_t
threadStop(UtThreadData **thr)
{
UtTraceBuffer *trcBuf;
UtThreadData *tempThr = *thr;
UtThreadData savedThr;
UtThreadData *stackThrP = &savedThr;
uint32_t oldCount;
uint32_t newCount;
J9rasTLS * tls;
PORT_ACCESS_FROM_PORT(UT_GLOBAL(portLibrary));
if (utGlobal == NULL) { /* very fatal! */
if (thr != NULL) {
*thr = NULL;
}
return OMR_ERROR_INTERNAL;
}
UT_DBGOUT(3, ("<UT> ThreadStop entered for thread anchor " UT_POINTER_SPEC "\n", thr));
/*
* Ensure a valid thread pointer has been passed
*/
if (*thr == NULL) {
UT_DBGOUT(1, ("<UT> Bad thread passed to ThreadStop\n" ));
return OMR_ERROR_INTERNAL;
}
/*
* Let any trace listeners know that this thread is terminating
*/
if ((UT_GLOBAL(traceDests) & UT_EXTERNAL) != 0) {
internalTrace(thr, NULL, UT_EXTERNAL, NULL);
}
/*
* Write out active buffers, where they'll be freed, or just free them
*/
omrthread_monitor_enter(UT_GLOBAL(threadLock));
if ((trcBuf = (*thr)->trcBuf) != NULL) {
if (!UT_GLOBAL(traceInCore)) {
/*
* Lost record mode for this thread ?
*/
if (trcBuf->lostCount != 0) {
uint64_t endTime;
incrementRecursionCounter(*thr);
endTime = ((uint64_t) j9time_current_time_millis()) + UT_PURGE_BUFFER_TIMEOUT;
while ((trcBuf->flags & UT_TRC_BUFFER_FULL) != 0 &&
(((uint64_t) j9time_current_time_millis()) < endTime)) {
omrthread_sleep(1);
}
decrementRecursionCounter(*thr);
}
/*
* Trace the thread purge unconditionally
* Write Trc_Purge, dg.262
*/
internalTrace(thr, NULL, (UT_TRC_PURGE_ID << 8) | UT_MINIMAL, NULL);
(*thr)->trcBuf = NULL;
incrementRecursionCounter(*thr);
/*
* Flag buffer to be purged and queue it
*/
UT_DBGOUT(3, ("<UT> Purging buffer " UT_POINTER_SPEC " for thread "
UT_POINTER_SPEC"\n", trcBuf, thr));
if (queueWrite(trcBuf, UT_TRC_BUFFER_PURGE) != NULL) {
notifySubscribers(&UT_GLOBAL(outputQueue));
}
} else {
uint32_t oldFlags = 0, newFlags = 0;
UT_DBGOUT(5, ("<UT> freeing buffer " UT_POINTER_SPEC " for thread "
UT_POINTER_SPEC"\n", trcBuf, thr));
do {
oldFlags = trcBuf->flags;
newFlags = UT_TRC_BUFFER_PURGE | oldFlags;
} while (!twCompareAndSwap32((unsigned int *)(&trcBuf->flags), oldFlags, newFlags));
freeBuffers(&trcBuf->queueData);
}
}
/*
* Revert to stack storage for UtThreadData
*/
savedThr = *tempThr;
savedThr.name = UT_NO_THREAD_NAME;
*thr = NULL;
thr = &stackThrP;
omrthread_monitor_exit(UT_GLOBAL(threadLock));
omrthread_tls_set(OS_THREAD_FROM_UT_THREAD(thr), j9uteTLSKey, NULL); /* Wipe _slot_ address from TLS */
/*
* Free up any threadlocal storage
*/
tls = (J9rasTLS *)omrthread_tls_get(OS_THREAD_FROM_UT_THREAD(thr), j9rasTLSKey);
if (tls != NULL) {
omrthread_tls_set(OS_THREAD_FROM_UT_THREAD(thr), j9rasTLSKey, NULL);
if (tls->appTrace != NULL) {
j9mem_free_memory(tls->appTrace);
}
j9mem_free_memory(tls);
}
/*
* Free the UtThreadData
*/
if (tempThr->name != NULL && tempThr->name != UT_NO_THREAD_NAME) {
char *tempName = (char *)tempThr->name;
j9mem_free_memory( tempName);
}
j9mem_free_memory( tempThr);
do {
oldCount = UT_GLOBAL(threadCount);
newCount = oldCount - 1;
} while (!twCompareAndSwap32(&UT_GLOBAL(threadCount), oldCount, newCount));
/*
* Last thread out frees the UtGlobalData and the list of available trace buffers
*/
if (newCount == 0 && UT_GLOBAL(traceFinalized)) {
UtTraceBuffer *next, *current;
UtGlobalData *global = utGlobal;
omrthread_monitor_enter(global->freeQueueLock);
current = UT_GLOBAL(freeQueue);
utGlobal = NULL;
/* Cannot use UT_DEBUG macro as utGlobal has been set to NULL */
if (global->traceDebug >= 2) {
j9tty_err_printf("<UT> ThreadStop entered for final thread " UT_POINTER_SPEC ", freeing buffers\n", thr);
}
while (current != NULL) {
UtTraceBuffer *gNext = NULL;
if (global->traceDebug >= 2) {
j9tty_err_printf("<UT> ThreadStop freeing buffer " UT_POINTER_SPEC "\n", current);
}
next = current->next;
/* remove current from traceGlobal if running with debug */
if (global->traceDebug >= 1) {
gNext = global->traceGlobal;
if (gNext == NULL) {
if (global->traceDebug >= 1) {
j9tty_err_printf("<UT> NULL global buffer list! " UT_POINTER_SPEC " not found in global list\n", current);
}
} else if (gNext == current) {
global->traceGlobal = gNext->globalNext;
} else {
for (;gNext != NULL && gNext->globalNext != current; gNext = gNext->globalNext);
if (gNext != NULL && gNext->globalNext == current) {
gNext->globalNext = current->globalNext;
} else {
if (global->traceDebug >= 1) {
j9tty_err_printf("<UT> trace buffer " UT_POINTER_SPEC " not found in global list\n", current);
}
}
}
}
j9mem_free_memory( current);
current = next;
}
global->freeQueue = NULL;
omrthread_monitor_exit(global->freeQueueLock);
/* output anything left on global if running with debug */
if (global->traceDebug >= 1) {
for (current = global->traceGlobal; current != NULL; current = current->globalNext) {
j9tty_err_printf("<UT> trace buffer " UT_POINTER_SPEC " not freed!\n", current);
j9tty_err_printf("<UT> owner: " UT_POINTER_SPEC " - %s\n", current->thr, current->record.threadName);
}
}
if (global->exceptionTrcBuf != NULL) {
j9mem_free_memory( global->exceptionTrcBuf);
}
omrthread_monitor_destroy(global->threadLock);
omrthread_monitor_destroy(global->freeQueueLock);
omrthread_monitor_destroy(global->traceLock);
omrthread_monitor_destroy(global->triggerOnTpidsWriteMutex);
omrthread_monitor_destroy(global->triggerOnGroupsWriteMutex);
j9mem_free_memory(global);
}
return OMR_ERROR_NONE;
}
omr_error_t
utTerminateTrace(UtThreadData **thr, char** daemonThreadNames)
{
UtTraceBuffer *trcBuf;
uint64_t endTime;
int32_t notPurged = TRUE;
int32_t bufferQueued = FALSE;
omr_error_t result = OMR_ERROR_NONE;
int i = 0;
PORT_ACCESS_FROM_PORT(UT_GLOBAL(portLibrary));
if (utGlobal == NULL) { /* very fatal! */
return OMR_ERROR_INTERNAL;
}
UT_GLOBAL(traceFinalized) = TRUE;
/*
* Trace initialized ?
*/
if (!UT_GLOBAL(traceInitialized)) {
result = OMR_ERROR_INTERNAL;
goto out;
}
/*
* Ensure a valid thread pointer has been passed
*/
if (*thr == NULL) {
result = OMR_ERROR_INTERNAL;
goto out;
}
UT_DBGOUT(1, ("<UT thr="UT_POINTER_SPEC"> Trace terminate entered\n", thr));
if (!UT_GLOBAL(traceInCore)) {
/* TODO: replace this block with a call to trcFlushTraceData */
/*
* Purge any buffers for this thread and give the other threads
* a chance to purge theirs. Ignore never ending threads.
*/
incrementRecursionCounter(*thr);
endTime = ((uint64_t) j9time_current_time_millis()) + UT_PURGE_BUFFER_TIMEOUT;
while (notPurged && ((uint64_t) j9time_current_time_millis()) < endTime) {
notPurged = FALSE;
for (trcBuf = UT_GLOBAL(traceGlobal); trcBuf != NULL; trcBuf = trcBuf->globalNext) {
if ((trcBuf->flags & UT_TRC_BUFFER_ACTIVE) != 0 ) {
notPurged = TRUE;
if( daemonThreadNames != NULL ) {
i = 0;
/* Check if this is a thread we expect to keep running. */
while( NULL != daemonThreadNames[i] ) {
if( 0 == strcmp(trcBuf->record.threadName, daemonThreadNames[i]) ) {
notPurged = FALSE;
break;
}
i++;
}
}
if( notPurged ) {
break;
}
}
}
omrthread_sleep(1);
}
/*
* Flush active buffers
*/
for (trcBuf = UT_GLOBAL(traceGlobal); trcBuf != NULL; trcBuf = trcBuf->globalNext) {
if ((trcBuf->flags & UT_TRC_BUFFER_ACTIVE) != 0) {
UT_DBGOUT(2, ("<UT> Flushing buffer " UT_POINTER_SPEC
" for thr " UT_POINTER_SPEC "\n",
(uintptr_t) trcBuf, (uintptr_t)trcBuf->record.threadId));
if (queueWrite(trcBuf, UT_TRC_BUFFER_FLUSH) != NULL) {
bufferQueued = TRUE;
}
}
}
/* let the subscribers have a chance to process any final buffers before we clean up */
if (bufferQueued == TRUE) {
notifySubscribers(&UT_GLOBAL(outputQueue));
}
}
/* stops anything new being queued and prompts subscribers to exit */
destroyQueue(&UT_GLOBAL(outputQueue));
/* Subscribers are deleting themselves in parallel with this. Don't access the contents of the subscribers list. */
omrthread_monitor_enter(UT_GLOBAL(subscribersLock));
while (NULL != UT_GLOBAL(subscribers)) {
IDATA rc;
UT_DBGOUT(1, ("<UT thr="UT_POINTER_SPEC"> Trace terminated, waiting for subscribers to complete\n", thr));
rc = omrthread_monitor_wait_timed(UT_GLOBAL(subscribersLock), UT_TRACE_SHUTDOWN_TIMEOUT_MILLIS, 0);
if (J9THREAD_TIMED_OUT == rc) {
UT_DBGOUT(1, ("<UT thr="UT_POINTER_SPEC"> Trace termination timed out waiting for subscribers to complete\n", thr));
j9nls_printf(PORTLIB, J9NLS_WARNING | J9NLS_STDERR, J9NLS_TRC_SHUTDOWN_TIMEOUT);
break; /* bail out now */
}
}
omrthread_monitor_exit(UT_GLOBAL(subscribersLock));
omrthread_monitor_destroy(UT_GLOBAL(subscribersLock));
UT_GLOBAL(subscribersLock) = NULL;
UT_DBGOUT(1, ("<UT thr="UT_POINTER_SPEC"> Trace terminated\n", thr));
result = OMR_ERROR_NONE;
out:
if (UT_GLOBAL(traceCount)) {
listCounters();
}
if (UT_GLOBAL(lostRecords) != 0) {
UT_DBGOUT(1, ("<UT> Discarded %d trace buffers\n", UT_GLOBAL(lostRecords)));
}
return result;
}
/*******************************************************************************
* name - cleanUpTrace
* description - Free up all trace structures
* parameters - UtThreadData struct
* returns - void
******************************************************************************/
void
freeTrace(UtThreadData **thr)
{
UtTraceCfg *config, *tmpconfig;
int i;
PORT_ACCESS_FROM_PORT(UT_GLOBAL(portLibrary));
UT_DBGOUT(1, ("<UT thr="UT_POINTER_SPEC"> cleanUpTrace Entered\n", thr));
if (UT_GLOBAL(traceFinalized) != TRUE) {
/* shut everything down before freeing everything */
UT_DBGOUT(1, ("<UT thr="UT_POINTER_SPEC"> Error: cleanUpTrace called before trace has been finalized\n", thr));
}
/* so that we get cleaned up even in the case where trace hasn't initialized yet */
destroyQueue(&UT_GLOBAL(outputQueue));
config = UT_GLOBAL(config);
while ( config != NULL) {
tmpconfig = config;
config = config->next;
j9mem_free_memory( tmpconfig);
}
if (UT_GLOBAL(ignore) != NULL) {
for (i = 0; UT_GLOBAL(ignore[i]) != NULL; i++) {
j9mem_free_memory( UT_GLOBAL(ignore[i]));
}
j9mem_free_memory( UT_GLOBAL(ignore));
UT_GLOBAL(ignore) = NULL;
}
/* free all trace component list storage */
freeComponentList(UT_GLOBAL(componentList));
freeComponentList(UT_GLOBAL(unloadedComponentList));
if (UT_GLOBAL(traceFormatSpec) != NULL) {
j9mem_free_memory( UT_GLOBAL(traceFormatSpec));
UT_GLOBAL(traceFormatSpec) = NULL;
}
if (UT_GLOBAL(properties) != NULL) {
j9mem_free_memory( UT_GLOBAL(properties));
UT_GLOBAL(properties) = NULL;
}
if (UT_GLOBAL(serviceInfo) != NULL) {
j9mem_free_memory( UT_GLOBAL(serviceInfo));
UT_GLOBAL(serviceInfo) = NULL;
}
if (UT_GLOBAL(traceHeader) != NULL) {
j9mem_free_memory( UT_GLOBAL(traceHeader));
UT_GLOBAL(traceHeader) = NULL;
}
if (UT_GLOBAL(traceFilename) != NULL) {
j9mem_free_memory( UT_GLOBAL(traceFilename));
UT_GLOBAL(traceFilename) = NULL;
}
if (UT_GLOBAL(exceptFilename) != NULL) {
j9mem_free_memory( UT_GLOBAL(exceptFilename));
UT_GLOBAL(exceptFilename) = NULL;
}
freeTriggerOptions(UT_GLOBAL(portLibrary));
UT_DBGOUT(1, ("<UT> cleanUpTrace complete\n"));
/* NOTE: utGlobal will be freed by the last thread... */
return;
}
omr_error_t
threadStart(UtThreadData **thr, const void *threadId, const char *threadName, const void *threadSynonym1, const void *threadSynonym2)
{
omr_error_t rc = OMR_ERROR_NONE;
UtThreadData *newThr = NULL;
UtThreadData tempThr;
uint32_t oldCount;
uint32_t newCount;
PORT_ACCESS_FROM_PORT(UT_GLOBAL(portLibrary));
/* Set up a temporary UtThreadData structure.
* TODO: Investigate whether we really need this, given that we can't trigger tracepoints at this stage. See the
* additional comments below. The 2.9 OMR implementation avoids doing it (and allocates UtThreadData from a pool).
*/
memset(&tempThr, 0, sizeof(UtThreadData));
initHeader(&tempThr.header, UT_THREAD_DATA_NAME, sizeof(UtThreadData));
tempThr.id = threadId;
tempThr.synonym1 = threadSynonym1;
tempThr.synonym2 = threadSynonym2;
tempThr.suspendResume = UT_GLOBAL(initialSuspendResume);
/* Trace points must not be triggered for this thread before we have switched to the final (portlib allocated)
* UtThreadData. This is because trcFlushTraceData() will update the owning UtThreadData when it flushes trace
* buffers (as it moves them to the freeQueue). The first tracepoint acquires and attaches a buffer. See PR 79176.
*/
tempThr.recursion = 1;
if (NULL == threadName) {
tempThr.name = UT_NO_THREAD_NAME;
} else {
tempThr.name = threadName;
}
do {
oldCount = UT_GLOBAL(threadCount);
newCount = oldCount + 1;
} while (!twCompareAndSwap32(&UT_GLOBAL(threadCount), oldCount, newCount));
UT_DBGOUT(2, ("<UT> Thread started , thread anchor " UT_POINTER_SPEC "\n", thr));
UT_DBGOUT(2, ("<UT> thread Id " UT_POINTER_SPEC ", thread name \"%s\", syn1 " UT_POINTER_SPEC ", syn2 "
UT_POINTER_SPEC " \n", threadId, threadName, threadSynonym1, threadSynonym2));
/*
* Setup the current UtThread with the temporary.
*/
*thr = &tempThr;
/*
* Now we can allocate memory using the port library.
*/
newThr = j9mem_allocate_memory(sizeof(UtThreadData), OMRMEM_CATEGORY_TRACE);
if (NULL == newThr) {
UT_DBGOUT(1, ("<UT> Unable to obtain storage for thread control block \n"));
*thr = NULL;