99import java .util .Map ;
1010
1111class ThreadState {
12- private Configuration config ;
13- private Thread thread ;
14- private StackTraceElement [] stackTraceElements ;
12+
13+ private final Configuration config ;
14+ private final Thread thread ;
15+ private final StackTraceElement [] stackTraceElements ;
16+ private Boolean errorReportingThread ;
1517
1618 ThreadState (Configuration config , Thread thread , StackTraceElement [] stackTraceElements ) {
1719 this .config = config ;
1820 this .thread = thread ;
1921 this .stackTraceElements = stackTraceElements ;
2022 }
2123
22- static List <ThreadState > getLiveThreads (Configuration config ) {
24+ static List <ThreadState > getLiveThreads (Configuration config ,
25+ Thread currentThread ,
26+ Map <Thread , StackTraceElement []> liveThreads ,
27+ Throwable exc ) {
2328 // Get current thread id (the crashing thread) and stacktraces for all live threads
24- long crashingThreadId = Thread .currentThread ().getId ();
25- Map <Thread , StackTraceElement []> liveThreads = Thread .getAllStackTraces ();
29+ long crashingThreadId = currentThread .getId ();
30+
31+ // if thread is not present for any reason, add the current stacktrace to the map
32+ // so that the errorReportingThread will always be reported
33+ if (!liveThreads .containsKey (currentThread )) {
34+ liveThreads .put (currentThread , currentThread .getStackTrace ());
35+ }
36+ if (exc != null ) { // unhandled errors use the exception trace
37+ liveThreads .put (currentThread , exc .getStackTrace ());
38+ }
2639
2740 // Sort threads by thread-id
2841 Object [] keys = liveThreads .keySet ().toArray ();
@@ -33,19 +46,16 @@ public int compare(Object first, Object second) {
3346 });
3447
3548 List <ThreadState > threads = new ArrayList <ThreadState >();
36- for (int i = 0 ; i < keys .length ; i ++) {
37- Thread thread = (Thread ) keys [i ];
38-
39- // Don't show the current stacktrace here. It'll point at this method
40- // rather than at the point they crashed.
41- if (thread .getId () == crashingThreadId ) {
42- continue ;
43- }
4449
50+ for (Object key : keys ) {
51+ Thread thread = (Thread ) key ;
4552 ThreadState threadState = new ThreadState (config , thread , liveThreads .get (thread ));
4653 threads .add (threadState );
47- }
4854
55+ if (threadState .getId () == crashingThreadId ) {
56+ threadState .setErrorReportingThread (true );
57+ }
58+ }
4959 return threads ;
5060 }
5161
@@ -63,4 +73,13 @@ public String getName() {
6373 public List <Stackframe > getStacktrace () {
6474 return Stackframe .getStacktrace (config , stackTraceElements );
6575 }
76+
77+ @ JsonProperty ("errorReportingThread" )
78+ public Boolean isErrorReportingThread () {
79+ return errorReportingThread ;
80+ }
81+
82+ public void setErrorReportingThread (Boolean errorReportingThread ) {
83+ this .errorReportingThread = errorReportingThread ;
84+ }
6685}
0 commit comments