Skip to content

Instantly share code, notes, and snippets.

@dpnishant
Last active June 3, 2023 07:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpnishant/c7c6b47ebfd8cd671ecf to your computer and use it in GitHub Desktop.
Save dpnishant/c7c6b47ebfd8cd671ecf to your computer and use it in GitHub Desktop.
'use strict';
var func_sqlite3_prepare_v2 = Module.findExportByName('libsqlite3.dylib', 'sqlite3_prepare_v2');
var func_sqlite3_sql = Module.findExportByName('libsqlite3.dylib', 'sqlite3_sql');
var sqlite3_sql = new NativeFunction(func_sqlite3_sql, 'char', ['pointer']);
Interceptor.attach(func_sqlite3_prepare_v2, {
onEnter: function(args) {
var sqlite3_stmt = args[3];
var output = sqlite3_sql(sqlite3_stmt);
console.log('SQL: ' + output);
},
onLeave: function (retval) {
//console.log('Return Value: ' + Memory.readUtf8String(retval));
}
});
@dpnishant
Copy link
Author

dpnishant commented Mar 18, 2016

Signature of sqlite3_prepare_v2() // Doc: https://www.sqlite.org/c3ref/prepare.html

  int sqlite3_prepare_v2(
    sqlite3 *db,            // Database handle 
    const char *zSql,       // SQL statement, UTF-8 encoded 
    int nByte,              // Maximum length of zSql in bytes.
    sqlite3_stmt **ppStmt,  // OUT: Statement handle
    const char **pzTail     // OUT: Pointer to unused portion of zSql
  );

Signature of sqlite3_sql() // Doc: http://www.sqlite.org/c3ref/sql.html
const char *sqlite3_sql(sqlite3_stmt *pStmt);

Question: I want to call the sqlite3_sql() method, from the loaded libsqlite3.dylib, by passing the 4th argument of sqlite3_prepare_v2().
But I'm getting the following error:

TypeError: NativeFunction: bad argument count
    at TypeError (native)
    at [object Object].Interceptor.attach.onEnter (repl1.js:10:18)

@riverar
Copy link

riverar commented Mar 19, 2016

Line 9 should be:
new NativeFunction(func_sqlite3_sql, 'char', ['pointer'])

@dpnishant
Copy link
Author

Revised the script. Now works as expected. Thanks @riverar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment