The attached small patch is "poor man's valgrind": it injects a dose
of logging into Newx() et alia so that if PERL_MEM_LOG is defined in
compile-time, all Newx() et alia calls go through logging functions,
which most importantly pass in the source code file and linenumber.

By default the logging functions do nothing (still giving a useful
breakpoint opportunity), but if PERL_MEM_LOG_STDERR was also defined
during compile, a logging message is sent to the STDERR.  With this one
gets from e.g. "miniperl -e 1" log like the following:

alloc: sv.c:233: 4080 1 char = 4080: 140029000
alloc: sv.c:1090: 4080 1 char = 4080: 140030000
alloc: sv.c:1090: 4080 1 char = 4080: 140031000
alloc: sv.c:1090: 4064 1 char = 4064: 140032000
alloc: sv.c:1090: 4080 1 char = 4080: 140033000
alloc: scope.c:49: 1 56 PERL_SI = 56: 140028b00
alloc: sv.c:1090: 4080 1 char = 4080: 140034000
alloc: av.c:157: 128 8 SV* = 1024: 140009e00
alloc: scope.c:60: 55 144 PERL_CONTEXT = 7920: 140036000
alloc: perl.c:4318: 128 8 SV* = 1024: 14000a300
alloc: perl.c:4323: 32 4 I32 = 128: 140009a40
alloc: perl.c:4329: 32 4 I32 = 128: 140009ae0
alloc: perl.c:4333: 128 8 ANY = 1024: 14002dc00
alloc: util.c:767: 2 1 char = 2: 140035000
alloc: util.c:767: 2 1 char = 2: 140035020
alloc: util.c:767: 2 1 char = 2: 140035040
free: locale.c:209: 0
alloc: util.c:767: 2 1 char = 2: 140035060
free: locale.c:123: 0
alloc: util.c:767: 2 1 char = 2: 140035080
...
free: opmini.c:315: 140005be0
free: opmini.c:457: 0
free: opmini.c:315: 140050e00
free: opmini.c:315: 140050bc0
free: opmini.c:315: 140050ec0
free: av.c:501: 140005c10
free: av.c:501: 0
free: av.c:501: 140005c40
free: perl.c:754: 0
free: perlio.c:704: 14003bde0
free: perlio.c:704: 140005bb0
free: perlio.c:704: 14003bd90
free: perlio.c:704: 140005b80
free: perlio.c:704: 14003bd40
free: perlio.c:704: 140005b50
free: perlio.c:564: 14003be80
free: perlio.c:590: 14003bc00
free: perlio.c:592: 140005b20
free: perlio.c:590: 14002ef60
free: perlio.c:592: 140005a60

621 allocs, 2 reallocs, and 62 frees.

Known problems:
- all memory allocs do not get logged, only those
  that go through Newx() and derivatives (while all
  Safefrees do get logged)
- __FILE__ and __LINE__ do not work everywhere.
- I think more goes on after the perlio frees but
  the thing is that STDERR gets closed (as do all
  the file descriptors)
- no deeper calling stack than the caller of the Newx
  or the kind, but do I look like a C reflection/introspection
  utility to you?
- the function prototypes for the logging functions
  probably should maybe be somewhere else than handy.h
- one could consider inlining (macrofying) the logging
  for speed, but I am too lazy
- one could imagine recording the allocations in a hash,
  (keyed by the allocation address?), and maintain that
  through reallocs and frees, but how to do that without
  any News() happening...?

Attachment: memlog.pat.gz
Description: GNU Zip compressed data

Reply via email to