Created
November 4, 2016 17:28
-
-
Save mrzor/4486f49eced97bba18fb32557090cd53 to your computer and use it in GitHub Desktop.
A flawed BTrace script to record all object allocations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package btraces; | |
| import com.sun.btrace.annotations.*; | |
| import static com.sun.btrace.BTraceUtils.*; | |
| @BTrace | |
| public class AllAllocationsTogglable { | |
| @TLS private static boolean enabled = false; | |
| @OnMethod( | |
| clazz="java.lang.System", | |
| method="setProperty" | |
| ) | |
| public static void onSetProperty(String key, String value) { | |
| if (Strings.compareTo("tracerino.allAllocations", key) != 0) | |
| return; | |
| if (Strings.compareTo("0", value) == 0) { | |
| enabled = false; | |
| String traceTag = Sys.Env.property("tracerino.allAllocations"); | |
| print("AllAllocations: disabling <"); | |
| print(traceTag); | |
| println('>'); | |
| return; | |
| } | |
| enabled = true; | |
| print("AllAllocations: enabling <"); | |
| print(value); | |
| println('>'); | |
| } | |
| @OnMethod(clazz="+java.lang.Object", | |
| method="<init>") | |
| public static void onNewThing(@Self Object o, | |
| @ProbeClassName String pcn) { | |
| if (!enabled) | |
| return; | |
| Class objectClass = Reflective.classOf(o); | |
| String objectClassString = Reflective.name(objectClass); | |
| if (Strings.compareTo(pcn, objectClassString) == 0) { | |
| print(pcn); | |
| print('\t'); | |
| println(Strings.str(sizeof(o))); | |
| } | |
| } | |
| } |
Author
Hi Eli Zor,
Thanks for providing this awesome script, according to btraceio/btrace#252, you made some modification on Feb 2017, could u please provide updated version of this script?
My task is use Btrace to track big object allocation and its stacktrace.
Really appreciated ur help in advance!
Thanks,
Roy
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Amongst other things, one pretty big flaw is that it doesn't instrument constructors that take arguments (which might be fixable).