What is the origin of the idiom "tearing hair out" or "pulling my hair out"?
OED has this pre-dating Shakespeare:
m. to tear (†rend) one's hair, i.e. as a symptom of passionate grief.
1548 Hall's Vnion: Henry IV f. xiiijv, This knight..sobbed, wept, and rent his heare.
1609 Shakespeare Troilus & Cressida iv. iii. 33 Teare my bright haire, & scratch my praised cheekes.
It is something which can actually be witnessed in more demonstrative cultures. Mourners can indeed pull their own hair.
As an indication of exasperation, however ("I was pulling my hair out trying to get the copier to work!") there seems to be rather less information — although Shyam's trichotillomania may well be related.
There is a specific name for the urge to pull out one's hair - trichotillomania. It is a disorder belonging to the obsessive compulsive spectrum and arises due to anxiety and stress, among other reasons. This idiom (and others like keep one's hair on) must have originated from this disorder.
It originates in the Bible in the Book of Nehemiah. The prophet Nehemiah pulled his hair out in response to the desecration of the holy Temple and at the abuse of the Sabbath.