<div dir="ltr"><div>Hi Roland,</div><div>so I have managed to compile simpletool and recreate a similar instrumentation tool also including the LLVMTracerInstrument and at this point I&#39;m not sure on how to proceed. I had these 2 ideas:</div><div><br></div><div><ol><li>inside my onCreate method reuse the code inside the LLVMTracerInstrument and attach a different EventFactory</li><li>create a LLVmTracerInstrument object inside my tool and somehow just override the methods I need ( not sure if it is feasible )</li></ol><div>Honestly the first solution seems to be better but I cannot get it to work. My onCreate method is:</div><div><br></div><div>protected void onCreate(final Env env) {<br><br>        final SourceSectionFilter.Builder builder = SourceSectionFilter.newBuilder();<br>        builder.mimeTypeIs(&quot;text/x-llvmir&quot;);<br>        builder.tagIs(StandardTags.StatementTag.class, StandardTags.RootTag.class);<br>        final SourceSectionFilter filter = builder.build();<br><br>        final Instrumenter instrumenter = env.getInstrumenter();<br>        String optionString;<br><br>        targetStream = new TargetStream(env, optionString);<br>    }</div><div><br></div><div>Unfortunately env is of the wrong type and I don&#39;t know how to fix.</div><div><br></div><div>Some guidance or example could would really help me if possible.</div><div><br></div><div>Thanks,</div><div>Alberto<br></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Il giorno mer 12 feb 2020 alle ore 16:26 Alberto Barbaro &lt;<a href="mailto:barbaro.alberto@gmail.com">barbaro.alberto@gmail.com</a>&gt; ha scritto:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="auto">Hi Roland,<div dir="auto">Yes you are right I&#39;m using 19.3.1. I&#39;ll upgrade when possible and try it.</div><div dir="auto"><br></div><div dir="auto">Atm I was able to create a maven project and create my own instrument tool. I&#39;m just missing to understand why the ID I&#39;m using as the new option is not available but I&#39;ll figure it out. I hope :)</div><div dir="auto"><br></div><div dir="auto">Thanks</div><div dir="auto">Alberto</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Feb 12, 2020, 15:25 Roland Schatz &lt;<a href="mailto:roland.schatz@oracle.com" target="_blank">roland.schatz@oracle.com</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
  
    
  
  <div>
    <div>Hi Alberto,<br>
      <br>
    </div>
    <div><br>
    </div>
    <div>On 2/12/20 2:52 PM, Alberto Barbaro
      wrote:<br>
    </div>
    <blockquote type="cite">
      
      <div dir="ltr">Hi Roland,
        <div>I think that&#39;s exactly what I need. I was able to build
          simpletool and to use Truffle so I&#39;ll adapt it to my need
          using LLVMTruffleInstrument instead TruffleInsturment. Does it
          sound correct to you?</div>
      </div>
    </blockquote>
    <p><br>
      I think either way is fine. It&#39;s probably a good idea to do your
      own instrument, but you can start from the LLVMTracerInstrument,
      because it already does something very similar to what you want to
      do.<br>
    </p>
    <br>
    <blockquote type="cite">
      <div dir="ltr">
        <div><br>
        </div>
        <div>In addition I tried lli with the experimental options but I
          had the following errors:</div>
        <div><br>
        </div>
        <div>[...]<br>
          ERROR: java.lang.IllegalStateException: The registerService
          method can only be called during the execution of the
          Env.createContext method.<br>
          org.graalvm.polyglot.PolyglotException:
          java.lang.IllegalStateException: The registerService method
          can only be called during the execution of the
          Env.createContext method.<br>
          at
com.oracle.truffle.api.TruffleLanguage$Env.registerService(TruffleLanguage.java:2382)<br>
          at
com.oracle.truffle.llvm.instruments.trace.LLVMTracerInstrument.initialize(LLVMTracerInstrument.java:58)<br>
          at
