Programming Practice: Debugging

  • Testing: design/imply test to check correct operation & expose bugs
  • Debugging: process of identify and solve run-time behavior of the software
  • Prevent bug
    • defensive programming
    • hard
      • multiple, complex interaction
      • infinite possible input
    • program defensively
      • knowing the pro/cons of language use
      • idiom
      • pre-condition/post-condition
      • use tools
  • Bug Terminology
    • reproducible is better than transient ones
    • Bohr bug: repeat bug under some unknown but well-defined conditions
    • Heisen bug: bug may disappears or alert when try to find it
  • Common type of bug
    • syntex bug
      • typing error
      • misunderstanding of symbol meaning
    • arithmetic bug
      • /0
      • overflow/underflow
        • variable value larger than maximum data type can hold
      • floating point
        • lost value when convert from a float to int
      • logic bug
        • infinite loop
        • infinite recursion
        • off-by-one error
          • use >= rather than >(depend by case)
      • resource bug
        • null pointer
        • do not free dynamic memory
        • re-use of freed memory
        • confusing different memory type(static, local,dynamic)
        • array overrun
        • buffer overflow
          • often source of Heisen bug
          • major security vulnerability
      • side-effect bug
        • datatype needed not defined
        • do not have deallocator for dynamic variable
  • Debug techniques
    • DEBUG flag and printf function
      • add flag and printf to place bug may occur
      • catch the ling bug occur
      • pro:
        • simple
      • con:
        • only useful with good placement
        • not flexible
        • may cause a lot output
    • wolf-fence
      • use a printf function to show which part bug occur, add printf until reach line bug occur
      • do not work if bug not in main file
    • logging
      • use print function to print relevant data for each function before and after
      • find if an error occur when not print or print wrong data
    • interactive debugger: ex. gdb
      • compile with -g for symbol and other information
      • stack trace
        • use debugger to find
          • where crash occur
          • what core made, order, and parameter
        • trace execution
          • use watch point and break pint to check through the code what happen by variable and memory location change
        • next command: execute next line include any thing happened
        • step command: execute until another line reached
        • display command: show expression each time program stop
        • watch command: set a watch point for expression
        • pro
          • use on program hard to add printf/DEBUG
          • interactive debugger to show what happen, not what programmer think happen
  • Core file
    • containing an image of process`s memory at the time of termination
    • often produced as the action due to a signal
    • name : core.PID

留下评论