sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3 | ---------------------------------------------------------------- |
| 4 | |
| 5 | Notice that the following BSD-style license applies to this one |
| 6 | file (valgrind.h) only. The entire rest of Valgrind is licensed |
| 7 | under the terms of the GNU General Public License, version 2. See |
| 8 | the COPYING file in the source distribution for details. |
| 9 | |
| 10 | ---------------------------------------------------------------- |
| 11 | |
njn | c953984 | 2002-10-02 13:26:35 +0000 | [diff] [blame] | 12 | This file is part of Valgrind, an extensible x86 protected-mode |
| 13 | emulator for monitoring program execution on x86-Unixes. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 14 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 15 | Copyright (C) 2000-2002 Julian Seward. All rights reserved. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 16 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 17 | Redistribution and use in source and binary forms, with or without |
| 18 | modification, are permitted provided that the following conditions |
| 19 | are met: |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 20 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 21 | 1. Redistributions of source code must retain the above copyright |
| 22 | notice, this list of conditions and the following disclaimer. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 23 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 24 | 2. The origin of this software must not be misrepresented; you must |
| 25 | not claim that you wrote the original software. If you use this |
| 26 | software in a product, an acknowledgment in the product |
| 27 | documentation would be appreciated but is not required. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 28 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 29 | 3. Altered source versions must be plainly marked as such, and must |
| 30 | not be misrepresented as being the original software. |
| 31 | |
| 32 | 4. The name of the author may not be used to endorse or promote |
| 33 | products derived from this software without specific prior written |
| 34 | permission. |
| 35 | |
| 36 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS |
| 37 | OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 38 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 39 | ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 40 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 41 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 42 | GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 43 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 44 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 45 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 46 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 47 | |
| 48 | ---------------------------------------------------------------- |
| 49 | |
| 50 | Notice that the above BSD-style license applies to this one file |
| 51 | (valgrind.h) only. The entire rest of Valgrind is licensed under |
| 52 | the terms of the GNU General Public License, version 2. See the |
| 53 | COPYING file in the source distribution for details. |
| 54 | |
| 55 | ---------------------------------------------------------------- |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 56 | */ |
| 57 | |
| 58 | |
| 59 | #ifndef __VALGRIND_H |
| 60 | #define __VALGRIND_H |
| 61 | |
| 62 | |
| 63 | /* This file is for inclusion into client (your!) code. |
| 64 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 65 | You can use these macros to manipulate and query Valgrind's |
| 66 | execution inside your own programs. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 67 | |
| 68 | The resulting executables will still run without Valgrind, just a |
| 69 | little bit more slowly than they otherwise would, but otherwise |
| 70 | unchanged. |
| 71 | |
| 72 | When run on Valgrind with --client-perms=yes, Valgrind observes |
| 73 | these macro calls and takes appropriate action. When run on |
| 74 | Valgrind with --client-perms=no (the default), Valgrind observes |
| 75 | these macro calls but does not take any action as a result. */ |
| 76 | |
| 77 | |
| 78 | |
| 79 | /* This defines the magic code sequence which the JITter spots and |
| 80 | handles magically. Don't look too closely at this; it will rot |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 81 | your brain. Valgrind dumps the result value in %EDX, so we first |
| 82 | copy the default value there, so that it is returned when not |
| 83 | running on Valgrind. Since %EAX points to a block of mem |
| 84 | containing the args, you can pass as many args as you want like |
| 85 | this. Currently this is set up to deal with 4 args since that's |
| 86 | the max that we appear to need (pthread_create). |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 87 | */ |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 88 | #define VALGRIND_MAGIC_SEQUENCE( \ |
| 89 | _zzq_rlval, /* result lvalue */ \ |
| 90 | _zzq_default, /* result returned when running on real CPU */ \ |
| 91 | _zzq_request, /* request code */ \ |
| 92 | _zzq_arg1, /* request first param */ \ |
| 93 | _zzq_arg2, /* request second param */ \ |
| 94 | _zzq_arg3, /* request third param */ \ |
| 95 | _zzq_arg4 /* request fourth param */ ) \ |
| 96 | \ |
| 97 | { volatile unsigned int _zzq_args[5]; \ |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 98 | _zzq_args[0] = (volatile unsigned int)(_zzq_request); \ |
| 99 | _zzq_args[1] = (volatile unsigned int)(_zzq_arg1); \ |
| 100 | _zzq_args[2] = (volatile unsigned int)(_zzq_arg2); \ |
| 101 | _zzq_args[3] = (volatile unsigned int)(_zzq_arg3); \ |
| 102 | _zzq_args[4] = (volatile unsigned int)(_zzq_arg4); \ |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 103 | asm volatile("movl %1, %%eax\n\t" \ |
| 104 | "movl %2, %%edx\n\t" \ |
| 105 | "roll $29, %%eax ; roll $3, %%eax\n\t" \ |
| 106 | "rorl $27, %%eax ; rorl $5, %%eax\n\t" \ |
| 107 | "roll $13, %%eax ; roll $19, %%eax\n\t" \ |
| 108 | "movl %%edx, %0\t" \ |
| 109 | : "=r" (_zzq_rlval) \ |
| 110 | : "r" (&_zzq_args[0]), "r" (_zzq_default) \ |
| 111 | : "eax", "edx", "cc", "memory" \ |
| 112 | ); \ |
| 113 | } |
| 114 | |
| 115 | |
| 116 | /* Some request codes. There are many more of these, but most are not |
| 117 | exposed to end-user view. These are the public ones, all of the |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 118 | form 0x1000 + small_number. |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 119 | */ |
| 120 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 121 | typedef |
| 122 | enum { VG_USERREQ__RUNNING_ON_VALGRIND = 0x1001, |
| 123 | VG_USERREQ__DISCARD_TRANSLATIONS, |
| 124 | VG_USERREQ__FINAL_DUMMY_CLIENT_REQUEST, |
| 125 | } Vg_ClientRequest; |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 126 | |
| 127 | |
| 128 | /* Returns 1 if running on Valgrind, 0 if running on the real CPU. |
| 129 | Currently implemented but untested. */ |
| 130 | #define RUNNING_ON_VALGRIND \ |
| 131 | ({unsigned int _qzz_res; \ |
| 132 | VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0 /* returned if not */, \ |
| 133 | VG_USERREQ__RUNNING_ON_VALGRIND, \ |
| 134 | 0, 0, 0, 0); \ |
| 135 | _qzz_res; \ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 136 | }) |
| 137 | |
| 138 | |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 139 | /* Discard translation of code in the range [_qzz_addr .. _qzz_addr + |
| 140 | _qzz_len - 1]. Useful if you are debugging a JITter or some such, |
| 141 | since it provides a way to make sure valgrind will retranslate the |
| 142 | invalidated area. Returns no value. */ |
| 143 | #define VALGRIND_DISCARD_TRANSLATIONS(_qzz_addr,_qzz_len) \ |
| 144 | {unsigned int _qzz_res; \ |
| 145 | VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \ |
| 146 | VG_USERREQ__DISCARD_TRANSLATIONS, \ |
| 147 | _qzz_addr, _qzz_len, 0, 0); \ |
| 148 | } |
| 149 | |
| 150 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 151 | #endif |