What is the exact mathematical relationship between the p-values and t-stat/degrees of freedom? Let's say, for whatever ungodly reason, I want to precisely calculate p-values by hand, without looking up tables for approximate values.
What is the exact specific formula for arriving at a p-value, given a t-stat and degrees of freedom?

Suppose you have $n=40$ observations sampled at
random from $\mathsf{Norm}(\mu=50, \sigma=10).$
You know data are normal, but do not know $\mu$ or $\sigma.$
Sampling in R and data summary:
set.seed(2022)
x = rnorm(40, 50, 10)
summary(x);  sd(x)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  20.99   43.05   50.20   49.29   56.75   62.12 
[1] 8.960198   # sample SD

You use a t test in R to test $H_0: \mu = 52$ against
$H_a: \mu < 52.$
t.test(x, mu=52, alt="less")

        One Sample t-test

data:  x
t = -1.9126, df = 39, p-value = 0.03158
alternative hypothesis: true mean is less than 52
95 percent confidence interval:
      -Inf 51.67738
sample estimates:
mean of x 
 49.29037 

You can get the 5% critical value $c = -1.685$ from a printed table of t distributions (using symmetry). So, because
$T= -1.9126 < c,$ you know that
the null hypothesis is rejected at the 5% level. Such
tables were originally computed (mainly) by numerical integration from the densities of Student's t distribution with various degrees of freedom. [Depending on where you find them, recent tables may be from statistical software, such as R or SAS.]
qt(0.05, 39)   # Inverse CDF of Student's t
[1] -1.684875

Ordinarily, you can bracket the P-value of such a one
sided test between tabled values of the tail probability,
here maybe between $0.025$ and $0.05,$ depending on
the level of detail of the table.
In R, you could compute the exact P-value as shown below.
The value $0.03158$ in the output of t.test is rounded to five places.
pt(-1.9126, 39)  # CDF of Student's t
[1] 0.03158175

The exact value above may be computed by R, using
a suitable rational approximation of the CDF of
$\mathsf{T}(\nu=39)$ or by numerical integration of
the PDF; I suspect that details may depend on the degrees of freedom. [See current R documentation.]
