<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: mary marchini</title>
    <description>The latest articles on DEV Community by mary marchini (@mmarchini).</description>
    <link>https://dev.to/mmarchini</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F460356%2F3dd4c03a-35ce-4ea9-bd28-ebfcd5a01c13.JPEG</url>
      <title>DEV Community: mary marchini</title>
      <link>https://dev.to/mmarchini</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mmarchini"/>
    <language>en</language>
    <item>
      <title>Node.js Performance Analysis Without Changing Your Code</title>
      <dc:creator>mary marchini</dc:creator>
      <pubDate>Wed, 21 Jun 2023 02:22:10 +0000</pubDate>
      <link>https://dev.to/mmarchini/nodejs-performance-analysis-without-changing-your-code-90g</link>
      <guid>https://dev.to/mmarchini/nodejs-performance-analysis-without-changing-your-code-90g</guid>
      <description>&lt;p&gt;Being a Performance Engineer in the Node.js Platform Team at Netflix means I oftentimes have to troubleshoot performance issues in applications my team doesn't own. It's unfeasible to expect all applications to have instrumentation code that allows them to run V8 performance analysis tools (such as CPU Profiles, Heap Profiles and Heap Snapshots), so I wrote a CLI that allows users to run those performance analysis tools without making any changes to their code: &lt;a href="https://www.npmjs.com/package/@mmarchini/observe"&gt;@mmarchini/observe&lt;/a&gt;. I realized I never wrote anything about this CLI, so this is a lil blog post about it :)&lt;/p&gt;

&lt;h2&gt;
  
  
  Why
&lt;/h2&gt;

&lt;p&gt;As mentioned before, sometimes you need to troubleshoot an application that is already running. The &lt;a href="https://v8.dev/docs/inspector"&gt;Inspector Protocol&lt;/a&gt; allows you to call V8 troubleshooting tools without making any changes to your application. It's the same protocol used by Chrome DevTools, and it exposes similar troubleshooting features as the ones available on Chrome DevTools. It is, therefore, a powerful protocol.&lt;/p&gt;

&lt;p&gt;Unfortunately, using the protocol by itself can be overwhelming for new users. Existing tools that use it are generally aimed at development environments, with either GUIs or REPLs as their only interfaces, which prevents automation.&lt;/p&gt;

&lt;p&gt;There are libraries that make usage of the inspector protocol more straightforward, but most libraries I found had to be loaded within the app, therefore requiring code change and a redeploy, which can take time to happen and hinder performance investigation for time sensitive issues.&lt;/p&gt;

&lt;p&gt;I also wanted something with as few direct and transitive dependencies as possible. Most packages I found had more dependencies than I wanted.&lt;/p&gt;

&lt;p&gt;The closest I found to what I wanted was &lt;a href="https://github.com/cyrus-and/chrome-remote-interface"&gt;chrome-remote-interface&lt;/a&gt;, which is a great package with few dependencies and a programmatic API that allows users to connect to running applications and run inspector protocol commands on it. It doesn't abstract away the complexities of the Inspector Protocol though, and I wanted something that could be run as a one-liner most of the time.&lt;/p&gt;

&lt;p&gt;Since the tools that Node.js/V8 provide for performance analysis via Inspector Protocol are well defined, it seemed like a good use case for a CLI. Its execution mode is somewhat inspired by &lt;a href="https://docs.oracle.com/javacomponents/jmc-5-4/jfr-runtime-guide/about.htm#JFRUH170"&gt;Java Flight Recorder&lt;/a&gt; (but without the need to start an application with specific flags), &lt;a href="http://dtrace.org/blogs/about/"&gt;dtrace&lt;/a&gt;, &lt;a href="https://bpftrace.org/"&gt;bpftrace&lt;/a&gt;, and similar tools. The resulting CLI depends only on &lt;code&gt;ws&lt;/code&gt; and &lt;code&gt;commander&lt;/code&gt;, which resulted in only two direct and no transitive dependencies. Small install size and lower vulnerability surface area ftw!&lt;/p&gt;

&lt;h2&gt;
  
  
  Using the CLI
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;@mmarchini/observe&lt;/code&gt; can be installed via your package manager of choice or can be run with &lt;code&gt;npx&lt;/code&gt;. I'll be using &lt;code&gt;npx&lt;/code&gt; in the examples below as that's my personal preference, but you should use whatever you have available and feel comfortable with.&lt;/p&gt;

