Jurassic cockroaches ate lots of dinosaur dung. Scientists have actually found fossilized cockroach feces that contain bits of wood that had already been partially digested by dinosaurs.
Termites, social insects famed for wood-eating ability, are the direct descendants of cockroaches. But termites don’t exactly digest wood themselves. Instead, microorganisms in their guts break it down for them. They keep their guts full of essential microbes by (ahem) eating one another’s feces.
Dinosaurs are known to have eaten rotting wood that was already broken down by fungi. They processed their food for a long time, churning it in their stomachs where they also kept collections of microbes that did much of the digestion for them. Some of these microbes and fungi came out in their feces. Young dinosaurs probably got their own digestive microbes by eating adult dinosaur feces.
Cockroaches that ate the feces would have also eaten those microbes. After millions of years, those cockroaches best able to incorporate dinosaur wood-digesting microbes into their own personal microbiomes would have thrived, at the expense of less successful roaches; the successful ones thus evolved into termites.