From 1e1dbfe63bb7a09a76fcca077f332eb6d8755948 Mon Sep 17 00:00:00 2001 From: Veronika Hanulikova Date: Thu, 4 Jun 2026 13:17:01 +0200 Subject: [PATCH 01/16] docs: Add motivation section into README --- README.adoc | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.adoc b/README.adoc index ebd1502a..ad0639bc 100644 --- a/README.adoc +++ b/README.adoc @@ -8,6 +8,21 @@ that provides a completely automated preprocessing, compilation, installation and profiling of JavaCard code on JavaCard smart cards or in the jCardSim simulator. +Why profiling? +-------------- +* *Side-channel security* -- non-constant execution time or memory usage can leak secret key material. The per-trap histograms immediately reveal whether a branch or loop depends on secret data. +* *Performance optimisation* -- identify slow operations and decide whether to move them to native JavaCard commands. +* *Hardware validation* -- confirm that your applet behaves identically on the real card and in the simulator before deployment. + +What You Get +~~~~~~~~~~~~ +After a successful run, JCProfilerNext produces: + +* timing or memory measurements, +* an interactive visualisation of those measurements with per-line histograms, +* original source files annotated with inline measurement comments, +* frequency table of every JavaCard API call made by your applet (stats mode only). + Prerequisites ------------- * Install JDK 8 or newer. From 4abaad2c99833139e0c64709428af86611fc4779 Mon Sep 17 00:00:00 2001 From: Veronika Hanulikova Date: Thu, 4 Jun 2026 13:19:05 +0200 Subject: [PATCH 02/16] docs: Add details about physical card prerequisities into README --- README.adoc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.adoc b/README.adoc index ad0639bc..74af7cf6 100644 --- a/README.adoc +++ b/README.adoc @@ -27,6 +27,8 @@ Prerequisites ------------- * Install JDK 8 or newer. * Obtain a copy of the JavaCard Development Kit, e.g. from this link:https://github.com/martinpaljak/oracle_javacard_sdks[GitHub archive] maintained by Martin Paljak. +* [Real card only] The card must have sufficient free EEPROM space for your applet. The instrumented applet is larger than the original (profiling traps are inserted), so ensure a few extra kilobytes are available. Use `--simulator` to avoid this requirement entirely. +* [Real card only] A PC/SC-compatible smart card reader and its drivers must be installed. .Compatibility Matrix [cols="^1,^2"] From f396e32fc1f41f37a78d9939011280add6cbb43b Mon Sep 17 00:00:00 2001 From: Veronika Hanulikova Date: Thu, 4 Jun 2026 13:19:54 +0200 Subject: [PATCH 03/16] docs: Add initial FAQ section --- README.adoc | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/README.adoc b/README.adoc index 74af7cf6..7deaa7c8 100644 --- a/README.adoc +++ b/README.adoc @@ -220,6 +220,28 @@ javacard.security,KeyPair,genKeyPair(),1 javacard.security,KeyPair,"KeyPair(byte,short)",1 ---- +FAQ +--- + +*My card is not recognised / connection fails.*:: + Check that a PC/SC driver for your reader is installed and that no other application is locking the card. + +*Compilation fails with a javac error.*:: + Verify that your JDK version is compatible with the target JavaCard SDK — see the Compatibility Matrix. + JDK 8 offers the widest compatibility (JavaCard 2.2.1+). + +*The tool crashes with an unexpected exception.*:: + Re-run with `--debug`, collect the full output, and open an issue at + https://github.com/crocs-muni/JCProfilerNext. + Please include the `--debug` log and the exact command you ran. + +*Measurements are inconsistent between runs.*:: + Increase `--repeat-count` (default: 1000) to reduce statistical noise. + +*What JDK should I use?*:: + JDK 8 is recommended for the widest JavaCard SDK compatibility. + See the Compatibility Matrix under _Prerequisites_ for details. + Limitations ----------- From 346b492bf709616e21462f795d80c85b777b66d2 Mon Sep 17 00:00:00 2001 From: Veronika Hanulikova Date: Thu, 4 Jun 2026 13:21:23 +0200 Subject: [PATCH 04/16] docs: Add section about uninstallation of applet from card --- README.adoc | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.adoc b/README.adoc index 7deaa7c8..a0f4320e 100644 --- a/README.adoc +++ b/README.adoc @@ -58,6 +58,12 @@ Compilation .. directly using built JAR: `java -jar build/libs/JCProfilerNext-1.0-SNAPSHOT.jar --help`. .. directly from distribution archives in `build/distributions`. +Profiling Applet Uninstallation +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +If you installed an applet on a real card during a profiling run, remove it using +link:https://github.com/martinpaljak/GlobalPlatformPro[GlobalPlatformPro]: +The default package AID is `123456789001` and the default applet AID is `12345678900101`. + Execution modes --------------- The tool can be executed in four different modes (select with `--mode`): From 0edd17cc4e95d3a687592e76282c0272e2ffb227 Mon Sep 17 00:00:00 2001 From: Veronika Hanulikova Date: Thu, 4 Jun 2026 13:50:35 +0200 Subject: [PATCH 05/16] args: Add flags for default and required options --- src/main/java/jcprofiler/args/Args.java | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/main/java/jcprofiler/args/Args.java b/src/main/java/jcprofiler/args/Args.java index 3c8142ea..fbfe4010 100644 --- a/src/main/java/jcprofiler/args/Args.java +++ b/src/main/java/jcprofiler/args/Args.java @@ -29,24 +29,24 @@ public class Args { public boolean debug = false; @Parameter(names = {"-w", "--work-dir"}, - description = "Path to the working directory", + description = "Path to the working directory (required)", required = true, converter = DirectoryPathConverter.class) public Path workDir; @Parameter(names = {"--start-from"}, - description = "Start from executing the given stage", + description = "Start from executing the given stage(default: instrumentation)", converter = StageConverter.class) public Stage startFrom = Stage.instrumentation; @Parameter(names = {"--stop-after"}, - description = "Stop after executing the given stage", + description = "Stop after executing the given stage (default: visualisation)", converter = StageConverter.class) public Stage stopAfter = Stage.visualisation; @Parameter(names = {"--jckit"}, required = true, - description = "Path to the root directory with JavaCard development kit", + description = "Path to the root directory with JavaCard development kit (required)", converter = JCKitConverter.class) public JavaCardSDK jcSDK; @@ -76,7 +76,7 @@ public class Args { public byte[] installParams; @Parameter(names = {"--mode"}, - description = "Measure the selected characteristic", + description = "Measure the selected characteristic (default: time)", converter = ModeConverter.class) public Mode mode = Mode.time; @@ -96,7 +96,7 @@ public class Args { public Path customHandler; @Parameter(names = {"--repeat-count"}, - description = "Number of profiling rounds (custom or time profiling only)", + description = "Number of profiling rounds (custom or time profiling only) (default: 1000)", validateWith = PositiveIntegerValidator.class) public int repeatCount = 1000; @@ -106,22 +106,22 @@ public class Args { public Byte resetIns; @Parameter(names = {"--cla"}, - description = "Applet CLA in hex", + description = "Applet CLA in hex (default: 0x00)", converter = ByteConverter.class) public byte cla = 0; @Parameter(names = {"--ins"}, - description = "Applet instruction in hex", + description = "Applet instruction in hex (default: 0x00)", converter = ByteConverter.class) public byte ins = 0; @Parameter(names = {"--p1"}, - description = "Applet P1 in hex", + description = "Applet P1 in hex (default: 0x00)", converter = ByteConverter.class) public byte p1 = 0; @Parameter(names = {"--p2"}, - description = "Applet P2 in hex", + description = "Applet P2 in hex (default: 0x00)", converter = ByteConverter.class) public byte p2 = 0; @@ -136,12 +136,12 @@ public class Args { public Path dataFile; @Parameter(names = {"--input-division"}, - description = "Divide generated inputs into two categories based on given characteristic", + description = "Divide generated inputs into two categories based on given characteristic (default: none)", converter = InputDivisionConverter.class) public InputDivision inputDivision = InputDivision.none; @Parameter(names = {"--time-unit"}, - description = "Time unit to be used in result visualisation (time profiling only)", + description = "Time unit to be used in result visualisation (time profiling only) (default: micro)", converter = TimeUnitConverter.class) public TimeUnit timeUnit = TimeUnit.micro; } From 169da03d7c341a0eee40bacb4f5b354e7d6623cc Mon Sep 17 00:00:00 2001 From: Veronika Hanulikova Date: Thu, 4 Jun 2026 14:48:51 +0200 Subject: [PATCH 06/16] help: Add extended help option Add note about required arguments, examples of tool usage in different modes for first-time users and note about bug reporting. --- src/main/java/jcprofiler/Main.java | 32 ++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/main/java/jcprofiler/Main.java b/src/main/java/jcprofiler/Main.java index 5659fe2a..1510671d 100644 --- a/src/main/java/jcprofiler/Main.java +++ b/src/main/java/jcprofiler/Main.java @@ -50,6 +50,7 @@ public static void main(final String[] argv) { // show help if (args.help) { jc.usage(); + printHelpExtras(); return; } @@ -82,6 +83,37 @@ public static void main(final String[] argv) { } } + private static void printHelpExtras() { + System.out.println(); + System.out.println("ARGUMENT STRUCTURE"); + System.out.println(" Required arguments for a profiling run: --work-dir and --jckit."); + System.out.println(" Hex values (--cla, --ins, --p1, --p2, --reset-ins) accept either a plain"); + System.out.println(" hex number (e.g. EE) or a 0x-prefixed value (e.g. 0xEE)."); + System.out.println(" Either --data-regex or --data-file must be provided when profiling a method"); + System.out.println(" (not needed for memory profiling of the constructor or stats mode)."); + System.out.println(); + System.out.println("EXAMPLES"); + System.out.println(); + System.out.println(" # Profile the 'example' method using the simulator (time mode, default):"); + System.out.println(" JCProfilerNext --work-dir my_applet --jckit path/to/jc222_kit \\"); + System.out.println(" --executable example --ins 0xEE \\"); + System.out.println(" --data-regex 00[0-9A-F]{2} --simulator"); + System.out.println(); + System.out.println(" # Measure memory usage during applet installation (constructor):"); + System.out.println(" JCProfilerNext --work-dir my_applet --jckit path/to/jc304_kit --mode memory"); + System.out.println(); + System.out.println(" # Collect API usage statistics:"); + System.out.println(" JCProfilerNext --work-dir my_applet --jckit path/to/jc222_kit --mode stats"); + System.out.println(); + System.out.println(" # Run only instrumentation and compilation, skip installation/profiling:"); + System.out.println(" JCProfilerNext --work-dir my_applet --jckit path/to/jc222_kit \\"); + System.out.println(" --stop-after compilation"); + System.out.println(); + System.out.println("REPORTING BUGS"); + System.out.println(" If you encounter an unexpected error, re-run with --debug, save the output,"); + System.out.println(" and open an issue at https://github.com/crocs-muni/JCProfilerNext"); + } + /** * Validates command line arguments. * From 549284c6bc0e3ed346bc2ff0ed21f7a24397057a Mon Sep 17 00:00:00 2001 From: Veronika Hanulikova Date: Thu, 4 Jun 2026 14:52:11 +0200 Subject: [PATCH 07/16] profiling: Highlight current stage of profiling in output log --- src/main/java/jcprofiler/JCProfiler.java | 46 ++++++++++++++++++------ 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/src/main/java/jcprofiler/JCProfiler.java b/src/main/java/jcprofiler/JCProfiler.java index 2f888f76..7359987c 100644 --- a/src/main/java/jcprofiler/JCProfiler.java +++ b/src/main/java/jcprofiler/JCProfiler.java @@ -37,17 +37,24 @@ private JCProfiler() {} */ public static void run(final Args args) { if (args.mode == Mode.stats) { - log.info("Collecting API usage statistics."); + log.info(""); + log.info("+---------------------------------+"); + log.info("| Stage 1/1: stats |"); + log.info("+---------------------------------+"); new Instrumenter(args).generateStatistics(); - log.info("Collecting complete."); + log.info(""); + log.info("Done. Results written to: {}", args.workDir.toAbsolutePath()); + log.info(" APIstatistics.csv -- API usage frequency table"); return; } // Instrumentation if (args.startFrom.ordinal() <= Stage.instrumentation.ordinal()) { - log.info("Instrumentation started."); + log.info(""); + log.info("+---------------------------------+"); + log.info("| Stage 1/5: instrumentation |"); + log.info("+---------------------------------+"); JCProfilerUtil.moveToSubDirIfNotExists(args.workDir, JCProfilerUtil.getSourceInputDirectory(args.workDir)); - new Instrumenter(args).process(); log.info("Instrumentation complete."); } @@ -64,7 +71,10 @@ public static void run(final Args args) { // Compilation if (args.startFrom.ordinal() <= Stage.compilation.ordinal()) { - log.info("Compilation started."); + log.info(""); + log.info("+---------------------------------+"); + log.info("| Stage 2/5: compilation |"); + log.info("+---------------------------------+"); Compiler.compile(args, entryPoint); log.info("Compilation complete."); } @@ -75,11 +85,13 @@ public static void run(final Args args) { // Installation CardManager cardManager = null; if (args.startFrom.ordinal() <= Stage.installation.ordinal()) { - // noop for --simulator + log.info(""); + log.info("+---------------------------------+"); + log.info("| Stage 3/5: installation |"); + log.info("+---------------------------------+"); if (args.useSimulator) { - log.info("Skipping installation because simulator is used."); + log.info("Skipping installation (simulator mode)."); } else { - log.info("Installation started."); cardManager = Installer.installOnCard(args, entryPoint); log.info("Installation complete."); } @@ -90,12 +102,15 @@ public static void run(final Args args) { // Profiling if (args.startFrom.ordinal() <= Stage.profiling.ordinal()) { + log.info(""); + log.info("+---------------------------------+"); + log.info("| Stage 4/5: profiling |"); + log.info("+---------------------------------+"); // Connect if the installation was skipped or simulator is used if (cardManager == null) // TODO: move connection stuff to a separate class? cardManager = Installer.connect(args, entryPoint); - log.info("Profiling started."); final AbstractProfiler profiler = AbstractProfiler.create(args, cardManager, model); profiler.profile(); profiler.generateCSV(); @@ -106,11 +121,20 @@ public static void run(final Args args) { return; // Visualisation - log.info("Visualising results."); + log.info(""); + log.info("+---------------------------------+"); + log.info("| Stage 5/5: visualisation |"); + log.info("+---------------------------------+"); final AbstractVisualiser vis = AbstractVisualiser.create(args, model); vis.loadAndProcessMeasurements(); vis.generateHTML(); vis.insertMeasurementsToSources(); - log.info("Visualising results complete."); + log.info("Visualisation complete."); + + log.info(""); + log.info("Done. Results written to: {}", args.workDir.toAbsolutePath()); + log.info(" measurements.csv -- raw per-trap measurements"); + log.info(" measurements.html -- interactive visualisation"); + log.info(" sources_perf/ -- sources annotated with inline measurements"); } } From fe3e14dc50706603338f4edbd285ac0a5330a110 Mon Sep 17 00:00:00 2001 From: Veronika Hanulikova Date: Thu, 4 Jun 2026 14:53:46 +0200 Subject: [PATCH 08/16] docs: Add note about installDist --- README.adoc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.adoc b/README.adoc index a0f4320e..3d083fd6 100644 --- a/README.adoc +++ b/README.adoc @@ -57,6 +57,10 @@ Compilation .. directly using Gradle: `./gradlew run --args='--help'`. .. directly using built JAR: `java -jar build/libs/JCProfilerNext-1.0-SNAPSHOT.jar --help`. .. directly from distribution archives in `build/distributions`. +.. as a standalone binary (recommended): run `./gradlew installDist`, then use + `build/install/JCProfilerNext/bin/JCProfilerNext` (Unix) or + `build\install\JCProfilerNext\bin\JCProfilerNext.bat` (Windows). + You can add this `bin/` directory to your `PATH` for convenient access. Profiling Applet Uninstallation ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ From 8b90db919dfe88cca8ef18b1fbeec01b4b32e1c8 Mon Sep 17 00:00:00 2001 From: Veronika Hanulikova Date: Thu, 4 Jun 2026 15:01:54 +0200 Subject: [PATCH 09/16] docs: Add concrete notes and un/qualified names of applets/methods --- README.adoc | 5 ++++- src/main/java/jcprofiler/Main.java | 21 ++++++++++++++------- src/main/java/jcprofiler/args/Args.java | 7 ++++--- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/README.adoc b/README.adoc index 3d083fd6..de429c9f 100644 --- a/README.adoc +++ b/README.adoc @@ -135,8 +135,11 @@ public class Example extends Applet { Run as: [source,console] ---- -$ ./gradlew run --args="--work-dir example --jckit jc222_kit --executable example --ins 0xEE --data-regex 00[0-9A-F]{2} --repeat-count 100" +$ ./gradlew run --args="--work-dir example --jckit jc222_kit --entry-point example.Example --executable example --ins 0xEE --data-regex 00[0-9A-F]{2} --repeat-count 100" ---- +The `--entry-point` value is the *fully-qualified class name* (`package.ClassName`). +The `--executable` value is the *unqualified method name* (`methodName`). + JCProfilerNext will process all sources in the `example` directory and will also use this directory to store all intermediate results: diff --git a/src/main/java/jcprofiler/Main.java b/src/main/java/jcprofiler/Main.java index 1510671d..7fbf3397 100644 --- a/src/main/java/jcprofiler/Main.java +++ b/src/main/java/jcprofiler/Main.java @@ -87,6 +87,8 @@ private static void printHelpExtras() { System.out.println(); System.out.println("ARGUMENT STRUCTURE"); System.out.println(" Required arguments for a profiling run: --work-dir and --jckit."); + System.out.println(" --entry-point expects a fully-qualified class name: package.ClassName"); + System.out.println(" --executable expects an unqualified method name: myMethod"); System.out.println(" Hex values (--cla, --ins, --p1, --p2, --reset-ins) accept either a plain"); System.out.println(" hex number (e.g. EE) or a 0x-prefixed value (e.g. 0xEE)."); System.out.println(" Either --data-regex or --data-file must be provided when profiling a method"); @@ -94,19 +96,24 @@ private static void printHelpExtras() { System.out.println(); System.out.println("EXAMPLES"); System.out.println(); - System.out.println(" # Profile the 'example' method using the simulator (time mode, default):"); - System.out.println(" JCProfilerNext --work-dir my_applet --jckit path/to/jc222_kit \\"); - System.out.println(" --executable example --ins 0xEE \\"); + System.out.println(" # Profile a specific method (time mode, real card):"); + System.out.println(" JCProfilerNext --work-dir path/to/applet --jckit path/to/jc304_kit \\"); + System.out.println(" --entry-point com.example.MyApplet --executable myMethod \\"); + System.out.println(" --repeat-count 100 --data-regex \"[0-9A-F]{64}\" --mode time"); + System.out.println(); + System.out.println(" # Profile a method using the simulator, no entry-point needed (single applet):"); + System.out.println(" JCProfilerNext --work-dir path/to/applet --jckit path/to/jc222_kit \\"); + System.out.println(" --executable myMethod --ins 0xEE \\"); System.out.println(" --data-regex 00[0-9A-F]{2} --simulator"); System.out.println(); - System.out.println(" # Measure memory usage during applet installation (constructor):"); - System.out.println(" JCProfilerNext --work-dir my_applet --jckit path/to/jc304_kit --mode memory"); + System.out.println(" # Measure memory usage during constructor (no --executable needed):"); + System.out.println(" JCProfilerNext --work-dir path/to/applet --jckit path/to/jc304_kit --mode memory"); System.out.println(); System.out.println(" # Collect API usage statistics:"); - System.out.println(" JCProfilerNext --work-dir my_applet --jckit path/to/jc222_kit --mode stats"); + System.out.println(" JCProfilerNext --work-dir path/to/applet --jckit path/to/jc222_kit --mode stats"); System.out.println(); System.out.println(" # Run only instrumentation and compilation, skip installation/profiling:"); - System.out.println(" JCProfilerNext --work-dir my_applet --jckit path/to/jc222_kit \\"); + System.out.println(" JCProfilerNext --work-dir path/to/applet --jckit path/to/jc222_kit \\"); System.out.println(" --stop-after compilation"); System.out.println(); System.out.println("REPORTING BUGS"); diff --git a/src/main/java/jcprofiler/args/Args.java b/src/main/java/jcprofiler/args/Args.java index fbfe4010..3df67338 100644 --- a/src/main/java/jcprofiler/args/Args.java +++ b/src/main/java/jcprofiler/args/Args.java @@ -62,12 +62,13 @@ public class Args { public boolean useSimulator = false; @Parameter(names = {"--executable"}, - description = "Constructor or method to profile or leave unset to instrument applet entry point " + - "constructor (constructors are custom and memory profiling only)") + description = "Unqualified method name to profile (e.g. myMethod). " + + "Omit to profile the entry-point class constructor (constructors: custom and memory mode only)") public String executable; @Parameter(names = {"--entry-point"}, - description = "Qualified name of a class to be used as an entry point (useful when there are more)") + description = "Fully-qualified class name of the applet entry point (e.g. com.example.MyApplet). " + + "Required when the working directory contains more than one applet class") public String entryPoint; @Parameter(names = {"--install-params"}, From c1e0c64d3b6529e4be398729bbf7f8a5a56c85eb Mon Sep 17 00:00:00 2001 From: Veronika Hanulikova Date: Tue, 16 Jun 2026 10:30:30 +0200 Subject: [PATCH 10/16] args: Show help when no argument given --- src/main/java/jcprofiler/Main.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/java/jcprofiler/Main.java b/src/main/java/jcprofiler/Main.java index 7fbf3397..8862b2f6 100644 --- a/src/main/java/jcprofiler/Main.java +++ b/src/main/java/jcprofiler/Main.java @@ -33,6 +33,17 @@ public class Main { public static void main(final String[] argv) { Configurator.setRootLevel(Level.INFO); + // show help + if (argv.length == 0) { + JCommander.newBuilder() + .addObject(new Args()) + .programName("JCProfilerNext") + .build() + .usage(); + printHelpExtras(); + return; + } + // parse commandline arguments final Args args = new Args(); final JCommander jc = JCommander.newBuilder() @@ -47,7 +58,6 @@ public static void main(final String[] argv) { System.exit(1); } - // show help if (args.help) { jc.usage(); printHelpExtras(); From 092ef5ce1c415b22211f16271d4fa014f1b4739c Mon Sep 17 00:00:00 2001 From: Veronika Hanulikova Date: Wed, 8 Jul 2026 15:39:40 +0200 Subject: [PATCH 11/16] profiling: Move logging of every profiling round to DEBUG --- src/main/java/jcprofiler/profiling/TimeProfiler.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/jcprofiler/profiling/TimeProfiler.java b/src/main/java/jcprofiler/profiling/TimeProfiler.java index 9fee390b..d373864e 100644 --- a/src/main/java/jcprofiler/profiling/TimeProfiler.java +++ b/src/main/java/jcprofiler/profiling/TimeProfiler.java @@ -60,12 +60,16 @@ protected void profileImpl() { // main profiling loop generateInputs(args.repeatCount); + final int progressStep = Math.max(1, args.repeatCount / 10); for (int round = 1; round <= args.repeatCount; round++) { final CommandAPDU triggerAPDU = getInputAPDU(round); final String input = Util.bytesToHex(triggerAPDU.getBytes()); - log.info("Round: {}/{} APDU: {}", round, args.repeatCount, input); + log.debug("Round: {}/{} APDU: {}", round, args.repeatCount, input); profileSingleStep(triggerAPDU); + + if (round % progressStep == 0 || round == args.repeatCount) + log.info("Progress: {}/{} rounds completed.", round, args.repeatCount); } // sanity check From b59f0868b841cee118251a93feb96afb62df421f Mon Sep 17 00:00:00 2001 From: Veronika Hanulikova Date: Wed, 8 Jul 2026 15:48:00 +0200 Subject: [PATCH 12/16] log: Reduce messages in INFO logging --- src/main/java/jcprofiler/Main.java | 2 +- src/main/java/jcprofiler/installation/Installer.java | 2 +- .../java/jcprofiler/instrumentation/Instrumenter.java | 8 ++++---- .../processors/AbstractInsertTrapProcessor.java | 2 +- .../processors/AbstractModifyEntryPointProcessor.java | 2 +- src/main/java/jcprofiler/profiling/AbstractProfiler.java | 6 +++--- src/main/java/jcprofiler/util/JCProfilerUtil.java | 2 +- .../java/jcprofiler/visualisation/AbstractVisualiser.java | 8 ++++---- .../java/jcprofiler/visualisation/TimeVisualiser.java | 2 +- 9 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/main/java/jcprofiler/Main.java b/src/main/java/jcprofiler/Main.java index 8862b2f6..8c55a82a 100644 --- a/src/main/java/jcprofiler/Main.java +++ b/src/main/java/jcprofiler/Main.java @@ -66,11 +66,11 @@ public static void main(final String[] argv) { // TODO: add proper versioning info as well log.info("Welcome to JCProfilerNext!"); - log.info("Command-line arguments parsed successfully."); if (args.debug) { Configurator.setRootLevel(Level.DEBUG); log.info("LogLevel set to DEBUG."); } + log.debug("Command-line arguments parsed successfully."); // log basic info log.info("Found JavaCard SDK {} ({})", args.jcSDK.getRelease(), args.jcSDK.getRoot().getAbsolutePath()); diff --git a/src/main/java/jcprofiler/installation/Installer.java b/src/main/java/jcprofiler/installation/Installer.java index 7e2b3497..313ddfa3 100644 --- a/src/main/java/jcprofiler/installation/Installer.java +++ b/src/main/java/jcprofiler/installation/Installer.java @@ -131,7 +131,7 @@ public static CardManager connect(final Args args, final CtClass entryPoint) * could not be selected successfully */ private static CardManager configureSimulator(final Args args, final CtClass entryPoint) { - log.info("Configuring jCardSim simulator."); + log.debug("Configuring jCardSim simulator."); // assure jCardSim compatibility with newer bouncycastle System.setProperty("com.licel.jcardsim.bouncycastle.rsa.allow_unsafe_mod", "true"); diff --git a/src/main/java/jcprofiler/instrumentation/Instrumenter.java b/src/main/java/jcprofiler/instrumentation/Instrumenter.java index af8bbff6..924dbe57 100644 --- a/src/main/java/jcprofiler/instrumentation/Instrumenter.java +++ b/src/main/java/jcprofiler/instrumentation/Instrumenter.java @@ -92,14 +92,14 @@ public void process() { spoon.addProcessor(new SpoonWorkarounds.FixStaticMethodImportProcessor()); spoon.addProcessor(new SpoonWorkarounds.FixStaticFieldImportProcessor()); - log.info("Instrumenting existing classes."); + log.debug("Instrumenting existing classes."); spoon.process(); // save the result spoon.getEnvironment().setOutputType(OutputType.CLASSES); spoon.setSourceOutputDirectory(outputDir.toFile()); - log.info("Saving instrumented classes."); + log.debug("Saving instrumented classes."); spoon.prettyprint(); // check that all PMC members are unique @@ -138,7 +138,7 @@ private CtModel buildModel(final Launcher spoon) { * @param model Spoon model */ private void checkArguments(final CtModel model) { - log.info("Validating '--entry-point' and '--executable' arguments."); + log.debug("Validating '--entry-point' and '--executable' arguments."); // validate args.entryPoint args.entryPoint = JCProfilerUtil.getEntryPoint(model, args.entryPoint).getQualifiedName(); @@ -168,7 +168,7 @@ private void checkArguments(final CtModel model) { * @throws UnsupportedOperationException for multipackage projects, for projects using the default package */ private void addMissingClasses(final Launcher spoon) { - log.info("Generating additional classes."); + log.debug("Generating additional classes."); // this atrocity seems to be required as Spoon does not allow a module rebuild :( final Launcher tmpSpoon = new Launcher(); diff --git a/src/main/java/jcprofiler/instrumentation/processors/AbstractInsertTrapProcessor.java b/src/main/java/jcprofiler/instrumentation/processors/AbstractInsertTrapProcessor.java index dd6eaa02..1693afff 100644 --- a/src/main/java/jcprofiler/instrumentation/processors/AbstractInsertTrapProcessor.java +++ b/src/main/java/jcprofiler/instrumentation/processors/AbstractInsertTrapProcessor.java @@ -77,7 +77,7 @@ public void process(final T executable) { trapNamePrefix = JCProfilerUtil.getTrapNamePrefix(executable); // instrument - log.info("Instrumenting {}.", fullSignature); + log.debug("Instrumenting {}.", fullSignature); final CtBlock block = executable.getBody(); if (isEmptyBlock(block)) { diff --git a/src/main/java/jcprofiler/instrumentation/processors/AbstractModifyEntryPointProcessor.java b/src/main/java/jcprofiler/instrumentation/processors/AbstractModifyEntryPointProcessor.java index e7beb623..6f3b5755 100644 --- a/src/main/java/jcprofiler/instrumentation/processors/AbstractModifyEntryPointProcessor.java +++ b/src/main/java/jcprofiler/instrumentation/processors/AbstractModifyEntryPointProcessor.java @@ -56,7 +56,7 @@ public boolean isToBeProcessed(final CtClass cls) { * @param fieldName custom instruction field name */ protected void process(final CtClass cls, final String fieldName) { - log.info("Instrumenting entry point class {}.", cls.getQualifiedName()); + log.debug("Instrumenting entry point class {}.", cls.getQualifiedName()); // get process(APDU) method final CtMethod processMethod = JCProfilerUtil.getProcessMethod(cls); diff --git a/src/main/java/jcprofiler/profiling/AbstractProfiler.java b/src/main/java/jcprofiler/profiling/AbstractProfiler.java index 5c9c897e..628bd4ca 100644 --- a/src/main/java/jcprofiler/profiling/AbstractProfiler.java +++ b/src/main/java/jcprofiler/profiling/AbstractProfiler.java @@ -170,7 +170,7 @@ protected String getTrapName(final short trapID) { * @throws RuntimeException if the extraction of traps from profiled executable failed */ private void buildPerfMapping() { - log.info("Looking for traps in the {}.", profiledExecutableSignature); + log.debug("Looking for traps in the {}.", profiledExecutableSignature); final String trapNamePrefix = JCProfilerUtil.getTrapNamePrefix(profiledExecutable); // get traps form profiledExecutable @@ -197,7 +197,7 @@ private void buildPerfMapping() { for (final CtField f : traps) { final CtLiteral evaluated = f.getDefaultExpression().partiallyEvaluate(); trapNameMap.put(evaluated.getValue().shortValue(), f.getSimpleName()); - log.info("Found {}.", f.getSimpleName()); + log.debug("Found {}.", f.getSimpleName()); } } @@ -218,7 +218,7 @@ protected void generateInputs(int size) { // regex if (args.dataRegex != null) { - log.info("Generating inputs from regular expression {}.", args.dataRegex); + log.debug("Generating inputs from regular expression {}.", args.dataRegex); final RgxGen rgxGen = RgxGen.parse(args.dataRegex); for (int i = 0; i < size * 100; i++) { diff --git a/src/main/java/jcprofiler/util/JCProfilerUtil.java b/src/main/java/jcprofiler/util/JCProfilerUtil.java index 0d36e0a2..2f6e4c15 100644 --- a/src/main/java/jcprofiler/util/JCProfilerUtil.java +++ b/src/main/java/jcprofiler/util/JCProfilerUtil.java @@ -671,7 +671,7 @@ public static void setupSpoon(final SpoonAPI spoon, final Args args) { * @return a {@link SpoonAPI} instance */ public static SpoonAPI getInstrumentedSpoon(final Args args) { - log.info("Validating Spoon model."); + log.debug("Validating Spoon model."); final Launcher spoon = new Launcher(); setupSpoon(spoon, args); diff --git a/src/main/java/jcprofiler/visualisation/AbstractVisualiser.java b/src/main/java/jcprofiler/visualisation/AbstractVisualiser.java index 14d7c2a5..fa3a4e68 100644 --- a/src/main/java/jcprofiler/visualisation/AbstractVisualiser.java +++ b/src/main/java/jcprofiler/visualisation/AbstractVisualiser.java @@ -146,7 +146,7 @@ public void loadAndProcessMeasurements() { */ private void loadCSV() { final Path csv = JCProfilerUtil.checkFile(args.workDir.resolve("measurements.csv"), Stage.profiling); - log.info("Loading measurements from {}.", csv); + log.debug("Loading measurements from {}.", csv); try (final CSVParser parser = CSVParser.parse(csv, Charset.defaultCharset(), JCProfilerUtil.getCSVFormat())) { final Iterator it = parser.iterator(); @@ -218,7 +218,7 @@ public void insertMeasurementsToSources() { final Path outputDir = JCProfilerUtil.getPerfOutputDirectory(args.workDir); JCProfilerUtil.recreateDirectory(outputDir); - log.info("Inserting measurements into sources."); + log.debug("Inserting measurements into sources."); final SpoonAPI spoon = JCProfilerUtil.getInstrumentedSpoon(args); spoon.setSourceOutputDirectory(outputDir.toFile()); spoon.addProcessor(getInsertMeasurementsProcessor()); @@ -237,7 +237,7 @@ public void insertMeasurementsToSources() { * Generates the HTML page with interactive visualisation. */ public void generateHTML() { - log.info("Initializing Apache Velocity."); + log.debug("Initializing Apache Velocity."); final Properties props = new Properties(); props.put(RuntimeConstants.EVENTHANDLER_INCLUDE, IncludeRelativePath.class.getName()); props.put(RuntimeConstants.RUNTIME_REFERENCES_STRICT, true); @@ -268,7 +268,7 @@ public void generateHTML() { prepareVelocityContext(context); final Path output = args.workDir.resolve("measurements.html"); - log.info("Generating {}.", output); + log.debug("Generating {}.", output); try (final Writer writer = new FileWriter(output.toFile())) { final Template template = velocityEngine.getTemplate("jcprofiler/visualisation/template.html.vm"); diff --git a/src/main/java/jcprofiler/visualisation/TimeVisualiser.java b/src/main/java/jcprofiler/visualisation/TimeVisualiser.java index 55c16352..1fcc0336 100644 --- a/src/main/java/jcprofiler/visualisation/TimeVisualiser.java +++ b/src/main/java/jcprofiler/visualisation/TimeVisualiser.java @@ -57,7 +57,7 @@ public void loadAndProcessMeasurements() { * Filters obvious outliers from the input measurements. */ private void filterOutliers() { - log.info("Filtering outliers from the loaded measurements."); + log.debug("Filtering outliers from the loaded measurements."); measurements.forEach((k, v) -> { final DescriptiveStatistics ds = new DescriptiveStatistics(); v.stream().filter(Objects::nonNull).map(Long::doubleValue).forEach(ds::addValue); From a31665addb869c886339b380093d6c29ca59c1d2 Mon Sep 17 00:00:00 2001 From: Veronika Hanulikova Date: Wed, 8 Jul 2026 16:04:39 +0200 Subject: [PATCH 13/16] log: Introduce TRACE level of logging --- src/main/java/jcprofiler/Main.java | 9 ++++++++- src/main/java/jcprofiler/args/Args.java | 4 ++++ src/main/java/jcprofiler/profiling/TimeProfiler.java | 8 ++++---- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/main/java/jcprofiler/Main.java b/src/main/java/jcprofiler/Main.java index 8c55a82a..8691846f 100644 --- a/src/main/java/jcprofiler/Main.java +++ b/src/main/java/jcprofiler/Main.java @@ -24,6 +24,7 @@ */ public class Main { private static final Logger log = LoggerFactory.getLogger(Main.class); + private static final String CARD_MANAGER_LOGGER = "cz.muni.fi.crocs.rcard.client.CardManager"; /** * JCProfilerNext's entry point method @@ -32,6 +33,7 @@ public class Main { */ public static void main(final String[] argv) { Configurator.setRootLevel(Level.INFO); + Configurator.setLevel(CARD_MANAGER_LOGGER, Level.WARN); // show help if (argv.length == 0) { @@ -66,7 +68,12 @@ public static void main(final String[] argv) { // TODO: add proper versioning info as well log.info("Welcome to JCProfilerNext!"); - if (args.debug) { + if (args.trace) { + args.debug = true; + Configurator.setRootLevel(Level.TRACE); + Configurator.setLevel(CARD_MANAGER_LOGGER, Level.TRACE); + log.info("LogLevel set to TRACE."); + } else if (args.debug) { Configurator.setRootLevel(Level.DEBUG); log.info("LogLevel set to DEBUG."); } diff --git a/src/main/java/jcprofiler/args/Args.java b/src/main/java/jcprofiler/args/Args.java index 3df67338..3e1a1946 100644 --- a/src/main/java/jcprofiler/args/Args.java +++ b/src/main/java/jcprofiler/args/Args.java @@ -28,6 +28,10 @@ public class Args { description = "Enable debug messages") public boolean debug = false; + @Parameter(names = {"-t", "--trace"}, + description = "Enable trace messages (implies --debug; also reveals raw APDU traffic)") + public boolean trace = false; + @Parameter(names = {"-w", "--work-dir"}, description = "Path to the working directory (required)", required = true, diff --git a/src/main/java/jcprofiler/profiling/TimeProfiler.java b/src/main/java/jcprofiler/profiling/TimeProfiler.java index d373864e..2dca944b 100644 --- a/src/main/java/jcprofiler/profiling/TimeProfiler.java +++ b/src/main/java/jcprofiler/profiling/TimeProfiler.java @@ -98,7 +98,7 @@ protected void profileImpl() { * @throws RuntimeException if setting the next fatal performance trap failed */ private void setTrap(short trapID) throws CardException { - log.debug("Setting next trap to {}.", getTrapName(trapID)); + log.trace("Setting next trap to {}.", getTrapName(trapID)); CommandAPDU setTrap = new CommandAPDU(args.cla, JCProfilerUtil.INS_PERF_HANDLER, 0, 0, Util.shortToByteArray(trapID)); @@ -127,7 +127,7 @@ private void profileSingleStep(CommandAPDU triggerAPDU) throws CardException { // execute target operation final String trapName = getTrapName(trapID); - log.debug("Measuring {}.", trapName); + log.trace("Measuring {}.", trapName); final ResponseAPDU response = cardManager.transmit(triggerAPDU); // SW should be equal to the trap ID @@ -141,7 +141,7 @@ private void profileSingleStep(CommandAPDU triggerAPDU) throws CardException { // we have not reached expected performance trap unreachedTraps.add(trapName); measurements.computeIfAbsent(trapName, k -> new ArrayList<>()).add(null); - log.debug("Duration: unreachable"); + log.trace("Duration: unreachable"); continue; } @@ -150,7 +150,7 @@ private void profileSingleStep(CommandAPDU triggerAPDU) throws CardException { final long diff = currentTransmitDuration - prevTransmitDuration; prevTransmitDuration = currentTransmitDuration; - log.debug("Duration: {} ns", diff); + log.trace("Duration: {} ns", diff); // store the difference measurements.computeIfAbsent(getTrapName(trapID), k -> new ArrayList<>()).add(diff); From 7b985f5e5ff51bed52ee5aec6f44987b62c41e15 Mon Sep 17 00:00:00 2001 From: Veronika Hanulikova Date: Thu, 9 Jul 2026 12:40:43 +0200 Subject: [PATCH 14/16] log: Move compilation log to the TRACE mode --- src/main/java/jcprofiler/compilation/Compiler.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/java/jcprofiler/compilation/Compiler.java b/src/main/java/jcprofiler/compilation/Compiler.java index 36fac650..060016ca 100644 --- a/src/main/java/jcprofiler/compilation/Compiler.java +++ b/src/main/java/jcprofiler/compilation/Compiler.java @@ -64,7 +64,14 @@ public static void compile(final Args args, final CtClass entryPoint) { final DefaultLogger consoleLogger = new DefaultLogger(); consoleLogger.setErrorPrintStream(System.err); consoleLogger.setOutputPrintStream(System.out); - consoleLogger.setMessageOutputLevel(args.debug ? Project.MSG_VERBOSE : Project.MSG_INFO); + final int messageOutputLevel; + if (args.trace) + messageOutputLevel = Project.MSG_VERBOSE; + else if (args.debug) + messageOutputLevel = Project.MSG_INFO; + else + messageOutputLevel = Project.MSG_WARN; + consoleLogger.setMessageOutputLevel(messageOutputLevel); project.addBuildListener(consoleLogger); // create javacard target From 384ee2af98b448451f726dc59056050160f59d08 Mon Sep 17 00:00:00 2001 From: Veronika Hanulikova Date: Mon, 27 Jul 2026 14:36:46 +0200 Subject: [PATCH 15/16] docs: Make lower case in headings consistent --- README.adoc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.adoc b/README.adoc index de429c9f..1d0b5417 100644 --- a/README.adoc +++ b/README.adoc @@ -14,7 +14,7 @@ Why profiling? * *Performance optimisation* -- identify slow operations and decide whether to move them to native JavaCard commands. * *Hardware validation* -- confirm that your applet behaves identically on the real card and in the simulator before deployment. -What You Get +What you get ~~~~~~~~~~~~ After a successful run, JCProfilerNext produces: @@ -85,7 +85,7 @@ locating a user-provided delimiter pattern inside each captured trace. This mode is currently under development and available in the link:https://github.com/crocs-muni/JCProfilerNext/tree/spa-time[`spa-time` branch]. -Time Example +Time example ~~~~~~~~~~~~ Consider the `example` method in the following class. The outer loop is clearly not constant since its iteration count depends on the input value. On the other @@ -155,7 +155,7 @@ produced by JCProfilerNext, you will see that some parts of the code are indeed not constant, namely if you inspect the corresponding histograms of traps after the `for` loops. -Memory Example +Memory example ~~~~~~~~~~~~~~ Consider the following source code: @@ -199,7 +199,7 @@ deselect memory may also affect the amount of free transient memory and vice ver See link:https://crocs-muni.github.io/JCProfilerNext/example-memory.html[`measurements.html`] for visualisation of the measurements. -Stats Example +Stats example ~~~~~~~~~~~~~ Consider the following source code: From c109baa4ef8133d47efa2437c3c2c936edf066b2 Mon Sep 17 00:00:00 2001 From: Veronika Hanulikova Date: Mon, 27 Jul 2026 15:40:50 +0200 Subject: [PATCH 16/16] args: Use unix style usage formatting --- build.gradle | 2 +- src/main/java/jcprofiler/Main.java | 20 +++++++++----------- src/main/java/jcprofiler/args/Args.java | 25 +++++++++++++++---------- 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/build.gradle b/build.gradle index 739dfc37..9ed10d7a 100644 --- a/build.gradle +++ b/build.gradle @@ -23,7 +23,7 @@ repositories { dependencies { // commandline arguments parsing - implementation 'com.beust:jcommander:1.82' + implementation 'org.jcommander:jcommander:1.85' // last version that supports Java 8 // Spoon implementation 'fr.inria.gforge.spoon:spoon-core:9.1.0' // last version that supports Java 8 diff --git a/src/main/java/jcprofiler/Main.java b/src/main/java/jcprofiler/Main.java index 8691846f..596bf877 100644 --- a/src/main/java/jcprofiler/Main.java +++ b/src/main/java/jcprofiler/Main.java @@ -4,6 +4,7 @@ package jcprofiler; import com.beust.jcommander.JCommander; +import com.beust.jcommander.UnixStyleUsageFormatter; import jcprofiler.args.Args; import jcprofiler.util.JCProfilerUtil; @@ -35,23 +36,20 @@ public static void main(final String[] argv) { Configurator.setRootLevel(Level.INFO); Configurator.setLevel(CARD_MANAGER_LOGGER, Level.WARN); - // show help - if (argv.length == 0) { - JCommander.newBuilder() - .addObject(new Args()) - .programName("JCProfilerNext") - .build() - .usage(); - printHelpExtras(); - return; - } - // parse commandline arguments final Args args = new Args(); final JCommander jc = JCommander.newBuilder() .addObject(args) .programName("JCProfilerNext") .build(); + jc.setUsageFormatter(new UnixStyleUsageFormatter(jc)); + + // show help + if (argv.length == 0) { + jc.usage(); + printHelpExtras(); + return; + } try { jc.parse(argv); diff --git a/src/main/java/jcprofiler/args/Args.java b/src/main/java/jcprofiler/args/Args.java index 3e1a1946..474262e2 100644 --- a/src/main/java/jcprofiler/args/Args.java +++ b/src/main/java/jcprofiler/args/Args.java @@ -39,12 +39,12 @@ public class Args { public Path workDir; @Parameter(names = {"--start-from"}, - description = "Start from executing the given stage(default: instrumentation)", + description = "Start from executing the given stage", converter = StageConverter.class) public Stage startFrom = Stage.instrumentation; @Parameter(names = {"--stop-after"}, - description = "Stop after executing the given stage (default: visualisation)", + description = "Stop after executing the given stage", converter = StageConverter.class) public Stage stopAfter = Stage.visualisation; @@ -81,7 +81,7 @@ public class Args { public byte[] installParams; @Parameter(names = {"--mode"}, - description = "Measure the selected characteristic (default: time)", + description = "Measure the selected characteristic", converter = ModeConverter.class) public Mode mode = Mode.time; @@ -101,32 +101,37 @@ public class Args { public Path customHandler; @Parameter(names = {"--repeat-count"}, - description = "Number of profiling rounds (custom or time profiling only) (default: 1000)", + description = "Number of profiling rounds (custom or time profiling only)", validateWith = PositiveIntegerValidator.class) public int repeatCount = 1000; @Parameter(names = {"--reset-ins"}, description = "Applet reset instruction in hex", + defaultValueDescription = "0x00", converter = ByteConverter.class) public Byte resetIns; @Parameter(names = {"--cla"}, - description = "Applet CLA in hex (default: 0x00)", + description = "Applet CLA in hex", + defaultValueDescription = "0x00", converter = ByteConverter.class) public byte cla = 0; @Parameter(names = {"--ins"}, - description = "Applet instruction in hex (default: 0x00)", + description = "Applet instruction in hex", + defaultValueDescription = "0x00", converter = ByteConverter.class) public byte ins = 0; @Parameter(names = {"--p1"}, - description = "Applet P1 in hex (default: 0x00)", + description = "Applet P1 in hex", + defaultValueDescription = "0x00", converter = ByteConverter.class) public byte p1 = 0; @Parameter(names = {"--p2"}, - description = "Applet P2 in hex (default: 0x00)", + description = "Applet P2 in hex", + defaultValueDescription = "0x00", converter = ByteConverter.class) public byte p2 = 0; @@ -141,12 +146,12 @@ public class Args { public Path dataFile; @Parameter(names = {"--input-division"}, - description = "Divide generated inputs into two categories based on given characteristic (default: none)", + description = "Divide generated inputs into two categories based on given characteristic", converter = InputDivisionConverter.class) public InputDivision inputDivision = InputDivision.none; @Parameter(names = {"--time-unit"}, - description = "Time unit to be used in result visualisation (time profiling only) (default: micro)", + description = "Time unit to be used in result visualisation (time profiling only)", converter = TimeUnitConverter.class) public TimeUnit timeUnit = TimeUnit.micro; }