I doubt it's your stomach because you didn't mention what you eat that might cause it. Try a good fiber. A good one is Fibercon from your drugstore or Walmart. Safe enough to be taken every night. Drink plenty of water all day long, and if pain continues after a few days, consider a colon cleanse. You can find a good one called Ultimate Cleanse at your local GNC. Water is very very important to have normal BMs, and most of us don't drink enough of it. If pain continues after all of these efforts, then consult your doc. Good luck.
The problem could be either. My doc said that even a problem in the bowels can cause symptoms throughout the entire digestion system. I'd see a good gastroenterologist to be sure. You might try paying attention to what you have eaten when this happens. I have discovered that if I eat garlic, peppers, pineapple, tumeric and a few others, my body reacts severely and almost immediately.