sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1 | |
| 2 | Dealing with missing system call or ioctl wrappers in Valgrind |
| 3 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 4 | You're probably reading this because Valgrind bombed out whilst |
| 5 | running your program, and advised you to read this file. The good |
| 6 | news is that, in general, it's easy to write the missing syscall or |
| 7 | ioctl wrappers you need, so that you can continue your debugging. If |
| 8 | you send the resulting patches to me, then you'll be doing a favour to |
| 9 | all future Valgrind users too. |
| 10 | |
| 11 | Note that an "ioctl" is just a special kind of system call, really; so |
| 12 | there's not a lot of need to distinguish them (at least conceptually) |
| 13 | in the discussion that follows. |
| 14 | |
njn | 75b31b3 | 2003-06-12 11:24:10 +0000 | [diff] [blame] | 15 | All this machinery is in coregrind/vg_syscalls.c. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 16 | |
| 17 | |
| 18 | What are syscall/ioctl wrappers? What do they do? |
| 19 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
njn | 75b31b3 | 2003-06-12 11:24:10 +0000 | [diff] [blame] | 20 | Valgrind does what it does, in part, by keeping track of everything your |
| 21 | program does. When a system call happens, for example a request to read |
| 22 | part of a file, control passes to the Linux kernel, which fulfills the |
| 23 | request, and returns control to your program. The problem is that the |
| 24 | kernel will often change the status of some part of your program's memory |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 25 | as a result, and tools (instrumentation plug-ins) may need to know about |
njn | 75b31b3 | 2003-06-12 11:24:10 +0000 | [diff] [blame] | 26 | this. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 27 | |
njn | 75b31b3 | 2003-06-12 11:24:10 +0000 | [diff] [blame] | 28 | Syscall and ioctl wrappers have two jobs: |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 29 | |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 30 | 1. Tell a tool what's about to happen, before the syscall takes place. A |
| 31 | tool could perform checks beforehand, eg. if memory about to be written |
njn | 75b31b3 | 2003-06-12 11:24:10 +0000 | [diff] [blame] | 32 | is actually writeable. This part is useful, but not strictly |
| 33 | essential. |
| 34 | |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 35 | 2. Tell a tool what just happened, after a syscall takes place. This is |
njn | 75b31b3 | 2003-06-12 11:24:10 +0000 | [diff] [blame] | 36 | so it can update its view of the program's state, eg. that memory has |
| 37 | just been written to. This step is essential. |
| 38 | |
| 39 | The "happenings" mostly involve reading/writing of memory. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 40 | |
| 41 | So, let's look at an example of a wrapper for a system call which |
| 42 | should be familiar to many Unix programmers. |
| 43 | |
| 44 | |
njn | 75b31b3 | 2003-06-12 11:24:10 +0000 | [diff] [blame] | 45 | The syscall wrapper for time() |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 46 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 47 | Removing the debug printing clutter, it looks like this: |
| 48 | |
nethercote | 98ae6da | 2004-01-19 19:32:30 +0000 | [diff] [blame] | 49 | PRE(time) |
| 50 | { |
njn | 75b31b3 | 2003-06-12 11:24:10 +0000 | [diff] [blame] | 51 | /* time_t time(time_t *t); */ |
nethercote | 98ae6da | 2004-01-19 19:32:30 +0000 | [diff] [blame] | 52 | MAYBE_PRINTF("time ( %p )\n",arg1); |
njn | 75b31b3 | 2003-06-12 11:24:10 +0000 | [diff] [blame] | 53 | if (arg1 != (UInt)NULL) { |
nethercote | 98ae6da | 2004-01-19 19:32:30 +0000 | [diff] [blame] | 54 | SYSCALL_TRACK( pre_mem_write, tid, "time", arg1, sizeof(time_t) ); |
njn | 75b31b3 | 2003-06-12 11:24:10 +0000 | [diff] [blame] | 55 | } |
nethercote | 98ae6da | 2004-01-19 19:32:30 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | POST(time) |
| 59 | { |
| 60 | if (arg1 != (UInt)NULL) { |
njn | 75b31b3 | 2003-06-12 11:24:10 +0000 | [diff] [blame] | 61 | VG_TRACK( post_mem_write, arg1, sizeof(time_t) ); |
| 62 | } |
nethercote | 98ae6da | 2004-01-19 19:32:30 +0000 | [diff] [blame] | 63 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 64 | |
nethercote | 98ae6da | 2004-01-19 19:32:30 +0000 | [diff] [blame] | 65 | The first thing we do happens before the syscall occurs, in the PRE() function: |
| 66 | if a non-NULL buffer is passed in as the argument, tell the tool that the |
| 67 | buffer is about to be written to: |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 68 | |
njn | 75b31b3 | 2003-06-12 11:24:10 +0000 | [diff] [blame] | 69 | if (arg1 != (UInt)NULL) { |
| 70 | SYSCALL_TRACK( pre_mem_write, tst, "time", arg1, sizeof(time_t) ); |
| 71 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 72 | |
nethercote | 98ae6da | 2004-01-19 19:32:30 +0000 | [diff] [blame] | 73 | Finally, the really important bit, after the syscall occurs, in the POST() |
| 74 | function: if, and only if, the system call was successful, tell the tool that |
| 75 | the memory was written: |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 76 | |
nethercote | 98ae6da | 2004-01-19 19:32:30 +0000 | [diff] [blame] | 77 | if (arg1 != (UInt)NULL) { |
njn | 75b31b3 | 2003-06-12 11:24:10 +0000 | [diff] [blame] | 78 | VG_TRACK( post_mem_write, arg1, sizeof(time_t) ); |
| 79 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 80 | |
fitzhardinge | 603e8c5 | 2004-01-19 22:02:43 +0000 | [diff] [blame] | 81 | The POST() function won't be called if the syscall failed, so you |
| 82 | don't need to worry about checking that in the POST() function. |
| 83 | (Note: this is sometimes a bug; some syscalls do return results when |
| 84 | they "fail" - for example, nanosleep returns the amount of unslept |
| 85 | time if interrupted. TODO: add another per-syscall flag for this |
| 86 | case.) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 87 | |
| 88 | |
| 89 | Writing your own syscall wrappers (see below for ioctl wrappers) |
| 90 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 91 | If Valgrind tells you that system call NNN is unimplemented, do the |
| 92 | following: |
| 93 | |
| 94 | 1. Find out the name of the system call: |
| 95 | |
| 96 | grep NNN /usr/include/asm/unistd.h |
| 97 | |
| 98 | This should tell you something like __NR_mysyscallname. |
fitzhardinge | 603e8c5 | 2004-01-19 22:02:43 +0000 | [diff] [blame] | 99 | Copy this entry to coregrind/vg_unistd.h. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 100 | |
| 101 | 2. Do 'man 2 mysyscallname' to get some idea of what the syscall |
fitzhardinge | 603e8c5 | 2004-01-19 22:02:43 +0000 | [diff] [blame] | 102 | does. Note that the actual kernel interface can differ from this, |
| 103 | so you might also want to check a version of the Linux kernel |
| 104 | source. |
| 105 | |
| 106 | NOTE: any syscall which has something to do with signals or |
| 107 | threads is probably "special", and needs more careful handling. |
| 108 | Post something to valgrind-developers if you aren't sure. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 109 | |
| 110 | |
| 111 | 3. Add a case to the already-huge collection of wrappers in |
njn | 39209d4 | 2003-06-13 15:02:29 +0000 | [diff] [blame] | 112 | coregrind/vg_syscalls.c. For each in-memory parameter which is |
| 113 | read or written by the syscall, do one of |
njn | 75b31b3 | 2003-06-12 11:24:10 +0000 | [diff] [blame] | 114 | |
| 115 | SYSCALL_TRACK( pre_mem_read, ... ) |
| 116 | SYSCALL_TRACK( pre_mem_read_asciiz, ... ) |
| 117 | SYSCALL_TRACK( pre_mem_write, ... ) |
| 118 | |
| 119 | for that parameter. Then do the syscall. Then, if the syscall |
| 120 | succeeds, issue suitable VG_TRACK( post_mem_write, ... ) calls. |
| 121 | (There's no need for post_mem_read calls.) |
nethercote | 98ae6da | 2004-01-19 19:32:30 +0000 | [diff] [blame] | 122 | |
fitzhardinge | 603e8c5 | 2004-01-19 22:02:43 +0000 | [diff] [blame] | 123 | Also, add it to the sys_info[] array; use SYSBA if it requires a |
| 124 | PRE() and POST() function, and SYSB_ if it only requires a PRE() |
| 125 | function. The 2nd arg of these macros indicate if the syscall |
| 126 | could possibly block. |
njn | 75b31b3 | 2003-06-12 11:24:10 +0000 | [diff] [blame] | 127 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 128 | If you find this difficult, read the wrappers for other syscalls |
| 129 | for ideas. A good tip is to look for the wrapper for a syscall |
| 130 | which has a similar behaviour to yours, and use it as a |
| 131 | starting point. |
| 132 | |
fitzhardinge | 603e8c5 | 2004-01-19 22:02:43 +0000 | [diff] [blame] | 133 | If you need structure definitions for your syscall, you can copy |
| 134 | structure definitions from the kernel headers into |
| 135 | include/vg_kerneliface.h, with the appropriate vki_* name |
| 136 | mangling. Alternatively, you can #include headers for structure |
| 137 | definitions, put your #includes into vg_unsafe.h (copying |
| 138 | syscall-related things into vg_kerneliface.h is preferred though). |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 139 | |
| 140 | Test it. |
| 141 | |
njn | 75b31b3 | 2003-06-12 11:24:10 +0000 | [diff] [blame] | 142 | Note that a common error is to call VG_TRACK( post_mem_write, ... ) |
| 143 | with 0 (NULL) as the first (address) argument. This usually means |
| 144 | your logic is slightly inadequate. It's a sufficiently common bug |
| 145 | that there's a built-in check for it, and you'll get a "probably |
| 146 | sanity check failure" for the syscall wrapper you just made, if this |
| 147 | is the case. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 148 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 149 | |
nethercote | 98ae6da | 2004-01-19 19:32:30 +0000 | [diff] [blame] | 150 | 4. Once happy, send us the patch. Pretty please. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | Writing your own ioctl wrappers |
| 156 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
fitzhardinge | 603e8c5 | 2004-01-19 22:02:43 +0000 | [diff] [blame] | 157 | |
| 158 | Is pretty much the same as writing syscall wrappers, except that all |
| 159 | the action happens within PRE(ioctl) and POST(ioctl). |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 160 | |
njn | 75b31b3 | 2003-06-12 11:24:10 +0000 | [diff] [blame] | 161 | There's a default case, sometimes it isn't correct and you have to write a |
| 162 | more specific case to get the right behaviour. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 163 | |
nethercote | 98ae6da | 2004-01-19 19:32:30 +0000 | [diff] [blame] | 164 | As above, please create a bug report and attach the patch as described |
mueller | 30c956a | 2004-01-02 23:27:29 +0000 | [diff] [blame] | 165 | on http://valgrind.kde.org/bugs.html |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 166 | |