Agnius Vasiliauskas
Agnius Vasiliauskas

Reputation: 11277

Programmatically calling debugger in GCC

Is it possible to programmatically break into debugger from GCC?

For example I want something like: #define STOP_EXECUTION_HERE ??? which when put on some code line will force debugger stop there. Is it possible at all ? I found some solution, but i can't use it because on my embedded ARM system I don't have signal.h.

(However I can use inline assembly).

Upvotes: 0

Views: 949

Answers (3)

Roman Saveljev
Roman Saveljev

Reputation: 2594

What you are trying to do is called software breakpoint

It is very hard to say precisely without knowing how you actually debug. I assume your embedded system runs gdbstub. There are various possibilities how this can be supported:

Use dedicated BKPT instruction

This could be a standard way on your system and debugger to support software breakpoints

Feed invalid instruction to CPU

gdbstub could have placed own UNDEF ARM mode handler placed. If you go this route you must be aware of current CPU mode (ARM or THUMB), because instruction size will be different. Examples of undefined instructions:

ARM: 0xE7F123F4
THUMB: 0xDE56

In runtime CPU mode could be found from the lowest bit of PC register. But the easier way is to know how you compiled object file, where you placed software breakpoint

Use SWI instruction

We did so when used RealView ICE. Most likely this does not apply to you, if you run some OS on your embedded system. SWI is usually used by OS to implement system calls

Upvotes: 3

pmdj
pmdj

Reputation: 23438

Normally, the best way to do this is via a library function supplied with your device's toolchain.

I can't test it out, but a general solution might be to insert an ARM BKPT instruction. It takes an immediate argument, which your debugger might interpret, with potentially odd results.

Upvotes: 2

Some programmer dude
Some programmer dude

Reputation: 409196

You could run your application from GDB, and in the code call e.g. abort. This will stop your application at that point. I'm not sure if it's possible to continue after that though.

Upvotes: 0

Related Questions