15 213 How to dBug your threaded proxy Ji im a PARALLEL DATA LABORATORY Carnegie Mellon University Motivation Concurrency is a good servant but a bad master Today we will cover good programming practices good debugging practices tips for designing and testing your proxy a tool for testing your multi threaded proxy http www pdl cmu edu 2 Jiri Simsa October 10 Before We Start Go to http www virtualbox org and download and install VirtualBox on your laptop Go to http www cs cmu edu 213 resources html and start downloading the dbug 213 vdi http www pdl cmu edu 3 Jiri Simsa October 10 GOOD PROGRAMMING PRACTICES http www pdl cmu edu 4 Jiri Simsa October 10 1 Know Your Library Calls pthread mutex lock mutex if cache empty pthread mutex unlock mutex pthread mutex unlock mutex Spending time reading a man page will dispel your misconceptions and save you hours of frustration http www pdl cmu edu 5 Jiri Simsa October 10 2 Check Return Values ret accept sockfd addr addrlen if ret 1 perror Accept failed n else assert pthread mutex lock mutex 0 Handling error cases correctly will facilitate identification of program failure root cause http www pdl cmu edu 6 Jiri Simsa October 10 3 Use Conditional Compilation void request handler void args ifdef DEBUG printf Entering s FUNCTION endif gcc DDEBUG http www pdl cmu edu Spending time including informative messages will greatly improve your debugging efficiency 7 Jiri Simsa October 10 4 Learn and Use Makefiles CC gcc CFLAGS g Wall Werror LDFLAGS lpthread all proxy proxy test proxy proxy c csapp c CC o CFLAGS O3 LDFLAGS proxy test CC o http www pdl cmu edu Spending time learning howcsapp c to use a Makefile will proxy c CFLAGS DDEBUG LDFLAGS save you hours of repetitive and dull work 8 Jiri Simsa October 10 5 Keep It Simple Silly int main int argc char argv init proxy while 1 if ret accept sockfd addr addrlen 0 setup args sockfd addr addrlen args ret pthread create tid 0 handler args assert ret 0 else perror Accept failed n Start with the simplest approach possible Often getting the simplest design right will be enough http www pdl cmu edu 9 Jiri Simsa October 10 6 Avoid Premature Optimizations All cache operations take O 1 time Sick fancy cache t cache inline void lookup cache entry struct cache entry entry 100 lines of code Start with the simplest approach possible Often getting the simplest design right will be enough http www pdl cmu edu 10 Jiri Simsa October 10 7 Learn and Use gdb ulimit c unlimited proxy 1234 Segmentation fault core dumped gdb proxy core gdb bt 3 0x08049865 in doit fd 4 at proxy c 179 4 0x080496c5 in thread handler connfdp 0x9920008 at proxy c 136 5 0x00da7cc9 in start thread arg 0xb78adb70 at pthread create c 304 Learning how to control and inspect execution of a program is instrumental to efficient debugging http www pdl cmu edu 11 Jiri Simsa October 10 Summary of Good Practices 1 2 3 4 5 6 7 Know Your Library Calls Check Return Values Use Conditional Compilation Learn and Use a Makefile Keep it Simple Avoid Premature Optimization Learn and Use gdb http www pdl cmu edu 12 Jiri Simsa October 10 TESTING YOUR PROXY http www pdl cmu edu 13 Jiri Simsa October 10 Sequential Proxy Test Case Open three terminal windows 1 cd TINY make sudo tiny 80 2 cd PROXY make proxy 1234 3 curl x localhost 1234 http localhost html head title test title head body img align middle src godzilla gif Dave O Hallaron body html http www pdl cmu edu 14 Jiri Simsa October 10 Case Proxy Test Case Open three terminal windows 1 cd TINY make sudo tiny 80 2 cd PROXY make proxy 1234 3 curl x localhost 1234 HTTP LoCaLhOst html head title test title head body img align middle src godzilla gif Dave O Hallaron body html http www pdl cmu edu 15 Jiri Simsa October 10 Port Proxy Test Case Open three terminal windows 1 cd TINY make tiny 8080 2 cd PROXY make proxy 1234 3 curl x localhost 1234 http localhost 8080 html head title test title head body img align middle src godzilla gif Dave O Hallaron body html http www pdl cmu edu 16 Jiri Simsa October 10 Cache Proxy Test Case Open three terminal windows 1 2 3 3 3 cd TINY make sudo tiny 80 cd PROXY make proxy 1234 curl x localhost 1234 http localhost killall 9 tiny curl x localhost 1234 http localhost html head title test title head body img align middle src godzilla gif http www pdl cmu edu 17 Jiri Simsa October 10 Concurrent Proxy Test Case Open three terminal windows 1 2 3 3 cd TINY make sudo tiny 80 cd PROXY make proxy 1234 export CMD curl x localhost 1234 http localhost CMD CMD Which request will get serviced 1st Will the 2nd request be serviced from cache Will a non reentrant function be called concurrently How to try out all possible cases http www pdl cmu edu 18 Jiri Simsa October 10 USING DBUG http www pdl cmu edu 19 Jiri Simsa October 10 dBug Concurrent gdb and more Systematic Testing of Concurrent Systems Given a concurrent system and a test dBug systematically explores all possible ways in which the test could have executed dBug searches for deadlocks data races and assertion violations http www pdl cmu edu 20 Jiri Simsa October 10
View Full Document