KOS, Visual Studio Code & gdb

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
User avatar
T_chan
DC Developer
DC Developer
Posts: 32
https://www.artistsworkshop.eu/meble-kuchenne-na-wymiar-warszawa-gdzie-zamowic/
Joined: Mon Aug 22, 2011 12:45 pm
Has thanked: 12 times
Been thanked: 22 times

KOS, Visual Studio Code & gdb

Post by T_chan »

Long time since I compiled for Dreamcast, so it was time to rebuild a completely new environment...
(Big thanks for all the updates to Kos / dcload-ip / GLdc / ALdc2 !)

Here are the steps I followed to integrate gdb with Visual Studio Code & compile/debug for the Dreamcast.

Disclaimers:
- I'm using Ubuntu 19.10, so if you want to run this under Windows, you'll have to adapt some things (but not much I expect)
- Can certainly be improved
- This is not configured for makefiles (yet?)

The result:
Visual Studio Code running gdb on a KOS example
Visual Studio Code running gdb on a KOS example

For those who don't have a toolchain yet, I followed these steps to install kos, kos-ports, gdb, dcloadip, GLdc & ALdc2:
Spoiler!

1) Install KOS Source:https://dcemulation.org/index.php?title ... S_on_Linux
git clone git://git.code.sf.net/p/cadcdev/kallistios /opt/toolchains/dc/kos
cd /opt/toolchains/dc/kos/utils/dc-chain
sh download.sh
sh unpack.sh
make erase=1
cp /opt/toolchains/dc/kos/doc/environ.sh.sample /opt/toolchains/dc/kos/environ.sh
source /opt/toolchains/dc/kos/environ.sh
cd /opt/toolchains/dc/kos
make

2) Install KOS-Ports Source: https://dcemulation.org/index.php?title ... S_on_Linux
git clone --recursive git://git.code.sf.net/p/cadcdev/kos-ports /opt/toolchains/dc/kos-ports
sh /opt/toolchains/dc/kos-ports/utils/build-all.sh

3) Install gdb (Sources: https://dreamcast.wiki/Building_the_req ... evelopment and
https://forums.sonicretro.org/index.php ... nux.38824/)
cd /opt/toolchains/dc/kos/utils/dc-chain
make gdb

4) Remove all unnecessary files after the toolchain build (Source: https://dreamcast.wiki/Building_the_req ... evelopment)
cd /opt/toolchains/dc/kos/utils/dc-chain
./cleanup.sh

5) Build dcload-ip & dc-tool (Source: https://github.com/sizious/dcload-ip)
git clone https://github.com/sizious/dcload-ip.git /opt/toolchains/dc/dcloadip
cd /opt/toolchains/dc/dcloadip
(edit /opt/toolchains/dc/dcloadip/Makefile.cfg to your preferences/settings. Defaults are OK (DHCP is now the default))
source /opt/toolchains/dc/kos/environ.sh
make
make install (installs dc-tool-ip to /opt/toolchains/dc/bin)

(To burn a cd with dcloadip:)
cd /opt/toolchains/dc/dcloadip/make-cd
sudo apt install wodim (wodim replaces cdrecord)
wodim -scanbus (shows the device triplet to fill in the makefile in the next step)
(edit Makefile to fill in your CD writer triplet dev=0,0,0 -> dev=4,0,0 for my 'PIONEER ' 'BD-RW BDR-208M' '1.10')
(insert a blank cd-r into your writer)
make

(To give always the same IP address to your BBA on Ubuntu 19.10: (prerequisite is that you run a DHCP server of course))
sudo cp /etc/dhcp/dhcpd.conf /etc/dhcp/dhcpd.confbackup
sudo nano /etc/dhcp/dhcpd.conf
add something like this into your subnet (with the MAC address of your BBA, and the desired IP address):
host dcbba1 {
hardware ethernet 00:B0:D1:04:2C:4B;
fixed-address 192.168.0.2;
}
sudo systemctl restart isc-dhcp-server.service

6) Install GLdc (aka OpenGL 1.2 instead of KOS's default libGL with OpenGL 1.1)
git clone https://gitlab.com/simulant/GLdc.git /opt/toolchains/dc/kos/addons/GLdc
cd /opt/toolchains/dc/kos/addons/GLdc
source /opt/toolchains/dc/kos/environ.sh
(I had to manually edit the Makefile & move "GL/version.h" to the first OBJS to compile, otherwise I got an error in the next step. Issue #64 logged: https://gitlab.com/simulant/GLdc/-/issues/64)
make defaultall

7) Install ALdc2 (core OpenAL 1.1)
git clone https://gitlab.com/simulant/aldc2.git /opt/toolchains/dc/kos/addons/ALdc2
cd /opt/toolchains/dc/kos/addons/ALdc2
source /opt/toolchains/dc/kos/environ.sh
make defaultall




8) To make it work with Visual Studio Code:
  • Install the "C/C++" extension from Microsoft
  • Create your project
  • Create the correct .vscode\tasks.json, .vscode\launch.json, .vscode\c_cpp_properties.json and kos_compile.sh: cfr this attachment
    VSCode_KOS_Files.zip
    Visual Studio Code configuration files
    (3.05 KiB) Downloaded 279 times
  • I removed the "-O2" flag from KOS_CFLAGS in /opt/toolchains/dc/kos/environ.sh, since it's taken care of by tasks.json.
    Note that normally this is not needed, since only the last optimization flag is the one that counts)
  • Be sure to include gdb_init(); in your program (you might need a #include <kos.h>)
  • Set your breakpoint in your code - don't put it on or before gdb_init();)
  • Ctrl-shift-b to compile, then select the compile task to run
  • Boot your dreamcast with dcloadip, and wait for the ip address to appear)
  • Ctrl-shift-d to debug, then select the debug launch to run & click on the green arrow to start the Debug

