blob: 1bcd02a813e4bbf876ce6890bb7247a6a105441d [file] [log] [blame]
sewardjde4a1d02002-03-22 01:27:54 +00001<html>
2 <head>
3 <style type="text/css">
4 body { background-color: #ffffff;
5 color: #000000;
6 font-family: Times, Helvetica, Arial;
7 font-size: 14pt}
8 h4 { margin-bottom: 0.3em}
9 code { color: #000000;
10 font-family: Courier;
11 font-size: 13pt }
12 pre { color: #000000;
13 font-family: Courier;
14 font-size: 13pt }
15 a:link { color: #0000C0;
16 text-decoration: none; }
17 a:visited { color: #0000C0;
18 text-decoration: none; }
19 a:active { color: #0000C0;
20 text-decoration: none; }
21 </style>
22 </head>
23
24<body bgcolor="#ffffff">
25
26<a name="title">&nbsp;</a>
27<h1 align=center>Valgrind, snapshot 20020317</h1>
28
29<center>
30<a href="mailto:jseward@acm.org">jseward@acm.org<br>
31<a href="http://www.muraroa.demon.co.uk">http://www.muraroa.demon.co.uk</a><br>
32Copyright &copy; 2000-2002 Julian Seward
33<p>
34Valgrind is licensed under the GNU General Public License,
35version 2<br>
36An open-source tool for finding memory-management problems in
37Linux-x86 executables.
38</center>
39
40<p>
41
42<hr width="100%">
43<a name="contents"></a>
44<h2>Contents of this manual</h2>
45
46<h4>1&nbsp; <a href="#intro">Introduction</a></h4>
47 1.1&nbsp; <a href="#whatfor">What Valgrind is for</a><br>
48 1.2&nbsp; <a href="#whatdoes">What it does with your program</a>
49
50<h4>2&nbsp; <a href="#howtouse">How to use it, and how to make sense
51 of the results</a></h4>
52 2.1&nbsp; <a href="#starta">Getting started</a><br>
53 2.2&nbsp; <a href="#comment">The commentary</a><br>
54 2.3&nbsp; <a href="#report">Reporting of errors</a><br>
55 2.4&nbsp; <a href="#suppress">Suppressing errors</a><br>
56 2.5&nbsp; <a href="#flags">Command-line flags</a><br>
57 2.6&nbsp; <a href="#errormsgs">Explaination of error messages</a><br>
58 2.7&nbsp; <a href="#suppfiles">Writing suppressions files</a><br>
59 2.8&nbsp; <a href="#install">Building and installing</a><br>
60 2.9&nbsp; <a href="#problems">If you have problems</a><br>
61
62<h4>3&nbsp; <a href="#machine">Details of the checking machinery</a></h4>
63 3.1&nbsp; <a href="#vvalue">Valid-value (V) bits</a><br>
64 3.2&nbsp; <a href="#vaddress">Valid-address (A)&nbsp;bits</a><br>
65 3.3&nbsp; <a href="#together">Putting it all together</a><br>
66 3.4&nbsp; <a href="#signals">Signals</a><br>
67 3.5&nbsp; <a href="#leaks">Memory leak detection</a><br>
68
69<h4>4&nbsp; <a href="#limits">Limitations</a></h4>
70
71<h4>5&nbsp; <a href="#howitworks">How it works -- a rough overview</a></h4>
72 5.1&nbsp; <a href="#startb">Getting started</a><br>
73 5.2&nbsp; <a href="#engine">The translation/instrumentation engine</a><br>
74 5.3&nbsp; <a href="#track">Tracking the status of memory</a><br>
75 5.4&nbsp; <a href="#sys_calls">System calls</a><br>
76 5.5&nbsp; <a href="#sys_signals">Signals</a><br>
77
78<h4>6&nbsp; <a href="#example">An example</a></h4>
79
80<h4>7&nbsp; <a href="techdocs.html">The design and implementation of Valgrind</a></h4>
81
82<hr width="100%">
83
84<a name="intro"></a>
85<h2>1&nbsp; Introduction</h2>
86
87<a name="whatfor"></a>
88<h3>1.1&nbsp; What Valgrind is for</h3>
89
90Valgrind is a tool to help you find memory-management problems in your
91programs. When a program is run under Valgrind's supervision, all
92reads and writes of memory are checked, and calls to
93malloc/new/free/delete are intercepted. As a result, Valgrind can
94detect problems such as:
95<ul>
96 <li>Use of uninitialised memory</li>
97 <li>Reading/writing memory after it has been free'd</li>
98 <li>Reading/writing off the end of malloc'd blocks</li>
99 <li>Reading/writing inappropriate areas on the stack</li>
100 <li>Memory leaks -- where pointers to malloc'd blocks are lost forever</li>
101</ul>
102
103Problems like these can be difficult to find by other means, often
104lying undetected for long periods, then causing occasional,
105difficult-to-diagnose crashes.
106
107<p>
108Valgrind is closely tied to details of the CPU, operating system and
109to a less extent, compiler and basic C libraries. This makes it
110difficult to make it portable, so I have chosen at the outset to
111concentrate on what I believe to be a widely used platform: Red Hat
112Linux 7.2, on x86s. I believe that it will work without significant
113difficulty on other x86 GNU/Linux systems which use the 2.4 kernel and
114GNU libc 2.2.X, for example SuSE 7.1 and Mandrake 8.0. Red Hat 6.2 is
115also supported. It has worked in the past, and probably still does,
116on RedHat 7.1 and 6.2. Note that I haven't compiled it on RedHat 7.1
117and 6.2 for a while, so they may no longer work now.
118<p>
119(Early Feb 02: after feedback from the KDE people it also works better
120on other Linuxes).
121<p>
122At some point in the past, Valgrind has also worked on Red Hat 6.2
123(x86), thanks to the efforts of Rob Noble.
124
125<p>
126Valgrind is licensed under the GNU General Public License, version
1272. Read the file LICENSE in the source distribution for details.
128
129<a name="whatdoes">
130<h3>1.2&nbsp; What it does with your program</h3>
131
132Valgrind is designed to be as non-intrusive as possible. It works
133directly with existing executables. You don't need to recompile,
134relink, or otherwise modify, the program to be checked. Simply place
135the word <code>valgrind</code> at the start of the command line
136normally used to run the program. So, for example, if you want to run
137the command <code>ls -l</code> on Valgrind, simply issue the
138command: <code>valgrind ls -l</code>.
139
140<p>Valgrind takes control of your program before it starts. Debugging
141information is read from the executable and associated libraries, so
142that error messages can be phrased in terms of source code
143locations. Your program is then run on a synthetic x86 CPU which
144checks every memory access. All detected errors are written to a
145log. When the program finishes, Valgrind searches for and reports on
146leaked memory.
147
148<p>You can run pretty much any dynamically linked ELF x86 executable using
149Valgrind. Programs run 25 to 50 times slower, and take a lot more
150memory, than they usually would. It works well enough to run large
151programs. For example, the Konqueror web browser from the KDE Desktop
152Environment, version 2.1.1, runs slowly but usably on Valgrind.
153
154<p>Valgrind simulates every single instruction your program executes.
155Because of this, it finds errors not only in your application but also
156in all supporting dynamically-linked (.so-format) libraries, including
157the GNU C library, the X client libraries, Qt, if you work with KDE, and
158so on. That often includes libraries, for example the GNU C library,
159which contain memory access violations, but which you cannot or do not
160want to fix.
161
162<p>Rather than swamping you with errors in which you are not
163interested, Valgrind allows you to selectively suppress errors, by
164recording them in a suppressions file which is read when Valgrind
165starts up. As supplied, Valgrind comes with a suppressions file
166designed to give reasonable behaviour on Red Hat 7.2 (also 7.1 and
1676.2) when running text-only and simple X applications.
168
169<p><a href="#example">Section 6</a> shows an example of use.
170<p>
171<hr width="100%">
172
173<a name="howtouse"></a>
174<h2>2&nbsp; How to use it, and how to make sense of the results</h2>
175
176<a name="starta"></a>
177<h3>2.1&nbsp; Getting started</h3>
178
179First off, consider whether it might be beneficial to recompile your
180application and supporting libraries with optimisation disabled and
181debugging info enabled (the <code>-g</code> flag). You don't have to
182do this, but doing so helps Valgrind produce more accurate and less
183confusing error reports. Chances are you're set up like this already,
184if you intended to debug your program with GNU gdb, or some other
185debugger.
186
187<p>Then just run your application, but place the word
188<code>valgrind</code> in front of your usual command-line invokation.
189Note that you should run the real (machine-code) executable here. If
190your application is started by, for example, a shell or perl script,
191you'll need to modify it to invoke Valgrind on the real executables.
192Running such scripts directly under Valgrind will result in you
193getting error reports pertaining to <code>/bin/sh</code>,
194<code>/usr/bin/perl</code>, or whatever interpreter you're using.
195This almost certainly isn't what you want and can be hugely confusing.
196
197<a name="comment"></a>
198<h3>2.2&nbsp; The commentary</h3>
199
200Valgrind writes a commentary, detailing error reports and other
201significant events. The commentary goes to standard output by
202default. This may interfere with your program, so you can ask for it
203to be directed elsewhere.
204
205<p>All lines in the commentary are of the following form:<br>
206<pre>
207 ==12345== some-message-from-Valgrind
208</pre>
209<p>The <code>12345</code> is the process ID. This scheme makes it easy
210to distinguish program output from Valgrind commentary, and also easy
211to differentiate commentaries from different processes which have
212become merged together, for whatever reason.
213
214<p>By default, Valgrind writes only essential messages to the commentary,
215so as to avoid flooding you with information of secondary importance.
216If you want more information about what is happening, re-run, passing
217the <code>-v</code> flag to Valgrind.
218
219
220<a name="report"></a>
221<h3>2.3&nbsp; Reporting of errors</h3>
222
223When Valgrind detects something bad happening in the program, an error
224message is written to the commentary. For example:<br>
225<pre>
226 ==25832== Invalid read of size 4
227 ==25832== at 0x8048724: BandMatrix::ReSize(int, int, int) (bogon.cpp:45)
228 ==25832== by 0x80487AF: main (bogon.cpp:66)
229 ==25832== by 0x40371E5E: __libc_start_main (libc-start.c:129)
230 ==25832== by 0x80485D1: (within /home/sewardj/newmat10/bogon)
231 ==25832== Address 0xBFFFF74C is not stack'd, malloc'd or free'd
232</pre>
233
234<p>This message says that the program did an illegal 4-byte read of
235address 0xBFFFF74C, which, as far as it can tell, is not a valid stack
236address, nor corresponds to any currently malloc'd or free'd blocks.
237The read is happening at line 45 of <code>bogon.cpp</code>, called
238from line 66 of the same file, etc. For errors associated with an
239identified malloc'd/free'd block, for example reading free'd memory,
240Valgrind reports not only the location where the error happened, but
241also where the associated block was malloc'd/free'd.
242
243<p>Valgrind remembers all error reports. When an error is detected,
244it is compared against old reports, to see if it is a duplicate. If
245so, the error is noted, but no further commentary is emitted. This
246avoids you being swamped with bazillions of duplicate error reports.
247
248<p>If you want to know how many times each error occurred, run with
249the <code>-v</code> option. When execution finishes, all the reports
250are printed out, along with, and sorted by, their occurrence counts.
251This makes it easy to see which errors have occurred most frequently.
252
253<p>Errors are reported before the associated operation actually
254happens. For example, if you program decides to read from address
255zero, Valgrind will emit a message to this effect, and the program
256will then duly die with a segmentation fault.
257
258<p>In general, you should try and fix errors in the order that they
259are reported. Not doing so can be confusing. For example, a program
260which copies uninitialised values to several memory locations, and
261later uses them, will generate several error messages. The first such
262error message may well give the most direct clue to the root cause of
263the problem.
264
265<a name="suppress"></a>
266<h3>2.4&nbsp; Suppressing errors</h3>
267
268Valgrind detects numerous problems in the base libraries, such as the
269GNU C library, and the XFree86 client libraries, which come
270pre-installed on your GNU/Linux system. You can't easily fix these,
271but you don't want to see these errors (and yes, there are many!) So
272Valgrind reads a list of errors to suppress at startup. By default
273this file is <code>redhat72.supp</code>, located in the Valgrind
274installation directory.
275
276<p>You can modify and add to the suppressions file at your leisure, or
277write your own. Multiple suppression files are allowed. This is
278useful if part of your project contains errors you can't or don't want
279to fix, yet you don't want to continuously be reminded of them.
280
281<p>Each error to be suppressed is described very specifically, to
282minimise the possibility that a suppression-directive inadvertantly
283suppresses a bunch of similar errors which you did want to see. The
284suppression mechanism is designed to allow precise yet flexible
285specification of errors to suppress.
286
287<p>If you use the <code>-v</code> flag, at the end of execution, Valgrind
288prints out one line for each used suppression, giving its name and the
289number of times it got used. Here's the suppressions used by a run of
290<code>ls -l</code>:
291<pre>
292 --27579-- supp: 1 socketcall.connect(serv_addr)/__libc_connect/__nscd_getgrgid_r
293 --27579-- supp: 1 socketcall.connect(serv_addr)/__libc_connect/__nscd_getpwuid_r
294 --27579-- supp: 6 strrchr/_dl_map_object_from_fd/_dl_map_object
295</pre>
296
297<a name="flags"></a>
298<h3>2.5&nbsp; Command-line flags</h3>
299
300You invoke Valgrind like this:
301<pre>
302 valgrind [options-for-Valgrind] your-prog [options for your-prog]
303</pre>
304
305<p>Valgrind's default settings succeed in giving reasonable behaviour
306in most cases. Available options, in no particular order, are as
307follows:
308<ul>
309 <li><code>--help</code></li><br>
310
311 <li><code>--version</code><br>
312 <p>The usual deal.</li><br><p>
313
314 <li><code>-v --verbose</code><br>
315 <p>Be more verbose. Gives extra information on various aspects
316 of your program, such as: the shared objects loaded, the
317 suppressions used, the progress of the instrumentation engine,
318 and warnings about unusual behaviour.
319 </li><br><p>
320
321 <li><code>-q --quiet</code><br>
322 <p>Run silently, and only print error messages. Useful if you
323 are running regression tests or have some other automated test
324 machinery.
325 </li><br><p>
326
327 <li><code>--demangle=no</code><br>
328 <code>--demangle=yes</code> [the default]
329 <p>Disable/enable automatic demangling (decoding) of C++ names.
330 Enabled by default. When enabled, Valgrind will attempt to
331 translate encoded C++ procedure names back to something
332 approaching the original. The demangler handles symbols mangled
333 by g++ versions 2.X and 3.X.
334
335 <p>An important fact about demangling is that function
336 names mentioned in suppressions files should be in their mangled
337 form. Valgrind does not demangle function names when searching
338 for applicable suppressions, because to do otherwise would make
339 suppressions file contents dependent on the state of Valgrind's
340 demangling machinery, and would also be slow and pointless.
341 </li><br><p>
342
343 <li><code>--num-callers=&lt;number&gt;</code> [default=4]<br>
344 <p>By default, Valgrind shows four levels of function call names
345 to help you identify program locations. You can change that
346 number with this option. This can help in determining the
347 program's location in deeply-nested call chains. Note that errors
348 are commoned up using only the top three function locations (the
349 place in the current function, and that of its two immediate
350 callers). So this doesn't affect the total number of errors
351 reported.
352 <p>
353 The maximum value for this is 50. Note that higher settings
354 will make Valgrind run a bit more slowly and take a bit more
355 memory, but can be useful when working with programs with
356 deeply-nested call chains.
357 </li><br><p>
358
359 <li><code>--gdb-attach=no</code> [the default]<br>
360 <code>--gdb-attach=yes</code>
361 <p>When enabled, Valgrind will pause after every error shown,
362 and print the line
363 <br>
364 <code>---- Attach to GDB ? --- [Return/N/n/Y/y/C/c] ----</code>
365 <p>
366 Pressing <code>Ret</code>, or <code>N</code> <code>Ret</code>
367 or <code>n</code> <code>Ret</code>, causes Valgrind not to
368 start GDB for this error.
369 <p>
370 <code>Y</code> <code>Ret</code>
371 or <code>y</code> <code>Ret</code> causes Valgrind to
372 start GDB, for the program at this point. When you have
373 finished with GDB, quit from it, and the program will continue.
374 Trying to continue from inside GDB doesn't work.
375 <p>
376 <code>C</code> <code>Ret</code>
377 or <code>c</code> <code>Ret</code> causes Valgrind not to
378 start GDB, and not to ask again.
379 <p>
380 <code>--gdb-attach=yes</code> conflicts with
381 <code>--trace-children=yes</code>. You can't use them
382 together. Valgrind refuses to start up in this situation.
383 </li><br><p>
384
385 <li><code>--partial-loads-ok=yes</code> [the default]<br>
386 <code>--partial-loads-ok=no</code>
387 <p>Controls how Valgrind handles word (4-byte) loads from
388 addresses for which some bytes are addressible and others
389 are not. When <code>yes</code> (the default), such loads
390 do not elicit an address error. Instead, the loaded V bytes
391 corresponding to the illegal addresses indicate undefined, and
392 those corresponding to legal addresses are loaded from shadow
393 memory, as usual.
394 <p>
395 When <code>no</code>, loads from partially
396 invalid addresses are treated the same as loads from completely
397 invalid addresses: an illegal-address error is issued,
398 and the resulting V bytes indicate valid data.
399 </li><br><p>
400
401 <li><code>--sloppy-malloc=no</code> [the default]<br>
402 <code>--sloppy-malloc=yes</code>
403 <p>When enabled, all requests for malloc/calloc are rounded up
404 to a whole number of machine words -- in other words, made
405 divisible by 4. For example, a request for 17 bytes of space
406 would result in a 20-byte area being made available. This works
407 around bugs in sloppy libraries which assume that they can
408 safely rely on malloc/calloc requests being rounded up in this
409 fashion. Without the workaround, these libraries tend to
410 generate large numbers of errors when they access the ends of
411 these areas. Valgrind snapshots dated 17 Feb 2002 and later are
412 cleverer about this problem, and you should no longer need to
413 use this flag.
414 </li><br><p>
415
416 <li><code>--trace-children=no</code> [the default]</br>
417 <code>--trace-children=yes</code>
418 <p>When enabled, Valgrind will trace into child processes. This
419 is confusing and usually not what you want, so is disabled by
420 default.</li><br><p>
421
422 <li><code>--freelist-vol=&lt;number></code> [default: 1000000]
423 <p>When the client program releases memory using free (in C) or
424 delete (C++), that memory is not immediately made available for
425 re-allocation. Instead it is marked inaccessible and placed in
426 a queue of freed blocks. The purpose is to delay the point at
427 which freed-up memory comes back into circulation. This
428 increases the chance that Valgrind will be able to detect
429 invalid accesses to blocks for some significant period of time
430 after they have been freed.
431 <p>
432 This flag specifies the maximum total size, in bytes, of the
433 blocks in the queue. The default value is one million bytes.
434 Increasing this increases the total amount of memory used by
435 Valgrind but may detect invalid uses of freed blocks which would
436 otherwise go undetected.</li><br><p>
437
438 <li><code>--logfile-fd=&lt;number></code> [default: 2, stderr]
439 <p>Specifies the file descriptor on which Valgrind communicates
440 all of its messages. The default, 2, is the standard error
441 channel. This may interfere with the client's own use of
442 stderr. To dump Valgrind's commentary in a file without using
443 stderr, something like the following works well (sh/bash
444 syntax):<br>
445 <code>&nbsp;&nbsp;
446 valgrind --logfile-fd=9 my_prog 9> logfile</code><br>
447 That is: tell Valgrind to send all output to file descriptor 9,
448 and ask the shell to route file descriptor 9 to "logfile".
449 </li><br><p>
450
451 <li><code>--suppressions=&lt;filename></code> [default:
452 /installation/directory/redhat72.supp] <p>Specifies an extra
453 file from which to read descriptions of errors to suppress. You
454 may use as many extra suppressions files as you
455 like.</li><br><p>
456
457 <li><code>--leak-check=no</code> [default]<br>
458 <code>--leak-check=yes</code>
459 <p>When enabled, search for memory leaks when the client program
460 finishes. A memory leak means a malloc'd block, which has not
461 yet been free'd, but to which no pointer can be found. Such a
462 block can never be free'd by the program, since no pointer to it
463 exists. Leak checking is disabled by default
464 because it tends to generate dozens of error messages.
465 </li><br><p>
466
467 <li><code>--show-reachable=no</code> [default]<br>
468 <code>--show-reachable=yes</code> <p>When disabled, the memory
469 leak detector only shows blocks for which it cannot find a
470 pointer to at all, or it can only find a pointer to the middle
471 of. These blocks are prime candidates for memory leaks. When
472 enabled, the leak detector also reports on blocks which it could
473 find a pointer to. Your program could, at least in principle,
474 have freed such blocks before exit. Contrast this to blocks for
475 which no pointer, or only an interior pointer could be found:
476 they are more likely to indicate memory leaks, because
477 you do not actually have a pointer to the start of the block
478 which you can hand to free(), even if you wanted to.
479 </li><br><p>
480
481 <li><code>--leak-resolution=low</code> [default]<br>
482 <code>--leak-resolution=med</code> <br>
483 <code>--leak-resolution=high</code>
484 <p>When doing leak checking, determines how willing Valgrind is
485 to consider different backtraces the same. When set to
486 <code>low</code>, the default, only the first two entries need
487 match. When <code>med</code>, four entries have to match. When
488 <code>high</code>, all entries need to match.
489 <p>
490 For hardcore leak debugging, you probably want to use
491 <code>--leak-resolution=high</code> together with
492 <code>--num-callers=40</code> or some such large number. Note
493 however that this can give an overwhelming amount of
494 information, which is why the defaults are 4 callers and
495 low-resolution matching.
496 <p>
497 Note that the <code>--leak-resolution=</code> setting does not
498 affect Valgrind's ability to find leaks. It only changes how
499 the results are presented to you.
500 </li><br><p>
501
502 <li><code>--workaround-gcc296-bugs=no</code> [default]<br>
503 <code>--workaround-gcc296-bugs=yes</code> <p>When enabled,
504 assume that reads and writes some small distance below the stack
505 pointer <code>%esp</code> are due to bugs in gcc 2.96, and does
506 not report them. The "small distance" is 256 bytes by default.
507 Note that gcc 2.96 is the default compiler on some popular Linux
508 distributions (RedHat 7.X, Mandrake) and so you may well need to
509 use this flag. Do not use it if you do not have to, as it can
510 cause real errors to be overlooked. A better option is to use a
511 gcc/g++ which works properly; 2.95.3 seems to be a good choice.
512 <p>
513 Unfortunately (27 Feb 02) it looks like g++ 3.0.4 is similarly
514 buggy, so you may need to issue this flag if you use 3.0.4.
515 </li><br><p>
516
517 <li><code>--client-perms=no</code> [default]<br>
518 <code>--client-perms=yes</code> <p>An experimental feature.
519 <p>
520 When enabled, and when <code>--instrument=yes</code> (which is
521 the default), Valgrind honours client directives to set and
522 query address range permissions. This allows the client program
523 to tell Valgrind about changes in memory range permissions that
524 Valgrind would not otherwise know about, and so allows clients
525 to get Valgrind to do arbitrary custom checks.
526 <p>
527 Clients need to include the header file <code>valgrind.h</code>
528 to make this work. The macros therein have the magical property
529 that they generate code in-line which Valgrind can spot.
530 However, the code does nothing when not run on Valgrind, so you
531 are not forced to run your program on Valgrind just because you
532 use the macros in this file.
533 <p>
534 A brief description of the available macros:
535 <ul>
536 <li><code>VALGRIND_MAKE_NOACCESS</code>,
537 <code>VALGRIND_MAKE_WRITABLE</code> and
538 <code>VALGRIND_MAKE_READABLE</code>. These mark address
539 ranges as completely inaccessible, accessible but containing
540 undefined data, and accessible and containing defined data,
541 respectively. Subsequent errors may have their faulting
542 addresses described in terms of these blocks. Returns a
543 "block handle".
544 <p>
545 <li><code>VALGRIND_DISCARD</code>: At some point you may want
546 Valgrind to stop reporting errors in terms of the blocks
547 defined by the previous three macros. To do this, the above
548 macros return a small-integer "block handle". You can pass
549 this block handle to <code>VALGRIND_DISCARD</code>. After
550 doing so, Valgrind will no longer be able to relate
551 addressing errors to the user-defined block associated with
552 the handle. The permissions settings associated with the
553 handle remain in place; this just affects how errors are
554 reported, not whether they are reported. Returns 1 for an
555 invalid handle and 0 for a valid handle (although passing
556 invalid handles is harmless).
557 <p>
558 <li><code>VALGRIND_CHECK_NOACCESS</code>,
559 <code>VALGRIND_CHECK_WRITABLE</code> and
560 <code>VALGRIND_CHECK_READABLE</code>: check immediately
561 whether or not the given address range has the relevant
562 property, and if not, print an error message. Also, for the
563 convenience of the client, returns zero if the relevant
564 property holds; otherwise, the returned value is the address
565 of the first byte for which the property is not true.
566 <p>
567 <li><code>VALGRIND_CHECK_NOACCESS</code>: a quick and easy way
568 to find out whether Valgrind thinks a particular variable
569 (lvalue, to be precise) is addressible and defined. Prints
570 an error message if not. Returns no value.
571 <p>
572 <li><code>VALGRIND_MAKE_NOACCESS_STACK</code>: a highly
573 experimental feature. Similarly to
574 <code>VALGRIND_MAKE_NOACCESS</code>, this marks an address
575 range as inaccessible, so that subsequent accesses to an
576 address in the range gives an error. However, this macro
577 does not return a block handle. Instead, all annotations
578 created like this are reviewed at each client
579 <code>ret</code> (subroutine return) instruction, and those
580 which now define an address range block the client's stack
581 pointer register (<code>%esp</code>) are automatically
582 deleted.
583 <p>
584 In other words, this macro allows the client to tell
585 Valgrind about red-zones on its own stack. Valgrind
586 automatically discards this information when the stack
587 retreats past such blocks. Beware: hacky and flaky.
588 </ul>
589 </li>
590 <p>
591 As of 17 March 02 (the time of writing this), there is a small
592 problem with all of these macros, which is that I haven't
593 figured out how to make them produce sensible (always-succeeds)
594 return values when the client is run on the real CPU or on
595 Valgrind without <code>--client-perms=yes</code>. So if you
596 write client code which depends on the return values, be aware
597 that it may misbehave when not run with full Valgrindification.
598 If you always ignore the return values you should always be
599 safe. I plan to fix this.
600</ul>
601
602There are also some options for debugging Valgrind itself. You
603shouldn't need to use them in the normal run of things. Nevertheless:
604
605<ul>
606
607 <li><code>--single-step=no</code> [default]<br>
608 <code>--single-step=yes</code>
609 <p>When enabled, each x86 insn is translated seperately into
610 instrumented code. When disabled, translation is done on a
611 per-basic-block basis, giving much better translations.</li><br>
612 <p>
613
614 <li><code>--optimise=no</code><br>
615 <code>--optimise=yes</code> [default]
616 <p>When enabled, various improvements are applied to the
617 intermediate code, mainly aimed at allowing the simulated CPU's
618 registers to be cached in the real CPU's registers over several
619 simulated instructions.</li><br>
620 <p>
621
622 <li><code>--instrument=no</code><br>
623 <code>--instrument=yes</code> [default]
624 <p>When disabled, the translations don't actually contain any
625 instrumentation.</li><br>
626 <p>
627
628 <li><code>--cleanup=no</code><br>
629 <code>--cleanup=yes</code> [default]
630 <p>When enabled, various improvments are applied to the
631 post-instrumented intermediate code, aimed at removing redundant
632 value checks.</li><br>
633 <p>
634
635 <li><code>--trace-syscalls=no</code> [default]<br>
636 <code>--trace-syscalls=yes</code>
637 <p>Enable/disable tracing of system call intercepts.</li><br>
638 <p>
639
640 <li><code>--trace-signals=no</code> [default]<br>
641 <code>--trace-signals=yes</code>
642 <p>Enable/disable tracing of signal handling.</li><br>
643 <p>
644
645 <li><code>--trace-symtab=no</code> [default]<br>
646 <code>--trace-symtab=yes</code>
647 <p>Enable/disable tracing of symbol table reading.</li><br>
648 <p>
649
650 <li><code>--trace-malloc=no</code> [default]<br>
651 <code>--trace-malloc=yes</code>
652 <p>Enable/disable tracing of malloc/free (et al) intercepts.
653 </li><br>
654 <p>
655
656 <li><code>--stop-after=&lt;number></code>
657 [default: infinity, more or less]
658 <p>After &lt;number> basic blocks have been executed, shut down
659 Valgrind and switch back to running the client on the real CPU.
660 </li><br>
661 <p>
662
663 <li><code>--dump-error=&lt;number></code>
664 [default: inactive]
665 <p>After the program has exited, show gory details of the
666 translation of the basic block containing the &lt;number>'th
667 error context. When used with <code>--single-step=yes</code>,
668 can show the
669 exact x86 instruction causing an error.</li><br>
670 <p>
671
672 <li><code>--smc-check=none</code><br>
673 <code>--smc-check=some</code> [default]<br>
674 <code>--smc-check=all</code>
675 <p>How carefully should Valgrind check for self-modifying code
676 writes, so that translations can be discarded?&nbsp; When
677 "none", no writes are checked. When "some", only writes
678 resulting from moves from integer registers to memory are
679 checked. When "all", all memory writes are checked, even those
680 with which are no sane program would generate code -- for
681 example, floating-point writes.</li>
682</ul>
683
684
685<a name="errormsgs">
686<h3>2.6&nbsp; Explaination of error messages</h3>
687
688Despite considerable sophistication under the hood, Valgrind can only
689really detect two kinds of errors, use of illegal addresses, and use
690of undefined values. Nevertheless, this is enough to help you
691discover all sorts of memory-management nasties in your code. This
692section presents a quick summary of what error messages mean. The
693precise behaviour of the error-checking machinery is described in
694<a href="#machine">Section 4</a>.
695
696
697<h4>2.6.1&nbsp; Illegal read / Illegal write errors</h4>
698For example:
699<pre>
700 ==30975== Invalid read of size 4
701 ==30975== at 0x40F6BBCC: (within /usr/lib/libpng.so.2.1.0.9)
702 ==30975== by 0x40F6B804: (within /usr/lib/libpng.so.2.1.0.9)
703 ==30975== by 0x40B07FF4: read_png_image__FP8QImageIO (kernel/qpngio.cpp:326)
704 ==30975== by 0x40AC751B: QImageIO::read() (kernel/qimage.cpp:3621)
705 ==30975== Address 0xBFFFF0E0 is not stack'd, malloc'd or free'd
706</pre>
707
708<p>This happens when your program reads or writes memory at a place
709which Valgrind reckons it shouldn't. In this example, the program did
710a 4-byte read at address 0xBFFFF0E0, somewhere within the
711system-supplied library libpng.so.2.1.0.9, which was called from
712somewhere else in the same library, called from line 326 of
713qpngio.cpp, and so on.
714
715<p>Valgrind tries to establish what the illegal address might relate
716to, since that's often useful. So, if it points into a block of
717memory which has already been freed, you'll be informed of this, and
718also where the block was free'd at.. Likewise, if it should turn out
719to be just off the end of a malloc'd block, a common result of
720off-by-one-errors in array subscripting, you'll be informed of this
721fact, and also where the block was malloc'd.
722
723<p>In this example, Valgrind can't identify the address. Actually the
724address is on the stack, but, for some reason, this is not a valid
725stack address -- it is below the stack pointer, %esp, and that isn't
726allowed.
727
728<p>Note that Valgrind only tells you that your program is about to
729access memory at an illegal address. It can't stop the access from
730happening. So, if your program makes an access which normally would
731result in a segmentation fault, you program will still suffer the same
732fate -- but you will get a message from Valgrind immediately prior to
733this. In this particular example, reading junk on the stack is
734non-fatal, and the program stays alive.
735
736
737<h4>2.6.2&nbsp; Use of uninitialised values</h4>
738For example:
739<pre>
740 ==19146== Use of uninitialised CPU condition code
741 ==19146== at 0x402DFA94: _IO_vfprintf (_itoa.h:49)
742 ==19146== by 0x402E8476: _IO_printf (printf.c:36)
743 ==19146== by 0x8048472: main (tests/manuel1.c:8)
744 ==19146== by 0x402A6E5E: __libc_start_main (libc-start.c:129)
745</pre>
746
747<p>An uninitialised-value use error is reported when your program uses
748a value which hasn't been initialised -- in other words, is undefined.
749Here, the undefined value is used somewhere inside the printf()
750machinery of the C library. This error was reported when running the
751following small program:
752<pre>
753 int main()
754 {
755 int x;
756 printf ("x = %d\n", x);
757 }
758</pre>
759
760<p>It is important to understand that your program can copy around
761junk (uninitialised) data to its heart's content. Valgrind observes
762this and keeps track of the data, but does not complain. A complaint
763is issued only when your program attempts to make use of uninitialised
764data. In this example, x is uninitialised. Valgrind observes the
765value being passed to _IO_printf and thence to
766_IO_vfprintf, but makes no comment. However,
767_IO_vfprintf has to examine the value of x
768so it can turn it into the corresponding ASCII string, and it is at
769this point that Valgrind complains.
770
771<p>Sources of uninitialised data tend to be:
772<ul>
773 <li>Local variables in procedures which have not been initialised,
774 as in the example above.</li><br><p>
775
776 <li>The contents of malloc'd blocks, before you write something
777 there. In C++, the new operator is a wrapper round malloc, so
778 if you create an object with new, its fields will be
779 uninitialised until you fill them in, which is only Right and
780 Proper.</li>
781</ul>
782
783
784
785<h4>2.6.3&nbsp; Illegal frees</h4>
786For example:
787<pre>
788 ==7593== Invalid free()
789 ==7593== at 0x4004FFDF: free (ut_clientmalloc.c:577)
790 ==7593== by 0x80484C7: main (tests/doublefree.c:10)
791 ==7593== by 0x402A6E5E: __libc_start_main (libc-start.c:129)
792 ==7593== by 0x80483B1: (within tests/doublefree)
793 ==7593== Address 0x3807F7B4 is 0 bytes inside a block of size 177 free'd
794 ==7593== at 0x4004FFDF: free (ut_clientmalloc.c:577)
795 ==7593== by 0x80484C7: main (tests/doublefree.c:10)
796 ==7593== by 0x402A6E5E: __libc_start_main (libc-start.c:129)
797 ==7593== by 0x80483B1: (within tests/doublefree)
798</pre>
799<p>Valgrind keeps track of the blocks allocated by your program with
800malloc/new, so it can know exactly whether or not the argument to
801free/delete is legitimate or not. Here, this test program has
802freed the same block twice. As with the illegal read/write errors,
803Valgrind attempts to make sense of the address free'd. If, as
804here, the address is one which has previously been freed, you wil
805be told that -- making duplicate frees of the same block easy to spot.
806
807
808<h4>2.6.4&nbsp; Passing system call parameters with inadequate
809read/write permissions</h4>
810
811Valgrind checks all parameters to system calls. If a system call
812needs to read from a buffer provided by your program, Valgrind checks
813that the entire buffer is addressible and has valid data, ie, it is
814readable. And if the system call needs to write to a user-supplied
815buffer, Valgrind checks that the buffer is addressible. After the
816system call, Valgrind updates its administrative information to
817precisely reflect any changes in memory permissions caused by the
818system call.
819
820<p>Here's an example of a system call with an invalid parameter:
821<pre>
822 #include &lt;stdlib.h>
823 #include &lt;unistd.h>
824 int main( void )
825 {
826 char* arr = malloc(10);
827 (void) write( 1 /* stdout */, arr, 10 );
828 return 0;
829 }
830</pre>
831
832<p>You get this complaint ...
833<pre>
834 ==8230== Syscall param write(buf) lacks read permissions
835 ==8230== at 0x4035E072: __libc_write
836 ==8230== by 0x402A6E5E: __libc_start_main (libc-start.c:129)
837 ==8230== by 0x80483B1: (within tests/badwrite)
838 ==8230== by &lt;bogus frame pointer> ???
839 ==8230== Address 0x3807E6D0 is 0 bytes inside a block of size 10 alloc'd
840 ==8230== at 0x4004FEE6: malloc (ut_clientmalloc.c:539)
841 ==8230== by 0x80484A0: main (tests/badwrite.c:6)
842 ==8230== by 0x402A6E5E: __libc_start_main (libc-start.c:129)
843 ==8230== by 0x80483B1: (within tests/badwrite)
844</pre>
845
846<p>... because the program has tried to write uninitialised junk from
847the malloc'd block to the standard output.
848
849
850<h4>2.6.5&nbsp; Warning messages you might see</h4>
851
852Most of these only appear if you run in verbose mode (enabled by
853<code>-v</code>):
854<ul>
855<li> <code>More than 50 errors detected. Subsequent errors
856 will still be recorded, but in less detail than before.</code>
857 <br>
858 After 50 different errors have been shown, Valgrind becomes
859 more conservative about collecting them. It then requires only
860 the program counters in the top two stack frames to match when
861 deciding whether or not two errors are really the same one.
862 Prior to this point, the PCs in the top four frames are required
863 to match. This hack has the effect of slowing down the
864 appearance of new errors after the first 50. The 50 constant can
865 be changed by recompiling Valgrind.
866<p>
867<li> <code>More than 500 errors detected. I'm not reporting any more.
868 Final error counts may be inaccurate. Go fix your
869 program!</code>
870 <br>
871 After 500 different errors have been detected, Valgrind ignores
872 any more. It seems unlikely that collecting even more different
873 ones would be of practical help to anybody, and it avoids the
874 danger that Valgrind spends more and more of its time comparing
875 new errors against an ever-growing collection. As above, the 500
876 number is a compile-time constant.
877<p>
878<li> <code>Warning: client exiting by calling exit(&lt;number>).
879 Bye!</code>
880 <br>
881 Your program has called the <code>exit</code> system call, which
882 will immediately terminate the process. You'll get no exit-time
883 error summaries or leak checks. Note that this is not the same
884 as your program calling the ANSI C function <code>exit()</code>
885 -- that causes a normal, controlled shutdown of Valgrind.
886<p>
887<li> <code>Warning: client switching stacks?</code>
888 <br>
889 Valgrind spotted such a large change in the stack pointer, %esp,
890 that it guesses the client is switching to a different stack.
891 At this point it makes a kludgey guess where the base of the new
892 stack is, and sets memory permissions accordingly. You may get
893 many bogus error messages following this, if Valgrind guesses
894 wrong. At the moment "large change" is defined as a change of
895 more that 2000000 in the value of the %esp (stack pointer)
896 register.
897<p>
898<li> <code>Warning: client attempted to close Valgrind's logfile fd &lt;number>
899 </code>
900 <br>
901 Valgrind doesn't allow the client
902 to close the logfile, because you'd never see any diagnostic
903 information after that point. If you see this message,
904 you may want to use the <code>--logfile-fd=&lt;number></code>
905 option to specify a different logfile file-descriptor number.
906<p>
907<li> <code>Warning: noted but unhandled ioctl &lt;number></code>
908 <br>
909 Valgrind observed a call to one of the vast family of
910 <code>ioctl</code> system calls, but did not modify its
911 memory status info (because I have not yet got round to it).
912 The call will still have gone through, but you may get spurious
913 errors after this as a result of the non-update of the memory info.
914<p>
915<li> <code>Warning: unblocking signal &lt;number> due to
916 sigprocmask</code>
917 <br>
918 Really just a diagnostic from the signal simulation machinery.
919 This message will appear if your program handles a signal by
920 first <code>longjmp</code>ing out of the signal handler,
921 and then unblocking the signal with <code>sigprocmask</code>
922 -- a standard signal-handling idiom.
923<p>
924<li> <code>Warning: bad signal number &lt;number> in __NR_sigaction.</code>
925 <br>
926 Probably indicates a bug in the signal simulation machinery.
927<p>
928<li> <code>Warning: set address range perms: large range &lt;number></code>
929 <br>
930 Diagnostic message, mostly for my benefit, to do with memory
931 permissions.
932</ul>
933
934
935<a name="suppfiles"></a>
936<h3>2.7&nbsp; Writing suppressions files</h3>
937
938A suppression file describes a bunch of errors which, for one reason
939or another, you don't want Valgrind to tell you about. Usually the
940reason is that the system libraries are buggy but unfixable, at least
941within the scope of the current debugging session. Multiple
942suppresions files are allowed. By default, Valgrind uses
943<code>linux24.supp</code> in the directory where it is installed.
944
945<p>
946You can ask to add suppressions from another file, by specifying
947<code>--suppressions=/path/to/file.supp</code>.
948
949<p>Each suppression has the following components:<br>
950<ul>
951
952 <li>Its name. This merely gives a handy name to the suppression, by
953 which it is referred to in the summary of used suppressions
954 printed out when a program finishes. It's not important what
955 the name is; any identifying string will do.
956 <p>
957
958 <li>The nature of the error to suppress. Either:
959 <code>Value1</code>,
960 <code>Value2</code>,
961 <code>Value4</code>,
962 <code>Value8</code> or
963 <code>Value0</code>,
964 meaning an uninitialised-value error when
965 using a value of 1, 2, 4 or 8 bytes,
966 or the CPU's condition codes, respectively. Or:
967 <code>Addr1</code>,
968 <code>Addr2</code>,
969 <code>Addr4</code> or
970 <code>Addr8</code>, meaning an invalid address during a
971 memory access of 1, 2, 4 or 8 bytes respectively. Or
972 <code>Param</code>,
973 meaning an invalid system call parameter error. Or
974 <code>Free</code>, meaning an invalid or mismatching free.</li><br>
975 <p>
976
977 <li>The "immediate location" specification. For Value and Addr
978 errors, is either the name of the function in which the error
979 occurred, or, failing that, the full path the the .so file
980 containing the error location. For Param errors, is the name of
981 the offending system call parameter. For Free errors, is the
982 name of the function doing the freeing (eg, <code>free</code>,
983 <code>__builtin_vec_delete</code>, etc)</li><br>
984 <p>
985
986 <li>The caller of the above "immediate location". Again, either a
987 function or shared-object name.</li><br>
988 <p>
989
990 <li>Optionally, one or two extra calling-function or object names,
991 for greater precision.</li>
992</ul>
993
994<p>
995Locations may be either names of shared objects or wildcards matching
996function names. They begin <code>obj:</code> and <code>fun:</code>
997respectively. Function and object names to match against may use the
998wildcard characters <code>*</code> and <code>?</code>.
999
1000A suppression only suppresses an error when the error matches all the
1001details in the suppression. Here's an example:
1002<pre>
1003 {
1004 __gconv_transform_ascii_internal/__mbrtowc/mbtowc
1005 Value4
1006 fun:__gconv_transform_ascii_internal
1007 fun:__mbr*toc
1008 fun:mbtowc
1009 }
1010</pre>
1011
1012<p>What is means is: suppress a use-of-uninitialised-value error, when
1013the data size is 4, when it occurs in the function
1014<code>__gconv_transform_ascii_internal</code>, when that is called
1015from any function of name matching <code>__mbr*toc</code>,
1016when that is called from
1017<code>mbtowc</code>. It doesn't apply under any other circumstances.
1018The string by which this suppression is identified to the user is
1019__gconv_transform_ascii_internal/__mbrtowc/mbtowc.
1020
1021<p>Another example:
1022<pre>
1023 {
1024 libX11.so.6.2/libX11.so.6.2/libXaw.so.7.0
1025 Value4
1026 obj:/usr/X11R6/lib/libX11.so.6.2
1027 obj:/usr/X11R6/lib/libX11.so.6.2
1028 obj:/usr/X11R6/lib/libXaw.so.7.0
1029 }
1030</pre>
1031
1032<p>Suppress any size 4 uninitialised-value error which occurs anywhere
1033in <code>libX11.so.6.2</code>, when called from anywhere in the same
1034library, when called from anywhere in <code>libXaw.so.7.0</code>. The
1035inexact specification of locations is regrettable, but is about all
1036you can hope for, given that the X11 libraries shipped with Red Hat
10377.2 have had their symbol tables removed.
1038
1039<p>Note -- since the above two examples did not make it clear -- that
1040you can freely mix the <code>obj:</code> and <code>fun:</code>
1041styles of description within a single suppression record.
1042
1043
1044<a name="install"></a>
1045<h3>2.8&nbsp; Building and installing</h3>
1046At the moment, very rudimentary.
1047
1048<p>The tarball is set up for a standard Red Hat 7.1 (6.2) machine. To
1049build, just do "make". No configure script, no autoconf, no nothing.
1050
1051<p>The files needed for installation are: valgrind.so, valgring.so,
1052valgrind, VERSION, redhat72.supp (or redhat62.supp). You can copy
1053these to any directory you like. However, you then need to edit the
1054shell script "valgrind". On line 4, set the environment variable
1055<code>VALGRIND</code> to point to the directory you have copied the
1056installation into.
1057
1058
1059<a name="problems"></a>
1060<h3>2.9&nbsp; If you have problems</h3>
1061Mail me (<a href="mailto:jseward@acm.org">jseward@acm.org</a>).
1062
1063<p>See <a href="#limits">Section 4</a> for the known limitations of
1064Valgrind, and for a list of programs which are known not to work on
1065it.
1066
1067<p>The translator/instrumentor has a lot of assertions in it. They
1068are permanently enabled, and I have no plans to disable them. If one
1069of these breaks, please mail me!
1070
1071<p>If you get an assertion failure on the expression
1072<code>chunkSane(ch)</code> in <code>vg_free()</code> in
1073<code>vg_malloc.c</code>, this may have happened because your program
1074wrote off the end of a malloc'd block, or before its beginning.
1075Valgrind should have emitted a proper message to that effect before
1076dying in this way. This is a known problem which I should fix.
1077<p>
1078
1079<hr width="100%">
1080
1081<a name="machine"></a>
1082<h2>3&nbsp; Details of the checking machinery</h2>
1083
1084Read this section if you want to know, in detail, exactly what and how
1085Valgrind is checking.
1086
1087<a name="vvalue"></a>
1088<h3>3.1&nbsp; Valid-value (V) bits</h3>
1089
1090It is simplest to think of Valgrind implementing a synthetic Intel x86
1091CPU which is identical to a real CPU, except for one crucial detail.
1092Every bit (literally) of data processed, stored and handled by the
1093real CPU has, in the synthetic CPU, an associated "valid-value" bit,
1094which says whether or not the accompanying bit has a legitimate value.
1095In the discussions which follow, this bit is referred to as the V
1096(valid-value) bit.
1097
1098<p>Each byte in the system therefore has a 8 V bits which accompanies
1099it wherever it goes. For example, when the CPU loads a word-size item
1100(4 bytes) from memory, it also loads the corresponding 32 V bits from
1101a bitmap which stores the V bits for the process' entire address
1102space. If the CPU should later write the whole or some part of that
1103value to memory at a different address, the relevant V bits will be
1104stored back in the V-bit bitmap.
1105
1106<p>In short, each bit in the system has an associated V bit, which
1107follows it around everywhere, even inside the CPU. Yes, the CPU's
1108(integer) registers have their own V bit vectors.
1109
1110<p>Copying values around does not cause Valgrind to check for, or
1111report on, errors. However, when a value is used in a way which might
1112conceivably affect the outcome of your program's computation, the
1113associated V bits are immediately checked. If any of these indicate
1114that the value is undefined, an error is reported.
1115
1116<p>Here's an (admittedly nonsensical) example:
1117<pre>
1118 int i, j;
1119 int a[10], b[10];
1120 for (i = 0; i &lt; 10; i++) {
1121 j = a[i];
1122 b[i] = j;
1123 }
1124</pre>
1125
1126<p>Valgrind emits no complaints about this, since it merely copies
1127uninitialised values from <code>a[]</code> into <code>b[]</code>, and
1128doesn't use them in any way. However, if the loop is changed to
1129<pre>
1130 for (i = 0; i &lt; 10; i++) {
1131 j += a[i];
1132 }
1133 if (j == 77)
1134 printf("hello there\n");
1135</pre>
1136then Valgrind will complain, at the <code>if</code>, that the
1137condition depends on uninitialised values.
1138
1139<p>Most low level operations, such as adds, cause Valgrind to
1140use the V bits for the operands to calculate the V bits for the
1141result. Even if the result is partially or wholly undefined,
1142it does not complain.
1143
1144<p>Checks on definedness only occur in two places: when a value is
1145used to generate a memory address, and where control flow decision
1146needs to be made. Also, when a system call is detected, valgrind
1147checks definedness of parameters as required.
1148
1149<p>If a check should detect undefinedness, and error message is
1150issued. The resulting value is subsequently regarded as well-defined.
1151To do otherwise would give long chains of error messages. In effect,
1152we say that undefined values are non-infectious.
1153
1154<p>This sounds overcomplicated. Why not just check all reads from
1155memory, and complain if an undefined value is loaded into a CPU register?
1156Well, that doesn't work well, because perfectly legitimate C programs routinely
1157copy uninitialised values around in memory, and we don't want endless complaints
1158about that. Here's the canonical example. Consider a struct
1159like this:
1160<pre>
1161 struct S { int x; char c; };
1162 struct S s1, s2;
1163 s1.x = 42;
1164 s1.c = 'z';
1165 s2 = s1;
1166</pre>
1167
1168<p>The question to ask is: how large is <code>struct S</code>, in
1169bytes? An int is 4 bytes and a char one byte, so perhaps a struct S
1170occupies 5 bytes? Wrong. All (non-toy) compilers I know of will
1171round the size of <code>struct S</code> up to a whole number of words,
1172in this case 8 bytes. Not doing this forces compilers to generate
1173truly appalling code for subscripting arrays of <code>struct
1174S</code>'s.
1175
1176<p>So s1 occupies 8 bytes, yet only 5 of them will be initialised.
1177For the assignment <code>s2 = s1</code>, gcc generates code to copy
1178all 8 bytes wholesale into <code>s2</code> without regard for their
1179meaning. If Valgrind simply checked values as they came out of
1180memory, it would yelp every time a structure assignment like this
1181happened. So the more complicated semantics described above is
1182necessary. This allows gcc to copy <code>s1</code> into
1183<code>s2</code> any way it likes, and a warning will only be emitted
1184if the uninitialised values are later used.
1185
1186<p>One final twist to this story. The above scheme allows garbage to
1187pass through the CPU's integer registers without complaint. It does
1188this by giving the integer registers V tags, passing these around in
1189the expected way. This complicated and computationally expensive to
1190do, but is necessary. Valgrind is more simplistic about
1191floating-point loads and stores. In particular, V bits for data read
1192as a result of floating-point loads are checked at the load
1193instruction. So if your program uses the floating-point registers to
1194do memory-to-memory copies, you will get complaints about
1195uninitialised values. Fortunately, I have not yet encountered a
1196program which (ab)uses the floating-point registers in this way.
1197
1198<a name="vaddress"></a>
1199<h3>3.2&nbsp; Valid-address (A) bits</h3>
1200
1201Notice that the previous section describes how the validity of values
1202is established and maintained without having to say whether the
1203program does or does not have the right to access any particular
1204memory location. We now consider the latter issue.
1205
1206<p>As described above, every bit in memory or in the CPU has an
1207associated valid-value (V) bit. In addition, all bytes in memory, but
1208not in the CPU, have an associated valid-address (A) bit. This
1209indicates whether or not the program can legitimately read or write
1210that location. It does not give any indication of the validity or the
1211data at that location -- that's the job of the V bits -- only whether
1212or not the location may be accessed.
1213
1214<p>Every time your program reads or writes memory, Valgrind checks the
1215A bits associated with the address. If any of them indicate an
1216invalid address, an error is emitted. Note that the reads and writes
1217themselves do not change the A bits, only consult them.
1218
1219<p>So how do the A bits get set/cleared? Like this:
1220
1221<ul>
1222 <li>When the program starts, all the global data areas are marked as
1223 accessible.</li><br>
1224 <p>
1225
1226 <li>When the program does malloc/new, the A bits for the exactly the
1227 area allocated, and not a byte more, are marked as accessible.
1228 Upon freeing the area the A bits are changed to indicate
1229 inaccessibility.</li><br>
1230 <p>
1231
1232 <li>When the stack pointer register (%esp) moves up or down, A bits
1233 are set. The rule is that the area from %esp up to the base of
1234 the stack is marked as accessible, and below %esp is
1235 inaccessible. (If that sounds illogical, bear in mind that the
1236 stack grows down, not up, on almost all Unix systems, including
1237 GNU/Linux.) Tracking %esp like this has the useful side-effect
1238 that the section of stack used by a function for local variables
1239 etc is automatically marked accessible on function entry and
1240 inaccessible on exit.</li><br>
1241 <p>
1242
1243 <li>When doing system calls, A bits are changed appropriately. For
1244 example, mmap() magically makes files appear in the process's
1245 address space, so the A bits must be updated if mmap()
1246 succeeds.</li><br>
1247</ul>
1248
1249
1250<a name="together"></a>
1251<h3>3.3&nbsp; Putting it all together</h3>
1252Valgrind's checking machinery can be summarised as follows:
1253
1254<ul>
1255 <li>Each byte in memory has 8 associated V (valid-value) bits,
1256 saying whether or not the byte has a defined value, and a single
1257 A (valid-address) bit, saying whether or not the program
1258 currently has the right to read/write that address.</li><br>
1259 <p>
1260
1261 <li>When memory is read or written, the relevant A bits are
1262 consulted. If they indicate an invalid address, Valgrind emits
1263 an Invalid read or Invalid write error.</li><br>
1264 <p>
1265
1266 <li>When memory is read into the CPU's integer registers, the
1267 relevant V bits are fetched from memory and stored in the
1268 simulated CPU. They are not consulted.</li><br>
1269 <p>
1270
1271 <li>When an integer register is written out to memory, the V bits
1272 for that register are written back to memory too.</li><br>
1273 <p>
1274
1275 <li>When memory is read into the CPU's floating point registers, the
1276 relevant V bits are read from memory and they are immediately
1277 checked. If any are invalid, an uninitialised value error is
1278 emitted. This precludes using the floating-point registers to
1279 copy possibly-uninitialised memory, but simplifies Valgrind in
1280 that it does not have to track the validity status of the
1281 floating-point registers.</li><br>
1282 <p>
1283
1284 <li>As a result, when a floating-point register is written to
1285 memory, the associated V bits are set to indicate a valid
1286 value.</li><br>
1287 <p>
1288
1289 <li>When values in integer CPU registers are used to generate a
1290 memory address, or to determine the outcome of a conditional
1291 branch, the V bits for those values are checked, and an error
1292 emitted if any of them are undefined.</li><br>
1293 <p>
1294
1295 <li>When values in integer CPU registers are used for any other
1296 purpose, Valgrind computes the V bits for the result, but does
1297 not check them.</li><br>
1298 <p>
1299
1300 <li>One the V bits for a value in the CPU have been checked, they
1301 are then set to indicate validity. This avoids long chains of
1302 errors.</li><br>
1303 <p>
1304
1305 <li>When values are loaded from memory, valgrind checks the A bits
1306 for that location and issues an illegal-address warning if
1307 needed. In that case, the V bits loaded are forced to indicate
1308 Valid, despite the location being invalid.
1309 <p>
1310 This apparently strange choice reduces the amount of confusing
1311 information presented to the user. It avoids the
1312 unpleasant phenomenon in which memory is read from a place which
1313 is both unaddressible and contains invalid values, and, as a
1314 result, you get not only an invalid-address (read/write) error,
1315 but also a potentially large set of uninitialised-value errors,
1316 one for every time the value is used.
1317 <p>
1318 There is a hazy boundary case to do with multi-byte loads from
1319 addresses which are partially valid and partially invalid. See
1320 details of the flag <code>--partial-loads-ok</code> for details.
1321 </li><br>
1322</ul>
1323
1324Valgrind intercepts calls to malloc, calloc, realloc, valloc,
1325memalign, free, new and delete. The behaviour you get is:
1326
1327<ul>
1328
1329 <li>malloc/new: the returned memory is marked as addressible but not
1330 having valid values. This means you have to write on it before
1331 you can read it.</li><br>
1332 <p>
1333
1334 <li>calloc: returned memory is marked both addressible and valid,
1335 since calloc() clears the area to zero.</li><br>
1336 <p>
1337
1338 <li>realloc: if the new size is larger than the old, the new section
1339 is addressible but invalid, as with malloc.</li><br>
1340 <p>
1341
1342 <li>If the new size is smaller, the dropped-off section is marked as
1343 unaddressible. You may only pass to realloc a pointer
1344 previously issued to you by malloc/calloc/new/realloc.</li><br>
1345 <p>
1346
1347 <li>free/delete: you may only pass to free a pointer previously
1348 issued to you by malloc/calloc/new/realloc, or the value
1349 NULL. Otherwise, Valgrind complains. If the pointer is indeed
1350 valid, Valgrind marks the entire area it points at as
1351 unaddressible, and places the block in the freed-blocks-queue.
1352 The aim is to defer as long as possible reallocation of this
1353 block. Until that happens, all attempts to access it will
1354 elicit an invalid-address error, as you would hope.</li><br>
1355</ul>
1356
1357
1358
1359<a name="signals"></a>
1360<h3>3.4&nbsp; Signals</h3>
1361
1362Valgrind provides suitable handling of signals, so, provided you stick
1363to POSIX stuff, you should be ok. Basic sigaction() and sigprocmask()
1364are handled. Signal handlers may return in the normal way or do
1365longjmp(); both should work ok. As specified by POSIX, a signal is
1366blocked in its own handler. Default actions for signals should work
1367as before. Etc, etc.
1368
1369<p>Under the hood, dealing with signals is a real pain, and Valgrind's
1370simulation leaves much to be desired. If your program does
1371way-strange stuff with signals, bad things may happen. If so, let me
1372know. I don't promise to fix it, but I'd at least like to be aware of
1373it.
1374
1375
1376<a name="leaks"><a/>
1377<h3>3.5&nbsp; Memory leak detection</h3>
1378
1379Valgrind keeps track of all memory blocks issued in response to calls
1380to malloc/calloc/realloc/new. So when the program exits, it knows
1381which blocks are still outstanding -- have not been returned, in other
1382words. Ideally, you want your program to have no blocks still in use
1383at exit. But many programs do.
1384
1385<p>For each such block, Valgrind scans the entire address space of the
1386process, looking for pointers to the block. One of three situations
1387may result:
1388
1389<ul>
1390 <li>A pointer to the start of the block is found. This usually
1391 indicates programming sloppiness; since the block is still
1392 pointed at, the programmer could, at least in principle, free'd
1393 it before program exit.</li><br>
1394 <p>
1395
1396 <li>A pointer to the interior of the block is found. The pointer
1397 might originally have pointed to the start and have been moved
1398 along, or it might be entirely unrelated. Valgrind deems such a
1399 block as "dubious", that is, possibly leaked,
1400 because it's unclear whether or
1401 not a pointer to it still exists.</li><br>
1402 <p>
1403
1404 <li>The worst outcome is that no pointer to the block can be found.
1405 The block is classified as "leaked", because the
1406 programmer could not possibly have free'd it at program exit,
1407 since no pointer to it exists. This might be a symptom of
1408 having lost the pointer at some earlier point in the
1409 program.</li>
1410</ul>
1411
1412Valgrind reports summaries about leaked and dubious blocks.
1413For each such block, it will also tell you where the block was
1414allocated. This should help you figure out why the pointer to it has
1415been lost. In general, you should attempt to ensure your programs do
1416not have any leaked or dubious blocks at exit.
1417
1418<p>The precise area of memory in which Valgrind searches for pointers
1419is: all naturally-aligned 4-byte words for which all A bits indicate
1420addressibility and all V bits indicated that the stored value is
1421actually valid.
1422
1423<p><hr width="100%">
1424
1425
1426<a name="limits"></a>
1427<h2>4&nbsp; Limitations</h2>
1428
1429The following list of limitations seems depressingly long. However,
1430most programs actually work fine.
1431
1432<p>Valgrind will run x86-GNU/Linux ELF dynamically linked binaries, on
1433a kernel 2.4.X system, subject to the following constraints:
1434
1435<ul>
1436 <li>No MMX, SSE, SSE2, 3DNow instructions. If the translator
1437 encounters these, Valgrind will simply give up. It may be
1438 possible to add support for them at a later time. Intel added a
1439 few instructions such as "cmov" to the integer instruction set
1440 on Pentium and later processors, and these are supported.
1441 Nevertheless it's safest to think of Valgrind as implementing
1442 the 486 instruction set.</li><br>
1443 <p>
1444
1445 <li>Multithreaded programs are not supported, since I haven't yet
1446 figured out how to do this. To be more specific, it is the
1447 "clone" system call which is not supported. A program calls
1448 "clone" to create threads. Valgrind will abort if this
1449 happens.</li><nr>
1450 <p>
1451
1452 <li>Valgrind assumes that the floating point registers are not used
1453 as intermediaries in memory-to-memory copies, so it immediately
1454 checks V bits in floating-point loads/stores. If you want to
1455 write code which copies around possibly-uninitialised values,
1456 you must ensure these travel through the integer registers, not
1457 the FPU.</li><br>
1458 <p>
1459
1460 <li>If your program does its own memory management, rather than
1461 using malloc/new/free/delete, it should still work, but
1462 Valgrind's error checking won't be so effective.</li><br>
1463 <p>
1464
1465 <li>Valgrind's signal simulation is not as robust as it could be.
1466 Basic POSIX-compliant sigaction and sigprocmask functionality is
1467 supplied, but it's conceivable that things could go badly awry
1468 if you do wierd things with signals. Workaround: don't.
1469 Programs that do non-POSIX signal tricks are in any case
1470 inherently unportable, so should be avoided if
1471 possible.</li><br>
1472 <p>
1473
1474 <li>I have no idea what happens if programs try to handle signals on
1475 an alternate stack (sigaltstack). YMMV.</li><br>
1476 <p>
1477
1478 <li>Programs which switch stacks are not well handled. Valgrind
1479 does have support for this, but I don't have great faith in it.
1480 It's difficult -- there's no cast-iron way to decide whether a
1481 large change in %esp is as a result of the program switching
1482 stacks, or merely allocating a large object temporarily on the
1483 current stack -- yet Valgrind needs to handle the two situations
1484 differently.</li><br>
1485 <p>
1486
1487 <li>x86 instructions, and system calls, have been implemented on
1488 demand. So it's possible, although unlikely, that a program
1489 will fall over with a message to that effect. If this happens,
1490 please mail me ALL the details printed out, so I can try and
1491 implement the missing feature.</li><br>
1492 <p>
1493
1494 <li>x86 floating point works correctly, but floating-point code may
1495 run even more slowly than integer code, due to my simplistic
1496 approach to FPU emulation.</li><br>
1497 <p>
1498
1499 <li>You can't Valgrind-ize statically linked binaries. Valgrind
1500 relies on the dynamic-link mechanism to gain control at
1501 startup.</li><br>
1502 <p>
1503
1504 <li>Memory consumption of your program is majorly increased whilst
1505 running under Valgrind. This is due to the large amount of
1506 adminstrative information maintained behind the scenes. Another
1507 cause is that Valgrind dynamically translates the original
1508 executable and never throws any translation away, except in
1509 those rare cases where self-modifying code is detected.
1510 Translated, instrumented code is 8-12 times larger than the
1511 original (!) so you can easily end up with 15+ MB of
1512 translations when running (eg) a web browser. There's not a lot
1513 you can do about this -- use Valgrind on a fast machine with a lot
1514 of memory and swap space. At some point I may implement a LRU
1515 caching scheme for translations, so as to bound the maximum
1516 amount of memory devoted to them, to say 8 or 16 MB.</li>
1517</ul>
1518
1519
1520Programs which are known not to work are:
1521
1522<ul>
1523 <li>Netscape 4.76 works pretty well on some platforms -- quite
1524 nicely on my AMD K6-III (400 MHz). I can surf, do mail, etc, no
1525 problem. On other platforms is has been observed to crash
1526 during startup. Despite much investigation I can't figure out
1527 why.</li><br>
1528 <p>
1529
1530 <li>kpackage (a KDE front end to rpm) dies because the CPUID
1531 instruction is unimplemented. Easy to fix.</li><br>
1532 <p>
1533
1534 <li>knode (a KDE newsreader) tries to do multithreaded things, and
1535 fails.</li><br>
1536 <p>
1537
1538 <li>emacs starts up but immediately concludes it is out of memory
1539 and aborts. Emacs has it's own memory-management scheme, but I
1540 don't understand why this should interact so badly with
1541 Valgrind.</li><br>
1542 <p>
1543
1544 <li>Gimp and Gnome and GTK-based apps die early on because
1545 of unimplemented system call wrappers. (I'm a KDE user :)
1546 This wouldn't be hard to fix.
1547 </li><br>
1548 <p>
1549
1550 <li>As a consequence of me being a KDE user, almost all KDE apps
1551 work ok -- except those which are multithreaded.
1552 </li><br>
1553 <p>
1554</ul>
1555
1556
1557<p><hr width="100%">
1558
1559
1560<a name="howitworks"></a>
1561<h2>5&nbsp; How it works -- a rough overview</h2>
1562Some gory details, for those with a passion for gory details. You
1563don't need to read this section if all you want to do is use Valgrind.
1564
1565<a name="startb"></a>
1566<h3>5.1&nbsp; Getting started</h3>
1567
1568Valgrind is compiled into a shared object, valgrind.so. The shell
1569script valgrind sets the LD_PRELOAD environment variable to point to
1570valgrind.so. This causes the .so to be loaded as an extra library to
1571any subsequently executed dynamically-linked ELF binary, viz, the
1572program you want to debug.
1573
1574<p>The dynamic linker allows each .so in the process image to have an
1575initialisation function which is run before main(). It also allows
1576each .so to have a finalisation function run after main() exits.
1577
1578<p>When valgrind.so's initialisation function is called by the dynamic
1579linker, the synthetic CPU to starts up. The real CPU remains locked
1580in valgrind.so for the entire rest of the program, but the synthetic
1581CPU returns from the initialisation function. Startup of the program
1582now continues as usual -- the dynamic linker calls all the other .so's
1583initialisation routines, and eventually runs main(). This all runs on
1584the synthetic CPU, not the real one, but the client program cannot
1585tell the difference.
1586
1587<p>Eventually main() exits, so the synthetic CPU calls valgrind.so's
1588finalisation function. Valgrind detects this, and uses it as its cue
1589to exit. It prints summaries of all errors detected, possibly checks
1590for memory leaks, and then exits the finalisation routine, but now on
1591the real CPU. The synthetic CPU has now lost control -- permanently
1592-- so the program exits back to the OS on the real CPU, just as it
1593would have done anyway.
1594
1595<p>On entry, Valgrind switches stacks, so it runs on its own stack.
1596On exit, it switches back. This means that the client program
1597continues to run on its own stack, so we can switch back and forth
1598between running it on the simulated and real CPUs without difficulty.
1599This was an important design decision, because it makes it easy (well,
1600significantly less difficult) to debug the synthetic CPU.
1601
1602
1603<a name="engine"></a>
1604<h3>5.2&nbsp; The translation/instrumentation engine</h3>
1605
1606Valgrind does not directly run any of the original program's code. Only
1607instrumented translations are run. Valgrind maintains a translation
1608table, which allows it to find the translation quickly for any branch
1609target (code address). If no translation has yet been made, the
1610translator - a just-in-time translator - is summoned. This makes an
1611instrumented translation, which is added to the collection of
1612translations. Subsequent jumps to that address will use this
1613translation.
1614
1615<p>Valgrind can optionally check writes made by the application, to
1616see if they are writing an address contained within code which has
1617been translated. Such a write invalidates translations of code
1618bracketing the written address. Valgrind will discard the relevant
1619translations, which causes them to be re-made, if they are needed
1620again, reflecting the new updated data stored there. In this way,
1621self modifying code is supported. In practice I have not found any
1622Linux applications which use self-modifying-code.
1623
1624<p>The JITter translates basic blocks -- blocks of straight-line-code
1625-- as single entities. To minimise the considerable difficulties of
1626dealing with the x86 instruction set, x86 instructions are first
1627translated to a RISC-like intermediate code, similar to sparc code,
1628but with an infinite number of virtual integer registers. Initially
1629each insn is translated seperately, and there is no attempt at
1630instrumentation.
1631
1632<p>The intermediate code is improved, mostly so as to try and cache
1633the simulated machine's registers in the real machine's registers over
1634several simulated instructions. This is often very effective. Also,
1635we try to remove redundant updates of the simulated machines's
1636condition-code register.
1637
1638<p>The intermediate code is then instrumented, giving more
1639intermediate code. There are a few extra intermediate-code operations
1640to support instrumentation; it is all refreshingly simple. After
1641instrumentation there is a cleanup pass to remove redundant value
1642checks.
1643
1644<p>This gives instrumented intermediate code which mentions arbitrary
1645numbers of virtual registers. A linear-scan register allocator is
1646used to assign real registers and possibly generate spill code. All
1647of this is still phrased in terms of the intermediate code. This
1648machinery is inspired by the work of Reuben Thomas (MITE).
1649
1650<p>Then, and only then, is the final x86 code emitted. The
1651intermediate code is carefully designed so that x86 code can be
1652generated from it without need for spare registers or other
1653inconveniences.
1654
1655<p>The translations are managed using a traditional LRU-based caching
1656scheme. The translation cache has a default size of about 14MB.
1657
1658<a name="track"></a>
1659
1660<h3>5.3&nbsp; Tracking the status of memory</h3> Each byte in the
1661process' address space has nine bits associated with it: one A bit and
1662eight V bits. The A and V bits for each byte are stored using a
1663sparse array, which flexibly and efficiently covers arbitrary parts of
1664the 32-bit address space without imposing significant space or
1665performance overheads for the parts of the address space never
1666visited. The scheme used, and speedup hacks, are described in detail
1667at the top of the source file vg_memory.c, so you should read that for
1668the gory details.
1669
1670<a name="sys_calls"></a>
1671
1672<h3>5.4 System calls</h3>
1673All system calls are intercepted. The memory status map is consulted
1674before and updated after each call. It's all rather tiresome. See
1675vg_syscall_mem.c for details.
1676
1677<a name="sys_signals"></a>
1678
1679<h3>5.5&nbsp; Signals</h3>
1680All system calls to sigaction() and sigprocmask() are intercepted. If
1681the client program is trying to set a signal handler, Valgrind makes a
1682note of the handler address and which signal it is for. Valgrind then
1683arranges for the same signal to be delivered to its own handler.
1684
1685<p>When such a signal arrives, Valgrind's own handler catches it, and
1686notes the fact. At a convenient safe point in execution, Valgrind
1687builds a signal delivery frame on the client's stack and runs its
1688handler. If the handler longjmp()s, there is nothing more to be said.
1689If the handler returns, Valgrind notices this, zaps the delivery
1690frame, and carries on where it left off before delivering the signal.
1691
1692<p>The purpose of this nonsense is that setting signal handlers
1693essentially amounts to giving callback addresses to the Linux kernel.
1694We can't allow this to happen, because if it did, signal handlers
1695would run on the real CPU, not the simulated one. This means the
1696checking machinery would not operate during the handler run, and,
1697worse, memory permissions maps would not be updated, which could cause
1698spurious error reports once the handler had returned.
1699
1700<p>An even worse thing would happen if the signal handler longjmp'd
1701rather than returned: Valgrind would completely lose control of the
1702client program.
1703
1704<p>Upshot: we can't allow the client to install signal handlers
1705directly. Instead, Valgrind must catch, on behalf of the client, any
1706signal the client asks to catch, and must delivery it to the client on
1707the simulated CPU, not the real one. This involves considerable
1708gruesome fakery; see vg_signals.c for details.
1709<p>
1710
1711<hr width="100%">
1712
1713<a name="example"></a>
1714<h2>6&nbsp; Example</h2>
1715This is the log for a run of a small program. The program is in fact
1716correct, and the reported error is as the result of a potentially serious
1717code generation bug in GNU g++ (snapshot 20010527).
1718<pre>
1719sewardj@phoenix:~/newmat10$
1720~/Valgrind-6/valgrind -v ./bogon
1721==25832== Valgrind 0.10, a memory error detector for x86 RedHat 7.1.
1722==25832== Copyright (C) 2000-2001, and GNU GPL'd, by Julian Seward.
1723==25832== Startup, with flags:
1724==25832== --suppressions=/home/sewardj/Valgrind/redhat71.supp
1725==25832== reading syms from /lib/ld-linux.so.2
1726==25832== reading syms from /lib/libc.so.6
1727==25832== reading syms from /mnt/pima/jrs/Inst/lib/libgcc_s.so.0
1728==25832== reading syms from /lib/libm.so.6
1729==25832== reading syms from /mnt/pima/jrs/Inst/lib/libstdc++.so.3
1730==25832== reading syms from /home/sewardj/Valgrind/valgrind.so
1731==25832== reading syms from /proc/self/exe
1732==25832== loaded 5950 symbols, 142333 line number locations
1733==25832==
1734==25832== Invalid read of size 4
1735==25832== at 0x8048724: _ZN10BandMatrix6ReSizeEiii (bogon.cpp:45)
1736==25832== by 0x80487AF: main (bogon.cpp:66)
1737==25832== by 0x40371E5E: __libc_start_main (libc-start.c:129)
1738==25832== by 0x80485D1: (within /home/sewardj/newmat10/bogon)
1739==25832== Address 0xBFFFF74C is not stack'd, malloc'd or free'd
1740==25832==
1741==25832== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
1742==25832== malloc/free: in use at exit: 0 bytes in 0 blocks.
1743==25832== malloc/free: 0 allocs, 0 frees, 0 bytes allocated.
1744==25832== For a detailed leak analysis, rerun with: --leak-check=yes
1745==25832==
1746==25832== exiting, did 1881 basic blocks, 0 misses.
1747==25832== 223 translations, 3626 bytes in, 56801 bytes out.
1748</pre>
1749<p>The GCC folks fixed this about a week before gcc-3.0 shipped.
1750<hr width="100%">
1751<p>
1752</body>
1753</html>