package io.quarkus.runtime.graal;

import java.io.PrintStream;
import java.time.Instant;
import java.util.Map;

import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;

import com.oracle.svm.core.annotate.Alias;
import com.oracle.svm.core.annotate.TargetClass;
import com.oracle.svm.core.posix.headers.Pthread;

/**
 * A signal handler that prints diagnostic thread info to standard output.
 */
public final class DiagnosticPrinter {
    @TargetClass(className = "com.oracle.svm.core.posix.thread.PosixJavaThreads")
    @Platforms({ Platform.LINUX.class, Platform.DARWIN.class })
    static final class Target_PosixJavaThreads {
        @Alias
        static native Pthread.pthread_t getPthreadIdentifier(Thread thread);

        @Alias
        static native boolean hasThreadIdentifier(Thread thread);
    }

    public static void printDiagnostics(PrintStream w) {
        Map<Thread, StackTraceElement[]> allStackTraces = Thread.getAllStackTraces();
        w.println(Instant.now().toString());
        w.println("Thread dump follows:");
        for (Map.Entry<Thread, StackTraceElement[]> entry : allStackTraces.entrySet()) {
            w.println();
            final Thread thread = entry.getKey();
            final StackTraceElement[] stackTrace = entry.getValue();
            w.print('"');
            w.print(thread.getName());
            w.print("\" #");
            w.print(thread.getId());
            w.print(" ");
            if (thread.isDaemon())
                w.print("daemon ");
            w.print("prio=");
            w.print(thread.getPriority());
            w.print(" tid=");
            if ((Platform.includedIn(Platform.LINUX.class) || Platform.includedIn(Platform.DARWIN.class))
                    && Target_PosixJavaThreads.hasThreadIdentifier(thread)) {
                final long nativeId = Target_PosixJavaThreads.getPthreadIdentifier(thread).rawValue();
                w.print("0x");
                w.println(Long.toHexString(nativeId));
            } else {
                w.println("(unknown)");
            }
            w.print("   java.lang.thread.State: ");
            w.println(thread.getState());
            for (StackTraceElement element : stackTrace) {
                w.print("\tat ");
                w.print(element.getClassName());
                w.print('.');
                w.print(element.getMethodName());
                w.print('(');
                // todo: "moduleName@version/"
                final String fileName = element.getFileName();
                w.print(fileName == null ? "unknown source" : fileName);
                final int lineNumber = element.getLineNumber();
                if (lineNumber > 0) {
                    w.print(':');
                    w.print(lineNumber);
                }
                w.println(')');
            }
        }
    }
}

@@ -109,732 +109,129 @@
 p;%0A%0A
-import org.graalvm.nativeimage.Platform;%0Aimport org.graalvm.nativeimage.Platforms;%0A%0Aimport com.oracle.svm.core.annotate.Alias;%0Aimport com.oracle.svm.core.annotate.TargetClass;%0Aimport com.oracle.svm.core.posix.headers.Pthread;%0A%0A/**%0A * A signal handler that prints diagnostic thread info to standard output.%0A */%0Apublic final class DiagnosticPrinter %7B%0A    @TargetClass(className = %22com.oracle.svm.core.posix.thread.PosixJavaThreads%22)%0A    @Platforms(%7B Platform.LINUX.class, Platform.DARWIN.class %7D)%0A    static final class Target_PosixJavaThreads %7B%0A        @Alias%0A        static native Pthread.pthread_t getPthreadIdentifier(Thread thread);%0A%0A        @Alias%0A        static native boolean hasThreadIdentifier(Thread thread);%0A    %7D%0A
+/**%0A * A signal handler that prints diagnostic thread info to standard output.%0A */%0Apublic final class DiagnosticPrinter %7B
 %0A   
@@ -996,486 +996,8 @@
 ));%0A
-            w.print(%22 tid=%22);%0A            if ((Platform.includedIn(Platform.LINUX.class) %7C%7C Platform.includedIn(Platform.DARWIN.class))%0A                    && Target_PosixJavaThreads.hasThreadIdentifier(thread)) %7B%0A                final long nativeId = Target_PosixJavaThreads.getPthreadIdentifier(thread).rawValue();%0A                w.print(%220x%22);%0A                w.println(Long.toHexString(nativeId));%0A            %7D else %7B%0A                w.println(%22(unknown)%22);%0A            %7D%0A
     

Remove printing of thread id from DiagnosticPrinter