{"id":150,"date":"2015-10-01T12:01:00","date_gmt":"2015-10-01T12:01:00","guid":{"rendered":"http:\/\/simon.aldrich.eu\/blog\/?p=150"},"modified":"2015-10-01T12:01:36","modified_gmt":"2015-10-01T12:01:36","slug":"gdb-batch-mode-pid-attach","status":"publish","type":"post","link":"https:\/\/simon.aldrich.cc\/blog\/2015\/10\/gdb-batch-mode-pid-attach\/","title":{"rendered":"GDB attach to a running process in batch mode"},"content":{"rendered":"<p>Ever needed to attach to a running process in a gdb batch mode script but didn&#8217;t know the process&#8217; PID in advance? You can use GDB&#8217;s built-in Python interpreter to do the heavy lifting for you. Put the following in your batch-mode script:<code><\/code><code><br \/>\npython gdb.execute(\"attach \" + os.popen('pgrep &lt;process name&gt;').read().rstrip())<br \/>\n&lt;gdb commands go here&gt;<br \/>\ndetach<br \/>\nquit<br \/>\n<\/code><br \/>\nThen run as follows:<br \/>\n<code>gdb --batch -x &lt;script&gt;<\/code><\/p>\n<p>Magic!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ever needed to attach to a running process in a gdb batch mode script but didn&#8217;t know the process&#8217; PID in advance? You can use GDB&#8217;s built-in Python interpreter to do the heavy lifting for you. Put the following in your batch-mode script: python gdb.execute(&#8220;attach &#8221; + os.popen(&#8216;pgrep &lt;process name&gt;&#8217;).read().rstrip()) &lt;gdb commands go here&gt; detach [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[7],"tags":[44,43,42,45,46],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2Rw4E-2q","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/simon.aldrich.cc\/blog\/wp-json\/wp\/v2\/posts\/150"}],"collection":[{"href":"https:\/\/simon.aldrich.cc\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/simon.aldrich.cc\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/simon.aldrich.cc\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/simon.aldrich.cc\/blog\/wp-json\/wp\/v2\/comments?post=150"}],"version-history":[{"count":2,"href":"https:\/\/simon.aldrich.cc\/blog\/wp-json\/wp\/v2\/posts\/150\/revisions"}],"predecessor-version":[{"id":152,"href":"https:\/\/simon.aldrich.cc\/blog\/wp-json\/wp\/v2\/posts\/150\/revisions\/152"}],"wp:attachment":[{"href":"https:\/\/simon.aldrich.cc\/blog\/wp-json\/wp\/v2\/media?parent=150"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/simon.aldrich.cc\/blog\/wp-json\/wp\/v2\/categories?post=150"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/simon.aldrich.cc\/blog\/wp-json\/wp\/v2\/tags?post=150"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}