xref: /netbsd-src/sys/ddb/db_syncobj.c (revision 5a1f45c803faf40d504feea71bd33282b91c31cd)
1 /*	$NetBSD: db_syncobj.c,v 1.3 2023/10/15 10:27:25 riastradh Exp $	*/
2 
3 /*-
4  * Copyright (c) 2023 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #define	__MUTEX_PRIVATE
30 #define	__RWLOCK_PRIVATE
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: db_syncobj.c,v 1.3 2023/10/15 10:27:25 riastradh Exp $");
34 
35 #include <sys/types.h>
36 
37 #include <sys/mutex.h>
38 #include <sys/null.h>
39 #include <sys/rwlock.h>
40 #include <sys/syncobj.h>
41 
42 #include <ddb/ddb.h>
43 
44 struct lwp *
db_syncobj_owner(const struct syncobj * sobj,wchan_t wchan)45 db_syncobj_owner(const struct syncobj *sobj, wchan_t wchan)
46 {
47 	db_expr_t mutex_syncobj_;
48 	db_expr_t rw_syncobj_;
49 
50 	if (db_value_of_name("mutex_syncobj", &mutex_syncobj_) &&
51 	    (db_expr_t)(uintptr_t)sobj == mutex_syncobj_) {
52 		volatile const struct kmutex *mtx = wchan;
53 		uintptr_t owner;
54 
55 		db_read_bytes((db_addr_t)&mtx->mtx_owner, sizeof(owner),
56 		    (char *)&owner);
57 		return (struct lwp *)(owner & MUTEX_THREAD);
58 
59 	} else if (db_value_of_name("rw_syncobj", &rw_syncobj_) &&
60 	    (db_expr_t)(uintptr_t)sobj == rw_syncobj_) {
61 		volatile const struct krwlock *rw = wchan;
62 		uintptr_t owner;
63 
64 		db_read_bytes((db_addr_t)&rw->rw_owner, sizeof(owner),
65 		    (char *)&owner);
66 		if (owner & RW_WRITE_LOCKED)
67 			return (struct lwp *)(owner & RW_THREAD);
68 		return NULL;
69 
70 	} else {
71 		return NULL;
72 	}
73 }
74