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.
Post Reply
User avatar
T_chan
DCEmu Fast Newbie
DCEmu Fast Newbie
Posts: 21
Joined: Mon Aug 22, 2011 12:45 pm
Has liked: 5 times
Been liked: 8 times

KOS, Visual Studio Code & gdb

Post by T_chan » Wed Sep 09, 2020 3:55 pm

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 91 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 liked the author T_chan for the post (total 3):
Ian RobinsonSiZiOUSbbmario
spencer723
DCEmu Cool Newbie
DCEmu Cool Newbie
Posts: 13
Joined: Sun Jul 26, 2020 2:31 pm
Has liked: 3 times
Been liked: 6 times

Re: KOS, Visual Studio Code & gdb

Post by spencer723 » 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
These users liked the author spencer723 for the post (total 2):
mrneo240Ian Robinson
User avatar
Ian Robinson
DCEmu Fast Newbie
DCEmu Fast Newbie
Posts: 24
Joined: Mon Mar 11, 2019 7:12 am
Has liked: 42 times
Been liked: 5 times

Re: KOS, Visual Studio Code & gdb

Post by Ian Robinson » Thu Sep 10, 2020 7:03 pm

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 liked: 16 times
Been liked: 19 times

Re: KOS, Visual Studio Code & gdb

Post by mrneo240 » 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!
These users liked the author mrneo240 for the post:
Ian Robinson
User avatar
T_chan
DCEmu Fast Newbie
DCEmu Fast Newbie
Posts: 21
Joined: Mon Aug 22, 2011 12:45 pm
Has liked: 5 times
Been liked: 8 times

Re: KOS, Visual Studio Code & gdb

Post by T_chan » Sun Sep 20, 2020 10:14 am

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: 397
Joined: Fri Mar 05, 2004 2:22 pm
Location: France
Has liked: 21 times
Been liked: 11 times
Contact:

Re: KOS, Visual Studio Code & gdb

Post by SiZiOUS » Wed Nov 11, 2020 9:51 am

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? :)
User avatar
T_chan
DCEmu Fast Newbie
DCEmu Fast Newbie
Posts: 21
Joined: Mon Aug 22, 2011 12:45 pm
Has liked: 5 times
Been liked: 8 times

Re: KOS, Visual Studio Code & gdb

Post by T_chan » Thu Nov 12, 2020 2:32 am

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 liked the author T_chan for the post:
SiZiOUS
User avatar
bbmario
DCEmu Freak
DCEmu Freak
Posts: 77
Joined: Wed Feb 05, 2014 5:58 am
Has liked: 4 times
Been liked: 2 times

Re: KOS, Visual Studio Code & gdb

Post by bbmario » Mon Dec 28, 2020 1:15 pm

What a fantastic tutorial. This is amazing.
User avatar
ThePerfectK
Insane DCEmu
Insane DCEmu
Posts: 130
Joined: Thu Apr 27, 2006 10:15 am
Has liked: 21 times
Been liked: 20 times

Re: KOS, Visual Studio Code & gdb

Post by ThePerfectK » Thu Feb 11, 2021 5:56 pm

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 liked: 1 time
Been liked: 0

Re: KOS, Visual Studio Code & gdb

Post by Hoplias973 » 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
mrneo240
DCEmu Freak
DCEmu Freak
Posts: 86
Joined: Wed Mar 14, 2018 12:22 am
Has liked: 16 times
Been liked: 19 times

Re: KOS, Visual Studio Code & gdb

Post by mrneo240 » Sat Feb 27, 2021 6:38 pm

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 liked: 1 time
Been liked: 0

Re: KOS, Visual Studio Code & gdb

Post by Hoplias973 » Sat Feb 27, 2021 8:00 pm

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 liked: 16 times
Been liked: 19 times

Re: KOS, Visual Studio Code & gdb

Post by mrneo240 » Sat Feb 27, 2021 8:09 pm

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 liked the author mrneo240 for the post (total 2):
Hoplias973Ian Robinson
User avatar
ThePerfectK
Insane DCEmu
Insane DCEmu
Posts: 130
Joined: Thu Apr 27, 2006 10:15 am
Has liked: 21 times
Been liked: 20 times

Re: KOS, Visual Studio Code & gdb

Post by ThePerfectK » Tue Mar 09, 2021 6:13 pm

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 liked 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 liked: 3 times
Been liked: 6 times

Re: KOS, Visual Studio Code & gdb

Post by spencer723 » Fri May 14, 2021 8:49 pm

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 liked the author spencer723 for the post (total 2):
SiZiOUSmrneo240
Post Reply