h_common.h revision 312126
1287111Smarcel/*	$NetBSD: h_common.h,v 1.2 2016/11/19 02:30:54 kamil Exp $	*/
2287111Smarcel
3287111Smarcel/*-
4287111Smarcel * Copyright (c) 2016 The NetBSD Foundation, Inc.
5287111Smarcel * All rights reserved.
6287111Smarcel *
7287111Smarcel * Redistribution and use in source and binary forms, with or without
8287111Smarcel * modification, are permitted provided that the following conditions
9287111Smarcel * are met:
10287111Smarcel * 1. Redistributions of source code must retain the above copyright
11287111Smarcel *    notice, this list of conditions and the following disclaimer.
12287111Smarcel * 2. Redistributions in binary form must reproduce the above copyright
13287111Smarcel *    notice, this list of conditions and the following disclaimer in the
14287111Smarcel *    documentation and/or other materials provided with the distribution.
15287111Smarcel *
16287111Smarcel * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17287111Smarcel * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18287111Smarcel * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19287111Smarcel * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20287111Smarcel * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21287111Smarcel * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22287111Smarcel * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23287111Smarcel * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24287111Smarcel * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25287111Smarcel * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26287111Smarcel * POSSIBILITY OF SUCH DAMAGE.
27287111Smarcel */
28287111Smarcel
29287111Smarcel
30287111Smarcel#ifndef H_COMMON_H
31287111Smarcel#define H_COMMON_H
32287111Smarcel
33287111Smarcel#include <sys/cdefs.h>
34287111Smarcel#include <dlfcn.h>
35287111Smarcel#include <pthread_dbg.h>
36287111Smarcel#include <string.h>
37287111Smarcel
38287111Smarcel#include <atf-c.h>
39287111Smarcel
40287111Smarcel#define PTHREAD_REQUIRE(x) \
41287111Smarcel    do { \
42287111Smarcel        int ret = (x); \
43287111Smarcel        ATF_REQUIRE_MSG(ret == 0, "%s: %s", #x, strerror(ret)); \
44287111Smarcel    } while (0)
45287111Smarcel
46287111Smarcel#define PTHREAD_REQUIRE_STATUS(x, v) \
47287111Smarcel    do { \
48287111Smarcel        int ret = (x); \
49287111Smarcel        ATF_REQUIRE_MSG(ret == (v), "%s: %s", #x, strerror(ret)); \
50287111Smarcel    } while (0)
51287111Smarcel
52287111Smarcelstatic int __used
53287111Smarceldummy_proc_read(void *arg, caddr_t addr, void *buf, size_t size)
54287111Smarcel{
55287111Smarcel	return TD_ERR_ERR;
56287111Smarcel}
57287111Smarcel
58287111Smarcelstatic int __used
59287111Smarceldummy_proc_write(void *arg, caddr_t addr, void *buf, size_t size)
60287111Smarcel{
61287111Smarcel	return TD_ERR_ERR;
62287111Smarcel}
63287111Smarcel
64287111Smarcelstatic int __used
65287111Smarceldummy_proc_lookup(void *arg, const char *sym, caddr_t *addr)
66287111Smarcel{
67287111Smarcel	return TD_ERR_ERR;
68287111Smarcel}
69287111Smarcel
70287111Smarcelstatic int __used
71287111Smarceldummy_proc_regsize(void *arg, int regset, size_t *size)
72287111Smarcel{
73287111Smarcel	return TD_ERR_ERR;
74287111Smarcel}
75287111Smarcel
76287111Smarcelstatic int __used
77287111Smarceldummy_proc_getregs(void *arg, int regset, int lwp, void *buf)
78287111Smarcel{
79287111Smarcel	return TD_ERR_ERR;
80287111Smarcel}
81287111Smarcel
82287111Smarcelstatic int __used
83287111Smarceldummy_proc_setregs(void *arg, int regset, int lwp, void *buf)
84287111Smarcel{
85287111Smarcel	return TD_ERR_ERR;
86287111Smarcel}
87287111Smarcel
88287111Smarcel/* Minimalistic basic implementation */
89287111Smarcel
90287111Smarcelstatic int __used
91287111Smarcelbasic_proc_read(void *arg, caddr_t addr, void *buf, size_t size)
92287111Smarcel{
93287111Smarcel	memcpy(buf, addr, size);
94287111Smarcel
95287111Smarcel	return TD_ERR_OK;
96287111Smarcel}
97287111Smarcel
98287111Smarcelstatic int __used
99287111Smarcelbasic_proc_write(void *arg, caddr_t addr, void *buf, size_t size)
100287111Smarcel{
101287111Smarcel	memcpy(addr, buf, size);
102287111Smarcel
103287111Smarcel	return TD_ERR_OK;
104287111Smarcel}
105287111Smarcel
106287111Smarcelstatic int __used
107287111Smarcelbasic_proc_lookup(void *arg, const char *sym, caddr_t *addr)
108287111Smarcel{
109287111Smarcel	void *handle;
110287111Smarcel	void *symbol;
111287111Smarcel
112287111Smarcel	ATF_REQUIRE_MSG((handle = dlopen(NULL, RTLD_LOCAL | RTLD_LAZY))
113287111Smarcel	    != NULL, "dlopen(3) failed: %s", dlerror());
114287111Smarcel
115287111Smarcel	symbol = dlsym(handle, sym);
116287111Smarcel
117	ATF_REQUIRE_MSG(dlclose(handle) == 0, "dlclose(3) failed: %s",
118	    dlerror());
119
120	if (!symbol)
121		return TD_ERR_NOSYM;
122
123	*addr = (caddr_t)(uintptr_t)symbol;
124
125	return TD_ERR_OK;
126}
127
128#endif // H_COMMON_H
129