Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build/shared/lib/languages/PDE.properties
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ menu.debug.continue = Continue
#menu.debug.variable_inspector = Variable Inspector
menu.debug.show_variables = Show Variables
menu.debug.hide_variables = Hide Variables
menu.debug.gather_diagnostics = Gather Diagnostics
#menu.debug.show_sketch_outline = Show Sketch Outline
#menu.debug.show_tabs_list = Show Tabs List

Expand Down
15 changes: 15 additions & 0 deletions core/src/processing/core/PApplet.java
Original file line number Diff line number Diff line change
Expand Up @@ -4011,6 +4011,21 @@ static public void debug(String msg) {
}
//

/**
* Gathers diagnostic information about the sketch and system.
* <p>
* This method is intended to be called remotely via the debugger
* to collect system information, memory statistics, and sketch
* runtime details for troubleshooting purposes.
* </p>
*
* @return JSON string containing diagnostic information
* @since 4.4
*/
public String getDiagnostics() {
return PDiagnostics.gather(this);
}

/*
// not very useful, because it only works for public (and protected?)
// fields of a class, not local variables to methods
Expand Down
188 changes: 188 additions & 0 deletions core/src/processing/core/PDiagnostics.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */

/*
Part of the Processing project - http://processing.org

Copyright (c) 2012-25 The Processing Foundation

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation, version 2.1.

This library 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
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
*/

package processing.core;

import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
* System diagnostics gathering for Processing sketches.
* <p>
* This class collects system information, Processing runtime details,
* and memory statistics for debugging purposes. It's designed to be
* called from the PDE debugger over the debug connection.
* </p>
*
* @author Processing Foundation
* @since 4.4
*/
public class PDiagnostics {

// Privacy-safe system properties to include
private static final String[] SAFE_PROPERTIES = {
"java.version",
"java.vendor",
"java.vm.name",
"java.vm.version",
"java.runtime.name",
"java.runtime.version",
"os.name",
"os.version",
"os.arch",
"file.separator",
"path.separator",
"line.separator"
};


/**
* Gathers diagnostic information from a running sketch.
*
* @param applet the PApplet instance to gather diagnostics from
* @return JSON string containing diagnostic information
*/
public static String gather(PApplet applet) {
StringBuilder json = new StringBuilder();
json.append("{\n");

// Timestamp
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
json.append(" \"timestamp\": \"").append(dateFormat.format(new Date())).append("\",\n");

// Processing version (read from system property, set by PDE at launch)
String processingVersion = System.getProperty("processing.version", "unknown");
json.append(" \"processingVersion\": ").append(jsonString(processingVersion)).append(",\n");

// System properties
json.append(" \"system\": {\n");
boolean first = true;
for (String prop : SAFE_PROPERTIES) {
String value = System.getProperty(prop);
if (value != null) {
if (!first) json.append(",\n");
// Sanitize line separator for JSON
if (prop.equals("line.separator")) {
value = escapeForJson(value);
}
json.append(" \"").append(prop).append("\": ").append(jsonString(value));
first = false;
}
}
json.append("\n },\n");

// Memory information
Runtime runtime = Runtime.getRuntime();
json.append(" \"memory\": {\n");
json.append(" \"totalMemory\": ").append(runtime.totalMemory()).append(",\n");
json.append(" \"freeMemory\": ").append(runtime.freeMemory()).append(",\n");
json.append(" \"maxMemory\": ").append(runtime.maxMemory()).append(",\n");
json.append(" \"usedMemory\": ").append(runtime.totalMemory() - runtime.freeMemory()).append("\n");
json.append(" },\n");

// Sketch-specific information
if (applet != null) {
json.append(" \"sketch\": {\n");

// Renderer information
PGraphics graphics = applet.g;
if (graphics != null) {
json.append(" \"renderer\": ").append(jsonString(graphics.getClass().getSimpleName())).append(",\n");
json.append(" \"width\": ").append(applet.width).append(",\n");
json.append(" \"height\": ").append(applet.height).append(",\n");
json.append(" \"pixelDensity\": ").append(applet.pixelDensity).append(",\n");
json.append(" \"frameCount\": ").append(applet.frameCount).append(",\n");
json.append(" \"frameRate\": ").append(String.format("%.2f", applet.frameRate)).append(",\n");
}

json.append(" \"focused\": ").append(applet.focused).append("\n");
json.append(" },\n");
}

// Display information
try {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] devices = ge.getScreenDevices();

json.append(" \"displays\": [\n");
for (int i = 0; i < devices.length; i++) {
if (i > 0) json.append(",\n");
GraphicsDevice device = devices[i];
DisplayMode mode = device.getDisplayMode();

json.append(" {\n");
json.append(" \"id\": ").append(i).append(",\n");
json.append(" \"width\": ").append(mode.getWidth()).append(",\n");
json.append(" \"height\": ").append(mode.getHeight()).append(",\n");
json.append(" \"refreshRate\": ").append(mode.getRefreshRate()).append(",\n");
json.append(" \"bitDepth\": ").append(mode.getBitDepth()).append("\n");
json.append(" }");
}
json.append("\n ]\n");

} catch (Exception e) {
json.append(" \"displays\": \"Error: ").append(e.getMessage()).append("\"\n");
}

json.append("}");
return json.toString();
}


