1312123Sngie/*	$NetBSD: h_common.h,v 1.2 2016/11/19 02:30:54 kamil Exp $	*/
2312123Sngie
3312123Sngie/*-
4312123Sngie * Copyright (c) 2016 The NetBSD Foundation, Inc.
5312123Sngie * All rights reserved.
6312123Sngie *
7312123Sngie * Redistribution and use in source and binary forms, with or without
8312123Sngie * modification, are permitted provided that the following conditions
9312123Sngie * are met:
10312123Sngie * 1. Redistributions of source code must retain the above copyright
11312123Sngie *    notice, this list of conditions and the following disclaimer.
12312123Sngie * 2. Redistributions in binary form must reproduce the above copyright
13312123Sngie *    notice, this list of conditions and the following disclaimer in the
14312123Sngie *    documentation and/or other materials provided with the distribution.
15312123Sngie *
16312123Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17312123Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18312123Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19312123Sngie * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20312123Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21312123Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22312123Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23312123Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24312123Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25312123Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26312123Sngie * POSSIBILITY OF SUCH DAMAGE.
27312123Sngie */
28312123Sngie
29312123Sngie
30312123Sngie#ifndef H_COMMON_H
31312123Sngie#define H_COMMON_H
32312123Sngie
33312123Sngie#include <sys/cdefs.h>
34312123Sngie#include <dlfcn.h>
35312123Sngie#include <pthread_dbg.h>
36312123Sngie#include <string.h>
37312123Sngie
38312123Sngie#include <atf-c.h>
39312123Sngie
40312123Sngie#define PTHREAD_REQUIRE(x) \
41312123Sngie    do { \
42312123Sngie        int ret = (x); \
43312123Sngie        ATF_REQUIRE_MSG(ret == 0, "%s: %s", #x, strerror(ret)); \
44312123Sngie    } while (0)
45312123Sngie
46312123Sngie#define PTHREAD_REQUIRE_STATUS(x, v) \
47312123Sngie    do { \
48312123Sngie        int ret = (x); \
49312123Sngie        ATF_REQUIRE_MSG(ret == (v), "%s: %s", #x, strerror(ret)); \
50312123Sngie    } while (0)
51312123Sngie
52312123Sngiestatic int __used
53312123Sngiedummy_proc_read(void *arg, caddr_t addr, void *buf, size_t size)
54312123Sngie{
55312123Sngie	return TD_ERR_ERR;
56312123Sngie}
57312123Sngie
58312123Sngiestatic int __used
59312123Sngiedummy_proc_write(void *arg, caddr_t addr, void *buf, size_t size)
60312123Sngie{
61312123Sngie	return TD_ERR_ERR;
62312123Sngie}
63312123Sngie
64312123Sngiestatic int __used
65312123Sngiedummy_proc_lookup(void *arg, const char *sym, caddr_t *addr)
66312123Sngie{
67312123Sngie	return TD_ERR_ERR;
68312123Sngie}
69312123Sngie
70312123Sngiestatic int __used
71312123Sngiedummy_proc_regsize(void *arg, int regset, size_t *size)
72312123Sngie{
73312123Sngie	return TD_ERR_ERR;
74312123Sngie}
75312123Sngie
76312123Sngiestatic int __used
77312123Sngiedummy_proc_getregs(void *arg, int regset, int lwp, void *buf)
78312123Sngie{
79312123Sngie	return TD_ERR_ERR;
80312123Sngie}
81312123Sngie
82312123Sngiestatic int __used
83312123Sngiedummy_proc_setregs(void *arg, int regset, int lwp, void *buf)
84312123Sngie{
85312123Sngie	return TD_ERR_ERR;
86312123Sngie}
87312123Sngie
88312123Sngie/* Minimalistic basic implementation */
89312123Sngie
90312123Sngiestatic int __used
91312123Sngiebasic_proc_read(void *arg, caddr_t addr, void *buf, size_t size)
92312123Sngie{
93312123Sngie	memcpy(buf, addr, size);
94312123Sngie
95312123Sngie	return TD_ERR_OK;
96312123Sngie}
97312123Sngie
98312123Sngiestatic int __used
99312123Sngiebasic_proc_write(void *arg, caddr_t addr, void *buf, size_t size)
100312123Sngie{
101312123Sngie	memcpy(addr, buf, size);
102312123Sngie
103312123Sngie	return TD_ERR_OK;
104312123Sngie}
105312123Sngie
106312123Sngiestatic int __used
107312123Sngiebasic_proc_lookup(void *arg, const char *sym, caddr_t *addr)
108312123Sngie{
109312123Sngie	void *handle;
110312123Sngie	void *symbol;
111312123Sngie
112312123Sngie	ATF_REQUIRE_MSG((handle = dlopen(NULL, RTLD_LOCAL | RTLD_LAZY))
113312123Sngie	    != NULL, "dlopen(3) failed: %s", dlerror());
114312123Sngie
115312123Sngie	symbol = dlsym(handle, sym);
116312123Sngie
117312123Sngie	ATF_REQUIRE_MSG(dlclose(handle) == 0, "dlclose(3) failed: %s",
118312123Sngie	    dlerror());
119312123Sngie
120312123Sngie	if (!symbol)
121312123Sngie		return TD_ERR_NOSYM;
122312123Sngie
123312123Sngie	*addr = (caddr_t)(uintptr_t)symbol;
124312123Sngie
125312123Sngie	return TD_ERR_OK;
126312123Sngie}
127312123Sngie
128312123Sngie#endif // H_COMMON_H
129