# Setting up JLink

**URL:** <https://forum.arduino.cc/t/setting-up-jlink/1200000>\
**Category:** IDE 2.x\
**Created:** [December 13, 2023, 1:48pm UTC](https://forum.arduino.cc/t/setting-up-jlink/1200000 "2023-12-13T13:48:34Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ddeletic](https://avatars.discourse-cdn.com/v4/letter/d/edb3f5/32.png) [@ddeletic](https://forum.arduino.cc/u/ddeletic)\
**Post date:** [December 13, 2023, 1:48pm UTC](https://forum.arduino.cc/t/setting-up-jlink/1200000/1 "2023-12-13T13:48:34Z")

</div>

Hello,

I have created a custom board platform (Nordic nRF9160 chip) Nordic Demo Kit board (nrf9160dk) has a built in J-Link debugger probe and I thought it would be great to add debugging support for this board.

After a lot of stumbling around, I somehow managed to get a working debugging configuration for a sketch, but I am not sure how I did it and I am not able to replicate the process.

I have followed both tutorials on how to setup debugging, analysed SAMD configuration, read numerous forum posts and I am still stuck. The most relevant information appears here: [https://forum.arduino.cc/t/debugging-project-out-of-iot-sktechbook/965173](https://forum.arduino.cc/t/debugging-project-out-of-iot-sktechbook/965173).

What I can do is manually find for the current sketch, `C:\Users\my_username\AppData\Local\Temp\arduino-ide2-<temporary_directory_name>`, and copy into it a previously created **launch.json**. If this JSON contains all the right information, the IDE (both v2.2.1 and v2.1.1) immediately shows it in the list of available debugging configurations, and I can debug the sketch.

For the record, once launch.json is in place, the existence of debug\_custom.json is irrelevant.

In Arduino IDE v2.2.1, the Debug button on the main toolbar is always disabled, no matter what I do. In v2.1.1 it is generally enabled, but when I place a known good launch.json into `<sketch>/.theia` directory and click Debug, I get a **'Cannot read properties of undefined (reading 'script')** error message.

So, my question is, is there a way to automatically create a working debugging configuration from profile.txt and GUI only. I cannot expect ordinary users to create launch.json themself and then be able to copy it into the exact one of hundreds of temporary directories.

I am happy to add post linking hooks into profile.txt that would create a correct launch.json and copy it into the correct location. The trouble is that I don't know what this correct location is.

So as a minimum, I would appreciate if someone could tell me how to get the name of the debugger's temporary directory where I could place launch.json automatically. I would appreciate if someone took the trouble to point me towards the details of cortex-debug plugin integration into Arduino.

A working launch.json looks something like this:

```arduino
{
    "version": "0.2.0",
    "configurations": [
      {
        "cwd": "${workspaceRoot}",
		"name": "Dejan",
        "executable": "C:/Users/username/AppData/Local/Temp/arduino/sketches/5D8829399059057C3570B582018D0B8D/AnalogInput.ino.elf",
        "type": "cortex-debug",
        "armToolchainPath": "C:/Users/username/AppData/Local/Arduino15/packages/platform_name/tools/gdb/1.0.0",
        "request": "launch",
        "device": "nrf9160",
        "servertype": "jlink",
        "serverpath": "C:/Program Files/SEGGER/JLink/JLinkGDBServerCL.exe",
        "configFiles": [
          null
        ]
      }
    ]
}

```

If you read this far, thank you for your attention. If you can provide some information that can help me move forward, I'll be very, very grateful.

Regards,  
Dejan

---

<div class="post-metadata">

**Author:** ![ptillisch](https://avatars.discourse-cdn.com/v4/letter/p/ad7895/32.png) [@ptillisch](https://forum.arduino.cc/u/ptillisch)\
**Post date:** [December 13, 2023, 3:11pm UTC](https://forum.arduino.cc/t/setting-up-jlink/1200000/2 "2023-12-13T15:11:24Z")

</div>

Hi @ddeletic.

> [@ddeletic](#):
>
> I have created a custom board platform (Nordic nRF9160 chip)

Very cool! Did you publish it online? If so, please post the link to it. It might be useful for us to be able to look at the platform code during the course of the discussion here.

Beyond that, I make an effort to be aware of all the amazing board platforms created by the Arduino community (and especially for the ones that take advantage of the interesting new features like the Arduino IDE 2.x integrated sketch debugger!).

> [@ddeletic](#):
>
> In Arduino IDE v2.2.1, the Debug button on the main toolbar is always disabled, no matter what I do.

Arduino IDE determines whether a platform has debugger support by the presence of the `debug.executable` property in the platform configuration files:

[https://arduino.github.io/arduino-cli/latest/platform-specification/#debugger-configuration-directives](https://arduino.github.io/arduino-cli/latest/platform-specification/#debugger-configuration-directives)

> [@ddeletic](#):
>
> is there a way to automatically create a working debugging configuration

This is exactly what Arduino IDE does. It automatically generates a `launch.json` file from the platform properties.

That automatically generated file is typically perfectly sufficient, so the user only needs to click the button in the Arduino IDE and they are set. However, at least in the "**Arduino SAMD Boards (32-bits ARM Cortex-M0+)**" platform, if the user wants to use a Segger J-Link probe specifically, it is also necessary for the user to create a `debug_custom.json` file. The reason is that the J-Link has a dependency on the proprietary "[**J-Link Software and Documentation Pack**](https://www.segger.com/downloads/jlink/#J-LinkSoftwareAndDocumentationPack)". The user must install that software on their own (as opposed to it being automatically installed by the Arduino IDE Boards Manager as is the case for the open source toolchains) so Arduino IDE doesn't have any way of automatically knowing where to find the `JLinkGDBServerCL` executable.

Maybe you will find some better approach to locating that executable in order to achieve a fully automatic debugger configuration. But even if not, at most the user should only need to specify that path in a `debug_custom.json` file. All this stuff you are doing with making your own `launch.json` is completely unnecessary.

> [@ddeletic](#):
>
> from profile.txt

What is `profile.txt`?

---

<div class="post-metadata">

**Author:** ![ddeletic](https://avatars.discourse-cdn.com/v4/letter/d/edb3f5/32.png) [@ddeletic](https://forum.arduino.cc/u/ddeletic)\
**Post date:** [December 14, 2023, 9:45am UTC](https://forum.arduino.cc/t/setting-up-jlink/1200000/3 "2023-12-14T09:45:18Z")

</div>

Hi @ptillisch,

thank you for taking the time to respond.

> [@ptillisch](#):
>
> Did you publish it online?

Not yet, mainly because none of the online services I use will accept a 133MB toolchain archive. Also, because it is still WIP - e.g. I am not sure yet that analogRead() produces valid readings. I will certainly let you know when I do eventually publish.

> [@ptillisch](#):
>
> What is `profile.txt` ?

Oh, it was past 1am when I wrote that original post. It is a typo which was meant to be `platform.txt`. 🙂

> [@ptillisch](#):
>
> Arduino IDE determines whether a platform has debugger support by the presence of the `debug.executable` property in the platform configuration files:

My `platform.txt` contains the following line:

`debug.executable={build.path}/{build.project_name}.ino.elf`

This should be sufficient, shouldn't it? I am not sure what it expands to when Arduino parses it. Is there a way to increase command line output verbosity level?

Even if I use:

`debug.executable=C:/Users/username/AppData/Local/Temp/arduino/sketches/5D8829399059057C3570B582018D0B8D/AnalogInput.ino.elf`

the Debug button remains disabled. I am positive that the elf file exists. In GitBash:

`cd C:/Users/username/AppData/Local/Temp/arduino/sketches/5D8829399059057C3570B582018D0B8D && ls -l AnalogInput.ino.elf`  
`-rw-r--r-- 1 user 197121 4018560 Dec 13 00:04 AnalogInput.ino.elf`

![image](https://europe1.discourse-cdn.com/arduino/original/4X/a/6/1/a61f35ec7c9251ee53ac9790476cd14ac74a87a8.png)

> [@ptillisch](#):
>
> All this stuff you are doing with making your own `launch.json` is completely unnecessary.

I wouldn't be doing any of that, if only I could get past the first hurdle - getting the Debug button to be enabled. Hopefully all pieces fall into place after that. My intention is to require minimal, and if possible, no user action in order to debug.

> [@ptillisch](#):
>
> This is exactly what Arduino IDE does. It automatically generates a `launch.json` file from the platform properties.
> 
> That automatically generated file is typically perfectly sufficient, so the user only needs to click the button in the Arduino IDE and they are set.

Clearly, in my case things do not work as expected, launch.json isn't generated. And yet, if I generate it manually, debugging works.

> [@ptillisch](#):
>
> Maybe you will find some better approach to locating that [JLinkGDBServerCL] executable in order to achieve a fully automatic debugger configuration.

Users will have to be instructed to install **J-Link software** somehow. Without it, there is no debugging. At this point, perhaps they can be asked to install into the default location (C:/Program Files/SEGGER/JLink) in which case platform.txt/debug\_custom.json will know where to find it.

For the record, my `platform.txt` contains the following debug directives:

```arduino
debug.name="nrf9160"
debug.executable={build.path}/{build.project_name}.ino.elf
debug.toolchain.path={runtime.tools.gdb.path}
debug.toolchain=gcc

debug.device=nrf9160
debug.server=jlink
debug.interface=swd

```

And both, the sketch and {build.path} directories contain debug\_custom.json with the following:

```arduino
{
    "name": "nrf9160",
    "device": "nrf9160",
    "serverpath": "C:/Program Files/SEGGER/JLink/JLinkGDBServerCL.exe"
}

```

Again, any additional information would be greatly appreciated.  
Thanks,  
Dejan

---

<div class="post-metadata">

**Author:** ![ddeletic](https://avatars.discourse-cdn.com/v4/letter/d/edb3f5/32.png) [@ddeletic](https://forum.arduino.cc/u/ddeletic)\
**Post date:** [December 26, 2023, 4:07am UTC](https://forum.arduino.cc/t/setting-up-jlink/1200000/4 "2023-12-26T04:07:10Z")

</div>

If anybody stumbles here, and is interested in the topic, my experience may help.

I have made some progress with the helping hand of **`arduino-cli debug`** (v0.34.2) which provides more diagnostics than the IDE.

Several things I was doing wrong:

- my debug.executable directive in platform.txt was incorrect, and arduino-cli helpfully pointed it out (I had an extra .ino)
- in platform.txt, debug.server must be set to openocd. Setting it to 'jlink' is rejected as not supported, and omitting it altogether is not allowed
- this must be overridden in debug\_custom.json with `"servertype": "jlink"`
- the above two points do not apply to Arduino IDE. It will happily accept `debug.server=jlink` in platform.txt
- I was under the impression (perhaps implied from an earlier version of the documentation) that debug.toolchain.prefix defaults to arm-none-eabi-. Incorrect. It has to be explicitly stated.

After fixing the above, I was able to start debugging using arduino-cli.

* * *

However, **Arduino IDE** debugging is still a struggle.

The 'Start Debugging' button on the main toolbar is always disabled on startup. Therefore launch.json can never be created. By pure accident, I discovered that I can re-enable it by changing the board type, and then reverting back to my board. Once I get the button enabled, launch.json is created and debugging can commence. Debugging works just fine after this point.

Unfortunately, besides being quite unintuitive and annoying having to do it for every sketch, this trick for enabling the button works in Arduino IDE v2.2.1 but not in the latest nightly build (202031225), so I expect it will cause problems down the line.

Addditionally, selecting the correct directives in platform.txt and debug\_custom.json was NOT plain sailing either. After a lot of trial and error attempts I found out that:

- platform.txt must contain 'debug.server.jlink.script=' directive even if empty. Otherwise when creating launch.json the IDE issues a cryptic notification "Cannot read properties of undefined (reading 'script')"
- [Platform specification documentation](https://arduino.github.io/arduino-cli/0.35/platform-specification/#custom-config-for-cortext-debug-plugin-for-arduino-ide) describes `debug.cortex-debug.custom.*` directives. Unfortunately they do NOT work in v2.2.1. Not even when copied verbatim from the documentation.
- until the previous point is resolved, there is no way around having debug\_custom.json maanually created next to the sketch file. This is not a great user experience and I feel that a small effort is needed to allow developers to automate this step.

For the record, my latest set of debugging directives in platform.txt is:

```arduino
debug.server=jlink
debug.server.jlink.script=
debug.toolchain.path={runtime.tools.arm-none-eabi-gdb.path}
debug.toolchain.prefix=arm-none-eabi-
debug.svd_file={runtime.platform.path}/nrf9160.svd
debug.executable={build.path}/{build.project_name}.elf

```

and debug\_custom.json is:

```arduino
{
    "name": "BoSL nrf9160",
    "serverpath": "C:/Program Files/SEGGER/JLink/JLinkGDBServerCL.exe",
    "device": "nrf9160"
}

```

**NOTE:** The cortex-debug plugin appears to be helpful by offering to create or add debugging configuration right in the GUI, and with a skeleton configuration and handy hover hints. However, it saves launch.json in .theia directory which seems to be completely ignored by the IDE. The only way to correctly create launch.json is to click on the 'Start Debugging' button on the main toolbar (providing it is enabled).

---

<div class="post-metadata">

**Author:** ![ptillisch](https://avatars.discourse-cdn.com/v4/letter/p/ad7895/32.png) [@ptillisch](https://forum.arduino.cc/u/ptillisch)\
**Post date:** [December 26, 2023, 4:56pm UTC](https://forum.arduino.cc/t/setting-up-jlink/1200000/5 "2023-12-26T16:56:12Z")

</div>

> [@ddeletic](#):
>
> The 'Start Debugging' button on the main toolbar is always disabled on startup. Therefore launch.json can never be created. By pure accident, I discovered that I can re-enable it by changing the board type, and then reverting back to my board

This is a known bug in Arduino IDE 2.2.0 and 2.2.1:

> <https://github.com/arduino/arduino-ide/issues/2237>
>
> \### Describe the problem
> 
> The board+port selection is associated with sketches…. That board and port are automatically selected in Arduino IDE when the sketch is later opened.
> 
> On startup, Arduino IDE automatically opens the sketches that were open at the time the previous IDE session was exited.
> 
> 🐛 If the board selection is the one produced by the IDE opening an existing sketch with an associated board, the "Start Debugging" button will be incorrectly disabled.
> 
> \### To reproduce
> 
> 1. Select \*\*File \> New\*\* from the Arduino IDE menus.
> 1. Select \*\*File \> Save As...\*\* from the Arduino IDE menus.
> 1. Save the sketch to any convenient name and location.
> \*\*ⓘ\*\* This step is performed in order to associate the board+network port selection with a sketch that can be restored on IDE startup.
> 1. Select a board from the Arduino IDE menus.
> 1. Select \*\*File \> Quit\*\* from the Arduino IDE menus.
> 1. Start Arduino IDE.
> The saved sketches that were open during the previous session will be restored.
> 1. Select the window of the sketch that has the board pre-selected due to the association created in the previous steps.
> 1. Hover the mouse pointer over the "Start Debugging" button on the IDE toolbar.
> 
> 🐛 The button is disabled and a "\*\*Debug - No board selected\*\*" message appears on the toolbar.
> 
> \### Arduino IDE version
> 
> 2.2.1
> 
> \### Operating system
> 
> Windows
> 
> \### Operating system version
> 
> 11
> 
> \### Additional context
> 
> I bisected the regression to 69ae38effaf950ae6fba83617b08b1cc0bc0a40d / https://github.com/arduino/arduino-ide/pull/2165 (does not occur when using the build from the previous commit 9a6a457)
> 
> \<a name="workaround"\>\</a\>
> 
> \#### Workaround
> 
> 1. Select any other board from the \*\*Tools \> Board\*\* menu in Arduino IDE.
> 1. Select the target board in Arduino IDE once again.
> 
> After doing that, the "Start Debugging" button will work as expected.
> 
> \#### Additional reports
> 
> \- https://github.com/arduino/arduino-ide/issues/2237#issuecomment-1733163552
> 
> \#### Related
> 
> \- https://github.com/arduino/arduino-ide/issues/2230
> \- https://github.com/dankeboy36/esp-exception-decoder/issues/10
> \- https://github.com/arduino/arduino-ide/pull/2165#pullrequestreview-1568872036
> 
> \### Issue checklist
> 
> \- \[X\] I searched for previous reports in \[the issue tracker\](https://github.com/arduino/arduino-ide/issues?q=)
> \- \[X\] I verified the problem still occurs when using the latest \[nightly build\](https://www.arduino.cc/en/software#nightly-builds)
> \- \[X\] My report contains all necessary details

It has already been fixed, but there hasn't been a release since that time. The fix is in the Arduino IDE nightly build. The download links for the nightly build are listed here:

[https://www.arduino.cc/en/software#nightly-builds](https://www.arduino.cc/en/software#nightly-builds)

I highly recommend you to use the nightly build if you are doing any work with the debugging framework because the framework was recently completely redone. The reason for this was that the platform developers ran into limitations of the old framework that forced them to make hacky workarounds like automatically generating `debug_custom.json` files. You might find that the problems you are currently experiencing no longer exist once you use the new framework.

You should refer to the "dev" version of the Arduino Platform Specification which is applicable to the 0.35.0-rc.7 pre-release version of Arduino CLI currently used by the latest nightly build of Arduino IDE:

> **[Platform specification - Arduino CLI](https://arduino.github.io/arduino-cli/dev/platform-specification/)**
>
> Arduino Command Line Interface

If you are working with Arduino CLI directly, you will also want to use the 0.35.0-rc.7 pre-release, which is available for download from the links under the " **Assets** section of the GitHub release page:

> **[Release v0.35.0-rc.7 · arduino/arduino-cli](https://github.com/arduino/arduino-cli/releases/tag/v0.35.0-rc.7)**
>
> Changelog
> 77222ec Fix caching for libraries when installation folder differents from Name (#2446)

---

<div class="post-metadata">

**Author:** ![ddeletic](https://avatars.discourse-cdn.com/v4/letter/d/edb3f5/32.png) [@ddeletic](https://forum.arduino.cc/u/ddeletic)\
**Post date:** [February 2, 2024, 5:54am UTC](https://forum.arduino.cc/t/setting-up-jlink/1200000/6 "2024-02-02T05:54:19Z")

</div>

Hello, Arduino gurus, and @ptillisch in particular. Thanks for your help so far. Sorry for the long delay, I have been busy with other, non-arduino stuff.

Firstly

> [@ptillisch](#):
>
> Did you publish it online?

It has now been published at `[https://www.bosl.com.au/code/nrf9160dk/package\_bosl\_nrf9160dk\_index.json](https://www.bosl.com.au/code/nrf9160dk/package_bosl_nrf9160dk_index.json)

As for my troubles with JLink, they are still there. Nightly builds do not resolve the issue. As a matter of fact, the issue is even worse, given that the Debug button is always inactive, and I cannot get the workaround used in v2.2.1 to work. Hopefully someone can use my package to test and perhaps resolve the issue.

Thanks,  
Dejan

---

<div class="post-metadata">

**Author:** ![ddeletic](https://avatars.discourse-cdn.com/v4/letter/d/edb3f5/32.png) [@ddeletic](https://forum.arduino.cc/u/ddeletic)\
**Post date:** [February 20, 2024, 12:43pm UTC](https://forum.arduino.cc/t/setting-up-jlink/1200000/7 "2024-02-20T12:43:26Z")

</div>

Wooooooo Hooooo!

Arduino IDE v2.3.2 finally solves debugging issues I've had. Debugging now works as expected.

Thank you developers!

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [August 28, 2024, 1:36pm UTC](https://forum.arduino.cc/t/setting-up-jlink/1200000/9 "2024-08-28T13:36:22Z")

</div>

Closed on August 28, 2024, 1:36pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