Troubleshooting tips:
Spoiler!

A) if this error: *** buffer overflow detected ***: /opt/toolchains/dc/bin/dc-tool-ip terminated
Aborted (core dumped)
check if the server is still running, and kill it:
sudo netstat -ap | grep :2159
sudo kill <pid>
(source: https://forums.sonicretro.org/index.php ... nux.38824/)

B) manual gdb session example (if you want to test/troubleshoot) cfr https://ftp.gnu.org/old-gnu/Manuals/gdb ... html#SEC38
(boot the dreamcast with the dcloadip disc.
Transfer your .elf file (which should contain gdb_init();) via dc-tool and start the GDB server on dc-tool-ip:
dc-tool-ip -g -t 192.168.0.2 -x myProgram.elf
Kos will init & automatically stop with the message "waiting for gdb client connection..." )
/opt/toolchains/dc/sh-elf/bin/sh-elf-gdb
target remote :2159
file myProgram.elf
(confirm to load the file, even if there is already one running)
b main (set breakpoint at main)
step (continue until next source line)
step count (same as step, but for count lines)
continue
Last edited by T_chan on Thu Sep 24, 2020 4:12 pm, edited 6 times in total.
These users thanked the author T_chan for the post (total 7):
Ian RobinsonSiZiOUSbbmarioBB HoodrpkmaslevinGyroVorbis
spencer723
DCEmu Cool Newbie
DCEmu Cool Newbie
Posts: 13
Joined: Sun Jul 26, 2020 2:31 pm
Has thanked: 3 times
Been thanked: 8 times

Re: KOS, Visual Studio Code & gdb

Post by spencer723 »

I actually did the same thing recently on Windows and wanted to make a guide. It works pretty well but code completion and stuff is a little wonky right now :P
These users thanked the author spencer723 for the post (total 2):
mrneo240Ian Robinson
User avatar
Ian Robinson
DC Developer
DC Developer
Posts: 114
Joined: Mon Mar 11, 2019 7:12 am
Has thanked: 206 times
Been thanked: 41 times

Re: KOS, Visual Studio Code & gdb

Post by Ian Robinson »

Very cool i like this a lot thanks for doing this how to faq :)
mrneo240
DCEmu Freak
DCEmu Freak
Posts: 86
Joined: Wed Mar 14, 2018 12:22 am
Has thanked: 16 times
Been thanked: 19 times

Re: KOS, Visual Studio Code & gdb

Post by mrneo240 »

spencer723 wrote: Thu Sep 10, 2020 12:48 pm I actually did the same thing recently on Windows and wanted to make a guide. It works pretty well but code completion and stuff is a little wonky right now :P
I'd love to see that!
These users thanked the author mrneo240 for the post:
Ian Robinson
User avatar
T_chan
DC Developer
DC Developer
Posts: 32
Joined: Mon Aug 22, 2011 12:45 pm
Has thanked: 12 times
Been thanked: 22 times

Re: KOS, Visual Studio Code & gdb

Post by T_chan »

update - zip file with the Visual Studio Code configuration files now also contains a c_cpp_properties.json file, which should make Intellisense work as long as your file has the correct includes :)
User avatar
SiZiOUS
DC Developer
DC Developer
Posts: 404
Joined: Fri Mar 05, 2004 2:22 pm
Location: France
Has thanked: 27 times
Been thanked: 19 times
Contact:

