2 /* -*- mode: C; c-file-style: "linux" -*- */
4 /* MemProf -- memory profiler and leak detector
5 * Copyright 1999, 2000, 2001, Red Hat, Inc.
6 * Copyright 2002, Kristian Rietveld
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 #include <sys/socket.h> /* For send() */
29 #include <sys/types.h>
32 #include "memintercept-utils.h"
42 while (written
< total
) {
43 /* Use send() to avoid EPIPE errors */
44 count
= write (fd
, buf
+ written
, total
- written
);
59 write_unsigned (int fd
,
69 write_all (fd
, "0", 1);
74 write_all (fd
, "0x", 2);
76 write_all (fd
, "0", 2);
92 buffer
[i
] = c
+ 'a' - 10;
96 write_all (fd
, buffer
, n
);
101 write_signed (int fd
,
106 write_all (fd
, "-", 1);
110 write_unsigned (fd
, num
, radix
);
122 const char *args
= NULL
;
126 while ((p
= strchr (p
, '%'))) {
129 write_all (fd
, q
, p
- q
);
143 argsize
= va_arg (va
, size_t);
146 argi
= va_arg (va
, int);
151 argp
= va_arg (va
, void *);
155 args
= va_arg (va
, const char *);
161 write_all (fd
, "%", 1);
170 write_signed (fd
, getpid(), 10);
173 write_signed (fd
, argl
, 10);
176 write_unsigned (fd
, argl
, 10);
180 write_unsigned (fd
, argl
, 16);
185 write_all (fd
, args
, strlen (args
));
194 write_all (fd
, q
, strlen (q
));
204 va_start (va
, format
);
205 mi_vprintf (fd
, format
, va
);
210 mi_debug (const char *format
,
220 fd
= open ("/dev/tty", O_WRONLY
);
225 mi_printf (fd
, "memintercept (%P): ");
227 va_start (va
, format
);
228 mi_vprintf (fd
, format
, va
);
233 mi_perror (const char *message
)
235 if (message
&& *message
)
236 mi_debug ("%s: %s (%d)\n", message
, strerror (errno
), errno
);
238 mi_debug ("%s (%d)\n", strerror (errno
), errno
);
249 while (written
< total
) {
250 /* Use send() to avoid EPIPE errors */
251 count
= send (fd
, buf
+ written
, total
- written
, MSG_NOSIGNAL
);
255 mi_debug ("mi_write error %d\n", errno
);
269 mi_atomic_increment(uint32_t *addr
)
271 #if defined (__GNUC__) && defined (__i386__)
273 __asm__
__volatile__("lock; xaddl %0, %1; incl %0"
274 : "=r"(result
), "=m"(*(addr
))
275 : "0" (1), "m"(*(addr
)) : "memory");
278 /* Hope for the best */
284 mi_atomic_decrement(uint32_t *addr
)
286 #if defined (__GNUC__) && defined (__i386__)
288 __asm__
__volatile__("lock; xaddl %0, %1; decl %0"
289 : "=r"(result
), "=m"(*(addr
))
290 : "0" (-1), "m"(*(addr
)) : "memory");
293 /* Hope for the best */