&lt;p&gt;A list of available commands can be seen by running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx @mmarchini/observe &lt;span class="nt"&gt;--help&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Options for each command can be seen by running those commands with &lt;code&gt;--help&lt;/code&gt; option:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx @mmarchini/observe heap-profile --help
npx @mmarchini/observe heap-snapshot --help
npx @mmarchini/observe cpu-profile --help
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As of right now, there are three tools available: &lt;code&gt;heap-profile&lt;/code&gt;, &lt;code&gt;heap-snapshot&lt;/code&gt; and &lt;code&gt;cpu-profile&lt;/code&gt;. Heap Snapshots and Heap Profiles are memory analysis tools, with the former taking a "snapshot" of V8's memory, and the latter sampling stack traces every X allocations. Heap Snapshots are expensive but comprehensive and good to determine where most of the memory is currently being used.&lt;/p&gt;

&lt;p&gt;Heap profiles (known as "Allocation Samples" on CDP interface) are a lightweight, sample based profiler that captures stack traces on allocations, and is a good tool to understand how memory is growing over time. CPU Profile, as the name suggests, is your typical CPU profiler and it samples stack traces at regular intervals.&lt;/p&gt;

&lt;p&gt;Only JavaScript frames are captured for both profilers. If you need a combination of native frames and JavaScript Frames, consider using something like &lt;a href="https://nodejs.org/en/docs/guides/diagnostics/poor-performance/using-linux-perf"&gt;Linux perf&lt;/a&gt; or &lt;a href="https://manpages.debian.org/unstable/bpfcc-tools/memleak-bpfcc.8.en.html"&gt;memleak&lt;/a&gt;. I'm currently working on a blog post about &lt;code&gt;memleak&lt;/code&gt; usage with Node.js applications, and I might write an updated guide on Linux perf in the future.&lt;/p&gt;

