📄 vex_util.c
字号:
/* Write P into the buffer according to these args: * If SIGN is true, p is a signed. * BASE is the base. * If WITH_ZERO is true, '0' must be added. * WIDTH is the width of the field. */static UIntmyvprintf_int64 ( void(*send)(HChar), Int flags, Int base, Int width, ULong p){ HChar buf[40]; Int ind = 0; Int i, nc = 0; Bool neg = False; HChar *digits = "0123456789ABCDEF"; UInt ret = 0; if (base < 2 || base > 16) return ret; if ((flags & VG_MSG_SIGNED) && (Long)p < 0) { p = - (Long)p; neg = True; } if (p == 0) buf[ind++] = '0'; else { while (p > 0) { if ((flags & VG_MSG_COMMA) && 10 == base && 0 == (ind-nc) % 3 && 0 != ind) { buf[ind++] = ','; nc++; } buf[ind++] = digits[p % base]; p /= base; } } if (neg) buf[ind++] = '-'; if (width > 0 && !(flags & VG_MSG_LJUSTIFY)) { for(; ind < width; ind++) { vassert(ind < 39); buf[ind] = toHChar((flags & VG_MSG_ZJUSTIFY) ? '0': ' '); } } /* Reverse copy to buffer. */ ret += ind; for (i = ind -1; i >= 0; i--) { send(buf[i]); } if (width > 0 && (flags & VG_MSG_LJUSTIFY)) { for(; ind < width; ind++) { ret++; send(' '); // Never pad with zeroes on RHS -- changes the value! } } return ret;}/* A simple vprintf(). */static UInt vprintf_wrk ( void(*send)(HChar), const HChar *format, va_list vargs ){ UInt ret = 0; int i; int flags; int width; Bool is_long; /* We assume that vargs has already been initialised by the caller, using va_start, and that the caller will similarly clean up with va_end. */ for (i = 0; format[i] != 0; i++) { if (format[i] != '%') { send(format[i]); ret++; continue; } i++; /* A '%' has been found. Ignore a trailing %. */ if (format[i] == 0) break; if (format[i] == '%') { /* `%%' is replaced by `%'. */ send('%'); ret++; continue; } flags = 0; is_long = False; width = 0; /* length of the field. */ if (format[i] == '(') { flags |= VG_MSG_PAREN; i++; } /* If ',' follows '%', commas will be inserted. */ if (format[i] == ',') { flags |= VG_MSG_COMMA; i++; } /* If '-' follows '%', justify on the left. */ if (format[i] == '-') { flags |= VG_MSG_LJUSTIFY; i++; } /* If '0' follows '%', pads will be inserted. */ if (format[i] == '0') { flags |= VG_MSG_ZJUSTIFY; i++; } /* Compute the field length. */ while (format[i] >= '0' && format[i] <= '9') { width *= 10; width += format[i++] - '0'; } while (format[i] == 'l') { i++; is_long = True; } switch (format[i]) { case 'd': /* %d */ flags |= VG_MSG_SIGNED; if (is_long) ret += myvprintf_int64(send, flags, 10, width, (ULong)(va_arg (vargs, Long))); else ret += myvprintf_int64(send, flags, 10, width, (ULong)(va_arg (vargs, Int))); break; case 'u': /* %u */ if (is_long) ret += myvprintf_int64(send, flags, 10, width, (ULong)(va_arg (vargs, ULong))); else ret += myvprintf_int64(send, flags, 10, width, (ULong)(va_arg (vargs, UInt))); break; case 'p': /* %p */ ret += 2; send('0'); send('x'); ret += myvprintf_int64(send, flags, 16, width, (ULong)((HWord)va_arg (vargs, void *))); break; case 'x': /* %x */ if (is_long) ret += myvprintf_int64(send, flags, 16, width, (ULong)(va_arg (vargs, ULong))); else ret += myvprintf_int64(send, flags, 16, width, (ULong)(va_arg (vargs, UInt))); break; case 'c': /* %c */ ret++; send(toHChar(va_arg (vargs, int))); break; case 's': case 'S': { /* %s */ char *str = va_arg (vargs, char *); if (str == (char*) 0) str = "(null)"; ret += myvprintf_str(send, flags, width, str, toBool(format[i]=='S')); break; }# if 0 case 'y': { /* %y - print symbol */ Char buf[100]; Char *cp = buf; Addr a = va_arg(vargs, Addr); if (flags & VG_MSG_PAREN) *cp++ = '('; if (VG_(get_fnname_w_offset)(a, cp, sizeof(buf)-4)) { if (flags & VG_MSG_PAREN) { cp += VG_(strlen)(cp); *cp++ = ')'; *cp = '\0'; } ret += myvprintf_str(send, flags, width, buf, 0); } break; }# endif default: break; } } return ret;}/* A general replacement for printf(). Note that only low-level debugging info should be sent via here. The official route is to to use vg_message(). This interface is deprecated.*/static HChar myprintf_buf[1000];static Int n_myprintf_buf;static void add_to_myprintf_buf ( HChar c ){ if (c == '\n' || n_myprintf_buf >= 1000-10 /*paranoia*/ ) { (*vex_log_bytes)( myprintf_buf, vex_strlen(myprintf_buf) ); n_myprintf_buf = 0; myprintf_buf[n_myprintf_buf] = 0; } myprintf_buf[n_myprintf_buf++] = c; myprintf_buf[n_myprintf_buf] = 0;}UInt vex_printf ( const char *format, ... ){ UInt ret; va_list vargs; va_start(vargs,format); n_myprintf_buf = 0; myprintf_buf[n_myprintf_buf] = 0; ret = vprintf_wrk ( add_to_myprintf_buf, format, vargs ); if (n_myprintf_buf > 0) { (*vex_log_bytes)( myprintf_buf, n_myprintf_buf ); } va_end(vargs); return ret;}/* A general replacement for sprintf(). */static HChar *vg_sprintf_ptr;static void add_to_vg_sprintf_buf ( HChar c ){ *vg_sprintf_ptr++ = c;}UInt vex_sprintf ( HChar* buf, const HChar *format, ... ){ Int ret; va_list vargs; vg_sprintf_ptr = buf; va_start(vargs,format); ret = vprintf_wrk ( add_to_vg_sprintf_buf, format, vargs ); add_to_vg_sprintf_buf(0); va_end(vargs); vassert(vex_strlen(buf) == ret); return ret;}/*---------------------------------------------------------------*//*--- end vex_util.c ---*//*---------------------------------------------------------------*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -