#include "config.h"
#include "dwarf_incl.h"
#include <stdio.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
#include "dwarf_frame.h"
#include "dwarf_harmless.h"
int dwarf_get_harmless_error_list(Dwarf_Debug dbg,
unsigned count,
const char ** errmsg_ptrs_array,
unsigned * errs_count)
{
struct Dwarf_Harmless_s *dhp = &dbg->de_harmless_errors;
if (!dhp->dh_errors) {
dhp->dh_errs_count = 0;
return DW_DLV_NO_ENTRY;
}
if (dhp->dh_errs_count == 0) {
return DW_DLV_NO_ENTRY;
}
if (errs_count) {
*errs_count = dhp->dh_errs_count;
}
if (count) {
--count;
errmsg_ptrs_array[count] = 0;
if (dhp->dh_next_to_use != dhp->dh_first) {
unsigned i = 0;
unsigned cur = dhp->dh_first;
for (i = 0; cur != dhp->dh_next_to_use; ++i) {
if (i >= count ) {
break;
}
errmsg_ptrs_array[i] = dhp->dh_errors[cur];
cur = (cur +1) % dhp->dh_maxcount;
}
errmsg_ptrs_array[i] = 0;
}
}
dhp->dh_next_to_use = 0;
dhp->dh_first = 0;
dhp->dh_errs_count = 0;
return DW_DLV_OK;
}
static void
safe_strncpy(char *targ, char *src, unsigned spaceavail)
{
unsigned goodcount = spaceavail-1;
if (spaceavail < 1) {
return;
}
strncpy(targ,src,goodcount);
targ[goodcount] = 0;
}
void dwarf_insert_harmless_error(Dwarf_Debug dbg,
char *newerror)
{
struct Dwarf_Harmless_s *dhp = &dbg->de_harmless_errors;
unsigned next = 0;
unsigned cur = dhp->dh_next_to_use;
char *msgspace;
if (!dhp->dh_errors) {
dhp->dh_errs_count++;
return;
}
msgspace = dhp->dh_errors[cur];
safe_strncpy(msgspace, newerror,DW_HARMLESS_ERROR_MSG_STRING_SIZE);
next = (cur+1) % dhp->dh_maxcount;
dhp->dh_errs_count++;
dhp->dh_next_to_use = next;
if (dhp->dh_next_to_use == dhp->dh_first) {
dhp->dh_first = (dhp->dh_first+1) % dhp->dh_maxcount;
}
}
unsigned dwarf_set_harmless_error_list_size(Dwarf_Debug dbg,
unsigned maxcount )
{
struct Dwarf_Harmless_s *dhp = &dbg->de_harmless_errors;
unsigned prevcount = dhp->dh_maxcount;
if (maxcount != 0) {
++maxcount;
if (maxcount != dhp->dh_maxcount) {
struct Dwarf_Harmless_s oldarray = *dhp;
dwarf_harmless_init(dhp,maxcount-1);
if (oldarray.dh_next_to_use != oldarray.dh_first) {
unsigned i = 0;
for (i = oldarray.dh_first; i != oldarray.dh_next_to_use;
i = (i+1)%oldarray.dh_maxcount) {
dwarf_insert_harmless_error(dbg,oldarray.dh_errors[i]);
}
if (oldarray.dh_errs_count > dhp->dh_errs_count) {
dhp->dh_errs_count = oldarray.dh_errs_count;
}
}
dwarf_harmless_cleanout(&oldarray);
}
}
return prevcount-1;
}
void
dwarf_harmless_init(struct Dwarf_Harmless_s *dhp,unsigned size)
{
unsigned i = 0;
memset(dhp,0,sizeof(*dhp));
dhp->dh_maxcount = size +1;
dhp->dh_errors = (char **)malloc(sizeof( char *) *dhp->dh_maxcount);
if (!dhp->dh_errors) {
dhp->dh_maxcount = 0;
return;
}
for (i = 0; i < dhp->dh_maxcount; ++i) {
char *newstr =
(char *)malloc(DW_HARMLESS_ERROR_MSG_STRING_SIZE);
dhp->dh_errors[i] = newstr;
if (!newstr) {
dhp->dh_maxcount = 0;
dhp->dh_errors = 0;
return;
}
newstr[0] = 0;
}
}
void
dwarf_harmless_cleanout(struct Dwarf_Harmless_s *dhp)
{
unsigned i = 0;
if (!dhp->dh_errors) {
return;
}
for (i = 0; i < dhp->dh_maxcount; ++i) {
free(dhp->dh_errors[i]);
dhp->dh_errors[i] = 0;
}
free(dhp->dh_errors);
dhp->dh_errors = 0;
dhp->dh_maxcount = 0;
}