1 /* -*- mode: C; c-basic-offset: 3; -*- */
3 /*--------------------------------------------------------------------*/
4 /*--- Debug (not-for-user) logging; also vprintf. m_debuglog.c ---*/
5 /*--------------------------------------------------------------------*/
8 This file is part of Valgrind, a dynamic binary instrumentation
11 Copyright (C) 2000-2017 Julian Seward
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation; either version 2 of the
17 License, or (at your option) any later version.
19 This program is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, see <http://www.gnu.org/licenses/>.
27 The GNU General Public License is contained in the file COPYING.
31 /* Performs low-level debug logging that can safely run immediately
32 after startup. To minimise the dependencies on any other parts of
33 the system, the only place the debug output may go is file
34 descriptor 2 (stderr).
36 /* This is the first-initialised module in the entire system!
37 Therefore it is CRITICAL that it does not depend on any other code
38 running first. Hence only the following very limited includes. We
39 cannot depend (directly or indirectly) on any dynamic memory
40 allocation facilities, nor on the m_libc facilities, since the
41 latter depend on this module. DO NOT MESS WITH THESE INCLUDES
42 UNLESS YOU ARE 100% CERTAIN YOU UNDERSTAND THE CONSEQUENCES.
45 /* This module is also notable because it is linked into both
48 /* IMPORTANT: on Darwin it is essential to use the _nocancel versions
49 of syscalls rather than the vanilla version, if a _nocancel version
50 is available. See docs/internals/Darwin-notes.txt for the reason
53 #include "pub_core_basics.h" /* basic types */
54 #include "pub_core_vkiscnums.h" /* for syscall numbers */
55 #include "pub_core_debuglog.h" /* our own iface */
56 #include "pub_core_clreq.h" /* for RUNNING_ON_VALGRIND */
57 #if defined(VGO_solaris)
58 #include "pub_core_vki.h" /* for EINTR and ERESTART */
63 void VG_(debugLog_setXml
)(Bool xml
)
68 /*------------------------------------------------------------*/
69 /*--- Stuff to make us completely independent. ---*/
70 /*------------------------------------------------------------*/
72 /* ----- Platform-specifics ----- */
74 #if defined(VGP_x86_linux)
76 static UInt
local_sys_write_stderr ( const HChar
* buf
, Int n
)
82 "movl $"VG_STRINGIFY(__NR_write
)", %%eax\n" /* %eax = __NR_write */
83 "movl $2, %%ebx\n" /* %ebx = stderr */
84 "int $0x80\n" /* write(stderr, buf, n) */
86 : /*wr*/ "=a" (result
)
87 : /*rd*/ "c" (buf
), "d" (n
)
88 : /*trash*/ "edi", "memory", "cc"
91 return result
>= 0 ? result
: -1;
94 static UInt
local_sys_getpid ( void )
98 "movl $"VG_STRINGIFY(__NR_getpid
)", %%eax\n" /* %eax = __NR_getpid */
99 "int $0x80\n" /* getpid() */
100 "movl %%eax, %0\n" /* set __res = eax */
107 #elif defined(VGP_amd64_linux)
109 __attribute__((noinline
))
110 static UInt
local_sys_write_stderr ( const HChar
* buf
, Int n
)
112 volatile Long block
[2];
113 block
[0] = (Long
)buf
;
116 "subq $256, %%rsp\n" /* don't trash the stack redzone */
117 "pushq %%r15\n" /* r15 is callee-save */
118 "movq %0, %%r15\n" /* r15 = &block */
119 "pushq %%r15\n" /* save &block */
120 "movq $"VG_STRINGIFY(__NR_write
)", %%rax\n" /* rax = __NR_write */
121 "movq $2, %%rdi\n" /* rdi = stderr */
122 "movq 0(%%r15), %%rsi\n" /* rsi = buf */
123 "movq 8(%%r15), %%rdx\n" /* rdx = n */
124 "syscall\n" /* write(stderr, buf, n) */
125 "popq %%r15\n" /* reestablish &block */
126 "movq %%rax, 0(%%r15)\n" /* block[0] = result */
127 "popq %%r15\n" /* restore r15 */
128 "addq $256, %%rsp\n" /* restore stack ptr */
131 : /*trash*/ "rax", "rdi", "rsi", "rdx", "memory", "cc", "rcx", "r11"
135 return (UInt
)block
[0];
138 static UInt
local_sys_getpid ( void )
142 "movq $"VG_STRINGIFY(__NR_getpid
)", %%rax\n" /* %rax = __NR_getpid */
143 "syscall\n" /* getpid() */
144 "movl %%eax, %0\n" /* set __res = %eax */
147 : "rax", "rcx", "r11"
152 #elif defined(VGP_ppc32_linux)
154 static UInt
local_sys_write_stderr ( const HChar
* buf
, Int n
)
156 volatile Int block
[2];
161 "mr 5,%0\n\t" /* r5 = &block[0] */
162 "stw 5,0(1)\n\t" /* stash on stack */
163 "li 0,"VG_STRINGIFY(__NR_write
)"\n\t" /* set %r0 = __NR_write */
164 "li 3,2\n\t" /* set %r3 = stderr */
165 "lwz 4,0(5)\n\t" /* set %r4 = buf */
166 "lwz 5,4(5)\n\t" /* set %r5 = n */
167 "sc\n\t" /* write(stderr, buf, n) */
170 "stw 3,0(5)\n" /* block[0] = result */
173 : "cc","memory","cr0","ctr",
174 "r0","r2","r3","r4","r5","r6","r7","r8","r9","r10","r11","r12"
178 return (UInt
)block
[0];
181 static UInt
local_sys_getpid ( void )
183 register UInt __res
__asm__ ("r3");
189 : "cc","memory","cr0","ctr",
190 "r0","r2","r4","r5","r6","r7","r8","r9","r10","r11","r12"
195 #elif defined(VGP_ppc64be_linux) || defined(VGP_ppc64le_linux)
197 static UInt
local_sys_write_stderr ( const HChar
* buf
, Int n
)
199 volatile Long block
[2];
200 block
[0] = (Long
)buf
;
204 "mr 5,%0\n\t" /* r5 = &block[0] */
205 "std 5,0(1)\n\t" /* stash on stack */
206 "li 0,"VG_STRINGIFY(__NR_write
)"\n\t" /* %r0 = __NR_write */
207 "li 3,2\n\t" /* set %r3 = stderr */
208 "ld 4,0(5)\n\t" /* set %r4 = buf */
209 "ld 5,8(5)\n\t" /* set %r5 = n */
210 "sc\n\t" /* write(stderr, buf, n) */
213 "std 3,0(5)\n" /* block[0] = result */
216 : "cc","memory","cr0","ctr",
217 "r0","r3","r4","r5","r6","r7","r8","r9","r10","r11","r12"
221 return (UInt
)(Int
)block
[0];
224 static UInt
local_sys_getpid ( void )
226 register ULong __res
__asm__ ("r3");
232 : "cc","memory","cr0","ctr",
233 "r0","r4","r5","r6","r7","r8","r9","r10","r11","r12"
238 #elif defined(VGP_arm_linux)
240 static UInt
local_sys_write_stderr ( const HChar
* buf
, Int n
)
242 volatile Int block
[2];
246 "mov r0, #2\n\t" /* stderr */
247 "ldr r1, [%0]\n\t" /* buf */
248 "ldr r2, [%0, #4]\n\t" /* n */
250 "mov r7, #"VG_STRINGIFY(__NR_write
)"\n\t"
251 "svc 0x0\n" /* write() */
260 return (UInt
)block
[0];
263 static UInt
local_sys_getpid ( void )
268 "mov r7, #"VG_STRINGIFY(__NR_getpid
)"\n\t"
269 "svc 0x0\n\t" /* getpid() */
278 #elif defined(VGP_arm64_linux)
280 static UInt
local_sys_write_stderr ( const HChar
* buf
, Int n
)
282 volatile ULong block
[2];
283 block
[0] = (ULong
)buf
;
286 "mov x0, #2\n\t" /* stderr */
287 "ldr x1, [%0]\n\t" /* buf */
288 "ldr x2, [%0, #8]\n\t" /* n */
289 "mov x8, #"VG_STRINGIFY(__NR_write
)"\n\t"
290 "svc 0x0\n" /* write() */
294 : "x0","x1","x2","x7"
298 return (UInt
)block
[0];
301 static UInt
local_sys_getpid ( void )
305 "mov x8, #"VG_STRINGIFY(__NR_getpid
)"\n"
306 "svc 0x0\n" /* getpid() */
314 #elif defined(VGP_x86_darwin)
316 /* We would use VG_DARWIN_SYSNO_TO_KERNEL instead of VG_DARWIN_SYSNO_INDEX
317 except that the former has a C ternary ?: operator which isn't valid in
318 asm code. Both macros give the same results for Unix-class syscalls (which
319 these all are, as identified by the use of 'int 0x80'). */
320 __attribute__((noinline
))
321 static UInt
local_sys_write_stderr ( const HChar
* buf
, Int n
)
325 "movl %2, %%eax\n" /* push n */
327 "movl %1, %%eax\n" /* push buf */
329 "movl $2, %%eax\n" /* push stderr */
331 "movl $"VG_STRINGIFY(VG_DARWIN_SYSNO_INDEX(__NR_write_nocancel
))
333 "pushl %%eax\n" /* push fake return address */
334 "int $0x80\n" /* write(stderr, buf, n) */
335 "jnc 1f\n" /* jump if no error */
336 "movl $-1, %%eax\n" /* return -1 if error */
338 "movl %%eax, %0\n" /* __res = eax */
339 "addl $16, %%esp\n" /* pop x4 */
347 static UInt
local_sys_getpid ( void )
351 "movl $"VG_STRINGIFY(VG_DARWIN_SYSNO_INDEX(__NR_getpid
))", %%eax\n"
352 "int $0x80\n" /* getpid() */
353 "movl %%eax, %0\n" /* set __res = eax */
360 #elif defined(VGP_amd64_darwin)
362 __attribute__((noinline
))
363 static UInt
local_sys_write_stderr ( const HChar
* buf
, Int n
)
367 "movq $2, %%rdi\n" /* push stderr */
368 "movq %1, %%rsi\n" /* push buf */
369 "movl %2, %%edx\n" /* push n */
370 "movl $"VG_STRINGIFY(VG_DARWIN_SYSNO_FOR_KERNEL(__NR_write_nocancel
))
372 "syscall\n" /* write(stderr, buf, n) */
373 "jnc 1f\n" /* jump if no error */
374 "movq $-1, %%rax\n" /* return -1 if error */
376 "movl %%eax, %0\n" /* __res = eax */
379 : "rdi", "rsi", "rdx", "rcx", "rax", "cc" );
383 static UInt
local_sys_getpid ( void )
387 "movl $"VG_STRINGIFY(VG_DARWIN_SYSNO_FOR_KERNEL(__NR_getpid
))", %%eax\n"
388 "syscall\n" /* getpid() */
389 "movl %%eax, %0\n" /* set __res = eax */
392 : "rax", "rcx", "cc" );
396 #elif defined(VGP_s390x_linux)
398 static UInt
local_sys_write_stderr ( const HChar
* buf
, Int n
)
400 register Int r2
asm("2") = 2; /* file descriptor STDERR */
401 register const HChar
* r3
asm("3") = buf
;
402 register ULong r4
asm("4") = n
;
403 register ULong r2_res
asm("2");
406 __asm__
__volatile__ (
416 if (__res
>= (ULong
)(-125))
418 return (UInt
)(__res
);
421 static UInt
local_sys_getpid ( void )
423 register ULong r2
asm("2");
426 __asm__
__volatile__ (
433 if (__res
>= (ULong
)(-125))
435 return (UInt
)(__res
);
438 #elif defined(VGP_x86_freebsd)
439 static UInt
local_sys_write_stderr (const HChar
* buf
, Int n
)
444 "movl %2, %%eax\n" /* push n */
445 "movl %1, %%edx\n" /* push buf */
448 "movl $2, %%eax\n" /* push stderr */
450 "movl $"VG_STRINGIFY(__NR_write
)", %%eax\n"
451 "pushl %%eax\n" /* push write syscall id */
452 "int $0x80\n" /* write(stderr, buf, n) */
453 "jnc 1f\n" /* jump if no error */
454 "movl $-1, %%eax\n" /* return -1 if error */
456 "movl %%eax, %0\n" /* __res = eax */
457 "addl $16, %%esp\n" /* pop x4 */
458 : /*wr*/ "=mr" (result
)
459 : /*rd*/ "g" (buf
), "g" (n
)
460 : /*trash*/ "eax", "edx", "cc"
462 return result
>= 0 ? result
: -1;
465 static UInt
local_sys_getpid ( void )
469 "movl $20, %%eax\n" /* set %eax = __NR_getpid */
470 "int $0x80\n" /* getpid() */
471 "movl %%eax, %0\n" /* set __res = eax */
478 #elif defined(VGP_amd64_freebsd)
479 __attribute__((noinline
))
480 static UInt
local_sys_write_stderr (const HChar
* buf
, Int n
)
482 volatile Long block
[2];
483 block
[0] = (Long
)buf
;
486 "subq $256, %%rsp\n" /* don't trash the stack redzone */
487 "pushq %%r15\n" /* r15 is callee-save */
488 "movq %0, %%r15\n" /* r15 = &block */
489 "pushq %%r15\n" /* save &block */
490 "movq $"VG_STRINGIFY(__NR_write
)", %%rax\n" /* rax = __NR_write */
491 "movq $2, %%rdi\n" /* rdi = stderr */
492 "movq 0(%%r15), %%rsi\n" /* rsi = buf */
493 "movq 8(%%r15), %%rdx\n" /* rdx = n */
494 "syscall\n" /* write(stderr, buf, n) */
495 "popq %%r15\n" /* reestablish &block */
496 "movq %%rax, 0(%%r15)\n" /* block[0] = result */
497 "popq %%r15\n" /* restore r15 */
498 "addq $256, %%rsp\n" /* restore stack ptr */
501 : /*trash*/ "rax", "rdi", "rsi", "rdx", "memory", "cc", "rcx", "r8", "r9", "r11"
505 return (UInt
)block
[0];
508 static UInt
local_sys_getpid ( void )
512 "movq $20, %%rax\n" /* set %rax = __NR_getpid */
513 "syscall\n" /* getpid() */
514 "movl %%eax, %0\n" /* set __res = %eax */
517 : "rax", "rcx");//, "r11" );
521 #elif defined(VGP_mips32_linux) || defined(VGP_mips64_linux)
523 static UInt
local_sys_write_stderr ( const HChar
* buf
, Int n
)
525 register RegWord v0
asm("2");
526 register RegWord a0
asm("4");
527 register RegWord a1
asm("5");
528 register RegWord a2
asm("6");
531 a1
= (RegWord
)(Addr
)buf
;
535 "addiu $4, $0, -1 \n\t"
536 #if ((defined(__mips_isa_rev) && __mips_isa_rev >= 6))
537 "selnez $4, $4, $7 \n\t"
538 "seleqz $2, $2, $7 \n\t"
541 "movn $2, $4, $7 \n\t"
543 : "+d" (v0
), "+d" (a0
), "+d" (a1
), "+d" (a2
)
545 : "$1", "$3", "$7", "$8", "$9", "$10", "$11", "$12", "$13", "$14", "$15",
551 static UInt
local_sys_getpid ( void )
553 register RegWord v0
asm("2");
559 : "$1", "$3", "$4", "$5", "$6", "$7", "$8", "$9", "$10", "$11", "$12",
560 "$13", "$14", "$15", "$24", "$25", "$31"
565 #elif defined(VGP_nanomips_linux)
567 __attribute__((noinline
))
568 static UInt
local_sys_write_stderr ( const HChar
* buf
, Int n
)
570 register RegWord t4
asm("2");
571 register RegWord a0
asm("4");
572 register RegWord a1
asm("5");
573 register RegWord a2
asm("6");
576 a1
= (RegWord
)(Addr
)buf
;
580 : "+d" (t4
), "+d" (a0
), "+d" (a1
), "+d" (a2
)
582 : "$at", "$t5", "$a3", "$a4", "$a5", "$a6", "$a7", "$t0", "$t1", "$t2",
588 __attribute__((noinline
))
589 static UInt
local_sys_getpid ( void )
591 register RegWord t4
asm("2");
592 register RegWord a0
asm("4");
596 : "+d" (t4
), "=d" (a0
)
598 : "$at", "$t5", "$a1", "$a2", "$a3", "$a4", "$a5", "$a6", "$a7", "$t0",
599 "$t1", "$t2", "$t3", "$t8", "$t9"
604 #elif defined(VGP_x86_solaris)
605 static UInt
local_sys_write_stderr ( const HChar
* buf
, Int n
)
611 /* The Solaris kernel does not restart syscalls automatically so it is
613 __asm__
__volatile__ (
614 "movl %[n], %%eax\n" /* push n */
616 "movl %[buf], %%eax\n" /* push buf */
618 "movl $2, %%eax\n" /* push stderr */
620 "movl $"VG_STRINGIFY(__NR_write
)", %%eax\n"
621 "pushl %%eax\n" /* push fake return address */
622 "int $0x91\n" /* write(stderr, buf, n) */
623 "movl $0, %%edx\n" /* assume no error */
624 "jnc 1f\n" /* jump if no error */
625 "movl $1, %%edx\n" /* set error flag */
627 "addl $16, %%esp\n" /* pop x4 */
628 : "=&a" (res
), "=d" (err
)
629 : [buf
] "g" (buf
), [n
] "g" (n
)
631 restart
= err
&& (res
== VKI_EINTR
|| res
== VKI_ERESTART
);
637 static UInt
local_sys_getpid ( void )
641 /* The getpid() syscall never returns EINTR or ERESTART so there is no need
642 for restarting it. */
643 __asm__
__volatile__ (
644 "movl $"VG_STRINGIFY(__NR_getpid
)", %%eax\n"
645 "int $0x91\n" /* getpid() */
653 #elif defined(VGP_amd64_solaris)
654 static UInt
local_sys_write_stderr ( const HChar
* buf
, Int n
)
660 /* The Solaris kernel does not restart syscalls automatically so it is
662 __asm__
__volatile__ (
663 "movq $2, %%rdi\n" /* push stderr */
664 "movq $"VG_STRINGIFY(__NR_write
)", %%rax\n"
665 "syscall\n" /* write(stderr, buf, n) */
666 "movq $0, %%rdx\n" /* assume no error */
667 "jnc 1f\n" /* jump if no error */
668 "movq $1, %%rdx\n" /* set error flag */
670 : "=a" (res
), "=d" (err
)
673 restart
= err
&& (res
== VKI_EINTR
|| res
== VKI_ERESTART
);
679 static UInt
local_sys_getpid ( void )
683 /* The getpid() syscall never returns EINTR or ERESTART so there is no need
684 for restarting it. */
685 __asm__
__volatile__ (
686 "movq $"VG_STRINGIFY(__NR_getpid
)", %%rax\n"
687 "syscall\n" /* getpid() */
696 # error Unknown platform
700 /* ----- generic ----- */
702 /* strlen, so we don't need m_libc */
703 static Int
local_strlen ( const HChar
* str
)
706 while (str
[i
] != 0) i
++;
710 static HChar
local_toupper ( HChar c
)
712 if (c
>= 'a' && c
<= 'z')
713 return c
+ ('A' - 'a');
718 /* Emit buf[0 .. n-1] to stderr. Unfortunately platform-specific.
720 static void emit ( const HChar
* buf
, Int n
)
723 (void)local_sys_write_stderr(buf
, n
);
727 /*------------------------------------------------------------*/
728 /*--- A simple, generic, vprintf implementation. ---*/
729 /*------------------------------------------------------------*/
731 /* -----------------------------------------------
732 Distantly derived from:
734 vprintf replacement for Checker.
735 Copyright 1993, 1994, 1995 Tristan Gingold
736 Written September 1993 Tristan Gingold
737 Tristan Gingold, 8 rue Parmentier, F-91120 PALAISEAU, FRANCE
739 (Checker itself was GPL'd.)
740 ----------------------------------------------- */
743 #define VG_MSG_SIGNED 1 /* The value is signed. */
744 #define VG_MSG_ZJUSTIFY 2 /* Must justify with '0'. */
745 #define VG_MSG_LJUSTIFY 4 /* Must justify on the left. */
746 #define VG_MSG_PAREN 8 /* Parenthesize if present (for %y) */
747 #define VG_MSG_COMMA 16 /* Add commas to numbers (for %d, %u) */
748 #define VG_MSG_ALTFORMAT 32 /* Convert the value to alternate format */
750 /* Copy a string into the buffer. */
752 UInt
myvprintf_str ( void(*send
)(HChar
,void*),
759 # define MAYBE_TOUPPER(ch) (capitalise ? local_toupper(ch) : (ch))
762 Int len
= local_strlen(str
);
766 for (i
= 0; i
< len
; i
++)
767 send(MAYBE_TOUPPER(str
[i
]), send_arg2
);
773 for (i
= 0; i
< width
; i
++)
774 send(MAYBE_TOUPPER(str
[i
]), send_arg2
);
779 if (! (flags
& VG_MSG_LJUSTIFY
)) {
781 for (i
= 0; i
< extra
; i
++)
782 send(' ', send_arg2
);
785 for (i
= 0; i
< len
; i
++)
786 send(MAYBE_TOUPPER(str
[i
]), send_arg2
);
787 if (flags
& VG_MSG_LJUSTIFY
) {
789 for (i
= 0; i
< extra
; i
++)
790 send(' ', send_arg2
);
793 # undef MAYBE_TOUPPER
798 /* Copy a string into the buffer, escaping bad XML chars. */
800 UInt
myvprintf_str_XML_simplistic ( void(*send
)(HChar
,void*),
806 Int len
= local_strlen(str
);
809 for (i
= 0; i
< len
; i
++) {
811 case '&': alt
= "&"; break;
812 case '<': alt
= "<"; break;
813 case '>': alt
= ">"; break;
819 send(*alt
, send_arg2
);
824 send(str
[i
], send_arg2
);
833 /* Write P into the buffer according to these args:
834 * If SIGN is true, p is a signed.
836 * If WITH_ZERO is true, '0' must be added.
837 * WIDTH is the width of the field.
840 UInt
myvprintf_int64 ( void(*send
)(HChar
,void*),
848 /* To print an ULong base 2 needs 64 characters. If commas are requested,
849 add 21. Plus 1 for a possible sign plus 1 for \0. Makes 87 -- so let's
850 say 90. The size of BUF needs to be max(90, WIDTH + 1) */
851 HChar buf
[width
+ 1 > 90 ? width
+ 1 : 90];
855 const HChar
* digits
= capitalised
? "0123456789ABCDEF" : "0123456789abcdef";
858 if (base
< 2 || base
> 16)
861 if ((flags
& VG_MSG_SIGNED
) && (Long
)p
< 0) {
870 if (flags
& VG_MSG_COMMA
&& 10 == base
&&
871 0 == (ind
-nc
) % 3 && 0 != ind
)
876 buf
[ind
++] = digits
[p
% base
];
884 if (width
> 0 && !(flags
& VG_MSG_LJUSTIFY
)) {
885 for(; ind
< width
; ind
++) {
886 buf
[ind
] = (flags
& VG_MSG_ZJUSTIFY
) ? '0': ' ';
890 /* Reverse copy to buffer. */
892 for (i
= ind
-1; i
>= 0; i
--) {
893 send(buf
[i
], send_arg2
);
895 if (width
> 0 && (flags
& VG_MSG_LJUSTIFY
)) {
896 for(; ind
< width
; ind
++) {
898 /* Never pad with zeroes on RHS -- changes the value! */
899 send(' ', send_arg2
);
906 /* A simple vprintf(). */
909 VG_(debugLog_vprintf
) (
910 void(*send
)(HChar
,void*),
919 Int width
, precision
;
921 Bool is_long
, is_sizet
, caps
;
923 /* We assume that vargs has already been initialised by the
924 caller, using va_start, and that the caller will similarly
925 clean up with va_end.
928 for (i
= 0; format
[i
] != 0; i
++) {
929 if (format
[i
] != '%') {
930 send(format
[i
], send_arg2
);
935 /* A '%' has been found. Ignore a trailing %. */
938 if (format
[i
] == '%') {
939 /* '%%' is replaced by '%'. */
940 send('%', send_arg2
);
946 width
= 0; /* length of the field. */
947 precision
= -1; /* unspecified precision */
951 flags
|= VG_MSG_PAREN
;
955 /* If ',' or '\'' follows '%', commas will be inserted. */
956 flags
|= VG_MSG_COMMA
;
959 /* If '-' follows '%', justify on the left. */
960 flags
|= VG_MSG_LJUSTIFY
;
963 /* If '0' follows '%', pads will be inserted. */
964 flags
|= VG_MSG_ZJUSTIFY
;
967 /* If '#' follows '%', alternative format will be used. */
968 flags
|= VG_MSG_ALTFORMAT
;
971 goto parse_fieldwidth
;
976 /* Compute the field length. */
977 if (format
[i
] == '*') {
978 width
= va_arg(vargs
, Int
);
981 while (format
[i
] >= '0' && format
[i
] <= '9') {
983 width
+= format
[i
++] - '0';
986 /* Parse precision, if any. Only meaningful for %f. For all other
987 format specifiers the precision will be silently ignored. */
988 if (format
[i
] == '.') {
990 if (format
[i
] == '*') {
991 precision
= va_arg(vargs
, Int
);
995 while (format
[i
] >= '0' && format
[i
] <= '9') {
997 precision
+= format
[i
++] - '0';
1003 if (format
[i
] == 'z') {
1007 while (format
[i
] == 'l') {
1013 // %d means print a 32-bit integer.
1014 // %ld means print a word-size integer.
1015 // %lld means print a 64-bit integer.
1016 if (0 == n_ls
) { is_long
= False
; }
1017 else if (1 == n_ls
) { is_long
= ( sizeof(void*) == sizeof(Long
) ); }
1018 else { is_long
= True
; }
1020 switch (format
[i
]) {
1022 if (flags
& VG_MSG_ALTFORMAT
) {
1024 send('0',send_arg2
);
1027 ret
+= myvprintf_int64(send
, send_arg2
, flags
, 8, width
, False
,
1028 (ULong
)(va_arg (vargs
, SizeT
)));
1030 ret
+= myvprintf_int64(send
, send_arg2
, flags
, 8, width
, False
,
1031 (ULong
)(va_arg (vargs
, ULong
)));
1033 ret
+= myvprintf_int64(send
, send_arg2
, flags
, 8, width
, False
,
1034 (ULong
)(va_arg (vargs
, UInt
)));
1037 flags
|= VG_MSG_SIGNED
;
1039 ret
+= myvprintf_int64(send
, send_arg2
, flags
, 10, width
, False
,
1040 (ULong
)(va_arg (vargs
, Long
)));
1042 ret
+= myvprintf_int64(send
, send_arg2
, flags
, 10, width
, False
,
1043 (ULong
)(va_arg (vargs
, Int
)));
1047 ret
+= myvprintf_int64(send
, send_arg2
, flags
, 10, width
, False
,
1048 (ULong
)(va_arg (vargs
, SizeT
)));
1050 ret
+= myvprintf_int64(send
, send_arg2
, flags
, 10, width
, False
,
1051 (ULong
)(va_arg (vargs
, ULong
)));
1053 ret
+= myvprintf_int64(send
, send_arg2
, flags
, 10, width
, False
,
1054 (ULong
)(va_arg (vargs
, UInt
)));
1057 if (format
[i
+1] == 'S') {
1059 /* %pS, like %s but escaping chars for XML safety */
1060 /* Note: simplistic; ignores field width and flags */
1061 const HChar
*str
= va_arg (vargs
, HChar
*);
1064 ret
+= myvprintf_str_XML_simplistic(send
, send_arg2
, str
);
1065 } else if (format
[i
+1] == 's') {
1067 /* %ps, synonym for %s with --xml=no / %pS with --xml=yes */
1068 const HChar
*str
= va_arg (vargs
, HChar
*);
1072 ret
+= myvprintf_str_XML_simplistic(send
, send_arg2
, str
);
1074 ret
+= myvprintf_str(send
, send_arg2
, flags
, width
, str
,
1079 send('0',send_arg2
);
1080 send('x',send_arg2
);
1081 ret
+= myvprintf_int64(send
, send_arg2
, flags
, 16, width
, True
,
1082 (ULong
)((UWord
)va_arg (vargs
, void *)));
1087 caps
= toBool(format
[i
] == 'X');
1088 if (flags
& VG_MSG_ALTFORMAT
) {
1090 send('0',send_arg2
);
1091 send('x',send_arg2
);
1094 ret
+= myvprintf_int64(send
, send_arg2
, flags
, 16, width
, False
,
1095 (ULong
)(va_arg (vargs
, SizeT
)));
1097 ret
+= myvprintf_int64(send
, send_arg2
, flags
, 16, width
, caps
,
1098 (ULong
)(va_arg (vargs
, ULong
)));
1100 ret
+= myvprintf_int64(send
, send_arg2
, flags
, 16, width
, caps
,
1101 (ULong
)(va_arg (vargs
, UInt
)));
1105 send(va_arg (vargs
, int), send_arg2
);
1107 case 's': case 'S': { /* %s */
1108 const HChar
*str
= va_arg (vargs
, HChar
*);
1109 if (str
== NULL
) str
= "(null)";
1110 ret
+= myvprintf_str(send
, send_arg2
,
1111 flags
, width
, str
, format
[i
]=='S');
1115 /* Print a floating point number in the format x.y without
1116 any exponent. Capabilities are extremely limited, basically
1117 a joke, but good enough for our needs. */
1118 Double val
= va_arg (vargs
, Double
);
1119 Bool is_negative
= False
;
1126 /* If the integral part of the floating point number cannot be
1127 represented by an ULONG_MAX, print '*' characters */
1128 if (val
> (Double
)(~0ULL)) {
1129 if (width
== 0) width
= 6; // say
1130 for (cnt
= 0; cnt
< width
; ++cnt
)
1131 send('*', send_arg2
);
1135 /* The integral part of the floating point number is representable
1138 Double frac
= val
- ipval
;
1140 if (precision
== -1) precision
= 6; // say
1142 /* Silently limit the precision to 10 digits. */
1143 if (precision
> 10) precision
= 10;
1145 /* Determine fractional part, possibly round up */
1147 for (cnt
= 0; cnt
< precision
; ++cnt
)
1149 ULong frval
= frac
* factor
;
1150 if ((frac
* factor
- frval
) > 0.5) // round up
1152 /* Check rounding. */
1153 if (frval
== factor
)
1157 /* Find out how many characters are needed to print the number */
1159 /* The integral part... */
1160 UInt ipwidth
, num_digit
= 1; // at least one digit
1162 for (x
= 10; ; old_x
= x
, x
*= 10, ++num_digit
) {
1163 if (x
<= old_x
) break; // overflow occurred
1164 if (ipval
< x
) break;
1166 ipwidth
= num_digit
; // width of integral part.
1167 if (is_negative
) ++num_digit
;
1169 num_digit
+= 1 + precision
;
1173 // Fill in blanks on the left
1174 if (num_digit
< width
&& (flags
& VG_MSG_LJUSTIFY
) == 0) {
1175 for (cnt
= 0; cnt
< width
- num_digit
; ++cnt
)
1176 send(' ', send_arg2
);
1177 ret
+= width
- num_digit
;
1181 send('-', send_arg2
);
1185 ret
+= myvprintf_int64(send
, send_arg2
, 0, 10, ipwidth
, False
,
1187 // Decimal point and fractional part
1188 if (precision
!= 0) {
1189 send('.', send_arg2
);
1192 ret
+= myvprintf_int64(send
, send_arg2
, VG_MSG_ZJUSTIFY
, 10,
1193 precision
, False
, frval
);
1195 // Fill in blanks on the right
1196 if (num_digit
< width
&& (flags
& VG_MSG_LJUSTIFY
) != 0) {
1197 for (cnt
= 0; cnt
< width
- num_digit
; ++cnt
)
1198 send(' ', send_arg2
);
1199 ret
+= width
- num_digit
;
1204 // case 'y': { /* %y - print symbol */
1205 // Addr a = va_arg(vargs, Addr);
1208 // if (VG_(get_fnname_w_offset)(a, &name)) {
1209 // HChar buf[1 + VG_strlen(name) + 1 + 1];
1210 // if (flags & VG_MSG_PAREN) {
1211 // VG_(sprintf)(str, "(%s)", name):
1213 // VG_(sprintf)(str, "%s", name):
1215 // ret += myvprintf_str(send, flags, width, buf, 0);
1227 /*------------------------------------------------------------*/
1228 /*--- Debuglog stuff. ---*/
1229 /*------------------------------------------------------------*/
1231 /* Only print messages whose stated level is less than or equal to
1232 this. By default, it makes this entire subsystem silent. */
1234 static Int loglevel
= 0;
1236 /* Module startup. */
1238 void VG_(debugLog_startup
) ( Int level
, const HChar
* who
)
1240 if (level
< 0) level
= 0;
1241 if (level
> 10) level
= 10;
1243 VG_(debugLog
)(1, "debuglog",
1244 "DebugLog system started by %s, "
1245 "level %d logging requested\n",
1249 /* Get the logging threshold level, as set by the most recent call to
1250 VG_(debugLog_startup), or zero if there have been no such calls so
1253 Int
VG_(debugLog_getLevel
) ( void )
1268 static void add_to_buf ( HChar c
, void* p
)
1270 printf_buf
* buf
= (printf_buf
*)p
;
1272 if (buf
->n
>= 100-10 /*paranoia*/ ) {
1273 emit( buf
->buf
, local_strlen(buf
->buf
) );
1275 buf
->buf
[buf
->n
] = 0;
1277 buf
->buf
[buf
->n
++] = c
;
1278 buf
->buf
[buf
->n
] = 0;
1281 /* Send a logging message. Nothing is output unless 'level'
1282 is <= the current loglevel. */
1284 void VG_(debugLog
) ( Int level
, const HChar
* modulename
,
1285 const HChar
* format
, ... )
1288 Int indent
, depth
, i
;
1292 if (level
> loglevel
)
1295 indent
= 2*level
- 1;
1296 if (indent
< 1) indent
= 1;
1300 pid
= local_sys_getpid();
1302 // Print one '>' in front of the messages for each level of self-hosting
1304 depth
= RUNNING_ON_VALGRIND
;
1305 for (i
= 0; i
< depth
; i
++) {
1306 (void)myvprintf_str ( add_to_buf
, &buf
, 0, 1, ">", False
);
1309 (void)myvprintf_str ( add_to_buf
, &buf
, 0, 2, "--", False
);
1310 (void)myvprintf_int64 ( add_to_buf
, &buf
, 0, 10, 1, False
, (ULong
)pid
);
1311 (void)myvprintf_str ( add_to_buf
, &buf
, 0, 1, ":", False
);
1312 (void)myvprintf_int64 ( add_to_buf
, &buf
, 0, 10, 1, False
, (ULong
)level
);
1313 (void)myvprintf_str ( add_to_buf
, &buf
, 0, 1, ":", False
);
1314 (void)myvprintf_str ( add_to_buf
, &buf
, 0, 8, modulename
, False
);
1315 (void)myvprintf_str ( add_to_buf
, &buf
, 0, indent
, "", False
);
1317 va_start(vargs
,format
);
1319 (void) VG_(debugLog_vprintf
) ( add_to_buf
, &buf
, format
, vargs
);
1322 emit( buf
.buf
, local_strlen(buf
.buf
) );
1330 /*--------------------------------------------------------------------*/
1331 /*--- end m_debuglog.c ---*/
1332 /*--------------------------------------------------------------------*/