Skip to content

Add debug messaging system to wolfPKCS11#100

Merged
dgarske merged 13 commits into
masterfrom
devin/1750344640-debug-messaging
Jun 20, 2025
Merged

Add debug messaging system to wolfPKCS11#100
dgarske merged 13 commits into
masterfrom
devin/1750344640-debug-messaging

Conversation

@devin-ai-integration
Copy link
Copy Markdown
Contributor

Add Debug Messaging System to wolfPKCS11

Summary

This PR implements a comprehensive debug messaging system for wolfPKCS11 that provides function enter/exit tracing and parameter logging for public PKCS#11 functions.

Changes Made

Debug Infrastructure

  • Compile-time control: Added DEBUG_WOLFPKCS11 flag support with --enable-debug configure option
  • Runtime control: Implemented wolfPKCS11_Debugging_On() and wolfPKCS11_Debugging_Off() functions
  • Debug macros: Added WOLFPKCS11_ENTER(), WOLFPKCS11_LEAVE(), and WOLFPKCS11_MSG() macros
  • Conditional compilation: Debug code is completely compiled out when DEBUG_WOLFPKCS11 is not defined

Instrumented Functions

  • wolfpkcs11.c: C_GetFunctionList(), C_Initialize(), C_Finalize(), C_GetInfo()
  • crypto.c: C_CreateObject(), C_DestroyObject() with parameter logging
  • slot.c: C_GetSlotList(), C_GetSlotInfo(), C_Login() with parameter logging

Public API

  • Added debug control function declarations to wolfpkcs11/pkcs11.h
  • Functions are conditionally declared based on DEBUG_WOLFPKCS11 flag
  • Used WP11_API visibility macro for proper symbol export

Testing

  • Included debug_test.c demonstration program showing debug functionality
  • Program demonstrates both debug-enabled and debug-disabled behavior

Design Decisions

Following wolfSSL Patterns

  • Modeled after wolfSSL's logging system in wolfcrypt/src/logging.c
  • Used similar function naming conventions and macro patterns
  • Consistent with existing wolfSSL debug infrastructure

Printf Output

  • Uses printf() by default as requested
  • Debug output format: "WOLFPKCS11 ENTER: function_name" and "WOLFPKCS11 LEAVE: function_name, returning X"
  • Parameter details logged for key functions like C_CreateObject() and C_Login()

Conditional Compilation

  • All debug code is wrapped in #ifdef DEBUG_WOLFPKCS11 guards
  • Zero runtime overhead when debug is disabled
  • Macros compile to empty statements when debug is disabled

Usage

Build with Debug

./configure --enable-debug
make

Runtime Control

#ifdef DEBUG_WOLFPKCS11
wolfPKCS11_Debugging_On();   // Enable debug output
// ... PKCS#11 function calls will now produce debug output
wolfPKCS11_Debugging_Off();  // Disable debug output
#endif

Example Output

wolfPKCS11 debug logging enabled
WOLFPKCS11 ENTER: C_GetFunctionList
WOLFPKCS11 LEAVE: C_GetFunctionList, returning 0
WOLFPKCS11 ENTER: C_Initialize
WOLFPKCS11 LEAVE: C_Initialize, returning 0

Testing

  • Created comprehensive test program demonstrating functionality
  • Verified conditional compilation works correctly
  • All debug code is properly guarded and compiles cleanly

Link to Devin run

https://app.devin.ai/sessions/0600d82de3fc4c0393b0e846643b337a

Requested by

andrew@wolfssl.com

devin-ai-integration Bot and others added 2 commits June 19, 2025 15:07
- Add DEBUG_WOLFPKCS11 compile flag support for conditional debug compilation
- Implement wolfPKCS11_Debugging_On() and wolfPKCS11_Debugging_Off() functions
- Add WOLFPKCS11_ENTER/LEAVE/MSG debug macros with printf output
- Instrument public functions in wolfpkcs11.c, crypto.c, and slot.c
- Add debug control function declarations to public API (pkcs11.h)
- Include debug test program demonstrating functionality
- Follow wolfSSL logging patterns for consistency

Debug system provides function enter/exit tracing and parameter logging
for public PKCS#11 functions when enabled with --enable-debug configure option.

Co-Authored-By: andrew@wolfssl.com <andrew@wolfssl.com>
- Replace direct printf calls with WOLFPKCS11_MSG macro
- Add user-customizable macro support with #ifndef guards
- Instrument all public PKCS#11 functions across crypto.c, slot.c, and wolfpkcs11.c
- Ensure consistent debug logging pattern with enter/exit tracing and parameter details

Co-Authored-By: andrew@wolfssl.com <andrew@wolfssl.com>
Comment thread src/crypto.c Outdated
devin-ai-integration Bot and others added 5 commits June 20, 2025 14:03
… printf in slot.c, move debug_test.c to tests

