In legion build config, updated path to GNU tools and updated deprecated Sun CC flag...
[OpenSPARC-T2-SAM] / legion / src / generic / lprintf.c
/*
* ========== Copyright Header Begin ==========================================
*
* OpenSPARC T2 Processor File: lprintf.c
* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES.
*
* The above named program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License version 2 as published by the Free Software Foundation.
*
* The above named program is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this work; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*
* ========== Copyright Header End ============================================
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "@(#)lprintf.c 1.12 06/08/14 SMI"
#include <stdarg.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <assert.h>
#include "basics.h"
#include "fatal.h"
#include "allocate.h"
#include "options.h"
#ifndef NDEBUG
typedef struct {
int id;
int off;
int len;
int size;
} log_entry_t;
#define LOGLINES 1000
#define LOGLINE_SIZE 80
#define LOGSPACE (LOGLINES*LOGLINE_SIZE)
static struct {
pthread_mutex_t lock;
bool_t enabled;
FILE *fp; /* output file handle */
char * fp_buffer;
log_entry_t entry[LOGLINES];
int idx;
int num;
char * bufferp;
int bufsize;
int freeoff;
} log;
#endif /* NDEBUG */
static void log_internal_printf(int id, char * fmt, va_list args);
#ifndef NDEBUG
void log_init()
{
log.bufsize = LOGSPACE;
log.bufferp = Xcalloc(log.bufsize, char);
log.num = LOGLINES;
pthread_mutex_init(&log.lock, NULL);
log.idx = 0;
log.freeoff = 0;
log.enabled = options.analyzer;
if (options.log_file != NULL) {
log.fp = fopen(options.log_file, "w");
if (log.fp == NULL)
fatal("cannot open log: %s", options.log_file);
} else
log.fp = stdout; /* stdout by default */
/*
* Was any buffering requested?
*/
if (options.buffersz != -1) {
if (options.buffersz <= 0) {
(void) setvbuf(log.fp, NULL, _IOLBF, 0);
} else {
log.fp_buffer = Xmalloc(options.buffersz);
(void) setvbuf(log.fp, log.fp_buffer,
_IOFBF, options.buffersz);
}
}
}
#define NO_LOG_FP log.fp
#else /* NDEBUG */
/* Output stream if no logging enabled. */
#define NO_LOG_FP stdout
#endif /* NDEBUG */
/*
* Very simple printf scheme for a rotating log file ...
* ... has a generous buffer but checks for potential overflow
*
* Idea is that it logs the last N lines of printf output into a rotating buffer Q
* excellent way to thrash the heap, but useful for debugging.
*/
void lprintf(int id, char * fmt, ...)
{
va_list args;
va_start(args, fmt);
log_lock();
log_internal_printf(id, fmt, args);
log_unlock();
va_end(args);
}
void log_lock()
{
flockfile(NO_LOG_FP);
#ifndef NDEBUG
pthread_mutex_lock( &log.lock );
#endif /* NDEBUG */
}
void log_unlock()
{
#ifndef NDEBUG
pthread_mutex_unlock( &log.lock );
#endif /* NDEBUG */
funlockfile(NO_LOG_FP);
}
/*
* Core printf function.
* Assumes you've already requested log_lock();
*/
void log_printf(int id, char * fmt, ...)
{
va_list args;
va_start(args, fmt);
log_internal_printf(id, fmt, args);
va_end(args);
}
static void log_internal_printf(int id, char * fmt, va_list args)
{
#ifndef NDEBUG
int len, lostsize, space;
#endif /* NDEBUG */
#ifndef NDEBUG
if (!log.enabled) {
#endif /* NDEBUG */
if (id != -1)
fprintf(NO_LOG_FP,"[%d] ",id);
vfprintf(NO_LOG_FP,fmt,args);
#ifndef NDEBUG
return;
}
/*
* Dont bother tracking how many live lines there are...
* just bother assigning pointers correctly ...
* then determine validity in the dump routine.
*/
lostsize = 0;
loop:;
space = log.bufsize - log.freeoff;
ASSERT(space>0);
/*
* returned len is the full output length,
* not including the null byte
*/
len = vsnprintf(log.bufferp + log.freeoff, space, fmt, args);
if (len >= space) {
/* didn't get all the output */
if (log.freeoff == 0)
fatal("lprintf: Ug! LOGSPACE needs to be raised to at least %d\n", len+1);
log.freeoff = 0;
lostsize = space;
goto loop;
}
log.idx++;
if (log.idx>=log.num) log.idx=0;
log.entry[log.idx].id = id;
log.entry[log.idx].off = log.freeoff;
log.entry[log.idx].len = len;
log.entry[log.idx].size = lostsize;
log.freeoff += len;
if (log.freeoff == log.bufsize) log.freeoff = 0;
#endif /* NDEBUG */
}
void log_flush()
{
log_lock();
fflush(NO_LOG_FP);
log_unlock();
}
void log_flush_unlock()
{
fflush(NO_LOG_FP);
log_unlock();
}
#ifndef NDEBUG
void log_dump()
{
int i,c,num,space;
if (!log.enabled)
return;
log_lock();
fflush(NO_LOG_FP);
i=log.idx;
space = log.entry[i].size + log.entry[i].len;
for(num = 0; space<log.bufsize && num<log.num; num++) {
i--;
if (i<0) i=log.num-1;
space += log.entry[i].size + log.entry[i].len;
}
for (c=0; c<num; c++) {
i++;
if (i>=log.num) i=0;
if (log.entry[i].id != -1)
fprintf(log.fp, "[%d] ", log.entry[i].id);
fwrite(log.bufferp + log.entry[i].off, log.entry[i].len, 1, log.fp);
}
fflush(log.fp);
log_unlock();
}
#endif /* NDEBUG */