Difference between revisions of "Other Dev Tools"
(Have valgrind helper replace double quotes in demangled names with two single quotes, because double quotes cause dot to fail and escaping them shows the backslash in kcachegrind.) |
(→Usage: Add complete walk-through) |
||
Line 23: | Line 23: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
===Usage=== | ===Usage=== | ||
− | |||
− | This will create a file called ''' | + | Let's take the classic '''"Hello, World!"''' example: |
+ | |||
+ | hello.d | ||
+ | <syntaxhighlight lang="D"> | ||
+ | import std.stdio; | ||
+ | |||
+ | void main(string[] args) | ||
+ | { | ||
+ | writeln("Hello, World!"); | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Compile '''hello.d''' with debug symbols: | ||
+ | |||
+ | <code>'''$ dmd -g hello.d'''</code> | ||
+ | |||
+ | Profile the resulting binary using Valgrind: | ||
+ | |||
+ | <code>'''$ valgrind --tool=callgrind --dump-instr=yes --collect-jumps=yes --callgrind-out-file=callgrind_out ./hello'''</code> | ||
+ | |||
+ | Valgrind options: | ||
+ | * ''--tool=callgrind'' - select the Callgrind plugin | ||
+ | * Callgrind options: | ||
+ | ** ''--dump-instr=yes'' - per-instruction granularity; allows to see assembly code in KCachegrind | ||
+ | ** ''--collect-jumps=yes'' - gather information about conditional jumps | ||
+ | ** ''--callgrind-out-file'' - as the name suggests, the file where you want Callgrind to save the profile data | ||
+ | |||
+ | For more information regarding Callgrind and it's options see the [http://valgrind.org/docs/manual/cl-manual.html man pages] | ||
+ | |||
+ | Now demangle the profile data: | ||
+ | |||
+ | <code>'''$ ./dcallgrind callgrind_out'''</code> | ||
+ | |||
+ | This will create a file called '''callgrind_out.demangled''' that you can then view using KCachegrind. | ||
== MiniLibD - Embedded D usage == | == MiniLibD - Embedded D usage == |
Revision as of 13:43, 14 December 2016
Valgrind helper
The Valgrind plugin Callgrind can be used to profile D code, but it does not know how to handle D's mangled names. This program can be used to clean up the callgrind.out.nnnnn file so that tools like KCachegrind can display them more legibly.
Code
dcallgrind.d
import std.array;
import std.stdio;
import std.demangle;
import std.regex;
import std.conv;
auto re = ctRegex!(`(_D\d[\w_]+)`);
void main(string[] args)
{
auto isStdin = args.length == 1;
auto inFile = isStdin ? stdin : File(args[1]);
auto outFile = isStdin ? stdout : File(args[1] ~ ".demangled", "w");
foreach (l; inFile.byLine(KeepTerminator.yes))
outFile.write(replace!(a => demangle(to!string(a.hit)).replace("\"", "''"))(l, re));
}
Usage
Let's take the classic "Hello, World!" example:
hello.d
import std.stdio;
void main(string[] args)
{
writeln("Hello, World!");
}
Compile hello.d with debug symbols:
$ dmd -g hello.d
Profile the resulting binary using Valgrind:
$ valgrind --tool=callgrind --dump-instr=yes --collect-jumps=yes --callgrind-out-file=callgrind_out ./hello
Valgrind options:
- --tool=callgrind - select the Callgrind plugin
- Callgrind options:
- --dump-instr=yes - per-instruction granularity; allows to see assembly code in KCachegrind
- --collect-jumps=yes - gather information about conditional jumps
- --callgrind-out-file - as the name suggests, the file where you want Callgrind to save the profile data
For more information regarding Callgrind and it's options see the man pages
Now demangle the profile data:
$ ./dcallgrind callgrind_out
This will create a file called callgrind_out.demangled that you can then view using KCachegrind.
MiniLibD - Embedded D usage
This collection has libraries and tools to compile D language programs for embedded systems with the gdc compiler.
The original D runtime library uses libraries from the operating system. It can not be used in embedded systems when there is no operating system.