Hello and welcome to e health forum.
The symptoms of pain, that arise during a specific activity, and not while doing others indicate, a wrong position while on the toilet, resulting in muscle spasm.
Other conditions, which need to be ruled out include any pathology in the joints of the leg, infections within the leg bones, or muscle weakness.
For a proper diagnosis, you may need to consult with a doctor for a complete examination and specific treatment.