#!/bin/bash

# test uses sp-rtrace
# http://maemo.gitorious.org/maemo-tools/sp-rtrace
#

file=gst-launch-mem-bfd.rtrace.txt
rm -f *.rtrace

# 1. Start Gstreamer under memory tracing and ^C after once it runs
G_SLICE=always-malloc G_DEBUG=gc-friendly sp-rtrace -s -p memory -b 16 -x gst-launch-0.10 "fakesrc ! fakesink"
   
# 2. Post-process and resolve the allocation backtraces:
cat *.rtrace | sp-rtrace-postproc -cl | sp-rtrace-resolve -t bfd > $file

# 3. Produce callgraph out of the unfreed allocations:
rtrace-calltree --type=graph --ignore=_start --ignore=g_malloc0 --ignore=realloc --ignore=malloc \
  --ignore=calloc --ignore=posix_memalign --limit=2.5 --emph-limit=10 --show-below-limit=yes $file \
  | sed -e 's/ at /\\nat /' -e 's/ in /\\nin /' -e 's/([^0-9)][^)]*)/()/' -e 's/)(.[^)]*)//' \
  | dot -Tsvg > ${file%.*}.svg

# 4. View the resulting callgraph
rsvg-view gst-launch-mem-bfd.rtrace.svg