Re: KOS, Visual Studio Code & gdb

Post by SiZiOUS »

Hey T_chan,
This is just great!
I will test if this works on Windows. If yes, I may integrate this in DreamSDK R3 or R4 if you agree? :)
These users thanked the author SiZiOUS for the post:
dclx
User avatar
T_chan
DC Developer
DC Developer
Posts: 32
Joined: Mon Aug 22, 2011 12:45 pm
Has thanked: 12 times
Been thanked: 22 times

Re: KOS, Visual Studio Code & gdb

Post by T_chan »

Of course ! :grin:
The conversion to Windows should be quite easy.
You'll have to convert the .sh script to a batch file or smt similar, but besides that, nothing complicated - probably only some paths to be customized.
These users thanked the author T_chan for the post (total 2):
SiZiOUSdclx
User avatar
bbmario
DCEmu Freak
DCEmu Freak
Posts: 88
Joined: Wed Feb 05, 2014 5:58 am
Has thanked: 9 times
Been thanked: 3 times

Re: KOS, Visual Studio Code & gdb

Post by bbmario »

What a fantastic tutorial. This is amazing.
User avatar
ThePerfectK
Insane DCEmu
Insane DCEmu
Posts: 147
Joined: Thu Apr 27, 2006 10:15 am
Has thanked: 27 times
Been thanked: 35 times

Re: KOS, Visual Studio Code & gdb

Post by ThePerfectK »

This is terrific! I've gotten all this stuff going with QTCreator before, but I know way more people use VS Code. Thanks so much for this tutorial!!
Still Thinking!~~
Hoplias973
DCEmu Newbie
DCEmu Newbie
Posts: 2
Joined: Sat Feb 27, 2021 6:21 pm
Has thanked: 1 time
Been thanked: 0

Re: KOS, Visual Studio Code & gdb

Post by Hoplias973 »

Hi,
i have installed the docker toolchain by kazade. https://hub.docker.com/r/kazade/dreamcast-sdk/
How can it work on vscode ?
thank you
mrneo240
DCEmu Freak
DCEmu Freak
Posts: 86
Joined: Wed Mar 14, 2018 12:22 am
Has thanked: 16 times
Been thanked: 19 times

Re: KOS, Visual Studio Code & gdb

Post by mrneo240 »

Hoplias973 wrote: Sat Feb 27, 2021 6:24 pm Hi,
i have installed the docker toolchain by kazade. https://hub.docker.com/r/kazade/dreamcast-sdk/
How can it work on vscode ?
thank you
I'm not sure what you're asking.
I write everything under vsc and use a docker container.
Although I just open a terminal and call make within the container and mounted filesystem. Would that be of interest? Or are you more looking to be "in" the container?
Hoplias973
DCEmu Newbie
DCEmu Newbie
Posts: 2
Joined: Sat Feb 27, 2021 6:21 pm
Has thanked: 1 time
Been thanked: 0

Re: KOS, Visual Studio Code & gdb

Post by Hoplias973 »

Thank you for your answer.
Well I need to configure vscode with auto completion for the kos lib. And compiling my code without using the terminal.
Like dcbuild make. Is it possible?
Merci
mrneo240
DCEmu Freak
DCEmu Freak
Posts: 86
Joined: Wed Mar 14, 2018 12:22 am
Has thanked: 16 times
Been thanked: 19 times

Re: KOS, Visual Studio Code & gdb

Post by mrneo240 »

Sure you could just setup actions and such that call the commands for you. Plenty of extensions to accomplish that however you'd like.

As for intellisense, you need to pull a local copy of just the headers then add that to your include folders
These users thanked the author mrneo240 for the post (total 2):
Hoplias973Ian Robinson
User avatar
ThePerfectK
Insane DCEmu
Insane DCEmu
Posts: 147
Joined: Thu Apr 27, 2006 10:15 am
Has thanked: 27 times
Been thanked: 35 times

Re: KOS, Visual Studio Code & gdb

Post by ThePerfectK »

So I sat down and decided to try and adapt this to work with windows vscode while writing a tutorial to get it all set up. Thus far everything is straight forward, except I'm having an enormous amount of trouble actually generating sh-elf-gdb.exe in windows with python support. MingGW includes gdb.exe with python support, so I know it's possible to build the windows version with it. And I can build sh-elf.gdb.exe in windows using WSL2 and minGW just fine *if* I don't include python support. But once I try to build with python support, I get a configuration error informing me that it can't find python installed on my computer. Checking the /usr/bin and usr/local/bin directories shows me python2.7 is indeed present.