com.oracle.truffle.llvm.runtime.LLVMContext.initialize(LLVMContext.java:276)<br>
          [...]<br>
        </div>
      </div>
    </blockquote>
    <p><br>
      From the stack trace, looks like you&#39;re on version 19.3, right?
      Then that is a known bug, it&#39;s already fixed on current master and
      on the 20.0 release branch. You can use one of our dev builds
      (<a href="https://urldefense.com/v3/__https://github.com/graalvm/graalvm-ce-dev-builds/releases__;!!GqivPVa7Brio!IFhTAYbgfA1Mga5iZfdylDyv3Uax2Zepu60xZZQG694ibYu5PWeRKOzrErWnMCz2MzThIw$" rel="noreferrer" target="_blank">https://github.com/graalvm/graalvm-ce-dev-builds/releases</a>) in the
      meantime. It will also be fixed in the upcoming 20.0 release.<br>
      <br>
      <br>
      - Roland<br>
      <br>
      <br>
    </p>
    <blockquote type="cite">
      <div dir="ltr">
        <div><br>
        </div>
      </div>
      <div class="gmail_quote">
        <div dir="ltr" class="gmail_attr">Il giorno mer 12 feb 2020 alle
          ore 11:24 Roland Schatz &lt;<a href="mailto:roland.schatz@oracle.com" rel="noreferrer" target="_blank">roland.schatz@oracle.com</a>&gt; ha
          scritto:<br>
        </div>
        <blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
          <div>
            <div>Hi Alberto,<br>
            </div>
            <div><br>
            </div>
            <div>On 2/11/20 6:40 PM, Alberto Barbaro wrote:<br>
            </div>
            <blockquote type="cite">
              <div dir="auto">Hi Roland,
                <div dir="auto">Yes you are right, that&#39;s the proper
                  class name.</div>
                <div dir="auto"><br>
                </div>
                <div dir="auto">Atm the goal for me is just to print all
                  the instructions that are executed In the LLVM IR firm
                  if possible.</div>
              </div>
            </blockquote>
            <br>
            The LLVMBitcodeInstructionVisitor is used just for parsing
            the bitcode. For runtime instrumentation, there is the
            Truffle instrumentation framework that might be better
            suited for what you&#39;re trying to do:<br>
            <a href="https://urldefense.com/v3/__https://www.graalvm.org/docs/graalvm-as-a-platform/implement-instrument/__;!!GqivPVa7Brio!M_VQB1VYM2oG0tUtVXzAs5zr9rDHleDIiNnN14WgTynkSubDpWG5C3J0oIZzg4DLWps$" rel="noreferrer" target="_blank">https://www.graalvm.org/docs/graalvm-as-a-platform/implement-instrument/</a><br>
            <br>
            We already have a few instruments that you might find
            useful, either for using directly, or for building on top of
            them.<br>
            <br>
            You can already get a trace of all executed bitcode
            instructions:<br>
            <blockquote type="cite">$ llvm-dis hello.bc<br>
              $ lli --experimental-options --llvm.llDebug --llvm.traceIR
              hello.bc<br>
              [lli] &gt;&gt; Entering function @main at hello.ll:9:1
              with arguments:[StackPointer 0x7f62b3fff010 (Bounds:
              0x7f62aefff010 - 0x7f62b3fff010), 1, 0x55ad10a8ba48,
              0x55ad10a8ba58]<br>
              [lli] &gt;&gt; hello.ll:10:1 -&gt;   %1 = alloca i32,
              align 4<br>
              [lli] &gt;&gt; hello.ll:11:1 -&gt;   store i32 0, i32* %1,
              align 4<br>
              [lli] &gt;&gt; hello.ll:12:1 -&gt;   %2 = call i32 (i8*,
              ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x
              i8]* @.str, i32 0, i32 0)), !dbg !13<br>
              Hello, World!<br>
              [lli] &gt;&gt; hello.ll:13:1 -&gt;   ret i32 0, !dbg !14<br>
              [lli] &gt;&gt; Leaving @main<br>
            </blockquote>
            <br>
            The code for that is mainly in the classes
            LLVMTracerInstrument and LLVMTraceNodeFactory. That might be
            an easier entry point for what you&#39;re trying to do.<br>
            <br>
            <br>
            <br>
            We also have a code coverage tool:<br>
            <a href="https://urldefense.com/v3/__https://www.graalvm.org/docs/reference-manual/tools/*code-coverage__;Iw!!GqivPVa7Brio!M_VQB1VYM2oG0tUtVXzAs5zr9rDHleDIiNnN14WgTynkSubDpWG5C3J0oIZzo_rD3k0$" rel="noreferrer" target="_blank">https://www.graalvm.org/docs/reference-manual/tools/#code-coverage</a><br>
            <a href="https://urldefense.com/v3/__https://github.com/oracle/graal/tree/master/tools/src/com.oracle.truffle.tools.coverage/src/com/oracle/truffle/tools/coverage__;!!GqivPVa7Brio!M_VQB1VYM2oG0tUtVXzAs5zr9rDHleDIiNnN14WgTynkSubDpWG5C3J0oIZzHgM1A5U$" rel="noreferrer" target="_blank">https://github.com/oracle/graal/tree/master/tools/src/com.oracle.truffle.tools.coverage/src/com/oracle/truffle/tools/coverage</a><br>
            <br>
            If you run it with regular &quot;lli --coverage ...&quot;, you&#39;ll get
            C code covarage, but you can combine that with the
            &quot;--llvm.llDebug&quot; option to get bitcode coverage, too (see
            below for example output).<br>
            <br>
            <br>
            I hope this helps,<br>
            Roland<br>
            <br>
            <br>
            <br>
            <blockquote type="cite">$ lli --coverage
              --coverage.Output=detailed --experimental-options
              --llvm.llDebug hello.bc<br>
              Hello, World!<br>
