winebuild: Make the cpu flag more generic to allow supporting a given entry point...
[wine/wine64.git] / server / procfs.c
blobda22af30c44ea6806bb3384ccaabb63da8f3cde6
1 /*
2 * Server-side /proc support for Solaris
4 * Copyright (C) 2007 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
23 #include <assert.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <stdio.h>
27 #include <signal.h>
28 #include <stdarg.h>
29 #include <sys/types.h>
30 #include <unistd.h>
32 #include "ntstatus.h"
33 #define WIN32_NO_STATUS
34 #include "winternl.h"
36 #include "file.h"
37 #include "process.h"
38 #include "thread.h"
40 #ifdef USE_PROCFS
42 #include <procfs.h>
44 static int open_proc_as( struct process *process, int flags )
46 char buffer[32];
47 int fd;
49 if (process->unix_pid == -1)
51 set_error( STATUS_ACCESS_DENIED );
52 return -1;
55 sprintf( buffer, "/proc/%u/as", process->unix_pid );
56 if ((fd = open( buffer, flags )) == -1)
58 if (errno == ENOENT) /* probably got killed */
60 process->unix_pid = -1;
61 set_error( STATUS_ACCESS_DENIED );
63 else file_set_error();
65 return fd;
68 static int open_proc_lwpctl( struct thread *thread )
70 char buffer[48];
71 int fd;
73 if (thread->unix_pid == -1) return -1;
75 sprintf( buffer, "/proc/%u/lwp/%u/lwpctl", thread->unix_pid, thread->unix_tid );
76 if ((fd = open( buffer, O_WRONLY )) == -1)
78 if (errno == ENOENT) /* probably got killed */
79 thread->unix_pid = thread->unix_tid = -1;
80 else
81 file_set_error();
83 return fd;
87 /* handle a SIGCHLD signal */
88 void sigchld_callback(void)
90 assert( 0 ); /* should only be called when using ptrace */
93 /* initialize the process tracing mechanism */
94 void init_tracing_mechanism(void)
96 /* no initialization needed */
99 /* initialize the per-process tracing mechanism */
100 void init_process_tracing( struct process *process )
102 /* setup is done on-demand */
105 /* terminate the per-process tracing mechanism */
106 void finish_process_tracing( struct process *process )
110 /* send a Unix signal to a specific thread */
111 int send_thread_signal( struct thread *thread, int sig )
113 int fd = open_proc_lwpctl( thread );
114 long kill[2];
115 ssize_t ret;
117 if (fd == -1) return 0;
119 kill[0] = PCKILL;
120 kill[1] = sig;
121 ret = write( fd, kill, sizeof(kill) );
122 close( fd );
123 return (ret == sizeof(kill));
126 /* read data from a process memory space */
127 int read_process_memory( struct process *process, const void *ptr, size_t size, char *dest )
129 ssize_t ret;
130 int fd = open_proc_as( process, O_RDONLY );
132 if (fd == -1) return 0;
134 ret = pread( fd, dest, size, (off_t)ptr );
135 close( fd );
136 if (ret == size) return 1;
138 if (ret == -1) file_set_error();
139 else set_error( STATUS_ACCESS_VIOLATION );
140 return 0;
143 /* write data to a process memory space */
144 int write_process_memory( struct process *process, void *ptr, size_t size, const char *src )
146 ssize_t ret;
147 int fd = open_proc_as( process, O_WRONLY );
149 if (fd == -1) return 0;
151 ret = pwrite( fd, src, size, (off_t)ptr );
152 close( fd );
153 if (ret == size) return 1;
155 if (ret == -1) file_set_error();
156 else set_error( STATUS_ACCESS_VIOLATION );
157 return 0;
160 /* retrieve an LDT selector entry */
161 void get_selector_entry( struct thread *thread, int entry, unsigned int *base,
162 unsigned int *limit, unsigned char *flags )
164 ssize_t ret;
165 off_t pos = (off_t)thread->process->ldt_copy;
166 int fd = open_proc_as( thread->process, O_RDONLY );
168 if (fd == -1) return;
170 ret = pread( fd, base, sizeof(*base), pos + entry*sizeof(int) );
171 if (ret != sizeof(*base)) goto error;
172 ret = pread( fd, limit, sizeof(*limit), pos + (8192 + entry)*sizeof(int) );
173 if (ret != sizeof(*limit)) goto error;
174 ret = pread( fd, flags, sizeof(*flags), pos + 2*8192*sizeof(int) + entry );
175 if (ret != sizeof(*flags)) goto error;
176 close( fd );
177 return;
179 error:
180 if (ret == -1) file_set_error();
181 else set_error( STATUS_ACCESS_VIOLATION );
182 close( fd );
185 /* retrieve the thread registers */
186 void get_thread_context( struct thread *thread, CONTEXT *context, unsigned int flags )
188 /* all other regs are handled on the client side */
189 assert( (flags | CONTEXT_i386) == CONTEXT_DEBUG_REGISTERS );
191 /* FIXME: get debug registers */
194 /* set the thread registers */
195 void set_thread_context( struct thread *thread, const CONTEXT *context, unsigned int flags )
197 /* all other regs are handled on the client side */
198 assert( (flags | CONTEXT_i386) == CONTEXT_DEBUG_REGISTERS );
200 /* FIXME: set debug registers */
203 #endif /* USE_PROCFS */