- Add varargs support to WOLFPKCS11_MSG macro for printf-style formatting
- Replace all direct printf calls in slot.c with WOLFPKCS11_MSG macro calls
- Convert all snprintf + WOLFPKCS11_MSG patterns to direct WOLFPKCS11_MSG calls
- Move debug_test.c from root to tests directory as official test
- Update tests/README.md to document debug_test.c purpose

Co-Authored-By: andrew@wolfssl.com <andrew@wolfssl.com>
…_MSG macro

- Replace ##__VA_ARGS__ with standard __VA_ARGS__ for MSVC compatibility
- Complete conversion of all snprintf + WOLFPKCS11_MSG patterns to direct varargs calls
- Fixes 90 compilation errors in Windows CI build

Co-Authored-By: andrew@wolfssl.com <andrew@wolfssl.com>
- Add debug_test to tests/include.am for proper build and test integration
- Return code 77 (skip test) when DEBUG_WOLFPKCS11 is not defined
- Implement stdout capture mechanism to verify debug output is generated
- Test passes/fails based on whether debug messages are actually produced
- Follows wolfPKCS11 test framework patterns and conventions

Co-Authored-By: andrew@wolfssl.com <andrew@wolfssl.com>
- Change main() to main(void) to fix strict-prototypes warning
- Remove unused debug_output_detected variable
- Addresses CI build failures caused by -Werror treating warnings as errors

Co-Authored-By: andrew@wolfssl.com <andrew@wolfssl.com>
- Move variable declarations inside DEBUG_WOLFPKCS11 ifdef to avoid unused variable errors in non-debug builds
- Ensure debug_test links against libwolfpkcs11.la in both static and dynamic builds

Co-Authored-By: andrew@wolfssl.com <andrew@wolfssl.com>
@LinuxJedi
Copy link
Copy Markdown
Member

I think this might be ready for review now. If it is approved, it might be a good idea to squash on merge.

@dgarske dgarske self-requested a review June 20, 2025 14:58
@dgarske dgarske self-assigned this Jun 20, 2025
Copy link
Copy Markdown
Member

@dgarske dgarske left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's very noisy when wolfPKCS11_Debugging_On();, but that's probably okay.

Comment thread debug_test.c Outdated
Comment thread tests/debug_test.c
Comment thread tests/debug_test.c Outdated
Comment thread tests/debug_test.c Outdated
Comment thread src/crypto.c Outdated
@dgarske dgarske assigned LinuxJedi and unassigned dgarske Jun 20, 2025
- Add standard wolfSSL GPL license header to debug_test.c with 2025 copyright
- Fix function brace style (braces on their own lines)
- Fix mixed variable declarations (move declarations to top of blocks)
- Fix line length violations (break long lines under 80 chars)
- Update copyright year in crypto.c from 2023 to 2025

Co-Authored-By: andrew@wolfssl.com <andrew@wolfssl.com>
@LinuxJedi LinuxJedi requested a review from dgarske June 20, 2025 15:24
@LinuxJedi LinuxJedi assigned dgarske and unassigned LinuxJedi Jun 20, 2025
Comment thread src/internal.c Outdated
Comment thread src/internal.c Outdated
devin-ai-integration Bot and others added 4 commits June 20, 2025 15:37
- Replace printf with WOLFPKCS11_MSG in wolfPKCS11_Debugging_On()
- Replace printf with WOLFPKCS11_MSG in wolfPKCS11_Debugging_Off()
- Ensures consistent debug output formatting across all debug functions

Co-Authored-By: andrew@wolfssl.com <andrew@wolfssl.com>
- Add ## before __VA_ARGS__ to handle empty argument lists
- Fixes compilation errors in wolfPKCS11_Debugging_On() and wolfPKCS11_Debugging_Off()
- Resolves CI build failures reported by andrew@wolfssl.com

Co-Authored-By: andrew@wolfssl.com <andrew@wolfssl.com>
- Add no-op versions of wolfPKCS11_Debugging_On() and wolfPKCS11_Debugging_Off()
- Functions now exist in both debug and non-debug modes
- Addresses andrew@wolfssl.com feedback for consistent API availability

Co-Authored-By: andrew@wolfssl.com <andrew@wolfssl.com>
- Move wolfPKCS11_Debugging_On() and wolfPKCS11_Debugging_Off()
  declarations outside #ifdef DEBUG_WOLFPKCS11 block
- Functions are now available in both debug and non-debug builds
- Addresses feedback from andrew@wolfssl.com

Co-Authored-By: andrew@wolfssl.com <andrew@wolfssl.com>
@LinuxJedi LinuxJedi requested a review from dgarske June 20, 2025 15:51
- Add new 'debug' job to unit-test.yml with --enable-debug configuration
- Ensures debug messaging system is tested in CI
- Addresses request from andrew@wolfssl.com

Co-Authored-By: andrew@wolfssl.com <andrew@wolfssl.com>
@dgarske dgarske merged commit 690fb6e into master Jun 20, 2025
38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants