Download presentation
Presentation is loading. Please wait.
1
15-213: How to dBug your (threaded) proxy Jiří Šimša PARALLEL DATA LABORATORY Carnegie Mellon University
2
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 Jiri Simsa © October 10http://www.pdl.cmu.edu/2
3
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 Jiri Simsa © October 10http://www.pdl.cmu.edu/3
4
GOOD PROGRAMMING PRACTICES Jiri Simsa © October 10http://www.pdl.cmu.edu/4
5
1. Know Your Library Calls... pthread_mutex_lock(&mutex); if (cache_empty()) { pthread_mutex_unlock(&mutex);... }... pthread_mutex_unlock(&mutex);... Jiri Simsa © October 10http://www.pdl.cmu.edu/5 Spending time reading a man page will dispel your misconceptions and save you hours of frustration.
6
2. Check Return Values... ret = accept(sockfd,addr,addrlen); if (ret == -1) { perror(“Accept failed.\n”); } else {... }... assert(pthread_mutex_lock(&mutex) == 0);... Jiri Simsa © October 10http://www.pdl.cmu.edu/6 Handling error cases correctly will facilitate identification of program failure root cause.
7
3. Use Conditional Compilation void * request_handler(void *args) { #ifdef DEBUG printf(“Entering %s”, __FUNCTION__); #endif... } $ gcc –DDEBUG... Jiri Simsa © October 10http://www.pdl.cmu.edu/7 Spending time including informative messages will greatly improve your debugging efficiency.
8
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: proxy.c csapp.c $(CC) -o $@ $(CFLAGS) -DDEBUG $(LDFLAGS) $^ Jiri Simsa © October 10http://www.pdl.cmu.edu/8 Spending time learning how to use a Makefile will save you hours of repetitive and dull work.
9
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”); } } Jiri Simsa © October 10http://www.pdl.cmu.edu/9 Start with the simplest approach possible. Often getting the simplest design right will be enough.
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... } Jiri Simsa © October 10http://www.pdl.cmu.edu/10 Start with the simplest approach possible. Often getting the simplest design right will be enough.
11
7. Learn and Use gdb Jiri Simsa © October 10http://www.pdl.cmu.edu/11 $ 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.
12
Summary of Good Practices 1.Know Your Library Calls 2.Check Return Values 3.Use Conditional Compilation 4.Learn and Use a Makefile 5.Keep it Simple 6.Avoid Premature Optimization 7.Learn and Use gdb Jiri Simsa © October 10http://www.pdl.cmu.edu/12
13
TESTING YOUR PROXY Jiri Simsa © October 10http://www.pdl.cmu.edu/13
14
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 test Dave O'Hallaron Jiri Simsa © October 10http://www.pdl.cmu.edu/14
15
Case Proxy Test Case Jiri Simsa © October 10http://www.pdl.cmu.edu/15 Open three terminal windows 1:$ cd $TINY; make; sudo./tiny 80 2:$ cd $PROXY; make;./proxy 1234 3:$ curl –x localhost:1234 HTTP://LoCaLhOst test Dave O'Hallaron
16
Port Proxy Test Case Jiri Simsa © October 10http://www.pdl.cmu.edu/16 Open three terminal windows 1:$ cd $TINY; make;./tiny 8080 2:$ cd $PROXY; make;./proxy 1234 3:$ curl –x localhost:1234 http://localhost:8080 test Dave O'Hallaron
17
Cache Proxy Test Case Jiri Simsa © October 10http://www.pdl.cmu.edu/17 Open three terminal windows 1:$ cd $TINY; make; sudo./tiny 80 2:$ cd $PROXY; make;./proxy 1234 3:$ curl –x localhost:1234 http://localhost 3:$ killall -9 tiny 3:$ curl –x localhost:1234 http://localhost test...
18
Concurrent Proxy Test Case Open three terminal windows 1:$ cd $TINY; make; sudo./tiny 80 2:$ cd $PROXY; make;./proxy 1234 3:$ export CMD=“curl –x localhost:1234 http://localhost” 3:$ $CMD & $CMD Which request will get serviced 1 st ? Will the 2 nd request be serviced from cache? Will a non-reentrant function be called concurrently? How to try out all possible cases? Jiri Simsa © October 10http://www.pdl.cmu.edu/18
19
USING DBUG Jiri Simsa © October 10http://www.pdl.cmu.edu/19
20
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 Jiri Simsa © October 10http://www.pdl.cmu.edu/20
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.