------------------------------------------------------------------------------------------<br>
              Code coverage per line of code and what percent of each
              element was covered during execution (per source)<br>
                + indicates the line is covered during execution<br>
                - indicates the line is not covered during execution<br>
                p indicates the line is part of a statement that was
              incidentally covered during execution<br>
                  e.g. a not-taken branch of a covered if statement<br>
------------------------------------------------------------------------------------------<br>
               Path                                                 | 
              Statements |    Lines |    Roots<br>
               /home/roland/test/hello/hello.ll                    
              |     100.00% |  100.00% |  100.00%<br>
              <br>
                ; ModuleID = &#39;hello.bc&#39;<br>
                source_filename = &quot;hello.c&quot;<br>
                target datalayout =
              &quot;e-m:e-i64:64-f80:128-n8:16:32:64-S128&quot;<br>
                target triple = &quot;x86_64-pc-linux-gnu&quot;<br>
              <br>
                @.str = private unnamed_addr constant [15 x i8] c&quot;Hello,
              World!\0A\00&quot;, align 1<br>
              <br>
                ; Function Attrs: noinline nounwind optnone sspstrong
              uwtable<br>
                define dso_local i32 @main() #0 !dbg !9 {<br>
              +   %1 = alloca i32, align 4<br>
              +   store i32 0, i32* %1, align 4<br>
              +   %2 = call i32 (i8*, ...) @printf(i8* getelementptr
              inbounds ([15 x i8], [15 x i8]* @.str, i32 0, i32 0)),
              !dbg !13<br>
              +   ret i32 0, !dbg !14<br>
                }<br>
              <br>
                declare i32 @printf(i8*, ...) #1<br>
              <br>
                attributes #0 = { noinline nounwind optnone sspstrong
              uwtable &quot;correctly-rounded-divide-sqrt-fp-math&quot;=&quot;false&quot;
              &quot;disable-tail-calls&quot;=&quot;false&quot; &quot;less-precise-fpmad&quot;=&quot;false&quot;
              &quot;min-legal-vector-width&quot;=&quot;0&quot;
              &quot;no-frame-pointer-elim&quot;=&quot;true&quot;
              &quot;no-frame-pointer-elim-non-leaf&quot; &quot;no-infs-fp-math&quot;=&quot;false&quot;
              &quot;no-jump-tables&quot;=&quot;false&quot; &quot;no-nans-fp-math&quot;=&quot;false&quot;
              &quot;no-signed-zeros-fp-math&quot;=&quot;false&quot;
              &quot;no-trapping-math&quot;=&quot;false&quot;
              &quot;stack-protector-buffer-size&quot;=&quot;8&quot; &quot;target-cpu&quot;=&quot;x86-64&quot;
              &quot;target-features&quot;=&quot;+fxsr,+mmx,+sse,+sse2,+x87&quot;
              &quot;unsafe-fp-math&quot;=&quot;false&quot; &quot;use-soft-float&quot;=&quot;false&quot; }<br>
                attributes #1 = {
              &quot;correctly-rounded-divide-sqrt-fp-math&quot;=&quot;false&quot;
              &quot;disable-tail-calls&quot;=&quot;false&quot; &quot;less-precise-fpmad&quot;=&quot;false&quot;
              &quot;no-frame-pointer-elim&quot;=&quot;true&quot;
              &quot;no-frame-pointer-elim-non-leaf&quot; &quot;no-infs-fp-math&quot;=&quot;false&quot;
              &quot;no-nans-fp-math&quot;=&quot;false&quot;
              &quot;no-signed-zeros-fp-math&quot;=&quot;false&quot;
              &quot;no-trapping-math&quot;=&quot;false&quot;
              &quot;stack-protector-buffer-size&quot;=&quot;8&quot; &quot;target-cpu&quot;=&quot;x86-64&quot;
              &quot;target-features&quot;=&quot;+fxsr,+mmx,+sse,+sse2,+x87&quot;
              &quot;unsafe-fp-math&quot;=&quot;false&quot; &quot;use-soft-float&quot;=&quot;false&quot; }<br>
              <br>
                !<a href="https://urldefense.com/v3/__http://llvm.dbg.cu__;!!GqivPVa7Brio!M_VQB1VYM2oG0tUtVXzAs5zr9rDHleDIiNnN14WgTynkSubDpWG5C3J0oIZzO4ag0HM$" rel="noreferrer" target="_blank">llvm.dbg.cu</a> =
              !{!0}<br>
                !llvm.module.flags = !{!3, !4, !5, !6, !7}<br>
                !llvm.ident = !{!8}<br>
              <br>
                !0 = distinct !DICompileUnit(language: DW_LANG_C99,
              file: !1, producer: &quot;clang version 8.0.1
              (tags/RELEASE_801/final)&quot;, isOptimized: false,
              runtimeVersion: 0, emissionKind: FullDebug, enums: !2,
              nameTableKind: None)<br>
                !1 = !DIFile(filename: &quot;hello.c&quot;, directory:
              &quot;/home/roland/test/hello&quot;)<br>
                !2 = !{}<br>
                !3 = !{i32 2, !&quot;Dwarf Version&quot;, i32 4}<br>
                !4 = !{i32 2, !&quot;Debug Info Version&quot;, i32 3}<br>
                !5 = !{i32 1, !&quot;wchar_size&quot;, i32 4}<br>
                !6 = !{i32 7, !&quot;PIC Level&quot;, i32 2}<br>
                !7 = !{i32 7, !&quot;PIE Level&quot;, i32 2}<br>
                !8 = !{!&quot;clang version 8.0.1 (tags/RELEASE_801/final)&quot;}<br>
                !9 = distinct !DISubprogram(name: &quot;main&quot;, scope: !1,
              file: !1, line: 3, type: !10, scopeLine: 3, spFlags:
              DISPFlagDefinition, unit: !0, retainedNodes: !2)<br>
                !10 = !DISubroutineType(types: !11)<br>
                !11 = !{!12}<br>
                !12 = !DIBasicType(name: &quot;int&quot;, size: 32, encoding:
              DW_ATE_signed)<br>
                !13 = !DILocation(line: 4, column: 5, scope: !9)<br>
                !14 = !DILocation(line: 5, column: 5, scope: !9)<br>
