<html 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=Windows-1252">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:Georgia;
panose-1:2 4 5 2 5 4 5 2 3 3;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:#0563C1;
text-decoration:underline;}
code
{mso-style-priority:99;
font-family:"Courier New";}
span.EmailStyle18
{mso-style-type:personal-reply;
font-family:"Calibri",sans-serif;
color:windowtext;}
span.apple-converted-space
{mso-style-name:apple-converted-space;}
.MsoChpDefault
{mso-style-type:export-only;
font-size:10.0pt;}
@page WordSection1
{size:612.0pt 792.0pt;
margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
{page:WordSection1;}
--></style>
</head>
<body lang="en-CZ" link="#0563C1" vlink="#954F72" style="word-wrap:break-word;-webkit-nbsp-mode: space;line-break:after-white-space">
<div class="WordSection1">
<p class="MsoNormal"><span lang="EN-US" style="mso-fareast-language:EN-US">As it is probably not the best to print to System.err and also filling large method print into an Exception message is overkill.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="mso-fareast-language:EN-US">I would suggest to create a new Classfile.Option for debug output, where user can specify a Consumer<String> as handler for such situations.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="mso-fareast-language:EN-US">For verification purposes such handler is a direct method argument, which can be unified to use the new Option.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="mso-fareast-language:EN-US">BTW: turning unfinished CodeBuilder into a MethodModel to re-use ClassPrinter is a bit tricky, however I already have a prototype and it seems to be valuable feature<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="mso-fareast-language:EN-US">And one trick to debug stack map generation I almost forgot – look at the StackMapGenerator Javadoc:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="mso-fareast-language:EN-US"><a href="https://htmlpreview.github.io/?https://raw.githubusercontent.com/openjdk/jdk-sandbox/classfile-api-javadoc-branch/doc/classfile-api/javadoc/jdk/classfile/impl/StackMapGenerator.html">https://htmlpreview.github.io/?https://raw.githubusercontent.com/openjdk/jdk-sandbox/classfile-api-javadoc-branch/doc/classfile-api/javadoc/jdk/classfile/impl/StackMapGenerator.html</a><o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p><span style="font-size:10.5pt;font-family:"Georgia",serif;color:#474747">In case of an exception during the Generator loop there is just minimal information available in the exception message.<o:p></o:p></span></p>
<p style="caret-color: rgb(71, 71, 71);font-variant-caps: normal;orphans: auto;text-align:start;widows: auto;-webkit-text-size-adjust: auto;-webkit-text-stroke-width: 0px;word-spacing:0px">
<span style="font-size:10.5pt;font-family:"Georgia",serif;color:#474747">To determine root cause of the exception it is recommended to enable debug logging of the Generator in one of the two modes using following<span class="apple-converted-space"> </span></span><code><span style="font-size:10.5pt;color:#474747">java.lang.System</span></code><span class="apple-converted-space"><span style="font-size:10.5pt;font-family:"Georgia",serif;color:#474747"> </span></span><span style="font-size:10.5pt;font-family:"Georgia",serif;color:#474747">properties:<o:p></o:p></span></p>
<p class="MsoNormal"><code><span style="font-size:10.5pt;color:#474747">-Djdk.classfile.impl.StackMapGenerator.DEBUG=true</span></code><span style="font-size:10.5pt;font-family:"Georgia",serif;color:#474747"><o:p></o:p></span></p>
<p class="MsoNormal" style="margin-left:36.0pt"><span style="font-size:10.5pt;font-family:"Georgia",serif;color:#474747">Activates debug logging with basic information + generated stack map frames in case of success. It also re-runs with enabled full trace
logging in case of an error or exception.<o:p></o:p></span></p>
<p class="MsoNormal"><code><span style="font-size:10.5pt;color:#474747">-Djdk.classfile.impl.StackMapGenerator.TRACE=true</span></code><span style="font-size:10.5pt;font-family:"Georgia",serif;color:#474747"><o:p></o:p></span></p>
<p class="MsoNormal" style="margin-left:36.0pt"><span style="font-size:10.5pt;font-family:"Georgia",serif;color:#474747">Activates full detailed tracing of the generator process for all invocations.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<div style="border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0cm 0cm 0cm">
<p class="MsoNormal" style="mso-margin-top-alt:0cm;margin-right:0cm;margin-bottom:12.0pt;margin-left:36.0pt">
<b><span style="font-size:12.0pt;color:black">From: </span></b><span style="font-size:12.0pt;color:black">Brian Goetz <brian.goetz@oracle.com><br>
<b>Date: </b>Thursday, 21 July 2022 16:15<br>
<b>To: </b>Michael van Acken <michael.van.acken@gmail.com><br>
<b>Cc: </b>Adam Sotona <adam.sotona@oracle.com>, classfile-api-dev@openjdk.org <classfile-api-dev@openjdk.org><br>
<b>Subject: </b>Re: Classfile API proposal to integrate basic print functionality directly to ClassModel and MethodModel<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:36.0pt">I think the root of the problem here is that CodeBuilder combines the “build my method” with stackmap generation, and if the latter fails, nothing is produced. And when stack map generation fails, you’d like
to see a javap-like output so you can see what you did wrong. <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:36.0pt"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:36.0pt">You can suppress stack map generation with an Option, and then you’ll get a classfile out, but that is probably a little hard to discover. <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:36.0pt"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:36.0pt">This is a more general problem, not just for stack maps; there are other things that can cause code generation to fail (e.g., forward branch to a label that is never defined; invalid labels in exception tables;
etc.) The main vector we have for feeding back information is the exception message, but putting the entire javap output of the method body in the exception message might be too much (but might not be, since any exception from building a classfile will trigger
a round of debugging.)<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:36.0pt"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:36.0pt">Any thoughts on how you would like to see this information fed back?<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:36.0pt"><br>
<br>
<o:p></o:p></p>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<div>
<div>
<div>
<p class="MsoNormal" style="margin-left:36.0pt">Is there a way to print out a trace of parts fed to CodeBuilder instances?<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:36.0pt"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:36.0pt">Just this morning I had Classfile die on me because of a stack underflow,<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:36.0pt">and it was quite hard to find out which parts were missing from the Code<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:36.0pt">attribute. And that with a Code totalling just 5 instructions...<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:36.0pt"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:36.0pt">If there would have been bytes output, then I could have inspected the<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:36.0pt">situation with javap. But if I mess up and pass inconsistent data to<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:36.0pt">CodeBuilder, causing it to throw instead of producing a byte array, then<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:36.0pt">I have an observability gap.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:36.0pt"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:36.0pt">-- mva<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:36.0pt"><o:p> </o:p></p>
</div>
</div>
</div>
</blockquote>
</div>
<p class="MsoNormal" style="margin-left:36.0pt"><o:p> </o:p></p>
</div>
</body>
</html>