error.c 3.93 KB
/*==============================================================================
    Module: error.c
    $Revision: 1.1.1.2 $
    $Date: 2002/10/29 08:07:15 $
    $Author: blythe $
    $Source: /root/leakn64/depot/rf/sw/n64os20l/tools/gcord/com/error.c,v $

    error handling
==============================================================================*/
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <time.h>
#include <string.h>

#include <libelf.h>
#include <libdwarf.h>

#include "option.h"
#include "global.h"

extern int _doprnt(char *, va_list, FILE);
extern int errno;
extern int sys_nerr;
extern char *sys_errlist[];

#undef stderr
#define stderr stdout

/*------------------------------------------------------------------------------
  print a note, a situation where no error should occur.
  It's something that the user should be aware of.
------------------------------------------------------------------------------*/
void note(const *format, ...)
{
    va_list ap;
    printf("Note: ");
    va_start(ap, format);
    _doprnt(format, ap, stderr);
    printf("\n");
    va_end(ap);
}
/*------------------------------------------------------------------------------
  print a warning, a situation where error may occur.
------------------------------------------------------------------------------*/
void warning(const *format, ...)
{
    va_list ap;

    fprintf(stderr, "Warning: ");
    va_start(ap, format );
    _doprnt(format, ap, stderr);
    fprintf(stderr, ".\n");
    va_end(ap);
}

/*------------------------------------------------------------------------------
  print a error and exit.
------------------------------------------------------------------------------*/
void error(const *format, ...)
{
    va_list ap;

    fprintf(stderr, "Error: ");
    va_start(ap, format);
    _doprnt(format, ap, stderr);
    fprintf(stderr, ".\n");
    va_end(ap);
    exit(1);
}

/*------------------------------------------------------------------------------
 sysfatal:
  - caused by error return values of a system call when it shouldn't.
  - program exits.
------------------------------------------------------------------------------*/
void sysfatal(const *format, ...)
{
    va_list ap;

    if (errno == -1) 
    {
        fprintf(stderr, "Error: ");
        fprintf(stderr, "unexpected end of file, ");
    }
    else if (errno < sys_nerr) 
    {
        fprintf(stderr, "Error: ");
        fprintf(stderr, "%s, ", sys_errlist[errno]);
    }
    else 
    {
        fprintf(stderr, "Error: ");
        fprintf(stderr, "error code %d, ", errno);
    }
    va_start(ap, format);
    _doprnt(format, ap, stderr);
    fprintf(stderr, ".\n");
    va_end(ap);
    exit(1); 
}

/*------------------------------------------------------------------------------  
  dump info with header
------------------------------------------------------------------------------*/
void dump(const *format, ...)
{
    va_list ap;
    printf("<Debug> ");
    va_start(ap, format);
    _doprnt(format, ap, stdout);
    printf("\n");
    va_end(ap);
}

/*------------------------------------------------------------------------------    
  print a dwarf error.
------------------------------------------------------------------------------*/
void print_dwarf_error (Dwarf_Debug dbg, int dwarf_code,
        Dwarf_Error err, const *format, ...)
{
    va_list ap;
    va_start(ap, format);

    fprintf(stderr, "Dwarf Error: ");

    if(dwarf_code == DW_DLV_ERROR)
    {
        char *errmsg = dwarf_errmsg(err);
        long long errno = dwarf_errno(err);

        fprintf(stderr,"%s (%lld) ", errmsg, errno);
        dwarf_dealloc (dbg, errmsg, DW_DLA_STRING);
    }
    else if (dwarf_code == DW_DLV_NO_ENTRY)
    {
        fprintf(stderr,"NO ENTRY ");
    }
    else if (dwarf_code == DW_DLV_OK)
    {
        fprintf(stderr,"DLV_OK ");
    }
    else
    {
        fprintf(stderr,"code %d ",dwarf_code);
    }

    _doprnt(format, ap, stderr);
    fprintf(stderr,"\n");
    va_end(ap);
    exit(1);
}