Apparently this is a common problem as I've seen lots of people complain about not finding python over the years regarding building gdb with mingw from source, but nobody ever posts a solution. I believe @sizious has build gdb from source before, perhaps he can help. Any group of people want to chime in and brainstorm a way to build sh-elf-gdb for windows with python support? It's the last hurdle before I can get vscode debugging dreamcast programs through the IDE entirely from windows/wsl2. I'd love to share with people how to do this.
These users thanked the author ThePerfectK for the post:
Ian Robinson
Still Thinking!~~
spencer723
DCEmu Cool Newbie
DCEmu Cool Newbie
Posts: 13
Joined: Sun Jul 26, 2020 2:31 pm
Has thanked: 3 times
Been thanked: 8 times

Re: KOS, Visual Studio Code & gdb

Post by spencer723 »

mrneo240 wrote: Thu Sep 10, 2020 7:06 pm
spencer723 wrote: Thu Sep 10, 2020 12:48 pm I actually did the same thing recently on Windows and wanted to make a guide. It works pretty well but code completion and stuff is a little wonky right now :P
I'd love to see that!
Wow. Sorry for leaving you hanging :oops: Here are my instructions for setting up the Dreamcast toolchain using Windows 10 and Windows Subsystem for Linux (WSL).

Installing WSL on Windows 10

Before we can start the setup process, we need to make sure WSL is installed and enabled on your computer. You can follow this guide provided by Microsoft. I used Ubuntu 20.04, personally but any flavor of Linux should work. I also HIGHLY suggest using Windows Terminal over PowerShell or Command Prompt as Terminal is far superior and will allow you to spawn new Ubuntu terminal sessions with two clicks (after WSL is setup and a Linux distribution is installed).

Installing the Dreamcast toolchain using WSL

I actually found a guide that explains how to compile and install the Dreamcast toolchain in more detail than I was originally going to write. I just performed the steps up until the "Downloading and Building dcload-ip" section since the rest wasn't relevant for my setup.

Setting up Visual Studio Code

You can download VS Code here. You'll also want to install the Remote - WSL extension, C/C++ extension and maybe also the Makefile extension provided by Microsoft (I haven't tried this yet). I copied one of the examples from the KallistiOS examples directory as a base for my new project somewhere in the Linux filesystem (I used ~/dreamcast/<project name>). Navigate to that folder in your Linux terminal and type "code .". This will launch VS Code with the Remote - WSL extension enabled and allow you to develop directly inside of WSL.

Now, create a new folder called ".vscode" and create a new file called "settings.json". Paste the following contents into settings.json:

Code: Select all

{
    "C_Cpp.default.includePath": [
        "/opt/toolchains/dc/kos/include/",
        "/opt/toolchains/dc/kos/addons/include/",
        "/opt/toolchains/dc/kos-ports/include/",
        "/opt/toolchains/dc/sh-elf/sh-elf/include/"
    ],
    "C_Cpp.default.compilerPath": "kos-cc"
}
This will tell VS Code where to look for the include files and which compiler to use for IntelliSense. When you go back to your source files, you should be able to see these awesome tooltips when you hover over functions thanks to the documentation in the source code:

Image

You can also hold CTRL and click on any of the KallistiOS methods and VS Code should navigate to the definition for that method. You can then press ALT+Left Arrow to navigate back.

I'm working on a guide on how to use lxdream with GDB within VS Code but I'm having a lot of trouble creating a CDI (which seems to be the only way VS Code and lxdream are able to communicate through GDB).

Please let me know if you need any more info :grin:
These users thanked the author spencer723 for the post (total 4):
SiZiOUSmrneo240BB Hoodrpk
User avatar
bbmario
DCEmu Freak
DCEmu Freak
Posts: 88
Joined: Wed Feb 05, 2014 5:58 am
Has thanked: 9 times
Been thanked: 3 times

Re: KOS, Visual Studio Code & gdb

Post by bbmario »

Doing the gods work here, my dude. Thank you! PLEASE SETUP A DONATION PAGE!
User avatar
rpk
DC Developer
DC Developer
Posts: 16
Joined: Thu Dec 29, 2016 1:02 pm
Has thanked: 10 times
Been thanked: 12 times

Re: KOS, Visual Studio Code & gdb

Post by rpk »

