#include <kern/kern_types.h>
#include <kern/processor.h>
#include <machine/db_machdep.h>
#include <ddb/db_task_thread.h>
#include <ddb/db_variables.h>
#include <ddb/db_command.h>
#include <ddb/db_expr.h>
#include <ddb/db_lex.h>
#include <ddb/db_output.h>
#include <ddb/db_sym.h>
#define DB_MAX_TASKID 0x10000
#define DB_MAX_THREADID 0x10000
#define DB_MAX_PSETS 0x10000
task_t db_default_task;
thread_act_t db_default_act;
task_t db_lookup_task_id(register int task_id);
static thread_act_t db_lookup_act_id(
task_t task,
register int thread_id);
int
db_lookup_task(task_t target_task)
{
register task_t task;
register int task_id;
register processor_set_t pset = &default_pset;
register int npset = 0;
task_id = 0;
if (npset++ >= DB_MAX_PSETS)
return(-1);
if (queue_first(&pset->tasks) == 0)
return(-1);
queue_iterate(&pset->tasks, task, task_t, pset_tasks) {
if (target_task == task)
return(task_id);
if (task_id++ >= DB_MAX_TASKID)
return(-1);
}
return(-1);
}
int
db_lookup_task_act(
task_t task,
thread_act_t target_act)
{
register thread_act_t thr_act;
register int act_id;
act_id = 0;
if (queue_first(&task->threads) == 0)
return(-1);
queue_iterate(&task->threads, thr_act, thread_act_t, task_threads) {
if (target_act == thr_act)
return(act_id);
if (act_id++ >= DB_MAX_THREADID)
return(-1);
}
return(-1);
}
int
db_lookup_act(thread_act_t target_act)
{
register int act_id;
register task_t task;
register processor_set_t pset = &default_pset;
register int ntask = 0;
register int npset = 0;
if (npset++ >= DB_MAX_PSETS)
return(-1);
if (queue_first(&pset->tasks) == 0)
return(-1);
queue_iterate(&pset->tasks, task, task_t, pset_tasks) {
if (ntask++ > DB_MAX_TASKID)
return(-1);
if (task->thread_count == 0)
continue;
act_id = db_lookup_task_act(task, target_act);
if (act_id >= 0)
return(act_id);
}
return(-1);
}
int force_act_lookup = 0;
boolean_t
db_check_act_address_valid(thread_act_t thr_act)
{
if (!force_act_lookup && db_lookup_act(thr_act) < 0) {
db_printf("Bad thr_act address 0x%x\n", thr_act);
db_flush_lex();
return(FALSE);
} else
return(TRUE);
}
task_t
db_lookup_task_id(register task_id)
{
register task_t task;
register processor_set_t pset = &default_pset;
register int npset = 0;
if (task_id > DB_MAX_TASKID)
return(TASK_NULL);
if (npset++ >= DB_MAX_PSETS)
return(TASK_NULL);
if (queue_first(&pset->tasks) == 0)
return(TASK_NULL);
queue_iterate(&pset->tasks, task, task_t, pset_tasks) {
if (task_id-- <= 0)
return(task);
}
return(TASK_NULL);
}
static thread_act_t
db_lookup_act_id(
task_t task,
register int act_id)
{
register thread_act_t thr_act;
if (act_id > DB_MAX_THREADID)
return(THR_ACT_NULL);
if (queue_first(&task->threads) == 0)
return(THR_ACT_NULL);
queue_iterate(&task->threads, thr_act, thread_act_t, task_threads) {
if (act_id-- <= 0)
return(thr_act);
}
return(THR_ACT_NULL);
}
boolean_t
db_get_next_act(
thread_act_t *actp,
int position)
{
db_expr_t value;
thread_act_t thr_act;
*actp = THR_ACT_NULL;
if (db_expression(&value)) {
thr_act = (thread_act_t) value;
if (!db_check_act_address_valid(thr_act)) {
db_flush_lex();
return(FALSE);
}
} else if (position <= 0) {
thr_act = db_default_act;
} else
return(FALSE);
*actp = thr_act;
return(TRUE);
}
void
db_init_default_act(void)
{
if (db_lookup_act(db_default_act) < 0) {
db_default_act = THR_ACT_NULL;
db_default_task = TASK_NULL;
} else
db_default_task = db_default_act->task;
}
int
db_set_default_act(
struct db_variable *vp,
db_expr_t *valuep,
int flag,
db_var_aux_param_t ap)
{
thread_act_t thr_act;
int task_id;
int act_id;
if (flag == DB_VAR_SHOW) {
db_printf("%#n", db_default_act);
task_id = db_lookup_task(db_default_task);
if (task_id != -1) {
act_id = db_lookup_act(db_default_act);
if (act_id != -1) {
db_printf(" (task%d.%d)", task_id, act_id);
}
}
return(0);
}
if (flag != DB_VAR_SET) {
*valuep = (db_expr_t) db_default_act;
return(0);
}
thr_act = (thread_act_t) *valuep;
if (thr_act != THR_ACT_NULL && !db_check_act_address_valid(thr_act))
db_error(0);
db_default_act = thr_act;
if (thr_act)
db_default_task = thr_act->task;
return(0);
}
int
db_get_task_act(
struct db_variable *vp,
db_expr_t *valuep,
int flag,
db_var_aux_param_t ap)
{
task_t task;
thread_act_t thr_act;
int task_id;
if (flag == DB_VAR_SHOW) {
db_printf("%#n", db_default_task);
task_id = db_lookup_task(db_default_task);
if (task_id != -1)
db_printf(" (task%d)", task_id);
return(0);
}
if (flag != DB_VAR_GET) {
db_error("Cannot set to $task variable\n");
}
if ((task = db_lookup_task_id(ap->suffix[0])) == TASK_NULL) {
db_printf("no such task($task%d)\n", ap->suffix[0]);
db_error(0);
}
if (ap->level <= 1) {
*valuep = (db_expr_t) task;
return(0);
}
if ((thr_act = db_lookup_act_id(task, ap->suffix[1])) == THR_ACT_NULL){
db_printf("no such thr_act($task%d.%d)\n",
ap->suffix[0], ap->suffix[1]);
db_error(0);
}
*valuep = (db_expr_t) thr_act;
return(0);
}