To find why a thing behaves other than intended. Debugging is the disciplined narrowing of where the truth differs from the belief.
English de- (reversal, removal) + bug (a flaw in a mechanism, attested from the 1870s, well before the often-repeated moth story of the 1940s); to find why a thing behaves other than intended, narrowing where belief and truth diverge.
The bug it removes is an old word for a flaw, older than any insect ever found in a machine — debugging was never really about an animal, only about a defect worth naming plainly. What is found this way is always a gap between what was believed and what was actually there.