[PATCH] fix saa7146 compilation
[linux-2.6/history.git] / kernel / ptrace.c
blob8b2856aaf6410310d83751e1af1bef61bf447115
1 /*
2 * linux/kernel/ptrace.c
4 * (C) Copyright 1999 Linus Torvalds
6 * Common interfaces for "ptrace()" which we do not want
7 * to continually duplicate across every architecture.
8 */
10 #include <linux/module.h>
11 #include <linux/sched.h>
12 #include <linux/errno.h>
13 #include <linux/mm.h>
14 #include <linux/highmem.h>
15 #include <linux/pagemap.h>
16 #include <linux/smp_lock.h>
17 #include <linux/ptrace.h>
18 #include <linux/security.h>
20 #include <asm/pgtable.h>
21 #include <asm/uaccess.h>
24 * ptrace a task: make the debugger its new parent and
25 * move it to the ptrace list.
27 * Must be called with the tasklist lock write-held.
29 void __ptrace_link(task_t *child, task_t *new_parent)
31 if (!list_empty(&child->ptrace_list))
32 BUG();
33 if (child->parent == new_parent)
34 return;
35 list_add(&child->ptrace_list, &child->parent->ptrace_children);
36 REMOVE_LINKS(child);
37 child->parent = new_parent;
38 SET_LINKS(child);
42 * unptrace a task: move it back to its original parent and
43 * remove it from the ptrace list.
45 * Must be called with the tasklist lock write-held.
47 void __ptrace_unlink(task_t *child)
49 if (!child->ptrace)
50 BUG();
51 child->ptrace = 0;
52 if (list_empty(&child->ptrace_list))
53 return;
54 list_del_init(&child->ptrace_list);
55 REMOVE_LINKS(child);
56 child->parent = child->real_parent;
57 SET_LINKS(child);
61 * Check that we have indeed attached to the thing..
63 int ptrace_check_attach(struct task_struct *child, int kill)
65 if (!(child->ptrace & PT_PTRACED))
66 return -ESRCH;
68 if (child->parent != current)
69 return -ESRCH;
71 if (!kill) {
72 if (child->state != TASK_STOPPED)
73 return -ESRCH;
74 wait_task_inactive(child);
77 /* All systems go.. */
78 return 0;
81 int ptrace_attach(struct task_struct *task)
83 int retval;
84 task_lock(task);
85 retval = -EPERM;
86 if (task->pid <= 1)
87 goto bad;
88 if (task == current)
89 goto bad;
90 if (!task->mm)
91 goto bad;
92 if(((current->uid != task->euid) ||
93 (current->uid != task->suid) ||
94 (current->uid != task->uid) ||
95 (current->gid != task->egid) ||
96 (current->gid != task->sgid) ||
97 (current->gid != task->gid)) && !capable(CAP_SYS_PTRACE))
98 goto bad;
99 rmb();
100 if (!task->mm->dumpable && !capable(CAP_SYS_PTRACE))
101 goto bad;
102 /* the same process cannot be attached many times */
103 if (task->ptrace & PT_PTRACED)
104 goto bad;
105 retval = security_ptrace(current, task);
106 if (retval)
107 goto bad;
109 /* Go */
110 task->ptrace |= PT_PTRACED;
111 if (capable(CAP_SYS_PTRACE))
112 task->ptrace |= PT_PTRACE_CAP;
113 task_unlock(task);
115 write_lock_irq(&tasklist_lock);
116 __ptrace_link(task, current);
117 write_unlock_irq(&tasklist_lock);
119 force_sig_specific(SIGSTOP, task);
120 return 0;
122 bad:
123 task_unlock(task);
124 return retval;
127 int ptrace_detach(struct task_struct *child, unsigned int data)
129 if ((unsigned long) data > _NSIG)
130 return -EIO;
132 /* Architecture-specific hardware disable .. */
133 ptrace_disable(child);
135 /* .. re-parent .. */
136 child->exit_code = data;
138 write_lock_irq(&tasklist_lock);
139 __ptrace_unlink(child);
140 /* .. and wake it up. */
141 if (child->state != TASK_ZOMBIE)
142 wake_up_process(child);
143 write_unlock_irq(&tasklist_lock);
145 return 0;
149 * Access another process' address space.
150 * Source/target buffer must be kernel space,
151 * Do not walk the page table directly, use get_user_pages
154 int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
156 struct mm_struct *mm;
157 struct vm_area_struct *vma;
158 struct page *page;
159 void *old_buf = buf;
161 mm = get_task_mm(tsk);
162 if (!mm)
163 return 0;
165 down_read(&mm->mmap_sem);
166 /* ignore errors, just check how much was sucessfully transfered */
167 while (len) {
168 int bytes, ret, offset;
169 void *maddr;
171 ret = get_user_pages(tsk, mm, addr, 1,
172 write, 1, &page, &vma);
173 if (ret <= 0)
174 break;
176 bytes = len;
177 offset = addr & (PAGE_SIZE-1);
178 if (bytes > PAGE_SIZE-offset)
179 bytes = PAGE_SIZE-offset;
181 flush_cache_page(vma, addr);
183 maddr = kmap(page);
184 if (write) {
185 copy_to_user_page(vma, page, addr,
186 maddr + offset, buf, bytes);
187 set_page_dirty_lock(page);
188 } else {
189 copy_from_user_page(vma, page, addr,
190 buf, maddr + offset, bytes);
192 kunmap(page);
193 page_cache_release(page);
194 len -= bytes;
195 buf += bytes;
196 addr += bytes;
198 up_read(&mm->mmap_sem);
199 mmput(mm);
201 return buf - old_buf;
204 int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len)
206 int copied = 0;
208 while (len > 0) {
209 char buf[128];
210 int this_len, retval;
212 this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
213 retval = access_process_vm(tsk, src, buf, this_len, 0);
214 if (!retval) {
215 if (copied)
216 break;
217 return -EIO;
219 if (copy_to_user(dst, buf, retval))
220 return -EFAULT;
221 copied += retval;
222 src += retval;
223 dst += retval;
224 len -= retval;
226 return copied;
229 int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len)
231 int copied = 0;
233 while (len > 0) {
234 char buf[128];
235 int this_len, retval;
237 this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
238 if (copy_from_user(buf, src, this_len))
239 return -EFAULT;
240 retval = access_process_vm(tsk, dst, buf, this_len, 1);
241 if (!retval) {
242 if (copied)
243 break;
244 return -EIO;
246 copied += retval;
247 src += retval;
248 dst += retval;
249 len -= retval;
251 return copied;
254 static int ptrace_setoptions(struct task_struct *child, long data)
256 child->ptrace &= ~PT_TRACE_MASK;
258 if (data & PTRACE_O_TRACESYSGOOD)
259 child->ptrace |= PT_TRACESYSGOOD;
261 if (data & PTRACE_O_TRACEFORK)
262 child->ptrace |= PT_TRACE_FORK;
264 if (data & PTRACE_O_TRACEVFORK)
265 child->ptrace |= PT_TRACE_VFORK;
267 if (data & PTRACE_O_TRACECLONE)
268 child->ptrace |= PT_TRACE_CLONE;
270 if (data & PTRACE_O_TRACEEXEC)
271 child->ptrace |= PT_TRACE_EXEC;
273 if (data & PTRACE_O_TRACEVFORKDONE)
274 child->ptrace |= PT_TRACE_VFORK_DONE;
276 if (data & PTRACE_O_TRACEEXIT)
277 child->ptrace |= PT_TRACE_EXIT;
279 return (data & ~PTRACE_O_MASK) ? -EINVAL : 0;
282 static int ptrace_getsiginfo(struct task_struct *child, siginfo_t __user * data)
284 if (child->last_siginfo == NULL)
285 return -EINVAL;
286 return copy_siginfo_to_user(data, child->last_siginfo);
289 static int ptrace_setsiginfo(struct task_struct *child, siginfo_t __user * data)
291 if (child->last_siginfo == NULL)
292 return -EINVAL;
293 if (copy_from_user(child->last_siginfo, data, sizeof (siginfo_t)) != 0)
294 return -EFAULT;
295 return 0;
298 int ptrace_request(struct task_struct *child, long request,
299 long addr, long data)
301 int ret = -EIO;
303 switch (request) {
304 #ifdef PTRACE_OLDSETOPTIONS
305 case PTRACE_OLDSETOPTIONS:
306 #endif
307 case PTRACE_SETOPTIONS:
308 ret = ptrace_setoptions(child, data);
309 break;
310 case PTRACE_GETEVENTMSG:
311 ret = put_user(child->ptrace_message, (unsigned long __user *) data);
312 break;
313 case PTRACE_GETSIGINFO:
314 ret = ptrace_getsiginfo(child, (siginfo_t __user *) data);
315 break;
316 case PTRACE_SETSIGINFO:
317 ret = ptrace_setsiginfo(child, (siginfo_t __user *) data);
318 break;
319 default:
320 break;
323 return ret;
326 void ptrace_notify(int exit_code)
328 BUG_ON (!(current->ptrace & PT_PTRACED));
330 /* Let the debugger run. */
331 current->exit_code = exit_code;
332 set_current_state(TASK_STOPPED);
333 notify_parent(current, SIGCHLD);
334 schedule();
337 * Signals sent while we were stopped might set TIF_SIGPENDING.
340 spin_lock_irq(&current->sighand->siglock);
341 recalc_sigpending();
342 spin_unlock_irq(&current->sighand->siglock);
345 EXPORT_SYMBOL(ptrace_notify);