TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Ask HN: How do you debug your code?

89 点作者 s4chin大约 9 年前
I have read a few methods of debugging code. What is the method that you use to efficiently do so?

71 条评论

dfan大约 9 年前
My techniques probably are somewhat due to having started 30 years ago.<p>I rarely enter an interactive debugger. I have TONS of logging statements I can toggle. I make program execution as deterministic and reproducible as possible (for example, all random numbers are generated from random number generators that are passed around). When something goes wrong, I turn on (or add) logging and run it again. Look for odd stuff in the log files. If it doesn&#x27;t make sense, add more logging. Repeat.<p>I worked on a pretty large videogame in the 90s where &#x2F;everything&#x2F; was reproducible from a &quot;recording&quot; of timestamped input that was automatically generated. The game crashed after half an hour of varied actions? No problem, just play the recording that was automatically saved and attached to the crash report. It was amazing how fast we fixed bugs that might otherwise take weeks to track down.
评论 #11384903 未加载
评论 #11384598 未加载
评论 #11388340 未加载
pmarreck大约 9 年前
Data point: The amount of time I&#x27;ve spent doing &quot;debugging work&quot; has gone down VASTLY since I&#x27;ve adopted TDD. Literally, every line of debug code you will ever write (whether it&#x27;s a println buried in the code or something done in a REPL) is a potential unit test assertion.<p>What is a debug statement, anyway? You&#x27;re checking the state at a point in the code. That&#x27;s exactly what a unit test assertion does... except that a unit test calls a single method&#x2F;function... meaning the code you&#x27;re trying to debug needs to be split up and written in a way that is easily unit-testable, with few, if any, dependencies that aren&#x27;t explicitly given to it... which makes it better code that is easier to reason about (and thus results in fewer bugs).<p>See where I&#x27;m going with this? TDD = say (mostly) goodbye to &quot;debugging&quot;
评论 #11384784 未加载
评论 #11387237 未加载
评论 #11395973 未加载
svec大约 9 年前
Oscilloscope, multimeter, GPIO toggles, logic analyzer, JTAG&#x2F;SWD-enabled debug hardware. (Hey, I&#x27;m an embedded software engineer).<p>Sometimes debug printfs out a UART or USB if my system lets me, sometimes I&#x27;ll create a log in memory if I can&#x27;t printf or if the issue is timing sensitive (which a lot of my work is).<p>Pen &amp; paper end up being very useful too - often writing out what I&#x27;m thinking helps me figure out what&#x27;s going wrong faster than poking around the code or circuit board.
评论 #11385102 未加载
评论 #11384713 未加载
skylark大约 9 年前
Good debugging is a lot like good science. You start with a firm understanding of the system, form a hypothesis about what could be wrong with it, test your hypothesis, and then repeat. The stronger your initial understanding of the system, the faster you can debug things.<p>As a Python&#x2F;JS developer a few print&#x2F;console.log statements are usually all it takes for me to figure out what&#x27;s wrong with something. For more thorny situations there&#x27;s always PDB&#x2F;chrome dev tools.<p>At the end of the day, the people who are the best at debugging things aren&#x27;t that way because of the tools they use. They&#x27;re the best because they can clearly visualize the system, how data is flowing through it, and where potential problems might arise. Intuition from dealing with other similar problems also helps.
评论 #11383382 未加载
评论 #11383454 未加载
评论 #11413525 未加载
评论 #11387644 未加载
评论 #11383694 未加载
DonaldFisk大约 9 年前
Most of my development is done in my own Lisp dialect, which is quite similar to Common Lisp. I use any of five different methods. (4) is the most interesting, and very useful. Does anyone else use it or something similar?<p>(1) Just add debug statements near where the bug is happening. These print a string, and the name and value of variables. Printed values in Lisp are human-readable, not just a pointer.<p>(2) Trace selected functions. This outputs the function name, arguments, and return value on function entry and exit.<p>(3) Use the virtual machine debugger. I can set breakpoints, display the stack, and trace VM instructions, but it&#x27;s most useful for printing out disassembled compiled code.<p>(4) Browse data structures in Firefox. While my Lisp is running, a web browser runs in another thread, and every symbol has its own URL. Data structures are displayed as HTML tables.<p>(5) Unit tests. I&#x27;ve used these to debug complex algorithms, e.g. for event handling, and type inference.
评论 #11387225 未加载
评论 #11384823 未加载
评论 #11384660 未加载
drinchev大约 9 年前
WebStorm has a pretty good debugger. Also if you choose to use other JetBrains product it will feel very familiar.<p>I&#x27;ve never read about specific methods on how to do this &quot;by the book&quot;. I&#x27;m usually doing ( for NodeJS ) :<p>1. Set a breakpoint inside my NodeJS 2-3 lines before the exception that I got.<p>2. Run debug mode<p>3. Do what I need in order to reach the breakpoint<p>4. Analyze the variables inside ( via watch ) or run code in that function ( via console )<p>Helps a lot more than `console.log(some_var_before_exception);` :D
评论 #11383464 未加载
blendo大约 9 年前
I start by asking, Did it used to work? If so, when did it last work? What changed? When? Does it work anywhere else? And most importantly, can you reliably recreate the bug?<p>Only after I&#x27;ve grappled with these questions will I move onto log analysis, printfs, the debugger, data fuzzing, etc.
calebm大约 9 年前
I can typically just add print statements and figure out the problem in less time than it would take to setup and attach a debugger. But occasionally, I will use the PyCharm debugger with Python. And even more occasionally, I&#x27;ll use an assembly level debugger (especially if I&#x27;m interested in the system calls, and it is not convenient to attach a Python debugger).
评论 #11386864 未加载
评论 #11390929 未加载
Alan01252大约 9 年前
I&#x27;m using debug logging ( that isn&#x27;t deleted ) more and more as I code. It&#x27;s useful not only for solving the current problem you&#x27;re experiencing but also helps the next person understand what the code is&#x2F;isn&#x27;t doing, adding to its self documenting nature.<p>Debuggers are great, but the knowledge gained by using them to solve a problem is completely lost once that close button has been pressed.<p>Also if I&#x27;m having to use a debugger to work out what&#x27;s going on, usually it&#x27;s a good sign my code is overly complicated...
leejo大约 9 年前
It depends on the nature of the bug. If it&#x27;s some behaviour that has changed then i try to isolate it to a test case, if one doesn&#x27;t already exist. I can then use git to automatically bisect to the offending commit which, nine times out of ten, makes it immediately clear where the bug is. The net result is the bug being fixed <i>and</i> a useful regression test to make sure the bug stays squashed. And i got the vcs to do most of the work for me.<p>If it&#x27;s something i think is trivial i&#x27;ll just use a few print statements. This is 90% of the time.<p>If i end up with too many print statements then i step into the debugger. Others scoff at debuggers, which is odd because they can be powerful tools. Maybe you only use it once every couple of months, but they can be very helpful. When you&#x27;re waist deep in the stack, or dealing with action at a distance, trying to fix something in poorly factored code, want to watch a variable, think there&#x27;s some weird timing issue, need to step down into libraries beyond your control, then debuggers can help.<p>Don&#x27;t think of the debugger as a debugger, think of it as a REPL. You just happen to be using the REPL with buggy code.
评论 #11384076 未加载
gravypod大约 9 年前
I use my eyes.<p>If I can narrow it down to what line, or even file, is throwing an error I just take a few minutes, read all the code and all the code of branching methods, and then can narrow it down to a single line.<p>From there it is actually developing a fix. As you mess around with more and more languages, you will notice that most compilers lend something far from a helping hand.<p>This only works, and I will stress this, for programs under 1 million lines. Past that mark, you need to do some extra steps.<p>When I debug one million line projects, I narrow it down to a file. I pull out the code from the file, and I mock all of the methods that file calls (This gets REALLY hard with networked code. Trust me). From this small subset, I slowly break the functionality of the external method until I resemble the error being made in the main project. From that I now know the method(s) that are actually causing the problem.<p>But, there is one thing that this makes an assumption about: your compiler is working.<p>Put blatantly, they&#x27;re crap.<p>Usually they won&#x27;t show you the correct file causing the error or they will not generate a helpful error. Runtime errors are even worse.<p>The best thing to do is avoid making the tricky errors. Make unit tests, using fuzzing tests, and well made documentation.<p>Documentation alone, that details all of the possible output and input states of the function will save you days on some bugs.<p>In Java, the @Nullable tag is a godsend. Use these features, they WILL help.<p>If you do tests, fuzzing, and documentation.<p>Using your brain and some things to make your brain&#x27;s job easier will make you faster at debugging then any debugger like your buds GDB&#x2F;DDD setup.
stcredzero大约 9 年前
I&#x27;ve done most of my debugging on dynamic languages, where you have a lot of power in the runtime, so my style is based on that. You can perform superpowered feats of uber debugging this way. These are generally also available on other environments, but the tools are less flexible, so they are much harder to pull off, much less inventing a new debugging method on the fly.<p>So imagine doing things like narrowing down execution to just before and just after your error, then taking snapshots of the runtime memory and diffing the objects. Or a conditional breakpoint that changes the class of a particular instance to a special debug class.<p>You can do many of the same things in compiled languages, I&#x27;ve since discovered, if you have a decent incremental compile set up, and you use some tactical thinking. But the environment always seems like it&#x27;s trying to get in your way. (As opposed to a good dynamic environment, which seems more like an eager golden retriever wanting to play more fetch.)
jtr1大约 9 年前
If all goes well, I work through a process like this. I reach for this tool when the bug in question seems to have its source in faulty logic, as opposed to say, resource consumption.<p>1. Reproduce the bug as consistently as possible.<p>2. Find a pivot for the bug. Whether this is a previous commit where the bug did not occur, or a piece of code that can be commented out to clear the bug, I need to find some kind of on&#x2F;off switch for the behavior.<p>3. I comment out code &#x2F; insert break points &#x2F; use git bisect to flip the switch on and off, performing a kind of binary search to narrow the scope of the issue until I have it down to one line or method.<p>4. Once the source is found, read the surrounding source code to gain context for the error and reason toward a solution.<p>Of course, this works best if the bug originates from a single source. Sometimes this is not case and there are multiple variables interacting to create the undesirable behavior. That’s when things get really fun :)
pcwalton大约 9 年前
In Rust, which I mainly work in these days: Either println using the Debug trait to pretty-print data structures or LLDB. Which is easier kind of depends on the situation, at least for me.
joeld42大约 9 年前
I still use a lot of printfs, but I make heavy use of the debugger. In general, my strategy for difficult bugs is to find a point A where you know the code is working correctly, find point B where you know it&#x27;s broken and can catch it in the debugger or log, and step forwards from A and backwards from B until you find the bug in the middle.<p>When debugging difficult, intermittent problems (e.g. non-repro crashes) my strategy is to keep a log of when it occurs, add lots of diagnostics and asserts around where I think the problem is, until hopefully I can catch it in the debugger or notice a pattern.<p>90% of the work of debugging is creating a quickly reproducible test case. Once you have that you can usually solve it.
gedrap大约 9 年前
Don&#x27;t see the top comments mentioning that, so I will chip in: I always, whenever possible, try to reproduce the bug in tests first, before launching debugger &#x2F; adding some statements, etc.<p>Being able to quickly reproduce the bug time and time again makes a big difference. Some permanent verification that it&#x27;s actually fixed (at least in the given case) at the end of the session is also nice and adds a lot when doing a major refactoring or something similar. Especially for bugs related to the domain specific requirements, rather than the technical ones.
评论 #11383521 未加载
halpme大约 9 年前
As a Java developer I rely heavily on using the debugger in Eclipse and using grep to search through files. I first try to have a solid understanding of what the program is supposed to do, reproduce the bug, and then step through the code with a debugger to understand why the bug is happening. Other times I just try to find where the bug is happening, and working backwards to the root of the cause of the problem just be reading the code. This works most of the time, but as a junior developer most of the issues I have to debug are not too complex.
physcab大约 9 年前
One very important &quot;soft skill&quot; aspect to debugging code is time and ego management. There are times when all I want to do is &quot;solve the problem&quot; and if it means losing 4 hours of sleep over it, my obsession will let it happen. But I&#x27;m starting to learn there should be thresholds before letting the problem go and asking for help. Sometimes the fastest way to solving a problem isn&#x27;t brute forcing it, but getting away from the problem, letting it simmer, or asking someone more experienced.
评论 #11384636 未加载
anarchy8大约 9 年前
I work with mainly Javascript. I like to log everything, so I rarely need to use debuggers. When I have to, the Chrome developer is all you need. You can even use it to debug Node stuff
epynonymous大约 9 年前
depends mostly on the type of issue, i don&#x27;t believe that there&#x27;s a one size fits all sort of solution. for timing sensitive issues or core dumps, it&#x27;s typically low level things like gdb, trace, etc. for memory issues, obviously tools like valgrind, etc help.<p>in the olden days when i used ide&#x27;s like visual studio or netbeans, i&#x27;d often times leverage their native debuggers to set watchpoints and step through code. but those days are over, now i mostly use interpreted languages like python, ruby, and compiled languages like golang (highly recommended). print statements are the way to go, especially if you&#x27;re writing server side code (restful apis, websockets, etc), you&#x27;ll want the log information as you won&#x27;t be able to attach a debugger to a production system.<p>just a random thought based on this topic, if debug&#x2F;log&#x2F;print statements were detailed enough, one could actually take a log file and write some code to parse that and transform into test cases in your favorite test framework, that may have some effect on saving time writing test cases. for production bugs, you could take the log output as the reproducer steps to generate test cases to cover this.<p>and i really liked the comment about tdd and more importantly unit testing, it&#x27;s critical and helps developers better organize their code.
评论 #11390995 未加载
forgotmypassw大约 9 年前
GDB and some fprintf calls cut it most of the time. The worst kind of bug is when the program segfaults into oblivion and works fine under GDB but in 99% of the cases that&#x27;s related to uninitialized memory, which might be a bit hard to find but is quite simple to fix (it&#x27;s the worst because it sometimes takes a while to figure out where you forgot to initialize variables when it&#x27;s unrelated to a new change and happened to just not occur during previous changes).
评论 #11383673 未加载
xkcd-sucks大约 9 年前
I copy-paste something different from stackexchange. If that doesn&#x27;t work in the first 10-20 tries I post a question describing my problem as abstractly as possible
Gibbon1大约 9 年前
I have a couple of things I do depending on the code I&#x27;m working on. With the firmware I work in debugging is tricky because normally stopping the world with gdb messes up the delicate dance going on. Also the processor only has two hardware break points which isn&#x27;t enough frankly.<p>However I do use gdb from the command line on occasion. Code I write is pretty heavy on global variable and with gdb you can poke about and see what they are. You can also use gdb to look at internal processor modules.<p>To get around the limits of not being able to use break points I have a command line interface built into the firmware, which I use to poke and prod for debugging. I&#x27;m dimly aware that almost no one else does this, but can&#x27;t for the life of me figure out how people get by without it.<p>I also have a critical error handler that can save information off to a no init section of memory and then reset, recover and then log the error via the serial port on startup and via the radio. This is useful because for instance I have that hooked into the bus fault interrupt, so I can pull the offending instructions address off the call stack. The binutils program addr2line.exe rats out the offending line of code about 99% of the time.<p>For timing related stuff I make heavy use of toggling port pins and watching what happens with an oscilloscope.<p>For networking stuff sometimes I use wireshark.<p>For C#&#x2F;.net development I use Visual Studio and logging either to a window or to a file. However I&#x27;ve noticed that when other programmers work on my code they immediately delete that stuff and switch to printing to &#x27;stderror&#x27;.
joegaudet大约 9 年前
IntelliJ has great debugging integration with JS &#x2F; PHP &#x2F; Scala (the tools I use currently).<p>Set a breakpoint in the code, refresh the browser, and all the variables in the scope will be annotated with their value at break time.<p>This is really what you&#x27;re after when you&#x27;re println debugging - it has the advantage of showing you everything in a minimally intrusive way which is helpful when you don&#x27;t know what you&#x27;re looking for exactly.
评论 #11383395 未加载
andreastt大约 9 年前
This is a great question! I tend to find my debugging practice differs depending on the language I’m using and the array of problems, but the recurring common denominator is printf.<p>In Python I mostly rely on print(f) debugging, especially when working on multiprocessing programs, which I do rather frequently. With multiprocessed Python, pdb is useless. pdb is great, but not for a multi-process program. Most of my issues are related to sub-optimal API documentation that fails to point out argument types, and find I do a lot of trial-and-error programming, for which printf’s are great. Occasionally I drop into `import pdb; pdb.set_trace()` to inspect objects or the REPL to try out ideas.<p>JavaScript is an interesting beast where, next to console.log, I find myself using linters a great deal. A linter isn’t as sophisticated as compile-time checks, but does a frightfully good job at most trivial mistakes that are hard to debug in runtime. I often find myself running the linter under watch(1) so it updates when I make changes.<p>In Perl and shell, which I use ever more infrequently, the equivalent of the printf debugging is the norm. The only language I have found the debugger to be the first choice has been Java.<p>With Rust, I find myself resorting to a debugger surprisingly seldom. Its type system and the safety checks in the compiler catches most of the mistakes I make.<p>I don’t do much C programming anymore, but if I did, I would be interested in using rr (<a href="http:&#x2F;&#x2F;rr-project.org&#x2F;" rel="nofollow">http:&#x2F;&#x2F;rr-project.org&#x2F;</a>), which allows you to record a run of the program and replay it _exactly_ as it was the first time, essentially letting you reproduce and fix race conditions.
tgreenhaw大约 9 年前
Debugging anything starts with the concept of repeatedly splitting the failing system in two. You check that the input is good and verify the output is bad. Then split the process in two. If things look good in the middle, split the second half and check. If things look bad in the middle, split the first half and check. Repeat this process until you have isolated the bad portion of the process to an indivisible step.
insanebits大约 9 年前
As I mostly work on PHP and JS will try to explain how my debugging session usually goes. Most of the bugs are easily reprodusable so for them I just go straight for the file and setup xdebug via PhpStorm. And that almost always gets fixed quickly since I have a good idea what went wrong.<p>But not all the bugs are easy to solve. The worst kind of bugs are hard&#x2F;impossible to reproduce. For them my approach is to suspected places where it could occur and add logging, and just wait until it occurs again(sometimes it takes weeks or even months). So I am trying to log how the data flows through the system by isolating the direction to look for.<p>For example: I&#x27;m yet to find cause of the mystery of the MS Word and TinyMCE, where sometimes it will not strip out microsoft&#x27;s formatting. It only occurs about once a month. I wrote a simple alert script which sends me an email when this happens and I can get to the person in seconds(most of the users are in the same office), and try to reproduce the exact moment when it occured on users computer.<p>My fix was just show an error asking users to repaste exact same text which then works as expected.
jaequery大约 9 年前
Doesn&#x27;t have to be rocket science really, (unless that is what you are doing). For web development, it&#x27;s code, refresh browser, repeat. With a tail -f on the application&#x2F;server logs usually does the trick.<p>But I think IDE&#x27;s can&#x27;t beat real-time debuggers, like console.log or Ruby&#x27;s bettererrors gem, having full real-time access to application logic&#x2F;code at the spot, you can&#x27;t beat that.
raviojha大约 9 年前
It always helps if you can see the code run line by line. For instance in Django, I use ipdb (import ipdb; ipdb.set_trace()) to check the state of variables and check where they are not what they were supposed to be. Similarly in JS, by adding break points. For compiled languages it has been a tough job, however its more or less the same.
cyphar大约 9 年前
Obviously the first step is to create a reproducible test case. But after that it becomes a bit more involved.<p>For most bugs I look at, I usually wish that Linux had DTrace. I can&#x27;t tell you how many weird bugs I&#x27;ve found where finding the solution would&#x27;ve been debugged in 20 minutes of DTracing. For example, I&#x27;m currently investigating a weird memory leak in Docker that actually appears to be a reference leak in the Go runtime. It took me several days to figure out it was a bug in the runtime (if I had DTrace I could&#x27;ve found symptoms of the issue much faster).<p>But in general, most of the bugs I find can be debugged with some print statements if I can correctly make an educated guess where the bug lies. For other sorts of issues, unit tests or creating minimal test cases works pretty well and for everything else I&#x27;ll jump into a debugger if it&#x27;s really necessary.
wmichelin大约 9 年前
For PHP: xdebug. For JavaScript: Chrome debug tools.<p>Logging doesn&#x27;t give you anywhere near the power the a good debugger does.
mikestew大约 9 年前
I think you&#x27;ve asked your question poorly, because it will very much depend on what it is you&#x27;re debugging. Obj-C for an iOS project? Xcode and its usable visual debugger. Good luck using that if you&#x27;re writing for embedded, though, or if you&#x27;re writing for a &quot;Made for iPhone&quot; accessory that uses your one-and-only USB debugging port, in which case you&#x27;ll want to hit the man pages for <i>printf</i>.<p>JavaScript? I dunno, do they even make debuggers for JS? (I&#x27;m being facetious; I rarely work with JS.) Whatever is used, I&#x27;ll bet it will involve something about a browser and a dev console.<p>Statically-typed language? Dynamic? Makes a difference.<p>Point is, 90% of whatever answers you receive here will not be well-suited to your particular situation.
howeyc大约 9 年前
Print statements, or logging. Using a debugger is an extremely rare occurance, once a year at most.
HankB99大约 9 年前
Relentless application of logic.<p>Probably says something about my coding practices that I&#x27;ve gotten good at it.
onetimePete大约 9 年前
Bisect and binary search. If the Module has been found that produces the error, I go to the fault-lines. These are: Data-Input and Filtering. States outside of the Set of accepted States (Anticipation-Error - the reason why every time you write else&#x2F;default for anything else then a error- that&#x27;s a error). Those points inside the code, where you switch between two brain language-models (arithmetic&#x2F;data-handling) (procedural&#x2F;object-orientated), (relational&#x2F; list-think), (value&#x2F;state-handling) etc.<p>If you do that right, you can skip the reading and go right for the beef. Breakpoint.<p>Last but not least - good architecture, worst heisenbug wont cost you as much as the smallest architecture error.
SyneRyder大约 9 年前
Like most of the other comments, I generally use printf &#x2F; MessageBox statements for most things.<p>But since I work on consumer desktop software, occasionally a customer will encounter a problem that I can&#x27;t replicate on my dev machine, and their bug description doesn&#x27;t help me locate the problem. In that case, I try to duplicate the customer&#x27;s system as much as possible: I have Parallels Desktop &amp; VM images of almost every version of Windows, and I can configure the amount of RAM. Sometimes it&#x27;s easier to ask the customer to run a special Debug build, but if they&#x27;re not very tech savvy, Parallels can often help me reproduce the bug myself.
andr大约 9 年前
Python&#x27;s pudb is extremely useful if you don&#x27;t use a full blown IDE. It gives you an ncurses-based GUI debugger inside any Python app.<p>Snapshot debuggers like Google Cloud Debugger are probably the way forward. Alas it doesn&#x27;t support Python 3 yet.
评论 #11383435 未加载
yekim大约 9 年前
Depends on what you&#x27;re debugging.<p>Desktop apps w&#x2F; C&#x2F;C++ -- IDE based debugger (Visual Studio, GDB w&#x2F; a front end, ADB) print &#x2F; logging statements<p>Embedded C -- IDE Debugger (Green Hills, ARM DS-5, IAR, Eclipse w&#x2F; remote GDB, etc) GPIO ports, logic analyzers, oscilloscopes, etc)<p>Apps -- ADT &#x2F; ADB, print &#x2F; logging statements<p>Python, bash, sh, .bat scripts -- print &#x2F; logging statements<p>As many others have mentioned, having a consistent bug reproduction methodology is vital, a strong mental model of the SW and its various components is important, and a willingness to dive deep and question assumptions is critical. Ie don&#x27;t always expect your compilers or various OSes to be infallible.
chewxy大约 9 年前
I use a lot of Golang. I write a logf() and a recoverFrom() function that only has a body in debug builds. Then I pepper the code that I want to debug with panic()s and logf()s. After it&#x27;s done, it goes into a test file.<p>I also output my code to .dot format to visualize the flow of data quite a bit. This is extremely useful in statemachine like stuff - I basically can create animated gifs (well, not really, it&#x27;s more of me pressing right on my image viewer) to watch how my programs execute.<p>Why not Delve? I do actually use Delve. Just not as much as logf()s and panic()s.<p>Hmm.. in retrospect, I also dump a lot of data (neural network weights for example) into csv for debugging
shruubi大约 9 年前
I&#x27;ve always found debugging primarily via logging statements strange, we have interactive debuggers which allow us to pause execution, analyze the current stack and monitor your variables to ensure correct values and people still feel that writing stuff to the console is the best way to debug?<p>That being said, I&#x27;m not trying to take anything away from log-based debugging, there have been many times when log-based debugging has saved my bacon, but it feels strange that there is almost an attitude of interactive debuggers being &quot;lesser&quot; in these comments.
评论 #11386188 未加载
tmaly大约 9 年前
I use vim as my editor, so I tend to use print statements.<p>I do use the perl debugger though when I am writing perl and when there is a need. The benefit to this debugger is that it comes built into the language.
alkonaut大约 9 年前
I try to figure out the error condition &quot;parent is null&quot; or &quot;order is placed but item count is zero&quot; or whatever the error condition might be.<p>Then just put a conditional breakpoint and wait for it to confirm the error and break. Once there I can probably reason backwards to an earlier state that led to the error condition such as &quot;an item must have been removed from a placed order&quot; so again a conditional breakpoint in the remove method with the condition that the order is already placed. Rinse repeat.
danieltillett大约 9 年前
Most bugs are pretty easy to find - what I would love to hear of is unusual ways of finding hard to reproduce bugs. The sort of bug that only shows up non-deterministically after the program has been running for 48 hours, bugs that come from third party closed source libraries, bugs that only occur on remote customer machines.<p>One thing I have found helpful is to compile the code using different compilers and on different platforms. A bug that is hard to reproduce on one OS can become deterministic on another OS.
评论 #11385870 未加载
krapp大约 9 年前
At the moment, I&#x27;m teaching myself Hack and how to use Vagrant, and I&#x27;m debugging through nano. It&#x27;s tedious and frustrating, but on the upside, it forces me to write extremely simple code just so I can keep debugging time (and the size of the errors) to a minimum.<p>I&#x27;ve also gotten into the habit of building SDL projects in Visual Studio to use the console so I can just dump whatever I want into it.<p>I&#x27;m probably an example of what not to do in many cases, but still get the job done.
twphonebillsoon大约 9 年前
For C++: Logging, gdb and valgrind
coherentpony大约 9 年前
I use gdb if I&#x27;m on linux, and I use<p><pre><code> std::cout &lt;&lt; &quot;got here&quot; &lt;&lt; std::endl; </code></pre> if I&#x27;m on OS X.<p>gdb on OS X is such a horror show.
callesgg大约 9 年前
I look at the error message and the Traceback if there is one, then i go to the code line in question.<p>If there is no Exception and there is som kind of logic bug. I search my way to the logic code with the issue by greping and reading.<p>When i have found the approximate location of the issue i will probably set up some var_dump&#x27;s or log.Println in the code to get a better understanding of what is happening.<p>After that it is usually a done deal.
heywire大约 9 年前
I work on a Windows-based point-of-sale product, so my time is spent inside Visual Studio. Most of the time I use the remote debugger attached to a virtual machine. For reproducing issues from production, good logging is key. From our logging we can easily retrace the steps that caused the issue. I also occasionally use memory dumps and network captures to help track down production issues.
nicolasiac大约 9 年前
Can anyone explain how should I go about writing unit tests for JavaScript? The code mostly creates the ui (i.e. create button and input controls dynamically) and then displays and updates data. We thought about using intern but I don&#x27;t really know what should the tests be about. The backend is already test driven so I don&#x27;t have to test that the data is actually saved.
评论 #11411377 未加载
raquo大约 9 年前
For particularly bad problems, after exhausting standard methods, I start writing a stackoverflow question or a help request to someone on my team. I barely ever have to send it, this just forces me to spell out an exhaustive list of things that I&#x27;ve tried (or forgot to try). Eventually I figure out what I&#x27;ve missed, and if not, I have a help request all ready to go!
评论 #11387250 未加载
przeor大约 9 年前
On frontend I use Redux so redux dev tool.<p>I&#x27;m interested about FalcorJS, does anyone use it ? I found it very interesting, check this out for more at: <a href="https:&#x2F;&#x2F;reactjs.co&#x2F;2016&#x2F;02&#x2F;03&#x2F;what-is-netflix-falcor-and-why-you-should-care&#x2F;" rel="nofollow">https:&#x2F;&#x2F;reactjs.co&#x2F;2016&#x2F;02&#x2F;03&#x2F;what-is-netflix-falcor-and-why...</a>
sn9大约 9 年前
Udacity has a course on [Software Debugging](<a href="https:&#x2F;&#x2F;www.udacity.com&#x2F;course&#x2F;software-debugging--cs259" rel="nofollow">https:&#x2F;&#x2F;www.udacity.com&#x2F;course&#x2F;software-debugging--cs259</a>) taught by Andreas Zeller, the original author of DDD.<p>Anyone who&#x27;d like to learn a more systematic debugging process should take it.
larrik大约 9 年前
As a python dev, I either debug it in PyCharm, or I run it in the shell (especially if its a situation only in production).
kstenerud大约 9 年前
I use whatever tools I can get my hands on. Debuggers, disassemblers, tracers, file snoopers, memory snoopers, datacomm snoopers, code isolation, in-circuit emulators, soft emulators, print statements, lamp toggles, jtag, breakout boards, even sound. If it&#x27;ll speed up my debugging session, I&#x27;ll use it.
japhyr大约 9 年前
I&#x27;m a hobbyist who&#x27;s slowly making the transition to professional work. Almost all of my work has been solo, and I&#x27;ve gotten away with using print statements for almost all of my debugging work. I work mostly in Python, and I&#x27;m going to start using the logging module in my next project.
radicalbyte大约 9 年前
Web: chrome dev tools.<p>Windows during development: Visual Studio.<p>Windows prod: DebugDiag to get the dumps, combo of VS, DebugDiag and the various native debuggers for analysis. Dumps created by the user or the system are also input.<p>Windows event tracing is also absolutely fantastic IF you have it.
oxplot大约 9 年前
I follow the data. If a network&#x2F;USB&#x2F;Bluetooth program, I fire up wireshark. Text&#x2F;binary outputting programs, hexdump. Looking at a single piece of invalid output gives me a lot more insight into the problems a lot faster.
catwell大约 9 年前
Print statements and assertions. I rarely use a debugger, except coupled with something like Valgrind (&quot;drop me into GDB if I use uninitialised memory&quot;) or to examine a core dump generated by a failed assertion.
tragic大约 9 年前
Cowboy-style - puts&#x2F;print&#x2F;console.log all over the place.<p>I feel no shame.
random_coder大约 9 年前
I mostly write C++ code on Linux systems. I use ddd for interactive debugging in bigger projects. For throwaway code(C++ &amp; Python), I just use print statements.
aerovistae大约 9 年前
Like the other commenter, I mainly use JS. The Chrome Development Tools are all I need. Breakpoints, conditional breakpoints, and walking through the call stack are the main tools. Adding watch expressions is pretty handy sometimes so I can quickly run through a loop with a breakpoint, just hitting continue repeatedly, and watch the values change while just hitting F8.<p>Next most important thing is the network requests tab-- seeing what data is coming back from the server, if any, is indispensable.<p>If I&#x27;m debugging minified code that we haven&#x27;t set up source maps for yet, I&#x27;ll find a string literal in the source to match up the minified code to unminified code so I can see what I&#x27;m doing anyway by looking back and forth.<p>When I have to reproduce a bug, I often use the FormFiller extension for Chrome to quickly navigate our forms without having to fill them out.<p>I use EditThisCookie (another Chrome extension) to modify or view the cookie as I work, or to delete it to start a session from scratch. I don&#x27;t like Incognito mode because I don&#x27;t have my extensions and it doesn&#x27;t maintain breakpoints when the tab is closed and reopened.<p>With regards to the call stack, being able to black-box certain scripts is awesome. You can right click a script in the sources explorer on the left side of the DevTools and black-box it, which will stop it showing up in the call stack. No more jQuery &#x2F; Angular &#x2F; Underscore calls cluttering my callstack!<p>What else...whenever I&#x27;m debugging CSS I always just do it in DevTools so I can see changes on the fly to figure out the problem.<p>I also used to use the handy &quot;debugger&quot; statement now and then, although I use it less and less these days since it&#x27;s the same as a breakpoint but takes slightly more effort. Mostly only use it when I already have the code open in my editor and don&#x27;t feel like manually finding that point in execution in the DevTools....it&#x27;s kind of like &quot;go-to this line.&quot;<p>Ctrl+P in sources of DevTools allows fuzzy search among files. Which is awesome.<p>There have been times I&#x27;ve used the XHR breakpoints, Event Listener breakpoints, and DOM breakpoints, but it&#x27;s really rare for me. Definitely though there are cases where I&#x27;m not sure where an event is originating from and these have very much come in handy at those times. Underneath those on the right of the sources you can also see a total list of all active event listeners, which is also nice.<p>I&#x27;ll add more thoughts if I think of anything else...I guess I&#x27;m mostly talking about my tools here. With regards to my thought process, that&#x27;s more complex...hmm. I guess I try to figure out what the desired behavior is, try to see what the actual behavior is and how consistent it is, then see if I can find the code that controls that part of the application. If I don&#x27;t already know, I Inspect Element on the page, find an element id or something, then look it up in our code and follow the trail to whatever&#x27;s controlling the page and relevant behavior. From there it&#x27;s just careful examination of the logic to find the flaw, using all the tools above.
评论 #11383567 未加载
评论 #11388183 未加载
评论 #11383339 未加载
emartinelli大约 9 年前
IntelliJ debugger for Java, VS debugger for C#, print for Python and Perl, and console.log and Chrome Dev Tools for JS.
cyberpunk大约 9 年前
As an operations dude (or, at least, a recovering one) I&#x27;ve spent much of my professional life fighting post-deploy issues at the coal face, which almost always involves (when it&#x27;s an app issue) digging into the code; even when we do that though, an important bit is observing what the boxes are doing in real time (we&#x27;re not all dumb &quot;INSTALL RPM AND RESTART&quot; guys!) so we can explain impact and mitigate.<p>Usually, my goal then is to either:<p>1) Find a configuration &#x2F; infra issue we can solve (best outcome for everyone) 2) Give the most info to dev to enable a code fix, and roll back&#x2F;mitigate in the interim.<p>In the last few years, people have paid me lots of money to build these really cool ELK or Splunk log chewing systems for them, which I have to admit, are utterly useless to me. There are really great monitoring tools which give me historical graphs of stuff I usually don&#x27;t care about too.. but... I, and most of the folks I run with don&#x27;t really reach for these tools when we hit a prod issue as the first resort.<p>Lets say, hypothetically, a customer of mine has an issue where some users are getting timeouts out on some API or another. We got alerted through some monitoring or whatever, and so we start taking a look.<p>First step, for me is <i>always</i> login a webserver at random (or all of them) and look at the obvious. Logs. Errors. dmesg. IO. Mem. Processes. The pretty graph ELK tools can tell me this info, but what I want to look at next is easier to jump to when I&#x27;m already there, than trying to locate IOWAIT in something like splunk.<p>All looks good on the web servers. Ok. Lets check out the dbs in one term and the apps in another. You follow the request though the infra. Splunk or ELK can tell me one of my apps is eating 25,000 FDs but then what? I need to login anyway. Next on the list are tools like strace&#x2F;truss, iostat, netstat and so on which will immediately tell you if it&#x27;s an infra&#x2F;load&#x2F;config issue, and we go from there. Down into the rabbit hole.<p>The point I&#x27;m trying to make is; for me at least, the tools we&#x27;re deploying and being paid well to deploy now like dataloop, newrelic, splunk and so on are actually useless for solving real-time prod issues (for me personally, and my crew, at least) because they only expose a very small amount of info, and almost regardless of the issue I&#x27;ll need to be on the box looking at something unique to the problem to either explain the impact of it or to mitigate it.<p>As I said though, I&#x27;m a recovering ops person and I&#x27;m doing dev these days. I still tend to use print statements when I hit a bug; although since I&#x27;m now mostly doing Erlang, bugs are rare and usually trivial to track down.
tbking大约 9 年前
console.log and dev tools all the way!
pcestrada大约 9 年前
Log the hell out of the code.
namuol大约 9 年前
The only advice I have regarding debugging:<p>Interrogate every assumption you make.
eecks大约 9 年前
For Java, eclipse
Mizza大约 9 年前
import pdb; pdb.set_trace()<p>Or the equivalent for whatever language I&#x27;m using.
lirinawi大约 9 年前
Thank you
Kenji大约 9 年前
- If I know what&#x27;s the problem just by looking at the bug, I go fix it.<p>- If I do not know what&#x27;s the problem, I do everything in my power to reproduce the bug and maybe write a test (as small as possible) that triggers the bug. I enable logging or write a special log function to track relevant state in case the bug is rare.<p>- Once I know what triggers the bug, I should know the general direction of where in the code it is. I start printing various variables and parameters in case it&#x27;s a low-hanging fruit like wrong sign or stuff like that.<p>- If I do not succeed with that, I comment out half of the code and look if the bug persists. If it does, then I know it&#x27;s in the other half. If it does not, then I know it&#x27;s in this half. I proceed with this binary search until I am down to 1 statement, which takes a logarithmic amount of steps. I found the bug. I fix it. (This does not work if the bug is in two places or if the bug is a bad memory operation that triggers a bug later)<p>- Real debuggers like valgrind are rarely necessary if you&#x27;re familiar enough with the program. In fact, unless you&#x27;re dealing with the hairiest of hairy memory problems and heisenbugs, you probably do not need a debugger at all. Debuggers are useful to debug e.g. generated assembly when you write a compiler.
评论 #11384838 未加载
lcfcjs大约 9 年前
This is a great question and I am curious as to how others do it as well. I primarily code in nodejs and usually I just add console logs to give me visibility, however I feel like this is primitive and time consuming.<p>I have played around with node-inspector, but I have found that it&#x27;s awfully slow, particularly with really large arrays or objects. So I eventually just abandoned it. It seems like a good idea, and might be worth revisiting in the future.