This is great! Managed to get VSCode set up with remote debugging and intellisense on Windows 11 & WSL2 using these instructions. It's way easier to step in and see what KOS is doing under the hood now. Thanks :)
maslevin
DC Developer
DC Developer
Posts: 13
Joined: Thu Apr 02, 2015 11:26 pm
Has thanked: 15 times
Been thanked: 6 times

Re: KOS, Visual Studio Code & gdb

Post by maslevin »

After meaning to try this out on macOS for literally years at this point, I dug out my Dreamcast and gave it a try. Ultimately I was able to get it to work but I had to do some extra steps to get it to compile and be usable, and I wanted to post my findings in case that would be useful for other developers in the future.

I am running macOS Big Sur 11.6.5 with XCode Command Line Tools 12.1 - your mileage may vary with different versions!

Firstly I rebuilt my toolchain from the current dc-chain (GitHub kos master branch) and attempted to build gdb. Sadly no luck out of the box. I got lots of errors about implicit call signatures for free and abort, so I patched a number of gdb source files with #include <stdlib.h> to get these errors to subside. That got gdb to build!

The rest of the guide worked for me, setting up VSCode with the tasks / launch / settings as above. However when I started to debug I got issues with gdb not being able to attach to dc-tool-ip because gdb wasn't signed. I followed the instructions at https://www.ics.uci.edu/~pattis/common/ ... stall.html to sign the gdb which was built above, which stopped this error from happening. But a different issue appeared when trying to attach to the symbols in my .elf file:
I'm sorry, Dave, I can't do that. Symbol format `elf32-shl' unknown.
I did some searching and I came across this interesting bug report here https://unix.stackexchange.com/question ... m-elf32-i3 which indicates to #include <string.h> at the top of gdb source file elf-bfd.h. If you don't do this when building gdb with LLVM based compilers, it will cause an error invoking strncmp which will surface as the above error. I applied this patch, rebuilt gdb and resigned the binary, and then I was able to attach to a remote debug session on my Dreamcast through VSCode.

I've always dreamed of having real IDE based debugging when working on Dreamcast software, and I'm really happy that it was just a few hurdles to get this working on my OS of choice. Thanks to T_Chan for the original post and for all the follow-ups from the rest of the development community!
These users thanked the author maslevin for the post (total 4):
T_chanThePerfectKBB Hood|darc|
User avatar
T_chan
DC Developer
DC Developer
Posts: 32
Joined: Mon Aug 22, 2011 12:45 pm
Has thanked: 12 times
Been thanked: 22 times

Re: KOS, Visual Studio Code & gdb

Post by T_chan »

I've been playing with GitHub codespaces this week-end,
didn't realize how easy it was nowadays to have a web-based development environment for Dreamcast...

Just takes 3 clicks and a couple of minutes of wait-time the first time you launch it...

For those who haven't tried yet, I've put my notes here, and you can also launch & try it for yourself directly:
https://github.com/Tchan0/Test_Kos_Codespace

A big part of this is of course @kazade 's pre-built docker image - thanks for that !

(& important tip - When you have finished working with your codespace, click on the green "Code" button again, and on the 3 dots next to your codespace to stop your container from running. This will save you some free execution minutes (default idle timeout is 30 minutes, and you get 120 free core hours per month).
These users thanked the author T_chan for the post (total 2):
SiZiOUSGyroVorbis
User avatar
T_chan
DC Developer
DC Developer
Posts: 32
Joined: Mon Aug 22, 2011 12:45 pm
Has thanked: 12 times
Been thanked: 22 times

Re: KOS, Visual Studio Code & gdb

Post by T_chan »

Small update on the configuration files for Visual Studio Code
(as a new post, since I have trouble updating the original post)

1) Profiles (new since 2023)
  • If you haven't already, create a "Profile" (File-Preferences-Profiles) called eg "Dreamcast" in VSCode, that will allow to keep your VSCode config & its extensions only for that Profile
  • Select that Profile for your workspace
2) Makefile Tools (new since 2021)
  • Install the "Makefile Tools" extension from Microsoft
  • Unzip the files of "kos_vscode_cfg_202306.zip" inside your workspace (they should all be in the .vscode folder of your project)
Be sure to close VSCode & reopen your project

With all the above, do a ctrl+shift+P -> "Makefile: build current target"

That should make VSCode work pretty close to a regular KOS command line make
Attachments
kos_vscode_cfg_202306.zip
(2.29 KiB) Downloaded 30 times
These users thanked the author T_chan for the post (total 2):
GyroVorbisBB Hood
Post Reply