subr.c revision 216370
1219820Sjeff/*-
2219820Sjeff * Copyright (c) 1988, 1993
3219820Sjeff *	The Regents of the University of California.  All rights reserved.
4219820Sjeff *
5219820Sjeff * Redistribution and use in source and binary forms, with or without
6219820Sjeff * modification, are permitted provided that the following conditions
7219820Sjeff * are met:
8219820Sjeff * 1. Redistributions of source code must retain the above copyright
9219820Sjeff *    notice, this list of conditions and the following disclaimer.
10219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright
11219820Sjeff *    notice, this list of conditions and the following disclaimer in the
12219820Sjeff *    documentation and/or other materials provided with the distribution.
13219820Sjeff * 4. Neither the name of the University nor the names of its contributors
14219820Sjeff *    may be used to endorse or promote products derived from this software
15219820Sjeff *    without specific prior written permission.
16219820Sjeff *
17219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18219820Sjeff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19219820Sjeff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20219820Sjeff * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21219820Sjeff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22219820Sjeff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23219820Sjeff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24219820Sjeff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25219820Sjeff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26219820Sjeff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27219820Sjeff * SUCH DAMAGE.
28219820Sjeff */
29219820Sjeff
30219820Sjeff#if 0
31219820Sjeff#ifndef lint
32219820Sjeffstatic char sccsid[] = "@(#)subr.c	8.1 (Berkeley) 6/6/93";
33219820Sjeff#endif /* not lint */
34219820Sjeff#endif
35219820Sjeff
36219820Sjeff#include <sys/cdefs.h>
37219820Sjeff__FBSDID("$FreeBSD: head/usr.bin/ktrace/subr.c 216370 2010-12-11 08:32:16Z joel $");
38219820Sjeff
39219820Sjeff#include <sys/param.h>
40219820Sjeff#include <sys/file.h>
41219820Sjeff#include <sys/proc.h>
42219820Sjeff#include <sys/time.h>
43219820Sjeff#include <sys/uio.h>
44219820Sjeff#include <sys/ktrace.h>
45219820Sjeff
46219820Sjeff#include <stdio.h>
47219820Sjeff
48219820Sjeff#include "ktrace.h"
49219820Sjeff
50219820Sjeffvoid timevaladd(struct timeval *, struct timeval *);
51219820Sjeffvoid timevalsub(struct timeval *, struct timeval *);
52219820Sjeffvoid timevalfix(struct timeval *);
53219820Sjeff
54219820Sjeffint
55219820Sjeffgetpoints(char *s)
56219820Sjeff{
57219820Sjeff	int facs = 0;
58219820Sjeff
59219820Sjeff	while (*s) {
60219820Sjeff		switch(*s) {
61219820Sjeff		case 'c':
62219820Sjeff			facs |= KTRFAC_SYSCALL | KTRFAC_SYSRET;
63219820Sjeff			break;
64219820Sjeff		case 'n':
65219820Sjeff			facs |= KTRFAC_NAMEI;
66219820Sjeff			break;
67219820Sjeff		case 'i':
68219820Sjeff			facs |= KTRFAC_GENIO;
69219820Sjeff			break;
70219820Sjeff		case 's':
71219820Sjeff			facs |= KTRFAC_PSIG;
72219820Sjeff			break;
73219820Sjeff		case 't':
74219820Sjeff			facs |= KTRFAC_STRUCT;
75219820Sjeff			break;
76219820Sjeff		case 'u':
77219820Sjeff			facs |= KTRFAC_USER;
78219820Sjeff			break;
79219820Sjeff		case 'w':
80219820Sjeff			facs |= KTRFAC_CSW;
81219820Sjeff			break;
82219820Sjeff		case 'y':
83219820Sjeff			facs |= KTRFAC_SYSCTL;
84219820Sjeff			break;
85219820Sjeff		case '+':
86219820Sjeff			facs |= DEF_POINTS;
87219820Sjeff			break;
88219820Sjeff		default:
89219820Sjeff			return (-1);
90219820Sjeff		}
91219820Sjeff		s++;
92219820Sjeff	}
93219820Sjeff	return (facs);
94219820Sjeff}
95219820Sjeff
96219820Sjeffvoid
97219820Sjefftimevaladd(struct timeval *t1, struct timeval *t2)
98219820Sjeff{
99219820Sjeff	t1->tv_sec += t2->tv_sec;
100219820Sjeff	t1->tv_usec += t2->tv_usec;
101219820Sjeff	timevalfix(t1);
102219820Sjeff}
103219820Sjeff
104219820Sjeffvoid
105219820Sjefftimevalsub(struct timeval *t1, struct timeval *t2)
106219820Sjeff{
107219820Sjeff	t1->tv_sec -= t2->tv_sec;
108219820Sjeff	t1->tv_usec -= t2->tv_usec;
109219820Sjeff	timevalfix(t1);
110219820Sjeff}
111219820Sjeff
112219820Sjeffvoid
113219820Sjefftimevalfix(struct timeval *t1)
114219820Sjeff{
115219820Sjeff	if (t1->tv_usec < 0) {
116219820Sjeff		t1->tv_sec--;
117219820Sjeff		t1->tv_usec += 1000000;
118219820Sjeff	}
119219820Sjeff	if (t1->tv_usec >= 1000000) {
120219820Sjeff		t1->tv_sec++;
121219820Sjeff		t1->tv_usec -= 1000000;
122219820Sjeff	}
123219820Sjeff}
124219820Sjeff