------------------------------------------------------------------------------------------<br>
               Path                                                 | 
              Statements |    Lines |    Roots<br>
               com.oracle.truffle.llvm.libraries.bitcode/src/crt0.c
              |     100.00% |          |  100.00%<br>
              <br>
              NO CONTENT AVAILABLE<br>
------------------------------------------------------------------------------------------<br>
               Path                                                 | 
              Statements |    Lines |    Roots<br>
               com.oracle.truffle.llvm.libraries.bitcode/src/exit.c
              |      87.50% |          |  100.00%<br>
              <br>
              NO CONTENT AVAILABLE<br>
------------------------------------------------------------------------------------------<br>
            </blockquote>
            <br>
            <blockquote type="cite">$ lli --coverage
              --coverage.Output=detailed hello.bc<br>
              Hello, World!<br>
------------------------------------------------------------------------------------------<br>
              Code coverage per line of code and what percent of each
              element was covered during execution (per source)<br>
                + indicates the line is covered during execution<br>
                - indicates the line is not covered during execution<br>
                p indicates the line is part of a statement that was
              incidentally covered during execution<br>
                  e.g. a not-taken branch of a covered if statement<br>
------------------------------------------------------------------------------------------<br>
               Path                                                 | 
              Statements |    Lines |    Roots<br>
               /home/roland/test/hello/hello.c                     
              |     100.00% |  100.00% |  100.00%<br>
              <br>
                #include &lt;stdio.h&gt;<br>
              <br>
                int main() {<br>
              +     printf(&quot;Hello, World!\n&quot;);<br>
              +     return 0;<br>
                }<br>
