dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / mdb / common / kmdb / kmdb_context.c
blob2a9ca2a4479242fa138e986b131109929c7a23bb
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 * Debugger co-routine context support. kmdb co-routines are essentially the
31 * same as the ones used by mdb, with the exception that we allocate the stack
32 * for the co-routine from our heap.
35 #include <kmdb/kmdb_context_impl.h>
36 #include <mdb/mdb_modapi.h>
37 #include <mdb/mdb_debug.h>
38 #include <mdb/mdb_err.h>
39 #include <mdb/mdb_umem.h>
40 #include <mdb/mdb.h>
42 #include <sys/types.h>
44 #include <ucontext.h>
45 #include <setjmp.h>
47 static void
48 context_init(mdb_context_t *volatile c)
50 c->ctx_status = c->ctx_func();
51 ASSERT(c->ctx_resumes > 0);
52 longjmp(c->ctx_pcb, 1);
55 mdb_context_t *
56 mdb_context_create(int (*func)(void))
58 mdb_context_t *c = mdb_zalloc(sizeof (mdb_context_t), UM_NOSLEEP);
59 size_t pagesize = mdb.m_pagesize;
61 if (c == NULL)
62 return (NULL);
64 c->ctx_func = func;
65 c->ctx_stacksize = pagesize * 4;
66 c->ctx_stack = mdb_alloc_align(c->ctx_stacksize, pagesize, UM_NOSLEEP);
68 if (c->ctx_stack == NULL) {
69 mdb_free(c, sizeof (mdb_context_t));
70 return (NULL);
73 kmdb_makecontext(&c->ctx_uc, (void (*)(void *))context_init, c,
74 c->ctx_stack, c->ctx_stacksize);
76 return (c);
79 void
80 mdb_context_destroy(mdb_context_t *c)
82 mdb_free_align(c->ctx_stack, c->ctx_stacksize);
83 mdb_free(c, sizeof (mdb_context_t));
86 void
87 mdb_context_switch(mdb_context_t *c)
89 if (setjmp(c->ctx_pcb) == 0 && kmdb_setcontext(&c->ctx_uc) == -1)
90 fail("failed to change context to %p", (void *)c);
91 else
92 fail("unexpectedly returned from context %p", (void *)c);
95 jmp_buf *
96 mdb_context_getpcb(mdb_context_t *c)
98 c->ctx_resumes++;
99 return (&c->ctx_pcb);