/*
 * Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 *
 */

package sun.jvm.hotspot.tools;

import java.io.*;
import java.util.*;
import sun.jvm.hotspot.*;
import sun.jvm.hotspot.code.*;
import sun.jvm.hotspot.interpreter.*;
import sun.jvm.hotspot.debugger.*;
import sun.jvm.hotspot.debugger.cdbg.*;
import sun.jvm.hotspot.debugger.remote.*;
import sun.jvm.hotspot.oops.*;
import sun.jvm.hotspot.runtime.*;
import sun.jvm.hotspot.utilities.PlatformInfo;

public class PStack extends Tool {
    // in non-verbose mode, Method*s are not printed in java frames
   public PStack(boolean v, boolean concurrentLocks, HotSpotAgent agent) {
      super(agent);
      this.verbose = v;
      this.concurrentLocks = concurrentLocks;
   }

   public PStack(boolean v, boolean concurrentLocks) {
      this(v, concurrentLocks, null);
   }

   public PStack() {
      this(true, true, null);
   }

   public PStack(JVMDebugger d) {
      super(d);
   }

   public void run() {
      run(System.out);
   }

   public void run(PrintStream out) {
      Debugger dbg = getAgent().getDebugger();
      run(out, dbg);
   }

   public void run(PrintStream out, Debugger dbg) {
      CDebugger cdbg = dbg.getCDebugger();
      if (cdbg != null) {
         ConcurrentLocksPrinter concLocksPrinter = null;
         // compute and cache java Vframes.
         if (concurrentLocks) {
            concLocksPrinter = new ConcurrentLocksPrinter(out);
         }
         // print Java level deadlocks
         try {
            DeadlockDetector.print(out);
         } catch (Exception exp) {
            out.println("can't print deadlock information: " + exp);
         }

         try {
             VMLocksPrinter vmLocksPrinter = new VMLocksPrinter(out);
             vmLocksPrinter.printVMLocks();
         } catch (Exception e) {
             out.println("can't print VM locks information: " + e);
         }

         List<ThreadProxy> l = cdbg.getThreadList();
         if (l.isEmpty() && PlatformInfo.getOS().equals("darwin")) {
           // If the list is empty, we assume we attached to a process, and on OSX we can only
           // get the native thread list for core files.
           out.println("Not available for Mac OS X processes");
           return;
        }
         final boolean cdbgCanDemangle = cdbg.canDemangle();
         Map<ThreadProxy, JavaThread> proxyToThread = createProxyToThread();;
         String fillerForAddress = " ".repeat(2 + 2 * (int) VM.getVM().getAddressSize()) + "\t";
         for (Iterator<ThreadProxy> itr = l.iterator() ; itr.hasNext();) {
            ThreadProxy th = itr.next();
            try {
               CFrame f = cdbg.topFrameForThread(th);
               out.print("----------------- ");
               out.print(th);
               out.println(" -----------------");
               JavaThread jthread = proxyToThread.get(th);
               if (jthread != null) {
                  jthread.printThreadInfoOn(out);
               }
               while (f != null) {
                  Address senderSP = null;
                  Address senderFP = null;
                  Address senderPC = null;
                  ClosestSymbol sym = f.closestSymbolToPC();
                  Address pc = f.pc();
                  out.print(pc + "\t");
                  if (sym != null) {
                     String name = sym.getName();
                     if (cdbgCanDemangle) {
                        name = cdbg.demangle(name);
                     }
                     out.print(name);
                     long diff = sym.getOffset();
                     if (diff != 0L) {
                        out.print(" + 0x" + Long.toHexString(diff));
                     }
                     out.println();
                  } else {
                      // look for one or more java frames
                      JavaNameInfo nameInfo = null;
                      // check interpreter frame
                      Interpreter interp = VM.getVM().getInterpreter();
                      if (interp.contains(pc)) {
                         nameInfo = getJavaNames(jthread, f);
                         // print codelet name if we can't determine method
                         if (nameInfo == null || nameInfo.names() == null || nameInfo.names().length == 0) {
                            out.print("<interpreter> ");
                            InterpreterCodelet ic = interp.getCodeletContaining(pc);
                            if (ic != null) {
                               String desc = ic.getDescription();
                               if (desc != null) out.print(desc);
                            }
                            out.println();
                         }
                      } else {
                         // look for known code blobs
                         CodeCache c = VM.getVM().getCodeCache();
                         if (c.contains(pc)) {
                            CodeBlob cb = c.findBlobUnsafe(pc);
                            if (cb.isNMethod()) {
                               if (cb.isNativeMethod()) {
                                  out.print(((NMethod)cb).getMethod().externalNameAndSignature());
                                  long diff = pc.minus(cb.codeBegin());
                                  if (diff != 0L) {
                                    out.print(" + 0x" + Long.toHexString(diff));
                                  }
                                  out.println(" (Native method)");
                               } else {
                                  nameInfo = getJavaNames(jthread, f);
                                  // just print compiled code, if can't determine method
                                  if (nameInfo == null || nameInfo.names() == null || nameInfo.names().length == 0) {
                                    out.println("<Unknown compiled code>");
                                  }
                               }
                            } else {
                               out.println("<" + cb.getName() + ">");
                               if (cb.getFrameSize() > 0) {
                                  Frame senderFrame = f.toFrame().sender(jthread.newRegisterMap(true));
                                  senderSP = senderFrame.getSP();
                                  senderFP = senderFrame.getFP();
                                  senderPC = senderFrame.getPC();
                               }
                            }
                         } else {
                            printUnknown(out);
                         }
                      }
                      // print java frames, if any
                      if (nameInfo != null) {
                         if (nameInfo.names() != null && nameInfo.names().length != 0) {
                             // print java frame(s)
                             for (int i = 0; i < nameInfo.names().length; i++) {
                                 if (i > 0) {
                                     out.print(fillerForAddress);
                                 }
                                 out.println(nameInfo.names()[i]);
                             }
                         }
                         senderSP = nameInfo.senderSP();
                         senderFP = nameInfo.senderFP();
                         senderPC = nameInfo.senderPC();
                      }
                  }
                  f = f.sender(th, senderSP, senderFP, senderPC);
               }
            } catch (Exception exp) {
               exp.printStackTrace();
               // continue, may be we can do a better job for other threads
            }
            if (concurrentLocks) {
               JavaThread jthread = proxyToThread.get(th);
               if (jthread != null) {
                   concLocksPrinter.print(jthread);
               }
            }
         } // for threads
      } else {
          if (getDebugeeType() == DEBUGEE_REMOTE) {
              out.print(((RemoteDebuggerClient)dbg).execCommandOnServer("pstack", Map.of("concurrentLocks", concurrentLocks)));
          } else {
              out.println("not yet implemented (debugger does not support CDebugger)!");
          }
      }
   }

