Ejemplo n.º 1
0
char * __cdecl _itoa(int val, char *buf, int radix) {
    if (radix == 10 && val < 0)
        xtoa((unsigned long)val, buf, radix, 1);
    else
        xtoa((unsigned long)(unsigned int)val, buf, radix, 0);
    return buf;
}
Ejemplo n.º 2
0
disp_itoa(int val, OLECHAR FAR* buf)
{
    if (val < 0)
        xtoa((unsigned long)val, buf, 1);
    else
        xtoa((unsigned long)(unsigned int)val, buf, 0);
    return buf;
}
Ejemplo n.º 3
0
Archivo: xtoa.c Proyecto: HarryR/sanos
char *itoa(int val, char *buf, int radix) {
  if (radix == 10 && val < 0) {
    xtoa((unsigned long) val, buf, radix, 1);
  } else {
    xtoa((unsigned long)(unsigned int) val, buf, radix, 0);
  }
  return buf;
}
Ejemplo n.º 4
0
void emitInt( FILE *file, char **buffer, int val, int base, int width, int flags, int *left )
{
    char lBuf[12];	// longest is base 10, 2^32
    char *str = lBuf;

    if( flags & FL_TYPE_SIGNED_INT ) {
        xtoa(val,str,base ,(val < 0));
    } else {
        xtoa((unsigned)val,str,base ,0);
    }

    emitString(file,buffer,str,width,flags,left);
}
Ejemplo n.º 5
0
int printf(char *format, ...)
{
    char c;
    int i;
    long n;

    va_list a;
    va_start(a, format);
    while(c = *format++) {
        if(c == '%') {
            switch(c = *format++) {
                case 's':                       // String
                    puts(va_arg(a, char*));
                    break;
                case 'c':                       // Char
                    putc(va_arg(a, char));
                    break;
                case 'i':                       // 16 bit Integer
                case 'u':                       // 16 bit Unsigned
                    i = va_arg(a, int);
                    if(c == 'i' && i < 0) i = -i, putc('-');
                    xtoa((unsigned)i, dv + 5);
                    break;
                case 'l':                       // 32 bit Long
                case 'd':
                case 'n':                       // 32 bit uNsigned loNg
                    n = va_arg(a, long);
                    if(c == 'l' &&  n < 0) n = -n, putc('-');
                    xtoa((unsigned long)n, dv);
                    break;
                case 'X':                       // 16 bit heXadecimal
                    i = va_arg(a, int);
                    puth(i >> 12);
                    puth(i >> 8);
                    puth(i >> 4);
                    puth(i);
                    break;
                case 'x':                       // 8 bit heXadecimal
                    i = va_arg(a, int);
                    puth(i >> 4);
                    puth(i);
                    break;
                case 0: return inde;
                default: goto bad_fmt;
            }
        }
        else
        	bad_fmt:    putc(c);
    }
Ejemplo n.º 6
0
void p_pos_assert(const char* text, const char *file, int line)
{
// Something fatal, stay here forever.

  portEnterCritical();

  while (*text) {

    while (!(UART0_LSR & 0x20));
    UART0_THR = *text;
    ++text;
  }

  while (*file) {

    while (!(UART0_LSR & 0x20));
    UART0_THR = *file;
    ++file;
  }

  char* l = xtoa(line);
  while (*l) {

    while (!(UART0_LSR & 0x20));
    UART0_THR = *l;
    ++l;
  }

  while(1);
}
Ejemplo n.º 7
0
Archivo: uart.c Proyecto: kcoewoys/work
void xtoa(int n, char buf[])
{
	int i;
	if(n < 16)
	{
		if(n < 10)
		{
			buf[0] = n + '0';	
		}
		else
		{
			buf[0] = n - 10 + 'a';	
		}
		buf[1] = '\0';
		return;
	}

	xtoa(n / 16, buf);

	//找到末尾
	for(i = 0; buf[i] != '\0'; i++)
		;

	if( (n % 16) < 10)
	{
	    buf[i] = n % 16 + '0';	
	}
	else
	{
	    buf[i] = n % 16 - 10 + 'a';	
	}
	buf[i + 1] = '\0';
}
Ejemplo n.º 8
0
//实现可变参数的打印,主要是为了观察打印的变量。
void sprintf(char *str,char *format ,...)
{
  
   int *var=(int *)(&format)+1; //得到第一个可变参数的地址
   char buffer[10];
   char *buf=buffer;
  while(*format)
  {
      if(*format!='%')
      {
	*str++=*format++;
	continue;
      }
      else
      {
	format++;
	switch (*format)
	{
	  case 'd':itoa(*var,buf);while(*buf){*str++=*buf++;};break;
	  case 'x':xtoa(*var,buf);while(*buf){*str++=*buf++;};break;
	  case 's':buf=(char*)(*var);while(*buf){*str++=*buf++;};break;
	  
	}
	buf=buffer;
	var++;
	format++;
	
      }
    
  }
  *str='\0';
  
}
Ejemplo n.º 9
0
void xtoa(unsigned int n, char *buf)
{
	int i;
	
	if(n < 16){
		if(n < 10){
			buf[0] = n + '0';
			buf[1] = '\0';
		}else{
			buf[0] = n - 10 + 'a';
			buf[1] = '\0';
		}
		return;
	}
	xtoa(n / 16, buf);
	for(i = 0; buf[i] != '\0'; i++){
		;
	}
	if((n % 16) < 10){
		buf[i] = (n % 16) + '0';
		buf[i + 1] = '\0';
	}else{
		buf[i] = (n % 16) - 10 + 'a';
                buf[i + 1] = '\0';
	}
}
Ejemplo n.º 10
0
uint16_t s_printf(char *instr, const char *format, ...)
{
    char c;
    int i;
    long n;

    sptr = instr;
    slen = 0;
    sptr[slen] = '\0';
    va_list a;
    va_start(a, format);
    while( (c = *format++) ) {
        if(c == '%') {
            switch(c = *format++) {
                case 's':                       // String
                    s_puts(va_arg(a, char*));
                    break;
                case 'c':                       // Char
                    s_putc(va_arg(a, int));   // Char gets promoted to Int in args, so it's an int we're looking for (GCC warning)
                    break;
                case 'i':                       // 16 bit Integer
                case 'd':                       // 16 bit Integer
                case 'u':                       // 16 bit Unsigned
                    i = va_arg(a, int);
                    if( (c == 'i' || c == 'd') && i < 0 ) i = -i, s_putc('-');
                    xtoa((unsigned)i, dv + 5);
                    break;
                case 'l':                       // 32 bit Long
                case 'n':                       // 32 bit uNsigned loNg
                    n = va_arg(a, long);
                    if(c == 'l' &&  n < 0) n = -n, s_putc('-');
                    xtoa((unsigned long)n, dv);
                    break;
                case 'x':                       // 16 bit heXadecimal
                    i = va_arg(a, int);
                    puth(i >> 12);
                    puth(i >> 8);
                    puth(i >> 4);
                    puth(i);
                    break;
                case 0: return slen;
                default: goto bad_fmt;
            }
        } else
bad_fmt:    s_putc(c);
    }
Ejemplo n.º 11
0
/*
 * This is do_hvis, for HTTP style (RFC 1808)
 */
static char *
do_hvis(char *dst, int c, int flag, int nextc, const char *extra)
{
	if (!isascii(c) || !isalnum(c) || strchr("$-_.+!*'(),", c) != NULL) {
		*dst++ = '%';
		*dst++ = xtoa(((unsigned int)c >> 4) & 0xf);
		*dst++ = xtoa((unsigned int)c & 0xf);
	} else {
Ejemplo n.º 12
0
char * __cdecl _ultoa (
        unsigned long val,
        char *buf,
        int radix
        )
{
        xtoa(val, buf, radix, 0);
        return buf;
}
Ejemplo n.º 13
0
char * __cdecl _ltoa (
        long val,
        char *buf,
        int radix
        )
{
        xtoa((unsigned long)val, buf, radix, (radix == 10 && val < 0));
        return buf;
}
Ejemplo n.º 14
0
char *xtoa( char *p, unsigned x )
/*******************************/
{
    if( x > 10 ) {
        p = xtoa( p, x/10 );
        x %= 10;
    }
    *p = x + '0';
    return( ++p );
}
Ejemplo n.º 15
0
void destroy_link(int pid, char *file)
{
    char buf[64];
    
    strcpy(buf, "/proc/");
    xtoa(pid, &buf[6]);
    strcpy(&buf[strlen(buf)], "/"); 
    strcpy(&buf[strlen(buf)], file); 
    remove_proc_entry(buf, 0);
}
Ejemplo n.º 16
0
void tprintf(char *format, ...)
{
    char c;
    int i;
    long n;
    
    va_list a;
    va_start(a, format);
    while((c = *format++)) {
        if(c == '%') {
            switch(c = *format++) {
                case 's':                       // String
                    UART0_PrintString(va_arg(a, char*));
                    break;
                case 'c':                       // Char
                    UART0_Sendchar((char) va_arg(a, int));
                    break;
                case 'i':                       // 16 bit Integer
                case 'u':                       // 16 bit Unsigned
                    i = va_arg(a, int);
                    if(c == 'i' && i < 0) i = -i, UART0_Sendchar('-');
                    xtoa((unsigned)i, dv + 5);
                    break;
                case 'l':                       // 32 bit Long
                case 'n':                       // 32 bit uNsigned loNg
                    n = va_arg(a, long);
                    if(c == 'l' &&  n < 0) n = -n, UART0_Sendchar('-');
                    xtoa((unsigned long)n, dv);
                    break;
                case 'x':                       // 16 bit heXadecimal
                    i = va_arg(a, int);
                    puth(i >> 12);
                    puth(i >> 8);
                    puth(i >> 4);
                    puth(i);
                    break;
                case 0: return;
                default: goto bad_fmt;
            }
        } else
bad_fmt:    UART0_Sendchar(c);
    }
Ejemplo n.º 17
0
int doprint(char *fmt, va_list ap)
{
	int ret = 0;
	int code;
	int value = 0;
	char *p = NULL;
	char string[24] = { 0 };

	for (; ; ) {
		while (((code = *fmt) != '\0') && (code != '%')) {
			PUTC(code);
			fmt++;
			ret++;
		}
		if (code == '\0')
			goto out;

		switch (code = *++fmt) {
			case 'd':
				value = va_arg(ap, int);
				p = itoa(string, value, 24);
				break;
			case 'o':
				value = va_arg(ap, int);
				p = otoa(string, value, 24);
				break;
			case 'x':
				value = va_arg(ap, int);
				p = xtoa(string, value, 24);
				break;
			case 's':
				p = va_arg(ap, char *);
				break;
			case 'c':
				value = va_arg(ap, int);
				string[0] = (char )value;
				string[1] = '\0';
				p = string;
				break;
			default:
				p = NULL;
				break;
		}
		while (p && (*p != '\0')) {
			PUTC(*p);
			p++, ret++;
		}
		fmt++;
	}

out:
	return ret;
}
Ejemplo n.º 18
0
static const char *
prmask(const sigset_t *m, char *buf, size_t len)
{
    size_t j = 2;
    assert(len >= 3 + sizeof(*m));
    buf[0] = '0';
    buf[1] = 'x';
#define N(p, a)	(((p) >> ((a) * 4)) & 0xf)
    for (size_t i = __arraycount(m->__bits); i > 0; i--) {
        uint32_t p = m->__bits[i - 1];
        for (size_t k = sizeof(p); k > 0; k--)
            buf[j++] = xtoa(N(p, k - 1));
    }
    buf[j] = '\0';
    return buf;
}
Ejemplo n.º 19
0
/*
 * buflen のサイズは DNS_IP6_REVENT_MAXLEN 以上である必要がある.
 */
static bool
DnsResolver_expandReverseEntry6(const struct in6_addr *addr6, char *buf, size_t buflen)
{
    if (buflen < DNS_IP6_REVENT_MAXLEN) {
        return false;
    }   // end if
    const unsigned char *rawaddr = (const unsigned char *) addr6;
    const unsigned char *rawaddr_tail = rawaddr + NS_IN6ADDRSZ;
    char *bufp = buf;
    for (; rawaddr < rawaddr_tail; ++rawaddr) {
        *(bufp++) = xtoa((*(rawaddr++) & 0xf0) >> 4);
        *(bufp++) = '.';
        *(bufp++) = xtoa(*(rawaddr++) & 0x0f);
        *(bufp++) = '.';
    }   // end for
    memcpy(bufp, DNS_IP6_REVENT_SUFFIX, sizeof(DNS_IP6_REVENT_SUFFIX)); // 終端のNULL文字もコピーする
    return true;
}   // end function : DnsResolver_expandReverseEntry6
Ejemplo n.º 20
0
/*
 * This is do_hvis, for HTTP style (RFC 1808)
 */
static wchar_t *
do_hvis(wchar_t *dst, wint_t c, int flags, wint_t nextc, const wchar_t *extra)
{
	if (iswalnum(c)
	    /* safe */
	    || c == L'$' || c == L'-' || c == L'_' || c == L'.' || c == L'+'
	    /* extra */
	    || c == L'!' || c == L'*' || c == L'\'' || c == L'(' || c == L')'
	    || c == L',')
		dst = do_svis(dst, c, flags, nextc, extra);
	else {
		*dst++ = L'%';
		*dst++ = xtoa(((unsigned int)c >> 4) & 0xf);
		*dst++ = xtoa((unsigned int)c & 0xf);
	}

	return dst;
}
Ejemplo n.º 21
0
/*
 * This is do_hvis, for HTTP style (RFC 1808)
 */
static char *
do_hvis(char *dst, int c, int flag, int nextc, const char *extra)
{

	if ((isascii(c) && isalnum(c))
	    /* safe */
	    || c == '$' || c == '-' || c == '_' || c == '.' || c == '+'
	    /* extra */
	    || c == '!' || c == '*' || c == '\'' || c == '(' || c == ')'
	    || c == ',') {
		dst = do_svis(dst, c, flag, nextc, extra);
	} else {
		*dst++ = '%';
		*dst++ = xtoa(((unsigned int)c >> 4) & 0xf);
		*dst++ = xtoa((unsigned int)c & 0xf);
	}

	return dst;
}
Ejemplo n.º 22
0
Archivo: uart.c Proyecto: kcoewoys/work
int _printf(const char *fmt, ...)
{
	va_list ap;

	int n;
	char buf[64];
	char *s;
	
	va_start(ap, fmt);

	while(*fmt)
	{
		if(*fmt == '%')
		{
			fmt++;
			switch(*fmt)
			{
				case 'd':
					n = va_arg(ap, int);
					itoa(n, buf);
					_puts(buf);
					break;
				case 'x':
					n = va_arg(ap, int);
					xtoa(n, buf);
					_puts(buf);
					break;
				case 'c':
					n = va_arg(ap, int);
					_putc(n);
					break;
				case 's':
					s = va_arg(ap, char *);
					_puts(s);
					break;
				default:
					break;
			}	
		}
		else
		{
Ejemplo n.º 23
0
int myprintf(const char *fmt, ...)
{
	va_list ap;
	int n;
	char *s;
	char buf[64];

	va_start(ap, fmt);
	while(*fmt != '\0'){
		if(*fmt == '%'){
			fmt++;
			switch(*fmt){
				case 'd':
					n = va_arg(ap, int);
					if(n < 0){
						uputchar('-');
						n =0 - n;
					}
					itoa(n, buf);
					_uputs(buf);
					break;
				case 's':
					s = va_arg(ap, char *);
					_uputs(s);
					break;
				case 'c':
					n = va_arg(ap, int);
					uputchar(n);
					break;
				case 'x':
					n = va_arg(ap, int);
					xtoa(n, buf);
					_uputs(buf);
					break;
				default:
					break;
			}
		}else{
Ejemplo n.º 24
0
char *xtoa(unsigned long num)
{// 65 0X41 "0x41"
	int i = 0;
	if(num < 10){
		U_tmp_buf[0] = num + '0';
		U_tmp_buf[1] = '\0';
	} else if(num >= 10 && num < 16){
		U_tmp_buf[0] = num - 10 + 'A';
		U_tmp_buf[1] = '\0';
	} else {
		xtoa(num / 16);
		while(U_tmp_buf[i]){
			i++;
		}
		if(num % 16 < 10){
			U_tmp_buf[i] = num % 16 + '0';
		} else {
			U_tmp_buf[i] = num % 16 - 10 + 'A';
		}
		U_tmp_buf[i + 1] = '\0';
	}
	return U_tmp_buf;
}
Ejemplo n.º 25
0
Archivo: http.c Proyecto: hdl/mips
static void MyPresent(IPSocket *socket, char *request, int bytes)
{   char i=0;
	uint8_t plain[8];
	uint8_t key[10];
	uint8_t cipher[8];
   char *text="HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"
              "<html><body><h2>PRESENT Encryption</h2><br>"
			  "64bits plain, 80bits key, 64bits cipher<br>";
	char *br="<br>";
   // (void)request;
	(void)bytes;
	atox(request+21,plain,8);
	atox(request+40,key,10);
    IPWrite(socket, (uint8*)text, (int)strlen(text));
	
	present(plain,key,cipher);
	for (i=0;i<8;i++)
	{
	IPWrite(socket, (uint8*)(xtoa(plain[i])+1), 1);
	IPWrite(socket, (uint8*)(xtoa(plain[i])), 1);
	}
	IPWrite(socket, (uint8*)br, (int)strlen(br));

	for (i=0;i<10;i++)
	{
	IPWrite(socket, (uint8*)(xtoa(key[i])+1),1);
	IPWrite(socket, (uint8*)(xtoa(key[i])), 1);
	}
	IPWrite(socket, (uint8*)br, (int)strlen(br));

	for (i=0;i<8;i++)
	{
	IPWrite(socket, (uint8*)(xtoa(cipher[i])+1), 1);
	IPWrite(socket, (uint8*)(xtoa(cipher[i])),1);
	}
	IPWrite(socket, (uint8*)br, (int)strlen(br));

	IPClose(socket);
}
Ejemplo n.º 26
0
static int GetExtName( cg_sym_handle sym, char *buffer, int max_len )
/*******************************************************************/
{
    const char           *src;
    char                 *dst;
    const char           *p;
    const char           *prefix;
    const char           *sufix;
    char                 *dst_basename;
    char                 *tmp_suffix;
    char                 c;
    int                  base_len;
    int                  dst_len;
    const char           *pattern;

    pattern = FEExtName( sym, EXTN_PATTERN );
    c = '\0';
    base_len = 0;
    prefix = pattern;
    for( p = pattern; *p != '\0'; p++ ) {
        if(( *p == '*' ) || ( *p == '!' ) || ( *p == '^' )) {
            if( c == '\0' ) {
                prefix = p;
                c = *p;
            }
        } else if( ( c != '\0' ) || ( *p == '#' ) ) {
            break;
        } else if( *p == '\\' ) {
            p++;
        }
    }
    if( c == '\0' )
        base_len = p - pattern;
    sufix = p;
    // add prefix to output buffer
    dst = buffer;
    for( src = pattern; src != prefix; ++src ) {
        if( *src != '\\' ) {
            *(dst++) = *src;
        }
    }
    *dst = '\0';
    // add sufix to output buffer
    dst_basename = dst;
    for( src = sufix; *src != '\0'; ++src ) {
        if( *src == '#' ) {
            unsigned    size;

            size = (unsigned)(pointer_int)FEExtName( sym, EXTN_PRMSIZE );
            if( size != (unsigned)-1 ) {
                *(dst++) = '@';
                dst = xtoa( dst, size );
            }
        } else {
            if( *src == '\\' )
                ++src;
            *(dst++) = *src;
        }
    }
    *dst = '\0';
    // max base name length
    dst_len = max_len - ( dst - buffer );
    if( dst_len > 0 ) {
        int     sufix_len;
        int     len;

        if( base_len != 0 ) {
            // alias
            src = pattern;
        } else {
            src = FEExtName( sym, EXTN_BASENAME );
            base_len = strlen( src );
        }
        // shift sufix to the end of buffer
        sufix_len = dst - dst_basename;
        tmp_suffix = buffer + max_len - sufix_len;
        memmove( tmp_suffix, dst_basename, sufix_len + 1 );
        // copy + truncate base symbol name
        len = copyBaseName( c, dst_basename, dst_len, src, base_len );
        if( len < 0 ) {
            FEMessage( MSG_SYMBOL_TOO_LONG, sym );
        } else {
            // shift sufix to the end of symbol name
            memcpy( dst_basename + len, tmp_suffix, sufix_len + 1 );
        }
    } else {
       // TODO: error prefix + sufix >= max_len
       assert( 0 );
    }
    return( 0 );
}
Ejemplo n.º 27
0
/* show_backtrace displays the backtrace, currently obtained by means of
   the glibc backtrace* functions.  */
void
show_backtrace (void)
{
#if GLIBC_BACKTRACE

#define DEPTH 50
#define BUFSIZE 1024

    void *trace[DEPTH];
    char **str;
    int depth;

    depth = backtrace (trace, DEPTH);
    if (depth <= 0)
        return;

    str = backtrace_symbols (trace, depth);

#if CAN_PIPE

#ifndef STDIN_FILENO
#define STDIN_FILENO 0
#endif

#ifndef STDOUT_FILENO
#define STDOUT_FILENO 1
#endif

#ifndef STDERR_FILENO
#define STDERR_FILENO 2
#endif

    /* We attempt to extract file and line information from addr2line.  */
    do
    {
        /* Local variables.  */
        int f[2], pid, line, i;
        FILE *output;
        char addr_buf[DEPTH][GFC_XTOA_BUF_SIZE], func[BUFSIZE], file[BUFSIZE];
        char *p, *end;
        const char *addr[DEPTH];

        /* Write the list of addresses in hexadecimal format.  */
        for (i = 0; i < depth; i++)
            addr[i] = xtoa ((GFC_UINTEGER_LARGEST) (intptr_t) trace[i], addr_buf[i],
                            sizeof (addr_buf[i]));

        /* Don't output an error message if something goes wrong, we'll simply
           fall back to the pstack and glibc backtraces.  */
        if (pipe (f) != 0)
            break;
        if ((pid = fork ()) == -1)
            break;

        if (pid == 0)
        {
            /* Child process.  */
#define NUM_FIXEDARGS 5
            char *arg[DEPTH+NUM_FIXEDARGS+1];

            close (f[0]);
            close (STDIN_FILENO);
            close (STDERR_FILENO);

            if (dup2 (f[1], STDOUT_FILENO) == -1)
                _exit (0);
            close (f[1]);

            arg[0] = (char *) "addr2line";
            arg[1] = (char *) "-e";
            arg[2] = full_exe_path ();
            arg[3] = (char *) "-f";
            arg[4] = (char *) "-s";
            for (i = 0; i < depth; i++)
                arg[NUM_FIXEDARGS+i] = (char *) addr[i];
            arg[NUM_FIXEDARGS+depth] = NULL;
            execvp (arg[0], arg);
            _exit (0);
#undef NUM_FIXEDARGS
        }

        /* Father process.  */
        close (f[1]);
        wait (NULL);
        output = fdopen (f[0], "r");
        i = -1;

        if (fgets (func, sizeof(func), output))
        {
            st_printf ("\nBacktrace for this error:\n");

            do
            {
                if (! fgets (file, sizeof(file), output))
                    goto fallback;

                i++;

                for (p = func; *p != '\n' && *p != '\r'; p++)
                    ;

                *p = '\0';

                /* Try to recognize the internal libgfortran functions.  */
                if (strncasecmp (func, "*_gfortran", 10) == 0
                        || strncasecmp (func, "_gfortran", 9) == 0
                        || strcmp (func, "main") == 0 || strcmp (func, "_start") == 0
                        || strcmp (func, "_gfortrani_handler") == 0)
                    continue;

                if (local_strcasestr (str[i], "libgfortran.so") != NULL
                        || local_strcasestr (str[i], "libgfortran.dylib") != NULL
                        || local_strcasestr (str[i], "libgfortran.a") != NULL)
                    continue;

                /* If we only have the address, use the glibc backtrace.  */
                if (func[0] == '?' && func[1] == '?' && file[0] == '?'
                        && file[1] == '?')
                {
                    st_printf ("  + %s\n", str[i]);
                    continue;
                }

                /* Extract the line number.  */
                for (end = NULL, p = file; *p; p++)
                    if (*p == ':')
                        end = p;
                if (end != NULL)
                {
                    *end = '\0';
                    line = atoi (++end);
                }
                else
                    line = -1;

                if (strcmp (func, "MAIN__") == 0)
                    st_printf ("  + in the main program\n");
                else
                    st_printf ("  + function %s (0x%s)\n", func, addr[i]);

                if (line <= 0 && strcmp (file, "??") == 0)
                    continue;

                if (line <= 0)
                    st_printf ("    from file %s\n", file);
                else
                    st_printf ("    at line %d of file %s\n", line, file);
            }
            while (fgets (func, sizeof(func), output));

            free (str);
            return;

fallback:
            st_printf ("** Something went wrong while running addr2line. **\n"
                       "** Falling back  to a simpler  backtrace scheme. **\n");
        }
    }
    while (0);

#undef DEPTH
#undef BUFSIZE

#endif
#endif

#if CAN_FORK && defined(HAVE_GETPPID)
    /* Try to call pstack.  */
    do
    {
        /* Local variables.  */
        int pid;

        /* Don't output an error message if something goes wrong, we'll simply
           fall back to the pstack and glibc backtraces.  */
        if ((pid = fork ()) == -1)
            break;

        if (pid == 0)
        {
            /* Child process.  */
#define NUM_ARGS 2
            char *arg[NUM_ARGS+1];
            char buf[20];

            st_printf ("\nBacktrace for this error:\n");
            arg[0] = (char *) "pstack";
#ifdef HAVE_SNPRINTF
            snprintf (buf, sizeof(buf), "%d", (int) getppid ());
#else
            sprintf (buf, "%d", (int) getppid ());
#endif
            arg[1] = buf;
            arg[2] = NULL;
            execvp (arg[0], arg);
#undef NUM_ARGS

            /* pstack didn't work, so we fall back to dumping the glibc
               backtrace if we can.  */
#if GLIBC_BACKTRACE
            dump_glibc_backtrace (depth, str);
#else
            st_printf ("  unable to produce a backtrace, sorry!\n");
#endif

            _exit (0);
        }

        /* Father process.  */
        wait (NULL);
        return;
    }
    while(0);
#endif

#if GLIBC_BACKTRACE
    /* Fallback to the glibc backtrace.  */
    st_printf ("\nBacktrace for this error:\n");
    dump_glibc_backtrace (depth, str);
#endif
}
Ejemplo n.º 28
0
void boinc_catch_signal(int signal, struct siginfo *siginfo, void *sigcontext) {
#else
void boinc_catch_signal(int signal) {
#endif
    switch(signal) {
    case SIGHUP: fprintf(stderr, "SIGHUP: terminal line hangup\n");
         return;
    case SIGINT: fprintf(stderr, "SIGINT: interrupt program\n"); break;
    case SIGILL: fprintf(stderr, "SIGILL: illegal instruction\n"); break;
    case SIGABRT: fprintf(stderr, "SIGABRT: abort called\n"); break;
#if SIGBUS != SIGSEGV
    // in case SIGBUS == SIGSEGV (e.g., Haiku)
    case SIGBUS: fprintf(stderr, "SIGBUS: bus error\n"); break;
#endif
    case SIGSEGV: fprintf(stderr, "SIGSEGV: segmentation violation\n"); break;
    case SIGSYS: fprintf(stderr, "SIGSYS: system call given invalid argument\n"); break;
    case SIGPIPE: fprintf(stderr, "SIGPIPE: write on a pipe with no reader\n");
        return;
    default: fprintf(stderr, "unknown signal %d\n", signal); break;
    }

#ifdef __GLIBC__
    void *array[64];
    size_t size;
    size = backtrace (array, 64);
//  Anything that calls malloc here (i.e *printf()) will probably fail
//  so we'll do it the hard way.
    (void) write(fileno(stderr),"Stack trace (",strlen("Stack trace ("));
    char mbuf[10];
    char *p=mbuf+9;
    int i=size;
    *(p--)=0;
    while (i) {
      *(p--)=i%10+'0';
      i/=10;
    }
    (void) write(fileno(stderr),p+1,strlen(p+1));
    (void) write(fileno(stderr)," frames):",strlen(" frames):"));
    mbuf[0]=10;
    (void) write(fileno(stderr),mbuf,1);
    backtrace_symbols_fd(array, size, fileno(stderr));
#endif

#ifdef __APPLE__
    PrintBacktrace();
#endif

#ifdef ANDROID
    // this is some dark undocumented Android voodoo that uses libcorkscrew.so
    // minimal use of library functions because they may not work in an signal
    // handler.
#define DUMP_LINE_LEN 256
    static backtrace_frame_t backtrace[64];
    static backtrace_symbol_t backtrace_symbols[64]; 
    if (unwind_backtrace_signal_arch != NULL) {
        map_info_t *map_info=acquire_my_map_info_list();
        ssize_t size=unwind_backtrace_signal_arch(siginfo,sigcontext,map_info,backtrace,0,64);
        get_backtrace_symbols(backtrace,size,backtrace_symbols);
        char line[DUMP_LINE_LEN];
        for (int i=0;i<size;i++) {
            format_backtrace_line(i,&backtrace[i],&backtrace_symbols[i],line,DUMP_LINE_LEN);
            line[DUMP_LINE_LEN-1]=0;
            if (backtrace_symbols[i].symbol_name) {
                strlcat(line," ",DUMP_LINE_LEN);
                if (backtrace_symbols[i].demangled_name) {
                   strlcat(line,backtrace_symbols[i].demangled_name,DUMP_LINE_LEN);
                }
            } else {
                symbol_table_t* symbols = NULL;
                if (backtrace_symbols[i].map_name) {
                    symbols = load_symbol_table(backtrace_symbols[i].map_name);
                } else {
                    symbols = load_symbol_table(argv0);
                }
                symbol_t* symbol = NULL;
                if (symbols) {
                    symbol = find_symbol(symbols, backtrace[i].absolute_pc);
                }
                if (symbol) {
                    int offset = backtrace[i].absolute_pc - symbol->start;
                    strlcat(line," (",DUMP_LINE_LEN);
                    strlcat(line,symbol->name,DUMP_LINE_LEN);
                    strlcat(line,"+",DUMP_LINE_LEN);
                    strlcat(line,xtoa(offset),DUMP_LINE_LEN);
                    strlcat(line,")",DUMP_LINE_LEN);
                    line[DUMP_LINE_LEN-1]=0;
                } else {
                    strlcat(line, " (\?\?\?)",DUMP_LINE_LEN);
                }
                if (symbols) free_symbol_table(symbols);
            }
            if (backtrace[i].absolute_pc) {
              strlcat(line," [",DUMP_LINE_LEN);
              strlcat(line,xtoa(*reinterpret_cast<unsigned int *>(backtrace[i].absolute_pc)),DUMP_LINE_LEN);
              strlcat(line,"]",DUMP_LINE_LEN);
            }
            strlcat(line,"\n",DUMP_LINE_LEN);
            write(fileno(stderr),line,strlen(line));
            fflush(stderr);
        }
    }
#endif // ANDROID

    fprintf(stderr, "\nExiting...\n");
    _exit(signal_exit_code);
}

#endif

//
// Diagnostics Routines common to all Platforms
//

// Converts the BOINCTRACE macro into a single string and report it
//   to the CRT so it can be reported via the normal means.
//
void boinc_trace(const char *pszFormat, ...) {
    static char szBuffer[4096];
    static char szDate[64];
    static char szTime[64];
    int n;

    // Trace messages should only be reported if running as a standalone
    //   application or told too.
    if ((flags & BOINC_DIAG_TRACETOSTDERR) ||
        (flags & BOINC_DIAG_TRACETOSTDOUT)) {

        memset(szBuffer, 0, sizeof(szBuffer));
        memset(szDate, 0, sizeof(szDate));
        memset(szTime, 0, sizeof(szTime));

#ifdef _WIN32
        strdate(szDate);
        strtime(szTime);
#else
        time_t t;
        char *theCR;
    
        time(&t);
        safe_strcpy(szTime, asctime(localtime(&t)));
        theCR = strrchr(szTime, '\n');
        if (theCR) *theCR = '\0';
        theCR = strrchr(szTime, '\r');
        if (theCR) *theCR = '\0';
#endif

        va_list ptr;
        va_start(ptr, pszFormat);

        vsnprintf(szBuffer, sizeof(szBuffer), pszFormat, ptr);

        va_end(ptr);

#if defined(_WIN32) && defined(_DEBUG)
        n = _CrtDbgReport(_CRT_WARN, NULL, NULL, NULL, "[%s %s] TRACE [%d]: %s", szDate, szTime, GetCurrentThreadId(), szBuffer);
#else
        if (flags & BOINC_DIAG_TRACETOSTDERR) {
#ifdef _WIN32
            n = fprintf(stderr, "[%s %s] TRACE [%d]: %s\n", szDate, szTime, GetCurrentThreadId(), szBuffer);
#else
            n = fprintf(stderr, "[%s] TRACE: %s\n", szTime, szBuffer);
#endif
            if (n > 0) stderr_file_size += n;
        }

        if (flags & BOINC_DIAG_TRACETOSTDOUT) {
#ifdef _WIN32
            n = fprintf(stdout, "[%s %s] TRACE [%d]: %s\n", szDate, szTime, GetCurrentThreadId(), szBuffer);
#else
            n = fprintf(stdout, "[%s] TRACE: %s\n", szTime, szBuffer);
#endif
            if (n > 0) stdout_file_size += n;
        }
#endif
    }
}


// Converts the BOINCINFO macro into a single string and report it
//   to stderr so it can be reported via the normal means.
//
#ifndef BOINC_INFOMSGS
void boinc_info(const char* /*pszFormat*/, ... ){ return; }
#else
void boinc_info(const char* pszFormat, ...){
    static char szBuffer[4096];
    static char szDate[64];
    static char szTime[64];
    int n;

    memset(szBuffer, 0, sizeof(szBuffer));
    memset(szDate, 0, sizeof(szDate));
    memset(szTime, 0, sizeof(szTime));

    strdate(szDate);
    strtime(szTime);

    va_list ptr;
    va_start(ptr, pszFormat);

    vsnprintf(szBuffer, sizeof(szBuffer), pszFormat, ptr);

    va_end(ptr);

#if defined(_WIN32) && defined(_DEBUG)
    _CrtDbgReport(_CRT_WARN, NULL, NULL, NULL, "[%s %s] BOINCMSG: %s\n", szDate, szTime, szBuffer);
#else
    if (flags & BOINC_DIAG_TRACETOSTDERR) {
        n = fprintf(stderr, "[%s %s] BOINCMSG: %s\n", szDate, szTime, szBuffer);
        if (n > 0) stderr_file_size += n;
    }

    if (flags & BOINC_DIAG_TRACETOSTDOUT) {
        n = fprintf(stdout, "[%s %s] BOINCMSG: %s\n", szDate, szTime, szBuffer);
        if (n > 0) stdout_file_size += n;
    }
#endif
}
#endif

void diagnostics_set_max_file_sizes(int stdout_size, int stderr_size) {
    if (stdout_size) max_stdout_file_size = stdout_size;
    if (stderr_size) max_stderr_file_size = stderr_size;
}

// Dump string to whatever the platform debuggers
// 
#ifndef _WIN32
int diagnostics_trace_to_debugger(const char*) {
    return 0;
}
Ejemplo n.º 29
0
disp_ultoa(unsigned long val, OLECHAR FAR* buf)
{
    xtoa(val, buf, FALSE);
    return buf;
}
Ejemplo n.º 30
0
disp_ltoa(long val, OLECHAR FAR* buf)
{
    xtoa((unsigned long)val, buf, (val < 0));
    return buf;
}