xref: /freebsd-src/cddl/contrib/opensolaris/lib/libdtrace/common/dt_proc.h (revision bcbce2a29b892ac10291f652b271963e08d641a8)
16ff6d951SJohn Birrell /*
26ff6d951SJohn Birrell  * CDDL HEADER START
36ff6d951SJohn Birrell  *
46ff6d951SJohn Birrell  * The contents of this file are subject to the terms of the
56ff6d951SJohn Birrell  * Common Development and Distribution License (the "License").
66ff6d951SJohn Birrell  * You may not use this file except in compliance with the License.
76ff6d951SJohn Birrell  *
86ff6d951SJohn Birrell  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
96ff6d951SJohn Birrell  * or http://www.opensolaris.org/os/licensing.
106ff6d951SJohn Birrell  * See the License for the specific language governing permissions
116ff6d951SJohn Birrell  * and limitations under the License.
126ff6d951SJohn Birrell  *
136ff6d951SJohn Birrell  * When distributing Covered Code, include this CDDL HEADER in each
146ff6d951SJohn Birrell  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
156ff6d951SJohn Birrell  * If applicable, add the following below this CDDL HEADER, with the
166ff6d951SJohn Birrell  * fields enclosed by brackets "[]" replaced with your own identifying
176ff6d951SJohn Birrell  * information: Portions Copyright [yyyy] [name of copyright owner]
186ff6d951SJohn Birrell  *
196ff6d951SJohn Birrell  * CDDL HEADER END
206ff6d951SJohn Birrell  */
216ff6d951SJohn Birrell 
226ff6d951SJohn Birrell /*
236ff6d951SJohn Birrell  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
246ff6d951SJohn Birrell  * Use is subject to license terms.
256ff6d951SJohn Birrell  */
266ff6d951SJohn Birrell 
2704006780SMark Johnston /*
2804006780SMark Johnston  * Copyright (c) 2012 by Delphix. All rights reserved.
2904006780SMark Johnston  */
3004006780SMark Johnston 
316ff6d951SJohn Birrell #ifndef	_DT_PROC_H
326ff6d951SJohn Birrell #define	_DT_PROC_H
336ff6d951SJohn Birrell 
346ff6d951SJohn Birrell #include <libproc.h>
356ff6d951SJohn Birrell #include <dtrace.h>
366ff6d951SJohn Birrell #include <pthread.h>
376ff6d951SJohn Birrell #include <dt_list.h>
386ff6d951SJohn Birrell 
396ff6d951SJohn Birrell #ifdef	__cplusplus
406ff6d951SJohn Birrell extern "C" {
416ff6d951SJohn Birrell #endif
426ff6d951SJohn Birrell 
43*bcbce2a2SEric van Gyzen enum dt_close_action {
44*bcbce2a2SEric van Gyzen 	DT_CLOSE_RUN,
45*bcbce2a2SEric van Gyzen 	DT_CLOSE_KILL,
46*bcbce2a2SEric van Gyzen };
47*bcbce2a2SEric van Gyzen 
486ff6d951SJohn Birrell typedef struct dt_proc {
496ff6d951SJohn Birrell 	dt_list_t dpr_list;		/* prev/next pointers for lru chain */
506ff6d951SJohn Birrell 	struct dt_proc *dpr_hash;	/* next pointer for pid hash chain */
516ff6d951SJohn Birrell 	dtrace_hdl_t *dpr_hdl;		/* back pointer to libdtrace handle */
526ff6d951SJohn Birrell 	struct ps_prochandle *dpr_proc;	/* proc handle for libproc calls */
536ff6d951SJohn Birrell 	char dpr_errmsg[BUFSIZ];	/* error message */
546ff6d951SJohn Birrell 	rd_agent_t *dpr_rtld;		/* rtld handle for librtld_db calls */
556ff6d951SJohn Birrell 	pthread_mutex_t dpr_lock;	/* lock for manipulating dpr_hdl */
566ff6d951SJohn Birrell 	pthread_cond_t dpr_cv;		/* cond for dpr_stop/quit/done */
576ff6d951SJohn Birrell 	pid_t dpr_pid;			/* pid of process */
586ff6d951SJohn Birrell 	uint_t dpr_refs;		/* reference count */
596ff6d951SJohn Birrell 	uint8_t dpr_cacheable;		/* cache handle using lru list */
606ff6d951SJohn Birrell 	uint8_t dpr_stop;		/* stop mask: see flag bits below */
616ff6d951SJohn Birrell 	uint8_t dpr_quit;		/* quit flag: ctl thread should quit */
626ff6d951SJohn Birrell 	uint8_t dpr_done;		/* done flag: ctl thread has exited */
636ff6d951SJohn Birrell 	uint8_t dpr_usdt;		/* usdt flag: usdt initialized */
646ff6d951SJohn Birrell 	uint8_t dpr_stale;		/* proc flag: been deprecated */
656ff6d951SJohn Birrell 	uint8_t dpr_rdonly;		/* proc flag: opened read-only */
666ff6d951SJohn Birrell 	pthread_t dpr_tid;		/* control thread (or zero if none) */
676ff6d951SJohn Birrell 	dt_list_t dpr_bps;		/* list of dt_bkpt_t structures */
68*bcbce2a2SEric van Gyzen 	enum dt_close_action dpr_close;	/* do this to child when exiting */
696ff6d951SJohn Birrell } dt_proc_t;
706ff6d951SJohn Birrell 
716ff6d951SJohn Birrell typedef struct dt_proc_notify {
726ff6d951SJohn Birrell 	dt_proc_t *dprn_dpr;		/* process associated with the event */
736ff6d951SJohn Birrell 	char dprn_errmsg[BUFSIZ];	/* error message */
746ff6d951SJohn Birrell 	struct dt_proc_notify *dprn_next; /* next pointer */
756ff6d951SJohn Birrell } dt_proc_notify_t;
766ff6d951SJohn Birrell 
776ff6d951SJohn Birrell #define	DT_PROC_STOP_IDLE	0x01	/* idle on owner's stop request */
786ff6d951SJohn Birrell #define	DT_PROC_STOP_CREATE	0x02	/* wait on dpr_cv at process exec */
796ff6d951SJohn Birrell #define	DT_PROC_STOP_GRAB	0x04	/* wait on dpr_cv at process grab */
806ff6d951SJohn Birrell #define	DT_PROC_STOP_PREINIT	0x08	/* wait on dpr_cv at rtld preinit */
816ff6d951SJohn Birrell #define	DT_PROC_STOP_POSTINIT	0x10	/* wait on dpr_cv at rtld postinit */
826ff6d951SJohn Birrell #define	DT_PROC_STOP_MAIN	0x20	/* wait on dpr_cv at a.out`main() */
836ff6d951SJohn Birrell 
846ff6d951SJohn Birrell typedef void dt_bkpt_f(dtrace_hdl_t *, dt_proc_t *, void *);
856ff6d951SJohn Birrell 
866ff6d951SJohn Birrell typedef struct dt_bkpt {
876ff6d951SJohn Birrell 	dt_list_t dbp_list;		/* prev/next pointers for bkpt list */
886ff6d951SJohn Birrell 	dt_bkpt_f *dbp_func;		/* callback function to execute */
896ff6d951SJohn Birrell 	void *dbp_data;			/* callback function private data */
906ff6d951SJohn Birrell 	uintptr_t dbp_addr;		/* virtual address of breakpoint */
916ff6d951SJohn Birrell 	ulong_t dbp_instr;		/* saved instruction from breakpoint */
926ff6d951SJohn Birrell 	ulong_t dbp_hits;		/* count of breakpoint hits for debug */
936ff6d951SJohn Birrell 	int dbp_active;			/* flag indicating breakpoint is on */
946ff6d951SJohn Birrell } dt_bkpt_t;
956ff6d951SJohn Birrell 
966ff6d951SJohn Birrell typedef struct dt_proc_hash {
976ff6d951SJohn Birrell 	pthread_mutex_t dph_lock;	/* lock protecting dph_notify list */
986ff6d951SJohn Birrell 	pthread_cond_t dph_cv;		/* cond for waiting for dph_notify */
996ff6d951SJohn Birrell 	dt_proc_notify_t *dph_notify;	/* list of pending proc notifications */
1006ff6d951SJohn Birrell 	dt_list_t dph_lrulist;		/* list of dt_proc_t's in lru order */
1016ff6d951SJohn Birrell 	uint_t dph_lrulim;		/* limit on number of procs to hold */
1026ff6d951SJohn Birrell 	uint_t dph_lrucnt;		/* count of cached process handles */
1036ff6d951SJohn Birrell 	uint_t dph_hashlen;		/* size of hash chains array */
1046ff6d951SJohn Birrell 	dt_proc_t *dph_hash[1];		/* hash chains array */
1056ff6d951SJohn Birrell } dt_proc_hash_t;
1066ff6d951SJohn Birrell 
1076ff6d951SJohn Birrell extern struct ps_prochandle *dt_proc_create(dtrace_hdl_t *,
10856b35563SCraig Rodrigues     const char *, char *const *, proc_child_func *, void *);
1096ff6d951SJohn Birrell 
1106ff6d951SJohn Birrell extern struct ps_prochandle *dt_proc_grab(dtrace_hdl_t *, pid_t, int, int);
1116ff6d951SJohn Birrell extern void dt_proc_release(dtrace_hdl_t *, struct ps_prochandle *);
1126ff6d951SJohn Birrell extern void dt_proc_continue(dtrace_hdl_t *, struct ps_prochandle *);
1136ff6d951SJohn Birrell extern void dt_proc_lock(dtrace_hdl_t *, struct ps_prochandle *);
1146ff6d951SJohn Birrell extern void dt_proc_unlock(dtrace_hdl_t *, struct ps_prochandle *);
1156ff6d951SJohn Birrell extern dt_proc_t *dt_proc_lookup(dtrace_hdl_t *, struct ps_prochandle *, int);
1166ff6d951SJohn Birrell 
11704006780SMark Johnston extern void dt_proc_init(dtrace_hdl_t *);
11804006780SMark Johnston extern void dt_proc_fini(dtrace_hdl_t *);
1196ff6d951SJohn Birrell 
1206ff6d951SJohn Birrell #ifdef	__cplusplus
1216ff6d951SJohn Birrell }
1226ff6d951SJohn Birrell #endif
1236ff6d951SJohn Birrell 
1246ff6d951SJohn Birrell #endif	/* _DT_PROC_H */
125