<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 14 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
span.EmailStyle17
{mso-style-type:personal-compose;
font-family:"Calibri","sans-serif";
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;
font-family:"Calibri","sans-serif";}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal">I am working on assembly profiler for MIPS using EmulateInstruction.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I have set the ContextType to eContextPopRegisterOffStack in the function which emulates “<span style="font-family:"Courier New"">ld ra,40(sp)”<o:p></o:p></span></p>
<p class="MsoNormal">(i.e. load/restore ra from stack) instruction.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">0xffffffff802009a4 <bar>:<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">0xffffffff802009a4: 67bdffd0 daddiu sp,sp,-48<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">0xffffffff802009a8: ffbf0028 sd ra,40(sp) -----> store return address<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">0xffffffff802009ac: ffbe0020 sd s8,32(sp)<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">0xffffffff802009b0: 03a0f02d move s8,sp<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""><..end of prologue...><o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""><..some other instructions...><o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""><..start of epilogue...><o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">0xffffffff802009f4: 03c0e82d move sp,s8<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">0xffffffff802009f8: dfbf0028 ld ra,40(sp) ------> restore return address<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">0xffffffff802009fc: dfbe0020 ld s8,32(sp)<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">0xffffffff80200a00: 67bd0030 daddiu sp,sp,48<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">0xffffffff80200a04: 03e00008 jr ra<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">0xffffffff80200a08: 00000000 nop<o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">With this setting, the unwinder fails when we try to unwind from 0xffffffff802009fc.<o:p></o:p></p>
<p class="MsoNormal">The call sequence is main()->foo()->bar() however, backtrace displays only current frame.<o:p></o:p></p>
<p class="MsoNormal">----------------------------------------<o:p></o:p></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">(lldb) bt<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">* thread #1: tid = 0x0001, 0xffffffff802009fc a.out_64`bar(p=4) + 88 at a.c:30, stop reason = breakpoint 1.1<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> * frame #0: 0xffffffff802009fc a.out_64`bar(p=4) + 88 at a.c:30<o:p></o:p></span></p>
<p class="MsoNormal">---------------------------------------<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Below is a part of generated log:<o:p></o:p></p>
<p class="MsoNormal">--------------------------------------------------------------------------------------<o:p></o:p></p>
<p class="MsoNormal">th1/fr0 0xffffffff802009fc: CFA=sp+48 => fp=[CFA-16] ra= <same> pc=[CFA-8]
<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">th1/fr0 CFA is 0xffffffff800fff60: Register sp (29) contents are 0xffffffff800fff30, offset is 48<o:p></o:p></p>
<p class="MsoNormal">th1/fr0 initialized frame current pc is 0xffffffff802009fc cfa is 0xffffffff800fff60 using EmulateInstructionMIPS64 UnwindPlan<o:p></o:p></p>
<p class="MsoNormal">th1/fr0 requested caller's saved PC but this UnwindPlan uses a RA reg; getting ra (31) instead<o:p></o:p></p>
<p class="MsoNormal">th1/fr0 supplying caller's saved ra (31)'s location using EmulateInstructionMIPS64 UnwindPlan<o:p></o:p></p>
<p class="MsoNormal"><b>th1/fr0 could not supply caller's ra (31) location, IsSame<o:p></o:p></b></p>
<p class="MsoNormal">th1/fr1 could not get pc value<o:p></o:p></p>
<p class="MsoNormal">Frame 1 invalid RegisterContext for this frame, stopping stack walk<o:p></o:p></p>
<p class="MsoNormal">th1 Unwind of this thread is complete.<o:p></o:p></p>
<p class="MsoNormal">--------------------------------------------------------------------------------------<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Here we just need to tell the unwinder that the register has been restored and take this register from live register context.<o:p></o:p></p>
<p class="MsoNormal">Which ContextType to use in such case?<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">1. There exists similar ContextType - eContextRegisterLoad, but it is unimplemented (just breaks out and does nothing) in UnwindAssemblyInstEmulation::WriteRegister().<o:p></o:p></p>
<p class="MsoNormal"> So will it be a good option to implement eContextRegisterLoad and set the register location to itself using SetRegisterLocationToRegister (reg_num, reg_num, true)<o:p></o:p></p>
<p class="MsoNormal"> OR<o:p></o:p></p>
<p class="MsoNormal">2. Add new ContextType say eContextRegisterRestore to EmulateInstruction class and implement it in UnwindAssemblyInstEmulation::WriteRegister().<o:p></o:p></p>
<p class="MsoNormal"> <o:p></o:p></p>
<p class="MsoNormal">Also, consider location of pc if we are restoring the return address register.
<o:p></o:p></p>
<p class="MsoNormal">This is similar to how eContextPushRegisterOnStack handles return address register in UnwindAssemblyInstEmulation::WriteMemory().<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Please let me know if I am missing something.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">-Regards,<o:p></o:p></p>
<p class="MsoNormal">Bhushan<o:p></o:p></p>
</div>
</body>
</html>