blob: 37eb7a5c6e1f08e9ccfd7e96bb4a82286fe60c75 [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
sewardjb5b87402011-03-07 16:05:35 +0000430
sewardj1cf558c2005-04-25 01:36:56 +0000431#else
sewardj21c6d0f2005-05-02 10:33:44 +0000432# error Unknown platform
sewardj1cf558c2005-04-25 01:36:56 +0000433#endif
434
435
436/* ----- generic ----- */
437
438/* strlen, so we don't need m_libc */
439static Int local_strlen ( const HChar* str )
440{
441 Int i = 0;
442 while (str[i] != 0) i++;
443 return i;
444}
445
446static HChar local_toupper ( HChar c )
447{
448 if (c >= 'a' && c <= 'z')
449 return c + ('A' - 'a');
450 else
451 return c;
452}
453
454/* Emit buf[0 .. n-1] to stderr. Unfortunately platform-specific.
455*/
floriandbb35842012-10-27 18:39:11 +0000456static void emit ( const HChar* buf, Int n )
sewardj1cf558c2005-04-25 01:36:56 +0000457{
458 if (n >= 1)
459 (void)local_sys_write_stderr(buf, n);
460}
461
462
463/*------------------------------------------------------------*/
464/*--- A simple, generic, vprintf implementation. ---*/
465/*------------------------------------------------------------*/
466
467/* -----------------------------------------------
468 Distantly derived from:
469
470 vprintf replacement for Checker.
471 Copyright 1993, 1994, 1995 Tristan Gingold
472 Written September 1993 Tristan Gingold
473 Tristan Gingold, 8 rue Parmentier, F-91120 PALAISEAU, FRANCE
474
475 (Checker itself was GPL'd.)
476 ----------------------------------------------- */
477
478/* Some flags. */
479#define VG_MSG_SIGNED 1 /* The value is signed. */
480#define VG_MSG_ZJUSTIFY 2 /* Must justify with '0'. */
481#define VG_MSG_LJUSTIFY 4 /* Must justify on the left. */
482#define VG_MSG_PAREN 8 /* Parenthesize if present (for %y) */
483#define VG_MSG_COMMA 16 /* Add commas to numbers (for %d, %u) */
barta0b6b2c2008-07-07 06:49:24 +0000484#define VG_MSG_ALTFORMAT 32 /* Convert the value to alternate format */
sewardj1cf558c2005-04-25 01:36:56 +0000485
486/* Copy a string into the buffer. */
487static
488UInt myvprintf_str ( void(*send)(HChar,void*),
489 void* send_arg2,
490 Int flags,
491 Int width,
floriandbb35842012-10-27 18:39:11 +0000492 const HChar* str,
sewardj1cf558c2005-04-25 01:36:56 +0000493 Bool capitalise )
494{
495# define MAYBE_TOUPPER(ch) (capitalise ? local_toupper(ch) : (ch))
496 UInt ret = 0;
497 Int i, extra;
498 Int len = local_strlen(str);
499
500 if (width == 0) {
501 ret += len;
502 for (i = 0; i < len; i++)
503 send(MAYBE_TOUPPER(str[i]), send_arg2);
504 return ret;
505 }
506
507 if (len > width) {
508 ret += width;
509 for (i = 0; i < width; i++)
510 send(MAYBE_TOUPPER(str[i]), send_arg2);
511 return ret;
512 }
513
514 extra = width - len;
515 if (flags & VG_MSG_LJUSTIFY) {
516 ret += extra;
517 for (i = 0; i < extra; i++)
518 send(' ', send_arg2);
519 }
520 ret += len;
521 for (i = 0; i < len; i++)
522 send(MAYBE_TOUPPER(str[i]), send_arg2);
523 if (!(flags & VG_MSG_LJUSTIFY)) {
524 ret += extra;
525 for (i = 0; i < extra; i++)
526 send(' ', send_arg2);
527 }
528
529# undef MAYBE_TOUPPER
530 return ret;
531}
532
533
sewardjdaf77af2005-07-19 14:17:37 +0000534/* Copy a string into the buffer, escaping bad XML chars. */
535static
536UInt myvprintf_str_XML_simplistic ( void(*send)(HChar,void*),
537 void* send_arg2,
floriandbb35842012-10-27 18:39:11 +0000538 const HChar* str )
sewardjdaf77af2005-07-19 14:17:37 +0000539{
540 UInt ret = 0;
541 Int i;
542 Int len = local_strlen(str);
floriandbb35842012-10-27 18:39:11 +0000543 const HChar* alt;
sewardjdaf77af2005-07-19 14:17:37 +0000544
545 for (i = 0; i < len; i++) {
546 switch (str[i]) {
547 case '&': alt = "&amp;"; break;
548 case '<': alt = "&lt;"; break;
549 case '>': alt = "&gt;"; break;
550 default: alt = NULL;
551 }
552
553 if (alt) {
554 while (*alt) {
555 send(*alt, send_arg2);
556 ret++;
557 alt++;
558 }
559 } else {
560 send(str[i], send_arg2);
561 ret++;
562 }
563 }
564
565 return ret;
566}
567
568
sewardj1cf558c2005-04-25 01:36:56 +0000569/* Write P into the buffer according to these args:
570 * If SIGN is true, p is a signed.
571 * BASE is the base.
572 * If WITH_ZERO is true, '0' must be added.
573 * WIDTH is the width of the field.
574 */
575static
576UInt myvprintf_int64 ( void(*send)(HChar,void*),
577 void* send_arg2,
578 Int flags,
579 Int base,
580 Int width,
sewardja44b15f2007-02-16 14:10:24 +0000581 Bool capitalised,
sewardj1cf558c2005-04-25 01:36:56 +0000582 ULong p )
583{
584 HChar buf[40];
585 Int ind = 0;
586 Int i, nc = 0;
587 Bool neg = False;
floriandbb35842012-10-27 18:39:11 +0000588 const HChar* digits = capitalised ? "0123456789ABCDEF" : "0123456789abcdef";
sewardj1cf558c2005-04-25 01:36:56 +0000589 UInt ret = 0;
590
591 if (base < 2 || base > 16)
592 return ret;
593
594 if ((flags & VG_MSG_SIGNED) && (Long)p < 0) {
595 p = - (Long)p;
596 neg = True;
597 }
598
599 if (p == 0)
600 buf[ind++] = '0';
601 else {
602 while (p > 0) {
603 if (flags & VG_MSG_COMMA && 10 == base &&
604 0 == (ind-nc) % 3 && 0 != ind)
605 {
606 buf[ind++] = ',';
607 nc++;
608 }
609 buf[ind++] = digits[p % base];
610 p /= base;
611 }
612 }
613
614 if (neg)
615 buf[ind++] = '-';
616
617 if (width > 0 && !(flags & VG_MSG_LJUSTIFY)) {
618 for(; ind < width; ind++) {
619 /* vg_assert(ind < 39); */
620 if (ind > 39) {
621 buf[39] = 0;
622 break;
623 }
624 buf[ind] = (flags & VG_MSG_ZJUSTIFY) ? '0': ' ';
625 }
626 }
627
628 /* Reverse copy to buffer. */
629 ret += ind;
630 for (i = ind -1; i >= 0; i--) {
631 send(buf[i], send_arg2);
632 }
633 if (width > 0 && (flags & VG_MSG_LJUSTIFY)) {
634 for(; ind < width; ind++) {
635 ret++;
636 /* Never pad with zeroes on RHS -- changes the value! */
637 send(' ', send_arg2);
638 }
639 }
640 return ret;
641}
642
643
644/* A simple vprintf(). */
645/* EXPORTED */
646UInt
647VG_(debugLog_vprintf) (
648 void(*send)(HChar,void*),
649 void* send_arg2,
650 const HChar* format,
651 va_list vargs
652)
653{
654 UInt ret = 0;
655 Int i;
656 Int flags;
657 Int width;
njn68e46592005-08-26 19:42:27 +0000658 Int n_ls = 0;
sewardja44b15f2007-02-16 14:10:24 +0000659 Bool is_long, caps;
sewardj1cf558c2005-04-25 01:36:56 +0000660
661 /* We assume that vargs has already been initialised by the
662 caller, using va_start, and that the caller will similarly
663 clean up with va_end.
664 */
665
666 for (i = 0; format[i] != 0; i++) {
667 if (format[i] != '%') {
668 send(format[i], send_arg2);
669 ret++;
670 continue;
671 }
672 i++;
673 /* A '%' has been found. Ignore a trailing %. */
674 if (format[i] == 0)
675 break;
676 if (format[i] == '%') {
njn02bc4b82005-05-15 17:28:26 +0000677 /* '%%' is replaced by '%'. */
sewardj1cf558c2005-04-25 01:36:56 +0000678 send('%', send_arg2);
679 ret++;
680 continue;
681 }
682 flags = 0;
njn68e46592005-08-26 19:42:27 +0000683 n_ls = 0;
sewardj1cf558c2005-04-25 01:36:56 +0000684 width = 0; /* length of the field. */
barta0b6b2c2008-07-07 06:49:24 +0000685 while (1) {
686 switch (format[i]) {
687 case '(':
688 flags |= VG_MSG_PAREN;
689 break;
690 case ',':
691 case '\'':
692 /* If ',' or '\'' follows '%', commas will be inserted. */
693 flags |= VG_MSG_COMMA;
694 break;
695 case '-':
696 /* If '-' follows '%', justify on the left. */
697 flags |= VG_MSG_LJUSTIFY;
698 break;
699 case '0':
700 /* If '0' follows '%', pads will be inserted. */
701 flags |= VG_MSG_ZJUSTIFY;
702 break;
703 case '#':
704 /* If '#' follows '%', alternative format will be used. */
705 flags |= VG_MSG_ALTFORMAT;
706 break;
707 default:
708 goto parse_fieldwidth;
709 }
sewardj1cf558c2005-04-25 01:36:56 +0000710 i++;
711 }
barta0b6b2c2008-07-07 06:49:24 +0000712 parse_fieldwidth:
sewardj1cf558c2005-04-25 01:36:56 +0000713 /* Compute the field length. */
714 while (format[i] >= '0' && format[i] <= '9') {
715 width *= 10;
716 width += format[i++] - '0';
717 }
718 while (format[i] == 'l') {
719 i++;
njn68e46592005-08-26 19:42:27 +0000720 n_ls++;
sewardj1cf558c2005-04-25 01:36:56 +0000721 }
722
njn68e46592005-08-26 19:42:27 +0000723 // %d means print a 32-bit integer.
724 // %ld means print a word-size integer.
725 // %lld means print a 64-bit integer.
726 if (0 == n_ls) { is_long = False; }
727 else if (1 == n_ls) { is_long = ( sizeof(void*) == sizeof(Long) ); }
728 else { is_long = True; }
729
sewardj1cf558c2005-04-25 01:36:56 +0000730 switch (format[i]) {
bart042257f2009-07-26 08:40:17 +0000731 case 'o': /* %o */
732 if (flags & VG_MSG_ALTFORMAT) {
733 ret += 2;
734 send('0',send_arg2);
735 }
736 if (is_long)
737 ret += myvprintf_int64(send, send_arg2, flags, 8, width, False,
738 (ULong)(va_arg (vargs, ULong)));
739 else
740 ret += myvprintf_int64(send, send_arg2, flags, 8, width, False,
741 (ULong)(va_arg (vargs, UInt)));
742 break;
sewardj1cf558c2005-04-25 01:36:56 +0000743 case 'd': /* %d */
744 flags |= VG_MSG_SIGNED;
745 if (is_long)
sewardja44b15f2007-02-16 14:10:24 +0000746 ret += myvprintf_int64(send, send_arg2, flags, 10, width, False,
sewardj1cf558c2005-04-25 01:36:56 +0000747 (ULong)(va_arg (vargs, Long)));
748 else
sewardja44b15f2007-02-16 14:10:24 +0000749 ret += myvprintf_int64(send, send_arg2, flags, 10, width, False,
sewardj1cf558c2005-04-25 01:36:56 +0000750 (ULong)(va_arg (vargs, Int)));
751 break;
752 case 'u': /* %u */
753 if (is_long)
sewardja44b15f2007-02-16 14:10:24 +0000754 ret += myvprintf_int64(send, send_arg2, flags, 10, width, False,
sewardj1cf558c2005-04-25 01:36:56 +0000755 (ULong)(va_arg (vargs, ULong)));
756 else
sewardja44b15f2007-02-16 14:10:24 +0000757 ret += myvprintf_int64(send, send_arg2, flags, 10, width, False,
sewardj1cf558c2005-04-25 01:36:56 +0000758 (ULong)(va_arg (vargs, UInt)));
759 break;
bartb3af9cf2011-10-06 19:08:37 +0000760 case 'p':
761 if (format[i+1] == 'S') {
762 i++;
763 /* %pS, like %s but escaping chars for XML safety */
764 /* Note: simplistic; ignores field width and flags */
floriandbb35842012-10-27 18:39:11 +0000765 HChar *str = va_arg (vargs, HChar *);
766 if (str == NULL)
bartb3af9cf2011-10-06 19:08:37 +0000767 str = "(null)";
768 ret += myvprintf_str_XML_simplistic(send, send_arg2, str);
bartdb4384e2011-10-11 18:49:35 +0000769 } else if (format[i+1] == 's') {
770 i++;
771 /* %ps, synonym for %s with --xml=no / %pS with --xml=yes */
floriandbb35842012-10-27 18:39:11 +0000772 HChar *str = va_arg (vargs, HChar *);
773 if (str == NULL)
bartdb4384e2011-10-11 18:49:35 +0000774 str = "(null)";
775 if (clo_xml)
776 ret += myvprintf_str_XML_simplistic(send, send_arg2, str);
777 else
778 ret += myvprintf_str(send, send_arg2, flags, width, str,
779 False);
bartb3af9cf2011-10-06 19:08:37 +0000780 } else {
781 /* %p */
782 ret += 2;
783 send('0',send_arg2);
784 send('x',send_arg2);
785 ret += myvprintf_int64(send, send_arg2, flags, 16, width, True,
786 (ULong)((UWord)va_arg (vargs, void *)));
787 }
sewardj1cf558c2005-04-25 01:36:56 +0000788 break;
789 case 'x': /* %x */
sewardja44b15f2007-02-16 14:10:24 +0000790 case 'X': /* %X */
791 caps = toBool(format[i] == 'X');
barta0b6b2c2008-07-07 06:49:24 +0000792 if (flags & VG_MSG_ALTFORMAT) {
793 ret += 2;
794 send('0',send_arg2);
795 send('x',send_arg2);
796 }
sewardj1cf558c2005-04-25 01:36:56 +0000797 if (is_long)
sewardja44b15f2007-02-16 14:10:24 +0000798 ret += myvprintf_int64(send, send_arg2, flags, 16, width, caps,
sewardj1cf558c2005-04-25 01:36:56 +0000799 (ULong)(va_arg (vargs, ULong)));
800 else
sewardja44b15f2007-02-16 14:10:24 +0000801 ret += myvprintf_int64(send, send_arg2, flags, 16, width, caps,
sewardj1cf558c2005-04-25 01:36:56 +0000802 (ULong)(va_arg (vargs, UInt)));
803 break;
804 case 'c': /* %c */
805 ret++;
806 send(va_arg (vargs, int), send_arg2);
807 break;
808 case 's': case 'S': { /* %s */
floriandbb35842012-10-27 18:39:11 +0000809 HChar *str = va_arg (vargs, HChar *);
810 if (str == NULL) str = "(null)";
sewardj1cf558c2005-04-25 01:36:56 +0000811 ret += myvprintf_str(send, send_arg2,
812 flags, width, str, format[i]=='S');
813 break;
814 }
sewardjdaf77af2005-07-19 14:17:37 +0000815
sewardj1cf558c2005-04-25 01:36:56 +0000816// case 'y': { /* %y - print symbol */
florian19f91bb2012-11-10 22:29:54 +0000817// HChar buf[100];
818// HChar *cp = buf;
sewardj1cf558c2005-04-25 01:36:56 +0000819// Addr a = va_arg(vargs, Addr);
820//
821// if (flags & VG_MSG_PAREN)
822// *cp++ = '(';
823// if (VG_(get_fnname_w_offset)(a, cp, sizeof(buf)-4)) {
824// if (flags & VG_MSG_PAREN) {
825// cp += VG_(strlen)(cp);
826// *cp++ = ')';
827// *cp = '\0';
828// }
829// ret += myvprintf_str(send, send_arg2, flags, width, buf, 0);
830// }
831// break;
832// }
833 default:
834 break;
835 }
836 }
837 return ret;
838}
839
840
841/*------------------------------------------------------------*/
842/*--- Debuglog stuff. ---*/
843/*------------------------------------------------------------*/
844
845/* Only print messages whose stated level is less than or equal to
846 this. By default, it makes this entire subsystem silent. */
847
848static Int loglevel = 0;
849
sewardj1cf558c2005-04-25 01:36:56 +0000850/* Module startup. */
sewardj10759312005-05-30 23:52:47 +0000851/* EXPORTED */
floriandbb35842012-10-27 18:39:11 +0000852void VG_(debugLog_startup) ( Int level, const HChar* who )
sewardj1cf558c2005-04-25 01:36:56 +0000853{
854 if (level < 0) level = 0;
855 if (level > 10) level = 10;
856 loglevel = level;
857 VG_(debugLog)(1, "debuglog",
858 "DebugLog system started by %s, "
859 "level %d logging requested\n",
860 who, loglevel);
861}
862
sewardj10759312005-05-30 23:52:47 +0000863/* Get the logging threshold level, as set by the most recent call to
864 VG_(debugLog_startup), or zero if there have been no such calls so
865 far. */
866/* EXPORTED */
867Int VG_(debugLog_getLevel) ( void )
868{
869 return loglevel;
870}
871
872
sewardj1cf558c2005-04-25 01:36:56 +0000873/* ------------ */
874
875typedef
876 struct {
877 HChar buf[100];
878 Int n;
879 }
880 printf_buf;
881
882static void add_to_buf ( HChar c, void* p )
883{
884 printf_buf* buf = (printf_buf*)p;
885
886 if (buf->n >= 100-10 /*paranoia*/ ) {
887 emit( buf->buf, local_strlen(buf->buf) );
888 buf->n = 0;
889 buf->buf[buf->n] = 0;
890 }
891 buf->buf[buf->n++] = c;
892 buf->buf[buf->n] = 0;
893}
894
895/* Send a logging message. Nothing is output unless 'level'
896 is <= the current loglevel. */
897/* EXPORTED */
sewardj1cf558c2005-04-25 01:36:56 +0000898void VG_(debugLog) ( Int level, const HChar* modulename,
899 const HChar* format, ... )
900{
sewardjc7ffc942011-03-28 16:26:42 +0000901 UInt pid;
sewardj45f4e7c2005-09-27 19:20:21 +0000902 Int indent, depth, i;
sewardj1cf558c2005-04-25 01:36:56 +0000903 va_list vargs;
904 printf_buf buf;
sewardja5ebfa92005-04-25 02:04:54 +0000905
sewardj1cf558c2005-04-25 01:36:56 +0000906 if (level > loglevel)
907 return;
908
sewardjd85feff2005-04-25 02:37:56 +0000909 indent = 2*level - 1;
sewardja5ebfa92005-04-25 02:04:54 +0000910 if (indent < 1) indent = 1;
911
sewardj1cf558c2005-04-25 01:36:56 +0000912 buf.n = 0;
913 buf.buf[0] = 0;
914 pid = local_sys_getpid();
sewardj45f4e7c2005-09-27 19:20:21 +0000915
916 // Print one '>' in front of the messages for each level of self-hosting
917 // being performed.
918 depth = RUNNING_ON_VALGRIND;
919 for (i = 0; i < depth; i++) {
920 (void)myvprintf_str ( add_to_buf, &buf, 0, 1, ">", False );
921 }
922
sewardja5ebfa92005-04-25 02:04:54 +0000923 (void)myvprintf_str ( add_to_buf, &buf, 0, 2, "--", False );
sewardja44b15f2007-02-16 14:10:24 +0000924 (void)myvprintf_int64 ( add_to_buf, &buf, 0, 10, 1, False, (ULong)pid );
sewardja5ebfa92005-04-25 02:04:54 +0000925 (void)myvprintf_str ( add_to_buf, &buf, 0, 1, ":", False );
sewardja44b15f2007-02-16 14:10:24 +0000926 (void)myvprintf_int64 ( add_to_buf, &buf, 0, 10, 1, False, (ULong)level );
sewardja5ebfa92005-04-25 02:04:54 +0000927 (void)myvprintf_str ( add_to_buf, &buf, 0, 1, ":", False );
floriandbb35842012-10-27 18:39:11 +0000928 (void)myvprintf_str ( add_to_buf, &buf, 0, 8, modulename, False );
sewardja5ebfa92005-04-25 02:04:54 +0000929 (void)myvprintf_str ( add_to_buf, &buf, 0, indent, "", False );
sewardj1cf558c2005-04-25 01:36:56 +0000930
931 va_start(vargs,format);
932
sewardjc7ffc942011-03-28 16:26:42 +0000933 (void) VG_(debugLog_vprintf) ( add_to_buf, &buf, format, vargs );
sewardj1cf558c2005-04-25 01:36:56 +0000934
935 if (buf.n > 0) {
936 emit( buf.buf, local_strlen(buf.buf) );
937 }
938
939 va_end(vargs);
940}
941
942
943
944/*--------------------------------------------------------------------*/
945/*--- end m_debuglog.c ---*/
946/*--------------------------------------------------------------------*/