   public static void main(String[] args) throws Exception {
      PStack t = new PStack();
      t.execute(args);
   }

   // -- Internals only below this point
   private PrintStream out;
   private boolean verbose;
   private boolean concurrentLocks;

   private Map<ThreadProxy, JavaThread> createProxyToThread() {
      Map<ThreadProxy, JavaThread> proxyToThread = new HashMap<>();
      Threads threads = VM.getVM().getThreads();
      for (int i = 0; i < threads.getNumberOfThreads(); i++) {
         JavaThread jthread = threads.getJavaThreadAt(i);
         proxyToThread.put(jthread.getThreadProxy(), jthread);
      }
      return proxyToThread;
   }

   private void printUnknown(PrintStream out) {
      out.println("\t????????");
   }

   private static record JavaNameInfo(String[] names, Address senderSP, Address senderFP, Address senderPC) {};

   private JavaNameInfo getJavaNames(JavaThread jthread, CFrame f) {
      List<String> names = new ArrayList<>(10);
      Address senderSP = null;
      Address senderFP = null;
      Address senderPC = null;
      VFrame vf = VFrame.newVFrame(f.toFrame(), jthread.newRegisterMap(true), jthread, true, true);
      while (vf != null && vf.isJavaFrame()) {
         StringBuilder sb = new StringBuilder();
         Method method = ((JavaVFrame)vf).getMethod();
         // a special char to identify java frames in output
         sb.append("* ");
         sb.append(method.externalNameAndSignature());
         sb.append(" bci:").append(((JavaVFrame)vf).getBCI());
         int lineNumber = method.getLineNumberFromBCI(((JavaVFrame)vf).getBCI());
         if (lineNumber != -1) {
            sb.append(" line:").append(lineNumber);
         }

         if (verbose) {
            sb.append(" Method*:").append(method.getAddress());
         }

         if (vf.isCompiledFrame()) {
            sb.append(" (Compiled frame");
            if (vf.isDeoptimized()) {
               sb.append(" [deoptimized]");
            }
         } else if (vf.isInterpretedFrame()) {
            sb.append(" (Interpreted frame");
         }
         if (vf.mayBeImpreciseDbg()) {
            sb.append("; information may be imprecise");
         }
         sb.append(")");
         names.add(sb.toString());

         // Keep registers in sender Frame
         Frame senderFrame = vf.getFrame()
                               .sender((RegisterMap)vf.getRegisterMap().clone());
         senderSP = senderFrame.getSP();
         senderFP = senderFrame.getFP();
         senderPC = senderFrame.getPC();

         // Get sender VFrame for next stack walking
         vf = vf.sender(true);
      }

      return new JavaNameInfo(names.toArray(new String[0]), senderSP, senderFP, senderPC);
   }

   public void setVerbose(boolean verbose) {
       this.verbose = verbose;
   }

   public void setConcurrentLocks(boolean concurrentLocks) {
       this.concurrentLocks = concurrentLocks;
   }
}
