devanarse los sesos

Etymology
lit. 'to coil the brain'.

Verb

 * 1)  to rack one's brains