1179187Sjb/*-
2179187Sjb * Copyright (c) 2007 John Birrell (jb@freebsd.org)
3179187Sjb * All rights reserved.
4179187Sjb *
5179187Sjb * Redistribution and use in source and binary forms, with or without
6179187Sjb * modification, are permitted provided that the following conditions
7179187Sjb * are met:
8179187Sjb * 1. Redistributions of source code must retain the above copyright
9179187Sjb *    notice, this list of conditions and the following disclaimer.
10179187Sjb * 2. Redistributions in binary form must reproduce the above copyright
11179187Sjb *    notice, this list of conditions and the following disclaimer in the
12179187Sjb *    documentation and/or other materials provided with the distribution.
13179187Sjb *
14179187Sjb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15179187Sjb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16179187Sjb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17179187Sjb * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18179187Sjb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19179187Sjb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20179187Sjb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21179187Sjb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22179187Sjb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23179187Sjb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24179187Sjb * SUCH DAMAGE.
25179187Sjb *
26179187Sjb * $FreeBSD$
27179187Sjb */
28179187Sjb
29179187Sjb#include <stdio.h>
30179187Sjb#include "_libdwarf.h"
31179187Sjb
32241720Sedstatic const char *_libdwarf_errors[] = {
33179187Sjb#define	DEFINE_ERROR(N,S)		[DWARF_E_##N] = S
34179187Sjb	DEFINE_ERROR(NONE,		"No Error"),
35179187Sjb	DEFINE_ERROR(ERROR,		"An error"),
36179187Sjb	DEFINE_ERROR(NO_ENTRY,		"No entry found"),
37179187Sjb	DEFINE_ERROR(ARGUMENT,		"Invalid argument"),
38179187Sjb	DEFINE_ERROR(DEBUG_INFO,	"Debug info NULL"),
39179187Sjb	DEFINE_ERROR(MEMORY,		"Insufficient memory"),
40179187Sjb	DEFINE_ERROR(ELF,		"ELF error"),
41179187Sjb	DEFINE_ERROR(INVALID_CU,	"Invalid compilation unit data"),
42179187Sjb	DEFINE_ERROR(CU_VERSION,	"Wrong CU version. Only 2 and 3 supported"),
43179187Sjb	DEFINE_ERROR(MISSING_ABBREV,	"Abbrev not found"),
44179187Sjb	DEFINE_ERROR(NOT_IMPLEMENTED,	"Unimplemented code at"),
45179187Sjb	DEFINE_ERROR(CU_CURRENT,	"No current compilation unit"),
46179187Sjb	DEFINE_ERROR(BAD_FORM,		"Wrong form type for attribute value"),
47179187Sjb	DEFINE_ERROR(INVALID_EXPR,	"Invalid DWARF expression"),
48179187Sjb	DEFINE_ERROR(NUM,		"Unknown DWARF error")
49179187Sjb#undef	DEFINE_ERROR
50179187Sjb};
51179187Sjb
52179187Sjbconst char *
53179187Sjbdwarf_errmsg(Dwarf_Error *error)
54179187Sjb{
55179187Sjb	const char *p;
56179187Sjb
57179187Sjb	if (error == NULL)
58179187Sjb		return NULL;
59179187Sjb
60179187Sjb	if (error->err_error < 0 || error->err_error >= DWARF_E_NUM)
61179187Sjb		return _libdwarf_errors[DWARF_E_NUM];
62179187Sjb	else if (error->err_error == DWARF_E_NONE)
63179187Sjb		return _libdwarf_errors[DWARF_E_NONE];
64179187Sjb	else
65179187Sjb		p = _libdwarf_errors[error->err_error];
66179187Sjb
67179187Sjb	if (error->err_error == DWARF_E_ELF)
68179187Sjb		snprintf(error->err_msg, sizeof(error->err_msg),
69179187Sjb		    "ELF error : %s [%s(%d)]", elf_errmsg(error->elf_error),
70179187Sjb		    error->err_func, error->err_line);
71179187Sjb	else
72179187Sjb		snprintf(error->err_msg, sizeof(error->err_msg),
73179187Sjb		    "%s [%s(%d)]", p, error->err_func, error->err_line);
74179187Sjb
75179187Sjb	return (const char *) error->err_msg;
76179187Sjb}
77