#! /usr/bin/env stap 

global bt%[100]

probe process("/lib*/libc.so.*").function("free") {
  bt[execname(),tid(),$mem,sprint_ubacktrace()]
    <<< local_clock_ns()
  // Any monotonically increasing expression would do.
  // With some arbitrary expression or constant instead,
  // at worst we get the last 100ish results out of order.
}

probe end,error {
  foreach ([e,t,p,b] in bt @max+) // iterate in increasing index order
     printf("free #%d: %s[%d], pointer %p:\n%s\n\n",
            @max(bt[e,t,p,b]), e, t, p, b)
}
