Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
99007a7
ADFA-7297 SQLite debugger: rows, change counts, named parameters and …
claude Nov 26, 2026
e73b348
ADFA-7297 style: spotless reformat, no functional change
davidschachterADFA Nov 26, 2026
e450122
ADFA-7297 SQLite debugger: say which args keys may drop their prefix
davidschachterADFA Nov 26, 2026
ffb3f18
ADFA-7297 sqlitedbg_jni.c: drop a stale comment above bind_value
claude Nov 26, 2026
e648501
ADFA-7297 style: test lines ktlint can format, no functional change
davidschachterADFA Nov 26, 2026
a8fca8c
ADFA-7297 Merge ffb3f18420 into sqlite-debugger/ADFA-7297
davidschachterADFA Nov 26, 2026
f0c931f
ADFA-7323 SQLite debugger: name a misspelt named-args key in the error
davidschachterADFA Nov 26, 2026
d354698
ADFA-7297 Merge sqlite-debugger/ADFA-7296 at 0c9a19ea3 into sqlite-de…
davidschachterADFA Nov 26, 2026
b87338e
ADFA-7297 Merge sqlite-debugger/ADFA-7296 at 2d674ecfc into sqlite-de…
davidschachterADFA Nov 27, 2026
2aa2c37
Merge sqlite-debugger/ADFA-7296 (#2311 at 5d8acf62d) into ADFA-7297
davidschachterADFA Dec 1, 2026
2523485
Merge sqlite-debugger/ADFA-7296 (#2311 at dd7ee1953) into ADFA-7297
davidschachterADFA Dec 1, 2026
f0e8d51
Merge sqlite-debugger/ADFA-7296 (#2311 at b9a31297e, ADFA-7379) into …
davidschachterADFA Dec 1, 2026
8760dc0
ADFA-7379 get_events: every event carries execution_id
davidschachterADFA Dec 1, 2026
954de4a
ADFA-7379 get_events: the null cases in full; the JNI test names its …
davidschachterADFA Dec 2, 2026
6958e2f
Merge p21/ADFA-7296 (#2311 at 4ffc26dc0, ADFA-7379 review fixes) into…
davidschachterADFA Dec 2, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions subprojects/sqlite-debugger/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,21 @@ The layout of `sqlitedbg_event_record` lives in `sqlitedbg.h`. The JNI layer han

**Rows and changes (ADFA-7296, interface version 3).** Each record carries `rows` and `changes` (the header grew from 56 to 72 bytes; the earlier fields kept their offsets).
- A PROFILE event's `rows` counts the rows the statement returned. Its `changes` is the statement's own change count, as `sqlite3_changes64` reports it (not rows changed by triggers). A read-only statement has `changes` -1, and so has a statement that had not finished when its PROFILE fired (reset or finalized part way, such as an `INSERT ... VALUES (1),(2) RETURNING a` stepped once, or a step that returned `SQLITE_BUSY`; `sqlite3_stmt_busy` is true then), because the count is partial or not yet set. A statement that changed nothing has 0 (a CREATE TABLE, which `sqlite3_changes64` alone would report with the previous write's count). `changes` is -1 as well when the connection's total changed while the statement ran and the statement is not an INSERT, UPDATE, DELETE or REPLACE (a leading WITH counts; decided from its own SQL, after whitespace and comments): a PRAGMA stepped across another statement's INSERT would otherwise get that INSERT's count.
- `SQLITEDBG_EVENT_ROW` records one row's values: the first N rows of each statement, while a client is connected and `sqlitedbg_set_row_capture` is on. Recording is off by default, and nothing turns it on yet; the server will (ADFA-7297).
- `SQLITEDBG_EVENT_ROW` records one row's values: the first N rows of each statement, while a client is connected and `sqlitedbg_set_row_capture` is on. Recording is off by default; `get_events` turns it on (see "Results and bound parameters").
- Each value is read with the accessor of its own type, after `sqlite3_column_type`. `sqlite3_column_text` on every column would convert values in place: on a BLOB, the app's own `sqlite3_column_type` then returns TEXT. TEXT and BLOB values are cut at a per-value limit, and a row's data at a per-row limit (at most 64 KiB, `SQLITEDBG_MAX_ROW_BYTES`, so a row record always fits the 256 KiB the server reads at a time); both are marked. A TEXT or BLOB larger than 1 MiB that does not fit the limits is not read at all (size recorded, no bytes, marked cut): `sqlite3_column_blob` would otherwise materialize it on the app's own statement (a zeroblob is virtual until read), costing its size in memory and risking `SQLITE_NOMEM` there. The size comes from `sqlite3_column_bytes` first. Accepted limit: in a UTF-16 database, recording rows converts each TEXT value to UTF-8 in full (`sqlite3_column_bytes`) before the 1 MiB guard applies, so a large value costs its full size in memory and time on the app's thread, for each row recorded while a client is connected (measured with a 25-million-character TEXT: a 100 MB allocation and about 230 ms for one row, against about 54 ms with recording off). Android's own databases and Room use UTF-8, which has no such copy. Decided not to change (David).
- The extension follows up to 16 statements per connection (`SQLITEDBG_STATEMENT_SLOTS`) from their start to their PROFILE event. Rows of a statement it did not see start are not recorded, which drops the rows of SQLite's own nested statements (a CREATE TABLE's schema read returns one; those reads are traced as ROW only and are ignored). Limit: a connection that steps more than 16 statements in turn loses the oldest, whose remaining rows go unrecorded and whose PROFILE event has `rows` and `changes` -1. A step that returns `SQLITE_BUSY` before any row fires a PROFILE event (rows 0, changes -1) and SQLite fires no other for that execution when it resumes; the slot is kept, so the rows that follow are still numbered and recorded, but they have no PROFILE event of their own. A row that cannot be recorded (the row limit is below a row header, or an allocation failed) is not counted as recorded and sets `ROWS_CUT`.
- Rows are counted on every statement, client or not: `SQLITE_TRACE_ROW` is part of the trace mask from the connection's open. Adding it when a client connects would take a `sqlite3_trace_v2` call on each app connection, which the server's thread may not make while the app uses the connection (`SQLITE_THREADSAFE=2`). The cost per row is one callback and a lookup in 16 slots. On a Galaxy J7 (Android 9, armeabi-v7a), with no client connected, a SELECT returning 20 rows took about 1.1 microseconds longer than before ADFA-7296 (71.6 against 70.5), and a one-row SELECT 0.6 longer (37.5 against 36.9); recording rows, once on, costs about 2.9 microseconds per row (native benchmark, PR #2311).

### Results and bound parameters (ADFA-7297)

What the server gives a client so it can format results as the sqlite3 shell would (`.mode`, `.nullvalue` and the rest stay client-side):
- **Values.** A NULL is JSON `null`. A BLOB is `{hex, bytes, truncated}`, lowercase hex (base64 before). An integer beyond 2^53 is a decimal string, and a NaN or infinite REAL is `"NaN"`, `"Infinity"` or `"-Infinity"`, as before.
- **Storage classes.** JSON alone cannot tell a REAL 2.0 from an INTEGER 2 (org.json writes both as `2`), or a big integer from TEXT, so `query` returns `storage_classes`, one string per row with a letter per column: `i` INTEGER, `r` REAL, `t` TEXT, `b` BLOB, `n` NULL. Row events carry the same string.
- **Declared types.** `query` returns `column_types`, each column's `sqlite3_column_decltype`, `null` for an expression.
- **Bound parameters (`.parameter`).** `query` and `explain_query_plan` take `args` as an array, bound by position (`?`, and `?NNN` by its number), or as an object bound by name (`:name`, `@name`, `$name`, `?NNN`). A key is written as in the SQL or bare: `id` matches `:id`, `@id` and `$id`, so SQL holding two of those gets the same value for both. Every parameter needs a value and every key must name a parameter, so a misspelt name fails rather than binding NULL, and the error names the key even when a parameter is also left without a value; an anonymous `?`, or a gap in the `?NNN` numbers, has no name and needs the array form. Names reach the JNI layer as UTF-8 and are matched there (`bind_named`), against `sqlite3_bind_parameter_name`.
- **Rows and changes in `get_events`.** A `profile` event carries `rows` (the rows the statement returned), `changes` (the rows it changed itself; `null` for a read) and `rows_cut`. Every event has `execution_id` (ADFA-7379): a `statement` event's own `seq`, and on that run's `profile` and `row` events the same number, so rows of two statements stepped in turn are told apart; it is `null` for `close`, a trigger's statements, a statement an app function runs inside a callback, an EXPLAIN's `profile`, a statement whose start was not seen, and the later `profile` and `row` events of a statement pushed out of its slot. One run can have more than one `profile`, such as one with `rows` 0 from a step that met a busy lock, before the run's rows (the event format above lists every case). A `row` event carries `row_number`, `column_count`, `values`, `storage_classes` and `cut_values`. `get_events` turns row recording on (`DebugEngine.setRowCapture`): the first 10 rows of each statement, TEXT and BLOB values cut at 256 bytes, a row's data at 4 KiB (`DatabaseTools.EVENT_*`). It costs the app about 2.9 microseconds a row on a Galaxy J7 (above), so `include_rows: false` turns it off and leaves row events out, and it goes off with bound-value recording, 60 s after the last `get_events`, and when the server stops. Only rows the app's statements return after the call are recorded.
- **Limits.** `execute` (ADFA-7298, a separate PR) binds positional `args` only until the two PRs meet. Row recording is one switch for the process: the last `get_events` call decides it.

## Decisions that shape it

- **Debug builds only.** `DebuggerStartup.onCreate` throws if the process is not debuggable, so a release build that includes the library by mistake crashes at once instead of opening a local server.
Expand All @@ -63,7 +73,7 @@ The layout of `sqlitedbg_event_record` lives in `sqlitedbg.h`. The JNI layer han

- **Host C tests of the extension:** `subprojects/sqlite-debugger/test.sh [--update-fixture] [AMALGAMATION_TARBALL]`. It builds SQLite 3.49.1 from the amalgamation as requery builds it (`SQLITE_THREADSAFE=2`), checks its headers against `sqlite-js/upstream/libs`, and runs `src/test/c/sqlitedbg_test.c` under AddressSanitizer with UBSan, then under ThreadSanitizer. It needs gcc, curl, tar and sha256sum, and downloads the tarball unless you pass one. It runs on Linux only: the script uses GNU `sed -i` without a suffix, `sha256sum` and `readlink -f`, and the JNI build includes `include/linux` and builds `.so` files. CI runs it on `ubuntu-latest`.
- **The fixture:** `src/test/resources/.../engine/events.bin` and `events.tsv` are written by the C test and read by `EventDecoderTest`. Without `--update-fixture`, `test.sh` checks the committed pair against the current record layout, so a layout change fails until you regenerate it and update `EventDecoder.kt`.
- **JNI test:** when a JDK is on PATH, `test.sh` also builds `libsqlitedbg.so` as it ships (no `SQLITE_CORE`) against a requery-like `libsqlite3x.so` and runs `src/test/jni/JniQueryTest.java` through `NativeBridge`: paging, the total-bytes budget, per-cell truncation, bound types, one statement per call, the deadline, refused writes and transactions, and the rollback of a transaction left open. It uses Java stubs of `NativeBridge` and `DebugEngineException` instead of the Kotlin, so a JDK is all it needs; `test.sh` checks the native names match `NativeBridge.kt`. Otherwise it is skipped with a message. CI runs `test.sh` in `.github/workflows/test-sqlite-debugger.yml` for changes under this module.
- **JNI test:** when a JDK is on PATH, `test.sh` also builds `libsqlitedbg.so` as it ships (no `SQLITE_CORE`) against a requery-like `libsqlite3x.so` and runs `src/test/jni/JniQueryTest.java` through `NativeBridge`: paging, the total-bytes budget, per-cell truncation, bound types, binding by name, declared types, the row-recording switch, one statement per call, the deadline, refused writes and transactions, and the rollback of a transaction left open. It uses Java stubs of `NativeBridge` and `DebugEngineException` instead of the Kotlin, so a JDK is all it needs; `test.sh` checks the native names match `NativeBridge.kt`. Otherwise it is skipped with a message. CI runs `test.sh` in `.github/workflows/test-sqlite-debugger.yml` for changes under this module.
- **JVM tests:** `flox activate -d flox/local -- ./gradlew :subprojects:sqlite-debugger:testDebugUnitTest`. They cover `EventDecoder`, `McpProtocol`, `McpHttp`, `DatabaseTools` (with `FakeDebugEngine`) and `EndpointBroadcast`, its announce receiver included.

`.github/workflows/test-sqlite-debugger.yml` runs `test.sh` (the C and JNI tests) on pull requests that touch the module or the SQLite headers. The JVM tests run in `.github/workflows/debug.yml`, in the step "Run sqlite-debugger unit tests" beside the `:plugin-api` one.
Expand Down
Loading
Loading