Search code examples
v8lldb

lldb debug v8: how to get the v8 the type T value of handle<T> type


I'm debugging v8 using lldb.

How can I print the string inside of Handle<String> Source?

The debug process is as follows:

(lldb) r
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 2.1
    frame #0: 0x000000010100536b libv8.dylib`v8::internal::Compiler::GetSharedFunctionInfoForScript(isolate=0x0000000118008000, source=Handle<v8::internal::String> @ 0x00007ffeefbfd4a0, script_details=0x00007ffeefbfd730, origin_options=(flags_ = 0), extension=0x0000000000000000, cached_data=0x0000000000000000, compile_options=kNoCompileOptions, no_cache_reason=kNoCacheNoReason, natives=NOT_NATIVES_CODE) at compiler.cc:2806:27
   2803     MaybeHandle<SharedFunctionInfo> Compiler::GetSharedFunctionInfoForScript(
   2804     solate* isolate, Handle<String> source,
   2805     ScriptCompiler::NoCacheReason no_cache_reason, NativesFlag natives) {
-> 2806   ScriptCompileTimerScope compile_timer(isolate, no_cache_reason);
   2807 
   2808   if (compile_options == ScriptCompiler::kNoCompileOptions ||
   2809       compile_options == ScriptCompiler::kEagerCompile) {
Target 0: (d8) stopped.
(lldb) p source
(v8::internal::Handle<v8::internal::String>) $9 = {
  v8::internal::HandleBase = {
    location_ = 0x000000010f009a60
  }
}
(lldb) p *$9 
(v8::internal::String) $10 = {
  v8::internal::TorqueGeneratedString<v8::internal::String, v8::internal::Name> = {
    .....
  }
}

Solution

  • Take a look at tools/lldb_commands.py. In short: configure your LLDB to load that script:

    echo "command script import /path/to/v8/tools/lldb_commands.py" >> ~/.lldbinit
    

    and then use the convenience commands it provides, the most important one being job, a mnemonic for "JavaScript object". It needs the raw pointer value that you'll see as ptr_ = ... somewhere in the output of p *$9, but you don't need to retrieve it manually. Example:

    (lldb) job source->ptr_
    0x28c008109019: [String]: "console.log('hello world');"
    

    (Side note: tools/gdbinit tends to have a few more features than tools/lldbinit, because most folks on the team use GDB. We'd be happy to accept patches to improve LLDB support; relevant to the case at hand would be gdbinit's jh shortcut (allowing simply jh source) that currently has no equivalent in lldbinit.)