blob: 961d86c5440d06b5781d8fa6b65ff03b195b33e0 [file] [log] [blame]
sewardj1cf558c2005-04-25 01:36:56 +00001
2/*--------------------------------------------------------------------*/
3/*--- Debug (not-for-user) logging; also vprintf. m_debuglog.c ---*/
4/*--------------------------------------------------------------------*/
5
6/*
7 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
9
sewardj03f8d3f2012-08-05 15:46:46 +000010 Copyright (C) 2000-2012 Julian Seward
sewardj1cf558c2005-04-25 01:36:56 +000011 jseward@acm.org
12
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of the
16 License, or (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 02111-1307, USA.
27
28 The GNU General Public License is contained in the file COPYING.
29*/
30
31
32/* Performs low-level debug logging that can safely run immediately
33 after startup. To minimise the dependencies on any other parts of
34 the system, the only place the debug output may go is file
35 descriptor 2 (stderr).
36*/
37/* This is the first-initialised module in the entire system!
38 Therefore it is CRITICAL that it does not depend on any other code
39 running first. Hence only the following very limited includes. We
40 cannot depend (directly or indirectly) on any dynamic memory
41 allocation facilities, nor on the m_libc facilities, since the
42 latter depend on this module. DO NOT MESS WITH THESE INCLUDES
43 UNLESS YOU ARE 100% CERTAIN YOU UNDERSTAND THE CONSEQUENCES.
44*/
sewardj1cf558c2005-04-25 01:36:56 +000045
njnc7561b92005-06-19 01:24:32 +000046/* This module is also notable because it is linked into both
47 stage1 and stage2. */
48
njnf76d27a2009-05-28 01:53:07 +000049/* IMPORTANT: on Darwin it is essential to use the _nocancel versions
50 of syscalls rather than the vanilla version, if a _nocancel version
51 is available. See docs/internals/Darwin-notes.txt for the reason
52 why. */
53
njnc7561b92005-06-19 01:24:32 +000054#include "pub_core_basics.h" /* basic types */
sewardj17edf032006-10-17 01:53:34 +000055#include "pub_core_vkiscnums.h" /* for syscall numbers */
njnc7561b92005-06-19 01:24:32 +000056#include "pub_core_debuglog.h" /* our own iface */
sewardj45f4e7c2005-09-27 19:20:21 +000057#include "valgrind.h" /* for RUNNING_ON_VALGRIND */
sewardj1cf558c2005-04-25 01:36:56 +000058
bartdb4384e2011-10-11 18:49:35 +000059static Bool clo_xml;
60
61void VG_(debugLog_setXml)(Bool xml)
62{
63 clo_xml = xml;
64}
65
sewardj1cf558c2005-04-25 01:36:56 +000066/*------------------------------------------------------------*/
67/*--- Stuff to make us completely independent. ---*/
68/*------------------------------------------------------------*/
69
sewardj2c48c7b2005-11-29 13:05:56 +000070/* ----- Platform-specifics ----- */
sewardj1cf558c2005-04-25 01:36:56 +000071
sewardj21c6d0f2005-05-02 10:33:44 +000072#if defined(VGP_x86_linux)
sewardj1cf558c2005-04-25 01:36:56 +000073
floriandbb35842012-10-27 18:39:11 +000074static UInt local_sys_write_stderr ( const HChar* buf, Int n )
sewardj1cf558c2005-04-25 01:36:56 +000075{
bartdf0c09e2012-01-30 15:07:20 +000076 Int result;
77
tom311400b2005-04-27 08:58:53 +000078 __asm__ volatile (
philippe5d5dd8e2012-08-05 00:08:25 +000079 "pushl %%ebx\n"
sewardj17edf032006-10-17 01:53:34 +000080 "movl $"VG_STRINGIFY(__NR_write)", %%eax\n" /* %eax = __NR_write */
njn5ed05a52009-05-20 06:59:19 +000081 "movl $2, %%ebx\n" /* %ebx = stderr */
sewardj55c43762006-04-28 21:01:33 +000082 "int $0x80\n" /* write(stderr, buf, n) */
philippe5d5dd8e2012-08-05 00:08:25 +000083 "popl %%ebx\n"
bartdf0c09e2012-01-30 15:07:20 +000084 : /*wr*/ "=a" (result)
85 : /*rd*/ "c" (buf), "d" (n)
philippe5d5dd8e2012-08-05 00:08:25 +000086 : /*trash*/ "edi", "memory", "cc"
sewardjd4d203b2005-04-27 23:17:48 +000087 );
bartdf0c09e2012-01-30 15:07:20 +000088
89 return result >= 0 ? result : -1;
sewardj1cf558c2005-04-25 01:36:56 +000090}
91
92static UInt local_sys_getpid ( void )
93{
94 UInt __res;
tom311400b2005-04-27 08:58:53 +000095 __asm__ volatile (
sewardj17edf032006-10-17 01:53:34 +000096 "movl $"VG_STRINGIFY(__NR_getpid)", %%eax\n" /* %eax = __NR_getpid */
tom311400b2005-04-27 08:58:53 +000097 "int $0x80\n" /* getpid() */
98 "movl %%eax, %0\n" /* set __res = eax */
sewardjd4d203b2005-04-27 23:17:48 +000099 : "=mr" (__res)
100 :
101 : "eax" );
sewardj1cf558c2005-04-25 01:36:56 +0000102 return __res;
103}
104
sewardj21c6d0f2005-05-02 10:33:44 +0000105#elif defined(VGP_amd64_linux)
sewardj7337f922006-05-26 11:31:15 +0000106__attribute__((noinline))
floriandbb35842012-10-27 18:39:11 +0000107static UInt local_sys_write_stderr ( const HChar* buf, Int n )
sewardj601371a2005-04-25 16:21:17 +0000108{
sewardj17edf032006-10-17 01:53:34 +0000109 volatile Long block[2];
sewardj7337f922006-05-26 11:31:15 +0000110 block[0] = (Long)buf;
111 block[1] = n;
tomc6121862005-04-27 09:23:02 +0000112 __asm__ volatile (
sewardj7337f922006-05-26 11:31:15 +0000113 "subq $256, %%rsp\n" /* don't trash the stack redzone */
114 "pushq %%r15\n" /* r15 is callee-save */
115 "movq %0, %%r15\n" /* r15 = &block */
116 "pushq %%r15\n" /* save &block */
sewardj17edf032006-10-17 01:53:34 +0000117 "movq $"VG_STRINGIFY(__NR_write)", %%rax\n" /* rax = __NR_write */
sewardj7337f922006-05-26 11:31:15 +0000118 "movq $2, %%rdi\n" /* rdi = stderr */
119 "movq 0(%%r15), %%rsi\n" /* rsi = buf */
120 "movq 8(%%r15), %%rdx\n" /* rdx = n */
121 "syscall\n" /* write(stderr, buf, n) */
122 "popq %%r15\n" /* reestablish &block */
123 "movq %%rax, 0(%%r15)\n" /* block[0] = result */
124 "popq %%r15\n" /* restore r15 */
125 "addq $256, %%rsp\n" /* restore stack ptr */
126 : /*wr*/
sewardjf068f192011-04-26 07:52:44 +0000127 : /*rd*/ "r" (block)
sewardj7337f922006-05-26 11:31:15 +0000128 : /*trash*/ "rax", "rdi", "rsi", "rdx", "memory", "cc"
129 );
130 if (block[0] < 0)
131 block[0] = -1;
132 return (UInt)block[0];
sewardj601371a2005-04-25 16:21:17 +0000133}
134
135static UInt local_sys_getpid ( void )
136{
tomc6121862005-04-27 09:23:02 +0000137 UInt __res;
138 __asm__ volatile (
sewardj17edf032006-10-17 01:53:34 +0000139 "movq $"VG_STRINGIFY(__NR_getpid)", %%rax\n" /* %rax = __NR_getpid */
tomc6121862005-04-27 09:23:02 +0000140 "syscall\n" /* getpid() */
141 "movl %%eax, %0\n" /* set __res = %eax */
142 : "=mr" (__res)
143 :
144 : "rax" );
145 return __res;
sewardj601371a2005-04-25 16:21:17 +0000146}
sewardj1cf558c2005-04-25 01:36:56 +0000147
cerion85665ca2005-06-20 15:51:07 +0000148#elif defined(VGP_ppc32_linux)
149
floriandbb35842012-10-27 18:39:11 +0000150static UInt local_sys_write_stderr ( const HChar* buf, Int n )
cerion85665ca2005-06-20 15:51:07 +0000151{
sewardj17edf032006-10-17 01:53:34 +0000152 volatile Int block[2];
sewardja2699582006-05-22 13:04:42 +0000153 block[0] = (Int)buf;
154 block[1] = n;
cerion85665ca2005-06-20 15:51:07 +0000155 __asm__ volatile (
sewardja2699582006-05-22 13:04:42 +0000156 "addi 1,1,-256\n\t"
157 "mr 5,%0\n\t" /* r5 = &block[0] */
158 "stw 5,0(1)\n\t" /* stash on stack */
sewardj17edf032006-10-17 01:53:34 +0000159 "li 0,"VG_STRINGIFY(__NR_write)"\n\t" /* set %r0 = __NR_write */
sewardja2699582006-05-22 13:04:42 +0000160 "li 3,2\n\t" /* set %r3 = stderr */
161 "lwz 4,0(5)\n\t" /* set %r4 = buf */
162 "lwz 5,4(5)\n\t" /* set %r5 = n */
163 "sc\n\t" /* write(stderr, buf, n) */
164 "lwz 5,0(1)\n\t"
165 "addi 1,1,256\n\t"
166 "stw 3,0(5)\n" /* block[0] = result */
167 :
168 : "b" (block)
169 : "cc","memory","cr0","ctr",
170 "r0","r2","r3","r4","r5","r6","r7","r8","r9","r10","r11","r12"
171 );
172 if (block[0] < 0)
173 block[0] = -1;
174 return (UInt)block[0];
cerion85665ca2005-06-20 15:51:07 +0000175}
176
177static UInt local_sys_getpid ( void )
178{
sewardja2699582006-05-22 13:04:42 +0000179 register UInt __res __asm__ ("r3");
180 __asm__ volatile (
181 "li 0, %1\n\t"
182 "sc"
183 : "=&r" (__res)
sewardj17edf032006-10-17 01:53:34 +0000184 : "i" (__NR_getpid)
sewardja2699582006-05-22 13:04:42 +0000185 : "cc","memory","cr0","ctr",
186 "r0","r2","r4","r5","r6","r7","r8","r9","r10","r11","r12"
187 );
cerion85665ca2005-06-20 15:51:07 +0000188 return __res;
189}
190
sewardj2c48c7b2005-11-29 13:05:56 +0000191#elif defined(VGP_ppc64_linux)
192
floriandbb35842012-10-27 18:39:11 +0000193static UInt local_sys_write_stderr ( const HChar* buf, Int n )
sewardj2c48c7b2005-11-29 13:05:56 +0000194{
sewardj17edf032006-10-17 01:53:34 +0000195 volatile Long block[2];
sewardja2699582006-05-22 13:04:42 +0000196 block[0] = (Long)buf;
197 block[1] = (Long)n;
sewardj2c48c7b2005-11-29 13:05:56 +0000198 __asm__ volatile (
sewardja2699582006-05-22 13:04:42 +0000199 "addi 1,1,-256\n\t"
200 "mr 5,%0\n\t" /* r5 = &block[0] */
201 "std 5,0(1)\n\t" /* stash on stack */
sewardj17edf032006-10-17 01:53:34 +0000202 "li 0,"VG_STRINGIFY(__NR_write)"\n\t" /* %r0 = __NR_write */
sewardja2699582006-05-22 13:04:42 +0000203 "li 3,2\n\t" /* set %r3 = stderr */
204 "ld 4,0(5)\n\t" /* set %r4 = buf */
205 "ld 5,8(5)\n\t" /* set %r5 = n */
206 "sc\n\t" /* write(stderr, buf, n) */
207 "ld 5,0(1)\n\t"
208 "addi 1,1,256\n\t"
209 "std 3,0(5)\n" /* block[0] = result */
210 :
211 : "b" (block)
212 : "cc","memory","cr0","ctr",
213 "r0","r2","r3","r4","r5","r6","r7","r8","r9","r10","r11","r12"
214 );
215 if (block[0] < 0)
216 block[0] = -1;
217 return (UInt)(Int)block[0];
sewardj2c48c7b2005-11-29 13:05:56 +0000218}
219
220static UInt local_sys_getpid ( void )
221{
sewardja2699582006-05-22 13:04:42 +0000222 register ULong __res __asm__ ("r3");
223 __asm__ volatile (
224 "li 0, %1\n\t"
225 "sc"
226 : "=&r" (__res)
sewardj17edf032006-10-17 01:53:34 +0000227 : "i" (__NR_getpid)
sewardja2699582006-05-22 13:04:42 +0000228 : "cc","memory","cr0","ctr",
229 "r0","r2","r4","r5","r6","r7","r8","r9","r10","r11","r12"
230 );
231 return (UInt)__res;
sewardj2c48c7b2005-11-29 13:05:56 +0000232}
233
sewardj59570ff2010-01-01 11:59:33 +0000234#elif defined(VGP_arm_linux)
235
floriandbb35842012-10-27 18:39:11 +0000236static UInt local_sys_write_stderr ( const HChar* buf, Int n )
sewardj59570ff2010-01-01 11:59:33 +0000237{
238 volatile Int block[2];
239 block[0] = (Int)buf;
240 block[1] = n;
241 __asm__ volatile (
sewardj188a6d72010-08-26 09:40:37 +0000242 "mov r0, #2\n\t" /* stderr */
243 "ldr r1, [%0]\n\t" /* buf */
244 "ldr r2, [%0, #4]\n\t" /* n */
sewardj59570ff2010-01-01 11:59:33 +0000245 "mov r7, #"VG_STRINGIFY(__NR_write)"\n\t"
246 "svc 0x0\n" /* write() */
247 "str r0, [%0]\n\t"
248 :
249 : "r" (block)
250 : "r0","r1","r2","r7"
251 );
252 if (block[0] < 0)
253 block[0] = -1;
254 return (UInt)block[0];
255}
256
257static UInt local_sys_getpid ( void )
258{
259 UInt __res;
260 __asm__ volatile (
261 "mov r7, #"VG_STRINGIFY(__NR_getpid)"\n"
262 "svc 0x0\n" /* getpid() */
263 "mov %0, r0\n"
264 : "=r" (__res)
265 :
266 : "r0", "r7" );
267 return __res;
268}
269
njnf76d27a2009-05-28 01:53:07 +0000270#elif defined(VGP_x86_darwin)
271
njn1a1e95c2009-06-03 06:50:06 +0000272/* We would use VG_DARWIN_SYSNO_TO_KERNEL instead of VG_DARWIN_SYSNO_INDEX
273 except that the former has a C ternary ?: operator which isn't valid in
274 asm code. Both macros give the same results for Unix-class syscalls (which
275 these all are, as identified by the use of 'int 0x80'). */
njnf76d27a2009-05-28 01:53:07 +0000276__attribute__((noinline))
floriandbb35842012-10-27 18:39:11 +0000277static UInt local_sys_write_stderr ( const HChar* buf, Int n )
njnf76d27a2009-05-28 01:53:07 +0000278{
279 UInt __res;
280 __asm__ volatile (
281 "movl %2, %%eax\n" /* push n */
282 "pushl %%eax\n"
283 "movl %1, %%eax\n" /* push buf */
284 "pushl %%eax\n"
285 "movl $2, %%eax\n" /* push stderr */
286 "pushl %%eax\n"
287 "movl $"VG_STRINGIFY(VG_DARWIN_SYSNO_INDEX(__NR_write_nocancel))
288 ", %%eax\n"
289 "pushl %%eax\n" /* push fake return address */
290 "int $0x80\n" /* write(stderr, buf, n) */
291 "jnc 1f\n" /* jump if no error */
292 "movl $-1, %%eax\n" /* return -1 if error */
293 "1: "
294 "movl %%eax, %0\n" /* __res = eax */
295 "addl $16, %%esp\n" /* pop x4 */
296 : "=mr" (__res)
297 : "g" (buf), "g" (n)
298 : "eax", "edx", "cc"
299 );
300 return __res;
301}
302
303static UInt local_sys_getpid ( void )
304{
305 UInt __res;
306 __asm__ volatile (
307 "movl $"VG_STRINGIFY(VG_DARWIN_SYSNO_INDEX(__NR_getpid))", %%eax\n"
308 "int $0x80\n" /* getpid() */
309 "movl %%eax, %0\n" /* set __res = eax */
310 : "=mr" (__res)
311 :
312 : "eax", "cc" );
313 return __res;
314}
315
316#elif defined(VGP_amd64_darwin)
317
318__attribute__((noinline))
floriandbb35842012-10-27 18:39:11 +0000319static UInt local_sys_write_stderr ( const HChar* buf, Int n )
njnf76d27a2009-05-28 01:53:07 +0000320{
321 UInt __res;
322 __asm__ volatile (
323 "movq $2, %%rdi\n" /* push stderr */
324 "movq %1, %%rsi\n" /* push buf */
325 "movl %2, %%edx\n" /* push n */
njn1a1e95c2009-06-03 06:50:06 +0000326 "movl $"VG_STRINGIFY(VG_DARWIN_SYSNO_FOR_KERNEL(__NR_write_nocancel))
njnf76d27a2009-05-28 01:53:07 +0000327 ", %%eax\n"
328 "syscall\n" /* write(stderr, buf, n) */
329 "jnc 1f\n" /* jump if no error */
330 "movq $-1, %%rax\n" /* return -1 if error */
331 "1: "
332 "movl %%eax, %0\n" /* __res = eax */
333 : "=mr" (__res)
334 : "g" (buf), "g" (n)
335 : "rdi", "rsi", "rdx", "rcx", "rax", "cc" );
336 return __res;
337}
338
339static UInt local_sys_getpid ( void )
340{
341 UInt __res;
342 __asm__ volatile (
njn1a1e95c2009-06-03 06:50:06 +0000343 "movl $"VG_STRINGIFY(VG_DARWIN_SYSNO_FOR_KERNEL(__NR_getpid))", %%eax\n"
njnf76d27a2009-05-28 01:53:07 +0000344 "syscall\n" /* getpid() */
345 "movl %%eax, %0\n" /* set __res = eax */
346 : "=mr" (__res)
347 :
348 : "rax", "rcx", "cc" );
349 return __res;
350}
351
sewardjb5b87402011-03-07 16:05:35 +0000352#elif defined(VGP_s390x_linux)
floriandbb35842012-10-27 18:39:11 +0000353static UInt local_sys_write_stderr ( const HChar* buf, Int n )
sewardjb5b87402011-03-07 16:05:35 +0000354{
floriandbb35842012-10-27 18:39:11 +0000355 register Int r2 asm("2") = 2; /* file descriptor STDERR */
356 register const HChar* r3 asm("3") = buf;
357 register ULong r4 asm("4") = n;
358 register ULong r2_res asm("2");
sewardjb5b87402011-03-07 16:05:35 +0000359 ULong __res;
360
361 __asm__ __volatile__ (
362 "svc %b1\n"
363 : "=d" (r2_res)
364 : "i" (__NR_write),
365 "0" (r2),
366 "d" (r3),
367 "d" (r4)
368 : "cc", "memory");
369 __res = r2_res;
370
371 if (__res >= (ULong)(-125))
372 __res = -1;
373 return (UInt)(__res);
374}
375
376static UInt local_sys_getpid ( void )
377{
378 register ULong r2 asm("2");
379 ULong __res;
380
381 __asm__ __volatile__ (
382 "svc %b1\n"
383 : "=d" (r2)
384 : "i" (__NR_getpid)
385 : "cc", "memory");
386 __res = r2;
387
388 if (__res >= (ULong)(-125))
389 __res = -1;
390 return (UInt)(__res);
391}
392
sewardj5db15402012-06-07 09:13:21 +0000393#elif defined(VGP_mips32_linux)
floriandbb35842012-10-27 18:39:11 +0000394static UInt local_sys_write_stderr ( const HChar* buf, Int n )
sewardj5db15402012-06-07 09:13:21 +0000395{
396 volatile Int block[2];
397 block[0] = (Int)buf;
398 block[1] = n;
399 __asm__ volatile (
400 "li $4, 2\n\t" /* stderr */
401 "lw $5, 0(%0)\n\t" /* buf */
402 "lw $6, 4(%0)\n\t" /* n */
403 "move $7, $0\n\t"
404 "li $2, %1\n\t" /* set v0 = __NR_write */
405 "syscall\n\t" /* write() */
406 "nop\n\t"
407 :
408 : "r" (block), "n" (__NR_write)
409 : "2", "4", "5", "6", "7"
410 );
411 if (block[0] < 0)
412 block[0] = -1;
413 return (UInt)block[0];
414}
415
416static UInt local_sys_getpid ( void )
417{
418 UInt __res;
419 __asm__ volatile (
420 "li $2, %1\n\t" /* set v0 = __NR_getpid */
421 "syscall\n\t" /* getpid() */
422 "nop\n\t"
423 "move %0, $2\n"
424 : "=r" (__res)
425 : "n" (__NR_getpid)
426 : "$2" );
427 return __res;
428}
429
petarj4df0bfc2013-02-27 23:17:33 +0000430#elif defined(VGP_mips64_linux)
431static UInt local_sys_write_stderr ( const HChar* buf, Int n )
432{
433 volatile Long block[2];
434 block[0] = (Long)buf;
435 block[1] = n;
436 __asm__ volatile (
437 "li $4, 2\n\t" /* std output*/
438 "ld $5, 0(%0)\n\t" /*$5 = buf*/
439 "ld $6, 8(%0)\n\t" /*$6 = n */
440 "move $7, $0\n\t"
441 "li $2, %1\n\t" /* set v0 = __NR_write */
442 "\tsyscall\n"
443 "\tnop\n"
444 : /*wr*/
445 : /*rd*/ "r" (block), "n" (__NR_write)
446 : "2", "4", "5", "6", "7"
447 );
448 if (block[0] < 0)
449 block[0] = -1;
450 return (UInt)(Int)block[0];
451}
452
453static UInt local_sys_getpid ( void )
454{
455 ULong __res;
456 __asm__ volatile (
457 "li $2, %1\n\t" /* set v0 = __NR_getpid */
458 "syscall\n\t" /* getpid() */
459 "nop\n\t"
460 "move %0, $2\n"
461 : "=r" (__res)
462 : "n" (__NR_getpid)
463 : "$2" );
464 return (UInt)(__res);
465}
sewardjb5b87402011-03-07 16:05:35 +0000466
sewardj1cf558c2005-04-25 01:36:56 +0000467#else
sewardj21c6d0f2005-05-02 10:33:44 +0000468# error Unknown platform
sewardj1cf558c2005-04-25 01:36:56 +0000469#endif
470
471
472/* ----- generic ----- */
473
474/* strlen, so we don't need m_libc */
475static Int local_strlen ( const HChar* str )
476{
477 Int i = 0;
478 while (str[i] != 0) i++;
479 return i;
480}
481
482static HChar local_toupper ( HChar c )
483{
484 if (c >= 'a' && c <= 'z')
485 return c + ('A' - 'a');
486 else
487 return c;
488}
489
490/* Emit buf[0 .. n-1] to stderr. Unfortunately platform-specific.
491*/
floriandbb35842012-10-27 18:39:11 +0000492static void emit ( const HChar* buf, Int n )
sewardj1cf558c2005-04-25 01:36:56 +0000493{
494 if (n >= 1)
495 (void)local_sys_write_stderr(buf, n);
496}
497
498
499/*------------------------------------------------------------*/
500/*--- A simple, generic, vprintf implementation. ---*/
501/*------------------------------------------------------------*/
502
503/* -----------------------------------------------
504 Distantly derived from:
505
506 vprintf replacement for Checker.
507 Copyright 1993, 1994, 1995 Tristan Gingold
508 Written September 1993 Tristan Gingold
509 Tristan Gingold, 8 rue Parmentier, F-91120 PALAISEAU, FRANCE
510
511 (Checker itself was GPL'd.)
512 ----------------------------------------------- */
513
514/* Some flags. */
515#define VG_MSG_SIGNED 1 /* The value is signed. */
516#define VG_MSG_ZJUSTIFY 2 /* Must justify with '0'. */
517#define VG_MSG_LJUSTIFY 4 /* Must justify on the left. */
518#define VG_MSG_PAREN 8 /* Parenthesize if present (for %y) */
519#define VG_MSG_COMMA 16 /* Add commas to numbers (for %d, %u) */
barta0b6b2c2008-07-07 06:49:24 +0000520#define VG_MSG_ALTFORMAT 32 /* Convert the value to alternate format */
sewardj1cf558c2005-04-25 01:36:56 +0000521
522/* Copy a string into the buffer. */
523static
524UInt myvprintf_str ( void(*send)(HChar,void*),
525 void* send_arg2,
526 Int flags,
527 Int width,
floriandbb35842012-10-27 18:39:11 +0000528 const HChar* str,
sewardj1cf558c2005-04-25 01:36:56 +0000529 Bool capitalise )
530{
531# define MAYBE_TOUPPER(ch) (capitalise ? local_toupper(ch) : (ch))
532 UInt ret = 0;
533 Int i, extra;
534 Int len = local_strlen(str);
535
536 if (width == 0) {
537 ret += len;
538 for (i = 0; i < len; i++)
539 send(MAYBE_TOUPPER(str[i]), send_arg2);
540 return ret;
541 }
542
543 if (len > width) {
544 ret += width;
545 for (i = 0; i < width; i++)
546 send(MAYBE_TOUPPER(str[i]), send_arg2);
547 return ret;
548 }
549
550 extra = width - len;
551 if (flags & VG_MSG_LJUSTIFY) {
552 ret += extra;
553 for (i = 0; i < extra; i++)
554 send(' ', send_arg2);
555 }
556 ret += len;
557 for (i = 0; i < len; i++)
558 send(MAYBE_TOUPPER(str[i]), send_arg2);
559 if (!(flags & VG_MSG_LJUSTIFY)) {
560 ret += extra;
561 for (i = 0; i < extra; i++)
562 send(' ', send_arg2);
563 }
564
565# undef MAYBE_TOUPPER
566 return ret;
567}
568
569
sewardjdaf77af2005-07-19 14:17:37 +0000570/* Copy a string into the buffer, escaping bad XML chars. */
571static
572UInt myvprintf_str_XML_simplistic ( void(*send)(HChar,void*),
573 void* send_arg2,
floriandbb35842012-10-27 18:39:11 +0000574 const HChar* str )
sewardjdaf77af2005-07-19 14:17:37 +0000575{
576 UInt ret = 0;
577 Int i;
578 Int len = local_strlen(str);
floriandbb35842012-10-27 18:39:11 +0000579 const HChar* alt;
sewardjdaf77af2005-07-19 14:17:37 +0000580
581 for (i = 0; i < len; i++) {
582 switch (str[i]) {
583 case '&': alt = "&amp;"; break;
584 case '<': alt = "&lt;"; break;
585 case '>': alt = "&gt;"; break;
586 default: alt = NULL;
587 }
588
589 if (alt) {
590 while (*alt) {
591 send(*alt, send_arg2);
592 ret++;
593 alt++;
594 }
595 } else {
596 send(str[i], send_arg2);
597 ret++;
598 }
599 }
600
601 return ret;
602}
603
604
sewardj1cf558c2005-04-25 01:36:56 +0000605/* Write P into the buffer according to these args:
606 * If SIGN is true, p is a signed.
607 * BASE is the base.
608 * If WITH_ZERO is true, '0' must be added.
609 * WIDTH is the width of the field.
610 */
611static
612UInt myvprintf_int64 ( void(*send)(HChar,void*),
613 void* send_arg2,
614 Int flags,
615 Int base,
616 Int width,
sewardja44b15f2007-02-16 14:10:24 +0000617 Bool capitalised,
sewardj1cf558c2005-04-25 01:36:56 +0000618 ULong p )
619{
620 HChar buf[40];
621 Int ind = 0;
622 Int i, nc = 0;
623 Bool neg = False;
floriandbb35842012-10-27 18:39:11 +0000624 const HChar* digits = capitalised ? "0123456789ABCDEF" : "0123456789abcdef";
sewardj1cf558c2005-04-25 01:36:56 +0000625 UInt ret = 0;
626
627 if (base < 2 || base > 16)
628 return ret;
629
630 if ((flags & VG_MSG_SIGNED) && (Long)p < 0) {
631 p = - (Long)p;
632 neg = True;
633 }
634
635 if (p == 0)
636 buf[ind++] = '0';
637 else {
638 while (p > 0) {
639 if (flags & VG_MSG_COMMA && 10 == base &&
640 0 == (ind-nc) % 3 && 0 != ind)
641 {
642 buf[ind++] = ',';
643 nc++;
644 }
645 buf[ind++] = digits[p % base];
646 p /= base;
647 }
648 }
649
650 if (neg)
651 buf[ind++] = '-';
652
653 if (width > 0 && !(flags & VG_MSG_LJUSTIFY)) {
654 for(; ind < width; ind++) {
655 /* vg_assert(ind < 39); */
656 if (ind > 39) {
657 buf[39] = 0;
658 break;
659 }
660 buf[ind] = (flags & VG_MSG_ZJUSTIFY) ? '0': ' ';
661 }
662 }
663
664 /* Reverse copy to buffer. */
665 ret += ind;
666 for (i = ind -1; i >= 0; i--) {
667 send(buf[i], send_arg2);
668 }
669 if (width > 0 && (flags & VG_MSG_LJUSTIFY)) {
670 for(; ind < width; ind++) {
671 ret++;
672 /* Never pad with zeroes on RHS -- changes the value! */
673 send(' ', send_arg2);
674 }
675 }
676 return ret;
677}
678
679
680/* A simple vprintf(). */
681/* EXPORTED */
682UInt
683VG_(debugLog_vprintf) (
684 void(*send)(HChar,void*),
685 void* send_arg2,
686 const HChar* format,
687 va_list vargs
688)
689{
690 UInt ret = 0;
691 Int i;
692 Int flags;
693 Int width;
njn68e46592005-08-26 19:42:27 +0000694 Int n_ls = 0;
sewardja44b15f2007-02-16 14:10:24 +0000695 Bool is_long, caps;
sewardj1cf558c2005-04-25 01:36:56 +0000696
697 /* We assume that vargs has already been initialised by the
698 caller, using va_start, and that the caller will similarly
699 clean up with va_end.
700 */
701
702 for (i = 0; format[i] != 0; i++) {
703 if (format[i] != '%') {
704 send(format[i], send_arg2);
705 ret++;
706 continue;
707 }
708 i++;
709 /* A '%' has been found. Ignore a trailing %. */
710 if (format[i] == 0)
711 break;
712 if (format[i] == '%') {
njn02bc4b82005-05-15 17:28:26 +0000713 /* '%%' is replaced by '%'. */
sewardj1cf558c2005-04-25 01:36:56 +0000714 send('%', send_arg2);
715 ret++;
716 continue;
717 }
718 flags = 0;
njn68e46592005-08-26 19:42:27 +0000719 n_ls = 0;
sewardj1cf558c2005-04-25 01:36:56 +0000720 width = 0; /* length of the field. */
barta0b6b2c2008-07-07 06:49:24 +0000721 while (1) {
722 switch (format[i]) {
723 case '(':
724 flags |= VG_MSG_PAREN;
725 break;
726 case ',':
727 case '\'':
728 /* If ',' or '\'' follows '%', commas will be inserted. */
729 flags |= VG_MSG_COMMA;
730 break;
731 case '-':
732 /* If '-' follows '%', justify on the left. */
733 flags |= VG_MSG_LJUSTIFY;
734 break;
735 case '0':
736 /* If '0' follows '%', pads will be inserted. */
737 flags |= VG_MSG_ZJUSTIFY;
738 break;
739 case '#':
740 /* If '#' follows '%', alternative format will be used. */
741 flags |= VG_MSG_ALTFORMAT;
742 break;
743 default:
744 goto parse_fieldwidth;
745 }
sewardj1cf558c2005-04-25 01:36:56 +0000746 i++;
747 }
barta0b6b2c2008-07-07 06:49:24 +0000748 parse_fieldwidth:
sewardj1cf558c2005-04-25 01:36:56 +0000749 /* Compute the field length. */
750 while (format[i] >= '0' && format[i] <= '9') {
751 width *= 10;
752 width += format[i++] - '0';
753 }
754 while (format[i] == 'l') {
755 i++;
njn68e46592005-08-26 19:42:27 +0000756 n_ls++;
sewardj1cf558c2005-04-25 01:36:56 +0000757 }
758
njn68e46592005-08-26 19:42:27 +0000759 // %d means print a 32-bit integer.
760 // %ld means print a word-size integer.
761 // %lld means print a 64-bit integer.
762 if (0 == n_ls) { is_long = False; }
763 else if (1 == n_ls) { is_long = ( sizeof(void*) == sizeof(Long) ); }
764 else { is_long = True; }
765
sewardj1cf558c2005-04-25 01:36:56 +0000766 switch (format[i]) {
bart042257f2009-07-26 08:40:17 +0000767 case 'o': /* %o */
768 if (flags & VG_MSG_ALTFORMAT) {
769 ret += 2;
770 send('0',send_arg2);
771 }
772 if (is_long)
773 ret += myvprintf_int64(send, send_arg2, flags, 8, width, False,
774 (ULong)(va_arg (vargs, ULong)));
775 else
776 ret += myvprintf_int64(send, send_arg2, flags, 8, width, False,
777 (ULong)(va_arg (vargs, UInt)));
778 break;
sewardj1cf558c2005-04-25 01:36:56 +0000779 case 'd': /* %d */
780 flags |= VG_MSG_SIGNED;
781 if (is_long)
sewardja44b15f2007-02-16 14:10:24 +0000782 ret += myvprintf_int64(send, send_arg2, flags, 10, width, False,
sewardj1cf558c2005-04-25 01:36:56 +0000783 (ULong)(va_arg (vargs, Long)));
784 else
sewardja44b15f2007-02-16 14:10:24 +0000785 ret += myvprintf_int64(send, send_arg2, flags, 10, width, False,
sewardj1cf558c2005-04-25 01:36:56 +0000786 (ULong)(va_arg (vargs, Int)));
787 break;
788 case 'u': /* %u */
789 if (is_long)
sewardja44b15f2007-02-16 14:10:24 +0000790 ret += myvprintf_int64(send, send_arg2, flags, 10, width, False,
sewardj1cf558c2005-04-25 01:36:56 +0000791 (ULong)(va_arg (vargs, ULong)));
792 else
sewardja44b15f2007-02-16 14:10:24 +0000793 ret += myvprintf_int64(send, send_arg2, flags, 10, width, False,
sewardj1cf558c2005-04-25 01:36:56 +0000794 (ULong)(va_arg (vargs, UInt)));
795 break;
bartb3af9cf2011-10-06 19:08:37 +0000796 case 'p':
797 if (format[i+1] == 'S') {
798 i++;
799 /* %pS, like %s but escaping chars for XML safety */
800 /* Note: simplistic; ignores field width and flags */
florian6bd9dc12012-11-23 16:17:43 +0000801 const HChar *str = va_arg (vargs, HChar *);
floriandbb35842012-10-27 18:39:11 +0000802 if (str == NULL)
bartb3af9cf2011-10-06 19:08:37 +0000803 str = "(null)";
804 ret += myvprintf_str_XML_simplistic(send, send_arg2, str);
bartdb4384e2011-10-11 18:49:35 +0000805 } else if (format[i+1] == 's') {
806 i++;
807 /* %ps, synonym for %s with --xml=no / %pS with --xml=yes */
florian6bd9dc12012-11-23 16:17:43 +0000808 const HChar *str = va_arg (vargs, HChar *);
floriandbb35842012-10-27 18:39:11 +0000809 if (str == NULL)
bartdb4384e2011-10-11 18:49:35 +0000810 str = "(null)";
811 if (clo_xml)
812 ret += myvprintf_str_XML_simplistic(send, send_arg2, str);
813 else
814 ret += myvprintf_str(send, send_arg2, flags, width, str,
815 False);
bartb3af9cf2011-10-06 19:08:37 +0000816 } else {
817 /* %p */
818 ret += 2;
819 send('0',send_arg2);
820 send('x',send_arg2);
821 ret += myvprintf_int64(send, send_arg2, flags, 16, width, True,
822 (ULong)((UWord)va_arg (vargs, void *)));
823 }
sewardj1cf558c2005-04-25 01:36:56 +0000824 break;
825 case 'x': /* %x */
sewardja44b15f2007-02-16 14:10:24 +0000826 case 'X': /* %X */
827 caps = toBool(format[i] == 'X');
barta0b6b2c2008-07-07 06:49:24 +0000828 if (flags & VG_MSG_ALTFORMAT) {
829 ret += 2;
830 send('0',send_arg2);
831 send('x',send_arg2);
832 }
sewardj1cf558c2005-04-25 01:36:56 +0000833 if (is_long)
sewardja44b15f2007-02-16 14:10:24 +0000834 ret += myvprintf_int64(send, send_arg2, flags, 16, width, caps,
sewardj1cf558c2005-04-25 01:36:56 +0000835 (ULong)(va_arg (vargs, ULong)));
836 else
sewardja44b15f2007-02-16 14:10:24 +0000837 ret += myvprintf_int64(send, send_arg2, flags, 16, width, caps,
sewardj1cf558c2005-04-25 01:36:56 +0000838 (ULong)(va_arg (vargs, UInt)));
839 break;
840 case 'c': /* %c */
841 ret++;
842 send(va_arg (vargs, int), send_arg2);
843 break;
844 case 's': case 'S': { /* %s */
florian6bd9dc12012-11-23 16:17:43 +0000845 const HChar *str = va_arg (vargs, HChar *);
floriandbb35842012-10-27 18:39:11 +0000846 if (str == NULL) str = "(null)";
sewardj1cf558c2005-04-25 01:36:56 +0000847 ret += myvprintf_str(send, send_arg2,
848 flags, width, str, format[i]=='S');
849 break;
850 }
sewardjdaf77af2005-07-19 14:17:37 +0000851
sewardj1cf558c2005-04-25 01:36:56 +0000852// case 'y': { /* %y - print symbol */
florian19f91bb2012-11-10 22:29:54 +0000853// HChar buf[100];
854// HChar *cp = buf;
sewardj1cf558c2005-04-25 01:36:56 +0000855// Addr a = va_arg(vargs, Addr);
856//
857// if (flags & VG_MSG_PAREN)
858// *cp++ = '(';
859// if (VG_(get_fnname_w_offset)(a, cp, sizeof(buf)-4)) {
860// if (flags & VG_MSG_PAREN) {
861// cp += VG_(strlen)(cp);
862// *cp++ = ')';
863// *cp = '\0';
864// }
865// ret += myvprintf_str(send, send_arg2, flags, width, buf, 0);
866// }
867// break;
868// }
869 default:
870 break;
871 }
872 }
873 return ret;
874}
875
876
877/*------------------------------------------------------------*/
878/*--- Debuglog stuff. ---*/
879/*------------------------------------------------------------*/
880
881/* Only print messages whose stated level is less than or equal to
882 this. By default, it makes this entire subsystem silent. */
883
884static Int loglevel = 0;
885
sewardj1cf558c2005-04-25 01:36:56 +0000886/* Module startup. */
sewardj10759312005-05-30 23:52:47 +0000887/* EXPORTED */
floriandbb35842012-10-27 18:39:11 +0000888void VG_(debugLog_startup) ( Int level, const HChar* who )
sewardj1cf558c2005-04-25 01:36:56 +0000889{
890 if (level < 0) level = 0;
891 if (level > 10) level = 10;
892 loglevel = level;
893 VG_(debugLog)(1, "debuglog",
894 "DebugLog system started by %s, "
895 "level %d logging requested\n",
896 who, loglevel);
897}
898
sewardj10759312005-05-30 23:52:47 +0000899/* Get the logging threshold level, as set by the most recent call to
900 VG_(debugLog_startup), or zero if there have been no such calls so
901 far. */
902/* EXPORTED */
903Int VG_(debugLog_getLevel) ( void )
904{
905 return loglevel;
906}
907
908
sewardj1cf558c2005-04-25 01:36:56 +0000909/* ------------ */
910
911typedef
912 struct {
913 HChar buf[100];
914 Int n;
915 }
916 printf_buf;
917
918static void add_to_buf ( HChar c, void* p )
919{
920 printf_buf* buf = (printf_buf*)p;
921
922 if (buf->n >= 100-10 /*paranoia*/ ) {
923 emit( buf->buf, local_strlen(buf->buf) );
924 buf->n = 0;
925 buf->buf[buf->n] = 0;
926 }
927 buf->buf[buf->n++] = c;
928 buf->buf[buf->n] = 0;
929}
930
931/* Send a logging message. Nothing is output unless 'level'
932 is <= the current loglevel. */
933/* EXPORTED */
sewardj1cf558c2005-04-25 01:36:56 +0000934void VG_(debugLog) ( Int level, const HChar* modulename,
935 const HChar* format, ... )
936{
sewardjc7ffc942011-03-28 16:26:42 +0000937 UInt pid;
sewardj45f4e7c2005-09-27 19:20:21 +0000938 Int indent, depth, i;
sewardj1cf558c2005-04-25 01:36:56 +0000939 va_list vargs;
940 printf_buf buf;
sewardja5ebfa92005-04-25 02:04:54 +0000941
sewardj1cf558c2005-04-25 01:36:56 +0000942 if (level > loglevel)
943 return;
944
sewardjd85feff2005-04-25 02:37:56 +0000945 indent = 2*level - 1;
sewardja5ebfa92005-04-25 02:04:54 +0000946 if (indent < 1) indent = 1;
947
sewardj1cf558c2005-04-25 01:36:56 +0000948 buf.n = 0;
949 buf.buf[0] = 0;
950 pid = local_sys_getpid();
sewardj45f4e7c2005-09-27 19:20:21 +0000951
952 // Print one '>' in front of the messages for each level of self-hosting
953 // being performed.
954 depth = RUNNING_ON_VALGRIND;
955 for (i = 0; i < depth; i++) {
956 (void)myvprintf_str ( add_to_buf, &buf, 0, 1, ">", False );
957 }
958
sewardja5ebfa92005-04-25 02:04:54 +0000959 (void)myvprintf_str ( add_to_buf, &buf, 0, 2, "--", False );
sewardja44b15f2007-02-16 14:10:24 +0000960 (void)myvprintf_int64 ( add_to_buf, &buf, 0, 10, 1, False, (ULong)pid );
sewardja5ebfa92005-04-25 02:04:54 +0000961 (void)myvprintf_str ( add_to_buf, &buf, 0, 1, ":", False );
sewardja44b15f2007-02-16 14:10:24 +0000962 (void)myvprintf_int64 ( add_to_buf, &buf, 0, 10, 1, False, (ULong)level );
sewardja5ebfa92005-04-25 02:04:54 +0000963 (void)myvprintf_str ( add_to_buf, &buf, 0, 1, ":", False );
floriandbb35842012-10-27 18:39:11 +0000964 (void)myvprintf_str ( add_to_buf, &buf, 0, 8, modulename, False );
sewardja5ebfa92005-04-25 02:04:54 +0000965 (void)myvprintf_str ( add_to_buf, &buf, 0, indent, "", False );
sewardj1cf558c2005-04-25 01:36:56 +0000966
967 va_start(vargs,format);
968
sewardjc7ffc942011-03-28 16:26:42 +0000969 (void) VG_(debugLog_vprintf) ( add_to_buf, &buf, format, vargs );
sewardj1cf558c2005-04-25 01:36:56 +0000970
971 if (buf.n > 0) {
972 emit( buf.buf, local_strlen(buf.buf) );
973 }
974
975 va_end(vargs);
976}
977
978
979
980/*--------------------------------------------------------------------*/
981/*--- end m_debuglog.c ---*/
982/*--------------------------------------------------------------------*/