Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CJS Decoder doesn't work #1050

Open
paulober opened this issue Oct 9, 2024 · 0 comments
Open

CJS Decoder doesn't work #1050

paulober opened this issue Oct 9, 2024 · 0 comments

Comments

@paulober
Copy link

paulober commented Oct 9, 2024

Describe the bug

If I want to use following .cjs file as advanced decoder in the rttConfig > decoders I always get following error: "Error Initializing Advanced Decoder: Error: Unable to load decoder class from: mypath"
But if I have an issue in the file like a super() call in the constructor without a parent class I get an error but if everything is fixed it just shows the generic error.

To Reproduce
Steps to reproduce the behavior:

  1. Set following decoder.cjs as decoder

Expected behavior

It gets loaded.

Environment (please complete the following information):

  • Cortex-Debug Version (this extension) current version
  • OS: Windows 11
  • GDB Version: arm-13.7
  • Compiler Toolchain Version: arm-none-eabi 13_2_Rel1

Please include launch.json

Note: We are unlikely to look at the issue if you do not supply this

Paste launch.json contents here
...
"rttConfig": {
        "enabled": true,
        "address": "auto",
        "decoders": [
          {
            "label": "My Label",
            "type": "advanced",
            "decoder": "${command:some.getDecoderPath}",
            "inputmode": "disabled",
            "noprompt": true,
            "ports": [
              0
            ],
            "config": {
              "elfPath": "${command:some.elfPath}"
            }
          }
        ],
      }

Additional context

const { spawn } = require('child_process');

class DefmtDecoder {
  constructor() {
    this.process = null;
    this.elfPath = null;
    this.displayOutput = null;
    this.graphData = null;
    this.ports = [];
  }

  init(config, displayOutput, graphData) {
    // Store the callbacks and elfPath from the config
    this.elfPath = config.elfPath;
    this.displayOutput = displayOutput;
    this.graphData = graphData;
    this.ports = config.ports;

    const defmtPrintPath = `${process.platform === "win32" ? process.env.USERPROFILE : process.env.HOME}/.cargo/bin/defmt-print${process.platform === "win32" ? ".exe" : ""}`;

    // Spawn the defmt-print process with the provided ELF path
    this.process = spawn(defmtPrintPath, ['-e', this.elfPath, "stdin"]);

    // Handle data from defmt-print stdout and relay it to the displayOutput callback
    this.process.stdout.on('data', (data) => {
      if (this.displayOutput) {
        this.displayOutput(data.toString());
      }
    });

    // Handle errors from defmt-print stderr
    this.process.stderr.on('data', (data) => {
      if (this.displayOutput) {
        this.displayOutput(data.toString());
      }
    });

    // Handle when the process closes
    this.process.on('close', (code) => {
      if (this.displayOutput) {
        this.displayOutput(`Decoding process exited with code: ${code}`);
      }
    });
  }

  sendData(input) {
    // Write input data to defmt-print's stdin
    try {
      if (this.process && this.process.stdin.writable) {
        this.process.stdin.write(input);
        return;
      }
    } catch { }

    throw new Error('Process stdin is not writable.');
  }

  // Expected methods from the SWODecoder API conforming to the AdvancedDecoder interface

  typeName() {
    return 'DefmtDecoder';
  }

  outputLabel() {
    return 'RPi Pico';
  }

  softwareEvent(port, data) {
    if (this.ports.indexOf(port) !== -1) {
      // Handle the software event, potentially by sending data to defmt-print stdin
      this.sendData(data);
    }
  }

  synchronized() {
    // Handle the synchronized event
    if (this.displayOutput) {
      this.displayOutput('Synchronized');
    }
  }

  lostSynchronization() {
    // Handle the lost synchronization event
    if (this.displayOutput) {
      this.displayOutput('Lost synchronization');
    }
  }

  dispose() {
    // Clean up the process
    if (this.process) {
      this.process.kill();
      this.process = null;
    }
  }
}

module.exports = exports = DefmtDecoder;
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

No branches or pull requests

1 participant