------------------------------------------------------------------------------------------<br>
               Path                                                 | 
              Statements |    Lines |    Roots<br>
               com.oracle.truffle.llvm.libraries.bitcode/src/crt0.c
              |     100.00% |          |  100.00%<br>
              <br>
              NO CONTENT AVAILABLE<br>
------------------------------------------------------------------------------------------<br>
               Path                                                 | 
              Statements |    Lines |    Roots<br>
               com.oracle.truffle.llvm.libraries.bitcode/src/exit.c
              |      87.50% |          |  100.00%<br>
              <br>
              NO CONTENT AVAILABLE<br>
------------------------------------------------------------------------------------------<br>
            </blockquote>
            <br>
            <br>
            <blockquote type="cite">
              <div dir="auto">
                <div dir="auto"><br>
                </div>
                <div dir="auto">If all goes well I&#39;d like to work on a
                  code coverage tool for the bitcode.</div>
                <div dir="auto"><br>
                </div>
                <div dir="auto">Thanks</div>
                <div dir="auto">Alberto</div>
              </div>
              <br>
              <div class="gmail_quote">
                <div dir="ltr" class="gmail_attr">On Tue, Feb 11, 2020,
                  16:02 Roland Schatz &lt;<a href="mailto:roland.schatz@oracle.com" rel="noreferrer" target="_blank">roland.schatz@oracle.com</a>&gt;
                  wrote:<br>
                </div>
                <blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
                  <div>
                    <div>Hi Alberto!<br>
                      <br>
                      I assume you mean the
                      LLVMBitcodeInstructionVisitor class, right? This
                      class is part of the internal implementation of
                      the LLVM runtime in GraalVM, and not meant to be
                      extended or used outside of the LLVM runtime. This
                      is not meant as API, we might change that class
                      without notice, even in minor releases.<br>
                      <br>
                      What are you trying to achieve by subclassing it?<br>
                      <br>
                      - Roland<br>
                      <br>
                      On 2/11/20 4:31 PM, Alberto Barbaro wrote:<br>
                    </div>
                    <blockquote type="cite">
                      <div dir="auto">Hi all,
                        <div dir="auto">I&#39;ve just started to use graalvm
                          and I&#39;ve noticed that the LLVMBitcodeVisitor
                          class is market as final. I&#39;d like to create
                          my own visitor extending it... So I was
                          wondering if there is a better approach rather
                          then modifying a bit the source code. How
                          would you recommend to do it?</div>
                        <div dir="auto"><br>
                        </div>
                        <div dir="auto">Thanks</div>
                        <div dir="auto">Alberto</div>
                      </div>
                      <br>
                      <fieldset></fieldset>
                      <pre>_______________________________________________
GraalVM-Users mailing list
<a href="mailto:GraalVM-Users@oss.oracle.com" rel="noreferrer noreferrer" target="_blank">GraalVM-Users@oss.oracle.com</a>
<a href="https://oss.oracle.com/mailman/listinfo/graalvm-users" rel="noreferrer noreferrer" target="_blank">https://oss.oracle.com/mailman/listinfo/graalvm-users</a></pre>
                    </blockquote>
                    <p><br>
                    </p>
                  </div>
                  _______________________________________________<br>
                  GraalVM-Users mailing list<br>
                  <a href="mailto:GraalVM-Users@oss.oracle.com" rel="noreferrer noreferrer" target="_blank">GraalVM-Users@oss.oracle.com</a><br>
                  <a href="https://oss.oracle.com/mailman/listinfo/graalvm-users" rel="noreferrer noreferrer noreferrer" target="_blank">https://oss.oracle.com/mailman/listinfo/graalvm-users</a></blockquote>
              </div>
            </blockquote>
            <p><br>
            </p>
          </div>
          _______________________________________________<br>
          GraalVM-Users mailing list<br>
          <a href="mailto:GraalVM-Users@oss.oracle.com" rel="noreferrer" target="_blank">GraalVM-Users@oss.oracle.com</a><br>
          <a href="https://oss.oracle.com/mailman/listinfo/graalvm-users" rel="noreferrer noreferrer" target="_blank">https://oss.oracle.com/mailman/listinfo/graalvm-users</a></blockquote>
      </div>
    </blockquote>
    <p><br>
    </p>
  </div>

</blockquote></div>
</blockquote></div>