1fcf3ce4John Forte/*
2fcf3ce4John Forte * CDDL HEADER START
3fcf3ce4John Forte *
4fcf3ce4John Forte * The contents of this file are subject to the terms of the
5fcf3ce4John Forte * Common Development and Distribution License (the "License").
6fcf3ce4John Forte * You may not use this file except in compliance with the License.
7fcf3ce4John Forte *
8fcf3ce4John Forte * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fcf3ce4John Forte * or http://www.opensolaris.org/os/licensing.
10fcf3ce4John Forte * See the License for the specific language governing permissions
11fcf3ce4John Forte * and limitations under the License.
12fcf3ce4John Forte *
13fcf3ce4John Forte * When distributing Covered Code, include this CDDL HEADER in each
14fcf3ce4John Forte * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fcf3ce4John Forte * If applicable, add the following below this CDDL HEADER, with the
16fcf3ce4John Forte * fields enclosed by brackets "[]" replaced with your own identifying
17fcf3ce4John Forte * information: Portions Copyright [yyyy] [name of copyright owner]
18fcf3ce4John Forte *
19fcf3ce4John Forte * CDDL HEADER END
20fcf3ce4John Forte */
21fcf3ce4John Forte/*
22fcf3ce4John Forte * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23fcf3ce4John Forte * Use is subject to license terms.
24fcf3ce4John Forte */
25fcf3ce4John Forte
26fcf3ce4John Forte
27fcf3ce4John Forte
28fcf3ce4John Forte#include "Trace.h"
29fcf3ce4John Forte#include <cstdarg>
30fcf3ce4John Forte#include <string>
31fcf3ce4John Forte#include <cstdio>
32fcf3ce4John Forte#include <string>
33fcf3ce4John Forte#include <sys/types.h>
34fcf3ce4John Forte#include <sys/stat.h>
35fcf3ce4John Forte#include <fcntl.h>
36fcf3ce4John Forte#include <unistd.h>
37f3aaec0Richard Lowe#include <cstring>
38fcf3ce4John Forte
39fcf3ce4John Forteusing namespace std;
40fcf3ce4John Forte
41fcf3ce4John Forte/**
42fcf3ce4John Forte * Tracking for the stacks
43fcf3ce4John Forte */
44fcf3ce4John Fortevector<vector<Trace *> > Trace::stacks;
45fcf3ce4John Forte
46fcf3ce4John Forte/**
47fcf3ce4John Forte * The indentation string for output
48fcf3ce4John Forte */
49fcf3ce4John Fortevector<string> Trace::indent;
50fcf3ce4John Forte
51fcf3ce4John Forte#define MAX_MSG_PREFIX_LEN 128
52fcf3ce4John Forte#define	CTIME_LEN	26
53fcf3ce4John Forte#define	DEBUG_FILE	"/var/adm/sun_fc.debug"
54fcf3ce4John Forte#define	LOG_FILE	"/var/adm/sun_fc"
55fcf3ce4John Forte
56fcf3ce4John Forte/**
57fcf3ce4John Forte * @memo	    Log a message
58fcf3ce4John Forte * @param	    priority The priority of the message (see syslog man page)
59fcf3ce4John Forte * @param	    msg The message string
60fcf3ce4John Forte *
61fcf3ce4John Forte * @doc		    If the debug file is present, we will log everything to
62fcf3ce4John Forte *		    that file.  Otherwise, if the normal log file is present,
63fcf3ce4John Forte *		    we'll log all non-debug messages to that file.  Lastly,
64fcf3ce4John Forte *		    if neither file is present, the message will be
65fcf3ce4John Forte *		    silently discarded.
66fcf3ce4John Forte */
67fcf3ce4John Fortevoid Trace::message(int priority, const char *msg) {
68fcf3ce4John Forte	char prefix[MAX_MSG_PREFIX_LEN];
69fcf3ce4John Forte	char message[MAX_MSG_PREFIX_LEN + MAX_MSG_LEN + 2];
70fcf3ce4John Forte	int fd;
71fcf3ce4John Forte	// char time[CTIME_LEN+1];
72fcf3ce4John Forte	std::string priString;
73fcf3ce4John Forte
74fcf3ce4John Forte
75fcf3ce4John Forte	/* If we can open the log file, write there, else use the cim log */
76fcf3ce4John Forte	fd = open(DEBUG_FILE, O_WRONLY|O_APPEND); /* will only open if exists */
77fcf3ce4John Forte	if (fd == -1) {
78fcf3ce4John Forte	    /* No debug file, how about the log file? */
79fcf3ce4John Forte	    if (priority == LOG_DEBUG) {
80fcf3ce4John Forte		return; /* Ignore debug */
81fcf3ce4John Forte	    }
82fcf3ce4John Forte	    fd = open(LOG_FILE, O_WRONLY|O_APPEND);
83fcf3ce4John Forte	    /* We catch open failures later */
84fcf3ce4John Forte	}
85fcf3ce4John Forte
86fcf3ce4John Forte	// now(time);
87fcf3ce4John Forte	/* First interpret the priority value */
88fcf3ce4John Forte	switch (priority) {
89fcf3ce4John Forte	case INTERNAL_ERROR:
90fcf3ce4John Forte	    priString = "INTERNAL";
91fcf3ce4John Forte	    break;
92fcf3ce4John Forte	case STACK_TRACE:
93fcf3ce4John Forte	    priString = "STACK";
94fcf3ce4John Forte	    break;
95fcf3ce4John Forte	case IO_ERROR:
96fcf3ce4John Forte	    priString = "IO";
97fcf3ce4John Forte	    break;
98fcf3ce4John Forte	case USER_ERROR:
99fcf3ce4John Forte	    priString = "USER";
100fcf3ce4John Forte	    break;
101fcf3ce4John Forte	case LOG_DEBUG:
102fcf3ce4John Forte	    priString = "DEBUG";
103fcf3ce4John Forte	    break;
104fcf3ce4John Forte	default:
105fcf3ce4John Forte	    priString = "UNKNOWN";
106fcf3ce4John Forte	    break;
107fcf3ce4John Forte	}
108fcf3ce4John Forte
109fcf3ce4John Forte	if (fd != -1) {
110fcf3ce4John Forte	    /* Format the prefix string for the log file */
111fcf3ce4John Forte	    snprintf(prefix, MAX_MSG_PREFIX_LEN, "%d:%d:%s%s:%s",
112fcf3ce4John Forte		time(NULL),
113fcf3ce4John Forte		tid,
114fcf3ce4John Forte		indent[tid].c_str(),
115fcf3ce4John Forte		routine.c_str(),
116fcf3ce4John Forte		priString.c_str());
117fcf3ce4John Forte
118fcf3ce4John Forte	    /* Format the message string for the log file */
119fcf3ce4John Forte	    snprintf(message, strlen(prefix) + MAX_MSG_LEN + 2, "%s:%s\n",
120fcf3ce4John Forte		prefix,
121fcf3ce4John Forte		msg);
122fcf3ce4John Forte	    write(fd, message, strlen(message));
123fcf3ce4John Forte	    close(fd);
124fcf3ce4John Forte	} /* Else discard the log message */
125fcf3ce4John Forte}
126fcf3ce4John Forte
127fcf3ce4John Forte/**
128fcf3ce4John Forte * @memo	    Create a new Trace instance and update stack.
129fcf3ce4John Forte * @param	    myRoutine The name of the routine
130fcf3ce4John Forte *
131fcf3ce4John Forte * @doc		    This class was developed to provide a Java
132fcf3ce4John Forte *		    like stack trace capability, as C++ does not provide
133fcf3ce4John Forte *		    a run-time stack lookup feature.  Instances of the
134fcf3ce4John Forte *		    Trace class should be created on the stack so they
135fcf3ce4John Forte *		    will be automatically freed when the stack is popped
136fcf3ce4John Forte *		    when the function returns.
137fcf3ce4John Forte */
138fcf3ce4John ForteTrace::Trace(std::string myRoutine) : routine(myRoutine) {
139fcf3ce4John Forte	tid = pthread_self();
140fcf3ce4John Forte	if (stacks.size() < tid+1) {
141fcf3ce4John Forte	    stacks.resize(tid+1);
142fcf3ce4John Forte	    indent.resize(tid+1);
143fcf3ce4John Forte	    indent[tid] = "";
144fcf3ce4John Forte	}
145fcf3ce4John Forte	message(LOG_DEBUG, "entered");
146fcf3ce4John Forte	stacks[tid].push_back(this);
147fcf3ce4John Forte	indent[tid] += " ";
148fcf3ce4John Forte}
149fcf3ce4John Forte
150fcf3ce4John Forte/**
151fcf3ce4John Forte * @memo	    Delete a trace instances and update the stack
152fcf3ce4John Forte */
153fcf3ce4John ForteTrace::~Trace() {
154fcf3ce4John Forte	string::size_type len = indent[tid].size();
155fcf3ce4John Forte	if (len > 0) {
156fcf3ce4John Forte	    indent[tid].resize(len - 1);
157fcf3ce4John Forte	}
158fcf3ce4John Forte	message(LOG_DEBUG, "exited");
159fcf3ce4John Forte	stacks[tid].pop_back();
160fcf3ce4John Forte}
161fcf3ce4John Forte
162fcf3ce4John Forte/**
163fcf3ce4John Forte * @memo	    Print out the current stack trace information
164fcf3ce4John Forte */
165fcf3ce4John Fortevoid Trace::stackTrace() {
166fcf3ce4John Forte	message(STACK_TRACE, "Stack trace follows");
167fcf3ce4John Forte	for (vector<Trace *>::size_type i = stacks[tid].size() - 1; ; i--) {
168fcf3ce4John Forte	    string msg = "	    ";
169fcf3ce4John Forte	    msg += (stacks[tid])[i]->label();
170fcf3ce4John Forte	    message(STACK_TRACE, msg.c_str());
171fcf3ce4John Forte	    if (i == 0) {
172fcf3ce4John Forte		break;
173fcf3ce4John Forte	    }
174fcf3ce4John Forte	}
175fcf3ce4John Forte}
176