minix/minix/usr.bin/trace/proc.c
David van Moolenbroek 521fa314e2 Add trace(1): the MINIX3 system call tracer
Change-Id: Ib970c8647409196902ed53d6e9631a1673a4ab2e
2014-11-04 21:46:31 +00:00

98 lines
1.8 KiB
C

#include "inc.h"
static TAILQ_HEAD(, trace_proc) proc_root;
static unsigned int nr_procs;
/*
* Initialize the list of traced processes.
*/
void
proc_init(void)
{
TAILQ_INIT(&proc_root);
nr_procs = 0;
}
/*
* Add a new process to the list of traced processes, allocating memory for it
* first. Return the new process structure with its PID assigned and the rest
* zeroed out, or NULL upon allocation failure (with errno set appropriately).
*/
struct trace_proc *
proc_add(pid_t pid)
{
struct trace_proc *proc;
proc = (struct trace_proc *)malloc(sizeof(struct trace_proc));
if (proc == NULL)
return NULL;
memset(proc, 0, sizeof(*proc));
proc->pid = pid;
TAILQ_INSERT_TAIL(&proc_root, proc, next);
nr_procs++;
return proc;
}
/*
* Retrieve the data structure for a traced process based on its PID. Return
* a pointer to the structure, or NULL if no structure exists for this process.
*/
struct trace_proc *
proc_get(pid_t pid)
{
struct trace_proc *proc;
/* Linear search for now; se we can easily add a hashtable later.. */
TAILQ_FOREACH(proc, &proc_root, next) {
if (proc->pid == pid)
return proc;
}
return NULL;
}
/*
* Remove a process from the list of traced processes.
*/
void
proc_del(struct trace_proc * proc)
{
TAILQ_REMOVE(&proc_root, proc, next);
nr_procs--;
free(proc);
}
/*
* Iterator for the list of traced processes. If a NULL pointer is given,
* return the first process in the list; otherwise, return the next process in
* the list. Not stable with respect to list modifications.
*/
struct trace_proc *
proc_next(struct trace_proc * proc)
{
if (proc == NULL)
return TAILQ_FIRST(&proc_root);
else
return TAILQ_NEXT(proc, next);
}
/*
* Return the number of processes in the list of traced processes.
*/
unsigned int
proc_count(void)
{
return nr_procs;
}