tools/time.c
author Norm Jacobs <Norm.Jacobs@Sun.COM>
Wed, 12 May 2010 00:32:41 -0500
changeset 6 20f80c019d73
child 16 33aaaec59991
permissions -rw-r--r--
add compiler date/time chatter reduction tools
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
     1
/*
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
     2
 * CDDL HEADER START
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
     3
 *
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
     4
 * The contents of this file are subject to the terms of the
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
     5
 * Common Development and Distribution License (the "License").
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
     6
 * You may not use this file except in compliance with the License.
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
     7
 *
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
     8
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
     9
 * or http://www.opensolaris.org/os/licensing.
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    10
 * See the License for the specific language governing permissions
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    11
 * and limitations under the License.
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    12
 *
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    13
 * When distributing Covered Code, include this CDDL HEADER in each
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    14
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    15
 * If applicable, add the following below this CDDL HEADER, with the
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    16
 * fields enclosed by brackets "[]" replaced with your own identifying
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    17
 * information: Portions Copyright [yyyy] [name of copyright owner]
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    18
 *
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    19
 * CDDL HEADER END
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    20
 *
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    21
 * Copyright (c) 2010, Oracle and/or it's affiliates.  All rights reserved.
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    22
 */
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    23
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    24
/*
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    25
 * This compiles to a module that can be preloaded during a build.  If this
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    26
 * is preloaded, it interposes on time(2) and returns a constant value when
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    27
 * the execname matches one of the desired "programs" and TIME_CONSTANT
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    28
 * contains an integer value to be returned.
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    29
 */
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    30
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    31
#include <stdlib.h>
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    32
#include <ucontext.h>
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    33
#include <dlfcn.h>
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    34
#include <strings.h>
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    35
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    36
/* The list of programs that we want to use a constant time. */
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    37
static char *programs[] = { "date", "cpp", "cc1", "perl", NULL };
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    38
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    39
static int
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    40
stack_info(uintptr_t pc, int signo, void *arg)
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    41
{
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    42
	Dl_info info;
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    43
	void *sym;
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    44
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    45
	if (dladdr1((void *)pc, &info, &sym, RTLD_DL_SYMENT) != NULL) {
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    46
		*(char **)arg = (char *)info.dli_fname;
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    47
	}
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    48
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    49
	return (0);
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    50
}
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    51
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    52
static char *
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    53
my_execname()
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    54
{
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    55
	static char *execname;
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    56
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    57
	if (execname == NULL) {
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    58
		ucontext_t ctx;
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    59
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    60
		if (getcontext(&ctx) == 0)
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    61
			walkcontext(&ctx, stack_info, &execname);
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    62
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    63
		if (execname != NULL) {
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    64
			char *s = strrchr(execname, '/');
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    65
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    66
			if (s != NULL)
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    67
				execname = ++s;
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    68
		}
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    69
	}
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    70
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    71
	return (execname);
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    72
}
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    73
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    74
static time_t
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    75
intercept()
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    76
{
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    77
	char *execname = my_execname();
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    78
	time_t result = -1;
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    79
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    80
	if (execname != NULL) {
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    81
		int i;
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    82
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    83
		for (i = 0; programs[i] != NULL; i++)
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    84
			if (strcmp(execname, programs[i]) == 0) {
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    85
				static char *time_constant;
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    86
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    87
				if (time_constant == NULL)
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    88
					time_constant = getenv("TIME_CONSTANT");
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    89
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    90
				if (time_constant != NULL)
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    91
					result = atoll(time_constant);
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    92
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    93
				break;
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    94
			}
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    95
	}
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    96
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    97
	return (result);
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    98
}
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
    99
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
   100
time_t
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
   101
time(time_t *ptr)
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
   102
{
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
   103
	time_t result = intercept();
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
   104
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
   105
	if (result == (time_t)-1) {
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
   106
		static time_t (*fptr)(time_t *);
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
   107
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
   108
		if (fptr == NULL)
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
   109
			fptr = (time_t (*)(time_t *))dlsym(RTLD_NEXT, "time");
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
   110
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
   111
		result = (fptr)(ptr);
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
   112
	} else if (ptr != NULL)
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
   113
			*ptr = result;
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
   114
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
   115
	return (result);
20f80c019d73 add compiler date/time chatter reduction tools
Norm Jacobs <Norm.Jacobs@Sun.COM>
parents:
diff changeset
   116
}