sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 1 | |
| 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 | |
sewardj | ec062e8 | 2011-10-23 07:32:08 +0000 | [diff] [blame] | 10 | Copyright (C) 2000-2011 Julian Seward |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 11 | 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 | */ |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 45 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 46 | /* This module is also notable because it is linked into both |
| 47 | stage1 and stage2. */ |
| 48 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 49 | /* 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 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 54 | #include "pub_core_basics.h" /* basic types */ |
sewardj | 17edf03 | 2006-10-17 01:53:34 +0000 | [diff] [blame] | 55 | #include "pub_core_vkiscnums.h" /* for syscall numbers */ |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 56 | #include "pub_core_debuglog.h" /* our own iface */ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 57 | #include "valgrind.h" /* for RUNNING_ON_VALGRIND */ |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 58 | |
bart | db4384e | 2011-10-11 18:49:35 +0000 | [diff] [blame] | 59 | static Bool clo_xml; |
| 60 | |
| 61 | void VG_(debugLog_setXml)(Bool xml) |
| 62 | { |
| 63 | clo_xml = xml; |
| 64 | } |
| 65 | |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 66 | /*------------------------------------------------------------*/ |
| 67 | /*--- Stuff to make us completely independent. ---*/ |
| 68 | /*------------------------------------------------------------*/ |
| 69 | |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 70 | /* ----- Platform-specifics ----- */ |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 71 | |
sewardj | 21c6d0f | 2005-05-02 10:33:44 +0000 | [diff] [blame] | 72 | #if defined(VGP_x86_linux) |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 73 | |
| 74 | static UInt local_sys_write_stderr ( HChar* buf, Int n ) |
| 75 | { |
bart | df0c09e | 2012-01-30 15:07:20 +0000 | [diff] [blame] | 76 | Int result; |
| 77 | |
tom | 311400b | 2005-04-27 08:58:53 +0000 | [diff] [blame] | 78 | __asm__ volatile ( |
sewardj | 17edf03 | 2006-10-17 01:53:34 +0000 | [diff] [blame] | 79 | "movl $"VG_STRINGIFY(__NR_write)", %%eax\n" /* %eax = __NR_write */ |
njn | 5ed05a5 | 2009-05-20 06:59:19 +0000 | [diff] [blame] | 80 | "movl $2, %%ebx\n" /* %ebx = stderr */ |
sewardj | 55c4376 | 2006-04-28 21:01:33 +0000 | [diff] [blame] | 81 | "int $0x80\n" /* write(stderr, buf, n) */ |
bart | df0c09e | 2012-01-30 15:07:20 +0000 | [diff] [blame] | 82 | : /*wr*/ "=a" (result) |
| 83 | : /*rd*/ "c" (buf), "d" (n) |
| 84 | : /*trash*/ "ebx", "edi", "memory", "cc" |
sewardj | d4d203b | 2005-04-27 23:17:48 +0000 | [diff] [blame] | 85 | ); |
bart | df0c09e | 2012-01-30 15:07:20 +0000 | [diff] [blame] | 86 | |
| 87 | return result >= 0 ? result : -1; |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | static UInt local_sys_getpid ( void ) |
| 91 | { |
| 92 | UInt __res; |
tom | 311400b | 2005-04-27 08:58:53 +0000 | [diff] [blame] | 93 | __asm__ volatile ( |
sewardj | 17edf03 | 2006-10-17 01:53:34 +0000 | [diff] [blame] | 94 | "movl $"VG_STRINGIFY(__NR_getpid)", %%eax\n" /* %eax = __NR_getpid */ |
tom | 311400b | 2005-04-27 08:58:53 +0000 | [diff] [blame] | 95 | "int $0x80\n" /* getpid() */ |
| 96 | "movl %%eax, %0\n" /* set __res = eax */ |
sewardj | d4d203b | 2005-04-27 23:17:48 +0000 | [diff] [blame] | 97 | : "=mr" (__res) |
| 98 | : |
| 99 | : "eax" ); |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 100 | return __res; |
| 101 | } |
| 102 | |
sewardj | 21c6d0f | 2005-05-02 10:33:44 +0000 | [diff] [blame] | 103 | #elif defined(VGP_amd64_linux) |
sewardj | 7337f92 | 2006-05-26 11:31:15 +0000 | [diff] [blame] | 104 | __attribute__((noinline)) |
sewardj | 601371a | 2005-04-25 16:21:17 +0000 | [diff] [blame] | 105 | static UInt local_sys_write_stderr ( HChar* buf, Int n ) |
| 106 | { |
sewardj | 17edf03 | 2006-10-17 01:53:34 +0000 | [diff] [blame] | 107 | volatile Long block[2]; |
sewardj | 7337f92 | 2006-05-26 11:31:15 +0000 | [diff] [blame] | 108 | block[0] = (Long)buf; |
| 109 | block[1] = n; |
tom | c612186 | 2005-04-27 09:23:02 +0000 | [diff] [blame] | 110 | __asm__ volatile ( |
sewardj | 7337f92 | 2006-05-26 11:31:15 +0000 | [diff] [blame] | 111 | "subq $256, %%rsp\n" /* don't trash the stack redzone */ |
| 112 | "pushq %%r15\n" /* r15 is callee-save */ |
| 113 | "movq %0, %%r15\n" /* r15 = &block */ |
| 114 | "pushq %%r15\n" /* save &block */ |
sewardj | 17edf03 | 2006-10-17 01:53:34 +0000 | [diff] [blame] | 115 | "movq $"VG_STRINGIFY(__NR_write)", %%rax\n" /* rax = __NR_write */ |
sewardj | 7337f92 | 2006-05-26 11:31:15 +0000 | [diff] [blame] | 116 | "movq $2, %%rdi\n" /* rdi = stderr */ |
| 117 | "movq 0(%%r15), %%rsi\n" /* rsi = buf */ |
| 118 | "movq 8(%%r15), %%rdx\n" /* rdx = n */ |
| 119 | "syscall\n" /* write(stderr, buf, n) */ |
| 120 | "popq %%r15\n" /* reestablish &block */ |
| 121 | "movq %%rax, 0(%%r15)\n" /* block[0] = result */ |
| 122 | "popq %%r15\n" /* restore r15 */ |
| 123 | "addq $256, %%rsp\n" /* restore stack ptr */ |
| 124 | : /*wr*/ |
sewardj | f068f19 | 2011-04-26 07:52:44 +0000 | [diff] [blame] | 125 | : /*rd*/ "r" (block) |
sewardj | 7337f92 | 2006-05-26 11:31:15 +0000 | [diff] [blame] | 126 | : /*trash*/ "rax", "rdi", "rsi", "rdx", "memory", "cc" |
| 127 | ); |
| 128 | if (block[0] < 0) |
| 129 | block[0] = -1; |
| 130 | return (UInt)block[0]; |
sewardj | 601371a | 2005-04-25 16:21:17 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | static UInt local_sys_getpid ( void ) |
| 134 | { |
tom | c612186 | 2005-04-27 09:23:02 +0000 | [diff] [blame] | 135 | UInt __res; |
| 136 | __asm__ volatile ( |
sewardj | 17edf03 | 2006-10-17 01:53:34 +0000 | [diff] [blame] | 137 | "movq $"VG_STRINGIFY(__NR_getpid)", %%rax\n" /* %rax = __NR_getpid */ |
tom | c612186 | 2005-04-27 09:23:02 +0000 | [diff] [blame] | 138 | "syscall\n" /* getpid() */ |
| 139 | "movl %%eax, %0\n" /* set __res = %eax */ |
| 140 | : "=mr" (__res) |
| 141 | : |
| 142 | : "rax" ); |
| 143 | return __res; |
sewardj | 601371a | 2005-04-25 16:21:17 +0000 | [diff] [blame] | 144 | } |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 145 | |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 146 | #elif defined(VGP_ppc32_linux) |
| 147 | |
| 148 | static UInt local_sys_write_stderr ( HChar* buf, Int n ) |
| 149 | { |
sewardj | 17edf03 | 2006-10-17 01:53:34 +0000 | [diff] [blame] | 150 | volatile Int block[2]; |
sewardj | a269958 | 2006-05-22 13:04:42 +0000 | [diff] [blame] | 151 | block[0] = (Int)buf; |
| 152 | block[1] = n; |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 153 | __asm__ volatile ( |
sewardj | a269958 | 2006-05-22 13:04:42 +0000 | [diff] [blame] | 154 | "addi 1,1,-256\n\t" |
| 155 | "mr 5,%0\n\t" /* r5 = &block[0] */ |
| 156 | "stw 5,0(1)\n\t" /* stash on stack */ |
sewardj | 17edf03 | 2006-10-17 01:53:34 +0000 | [diff] [blame] | 157 | "li 0,"VG_STRINGIFY(__NR_write)"\n\t" /* set %r0 = __NR_write */ |
sewardj | a269958 | 2006-05-22 13:04:42 +0000 | [diff] [blame] | 158 | "li 3,2\n\t" /* set %r3 = stderr */ |
| 159 | "lwz 4,0(5)\n\t" /* set %r4 = buf */ |
| 160 | "lwz 5,4(5)\n\t" /* set %r5 = n */ |
| 161 | "sc\n\t" /* write(stderr, buf, n) */ |
| 162 | "lwz 5,0(1)\n\t" |
| 163 | "addi 1,1,256\n\t" |
| 164 | "stw 3,0(5)\n" /* block[0] = result */ |
| 165 | : |
| 166 | : "b" (block) |
| 167 | : "cc","memory","cr0","ctr", |
| 168 | "r0","r2","r3","r4","r5","r6","r7","r8","r9","r10","r11","r12" |
| 169 | ); |
| 170 | if (block[0] < 0) |
| 171 | block[0] = -1; |
| 172 | return (UInt)block[0]; |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | static UInt local_sys_getpid ( void ) |
| 176 | { |
sewardj | a269958 | 2006-05-22 13:04:42 +0000 | [diff] [blame] | 177 | register UInt __res __asm__ ("r3"); |
| 178 | __asm__ volatile ( |
| 179 | "li 0, %1\n\t" |
| 180 | "sc" |
| 181 | : "=&r" (__res) |
sewardj | 17edf03 | 2006-10-17 01:53:34 +0000 | [diff] [blame] | 182 | : "i" (__NR_getpid) |
sewardj | a269958 | 2006-05-22 13:04:42 +0000 | [diff] [blame] | 183 | : "cc","memory","cr0","ctr", |
| 184 | "r0","r2","r4","r5","r6","r7","r8","r9","r10","r11","r12" |
| 185 | ); |
cerion | 85665ca | 2005-06-20 15:51:07 +0000 | [diff] [blame] | 186 | return __res; |
| 187 | } |
| 188 | |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 189 | #elif defined(VGP_ppc64_linux) |
| 190 | |
| 191 | static UInt local_sys_write_stderr ( HChar* buf, Int n ) |
| 192 | { |
sewardj | 17edf03 | 2006-10-17 01:53:34 +0000 | [diff] [blame] | 193 | volatile Long block[2]; |
sewardj | a269958 | 2006-05-22 13:04:42 +0000 | [diff] [blame] | 194 | block[0] = (Long)buf; |
| 195 | block[1] = (Long)n; |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 196 | __asm__ volatile ( |
sewardj | a269958 | 2006-05-22 13:04:42 +0000 | [diff] [blame] | 197 | "addi 1,1,-256\n\t" |
| 198 | "mr 5,%0\n\t" /* r5 = &block[0] */ |
| 199 | "std 5,0(1)\n\t" /* stash on stack */ |
sewardj | 17edf03 | 2006-10-17 01:53:34 +0000 | [diff] [blame] | 200 | "li 0,"VG_STRINGIFY(__NR_write)"\n\t" /* %r0 = __NR_write */ |
sewardj | a269958 | 2006-05-22 13:04:42 +0000 | [diff] [blame] | 201 | "li 3,2\n\t" /* set %r3 = stderr */ |
| 202 | "ld 4,0(5)\n\t" /* set %r4 = buf */ |
| 203 | "ld 5,8(5)\n\t" /* set %r5 = n */ |
| 204 | "sc\n\t" /* write(stderr, buf, n) */ |
| 205 | "ld 5,0(1)\n\t" |
| 206 | "addi 1,1,256\n\t" |
| 207 | "std 3,0(5)\n" /* block[0] = result */ |
| 208 | : |
| 209 | : "b" (block) |
| 210 | : "cc","memory","cr0","ctr", |
| 211 | "r0","r2","r3","r4","r5","r6","r7","r8","r9","r10","r11","r12" |
| 212 | ); |
| 213 | if (block[0] < 0) |
| 214 | block[0] = -1; |
| 215 | return (UInt)(Int)block[0]; |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | static UInt local_sys_getpid ( void ) |
| 219 | { |
sewardj | a269958 | 2006-05-22 13:04:42 +0000 | [diff] [blame] | 220 | register ULong __res __asm__ ("r3"); |
| 221 | __asm__ volatile ( |
| 222 | "li 0, %1\n\t" |
| 223 | "sc" |
| 224 | : "=&r" (__res) |
sewardj | 17edf03 | 2006-10-17 01:53:34 +0000 | [diff] [blame] | 225 | : "i" (__NR_getpid) |
sewardj | a269958 | 2006-05-22 13:04:42 +0000 | [diff] [blame] | 226 | : "cc","memory","cr0","ctr", |
| 227 | "r0","r2","r4","r5","r6","r7","r8","r9","r10","r11","r12" |
| 228 | ); |
| 229 | return (UInt)__res; |
sewardj | 2c48c7b | 2005-11-29 13:05:56 +0000 | [diff] [blame] | 230 | } |
| 231 | |
sewardj | 59570ff | 2010-01-01 11:59:33 +0000 | [diff] [blame] | 232 | #elif defined(VGP_arm_linux) |
| 233 | |
| 234 | static UInt local_sys_write_stderr ( HChar* buf, Int n ) |
| 235 | { |
| 236 | volatile Int block[2]; |
| 237 | block[0] = (Int)buf; |
| 238 | block[1] = n; |
| 239 | __asm__ volatile ( |
sewardj | 188a6d7 | 2010-08-26 09:40:37 +0000 | [diff] [blame] | 240 | "mov r0, #2\n\t" /* stderr */ |
| 241 | "ldr r1, [%0]\n\t" /* buf */ |
| 242 | "ldr r2, [%0, #4]\n\t" /* n */ |
sewardj | 59570ff | 2010-01-01 11:59:33 +0000 | [diff] [blame] | 243 | "mov r7, #"VG_STRINGIFY(__NR_write)"\n\t" |
| 244 | "svc 0x0\n" /* write() */ |
| 245 | "str r0, [%0]\n\t" |
| 246 | : |
| 247 | : "r" (block) |
| 248 | : "r0","r1","r2","r7" |
| 249 | ); |
| 250 | if (block[0] < 0) |
| 251 | block[0] = -1; |
| 252 | return (UInt)block[0]; |
| 253 | } |
| 254 | |
| 255 | static UInt local_sys_getpid ( void ) |
| 256 | { |
| 257 | UInt __res; |
| 258 | __asm__ volatile ( |
| 259 | "mov r7, #"VG_STRINGIFY(__NR_getpid)"\n" |
| 260 | "svc 0x0\n" /* getpid() */ |
| 261 | "mov %0, r0\n" |
| 262 | : "=r" (__res) |
| 263 | : |
| 264 | : "r0", "r7" ); |
| 265 | return __res; |
| 266 | } |
| 267 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 268 | #elif defined(VGP_x86_darwin) |
| 269 | |
njn | 1a1e95c | 2009-06-03 06:50:06 +0000 | [diff] [blame] | 270 | /* We would use VG_DARWIN_SYSNO_TO_KERNEL instead of VG_DARWIN_SYSNO_INDEX |
| 271 | except that the former has a C ternary ?: operator which isn't valid in |
| 272 | asm code. Both macros give the same results for Unix-class syscalls (which |
| 273 | these all are, as identified by the use of 'int 0x80'). */ |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 274 | __attribute__((noinline)) |
| 275 | static UInt local_sys_write_stderr ( HChar* buf, Int n ) |
| 276 | { |
| 277 | UInt __res; |
| 278 | __asm__ volatile ( |
| 279 | "movl %2, %%eax\n" /* push n */ |
| 280 | "pushl %%eax\n" |
| 281 | "movl %1, %%eax\n" /* push buf */ |
| 282 | "pushl %%eax\n" |
| 283 | "movl $2, %%eax\n" /* push stderr */ |
| 284 | "pushl %%eax\n" |
| 285 | "movl $"VG_STRINGIFY(VG_DARWIN_SYSNO_INDEX(__NR_write_nocancel)) |
| 286 | ", %%eax\n" |
| 287 | "pushl %%eax\n" /* push fake return address */ |
| 288 | "int $0x80\n" /* write(stderr, buf, n) */ |
| 289 | "jnc 1f\n" /* jump if no error */ |
| 290 | "movl $-1, %%eax\n" /* return -1 if error */ |
| 291 | "1: " |
| 292 | "movl %%eax, %0\n" /* __res = eax */ |
| 293 | "addl $16, %%esp\n" /* pop x4 */ |
| 294 | : "=mr" (__res) |
| 295 | : "g" (buf), "g" (n) |
| 296 | : "eax", "edx", "cc" |
| 297 | ); |
| 298 | return __res; |
| 299 | } |
| 300 | |
| 301 | static UInt local_sys_getpid ( void ) |
| 302 | { |
| 303 | UInt __res; |
| 304 | __asm__ volatile ( |
| 305 | "movl $"VG_STRINGIFY(VG_DARWIN_SYSNO_INDEX(__NR_getpid))", %%eax\n" |
| 306 | "int $0x80\n" /* getpid() */ |
| 307 | "movl %%eax, %0\n" /* set __res = eax */ |
| 308 | : "=mr" (__res) |
| 309 | : |
| 310 | : "eax", "cc" ); |
| 311 | return __res; |
| 312 | } |
| 313 | |
| 314 | #elif defined(VGP_amd64_darwin) |
| 315 | |
| 316 | __attribute__((noinline)) |
| 317 | static UInt local_sys_write_stderr ( HChar* buf, Int n ) |
| 318 | { |
| 319 | UInt __res; |
| 320 | __asm__ volatile ( |
| 321 | "movq $2, %%rdi\n" /* push stderr */ |
| 322 | "movq %1, %%rsi\n" /* push buf */ |
| 323 | "movl %2, %%edx\n" /* push n */ |
njn | 1a1e95c | 2009-06-03 06:50:06 +0000 | [diff] [blame] | 324 | "movl $"VG_STRINGIFY(VG_DARWIN_SYSNO_FOR_KERNEL(__NR_write_nocancel)) |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 325 | ", %%eax\n" |
| 326 | "syscall\n" /* write(stderr, buf, n) */ |
| 327 | "jnc 1f\n" /* jump if no error */ |
| 328 | "movq $-1, %%rax\n" /* return -1 if error */ |
| 329 | "1: " |
| 330 | "movl %%eax, %0\n" /* __res = eax */ |
| 331 | : "=mr" (__res) |
| 332 | : "g" (buf), "g" (n) |
| 333 | : "rdi", "rsi", "rdx", "rcx", "rax", "cc" ); |
| 334 | return __res; |
| 335 | } |
| 336 | |
| 337 | static UInt local_sys_getpid ( void ) |
| 338 | { |
| 339 | UInt __res; |
| 340 | __asm__ volatile ( |
njn | 1a1e95c | 2009-06-03 06:50:06 +0000 | [diff] [blame] | 341 | "movl $"VG_STRINGIFY(VG_DARWIN_SYSNO_FOR_KERNEL(__NR_getpid))", %%eax\n" |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 342 | "syscall\n" /* getpid() */ |
| 343 | "movl %%eax, %0\n" /* set __res = eax */ |
| 344 | : "=mr" (__res) |
| 345 | : |
| 346 | : "rax", "rcx", "cc" ); |
| 347 | return __res; |
| 348 | } |
| 349 | |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 350 | #elif defined(VGP_s390x_linux) |
| 351 | static UInt local_sys_write_stderr ( HChar* buf, Int n ) |
| 352 | { |
| 353 | register Int r2 asm("2") = 2; /* file descriptor STDERR */ |
| 354 | register HChar* r3 asm("3") = buf; |
| 355 | register ULong r4 asm("4") = n; |
| 356 | register ULong r2_res asm("2"); |
| 357 | ULong __res; |
| 358 | |
| 359 | __asm__ __volatile__ ( |
| 360 | "svc %b1\n" |
| 361 | : "=d" (r2_res) |
| 362 | : "i" (__NR_write), |
| 363 | "0" (r2), |
| 364 | "d" (r3), |
| 365 | "d" (r4) |
| 366 | : "cc", "memory"); |
| 367 | __res = r2_res; |
| 368 | |
| 369 | if (__res >= (ULong)(-125)) |
| 370 | __res = -1; |
| 371 | return (UInt)(__res); |
| 372 | } |
| 373 | |
| 374 | static UInt local_sys_getpid ( void ) |
| 375 | { |
| 376 | register ULong r2 asm("2"); |
| 377 | ULong __res; |
| 378 | |
| 379 | __asm__ __volatile__ ( |
| 380 | "svc %b1\n" |
| 381 | : "=d" (r2) |
| 382 | : "i" (__NR_getpid) |
| 383 | : "cc", "memory"); |
| 384 | __res = r2; |
| 385 | |
| 386 | if (__res >= (ULong)(-125)) |
| 387 | __res = -1; |
| 388 | return (UInt)(__res); |
| 389 | } |
| 390 | |
sewardj | 5db1540 | 2012-06-07 09:13:21 +0000 | [diff] [blame^] | 391 | #elif defined(VGP_mips32_linux) |
| 392 | static UInt local_sys_write_stderr ( HChar* buf, Int n ) |
| 393 | { |
| 394 | volatile Int block[2]; |
| 395 | block[0] = (Int)buf; |
| 396 | block[1] = n; |
| 397 | __asm__ volatile ( |
| 398 | "li $4, 2\n\t" /* stderr */ |
| 399 | "lw $5, 0(%0)\n\t" /* buf */ |
| 400 | "lw $6, 4(%0)\n\t" /* n */ |
| 401 | "move $7, $0\n\t" |
| 402 | "li $2, %1\n\t" /* set v0 = __NR_write */ |
| 403 | "syscall\n\t" /* write() */ |
| 404 | "nop\n\t" |
| 405 | : |
| 406 | : "r" (block), "n" (__NR_write) |
| 407 | : "2", "4", "5", "6", "7" |
| 408 | ); |
| 409 | if (block[0] < 0) |
| 410 | block[0] = -1; |
| 411 | return (UInt)block[0]; |
| 412 | } |
| 413 | |
| 414 | static UInt local_sys_getpid ( void ) |
| 415 | { |
| 416 | UInt __res; |
| 417 | __asm__ volatile ( |
| 418 | "li $2, %1\n\t" /* set v0 = __NR_getpid */ |
| 419 | "syscall\n\t" /* getpid() */ |
| 420 | "nop\n\t" |
| 421 | "move %0, $2\n" |
| 422 | : "=r" (__res) |
| 423 | : "n" (__NR_getpid) |
| 424 | : "$2" ); |
| 425 | return __res; |
| 426 | } |
| 427 | |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 428 | |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 429 | #else |
sewardj | 21c6d0f | 2005-05-02 10:33:44 +0000 | [diff] [blame] | 430 | # error Unknown platform |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 431 | #endif |
| 432 | |
| 433 | |
| 434 | /* ----- generic ----- */ |
| 435 | |
| 436 | /* strlen, so we don't need m_libc */ |
| 437 | static Int local_strlen ( const HChar* str ) |
| 438 | { |
| 439 | Int i = 0; |
| 440 | while (str[i] != 0) i++; |
| 441 | return i; |
| 442 | } |
| 443 | |
| 444 | static HChar local_toupper ( HChar c ) |
| 445 | { |
| 446 | if (c >= 'a' && c <= 'z') |
| 447 | return c + ('A' - 'a'); |
| 448 | else |
| 449 | return c; |
| 450 | } |
| 451 | |
| 452 | /* Emit buf[0 .. n-1] to stderr. Unfortunately platform-specific. |
| 453 | */ |
| 454 | static void emit ( HChar* buf, Int n ) |
| 455 | { |
| 456 | if (n >= 1) |
| 457 | (void)local_sys_write_stderr(buf, n); |
| 458 | } |
| 459 | |
| 460 | |
| 461 | /*------------------------------------------------------------*/ |
| 462 | /*--- A simple, generic, vprintf implementation. ---*/ |
| 463 | /*------------------------------------------------------------*/ |
| 464 | |
| 465 | /* ----------------------------------------------- |
| 466 | Distantly derived from: |
| 467 | |
| 468 | vprintf replacement for Checker. |
| 469 | Copyright 1993, 1994, 1995 Tristan Gingold |
| 470 | Written September 1993 Tristan Gingold |
| 471 | Tristan Gingold, 8 rue Parmentier, F-91120 PALAISEAU, FRANCE |
| 472 | |
| 473 | (Checker itself was GPL'd.) |
| 474 | ----------------------------------------------- */ |
| 475 | |
| 476 | /* Some flags. */ |
| 477 | #define VG_MSG_SIGNED 1 /* The value is signed. */ |
| 478 | #define VG_MSG_ZJUSTIFY 2 /* Must justify with '0'. */ |
| 479 | #define VG_MSG_LJUSTIFY 4 /* Must justify on the left. */ |
| 480 | #define VG_MSG_PAREN 8 /* Parenthesize if present (for %y) */ |
| 481 | #define VG_MSG_COMMA 16 /* Add commas to numbers (for %d, %u) */ |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 482 | #define VG_MSG_ALTFORMAT 32 /* Convert the value to alternate format */ |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 483 | |
| 484 | /* Copy a string into the buffer. */ |
| 485 | static |
| 486 | UInt myvprintf_str ( void(*send)(HChar,void*), |
| 487 | void* send_arg2, |
| 488 | Int flags, |
| 489 | Int width, |
| 490 | HChar* str, |
| 491 | Bool capitalise ) |
| 492 | { |
| 493 | # define MAYBE_TOUPPER(ch) (capitalise ? local_toupper(ch) : (ch)) |
| 494 | UInt ret = 0; |
| 495 | Int i, extra; |
| 496 | Int len = local_strlen(str); |
| 497 | |
| 498 | if (width == 0) { |
| 499 | ret += len; |
| 500 | for (i = 0; i < len; i++) |
| 501 | send(MAYBE_TOUPPER(str[i]), send_arg2); |
| 502 | return ret; |
| 503 | } |
| 504 | |
| 505 | if (len > width) { |
| 506 | ret += width; |
| 507 | for (i = 0; i < width; i++) |
| 508 | send(MAYBE_TOUPPER(str[i]), send_arg2); |
| 509 | return ret; |
| 510 | } |
| 511 | |
| 512 | extra = width - len; |
| 513 | if (flags & VG_MSG_LJUSTIFY) { |
| 514 | ret += extra; |
| 515 | for (i = 0; i < extra; i++) |
| 516 | send(' ', send_arg2); |
| 517 | } |
| 518 | ret += len; |
| 519 | for (i = 0; i < len; i++) |
| 520 | send(MAYBE_TOUPPER(str[i]), send_arg2); |
| 521 | if (!(flags & VG_MSG_LJUSTIFY)) { |
| 522 | ret += extra; |
| 523 | for (i = 0; i < extra; i++) |
| 524 | send(' ', send_arg2); |
| 525 | } |
| 526 | |
| 527 | # undef MAYBE_TOUPPER |
| 528 | return ret; |
| 529 | } |
| 530 | |
| 531 | |
sewardj | daf77af | 2005-07-19 14:17:37 +0000 | [diff] [blame] | 532 | /* Copy a string into the buffer, escaping bad XML chars. */ |
| 533 | static |
| 534 | UInt myvprintf_str_XML_simplistic ( void(*send)(HChar,void*), |
| 535 | void* send_arg2, |
| 536 | HChar* str ) |
| 537 | { |
| 538 | UInt ret = 0; |
| 539 | Int i; |
| 540 | Int len = local_strlen(str); |
| 541 | HChar* alt; |
| 542 | |
| 543 | for (i = 0; i < len; i++) { |
| 544 | switch (str[i]) { |
| 545 | case '&': alt = "&"; break; |
| 546 | case '<': alt = "<"; break; |
| 547 | case '>': alt = ">"; break; |
| 548 | default: alt = NULL; |
| 549 | } |
| 550 | |
| 551 | if (alt) { |
| 552 | while (*alt) { |
| 553 | send(*alt, send_arg2); |
| 554 | ret++; |
| 555 | alt++; |
| 556 | } |
| 557 | } else { |
| 558 | send(str[i], send_arg2); |
| 559 | ret++; |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | return ret; |
| 564 | } |
| 565 | |
| 566 | |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 567 | /* Write P into the buffer according to these args: |
| 568 | * If SIGN is true, p is a signed. |
| 569 | * BASE is the base. |
| 570 | * If WITH_ZERO is true, '0' must be added. |
| 571 | * WIDTH is the width of the field. |
| 572 | */ |
| 573 | static |
| 574 | UInt myvprintf_int64 ( void(*send)(HChar,void*), |
| 575 | void* send_arg2, |
| 576 | Int flags, |
| 577 | Int base, |
| 578 | Int width, |
sewardj | a44b15f | 2007-02-16 14:10:24 +0000 | [diff] [blame] | 579 | Bool capitalised, |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 580 | ULong p ) |
| 581 | { |
| 582 | HChar buf[40]; |
| 583 | Int ind = 0; |
| 584 | Int i, nc = 0; |
| 585 | Bool neg = False; |
sewardj | a44b15f | 2007-02-16 14:10:24 +0000 | [diff] [blame] | 586 | HChar* digits = capitalised ? "0123456789ABCDEF" : "0123456789abcdef"; |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 587 | UInt ret = 0; |
| 588 | |
| 589 | if (base < 2 || base > 16) |
| 590 | return ret; |
| 591 | |
| 592 | if ((flags & VG_MSG_SIGNED) && (Long)p < 0) { |
| 593 | p = - (Long)p; |
| 594 | neg = True; |
| 595 | } |
| 596 | |
| 597 | if (p == 0) |
| 598 | buf[ind++] = '0'; |
| 599 | else { |
| 600 | while (p > 0) { |
| 601 | if (flags & VG_MSG_COMMA && 10 == base && |
| 602 | 0 == (ind-nc) % 3 && 0 != ind) |
| 603 | { |
| 604 | buf[ind++] = ','; |
| 605 | nc++; |
| 606 | } |
| 607 | buf[ind++] = digits[p % base]; |
| 608 | p /= base; |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | if (neg) |
| 613 | buf[ind++] = '-'; |
| 614 | |
| 615 | if (width > 0 && !(flags & VG_MSG_LJUSTIFY)) { |
| 616 | for(; ind < width; ind++) { |
| 617 | /* vg_assert(ind < 39); */ |
| 618 | if (ind > 39) { |
| 619 | buf[39] = 0; |
| 620 | break; |
| 621 | } |
| 622 | buf[ind] = (flags & VG_MSG_ZJUSTIFY) ? '0': ' '; |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | /* Reverse copy to buffer. */ |
| 627 | ret += ind; |
| 628 | for (i = ind -1; i >= 0; i--) { |
| 629 | send(buf[i], send_arg2); |
| 630 | } |
| 631 | if (width > 0 && (flags & VG_MSG_LJUSTIFY)) { |
| 632 | for(; ind < width; ind++) { |
| 633 | ret++; |
| 634 | /* Never pad with zeroes on RHS -- changes the value! */ |
| 635 | send(' ', send_arg2); |
| 636 | } |
| 637 | } |
| 638 | return ret; |
| 639 | } |
| 640 | |
| 641 | |
| 642 | /* A simple vprintf(). */ |
| 643 | /* EXPORTED */ |
| 644 | UInt |
| 645 | VG_(debugLog_vprintf) ( |
| 646 | void(*send)(HChar,void*), |
| 647 | void* send_arg2, |
| 648 | const HChar* format, |
| 649 | va_list vargs |
| 650 | ) |
| 651 | { |
| 652 | UInt ret = 0; |
| 653 | Int i; |
| 654 | Int flags; |
| 655 | Int width; |
njn | 68e4659 | 2005-08-26 19:42:27 +0000 | [diff] [blame] | 656 | Int n_ls = 0; |
sewardj | a44b15f | 2007-02-16 14:10:24 +0000 | [diff] [blame] | 657 | Bool is_long, caps; |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 658 | |
| 659 | /* We assume that vargs has already been initialised by the |
| 660 | caller, using va_start, and that the caller will similarly |
| 661 | clean up with va_end. |
| 662 | */ |
| 663 | |
| 664 | for (i = 0; format[i] != 0; i++) { |
| 665 | if (format[i] != '%') { |
| 666 | send(format[i], send_arg2); |
| 667 | ret++; |
| 668 | continue; |
| 669 | } |
| 670 | i++; |
| 671 | /* A '%' has been found. Ignore a trailing %. */ |
| 672 | if (format[i] == 0) |
| 673 | break; |
| 674 | if (format[i] == '%') { |
njn | 02bc4b8 | 2005-05-15 17:28:26 +0000 | [diff] [blame] | 675 | /* '%%' is replaced by '%'. */ |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 676 | send('%', send_arg2); |
| 677 | ret++; |
| 678 | continue; |
| 679 | } |
| 680 | flags = 0; |
njn | 68e4659 | 2005-08-26 19:42:27 +0000 | [diff] [blame] | 681 | n_ls = 0; |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 682 | width = 0; /* length of the field. */ |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 683 | while (1) { |
| 684 | switch (format[i]) { |
| 685 | case '(': |
| 686 | flags |= VG_MSG_PAREN; |
| 687 | break; |
| 688 | case ',': |
| 689 | case '\'': |
| 690 | /* If ',' or '\'' follows '%', commas will be inserted. */ |
| 691 | flags |= VG_MSG_COMMA; |
| 692 | break; |
| 693 | case '-': |
| 694 | /* If '-' follows '%', justify on the left. */ |
| 695 | flags |= VG_MSG_LJUSTIFY; |
| 696 | break; |
| 697 | case '0': |
| 698 | /* If '0' follows '%', pads will be inserted. */ |
| 699 | flags |= VG_MSG_ZJUSTIFY; |
| 700 | break; |
| 701 | case '#': |
| 702 | /* If '#' follows '%', alternative format will be used. */ |
| 703 | flags |= VG_MSG_ALTFORMAT; |
| 704 | break; |
| 705 | default: |
| 706 | goto parse_fieldwidth; |
| 707 | } |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 708 | i++; |
| 709 | } |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 710 | parse_fieldwidth: |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 711 | /* Compute the field length. */ |
| 712 | while (format[i] >= '0' && format[i] <= '9') { |
| 713 | width *= 10; |
| 714 | width += format[i++] - '0'; |
| 715 | } |
| 716 | while (format[i] == 'l') { |
| 717 | i++; |
njn | 68e4659 | 2005-08-26 19:42:27 +0000 | [diff] [blame] | 718 | n_ls++; |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 719 | } |
| 720 | |
njn | 68e4659 | 2005-08-26 19:42:27 +0000 | [diff] [blame] | 721 | // %d means print a 32-bit integer. |
| 722 | // %ld means print a word-size integer. |
| 723 | // %lld means print a 64-bit integer. |
| 724 | if (0 == n_ls) { is_long = False; } |
| 725 | else if (1 == n_ls) { is_long = ( sizeof(void*) == sizeof(Long) ); } |
| 726 | else { is_long = True; } |
| 727 | |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 728 | switch (format[i]) { |
bart | 042257f | 2009-07-26 08:40:17 +0000 | [diff] [blame] | 729 | case 'o': /* %o */ |
| 730 | if (flags & VG_MSG_ALTFORMAT) { |
| 731 | ret += 2; |
| 732 | send('0',send_arg2); |
| 733 | } |
| 734 | if (is_long) |
| 735 | ret += myvprintf_int64(send, send_arg2, flags, 8, width, False, |
| 736 | (ULong)(va_arg (vargs, ULong))); |
| 737 | else |
| 738 | ret += myvprintf_int64(send, send_arg2, flags, 8, width, False, |
| 739 | (ULong)(va_arg (vargs, UInt))); |
| 740 | break; |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 741 | case 'd': /* %d */ |
| 742 | flags |= VG_MSG_SIGNED; |
| 743 | if (is_long) |
sewardj | a44b15f | 2007-02-16 14:10:24 +0000 | [diff] [blame] | 744 | ret += myvprintf_int64(send, send_arg2, flags, 10, width, False, |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 745 | (ULong)(va_arg (vargs, Long))); |
| 746 | else |
sewardj | a44b15f | 2007-02-16 14:10:24 +0000 | [diff] [blame] | 747 | ret += myvprintf_int64(send, send_arg2, flags, 10, width, False, |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 748 | (ULong)(va_arg (vargs, Int))); |
| 749 | break; |
| 750 | case 'u': /* %u */ |
| 751 | if (is_long) |
sewardj | a44b15f | 2007-02-16 14:10:24 +0000 | [diff] [blame] | 752 | ret += myvprintf_int64(send, send_arg2, flags, 10, width, False, |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 753 | (ULong)(va_arg (vargs, ULong))); |
| 754 | else |
sewardj | a44b15f | 2007-02-16 14:10:24 +0000 | [diff] [blame] | 755 | ret += myvprintf_int64(send, send_arg2, flags, 10, width, False, |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 756 | (ULong)(va_arg (vargs, UInt))); |
| 757 | break; |
bart | b3af9cf | 2011-10-06 19:08:37 +0000 | [diff] [blame] | 758 | case 'p': |
| 759 | if (format[i+1] == 'S') { |
| 760 | i++; |
| 761 | /* %pS, like %s but escaping chars for XML safety */ |
| 762 | /* Note: simplistic; ignores field width and flags */ |
| 763 | char *str = va_arg (vargs, char *); |
| 764 | if (str == (char*) 0) |
| 765 | str = "(null)"; |
| 766 | ret += myvprintf_str_XML_simplistic(send, send_arg2, str); |
bart | db4384e | 2011-10-11 18:49:35 +0000 | [diff] [blame] | 767 | } else if (format[i+1] == 's') { |
| 768 | i++; |
| 769 | /* %ps, synonym for %s with --xml=no / %pS with --xml=yes */ |
| 770 | char *str = va_arg (vargs, char *); |
| 771 | if (str == (char*) 0) |
| 772 | str = "(null)"; |
| 773 | if (clo_xml) |
| 774 | ret += myvprintf_str_XML_simplistic(send, send_arg2, str); |
| 775 | else |
| 776 | ret += myvprintf_str(send, send_arg2, flags, width, str, |
| 777 | False); |
bart | b3af9cf | 2011-10-06 19:08:37 +0000 | [diff] [blame] | 778 | } else { |
| 779 | /* %p */ |
| 780 | ret += 2; |
| 781 | send('0',send_arg2); |
| 782 | send('x',send_arg2); |
| 783 | ret += myvprintf_int64(send, send_arg2, flags, 16, width, True, |
| 784 | (ULong)((UWord)va_arg (vargs, void *))); |
| 785 | } |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 786 | break; |
| 787 | case 'x': /* %x */ |
sewardj | a44b15f | 2007-02-16 14:10:24 +0000 | [diff] [blame] | 788 | case 'X': /* %X */ |
| 789 | caps = toBool(format[i] == 'X'); |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 790 | if (flags & VG_MSG_ALTFORMAT) { |
| 791 | ret += 2; |
| 792 | send('0',send_arg2); |
| 793 | send('x',send_arg2); |
| 794 | } |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 795 | if (is_long) |
sewardj | a44b15f | 2007-02-16 14:10:24 +0000 | [diff] [blame] | 796 | ret += myvprintf_int64(send, send_arg2, flags, 16, width, caps, |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 797 | (ULong)(va_arg (vargs, ULong))); |
| 798 | else |
sewardj | a44b15f | 2007-02-16 14:10:24 +0000 | [diff] [blame] | 799 | ret += myvprintf_int64(send, send_arg2, flags, 16, width, caps, |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 800 | (ULong)(va_arg (vargs, UInt))); |
| 801 | break; |
| 802 | case 'c': /* %c */ |
| 803 | ret++; |
| 804 | send(va_arg (vargs, int), send_arg2); |
| 805 | break; |
| 806 | case 's': case 'S': { /* %s */ |
| 807 | char *str = va_arg (vargs, char *); |
| 808 | if (str == (char*) 0) str = "(null)"; |
| 809 | ret += myvprintf_str(send, send_arg2, |
| 810 | flags, width, str, format[i]=='S'); |
| 811 | break; |
| 812 | } |
sewardj | daf77af | 2005-07-19 14:17:37 +0000 | [diff] [blame] | 813 | |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 814 | // case 'y': { /* %y - print symbol */ |
| 815 | // Char buf[100]; |
| 816 | // Char *cp = buf; |
| 817 | // Addr a = va_arg(vargs, Addr); |
| 818 | // |
| 819 | // if (flags & VG_MSG_PAREN) |
| 820 | // *cp++ = '('; |
| 821 | // if (VG_(get_fnname_w_offset)(a, cp, sizeof(buf)-4)) { |
| 822 | // if (flags & VG_MSG_PAREN) { |
| 823 | // cp += VG_(strlen)(cp); |
| 824 | // *cp++ = ')'; |
| 825 | // *cp = '\0'; |
| 826 | // } |
| 827 | // ret += myvprintf_str(send, send_arg2, flags, width, buf, 0); |
| 828 | // } |
| 829 | // break; |
| 830 | // } |
| 831 | default: |
| 832 | break; |
| 833 | } |
| 834 | } |
| 835 | return ret; |
| 836 | } |
| 837 | |
| 838 | |
| 839 | /*------------------------------------------------------------*/ |
| 840 | /*--- Debuglog stuff. ---*/ |
| 841 | /*------------------------------------------------------------*/ |
| 842 | |
| 843 | /* Only print messages whose stated level is less than or equal to |
| 844 | this. By default, it makes this entire subsystem silent. */ |
| 845 | |
| 846 | static Int loglevel = 0; |
| 847 | |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 848 | /* Module startup. */ |
sewardj | 1075931 | 2005-05-30 23:52:47 +0000 | [diff] [blame] | 849 | /* EXPORTED */ |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 850 | void VG_(debugLog_startup) ( Int level, HChar* who ) |
| 851 | { |
| 852 | if (level < 0) level = 0; |
| 853 | if (level > 10) level = 10; |
| 854 | loglevel = level; |
| 855 | VG_(debugLog)(1, "debuglog", |
| 856 | "DebugLog system started by %s, " |
| 857 | "level %d logging requested\n", |
| 858 | who, loglevel); |
| 859 | } |
| 860 | |
sewardj | 1075931 | 2005-05-30 23:52:47 +0000 | [diff] [blame] | 861 | /* Get the logging threshold level, as set by the most recent call to |
| 862 | VG_(debugLog_startup), or zero if there have been no such calls so |
| 863 | far. */ |
| 864 | /* EXPORTED */ |
| 865 | Int VG_(debugLog_getLevel) ( void ) |
| 866 | { |
| 867 | return loglevel; |
| 868 | } |
| 869 | |
| 870 | |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 871 | /* ------------ */ |
| 872 | |
| 873 | typedef |
| 874 | struct { |
| 875 | HChar buf[100]; |
| 876 | Int n; |
| 877 | } |
| 878 | printf_buf; |
| 879 | |
| 880 | static void add_to_buf ( HChar c, void* p ) |
| 881 | { |
| 882 | printf_buf* buf = (printf_buf*)p; |
| 883 | |
| 884 | if (buf->n >= 100-10 /*paranoia*/ ) { |
| 885 | emit( buf->buf, local_strlen(buf->buf) ); |
| 886 | buf->n = 0; |
| 887 | buf->buf[buf->n] = 0; |
| 888 | } |
| 889 | buf->buf[buf->n++] = c; |
| 890 | buf->buf[buf->n] = 0; |
| 891 | } |
| 892 | |
| 893 | /* Send a logging message. Nothing is output unless 'level' |
| 894 | is <= the current loglevel. */ |
| 895 | /* EXPORTED */ |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 896 | void VG_(debugLog) ( Int level, const HChar* modulename, |
| 897 | const HChar* format, ... ) |
| 898 | { |
sewardj | c7ffc94 | 2011-03-28 16:26:42 +0000 | [diff] [blame] | 899 | UInt pid; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 900 | Int indent, depth, i; |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 901 | va_list vargs; |
| 902 | printf_buf buf; |
sewardj | a5ebfa9 | 2005-04-25 02:04:54 +0000 | [diff] [blame] | 903 | |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 904 | if (level > loglevel) |
| 905 | return; |
| 906 | |
sewardj | d85feff | 2005-04-25 02:37:56 +0000 | [diff] [blame] | 907 | indent = 2*level - 1; |
sewardj | a5ebfa9 | 2005-04-25 02:04:54 +0000 | [diff] [blame] | 908 | if (indent < 1) indent = 1; |
| 909 | |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 910 | buf.n = 0; |
| 911 | buf.buf[0] = 0; |
| 912 | pid = local_sys_getpid(); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 913 | |
| 914 | // Print one '>' in front of the messages for each level of self-hosting |
| 915 | // being performed. |
| 916 | depth = RUNNING_ON_VALGRIND; |
| 917 | for (i = 0; i < depth; i++) { |
| 918 | (void)myvprintf_str ( add_to_buf, &buf, 0, 1, ">", False ); |
| 919 | } |
| 920 | |
sewardj | a5ebfa9 | 2005-04-25 02:04:54 +0000 | [diff] [blame] | 921 | (void)myvprintf_str ( add_to_buf, &buf, 0, 2, "--", False ); |
sewardj | a44b15f | 2007-02-16 14:10:24 +0000 | [diff] [blame] | 922 | (void)myvprintf_int64 ( add_to_buf, &buf, 0, 10, 1, False, (ULong)pid ); |
sewardj | a5ebfa9 | 2005-04-25 02:04:54 +0000 | [diff] [blame] | 923 | (void)myvprintf_str ( add_to_buf, &buf, 0, 1, ":", False ); |
sewardj | a44b15f | 2007-02-16 14:10:24 +0000 | [diff] [blame] | 924 | (void)myvprintf_int64 ( add_to_buf, &buf, 0, 10, 1, False, (ULong)level ); |
sewardj | a5ebfa9 | 2005-04-25 02:04:54 +0000 | [diff] [blame] | 925 | (void)myvprintf_str ( add_to_buf, &buf, 0, 1, ":", False ); |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 926 | (void)myvprintf_str ( add_to_buf, &buf, 0, 8, (HChar*)modulename, False ); |
sewardj | a5ebfa9 | 2005-04-25 02:04:54 +0000 | [diff] [blame] | 927 | (void)myvprintf_str ( add_to_buf, &buf, 0, indent, "", False ); |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 928 | |
| 929 | va_start(vargs,format); |
| 930 | |
sewardj | c7ffc94 | 2011-03-28 16:26:42 +0000 | [diff] [blame] | 931 | (void) VG_(debugLog_vprintf) ( add_to_buf, &buf, format, vargs ); |
sewardj | 1cf558c | 2005-04-25 01:36:56 +0000 | [diff] [blame] | 932 | |
| 933 | if (buf.n > 0) { |
| 934 | emit( buf.buf, local_strlen(buf.buf) ); |
| 935 | } |
| 936 | |
| 937 | va_end(vargs); |
| 938 | } |
| 939 | |
| 940 | |
| 941 | |
| 942 | /*--------------------------------------------------------------------*/ |
| 943 | /*--- end m_debuglog.c ---*/ |
| 944 | /*--------------------------------------------------------------------*/ |