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: 7 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 16 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 2):
Ian RobinsonSiZiOUS
spencer723
DCEmu Cool Newbie
DCEmu Cool Newbie
Posts: 12
Joined: Sun Jul 26, 2020 2:31 pm
Has liked: 2 times
Been liked: 4 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: 23
Joined: Mon Mar 11, 2019 7:12 am
Has liked: 38 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: 83
Joined: Wed Mar 14, 2018 12:22 am
Has liked: 13 times
Been liked: 16 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: 7 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: 396
Joined: Fri Mar 05, 2004 2:22 pm
Location: France
Has liked: 20 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: 7 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
Post Reply