{"id":223,"date":"2013-01-03T18:54:33","date_gmt":"2013-01-03T18:54:33","guid":{"rendered":"http:\/\/blog.mozilla.org\/javascript\/?p=223"},"modified":"2013-01-03T18:54:33","modified_gmt":"2013-01-03T18:54:33","slug":"support-for-debugging-spidermonkey-with-gdb-now-landed","status":"publish","type":"post","link":"https:\/\/blog.mozilla.org\/javascript\/2013\/01\/03\/support-for-debugging-spidermonkey-with-gdb-now-landed\/","title":{"rendered":"Support for debugging SpiderMonkey with GDB now landed"},"content":{"rendered":"<p>The main source tree for Firefox, <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/mozilla-central\" title=\"Mozilla Central Mercurial repository\">Mozilla Central<\/a>, now includes some code that should make debugging SpiderMonkey with GDB on Linux much more pleasant and productive.<\/p>\n<p>GDB understands C++ types and values, but naturally it has no idea what the debuggee intends them to represent. As a result, GDB&#8217;s attempts to display those values can sometimes be worse than printing nothing at all. For example, here&#8217;s how a stock GDB displays a stack frame for a call to <code>js::baseops::SetPropertyHelper<\/code>:<\/p>\n<p><code style=\"display:block;background-color:#f0f0f0;line-height:130%;padding:10px;\">(gdb) frame<br \/>\n#0  js::baseops::SetPropertyHelper (cx=0xc648b0, obj={&lt;js::HandleBase&gt; = {}, ptr = 0x7fffffffc960}, receiver={&lt;js::HandleBase&gt; = {}, ptr = 0x7fffffffc960}, id={&lt;js::HandleBase&gt; = {}, ptr = 0x7fffffffc1e0}, defineHow=4, vp={&lt;js::MutableHandleBase&gt; = {&lt;js::MutableValueOperations&lt;JS::MutableHandle &gt;&gt; = {&lt;js::ValueOperations&lt;JS::MutableHandle &gt;&gt; = {}, }, }, ptr = 0x7fffffffc1f0}, strict=0) at \/home\/jimb\/moz\/dbg\/js\/src\/jsobj.cpp:3593<br \/>\n<\/code><\/p>\n<p>There exist people who can pick through that, but for me it&#8217;s just a pile of hexadecimal noise. And yet, if you persevere, what those arguments <em>represent<\/em> is quite simple: in this case, <code>obj<\/code> and <code>receiver<\/code> are both the JavaScript global object; <code>id<\/code> is the identifier <code>\"x\"<\/code>; and <code>vp<\/code> refers to the JavaScript string value <code>\"foo\"<\/code>. This <code>SetPropertyHelper<\/code> call is simply storing <code>\"foo\"<\/code> as the value of the global variable <code>x<\/code>. But it sure is hard to tell&mdash;and that&#8217;s an annoyance for SpiderMonkey developers.<\/p>\n<p>As of early December, Mozilla Central includes <a href=\"https:\/\/hg.mozilla.org\/mozilla-central\/file\/tip\/js\/src\/gdb\" title=\"Mozilla Central repository: js\/src\/gdb directory\">Python scripts for GDB<\/a> that define custom printers for SpiderMonkey types, so that when GDB comes across a SpiderMonkey type like <code>MutableValueHandle<\/code>, it can print it in a meaningful way. With these changes, GDB displays the stack frame shown above like this:<\/p>\n<p><code style=\"display:block;background-color:#f0f0f0;line-height:130%;padding:10px;\">(gdb) frame<br \/>\n#0  js::baseops::SetPropertyHelper (cx=0xc648b0, obj=(JSObject * const) 0x7ffff151f060 [object global] delegate, receiver=(JSObject * const) 0x7ffff151f060 [object global] delegate, id=$jsid(\"x\"), defineHow=4, vp=$jsval(\"foo\"), strict=0) at \/home\/jimb\/moz\/dbg\/js\/src\/jsobj.cpp:3593<br \/>\n<\/code><\/p>\n<p>Here it&#8217;s much easier to see what&#8217;s going on. Objects print with their class, like &#8220;global&#8221; or &#8220;Object&#8221;; strings print as strings; <code>jsval<\/code> values print as appropriate for their tags; and so on. (The line breaks could still be improved, but that&#8217;s GDB for you.)<\/p>\n<p>Naturally, the pretty-printers work with any command in GDB that displays values: <code>print<\/code>, <code>backtrace<\/code>, <code>display<\/code>, and so on. Each type requires custom Python code to decode it; at present we have pretty-printers for JSObject, JSString, jsval, the various <code>Rooted<\/code> and <code>Handle<\/code> types, and things derived from those. The list will grow.<\/p>\n<p>GDB picks up the SpiderMonkey support scripts automatically when you&#8217;re debugging the JavaScript shell, as directed by the <code>js-gdb.py<\/code> file that the build system places in the same directory as the <code>js<\/code> executable. We haven&#8217;t yet made the support scripts load automatically when debugging Firefox, as that&#8217;s a much larger audience of developers, and we&#8217;d like a chance to shake out bugs before foisting it on everyone.<\/p>\n<p>Some versions of GDB are patched to trust only auto-load files found in directories you&#8217;ve whitelisted; if this is the case for you, GDB will complain, and you&#8217;ll need to add a command like the following to your <code>~\/.gdbinit<\/code> file:<\/p>\n<p><code style=\"display:block;background-color:#f0f0f0;line-height:130%;padding:10px;\"># Tell GDB to trust auto-load files found under ~\/moz.<br \/>\nadd-auto-load-safe-path ~\/moz<br \/>\n<\/code><\/p>\n<p>If you need to see a value in its plain C++ form, with no pretty-printing applied, you can add the <code>\/r<\/code> format modifier to the <code>print<\/code> command:<\/p>\n<p><code style=\"display:block;background-color:#f0f0f0;line-height:130%;padding:10px;\">(gdb) print vp<br \/>\n$1 = $jsval(\"foo\")<br \/>\n(gdb) print\/r vp<br \/>\n$2 = {<br \/>\n&nbsp;&nbsp;&lt;js::MutableHandleBase&gt; = {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;js::MutableValueOperations&lt;JS::MutableHandle &gt;&gt; = {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;js::ValueOperations&lt;JS::MutableHandle &gt;&gt; = {}, }, },<br \/>\n&nbsp;&nbsp;members of JS::MutableHandle:<br \/>\n&nbsp;&nbsp;ptr = 0x7fffffffc1f0<br \/>\n}<br \/>\n<\/code><\/p>\n<p>If you run into troubles with a pretty-printer, please file a bug in <a href=\"https:\/\/bugzilla.mozilla.org\/enter_bug.cgi?product=Core&amp;component=JavaScript%20Engine\" title=\"Bugzilla: enter new JavaScript bug\">Bugzilla<\/a>, under the &#8220;Core&#8221; product, for the &#8220;JavaScript Engine&#8221; component. (The pretty-printers have their own unit and regression tests; we do want them to be reliable.) In the mean time, you can disable the pretty-printers with the <code>disable pretty-printer<\/code> command:<\/p>\n<p><code style=\"display:block;background-color:#f0f0f0;line-height:130%;padding:10px;\">(gdb) disable pretty-printer .* SpiderMonkey<br \/>\n12 printers disabled<br \/>\n128 of 140 printers enabled<br \/>\n<\/code><\/p>\n<p>For more details, see the GDB support directory&#8217;s <a href=\"https:\/\/hg.mozilla.org\/mozilla-central\/raw-file\/tip\/js\/src\/gdb\/README\" title=\"Mozilla Central repository: js\/src\/gdb\/README\">README file<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The main source tree for Firefox, Mozilla Central, now includes some code that should make debugging SpiderMonkey with GDB on Linux much more pleasant and productive. GDB understands C++ types and values, but naturally it has no idea what the &hellip; <a class=\"go\" href=\"https:\/\/blog.mozilla.org\/javascript\/2013\/01\/03\/support-for-debugging-spidermonkey-with-gdb-now-landed\/\">Continue reading<\/a><\/p>\n","protected":false},"author":543,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.mozilla.org\/javascript\/wp-json\/wp\/v2\/posts\/223"}],"collection":[{"href":"https:\/\/blog.mozilla.org\/javascript\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.mozilla.org\/javascript\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.mozilla.org\/javascript\/wp-json\/wp\/v2\/users\/543"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.mozilla.org\/javascript\/wp-json\/wp\/v2\/comments?post=223"}],"version-history":[{"count":0,"href":"https:\/\/blog.mozilla.org\/javascript\/wp-json\/wp\/v2\/posts\/223\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.mozilla.org\/javascript\/wp-json\/wp\/v2\/media?parent=223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.mozilla.org\/javascript\/wp-json\/wp\/v2\/categories?post=223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.mozilla.org\/javascript\/wp-json\/wp\/v2\/tags?post=223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}