summaryrefslogtreecommitdiff
path: root/src/lwp.c
blob: 86f81d1ed3b4f76668890f1818bb129f4987fcbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
/*
   This file is part of odin, a memory profiler with fragmentation analysis.

   Copyright (C) 2007 Chris Wilson <chris@chris-wilson.co.uk>

   odin is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 3 of the
   License, or (at your option) any later version.

   odin is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with odin. If not, see <http://www.gnu.org/licenses/>/

   The GNU General Public License is contained in the file COPYING.
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#define _GNU_SOURCE
#define __USE_GNU

#include <dlfcn.h>
#include <unistd.h>
#include <fcntl.h>
#include <execinfo.h> /* backtrace() */
#include <link.h>
#include <sys/time.h> /* gettimeofday() */
#include <sys/socket.h>
#include <sys/un.h>
#include <pthread.h>
#include <stdio.h> /* snprintf */
#include <errno.h>

#include <glib.h> /* types and macros */

#include <stdlib.h> /* malloc() and friends */
#include <malloc.h> /* memalign() and friends */
#include <string.h> /* memcpy() */

#include "lwp-events.h"

#ifndef RTLD_NEXT
#define RTLD_NEXT ((void *) -1)
#endif

#define DLSYM_DECLARE(name) \
    static typeof (&name) name##_real
#define DLSYM_DEFINE(name) \
    name##_real = dlsym (RTLD_NEXT, #name)
