Advertisement
Guest User

hackers.txt

a guest
Oct 15th, 2012
23,113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.71 KB | None | 0 0
  1. Merc Release 2.1
  2. Sunday 01 August 1993
  3.  
  4.  
  5.  
  6.  
  7. === 'I'm running a Mud so I can learn C programming!'
  8.  
  9. Yeah, right.
  10.  
  11. The purpose of this document is to record some of our knowledge, experience and
  12. philosophy. No matter what your level, we hope that this document will help
  13. you become a better software engineer.
  14.  
  15. Remember that engineering is work, and NO document will substitute for your
  16. own thinking, learning and experimentation.
  17.  
  18.  
  19.  
  20. === How to Learn in the First Place
  21.  
  22. (1) Play with something.
  23. (2) Read the documentation on it.
  24. (3) Play with it some more.
  25. (4) Read documentation again.
  26. (5) Play with it some more.
  27. (6) Read documentation again.
  28. (7) Play with it some more.
  29. (8) Read documentation again.
  30. (9) Get the idea?
  31.  
  32. The idea is that your mind can accept only so much 'new data' in a single
  33. session. Playing with something doesn't introduce very much new data, but it
  34. does transform data in your head from the 'new' category to the 'familiar'
  35. category. Reading documentation doesn't make anything 'familiar', but it
  36. refills your 'new' hopper.
  37.  
  38. Most people, if they even read documentation in the first place, never return
  39. to it. They come to a certain minimum level of proficiency and then never
  40. learn any more. But modern operating systems, languages, networks, and even
  41. applications simply cannot be learned in a single session. You have to work
  42. through the two-step learning cycle MANY times to master it.
  43.  
  44.  
  45.  
  46. === The Environment
  47.  
  48. Computer: the big or little box that you're using to run Merc. Computers come
  49. from a _manufacturer_ and have a _model_ name. Here is a list of common
  50. manufacturers and models that you're likely to encounter:
  51.  
  52. Manufacturer Model
  53. ------------ -----
  54.  
  55. Sun Sun-2
  56. Sun Sun-3
  57. Sun Sun-4
  58. DEC Vax 5000
  59. DEC Vax 5900
  60. IBM RS/6000
  61. NeXT NextCube
  62. Sequent Symmetry
  63. Sequent Balance
  64.  
  65. As far as hardware goes, Merc will run on any 32-bit hardware.
  66.  
  67. Operating system: the lowest level program running on your computer. Most
  68. common computers run Unix or some variant of it, such as SunOS, Ultrix,
  69. AIX, Mach, or Dynix. Notice that many of these variants end in 'IX'.
  70.  
  71. The two major 'families' of Unix are Berkeley Unix (developed at the
  72. illustrious University of California, Berkeley) and System 5 Unix
  73. (developed by Bell Laboratories, the progenitors of Unix).
  74.  
  75. The most common non-Unix operating system is VMS (a proprietary operating
  76. system from DEC for their VAX computers). In the personal computer world,
  77. you'll find MS-DOS, OS/2 for IBM PC's and compatibles, and MacOS for Apple
  78. Macintosh'es.
  79.  
  80. GET THIS STRAIGHT: 'VAX' IS NOT AN OPERATING SYSTEM. It's the name of a
  81. family of computers from DEC. There are plenty of Vax'es running VMS, and
  82. there are even more Vax'es running Berkeley Unix or Ultrix. The Vax'es
  83. running Unix have a lot more in common with other machines running
  84. Unix than they have with Vax'es running VMS.
  85.  
  86. As far as operating systems go, Merc will run on Unix or Unix variants with
  87. TCP/IP networking compatible with Berkeley Unix. It will also run, in
  88. single-user mode only, on MS-DOS. With a reasonable amount of work, Merc
  89. can be ported to any operating system that provides TCP service for telnet
  90. connections.
  91.  
  92. Languages: Merc is written in C. ANSI (the American National Standards
  93. Institute) has a specification for the C language, and Merc is written in
  94. Ansi Standard C.
  95.  
  96. The most popular compiler for Ansi Standard C is the Gnu 'gcc' compiler
  97. produced by the Free Software Foundation. It's available by anonymous
  98. ftp from prep.ai.mit.edu. Merc compiles just fine with Gcc 1.38, so
  99. you can probably use 1.42 and skip the much larger 2.X versions.
  100.  
  101. You don't have to use gcc. IBM RS/6000's running the AIX operating system
  102. come with an Ansi C compiler already. So do NeXT machines (the standard
  103. 'cc' on NeXT happens to be the Gnu C compiler). Any Ansi compiler will
  104. work.
  105.  
  106. Unfortunately, there are still many machines out there without an Ansi
  107. standard C compiler. (Sun is the worst offender in this regard). You
  108. can attempt to compile Merc with a non-Ansi (traditional) C compiler by
  109. using the 'mktrad' script. See trad.txt for details.
  110.  
  111. If you don't know what the manufacturer and model of your computer is, as well
  112. as its operating system, and whether the C compiler is Ansi or non-Ansi, then
  113. you need to find out.
  114.  
  115.  
  116.  
  117. === Basic Unix Tools
  118.  
  119. 'man' -- gives you online manual pages
  120.  
  121. 'grep' -- stands for 'global regular expression print'
  122.  
  123. 'vi'
  124. 'emacs'
  125. 'jove' -- use whatever editor floats your boat
  126. but learn the hell out of it
  127. you should know EVERY command in your editor
  128.  
  129. 'ctags' -- makes 'tags' for your editor
  130. allows you to goto functions by name in any source file
  131.  
  132. '>'
  133. '>>'
  134. '<'
  135. '|' -- input and output redirection
  136. get someone to show you, or dig it out of 'man csh'
  137.  
  138. These are the basic day-in day-out development tools. Developing without
  139. knowing how to use ALL of these well is like driving a car without knowing how
  140. to change gears.
  141.  
  142.  
  143.  
  144. === Debugging: Theory
  145.  
  146. Debugging is a science. You formulate a hypothesis, make predictions based on
  147. the hypothesis, run the program and provide it experimental input, observe its
  148. behavior, and confirm or refute the hypothesis.
  149.  
  150. A good hypothesis is one which makes surprising predictions which then come
  151. true; predictions that other hypotheses don't make.
  152.  
  153. The first step in debugging is not to write bugs in the first place. This
  154. sounds obvious, but sadly, is all too often ignored.
  155.  
  156. If you build a program, and you get ANY errors or ANY warnings, you should fix
  157. them before continuing. C was designed so that many buggy ways of writing code
  158. are legal, but will draw warnings from a suitably smart compiler (such as 'gcc'
  159. with the '-Wall' flag enabled). It takes only minutes to check your warnings
  160. and to fix the code that generates them, but it takes hours to find bugs
  161. otherwise.
  162.  
  163. 'Desk checking' (proof reading) is almost a lost art in 1993. Too bad. You
  164. should desk check your code before even compiling it, and desk-check it again
  165. periodically to keep it fresh in mind and find new errors. If you have someone
  166. in your group whose ONLY job it is to desk-check other people's code, that
  167. person will find and fix more bugs than everyone else combined.
  168.  
  169. One can desk-check several hundred lines of code per hour. A top-flight
  170. software engineer will write, roughly, 99% accurate code on the first pass,
  171. which still means one bug per hundred lines. And you are not top flight.
  172. So ... you will find several bugs per hour by desk checking. This is a very
  173. rapid bug fixing technique. Compare that to all the hours you spend screwing
  174. around with broken programs trying to find ONE bug at a time.
  175.  
  176. The next technique beyond desk-checking is the time-honored technique of
  177. inserting 'print' statements into the code, and then watching the logged
  178. values. Within Merc code, you can call 'printf' or 'fprintf' to dump
  179. interesting values at interesting times. Where and when to dump these values
  180. is an art, which you will learn only with practice.
  181.  
  182. If you don't already know how to redirect output in your operating system, now
  183. is the time to learn. On Unix, type the command 'man csh', and read the part
  184. about the '>' operator. You should also learn the difference between
  185. 'standard output' (e.g. output from 'printf') and 'error output' (e.g. output
  186. from 'fprintf').
  187.  
  188. Ultimately, you cannot fix a program unless you understand how it's operating
  189. in the first place. Powerful debugging tools will help you collect data, but
  190. they can't interpret it, and they can't fix the underlying problems. Only you
  191. can do that.
  192.  
  193. When you find a bug ... your first impulse will be to change the code, kill the
  194. manifestation of the bug, and declare it fixed. Not so fast! The bug you
  195. observe is often just the symptom of a deeper bug. You should keep pursuing
  196. the bug, all the way down. You should grok the bug and cherish it in fullness
  197. before causing its discorporation.
  198.  
  199. Also, when finding a bug, ask yourself two questions: 'what design and
  200. programming habits led to the introduction of the bug in the first place?'
  201. And: 'what habits would systematically prevent the introduction of bugs like
  202. this?'
  203.  
  204.  
  205.  
  206. === Debugging: Tools
  207.  
  208. When a Unix process accesses an invalid memory location, or (more rarely)
  209. executes an illegal instruction, or (even more rarely) something else goes
  210. wrong, the Unix operating system takes control. The process is incapable of
  211. further execution and must be killed. Before killing the process, however, the
  212. operating system does something for you: it opens a file named 'core' and
  213. writes the entire data space of the process into it.
  214.  
  215. Thus, 'dumping core' is not a cause of problems, or even an effect of problems.
  216. It's something the operating system does to help you find fatal problems which
  217. have rendered your process unable to continue.
  218.  
  219. One reads a 'core' file with a debugger. The two most popular debuggers on
  220. Unix are 'adb' and 'gdb', although occasionally one finds 'dbx'. Typically
  221. one starts a debugger like this: 'adb merc' or 'gdb merc core'.
  222.  
  223. The first thing, and often the only thing, you need to do inside the debugger
  224. is take a stack trace. In 'adb', the command for this is '$c'. In gdb,
  225. the command is 'backtrace'. The stack trace will tell you what function your
  226. program was in when it crashed, and what functions were calling it. The
  227. debugger will also list the arguments to these functions. Interpreting these
  228. arguments, and using more advanced debugger features, requires a fair amount of
  229. knowledge about assembly language programming.
  230.  
  231. If you have access to a program named 'Purify' ... learn how to use it.
  232.  
  233.  
  234.  
  235. === Profiling
  236.  
  237. Here is how to profile a program:
  238.  
  239. (1) Remove all the .o files and the 'merc' executable:
  240.  
  241. rm *.o 'merc'
  242.  
  243. (2) Edit your makefile, and change the PROF= line:
  244.  
  245. PROF = -p
  246.  
  247. (3) Remake merc:
  248.  
  249. make
  250.  
  251. (4) Run merc as usual. Shutdown the game with shutdown when you have run long
  252. enough to get a good profiling base. If you crash the game, or kill the
  253. process externally, you won't get profiling information.
  254.  
  255. (5) Run the 'prof' command:
  256.  
  257. prof merc > prof.out
  258.  
  259. (6) Read prof.out. Run 'man prof' to understand the format of the output.
  260.  
  261. For advanced profiling, you can use 'PROF = -pg' in step (2), and use the
  262. 'gprof' command in step 5. The 'gprof' form of profiling gives you a report
  263. which lists exactly how many times any function calls any other function. This
  264. information is valuable for debugging as well as performance analysis.
  265.  
  266. Availability of 'prof' and 'gprof' varies from system to system. Almost every
  267. Unix system has 'prof'. Only some systems have 'gprof'.
  268.  
  269.  
  270.  
  271. === Schedule versus Features versus Quality
  272.  
  273. Now for a few words on project management.
  274.  
  275. Sooner or later, almost any project faces a trade-off between schedule,
  276. features, and quality. Consider a student writing a term paper on the last
  277. night. He has three unpalatable choices: he can turn it in late (miss the
  278. schedule). He can turn in a shorter paper that doesn't cover everything
  279. (reduce the features). Or he can churn out gibberish (lower the quality).
  280.  
  281. Similarly in a software project, one often has a choice between making the
  282. release date, or dropping features, or shipping everything on time and
  283. hoping that it works (it usually doesn't).
  284.  
  285. The most important thing to realize about this decision is that it IS a
  286. decision. One can't get out of it by hoping that some miracle will occur.
  287. If you don't react consciously, then external circumstances will drive the
  288. decision.
  289.  
  290. Ok, so suppose you are faced with the trade-off and go for a schedule slip.
  291. Don't take a small slip ... take a big impressive slip. If you say
  292. 'I'll just fix this one problem and finish ASAP', then likely you will
  293. wish you had taken just a little more time later. If you say 'I think I
  294. need another day, so I'll slip by a week', then it's much more likely
  295. that what you'll have at the end of the week will do the job. It's better
  296. to slip a large block of time once then to slip day-by-day or hour-by-hour
  297. repeatedly.
  298.  
  299. If you go for dropping features, again, carve off a big hunk. Don't be
  300. timid and pretend that you're going to do that work 'if you just get a
  301. little spare time.' That feature of your project is GONE, exploit the
  302. lessened requirements for all the savings you can!
  303.  
  304. I can't offer much advise on how to reduce quality, because that's always
  305. my last choice for what to drop on a project.
  306.  
  307.  
  308.  
  309. === Sleeping
  310.  
  311. Simple and obvious, but true ... engineering takes an alert mind.
  312.  
  313. It's very easy, very seductive, to throw a lot of consecutive hours at a
  314. problem. One can get into a 'flow' state where one's mind becomes filled
  315. with the problem, and the work just pours out, hour after hour. Many
  316. writers report that they watch a story take place, and just transcribe
  317. what they see, pounding out page after page of text. Many software
  318. engineers have experienced a similar feeling, where the code appears
  319. to arise spontaneously as they watch themselves type.
  320.  
  321. I believe most real work gets done in this state.
  322.  
  323. My experience, however, is that the 'flow' period can end subtly and
  324. gradually. Without ever noticing a change, I notice that new work isn't
  325. flowing out of my hands anymore, that I'm spending lots of time fixing
  326. up mistakes I made just a few moments ago. Instead of ideas flashing
  327. confidently through my mind, doubts and questions arise.
  328.  
  329. At this point there is a temptation to throw some more hours at the problem.
  330. 'I'm here, and I was getting a lot of work done, why don't I just stay all
  331. night until I figure this out?' This is a trap! Don't do it!
  332.  
  333. Instead, I suggest: go home, eat, shower, sleep, put yourself back together
  334. again. Resume the next day. While you sleep, your mind will work on the
  335. problem anyways, and you'll probably wake up with new ideas. You'll get
  336. more done between 10:00 am and 2:00 pm the next day, then if you stayed up
  337. between midnight and 10:00 am.
  338.  
  339. There is a problem with this strategy: remotivating yourself in the morning.
  340. If the project is one of your choice, that's usually not a problem. If it's
  341. something you have to do but don't enjoy, you have to balance the remotivation
  342. problem versus the very low productivity of working without sleep.
  343.  
  344.  
  345.  
  346. === Books for Serious Programmers
  347.  
  348. Out of all the thousands of books out there, three stand out:
  349.  
  350. Kernighan and Plaugher, _The Elements of Programming Style_.
  351.  
  352. Kernighan and Ritchie, _The C Programming Language_.
  353.  
  354. Brooks, _The Mythical Man Month_
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement