1178479Sjb/*
2178479Sjb * CDDL HEADER START
3178479Sjb *
4178479Sjb * The contents of this file are subject to the terms of the
5178479Sjb * Common Development and Distribution License (the "License").
6178479Sjb * You may not use this file except in compliance with the License.
7178479Sjb *
8178479Sjb * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9178479Sjb * or http://www.opensolaris.org/os/licensing.
10178479Sjb * See the License for the specific language governing permissions
11178479Sjb * and limitations under the License.
12178479Sjb *
13178479Sjb * When distributing Covered Code, include this CDDL HEADER in each
14178479Sjb * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15178479Sjb * If applicable, add the following below this CDDL HEADER, with the
16178479Sjb * fields enclosed by brackets "[]" replaced with your own identifying
17178479Sjb * information: Portions Copyright [yyyy] [name of copyright owner]
18178479Sjb *
19178479Sjb * CDDL HEADER END
20178479Sjb */
21178479Sjb
22178479Sjb/*
23178479Sjb * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24178479Sjb * Use is subject to license terms.
25178479Sjb */
26178479Sjb
27178479Sjb#ifndef	_DT_PROC_H
28178479Sjb#define	_DT_PROC_H
29178479Sjb
30249573Spfg#pragma ident	"%Z%%M%	%I%	%E% SMI"
31249573Spfg
32178479Sjb#include <libproc.h>
33178479Sjb#include <dtrace.h>
34178479Sjb#include <pthread.h>
35178479Sjb#include <dt_list.h>
36178479Sjb
37178479Sjb#ifdef	__cplusplus
38178479Sjbextern "C" {
39178479Sjb#endif
40178479Sjb
41178479Sjbtypedef struct dt_proc {
42178479Sjb	dt_list_t dpr_list;		/* prev/next pointers for lru chain */
43178479Sjb	struct dt_proc *dpr_hash;	/* next pointer for pid hash chain */
44178479Sjb	dtrace_hdl_t *dpr_hdl;		/* back pointer to libdtrace handle */
45178479Sjb	struct ps_prochandle *dpr_proc;	/* proc handle for libproc calls */
46178479Sjb	char dpr_errmsg[BUFSIZ];	/* error message */
47178479Sjb	rd_agent_t *dpr_rtld;		/* rtld handle for librtld_db calls */
48178479Sjb	pthread_mutex_t dpr_lock;	/* lock for manipulating dpr_hdl */
49178479Sjb	pthread_cond_t dpr_cv;		/* cond for dpr_stop/quit/done */
50178479Sjb	pid_t dpr_pid;			/* pid of process */
51178479Sjb	uint_t dpr_refs;		/* reference count */
52178479Sjb	uint8_t dpr_cacheable;		/* cache handle using lru list */
53178479Sjb	uint8_t dpr_stop;		/* stop mask: see flag bits below */
54178479Sjb	uint8_t dpr_quit;		/* quit flag: ctl thread should quit */
55178479Sjb	uint8_t dpr_done;		/* done flag: ctl thread has exited */
56178479Sjb	uint8_t dpr_usdt;		/* usdt flag: usdt initialized */
57178479Sjb	uint8_t dpr_stale;		/* proc flag: been deprecated */
58178479Sjb	uint8_t dpr_rdonly;		/* proc flag: opened read-only */
59178479Sjb	pthread_t dpr_tid;		/* control thread (or zero if none) */
60178479Sjb	dt_list_t dpr_bps;		/* list of dt_bkpt_t structures */
61178479Sjb} dt_proc_t;
62178479Sjb
63178479Sjbtypedef struct dt_proc_notify {
64178479Sjb	dt_proc_t *dprn_dpr;		/* process associated with the event */
65178479Sjb	char dprn_errmsg[BUFSIZ];	/* error message */
66178479Sjb	struct dt_proc_notify *dprn_next; /* next pointer */
67178479Sjb} dt_proc_notify_t;
68178479Sjb
69178479Sjb#define	DT_PROC_STOP_IDLE	0x01	/* idle on owner's stop request */
70178479Sjb#define	DT_PROC_STOP_CREATE	0x02	/* wait on dpr_cv at process exec */
71178479Sjb#define	DT_PROC_STOP_GRAB	0x04	/* wait on dpr_cv at process grab */
72178479Sjb#define	DT_PROC_STOP_PREINIT	0x08	/* wait on dpr_cv at rtld preinit */
73178479Sjb#define	DT_PROC_STOP_POSTINIT	0x10	/* wait on dpr_cv at rtld postinit */
74178479Sjb#define	DT_PROC_STOP_MAIN	0x20	/* wait on dpr_cv at a.out`main() */
75178479Sjb
76178479Sjbtypedef void dt_bkpt_f(dtrace_hdl_t *, dt_proc_t *, void *);
77178479Sjb
78178479Sjbtypedef struct dt_bkpt {
79178479Sjb	dt_list_t dbp_list;		/* prev/next pointers for bkpt list */
80178479Sjb	dt_bkpt_f *dbp_func;		/* callback function to execute */
81178479Sjb	void *dbp_data;			/* callback function private data */
82178479Sjb	uintptr_t dbp_addr;		/* virtual address of breakpoint */
83178479Sjb	ulong_t dbp_instr;		/* saved instruction from breakpoint */
84178479Sjb	ulong_t dbp_hits;		/* count of breakpoint hits for debug */
85178479Sjb	int dbp_active;			/* flag indicating breakpoint is on */
86178479Sjb} dt_bkpt_t;
87178479Sjb
88178479Sjbtypedef struct dt_proc_hash {
89178479Sjb	pthread_mutex_t dph_lock;	/* lock protecting dph_notify list */
90178479Sjb	pthread_cond_t dph_cv;		/* cond for waiting for dph_notify */
91178479Sjb	dt_proc_notify_t *dph_notify;	/* list of pending proc notifications */
92178479Sjb	dt_list_t dph_lrulist;		/* list of dt_proc_t's in lru order */
93178479Sjb	uint_t dph_lrulim;		/* limit on number of procs to hold */
94178479Sjb	uint_t dph_lrucnt;		/* count of cached process handles */
95178479Sjb	uint_t dph_hashlen;		/* size of hash chains array */
96178479Sjb	dt_proc_t *dph_hash[1];		/* hash chains array */
97178479Sjb} dt_proc_hash_t;
98178479Sjb
99178479Sjbextern struct ps_prochandle *dt_proc_create(dtrace_hdl_t *,
100184696Srodrigc    const char *, char *const *, proc_child_func *, void *);
101178479Sjb
102178479Sjbextern struct ps_prochandle *dt_proc_grab(dtrace_hdl_t *, pid_t, int, int);
103178479Sjbextern void dt_proc_release(dtrace_hdl_t *, struct ps_prochandle *);
104178479Sjbextern void dt_proc_continue(dtrace_hdl_t *, struct ps_prochandle *);
105178479Sjbextern void dt_proc_lock(dtrace_hdl_t *, struct ps_prochandle *);
106178479Sjbextern void dt_proc_unlock(dtrace_hdl_t *, struct ps_prochandle *);
107178479Sjbextern dt_proc_t *dt_proc_lookup(dtrace_hdl_t *, struct ps_prochandle *, int);
108178479Sjb
109249573Spfgextern void dt_proc_hash_create(dtrace_hdl_t *);
110249573Spfgextern void dt_proc_hash_destroy(dtrace_hdl_t *);
111178479Sjb
112178479Sjb#ifdef	__cplusplus
113178479Sjb}
114178479Sjb#endif
115178479Sjb
116178479Sjb#endif	/* _DT_PROC_H */
117