&lt;p&gt;You should pass the &lt;code&gt;-f &amp;lt;filename&amp;gt;&lt;/code&gt; option to save the output from each tool into a file. Those files can then be loaded on Chrome DevTools (chrome://inspect and open the "dedicated DevTools for Node"), or with any other tools that can process these files. Since DevTools don't have FlameGraphs for CPU Profiles, I usually use &lt;a href="https://www.speedscope.app/"&gt;Speedscope&lt;/a&gt; since it has a nice interface and features, and has no server-side processing. I won't go into how to interpret the results from each tool in this blog post, but I plan on writing individual interpretation guides for each tool.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;That's &lt;code&gt;@mmarchini/observe&lt;/code&gt;, a tool intended to be simple and portable, yet powerful. &lt;a href="https://github.com/mmarchini-oss/node-observe"&gt;Contributions are welcome&lt;/a&gt;, I have some features in mind for future updates but don't know when I'll get to it. As mentioned above, I only showed how to use the tools here. I'll be writing follow up blog posts on how to successfully interpret the results from each tool.&lt;/p&gt;

</description>
      <category>performance</category>
      <category>node</category>
    </item>
    <item>
      <title>Taming the dragon: using llnode to debug your Node.js application</title>
      <dc:creator>mary marchini</dc:creator>
      <pubDate>Tue, 13 Jun 2023 03:53:03 +0000</pubDate>
      <link>https://dev.to/mmarchini/taming-the-dragon-using-llnode-to-debug-your-nodejs-application-4nh1</link>
      <guid>https://dev.to/mmarchini/taming-the-dragon-using-llnode-to-debug-your-nodejs-application-4nh1</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally posted at &lt;a href="https://medium.com/sthima-insights/taming-the-dragon-using-llnode-to-debug-your-node-js-application-fc54c6efd0f1"&gt;Sthima Insights&lt;/a&gt; on Jun 20, 2018. Reposted here for preservation purposes.&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: llnode hasn't been updated in years and the latest Node.js version it is known to work on is Node.js v14.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you ever experienced memory leaks or infinite loops in your applications, you know how frustrating can be to find the source of those issues. Even though Node.js has a &lt;a href="https://blog.risingstack.com/node-js-at-scale-node-js-garbage-collection/"&gt;Garbage Collector&lt;/a&gt; powered by V8, it is still possible to have memory leaks (or infinite memory growth) if we keep unnecessary references to objects.&lt;/p&gt;

&lt;p&gt;One technique available to investigate those kinds of issues is postmortem debugging. Developers of native languages (such as C++ and Go) might be familiar with it, especially postmortem debugging of &lt;a href="https://wiki.archlinux.org/index.php/Core_dump"&gt;core dump&lt;/a&gt; files — files with a copy of the process memory at a given time.&lt;/p&gt;

&lt;p&gt;Fortunately, we can use this same technique with our Node.js applications! This is possible through &lt;code&gt;llnode&lt;/code&gt;: a &lt;a href="https://lldb.llvm.org/"&gt;LLDB&lt;/a&gt; plugin which enables us to inspect Node.js core dumps. With &lt;code&gt;llnode&lt;/code&gt;, we can inspect objects in the memory and look at the complete backtrace of the program, including native (C++) frames and JavaScript frames. It can be used on a running Node.js application or through a core dump.&lt;/p&gt;

&lt;h1&gt;
  
  
  It’s dangerous to go alone. Take this!
&lt;/h1&gt;

&lt;p&gt;&lt;code&gt;llnode&lt;/code&gt; is available through &lt;code&gt;npm&lt;/code&gt; and works on with active Node.js Release Lines (v6.x, v8.x, v9.x and v10.x by the time of this writing). Since it is a plugin for &lt;code&gt;lldb&lt;/code&gt;, you'll need to install it first. &lt;code&gt;llnode&lt;/code&gt; works best on LLDB 3.9 or later.&lt;/p&gt;

&lt;p&gt;On Mac OS you get &lt;code&gt;lldb&lt;/code&gt; by installing &lt;a href="https://developer.apple.com/xcode/"&gt;Xcode&lt;/a&gt;. If you’re using Ubuntu though, you can install LLDB by running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt install lldb-4.0 liblldb-4.0-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After &lt;code&gt;lldb&lt;/code&gt; is installed, you can use &lt;code&gt;npm install -g llnode&lt;/code&gt; to install &lt;code&gt;llnode&lt;/code&gt; globally. Please be aware that if you're not using Node.js through &lt;code&gt;nvm&lt;/code&gt; or don't have a &lt;a href="https://docs.npmjs.com/getting-started/fixing-npm-permissions#option-two-change-npms-default-directory"&gt;global prefix&lt;/a&gt; set, you'll need to run &lt;code&gt;sudo install -g llnode&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Generating a core dump
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;llnode&lt;/code&gt; can either attach to a process or read a core dump file. In this tutorial we'll focus on the latter, and because of that we need to first be able to generate core dumps.&lt;/p&gt;

&lt;p&gt;There are two main methods to generate core dumps in Unix systems: you can either create a core dump from a running process or create one when the process crashes.&lt;/p&gt;

&lt;p&gt;To generate a core dump of a running process, you can use &lt;code&gt;gcore **{PID}**&lt;/code&gt;(Linux) or &lt;code&gt;lldb --attach-pid **{PID}** -b -o 'process save-core "core.**{PID}**"'&lt;/code&gt; (OS X), replacing &lt;code&gt;**{PID}**&lt;/code&gt; with the PID of your application. Both methods will create a core dump file called &lt;code&gt;core.$PID&lt;/code&gt; at the current working directory.&lt;/p&gt;

&lt;p&gt;If, however, you want to generate a core dump when your process crashes (useful to debug your application when it dies by memory starvation, for example), you’ll need to set the ulimit of your system by running &lt;code&gt;ulimit -c unlimited&lt;/code&gt;. This will set ulimit for the current terminal session, which means you'll need to set again if you open another terminal. Core dumps generated this way are saved in a file named &lt;code&gt;core&lt;/code&gt; in the current working directory for Linux, while on OS X cores are stored in the &lt;code&gt;/cores/&lt;/code&gt; folder. Be careful on Mac OS, because core dumps can quickly fill your HD.&lt;/p&gt;

&lt;p&gt;Another thing about generating core dumps when your process crashes: Node.js won’t crash when it terminates because of an uncaught exception. If you want to generate a core dump in those cases, you’ll need to run Node.js with &lt;code&gt;--abort-on-uncaught-exception&lt;/code&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Taming the Dragon
&lt;/h1&gt;

&lt;p&gt;Now that we have &lt;code&gt;llnode&lt;/code&gt; installed and we know how to generate a core dump let's learn how to use them together. We'll be using a small Node.js HTTP server as an example:&lt;/p&gt;

&lt;p&gt;First, let’s run this server:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ node index.js  
server is listening on 3000. Our PID is: **74262**
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To make things interesting, let’s send some requests to this server with &lt;a href="https://github.com/mcollina/autocannon"&gt;autocannon&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ # Send requests to localhost:3000 during 10 seconds  
$ npx autocannon -d 10 localhost:3000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we’ll generate a core dump of the running process. In this case, the PID is &lt;strong&gt;74262&lt;/strong&gt;. &lt;strong&gt;&lt;em&gt;Remember to change the PID in the commands below with the PID of your Node.js process&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;To generate a core dump on Linux, run the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo gcore **74262** 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you are on OS X, you can run the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ lldb --attach-pid **74262** -b -o 'process save-core "core.74262"'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both commands will create a core dump in the current working directory with the name &lt;code&gt;core.**74262**&lt;/code&gt;, and now we can use &lt;code&gt;llnode&lt;/code&gt; to open it. &lt;code&gt;llnode&lt;/code&gt; takes one required argument, which is the binary you want to debug (in this case, &lt;code&gt;node&lt;/code&gt;) and a variety of optional arguments. Since we want to debug a core dump, we need to tell &lt;code&gt;llnode&lt;/code&gt; where the core dump file is with &lt;code&gt;-c **{core-file}**&lt;/code&gt;. The final command will look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ llnode node -c core.**74262**  
(llnode)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s start with a command that prints information about Node.js and its dependencies. This command will also help us understand the syntax of the commands for &lt;code&gt;llnode&lt;/code&gt;. On the &lt;code&gt;(llnode)&lt;/code&gt; prompt, type &lt;code&gt;v8 nodeinfo&lt;/code&gt; and press Enter:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(llnode) v8 nodeinfo  
Information for process id 74262 (process=0x1ca4ddb89cb9)  
Platform = darwin, Architecture = x64, **Node Version = v8.11.2**  
Component versions (process.versions=0x1ca4b45a0379):  
    cldr = 32.0  
    icu = 60.1  
    tz = 2017c  
    unicode = 10.0  
Release Info (process.release=0x1ca4b45a0451):  
Executable Path = /Users/mmarchini/.nvm/versions/node/v8.11.2/bin/node  
Command line arguments (process.argv=0x1ca4b45a0411):  
    \[0\] = '/Users/mmarchini/.nvm/versions/node/v8.11.2/bin/node'  
    \[1\] = '/Users/mmarchini/workspace/blog-posts/index.js'  
Node.js Comamnd line arguments (process.execArgv=0x1ca4b45a0579):
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;llnode&lt;/code&gt; commands are prefixed by &lt;code&gt;v8&lt;/code&gt; + a space. This will help you distinguish between &lt;code&gt;llnode&lt;/code&gt; and &lt;code&gt;lldb&lt;/code&gt; commands. Speaking of help, &lt;code&gt;v8 help&lt;/code&gt; will be your best friend from now on:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(llnode) v8 help  
     Node.js helpersSyntax:The following subcommands are supported: **bt**              -- Show a backtrace with node.js JavaScript   
                         functions and their args. An optional   
                         argument is accepted; if that argument is a   
                         number, it specifies the number of frames   
                         to display. Otherwise all frames will be   
                         dumped.  
                         Syntax: v8 bt \[number\]  
      **findjsinstances** -- List every object with the specified type   
                         name. Use -v or --verbose to display   
                         detailed \`v8 inspect\` output for each   
                         object. Accepts the same options as   
                         \`v8 inspect\`  
      **findjsobjects**   -- List all object types and instance counts       
                         grouped by type name and sorted by instance   
                         count. Use -d or --detailed to get an   
                         output grouped by type name, properties,   
                         and array length, as well as more   
                         information regarding each type.  
      **findrefs**        -- Finds all the object properties which meet   
                         the search criteria. The default is to list   
                         all the object properties that reference   
                         the specified value.  
                         Flags:  
                         \* -v, --value expr     - all properties   
                                                  that refer to the   
                                                  specified   
                                                  JavaScript object   
                                                  (default)  
                         \* -n, --name  name     - all properties   
                                                  with the specified       
                                                  name  
                         \* -s, --string string  - all properties   
                                                  that refer to the   
                                                  specified   
                                                  JavaScript string   
                                                  value  
      **inspect**         -- Print detailed description and contents of   
                         the JavaScript value.  
                         Possible flags (all optional):  
                         \* -F, --full-string    - print whole string   
                                                  without adding   
                                                  ellipsis  
                         \* -m, --print-map      - print object's map   
                                                  address  
                         \* -s, --print-source   - print source code   
                                                  for function   
                                                  objects  
                         \* -l num, --length num - print maximum of   
                                                  \`num\` elements   
                                                  from string/array  
                         Syntax: v8 inspect \[flags\] expr  
      **nodeinfo**        -- Print information about Node.js  
      **print**           -- Print short description of the JavaScript   
                         value.  
                         Syntax: v8 print expr  
      **source**          -- Source code informationFor more help on any particular subcommand, type 'help &amp;lt;command&amp;gt; &amp;lt;subcommand&amp;gt;'.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Yes, there’s a lot of information there. But don’t worry, we’ll go through the interesting commands now.&lt;/p&gt;

&lt;h2&gt;
  
  
  v8 findjsobjects
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;v8 findjsobjects&lt;/code&gt; lists all object types alongside their sizes and quantities. This is useful to find out which object types are using most of your memory. The first time you run this command might take a few minutes to process, so don't worry if that happens.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(llnode) v8 findjsobjects  
 Instances  Total Size Name  
 ---------- ---------- ----  
          1         24 AssertionError  
          1         24 AsyncResource  
          1         24 Control  
          1         24 FastBuffer  
          1         24 Loader  
          1         24 ModuleJob  
          1         24 ModuleMap  
          1         24 Performance  
          1         24 PerformanceObserver  
          1         24 SafeMap  
          1         24 SafePromise  
          1         24 SafeSet  
          1         24 SocketListReceive  
          1         24 SocketListSend  
          1         24 TextDecoder  
          1         24 TextEncoder  
          1         24 URL  
          1         24 URLContext  
          1         24 URLSearchParams  
          1         24 WebAssembly  
          1         32 (Object)  
          1         32 ContextifyScript  
          1        104 ImmediateList  
          1        104 Stack  
          1        128 Server  
          1        168 Agent  
          2         48 (anonymous)  
          2         48 process  
          2         64 ChannelWrap  
          2         64 Signal  
          2        120 Resolver  
          2        128 PerformanceNodeTiming  
          2        136 NextTickQueue  
          2        144 FreeList  
          2        200 PerformanceObserverEntryList  
          2        208 EventEmitter  
          2        208 WriteStream  
          2        224 Console  
          2        272 Module  
          3         72 NodeError  
          3         96 TTY  
          3        280 AsyncHook  
          4        128 Timer  
          6        432 TimersList  
         10       2480 Socket  
         11        352 HTTPParser  
         11        352 WriteWrap  
         12        384 TCP  
         12       2688 WritableState  
         15       1360 (ArrayBufferView)  
         74       4736 NativeModule  
       5715    1234440 IncomingMessage  
       5744     781184 ServerResponse  
       5747    1103424 ReadableState  
       5748     275880 BufferList  
      45980    2942680 TickObject  
      69344    2219008 (Array)  
     **235515    9420584 Visit**  
     293720   15437744 Object  
     615411    3750984 (String)  
 ---------- ----------  
    1283140   37182200
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we look at the output, we'll see a lot of &lt;code&gt;Visit&lt;/code&gt; objects generated by our &lt;code&gt;autocannon&lt;/code&gt; run. We'll also see Visit is the third type using more memory. In a real case scenario, this information is the first step to track down memory leaks.&lt;/p&gt;

&lt;p&gt;There’s also a detailed version of this command which can be called with &lt;code&gt;v8 findjsobjects -d&lt;/code&gt;. The result will group types with the same attributes and number of elements, and will also provide the address to an example object of that type.&lt;/p&gt;

&lt;h2&gt;
  
  
  v8 findjsinstances
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;v8 findjsinstances&lt;/code&gt; gives you a list with all objects of a given type. It also has a detailed version which can be called with &lt;code&gt;v8 findjsinstances -d&lt;/code&gt;. This will print all objects of a given type with their attributes and elements.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(llnode) v8 findjsinstances -d Visit  
0x0000176d04402201:&amp;lt;Object: Visit properties {  
    .visit\_id=&amp;lt;Smi: 82704&amp;gt;,  
    .headers=0x0000176d7d99f1c9:&amp;lt;Object: Object&amp;gt;}&amp;gt;  
0x0000176d04402229:&amp;lt;Object: Visit properties {  
    .visit\_id=&amp;lt;Smi: 82705&amp;gt;,  
    .headers=0x0000176d7d99f191:&amp;lt;Object: Object&amp;gt;}&amp;gt;  
0x0000176d04402251:&amp;lt;Object: Visit properties {  
    .visit\_id=&amp;lt;Smi: 82706&amp;gt;,  
    .headers=0x0000176d7d99f159:&amp;lt;Object: Object&amp;gt;}&amp;gt;  
0x0000176d04402279:&amp;lt;Object: Visit properties {  
    .visit\_id=&amp;lt;Smi: 82707&amp;gt;,  
    .headers=0x0000176d7d99f121:&amp;lt;Object: Object&amp;gt;}&amp;gt;  
0x0000176d044022a1:&amp;lt;Object: Visit properties {  
    .visit\_id=&amp;lt;Smi: 82708&amp;gt;,  
    .headers=0x0000176d7d99f0e9:&amp;lt;Object: Object&amp;gt;}&amp;gt;  
0x0000176d044022c9:&amp;lt;Object: Visit properties {  
    .visit\_id=&amp;lt;Smi: 82709&amp;gt;,  
    .headers=0x0000176d7d99f0b1:&amp;lt;Object: Object&amp;gt;}&amp;gt;  
_// A thousand miles later...  
_0x0000176dffba62d9:&amp;lt;Object: Visit properties {  
    .visit\_id=&amp;lt;Smi: 156026&amp;gt;,  
    .headers=0x0000176dffbef559:&amp;lt;Object: Object&amp;gt;}&amp;gt;  
0x0000176dffba6301:&amp;lt;Object: Visit properties {  
    .visit\_id=&amp;lt;Smi: 156027&amp;gt;,  
    .headers=0x0000176dffbef8a9:&amp;lt;Object: Object&amp;gt;}&amp;gt;  
**0x0000176dffba6329**:&amp;lt;Object: Visit properties {  
    .visit\_id=&amp;lt;Smi: 156028&amp;gt;,  
    .headers=0x0000176dffb82481:&amp;lt;Object: Object&amp;gt;}&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;v8 findjsinstances -d&lt;/code&gt; accepts the same arguments you can pass to &lt;code&gt;v8 inspect&lt;/code&gt;, which we will see next.&lt;/p&gt;

&lt;h2&gt;
  
  
  v8 inspect
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;v8 inspect&lt;/code&gt; prints all attributes and elements of a given object. It can also print additional information, such as the address to the object's map. If you want to see the object's map address, you should run &lt;code&gt;v8 inspect -m&lt;/code&gt;. You can inspect the map like any other object by using &lt;code&gt;v8 inspect&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(llnode) v8 inspect -m **0x0000176dffba6329**  
0x0000176dffba6329(map=**_0x0000176d689cec29_**):&amp;lt;Object: Visit properties {  
    .visit\_id=&amp;lt;Smi: 156028&amp;gt;,  
    .headers=0x0000176dffb82481:&amp;lt;Object: Object&amp;gt;}&amp;gt;  
(llnode) v8 inspect **_0x0000176d689cec29_**  
0x0000176d689cec29:&amp;lt;Map own\_descriptors=2 in\_object\_size=2   
  instance\_size=40   
  descriptors=0x0000176d7f284569:&amp;lt;FixedArray, len=8 contents={  
    \[0\]=&amp;lt;Smi: 2&amp;gt;,  
    \[1\]=&amp;lt;Smi: 0&amp;gt;,  
    \[2\]=0x0000176dd8566a11:&amp;lt;String: "visit\_id"&amp;gt;,  
    \[3\]=&amp;lt;Smi: 320&amp;gt;,  
    \[4\]=&amp;lt;Smi: 1&amp;gt;,  
    \[5\]=0x0000176dd8566a31:&amp;lt;String: "headers"&amp;gt;,  
    \[6\]=&amp;lt;Smi: 1050112&amp;gt;,  
    \[7\]=0x0000176d117509f9:&amp;lt;unknown&amp;gt;}&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  v8 findrefs
&lt;/h2&gt;

&lt;p&gt;Now we know how to find which types are using a huge amount of memory, how to find objects of those types and how to inspect attributes of those objects. But if we want to find memory leaks, we'll need to find what's keeping those objects in the memory. In other words, we need to find other objects referencing them. There's a command perfect for that: &lt;code&gt;v8 findrefs&lt;/code&gt;. This command will return all objects referencing another object. Let's give it a try:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(llnode) v8 findrefs **0x0000176dffba6329  
**0x176d1f4fac41: (Array)\[156027\]=0x176dffba6329
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result shows us that there's an array holding a lot (156027) of objects, and is probably the reason we have so many &lt;code&gt;Visit&lt;/code&gt; objects in memory (spoiler: it is, look at line 13 and 16 of our server). Unfortunately, llnode can't tell where this array is located &lt;em&gt;yet&lt;/em&gt;, but there's an open &lt;a href="https://github.com/nodejs/llnode/issues/195"&gt;issue&lt;/a&gt; to add this feature in the future.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Even with a limited set of features, &lt;code&gt;llnode&lt;/code&gt; is a powerful debugging tool to have on our Node.js Diagnostics arsenal. It’s ability to inspect the state of an application right after it crashed and the lightweight nature of core dumps can help us identify memory issues with less effort when compared to traditional tools. It also makes llnode and core dumps a good fit to be used on production environments.&lt;/p&gt;

</description>
      <category>node</category>
      <category>debugging</category>
      <category>lldb</category>
    </item>
    <item>
      <title>We just got a new super-power! Runtime USDT comes to Linux</title>
      <dc:creator>mary marchini</dc:creator>
      <pubDate>Tue, 13 Jun 2023 03:30:21 +0000</pubDate>
      <link>https://dev.to/mmarchini/we-just-got-a-new-super-power-runtime-usdt-comes-to-linux-12c9</link>
      <guid>https://dev.to/mmarchini/we-just-got-a-new-super-power-runtime-usdt-comes-to-linux-12c9</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally posted at &lt;a href="https://medium.com/sthima-insights/we-just-got-a-new-super-power-runtime-usdt-comes-to-linux-814dc47e909f"&gt;Sthima Insights&lt;/a&gt; on Oct 30, 2017. Reposted here for preservation purposes. Links have been updated, but the content hasn't.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;A new tracing capability has arrived in Linux: the ability to instrument user-level statically defined tracing (USDT) probes defined in dynamic languages such as Python and Node. Prior Linux USDT support has been limited to the static code, such as C or C++. Now, USDT probes can be defined in a Node.js program and used from there, allowing new analysis tools that combine custom Node.js tracepoints along with other library and kernel events. Dtrace-capable operating systems have libusdt, and now Linux has libstapsdt — a library to create USDT probes at runtime. Both have a similar API, as libstapsdt is inspired on libusdt, but they work very different internally.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/linux-usdt/libstapsdt"&gt;libstapsdt&lt;/a&gt; uses the same structure provided by Systemtap SDT. Thus it should work with any tool able to trace SDT probes from Systemtap (for example, bcc’s tplist and trace tools). It is written in C, which makes it easy to be wrapped by many other languages. If you want to learn more about how libstapsdt works internally, you can look at our documentation &lt;a href="http://libstapsdt.readthedocs.io/en/latest/"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In this tutorial, we will show how to use &lt;a href="https://github.com/iovisor/bcc"&gt;eBPF/bcc&lt;/a&gt; to trace probes using Node.js and Python wrappers for libstapsdt. We assume a basic understanding of software instrumentation and tracing tools on this tutorial.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installing Dependencies
&lt;/h2&gt;

&lt;p&gt;To use either Node.js or Python wrappers, you first need to install libstapsdt on your system. On Ubuntu 16.04, you can install libstapsdt from a PPA. For other distributions, you can build it from source following the instructions &lt;a href="https://github.com/sthima/libstapsdt"&gt;here&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo add-apt-repository ppa:sthima/oss  
sudo apt-get update  
sudo apt-get install libstapsdt0 libstapsdt-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;: the PPA above is no longer maintained or updated. A new PPA might be provided in the future, otherwise users who want the latest version can follow build instructions on GitHub.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this tutorial, we will use eBPF/bcc (version 0.4.0 or higher) to trace USDT probes. You can install the latest stable version by running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys D4284CDD  
echo "deb https://repo.iovisor.org/apt/xenial xenial main" | sudo tee /etc/apt/sources.list.d/iovisor.list  
sudo apt-get update  
sudo apt-get install bcc-tools libbcc-examples linux-headers-$(uname -r)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can either try the Node.js Example or the Python Example.&lt;/p&gt;

&lt;h2&gt;
  
  
  Node.js Example
&lt;/h2&gt;

&lt;p&gt;You can install Node.js’s wrapper by running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install usdt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Copy the following code and paste it into a file named &lt;strong&gt;index.js&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Import usdt module&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;USDT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;usdt&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Create a Provider, which we'll use to register probes&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;provider&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;USDT&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;USDTProvider&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;nodeProvider&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Register your first probe with two arguments by passing its types&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;probe1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;provider&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addProbe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;firstProbe&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;int&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;char *&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Enable provider (needed to fire probes later)&lt;/span&gt;
&lt;span class="nx"&gt;provider&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;enable&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;countdown&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;waiter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Trying to fire probe...&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;countdown&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Disable provider&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;provider&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;disable&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="c1"&gt;// Try to fire probe&lt;/span&gt;
    &lt;span class="nx"&gt;probe1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fire&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// This function will only run if the probe was enabled by an external tool&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Probe fired!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;countdown&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;countdown&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;// Returning values will be passed as arguments to the probe&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;countdown&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;My little string&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;setInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;waiter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can then run it with the following command. It will print “Trying to fire probe…” every 1 second.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;node index.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can now use some tools to trace your program. For example, if you run the command below in another terminal, it will print the arguments returned on line 26 every time it fires a probe. You can also see that now your Node.js application is printing “Probe fired!” in the terminal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;\# PID is the pid number for "node index.js"  
sudo /usr/share/bcc/tools/trace -p \[PID\] 'u::firstProbe "%d - %s", arg1, arg2'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can use any tool from bcc which uses USDT probes to analyze your Node.js programs with USDT probes!&lt;/p&gt;

&lt;p&gt;This example is also available as an ASCIICast:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://asciinema.org/a/144982"&gt;https://asciinema.org/a/144982&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Python Example
&lt;/h2&gt;

&lt;p&gt;You can install Python’s wrapper by running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install stapsdt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Copy the following code and paste it into a file named &lt;strong&gt;demo.py&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;time&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;sleep&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;stapsdt&lt;/span&gt;

&lt;span class="c1"&gt;# Create a Provider, which we'll use to register probes
&lt;/span&gt;&lt;span class="n"&gt;provider&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stapsdt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Provider&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"pythonapp"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Register your first probe with two arguments by passing its types
&lt;/span&gt;&lt;span class="n"&gt;probe&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;provider&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;add_probe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"firstProbe"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stapsdt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ArgTypes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;uint64&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stapsdt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ArgTypes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;int32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Enable provider (needed to fire probes later)
&lt;/span&gt;&lt;span class="n"&gt;provider&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;load&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Trying to fire probe..."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;# Try to fire probe
&lt;/span&gt;    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;probe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fire&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"My little probe"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# This will only run if the probe was enabled by an external tool
&lt;/span&gt;        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Probe fired!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can then run it with the following command. It will print “Trying to fire probe…” every 1 second.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python demo.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can now use some tools to trace your program. For example, if you run the command below in another terminal, it will print the arguments passed on line 18 every time it fires a probe. You can also see that now your Python application is printing “Probe fired!” in the terminal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;\# PID is the pid number for "python demo.py"  
sudo /usr/share/bcc/tools/trace -p \[PID\]'u::firstProbe "%s - %d", arg1, arg2'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can use any tool from bcc which uses USDT probes to analyze your Python programs with USDT probes!&lt;/p&gt;

&lt;p&gt;This example is also available as an ASCIICast:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://asciinema.org/a/144984"&gt;https://asciinema.org/a/144984&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Where to go from here
&lt;/h2&gt;

&lt;p&gt;Instrumentation on Linux is growing fast, especially with recent improvements to eBPF on the Kernel and the fantastic tools present on bcc. With libstapsdt, we’re taking another step by adding the ability to developers to instrument their code in dynamic languages.&lt;/p&gt;

&lt;p&gt;There are still some refinements to be done on libstapsdt, but it works for most use cases. If you want to contribute to libstapsdt, you can find us on our &lt;a href="https://github.com/linux-usdt/libstapsdt"&gt;Github page&lt;/a&gt;. If you decide to write a wrapper, please let us know (we’ll add it to the list of wrappers on the main page)!&lt;/p&gt;

</description>
      <category>bpf</category>
      <category>linux</category>
      <category>performance</category>
    </item>
  </channel>
</rss>