/**
* Converts a string to JSON-safe quoted string.
*/
private static String jsonString(String value) {
if (value == null) {
return "null";
}
return "\"" + escapeForJson(value) + "\"";
}


/**
* Escapes special characters for JSON.
*/
private static String escapeForJson(String value) {
return value
.replace("\\", "\\\\")
.replace("\"", "\\\"")
.replace("\n", "\\n")
.replace("\r", "\\r")
.replace("\t", "\\t");
}


/**
* Formats bytes into human-readable format (KB, MB, GB).
*/
public static String formatBytes(long bytes) {
if (bytes < 1024) return bytes + " B";
int exp = (int) (Math.log(bytes) / Math.log(1024));
char unit = "KMGT".charAt(exp - 1);
return String.format("%.2f %sB", bytes / Math.pow(1024, exp), unit);
}
}
105 changes: 105 additions & 0 deletions java/src/processing/mode/java/debug/Debugger.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,16 @@ public void actionPerformed(ActionEvent e) {

debugMenu.addSeparator();

item = Toolkit.newJMenuItemExt("menu.debug.gather_diagnostics");
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Messages.log("Invoked 'Gather Diagnostics' menu item");
gatherDiagnostics();
}
});
debugMenu.add(item);
item.setEnabled(false);

item =
Toolkit.newJMenuItem(Language.text("menu.debug.toggle_breakpoint"), 'B');
item.addActionListener(new ActionListener() {
Expand Down Expand Up @@ -458,6 +468,101 @@ public synchronized void stepOut() {
}


/**
* Gather system diagnostics from the running sketch.
* <p>
* Calls the getDiagnostics() method on the sketch's PApplet instance
* via JDWP and displays the results in a dialog.
* </p>
*/
public synchronized void gatherDiagnostics() {
if (!isStarted()) {
editor.statusNotice("Sketch must be running in debug mode to gather diagnostics.");
return;
}

if (!isPaused()) {
editor.statusNotice("Please pause the sketch first (at a breakpoint or use Continue/Step).");
return;
}

try {
if (currentThread == null || currentThread.frameCount() == 0) {
editor.statusError("No valid stack frame available.");
return;
}

ObjectReference appletRef = currentThread.frame(0).thisObject();
if (appletRef == null) {
editor.statusError("Could not find PApplet instance. This may happen if the sketch " +
"is paused in a static method. Try pausing in an instance method instead.");
return;
}

ReferenceType appletType = appletRef.referenceType();
if (!(appletType instanceof ClassType)) {
editor.statusError("PApplet reference is not a ClassType.");
return;
}

// Walk the class hierarchy to find the getDiagnostics method
Method diagnosticsMethod = null;
ClassType currentClass = (ClassType) appletType;
while (currentClass != null && diagnosticsMethod == null) {
diagnosticsMethod = currentClass.concreteMethodByName("getDiagnostics", "()Ljava/lang/String;");
if (diagnosticsMethod == null) {
currentClass = currentClass.superclass();
}
}

if (diagnosticsMethod == null) {
editor.statusError("getDiagnostics() method not found. Make sure core library is up to date.");
return;
}

editor.statusNotice("Gathering diagnostics from sketch...");
Value resultValue = appletRef.invokeMethod(
currentThread,
diagnosticsMethod,
new ArrayList<>(),
ObjectReference.INVOKE_SINGLE_THREADED
);

if (resultValue == null) {
editor.statusError("getDiagnostics() returned null.");
return;
}

if (!(resultValue instanceof StringReference)) {
editor.statusError("getDiagnostics() returned unexpected type: " + resultValue.getClass().getName());
return;
}

StringReference result = (StringReference) resultValue;
String diagnosticData = result.value();

javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
DiagnosticsDialog dialog = new DiagnosticsDialog(editor, diagnosticData);
dialog.setVisible(true);
}
});

editor.statusNotice("Diagnostics gathered successfully.");

} catch (IncompatibleThreadStateException e) {
editor.statusError("Thread is not suspended.");
logitse(e);
} catch (ClassCastException e) {
editor.statusError("Unexpected return type from getDiagnostics().");
e.printStackTrace();
} catch (Exception e) {
editor.statusError("Error gathering diagnostics: " + e.getMessage());
e.printStackTrace();
}
}


// /** Print the current stack trace. */
// public synchronized void printStackTrace() {
// if (isStarted()) {
Expand Down
Loading