{"id":47,"date":"2014-03-25T17:04:07","date_gmt":"2014-03-25T17:04:07","guid":{"rendered":"https:\/\/blogs.scummvm.org\/josejx\/?p=47"},"modified":"2022-05-21T17:07:00","modified_gmt":"2022-05-21T17:07:00","slug":"detour-fixing-the-segfault-part-2","status":"publish","type":"post","link":"https:\/\/blogs.scummvm.org\/josejx\/2014\/03\/25\/detour-fixing-the-segfault-part-2\/","title":{"rendered":"Detour: Fixing the Segfault &#8211; Part 2"},"content":{"rendered":"<p><i>Continued from the <a href=\"https:\/\/blogs.scummvm.org\/josejx\/2014\/03\/23\/detour-fixing-a-segfault\/\">previous entry<\/a><\/i><\/p>\n<p>With the segfault resolved, the bug no longer crashes ResidualVM, but instead, the game gets stuck, preventing the player from continuing. In the game log, we see some messages that might help us to determine the problem:<\/p>\n<figure id=\"attachment_49\" aria-describedby=\"caption-attachment-49\" style=\"width: 418px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/blogs.scummvm.org\/josejx\/wp-content\/uploads\/sites\/23\/2014\/03\/Error.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-49\" src=\"https:\/\/blogs.scummvm.org\/josejx\/wp-content\/uploads\/sites\/23\/2014\/03\/Error.png\" alt=\"\" width=\"418\" height=\"234\" srcset=\"https:\/\/blogs.scummvm.org\/josejx\/wp-content\/uploads\/sites\/23\/2014\/03\/Error.png 418w, https:\/\/blogs.scummvm.org\/josejx\/wp-content\/uploads\/sites\/23\/2014\/03\/Error-300x168.png 300w\" sizes=\"auto, (max-width: 418px) 100vw, 418px\" \/><\/a><figcaption id=\"caption-attachment-49\" class=\"wp-caption-text\">New Error in the Log, Exposed by Fixing the Segfault<\/figcaption><\/figure>\n<p>In this error, we see first that the Lua interpreter is printing <i>lua: (null)<\/i>. This indicates that a value was unexpectedly null. The <i>Active Stack<\/i> tells us where this error occurred, much like the backtrace in the previous entry, but for the Lua script. Finally, we see the warning that we added in the previous entry, telling us that there&#8217;s a registry key read, <i>SpewOnError<\/i>, which doesn&#8217;t cause the segfault anymore because of our fix. So that&#8217;s good!<\/p>\n<p>In the stack trace, we see that this failed on a call to a tag method named (helpfully) <i>function<\/i>, so let&#8217;s see if we can find that code. In the EMI Demo, the scripts of interest are located in the <i>MagDemo.lab <\/i>file. Following the same directions as before, <i>unlab<\/i> the file and <i>delua<\/i> the Lua scripts.<\/p>\n<p>Inside, <i>_options.lua<\/i>, there&#8217;s a comment with the original line number, 1503. Search for that and we find that the function called was <i>main_menu.return_to_game<\/i>, which kind of makes sense as to why things are getting fouled up. In the original demo on Windows, pressing <i>F1<\/i> does not bring up the main menu, but rather, does nothing, while pressing <i>ESC <\/i>skips the cutscene.<\/p>\n<p>It appears that the game is in a wrong state, but it would be helpful to have more information about the problem and more details as to what was run. Let&#8217;s enable debugging information in ResidualVM to see if there&#8217;s anything else that can help us track this down.<\/p>\n<p>In ResidualVM, there are debug flags that can be enabled from the command line, like this:<\/p>\n<ul>\n<li>.\/residualvm &#8211;debugflags=&lt;flag list separated by commas&gt;<\/li>\n<\/ul>\n<p>In addition, there&#8217;s an in game debug mode you can enter by pressing <i>CTRL-D<\/i>. This will bring up a console from which you can turn on debug flags. For both, individual classes of flags may be enabled instead of all flags if you&#8217;re working on a specific area of the engine.<\/p>\n<p>Let&#8217;s tackle the part of the bug where pressing <i>ESC<\/i> doesn&#8217;t skip the cutscene first. With all of the debugging messages on, we get these messages in the log when we press <i>ESC<\/i> during the opening cutscene:<\/p>\n<figure id=\"attachment_50\" aria-describedby=\"caption-attachment-50\" style=\"width: 255px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/blogs.scummvm.org\/josejx\/wp-content\/uploads\/sites\/23\/2014\/03\/ESCinCutscene.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-50\" src=\"https:\/\/blogs.scummvm.org\/josejx\/wp-content\/uploads\/sites\/23\/2014\/03\/ESCinCutscene.png\" alt=\"\" width=\"255\" height=\"228\" \/><\/a><figcaption id=\"caption-attachment-50\" class=\"wp-caption-text\">Debug Output of Pressing ESC in the Cutscene<\/figcaption><\/figure>\n<p>Following the path of execution, we see a call to GetControlState(). This function returns the state of the key being passed in. From common\/keyboard.h, we see that the keys its checking are:<\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>KEYCODE_LCTRL (306)<\/li>\n<li>KEYCODE_LALT (308)<\/li>\n<li>KEYCODE_BACKSPACE (8)<\/li>\n<li>KEYCODE_LCTRL (306)<\/li>\n<li>KEYCODE_LALT (308)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>From this sequence, it appears that the script being run is the <i>SampleButtonHandler (<\/i>in<i> _control.lua), <\/i>which includes the first three calls, then the <i>CommonButtonHandler (<\/i>in<i> _control.lua)<\/i> which does the second two.<\/p>\n<p>We then see that the script reported that the <i>Override<\/i> key was hit. This code is in the <i>CommonButtonHandler<\/i> which then calls the <i>call_override<\/i> function, which can be found in the <i>_system.lua<\/i> file. This function is supposed to stop the current script if the system override is active. Let&#8217;s check the value of this variable.<\/p>\n<p>Using the console, type:<\/p>\n<div>\n<ul>\n<li>lua_do if(system.override.is_active) then PrintDebug(&#8220;Active&#8221;) else PrintDebug(&#8220;Inactive&#8221;) end<\/li>\n<\/ul>\n<p>We find that the override is inactive after starting the game up again and checking the log. So this function call does nothing, control is handed back to the button handler and the movie continues. This reflects the behavior that we see when playing the game, the <i>ESC<\/i> key is ignored.<\/p>\n<p>Also, in the previous screenshot, we see a repeated series of functions:<\/p>\n<ul>\n<li>function: IsMoviePlaying()<\/li>\n<li>function: break_here()<\/li>\n<\/ul>\n<p>These function calls either come from a function in the <i>_system.lua<\/i> script called <i>wait_for_movie<\/i> which does these checks repeatedly until the movie is finished, or from the <i>StartMovie<\/i> function which contains similar logic.<\/p>\n<p>In the actual game, in <i>_cut_scenes.lua<\/i>, there&#8217;s a call to <i>EscapeMovie<\/i> when the override key is pressed during the playback. In the demo, <i>RunFullscreenMovie<\/i> is much simpler, without this logic. In the Demo, the <i>BOOTTWO <\/i>function, which is part of the game scripts&#8217; startup sequence. In this function, there&#8217;s a call to a function called <i>StartMovie<\/i> this function begins playing the <i>intro<\/i> movie. So the demo doesn&#8217;t use <i>RunFullscreenMovie<\/i> at all! We can confirm that there is a movie named <i>intro<\/i> by checking in the movies directory, so we are sure that this is the code that starts the demo and plays the movie.<\/p>\n<p>So, how did it work in the original interpreter in Windows and what can we do to fix it in ResidualVM? We&#8217;ll keep digging in the next blog post!<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Continued from the previous entry With the segfault resolved, the bug no longer crashes ResidualVM, but instead, the game gets stuck, preventing the player from continuing. In the game log, we see some messages that might help us to determine the problem: In this error, we see first that the Lua interpreter is printing lua: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-47","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blogs.scummvm.org\/josejx\/wp-json\/wp\/v2\/posts\/47","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.scummvm.org\/josejx\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.scummvm.org\/josejx\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.scummvm.org\/josejx\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.scummvm.org\/josejx\/wp-json\/wp\/v2\/comments?post=47"}],"version-history":[{"count":2,"href":"https:\/\/blogs.scummvm.org\/josejx\/wp-json\/wp\/v2\/posts\/47\/revisions"}],"predecessor-version":[{"id":51,"href":"https:\/\/blogs.scummvm.org\/josejx\/wp-json\/wp\/v2\/posts\/47\/revisions\/51"}],"wp:attachment":[{"href":"https:\/\/blogs.scummvm.org\/josejx\/wp-json\/wp\/v2\/media?parent=47"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.scummvm.org\/josejx\/wp-json\/wp\/v2\/categories?post=47"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.scummvm.org\/josejx\/wp-json\/wp\/v2\/tags?post=47"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}