Upgrade to Pro — share decks privately, control downloads, hide ads and more …

如果下半輩子只想 DEBUG 怎麼辦?

如果下半輩子只想 DEBUG 怎麼辦?

Kudo Chien

June 05, 2015
Tweet

More Decks by Kudo Chien

Other Decks in Technology

Transcript

  1. BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG

    BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG https://www.flickr.com/photos/djjohnson/169497259
  2. DEBUG 也能出頭天的⼈人⽣生故事 • biideal 2013 - now • 從 software

    debugging 到
 product market debugging https://www.flickr.com/photos/gagillphoto/3336353424
  3. 你我都會的 DEBUG • printf() • fprintf() • 合理質疑,log 也是會掉的 •

    stdout is line buffered • stderr is unbuffered • FILE is fully buffered • In default C
  4. 你我都會的 DEBUG • console.log() • console.error() • console.warn() • console.trace()

    • https://developer.chrome.com/devtools/docs/console-api Chrom e
  5. Basic Debugger • gcc(clang) -g main.c • gdb(lldb) a.out •

    br • info locals • n / s / finish / cont • bt C
  6. Basic Debugger • Crash / Exception 相對是容易 抓的 bug •

    Debugger 黏上去就對了 https://www.flickr.com/photos/rachaelvoorhees/828354236
  7. Conditional Breakpoint • br funcName if val == n •

    Sometimes, Conditional Breakpoint is slow • Do logic in code and set breakpoint at the block
  8. Conditional Breakpoint C for (int num = 0; num <

    100; ++num) { int randVal = rand() % 10; if (randVal == 0) { puts("set breakpoint here."); // raise(SIGINT); } printf("Result: %d\n”, num / randVal); }
  9. Conditional Breakpoint Chrom e function foo() { var num, randVal;

    for (num = 0; num < 100; ++num) { randVal = Math.floor(Math.random() * 10); if (randVal == 0) { debugger; } console.log("Result: " + num / randVal); } }
  10. Post-mortem Debugging • Onsite debug • Code sign / Build

    concern • Debug old product version https://www.flickr.com/photos/x1klima/14594615831
  11. RELEASE vs DEBUG build • RELEASE vs DEBUG build •

    gcc -O + strip vs gcc -g • debug information(symbol) • symbol names / line number … etc • pdb / dSYM
  12. Post-mortem Debugging • Linux as example • gdb executable_with_symbol •

    https://sourceware.org/gdb/onlinedocs/gdb/ Separate-Debug-Files.html C
  13. Post-mortem Debugging • JavaScript as dynamic language • Best practice

    is to do compress • UglifyJS2, Google Closure Compiler • Source Map Chrom e
  14. Post-mortem Debugging Chrom e function foo() { var num, randVal;

    for (num = 0; num < 100; ++num) { randVal = Math.floor(Math.random() * 10); if (randVal == 0) { debugger; } console.log("Result: " + num / randVal); } } function foo(){var o,a;for(o=0;100>o;++o)a=Math.floor(10*Math.random()),console.log("Result: "+o/a)}
  15. Post-mortem Debugging • //# sourceMappingURL=main.min.js.map • Comment in minified JS

    file Chrom e {"version":3,"file":"main.min.js","sources":["main.c"],"names": ["foo","num","randVal","Math","floor","random","console","log"],"mappings":"AAAA,QAASA,OACP,G AAIC,GAAKC,CAET,KAAKD,EAAM,EAAS,IAANA,IAAaA,EACzBC,EAAUC,KAAKC,MAAsB,GAAhBD,KAAKE,UAI1BC,QAAQ C,IAAI,WAAaN,EAAMC"}
  16. Debug Hanging C for (int num = 0; num <

    100; ++num) { int randVal = rand() % 10; if (randVal == 0) { sleep(3000); } else { sleep(randVal); } }
  17. Debug Hanging • run as you did • Get the

    pid • ulimit -c unlimited • kill -SIGTRAP [pid] • gdb execfile core • bt C • Use cases • Daemon / Service • PhantomJS hanging case
  18. Profiling • How to debug for a HTTP response timeout

    issue • framework / database / external call … etc W eb 想法 驗證 學習
  19. Mobile Remote Debugging • Real device debugging • Mobile Safari

    • Chrome on Android Chrom e Safari https://developer.chrome.com/devtools/docs/remote-debugging
  20. • Server DEBUG • 讓它噴 • Fast Deployment • Client

    DEBUG • 盡其所能地防⽌止 Crash 並留下 log • Slow Upgrade
  21. Dependency Injection int adder(int a, int b) { return a

    + b; } int calc(int a, int b) { return adder(a, b); } int main(int argc, char *argv[]) { int result = calc(3, 6); printf("Result: %d\n", result); return EXIT_SUCCESS; }
  22. int adder(int a, int b) { return a + b;

    } int calc(int a, int b, int (*strategy)(int, int)) { return (*strategy)(a, b); } int main(int argc, char *argv[]) { int result = calc(3, 6, &adder); printf("Result: %d\n", result); return EXIT_SUCCESS; }
  23. int realRemoteAdder(int a, int b) { // Assume to get

    data from remote server sleep(10); return a + b; } int mockAdder(int a, int b) { return a + b; } int calc(int a, int b, int (*strategy)(int, int)) { return (*strategy)(a, b); } int main(int argc, char *argv[]) { int result = calc(3, 6, &mockAdder); printf("Result: %d\n", result); return EXIT_SUCCESS; }