SQLite Forum

Debugging opcodes in vdbe.c
I'd like to develop a process for examining what functions call and
are called by a particular OpCode. To do this I need a reliable way of
adding debug statements, and I have found some surprises in that.

The example I picked is the opcode IsSmaller.

From code inspection using grep and less, IsSmaller appears to only be
triggered by "PRAGMA optimize", which in turn is somewhat exercised by
the script test/busy.test. I used various debugging techniques to
learn about how IsSmaller is used:

1. I compiled with and without -DSQLITE_DEBUG
2. I added some printfs to the implementation of IsSmaller in vdbe.c
3. I made these printfs inside and outside #ifdef SQLITE_DEBUG
4. I added PRAGMA vdbe_trace=ON/OFF at the beginning and end of busy.test
5. I tried turning on debug mode for PRAGMA optimize with mask=0x0001
6. Instead of printfs in vdbe.c, I appended debugging statements to a file
7. I tried running the sqlite commandline as well as make test to
trigger my debug statements inside IsSmaller

Note: When editing vdbe.c, it is important to strictly respect
formatting because the whole C file including comments is parsed to generate
code before the C compiler sees it.

Fundamental question: why does a printf statement inside an opcode seem to
only sometimes work? Appending to a file always works, which is how I
can compare. It is possible there are so many moving parts that I just got confused.

A subquestion: does the make test infrastructure capture and process stdout before resending it to stdout?

Opening and appending to a file does work reliably, which is how I
know there are exactly 8 invocations of IsSmaller caused by operations in make test. Sometimes the printfs agree with this, and sometimes they don't appear
at all. At least once only a few of the printfs are displayed.

I thought maybe vdbe_trace was the answer, but fifty thousand debug
lines later, I began to see that ad-hoc printf's have some advantages.
On the hand, when using vdbe_trace, printfs always seem to work,
whether inside make test or not.

(As expected, adding \r in front of the \n at the end of printf
statements makes no difference but I did try.)

I don't think I'm the first to have noticed strange behaviour of this kind. I'd like to work out what it is and document it.