#define DLCALL(name, args...) \
    (*name##_real) (args)

#define __libc_alias(N) typeof (N) __libc_##N __attribute__((alias(#N)))

#define N_EVENTS 32768

typedef struct _lwp_ip LWP_IP;
typedef struct _lwp_symbol LWP_Symbol;
typedef struct _lwp_shared_object LWP_SharedObject;

struct _lwp_shared_object {
    LWP_SharedObject *ht_next;
    ElfW (Addr) addr;

    guint key;
    const char *name;
};

struct _lwp_symbol {
    LWP_Symbol *next; /* used by lookup_symbols/free list */
    gconstpointer eip;

    guint so;
    gulong offset;
};

struct _lwp_ip {
    LWP_IP *ht_next;
    gconstpointer eip;
};

struct _lwp_allocator {
    struct _lwp_allocator *ht_next;
    gulong hash;

    guint key;
    guint n_ips;
    gpointer ips[0];
};

static pthread_mutex_t event_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
static gboolean _lwp_disable_stacktraces;
static guint _lwp_num_callers = (guint) -1;

static GSList *pending_allocators;
static LWP_Allocator *allocator_ht[31627];

static gushort n_pending_symbols;
static GSList *pending_symbols;
static LWP_Symbol *free_symbols;
static LWP_IP *ip_ht[31627];

static GSList *pending_sos;
static LWP_SharedObject *so_ht[673];

static GSList *gslist_free_list;

static int _lwp_initialized;
static int _lwp_dlcall_initialized;

static void __attribute__ ((constructor))
__lwp_init (void);

DLSYM_DECLARE (malloc);
DLSYM_DECLARE (calloc);
DLSYM_DECLARE (realloc);
DLSYM_DECLARE (valloc);
DLSYM_DECLARE (pvalloc);
DLSYM_DECLARE (memalign);
DLSYM_DECLARE (posix_memalign);
DLSYM_DECLARE (free);

DLSYM_DECLARE (dlopen);
DLSYM_DECLARE (dlclose);

static gpointer
_lwp_perm_alloc (gsize size)
{
    static gchar *ptr;
    static gsize rem;

    gpointer ret;

#define SUPERBLOCK_SIZE (1<<23)
#define align(x, y) (((x) + ((y)-1)) & ~((y)-1))

    size = align (size, 2 * sizeof (gpointer));
    if (size > rem || rem == 0) {
	ptr = DLCALL (malloc, SUPERBLOCK_SIZE);
	if (ptr == NULL)
	    exit (1);
	rem = SUPERBLOCK_SIZE;
    }

    ret = ptr;
    rem -= size;
    ptr += size;

    return ret;
}

static GSList *
_lwp_gslist_alloc (void)
{
    GSList *l;

    l = gslist_free_list;
    if (l != NULL) {
	if (l->data != NULL) {
	    GSList *ll = l->data;
	    l->data = ll->next;
	    return ll;
	} else
	    gslist_free_list = l->next;
    } else
	l = _lwp_perm_alloc (sizeof (GSList));

    return l;
}

static void
_lwp_gslist_free (GSList *list)
{
    list->data = list->next;
    list->next = gslist_free_list;
    gslist_free_list = list;
}

static GSList *
_lwp_gslist_prepend (GSList *list, gpointer data)
{
    GSList *l = _lwp_gslist_alloc ();
    l->next = list;
    l->data = data;
    return l;
}

static guint
_lwp_read_time (void)
{
    static struct timeval first;
    struct timeval tv;

    gettimeofday (&tv, NULL);

    if (! timerisset (&first))
	first = tv;

    timersub (&tv, &first, &tv);

    /* milliseconds */
    return tv.tv_usec / 1000 + tv.tv_sec * 1000;
}

static gboolean
_lwp_writen (int fd, gconstpointer _data, gsize len)
{
    const gchar *data = _data;

    while (len) {
	int ret = write (fd, data, len);
	if (ret < 0) {
	    int err = errno;
	    switch (err) {
		case EAGAIN:
		case EINTR:
		    continue;
		default:
		    return FALSE;
	    }
	}
	len -= ret;
	data += ret;
    }

    return TRUE;
}

static gboolean
_lwp_write_string (int fd, const char *str)
{
    gushort len = strlen (str);
    return _lwp_writen (fd, &len, sizeof (len)) &&
	   _lwp_writen (fd, str, len);
}

static const char *
_lwp_exe_path (void)
{
    static char path[1024];
    if (path[0] == '\0')
	readlink ("/proc/self/exe", path, sizeof (path) - 1);
    return path;
}

static guint
_lwp_shared_object_alloc (struct dl_phdr_info *info)
{
    static guint so_key;
    LWP_SharedObject *so;
    guint index;

    so = _lwp_perm_alloc (sizeof (LWP_SharedObject));

    so->key = ++so_key;
    so->addr = info->dlpi_addr;
    so->name = info->dlpi_name;
    if (so->name == NULL || *so->name == '\0')
	so->name = _lwp_exe_path ();

    index = so->addr % G_N_ELEMENTS (so_ht);
    so->ht_next = so_ht[index];
    so_ht[index] = so;

    pending_sos = _lwp_gslist_prepend (pending_sos, so);

    return so->key;
}

static guint
_lwp_add_shared_object (struct dl_phdr_info *info)
{
    LWP_SharedObject *so;
    guint index;

    index = info->dlpi_addr % G_N_ELEMENTS (so_ht);
    for (so = so_ht[index]; so != NULL; so = so->ht_next) {
	if (so->addr == info->dlpi_addr)
	    return so->key;
    }

    return _lwp_shared_object_alloc (info);
}

static int
find_matching_so (struct dl_phdr_info *info, size_t size, void *data)
{
    LWP_Symbol **symbols = data;
    long n;
    const ElfW (Phdr) *phdr;
    ElfW (Addr) load_base = info->dlpi_addr;
    guint key = 0;

    phdr = info->dlpi_phdr;
    for (n = info->dlpi_phnum; --n >= 0; phdr++) {
	if (phdr->p_type == PT_LOAD) {
	    ElfW (Addr) vaddr = phdr->p_vaddr + load_base;
	    LWP_Symbol **prev, *next, *symbol;

	    prev = symbols;
	    for (symbol = *symbols; symbol != NULL; symbol = next) {
		ElfW (Addr) addr = (ElfW (Addr)) symbol->eip - vaddr;

		next = symbol->next;

		if (addr < phdr->p_memsz) {
		    *prev = symbol->next;

		    if (key == 0)
			key = _lwp_add_shared_object (info);
		    symbol->so = key;
		    symbol->offset = addr;
		} else
		    prev = &symbol->next;
	    }
	    *prev = NULL;
	    if (prev == symbols)
		return 1;
	}
    }

    return 0;
}

static void
_lwp_lookup_so (LWP_Symbol *symbols)
{
    dl_iterate_phdr (find_matching_so, &symbols);
}

static gboolean
_lwp_write_events (const LWP_EventRecord *events, gushort n_events)
{
    int fd;
    guint32 time;
    pid_t pid;
    gushort count;
    const char *env;
    GSList *l;

    env = getenv ("LWP_PATH");
    if (env == NULL)
	return FALSE;

    if (strncmp (env, "unix:", 5) == 0) {
	struct sockaddr_un addr;

	if (strlen (env + 5) >= sizeof (addr.sun_path))
	    return FALSE;

	fd = socket (PF_UNIX, SOCK_STREAM, 0);
	if (fd <0)
	    return FALSE;

	memset (&addr, 0, sizeof (addr));
	addr.sun_family = AF_UNIX;
	strcpy (addr.sun_path, env + 5);
	if (connect (fd, &addr, sizeof (addr)) < 0)
	    goto CLEAN_FD;
    } else {
	char filename[1024];
	snprintf (filename, sizeof (filename), "%s.%d", env, getpid ());
	fd = open (filename, O_WRONLY|O_CREAT|O_APPEND|O_LARGEFILE, 0666);
	if (fd < 0)
	    return FALSE;
    }

    if (! _lwp_writen (fd, "LWP", 4))
	goto CLEAN_FD;

    pid = getpid();
    if (! _lwp_writen (fd, &pid, sizeof (pid)))
	goto CLEAN_FD;

    time = _lwp_read_time ();
    if (! _lwp_writen (fd, &time, sizeof (time)))
	goto CLEAN_FD;

    if (n_pending_symbols) {
	_lwp_lookup_so (pending_symbols->data);

	/* send any new objects */
	count = 0;
	for (l = pending_sos; l != NULL; l = l->next)
	    count++;

	if (! _lwp_writen (fd, &count, sizeof (count)))
	    goto CLEAN_FD;

	for (l = pending_sos; l != NULL; l = l->next) {
	    LWP_SharedObject *so = l->data;
	    gboolean ok = TRUE;

	    ok &= _lwp_writen (fd, &so->key, sizeof (so->key));
	    ok &= _lwp_write_string (fd, so->name);

	    if (!ok)
		goto CLEAN_FD;
	}

	/* send any new symbols */
	count = n_pending_symbols;
	n_pending_symbols = 0;
	if (! _lwp_writen (fd, &count, sizeof (count)))
	    goto CLEAN_FD;

	for (l = pending_symbols; l != NULL; l = l->next) {
	    LWP_Symbol *symbol = l->data;
	    gboolean ok = TRUE;

	    ok &= _lwp_writen (fd, &symbol->eip, sizeof (symbol->eip));
	    ok &= _lwp_writen (fd, &symbol->so, sizeof (symbol->so));
	    ok &= _lwp_writen (fd, &symbol->offset, sizeof (symbol->offset));

	    symbol->next = free_symbols;
	    free_symbols = symbol;

	    if (!ok)
		goto CLEAN_FD;
	}
    } else {
	/* no new symbols or objects */
	count = 0;
	if (! _lwp_writen (fd, &count, sizeof (count)) ||
	    ! _lwp_writen (fd, &count, sizeof (count)))
	{
	    goto CLEAN_FD;
	}
    }

    /* send any new allocation sites */
    count = 0;
    for (l = pending_allocators; l != NULL; l = l->next)
	count++;

    if (! _lwp_writen (fd, &count, sizeof (count)))
	goto CLEAN_FD;

    for (l = pending_allocators; l != NULL; l = l->next) {
	LWP_Allocator *A = l->data;

	if (! _lwp_writen (fd, &A->key, sizeof (A->key)) ||
	    ! _lwp_writen (fd, &A->n_ips, sizeof (A->n_ips)) ||
	    ! _lwp_writen (fd, A->ips, sizeof (A->ips[0]) * A->n_ips))
	{
	    goto CLEAN_FD;
	}
    }

    /* send all the recent events */
    if (! _lwp_writen (fd, &n_events, sizeof (n_events)))
	goto CLEAN_FD;

    for (count = 0; count < n_events; count++) {
	const LWP_Event *ev = &events[count].event;
	char c = events[count].type;

	if (! _lwp_writen (fd, &c, 1) ||
	    ! _lwp_writen (fd, &events[count].time, sizeof (events[count].time)) ||
	    ! _lwp_writen (fd, &events[count].allocator, sizeof (events[count].allocator)))
	{
	    goto CLEAN_FD;
	}

	switch (events[count].type) {
	case LWP_INIT:
	case LWP_FINI:
	case LWP_DLOPEN:
	case LWP_DLCLOSE:
	    break;

	case LWP_MALLOC:
	    if (! _lwp_writen (fd, &ev->malloc.size, sizeof (ev->malloc.size)) ||
	        ! _lwp_writen (fd, &ev->malloc.addr, sizeof (ev->malloc.addr)))
	    {
		goto CLEAN_FD;
	    }
	    break;

	case LWP_MEMALIGN:
	    if (! _lwp_writen (fd, &ev->memalign.align, sizeof (ev->memalign.align)) ||
	        ! _lwp_writen (fd, &ev->memalign.size, sizeof (ev->memalign.size)) ||
	        ! _lwp_writen (fd, &ev->memalign.addr, sizeof (ev->memalign.addr)))
	    {
		goto CLEAN_FD;
	    }
	    break;

	case LWP_REALLOC:
	    if (! _lwp_writen (fd, &ev->realloc.size, sizeof (ev->realloc.size)) ||
	        ! _lwp_writen (fd, &ev->realloc.old_addr, sizeof (ev->realloc.old_addr)) ||
	        ! _lwp_writen (fd, &ev->realloc.new_addr, sizeof (ev->realloc.new_addr)))
	    {
		goto CLEAN_FD;
	    }
	    break;

	case LWP_FREE:
	    if (! _lwp_writen (fd, &ev->free.addr, sizeof (ev->free.addr)))
	    {
		goto CLEAN_FD;
	    }
	    break;
	}
    }

    if (pending_sos != NULL) {
	_lwp_gslist_free (pending_sos);
	pending_sos = NULL;
    }
    if (pending_symbols != NULL) {
	_lwp_gslist_free (pending_symbols);
	pending_symbols = NULL;
    }
    if (pending_allocators != NULL) {
	_lwp_gslist_free (pending_allocators);
	pending_allocators = NULL;
    }

    return close (fd) != -1;

CLEAN_FD:
    close (fd);
    return FALSE;
}

static void
_lwp_queue_lookup_symbol (gconstpointer eip)
{
    LWP_Symbol *symbol;

    symbol = free_symbols;
    if (symbol != NULL)
	free_symbols = symbol->next;
    else
	symbol = _lwp_perm_alloc (sizeof (LWP_Symbol));

    if (symbol != NULL) {
	symbol->eip = eip;

	symbol->so = 0;
	symbol->offset = 0;

	symbol->next = pending_symbols ? pending_symbols->data : NULL;
	pending_symbols = _lwp_gslist_prepend (pending_symbols, symbol);
	n_pending_symbols++;
    }
}

static void
_lwp_add_ip (gconstpointer eip)
{
    guint index = ((gulong) eip ^ 1215497) % G_N_ELEMENTS (ip_ht);
    LWP_IP *ip;

    /* XXX track mmap for invalidation of addresses */

    for (ip = ip_ht[index]; ip != NULL; ip = ip->ht_next)
	if (ip->eip == eip)
	    break;

    if (ip != NULL)
	return;

    /* unknown eip, lookup and add to hash table */
    ip = _lwp_perm_alloc (sizeof (LWP_IP));
    ip->eip = eip;

    ip->ht_next = ip_ht[index];
    ip_ht[index] = ip;

    _lwp_queue_lookup_symbol (eip);
}


static guint
_lwp_allocator_alloc (gpointer *ips, guint n_ips, gulong hash)
{
    static guint allocator_key;

    LWP_Allocator *A;
    guint n, index;

    A = _lwp_perm_alloc (sizeof (LWP_Allocator) + sizeof(gpointer) * n_ips);

    A->key = ++allocator_key;
    A->hash = hash;
    A->n_ips = n_ips;
    memcpy (A->ips, ips, sizeof (gpointer) * n_ips);

    index = hash % G_N_ELEMENTS (allocator_ht);
    A->ht_next = allocator_ht[index];
    allocator_ht[index] = A;

    pending_allocators = _lwp_gslist_prepend (pending_allocators, A);

    /* add to symbol translation ht */
    for (n = 0; n < n_ips; n++)
	_lwp_add_ip (ips[n]);

    return A->key;
}

static gulong
_lwp_allocator_hash (gpointer *ips, guint n_ips)
{
    gulong hash = 1215497;
    guint n;

    hash ^= n_ips;
    for (n = 0; n < n_ips; n++)
	hash ^= (gulong) ips[n];

    return hash;
}

static gboolean
_lwp_allocator_equal (const LWP_Allocator *A,
	              gpointer *ips,
		      guint n_ips,
		      gulong hash)
{
    guint n;

    if (A->hash != hash)
	return FALSE;

    if (A->n_ips != n_ips)
	return FALSE;

    for (n = 0; n < n_ips; n++)
	if (A->ips[n] != ips[n])
	    return FALSE;

    return TRUE;
}

static guint
_lwp_add_allocator (gpointer *ips, guint n_ips)
{
    LWP_Allocator *A;
    gulong hash;
    guint index;

    hash = _lwp_allocator_hash (ips, n_ips);
    index = hash % G_N_ELEMENTS (allocator_ht);
    for (A = allocator_ht[index]; A != NULL; A = A->ht_next) {
	if (_lwp_allocator_equal (A, ips, n_ips, hash))
	    return A->key;
    }

    return _lwp_allocator_alloc (ips, n_ips, hash);
}

static void
_lwp_record_event (LWP_EventType type, const LWP_Event *ev)
{
    static LWP_EventRecord events[N_EVENTS];
    static guint n_events;
    static gboolean _write_failed;
    static __thread guint depth;

    gboolean lookup_allocator = TRUE;
    gboolean force_send = FALSE;

    switch (type) {
    case LWP_INIT:
	force_send = TRUE;
	lookup_allocator = FALSE;
	break;

    case LWP_FINI:
	force_send = TRUE;
	lookup_allocator = FALSE;
	break;

    case LWP_DLOPEN:
	lookup_allocator = FALSE;
	break;

    case LWP_DLCLOSE:
	force_send = TRUE;
	lookup_allocator = FALSE;
	break;

    case LWP_MALLOC:
    case LWP_REALLOC:
    case LWP_MEMALIGN:
    case LWP_FREE:
    default:
	break;
    }

    pthread_mutex_lock (&event_mutex);
    if (depth++ || _write_failed)
	goto unlock;

    if (! lookup_allocator) {
	events[n_events].allocator = 0;
    } else if (! _lwp_disable_stacktraces) {
	gpointer stack_ips[1024 / sizeof (gpointer)], *ips;
	guint n_ips, max_ips;

	max_ips = G_N_ELEMENTS (stack_ips);
	if (max_ips > _lwp_num_callers)
	    max_ips = _lwp_num_callers;

	/* drop the mutex to avoid a deadlock with dl */
	pthread_mutex_unlock (&event_mutex);

	ips = stack_ips;
	n_ips = backtrace (ips, max_ips);
	while (n_ips == max_ips && max_ips < _lwp_num_callers) {
	    max_ips *= 2;
	    if (max_ips > _lwp_num_callers)
		max_ips = _lwp_num_callers;

	    ips = g_newa (gpointer, max_ips);
	    if (ips == NULL)
		goto unlock;
	    n_ips = backtrace (ips, max_ips);
	}

	pthread_mutex_lock (&event_mutex);
	if (_write_failed) /* need to recheck after acquiring mutex */
	    goto unlock;

	if (G_UNLIKELY (n_ips + n_pending_symbols > G_MAXUSHORT)) {
	    _write_failed = ! _lwp_write_events (events, n_events);
	    n_events = 0;

	    if (_write_failed)
		goto unlock;
	}

	events[n_events].allocator = _lwp_add_allocator (ips+1, n_ips-1);
    } else {
	gpointer caller;
	switch (type) {
	default:
	case LWP_INIT:
	case LWP_FINI:
	case LWP_DLOPEN:
	case LWP_DLCLOSE:
	case LWP_MALLOC:   caller = malloc;   break;
	case LWP_MEMALIGN: caller = memalign; break;
	case LWP_REALLOC:  caller = realloc;  break;
	case LWP_FREE:     caller = free;     break;
	}
	events[n_events].allocator = _lwp_add_allocator (&caller, 1);
    }

    events[n_events].type = type;
    events[n_events].time = _lwp_read_time ();
    events[n_events].event = *ev;
    if (G_UNLIKELY (++n_events == G_N_ELEMENTS (events) || force_send)) {
	_write_failed = ! _lwp_write_events (events, n_events);
	n_events = 0;
    }

unlock:
    --depth;
    pthread_mutex_unlock (&event_mutex);
}

void *
malloc (size_t size)
{
    void *ret;
    LWP_Event event;

    if (_lwp_initialized == 0)
	__lwp_init ();

    if (! _lwp_dlcall_initialized)
	return NULL;

    ret = DLCALL (malloc, size);

    event.malloc.size = size;
    event.malloc.addr = ret;
    _lwp_record_event (LWP_MALLOC, &event);

    return ret;
}
__libc_alias (malloc);

void *
calloc (size_t n, size_t size)
{
    void *ret;
    LWP_Event event;

    if (_lwp_initialized == 0)
	__lwp_init ();

    if (! _lwp_dlcall_initialized)
	return NULL;

    ret = DLCALL (calloc, n, size);

    /* XXX differentiate? */
    event.malloc.size = n * size;
    event.malloc.addr = ret;
    _lwp_record_event (LWP_MALLOC, &event);

    return ret;
}
__libc_alias (calloc);

void *
valloc (size_t size)
{
    void *ret;
    LWP_Event event;

    if (! _lwp_dlcall_initialized)
	return NULL;

    ret = DLCALL (valloc, size);

    /* XXX differentiate? */
    event.memalign.align = sysconf (_SC_PAGESIZE);
    event.memalign.size = size;
    event.memalign.addr = ret;
    _lwp_record_event (LWP_MEMALIGN, &event);

    return ret;
}
__libc_alias (valloc);

void *
pvalloc (size_t size)
{
    void *ret;
    LWP_Event event;

    if (! _lwp_dlcall_initialized)
	return NULL;

    ret = DLCALL (pvalloc, size);

    /* XXX differentiate? */
    event.memalign.align = sysconf (_SC_PAGESIZE);
    event.memalign.size = size;
    event.memalign.addr = ret;
    _lwp_record_event (LWP_MEMALIGN, &event);

    return ret;
}
__libc_alias (pvalloc);

void *
memalign (size_t boundary, size_t size)
{
    void *ret;
    LWP_Event event;

    if (! _lwp_dlcall_initialized)
	return NULL;

    ret = DLCALL (memalign, boundary, size);

    event.memalign.align = boundary;
    event.memalign.size = size;
    event.memalign.addr = ret;
    _lwp_record_event (LWP_MEMALIGN, &event);

    return ret;
}

int
posix_memalign (void **ptr, size_t alignment, size_t size)
{
    int ret;
    LWP_Event event;

    if (! _lwp_dlcall_initialized)
	return 0;

    ret = DLCALL (posix_memalign, ptr, alignment, size);

    event.memalign.align = alignment;
    event.memalign.size = size;
    event.memalign.addr = *ptr;
    _lwp_record_event (LWP_MEMALIGN, &event);

    return ret;
}

void *
realloc (void *ptr, size_t size)
{
    void *ret;
    LWP_Event event;

    if (_lwp_initialized == 0)
	__lwp_init ();

    if (! _lwp_dlcall_initialized)
	return NULL;

    ret = DLCALL (realloc, ptr, size);

    event.realloc.size = size;
    event.realloc.old_addr = ptr;
    event.realloc.new_addr = ret;
    _lwp_record_event (LWP_REALLOC, &event);

    return ret;
}
__libc_alias (realloc);

void
free (void *ptr)
{
    LWP_Event event;

    if (! _lwp_dlcall_initialized)
	return;

    DLCALL (free, ptr);

    event.free.addr = ptr;
    _lwp_record_event (LWP_FREE, &event);
}

void *
dlopen (const char *filename, int flag)
{
    LWP_Event event;
    void *ret;

    if (! _lwp_dlcall_initialized)
	return NULL;

    ret = DLCALL (dlopen, filename, flag);

    _lwp_record_event (LWP_DLOPEN, &event);

    return ret;
}

int
dlclose (void *handle)
{
    LWP_Event event;
    int ret;

    if (! _lwp_dlcall_initialized)
	return -1;

    _lwp_record_event (LWP_DLCLOSE, &event);

    ret = DLCALL (dlclose, handle);

    return ret;
}


/* XXX __builtin_new */
static void __attribute__ ((constructor))
__lwp_init (void)
{
    const char *env;
    LWP_Event event;

    if (_lwp_initialized++)
	return;

    env = getenv ("LWP_DISABLE_STACKTRACES");
    _lwp_disable_stacktraces = env != NULL;

    env = getenv ("LWP_NUM_CALLERS");
    if (env != NULL) {
	_lwp_num_callers = strtoul (env, NULL, 0);
	if (_lwp_num_callers == 0)
	    _lwp_disable_stacktraces = TRUE;
    }

    DLSYM_DEFINE (malloc);
    DLSYM_DEFINE (calloc);
    DLSYM_DEFINE (realloc);
    DLSYM_DEFINE (valloc);
    DLSYM_DEFINE (pvalloc);
    DLSYM_DEFINE (memalign);
    DLSYM_DEFINE (posix_memalign);
    DLSYM_DEFINE (free);

    DLSYM_DEFINE (dlopen);
    DLSYM_DEFINE (dlclose);

    _lwp_perm_alloc (0); /* grab our buffer first */

    _lwp_dlcall_initialized = TRUE;

    _lwp_record_event (LWP_INIT, &event);
}

static void __attribute__ ((destructor))
__lwp_fini (void)
{
    LWP_Event event;

    _lwp_record_event (LWP_FINI, &event);
}