1*1c60b9acSAndroid Build Coastguard Worker# Tips on debugging with lws 2*1c60b9acSAndroid Build Coastguard Worker 3*1c60b9acSAndroid Build Coastguard Worker## Problem with the library, or your code? 4*1c60b9acSAndroid Build Coastguard Worker 5*1c60b9acSAndroid Build Coastguard WorkerBecause lws is only really used when already combined with user code, 6*1c60b9acSAndroid Build Coastguard Workerit can be a headache figuring out if the actual problem is inside lws 7*1c60b9acSAndroid Build Coastguard Workeror in the user code. 8*1c60b9acSAndroid Build Coastguard Worker 9*1c60b9acSAndroid Build Coastguard WorkerIf it's in lws, I would really like to solve it, but if it's in your 10*1c60b9acSAndroid Build Coastguard Workercode, that's your problem. Finding out which side it's on when it 11*1c60b9acSAndroid Build Coastguard Workerinvolves your code is also something you need to try to resolve. 12*1c60b9acSAndroid Build Coastguard Worker 13*1c60b9acSAndroid Build Coastguard WorkerThe minimal examples are useful because if they demonstrate the same 14*1c60b9acSAndroid Build Coastguard Workerproblem, it's something about your platform or lws itself, I have the 15*1c60b9acSAndroid Build Coastguard Workerminimal examples so I can test it and find out if it's your platform. 16*1c60b9acSAndroid Build Coastguard WorkerIf I can reproduce it, it's my problem. 17*1c60b9acSAndroid Build Coastguard Worker 18*1c60b9acSAndroid Build Coastguard Worker## Debug builds 19*1c60b9acSAndroid Build Coastguard Worker 20*1c60b9acSAndroid Build Coastguard WorkerWith cmake, build with `-DCMAKE_BUILD_TYPE=DEBUG` to build in extra 21*1c60b9acSAndroid Build Coastguard Workerlogging, and use a log level bitmap of eg, 1039 or 1151 to enable 22*1c60b9acSAndroid Build Coastguard Workerthe extra logs for print. 23*1c60b9acSAndroid Build Coastguard Worker 24*1c60b9acSAndroid Build Coastguard WorkerThe minimal examples take a -d xxx commandline parameter so you can 25*1c60b9acSAndroid Build Coastguard Workerselect the logging level when you run it. 26*1c60b9acSAndroid Build Coastguard Worker 27*1c60b9acSAndroid Build Coastguard WorkerThe extra logging can be very useful to understand the sequencing of 28*1c60b9acSAndroid Build Coastguard Workerproblematic actions. 29*1c60b9acSAndroid Build Coastguard Worker 30*1c60b9acSAndroid Build Coastguard Worker## Valgrind 31*1c60b9acSAndroid Build Coastguard Worker 32*1c60b9acSAndroid Build Coastguard WorkerIf your problems involve heap corruption or use-after-free, Valgrind 33*1c60b9acSAndroid Build Coastguard Workeris indespensible. It's simple to use, if you normally run `xxx`, just 34*1c60b9acSAndroid Build Coastguard Workerrun `valgrind xxx`. Your code will run slower, usually something 35*1c60b9acSAndroid Build Coastguard Workerlike 2 - 4x slower but it depends on the exact code. However you will 36*1c60b9acSAndroid Build Coastguard Workerget a backtrace as soon as there is some kind of misbehaviour of either 37*1c60b9acSAndroid Build Coastguard Workerlws or your code. 38*1c60b9acSAndroid Build Coastguard Worker 39*1c60b9acSAndroid Build Coastguard Workerlws is developed using valgrind routinely and strives to be completely 40*1c60b9acSAndroid Build Coastguard Workervalgrind-clean. So typically any problems reported are telling you 41*1c60b9acSAndroid Build Coastguard Workerabout problems in user code (or my bugs). 42*1c60b9acSAndroid Build Coastguard Worker 43*1c60b9acSAndroid Build Coastguard Worker## Traffic dumping 44*1c60b9acSAndroid Build Coastguard Worker 45*1c60b9acSAndroid Build Coastguard WorkerThe best place for dumping traffic, assuming you are linking against a 46*1c60b9acSAndroid Build Coastguard Workertls library, is `lws_ssl_capable_read()` and `lws_ssl_capable_write()` 47*1c60b9acSAndroid Build Coastguard Workerin either `./lib/tls/openssl/openssl-ssl.c` or 48*1c60b9acSAndroid Build Coastguard Worker`./lib/tls/mbedtls/mbedtls-ssl.c` according to which tls library you 49*1c60b9acSAndroid Build Coastguard Workerare using. There are default-`#if 0` sections in each function like 50*1c60b9acSAndroid Build Coastguard Worker 51*1c60b9acSAndroid Build Coastguard Worker``` 52*1c60b9acSAndroid Build Coastguard Worker#if 0 53*1c60b9acSAndroid Build Coastguard Worker /* 54*1c60b9acSAndroid Build Coastguard Worker * If using mbedtls type tls library, this is the earliest point for all 55*1c60b9acSAndroid Build Coastguard Worker * paths to dump what was received as decrypted data from the tls tunnel 56*1c60b9acSAndroid Build Coastguard Worker */ 57*1c60b9acSAndroid Build Coastguard Worker lwsl_notice("%s: len %d\n", __func__, len); 58*1c60b9acSAndroid Build Coastguard Worker lwsl_hexdump_notice(buf, len); 59*1c60b9acSAndroid Build Coastguard Worker#endif 60*1c60b9acSAndroid Build Coastguard Worker``` 61*1c60b9acSAndroid Build Coastguard Worker 62*1c60b9acSAndroid Build Coastguard WorkerEnable these to get hexdumps for all unencrypted data in both directions. 63*1c60b9acSAndroid Build Coastguard Worker 64